From a5ec280b4a684a5298279a0c303b85e47cd8763e Mon Sep 17 00:00:00 2001 From: Edman Anjos Date: Thu, 26 Jan 2017 22:16:13 +0100 Subject: [PATCH 1/2] Fix delegation broadcast to send an ArrayList extra. Change DPMS to call Intent#putStringArrayListExtra to ensure the extra is sent as an array list of strings. Bug: 33099995 Test: cts-tradefed run cts-dev --module CtsDevicePolicyManagerTestCases --test com.android.cts.devicepolicy.MixedDeviceOwnerTest#testDelegation Change-Id: I1466fb457e34adbfb7704320c021210c1569f55f --- .../server/devicepolicy/DevicePolicyManagerService.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index d91b473096d7e..5b5e904abd1f9 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -4887,7 +4887,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { // Set the new delegate in user policies. final DevicePolicyData policy = getUserData(userId); if (!scopes.isEmpty()) { - policy.mDelegationMap.put(delegatePackage, new ArrayList<>(scopes)); + policy.mDelegationMap.put(delegatePackage, scopes); } else { // Remove any delegation info if the given scopes list is empty. policy.mDelegationMap.remove(delegatePackage); @@ -4896,12 +4896,13 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { // Notify delegate package of updates. final Intent intent = new Intent( DevicePolicyManager.ACTION_APPLICATION_DELEGATION_SCOPES_CHANGED); - // Only call receivers registered in the manifest (don’t wake app if not running). + // Only call receivers registered with Context#registerReceiver (don’t wake delegate). intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); // Limit components this intent resolves to to the delegate package. intent.setPackage(delegatePackage); // Include the list of delegated scopes as an extra. - intent.putExtra(DevicePolicyManager.EXTRA_DELEGATION_SCOPES, scopes.toArray()); + intent.putStringArrayListExtra(DevicePolicyManager.EXTRA_DELEGATION_SCOPES, + (ArrayList) scopes); // Send the broadcast. mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); From 9e62c3111e6d32008332689066cc96e924868180 Mon Sep 17 00:00:00 2001 From: Edman Anjos Date: Thu, 26 Jan 2017 22:22:58 +0100 Subject: [PATCH 2/2] Update in DPM delegation documentation. Add note on DPM#setDelegatedScopes documentation regarding the broadcast sent to the delegate package to notify its new scopes; and change the admin ComponentName annotation to @Nullable in DPM#getDelegatedScopes. Bug: 33099995 Test: cts-tradefed run cts-dev --module CtsDevicePolicyManagerTestCases --test com.android.cts.devicepolicy.MixedDeviceOwnerTest#testDelegation Change-Id: I28fe3a631c05a9e6b8dae766ce6c42881f2e3a00 --- .../android/app/admin/DevicePolicyManager.java | 15 ++++++++++----- .../devicepolicy/DevicePolicyManagerService.java | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java index 4be011e80a559..52d73869e3c62 100644 --- a/core/java/android/app/admin/DevicePolicyManager.java +++ b/core/java/android/app/admin/DevicePolicyManager.java @@ -1177,9 +1177,9 @@ public class DevicePolicyManager { = "android.app.action.SHOW_DEVICE_MONITORING_DIALOG"; /** - * Broadcast Action: Sent after application delegation scopes are changed. The new list of - * delegation scopes will be sent in an extra identified by the {@link #EXTRA_DELEGATION_SCOPES} - * key. + * Broadcast Action: Sent after application delegation scopes are changed. The new delegation + * scopes will be sent in an {@code ArrayList} extra identified by the + * {@link #EXTRA_DELEGATION_SCOPES} key. * *

Note: This is a protected intent that can only be sent by the system.

*/ @@ -1188,7 +1188,7 @@ public class DevicePolicyManager { "android.app.action.APPLICATION_DELEGATION_SCOPES_CHANGED"; /** - * A list of Strings corresponding to the delegation scopes given to an app in the + * An {@code ArrayList} corresponding to the delegation scopes given to an app in the * {@link #ACTION_APPLICATION_DELEGATION_SCOPES_CHANGED} broadcast. */ public static final String EXTRA_DELEGATION_SCOPES = "android.app.extra.DELEGATION_SCOPES"; @@ -3669,6 +3669,11 @@ public class DevicePolicyManager { * Granted APIs are determined by {@code scopes}, which is a list of the {@code DELEGATION_*} * constants. *

+ * A broadcast with the {@link #ACTION_APPLICATION_DELEGATION_SCOPES_CHANGED} action will be + * sent to the {@code delegatePackage} with its new scopes in an {@code ArrayList} extra + * under the {@link #EXTRA_DELEGATION_SCOPES} key. The broadcast is sent with the + * {@link Intent#FLAG_RECEIVER_REGISTERED_ONLY} flag. + *

* Delegated scopes are a per-user state. The delegated access is persistent until it is later * cleared by calling this method with an empty {@code scopes} list or uninstalling the * {@code delegatePackage}. @@ -3704,7 +3709,7 @@ public class DevicePolicyManager { * @throws SecurityException if {@code admin} is not a device or a profile owner. */ @NonNull - public List getDelegatedScopes(@NonNull ComponentName admin, + public List getDelegatedScopes(@Nullable ComponentName admin, @NonNull String delegatedPackage) { throwIfParentInstance("getDelegatedScopes"); if (mService != null) { diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index 5b5e904abd1f9..3c1d2744dd2ef 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -4887,7 +4887,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { // Set the new delegate in user policies. final DevicePolicyData policy = getUserData(userId); if (!scopes.isEmpty()) { - policy.mDelegationMap.put(delegatePackage, scopes); + policy.mDelegationMap.put(delegatePackage, new ArrayList<>(scopes)); } else { // Remove any delegation info if the given scopes list is empty. policy.mDelegationMap.remove(delegatePackage); @@ -4902,7 +4902,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { intent.setPackage(delegatePackage); // Include the list of delegated scopes as an extra. intent.putStringArrayListExtra(DevicePolicyManager.EXTRA_DELEGATION_SCOPES, - (ArrayList) scopes); + (ArrayList) scopes); // Send the broadcast. mContext.sendBroadcastAsUser(intent, UserHandle.of(userId));