diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java index b7f113609188d..0e1a5461840eb 100644 --- a/core/java/android/app/admin/DevicePolicyManager.java +++ b/core/java/android/app/admin/DevicePolicyManager.java @@ -9262,6 +9262,21 @@ public class DevicePolicyManager { return null; } + /** + * Checks if the specified component is the supervision component. + * @hide + */ + public boolean isSupervisionComponent(@NonNull ComponentName who) { + if (mService != null) { + try { + return getService().isSupervisionComponent(who); + } catch (RemoteException re) { + throw re.rethrowFromSystemServer(); + } + } + return false; + } + /** * @hide * @return the human readable name of the organisation associated with this DPM or {@code null} diff --git a/core/java/android/app/admin/IDevicePolicyManager.aidl b/core/java/android/app/admin/IDevicePolicyManager.aidl index 6c6a7ca708371..fea77703191e7 100644 --- a/core/java/android/app/admin/IDevicePolicyManager.aidl +++ b/core/java/android/app/admin/IDevicePolicyManager.aidl @@ -178,6 +178,7 @@ interface IDevicePolicyManager { boolean setProfileOwner(in ComponentName who, String ownerName, int userHandle); ComponentName getProfileOwnerAsUser(int userHandle); ComponentName getProfileOwnerOrDeviceOwnerSupervisionComponent(in UserHandle userHandle); + boolean isSupervisionComponent(in ComponentName who); String getProfileOwnerName(int userHandle); void setProfileEnabled(in ComponentName who); void setProfileName(in ComponentName who, String profileName); diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index cb78ad8345410..590de7b5e1191 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -9292,11 +9292,11 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { } // Check profile owner first as that is what most likely is set. - if (isSupervisionComponent(poComponent)) { + if (isSupervisionComponentLocked(poComponent)) { return poComponent; } - if (isSupervisionComponent(doComponent)) { + if (isSupervisionComponentLocked(doComponent)) { return doComponent; } @@ -9304,7 +9304,26 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { } } - private boolean isSupervisionComponent(@Nullable ComponentName who) { + /** + * Returns if the specified component is the supervision component. + */ + @Override + public boolean isSupervisionComponent(@NonNull ComponentName who) { + if (!mHasFeature) { + return false; + } + synchronized (getLockObject()) { + if (mConstants.USE_TEST_ADMIN_AS_SUPERVISION_COMPONENT) { + final CallerIdentity caller = getCallerIdentity(); + if (isAdminTestOnlyLocked(who, caller.getUserId())) { + return true; + } + } + return isSupervisionComponentLocked(who); + } + } + + private boolean isSupervisionComponentLocked(@Nullable ComponentName who) { if (who == null) { return false; } @@ -9508,7 +9527,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { "Cannot set the profile owner on a user which is already set-up"); if (!mIsWatch) { - if (!isSupervisionComponent(owner)) { + if (!isSupervisionComponentLocked(owner)) { throw new IllegalStateException("Unable to set non-default profile owner" + " post-setup " + owner); } @@ -12102,8 +12121,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { synchronized (getLockObject()) { // Allow testOnly admins to bypass supervision config requirement. Preconditions.checkCallAuthorization(isAdminTestOnlyLocked(who, caller.getUserId()) - || isSupervisionComponent(caller.getComponentName()), "Admin %s is not the " - + "default supervision component", caller.getComponentName()); + || isSupervisionComponentLocked(caller.getComponentName()), "Admin %s is not " + + "the default supervision component", caller.getComponentName()); DevicePolicyData policy = getUserData(caller.getUserId()); policy.mSecondaryLockscreenEnabled = enabled; saveSettingsLocked(caller.getUserId()); @@ -13004,7 +13023,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { return false; } - return isSupervisionComponent(admin.info.getComponent()); + return isSupervisionComponentLocked(admin.info.getComponent()); } } diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java index 8014d2502f48a..388170bd24cbd 100644 --- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java @@ -3360,9 +3360,11 @@ public class DevicePolicyManagerTest extends DpmTestBase { assertThat(dpmi.isActiveSupervisionApp(uid)).isTrue(); assertThat(dpm.getProfileOwnerOrDeviceOwnerSupervisionComponent(user)) .isEqualTo(admin1); + assertThat(dpm.isSupervisionComponent(admin1)).isTrue(); } else { assertThat(dpmi.isActiveSupervisionApp(uid)).isFalse(); assertThat(dpm.getProfileOwnerOrDeviceOwnerSupervisionComponent(user)).isNull(); + assertThat(dpm.isSupervisionComponent(admin1)).isFalse(); } }