Loading services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +7 −3 Original line number Diff line number Diff line Loading @@ -18330,12 +18330,16 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { private void setDeviceOwnerTypeLocked(ComponentName admin, @DeviceOwnerType int deviceOwnerType) { String packageName = admin.getPackageName(); boolean isAdminTestOnly; verifyDeviceOwnerTypePreconditionsLocked(admin); Preconditions.checkState(!mOwners.isDeviceOwnerTypeSetForDeviceOwner(packageName), "The device owner type has already been set for " + packageName); mOwners.setDeviceOwnerType(packageName, deviceOwnerType); isAdminTestOnly = isAdminTestOnlyLocked(admin, mOwners.getDeviceOwnerUserId()); Preconditions.checkState(isAdminTestOnly || !mOwners.isDeviceOwnerTypeSetForDeviceOwner(packageName), "Test only admins can only set the device owner type more than once"); mOwners.setDeviceOwnerType(packageName, deviceOwnerType, isAdminTestOnly); } @Override services/devicepolicy/java/com/android/server/devicepolicy/Owners.java +5 −3 Original line number Diff line number Diff line Loading @@ -637,13 +637,15 @@ class Owners { } } void setDeviceOwnerType(String packageName, @DeviceOwnerType int deviceOwnerType) { void setDeviceOwnerType(String packageName, @DeviceOwnerType int deviceOwnerType, boolean isAdminTestOnly) { synchronized (mLock) { if (!hasDeviceOwner()) { Slog.e(TAG, "Attempting to set a device owner type when there is no device owner"); return; } else if (isDeviceOwnerTypeSetForDeviceOwner(packageName)) { Slog.e(TAG, "Device owner type for " + packageName + " has already been set"); } else if (!isAdminTestOnly && isDeviceOwnerTypeSetForDeviceOwner(packageName)) { Slog.e(TAG, "Setting the device owner type more than once is only allowed" + " for test only admins"); return; } Loading services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java +5 −15 Original line number Diff line number Diff line Loading @@ -7737,30 +7737,20 @@ public class DevicePolicyManagerTest extends DpmTestBase { dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); int returnedDeviceOwnerType = dpm.getDeviceOwnerType(admin1); assertThat(dpms.mOwners.hasDeviceOwner()).isTrue(); assertThat(returnedDeviceOwnerType).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); assertThat(dpm.getDeviceOwnerType(admin1)).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); initializeDpms(); returnedDeviceOwnerType = dpm.getDeviceOwnerType(admin1); assertThat(dpms.mOwners.hasDeviceOwner()).isTrue(); assertThat(returnedDeviceOwnerType).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); assertThat(dpm.getDeviceOwnerType(admin1)).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); } @Test public void testSetDeviceOwnerType_asDeviceOwner_throwsExceptionWhenSetDeviceOwnerTypeAgain() public void testSetDeviceOwnerType_asDeviceOwner_setDeviceOwnerTypeTwice_success() throws Exception { setDeviceOwner(); dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_DEFAULT); dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); int returnedDeviceOwnerType = dpm.getDeviceOwnerType(admin1); assertThat(dpms.mOwners.hasDeviceOwner()).isTrue(); assertThat(returnedDeviceOwnerType).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); assertThrows(IllegalStateException.class, () -> dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_DEFAULT)); assertThat(dpm.getDeviceOwnerType(admin1)).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); } @Test Loading services/tests/servicestests/src/com/android/server/devicepolicy/OwnersTest.java +5 −4 Original line number Diff line number Diff line Loading @@ -93,7 +93,7 @@ public class OwnersTest extends DpmTestBase { assertThat(owners.getProfileOwnerUserRestrictionsNeedsMigration(21)).isFalse(); owners.setDeviceOwnerType(owners.getDeviceOwnerPackageName(), DEVICE_OWNER_TYPE_FINANCED); DEVICE_OWNER_TYPE_FINANCED, /* isAdminTestOnly= */ false); // There is no device owner, so the default owner type should be returned. assertThat(owners.getDeviceOwnerType(owners.getDeviceOwnerPackageName())).isEqualTo( DEVICE_OWNER_TYPE_DEFAULT); Loading Loading @@ -367,7 +367,7 @@ public class OwnersTest extends DpmTestBase { owners.setDeviceOwnerUserRestrictionsMigrated(); owners.setDeviceOwnerType(owners.getDeviceOwnerPackageName(), DEVICE_OWNER_TYPE_FINANCED); DEVICE_OWNER_TYPE_FINANCED, /* isAdminTestOnly= */ false); assertThat(owners.getDeviceOwnerType(owners.getDeviceOwnerPackageName())).isEqualTo( DEVICE_OWNER_TYPE_FINANCED); Loading Loading @@ -399,7 +399,7 @@ public class OwnersTest extends DpmTestBase { owners.setProfileOwnerUserRestrictionsMigrated(11); owners.setDeviceOwnerType(owners.getDeviceOwnerPackageName(), DEVICE_OWNER_TYPE_DEFAULT); DEVICE_OWNER_TYPE_DEFAULT, /* isAdminTestOnly= */ false); // The previous device owner type should persist. assertThat(owners.getDeviceOwnerType(owners.getDeviceOwnerPackageName())).isEqualTo( DEVICE_OWNER_TYPE_FINANCED); Loading Loading @@ -585,7 +585,8 @@ public class OwnersTest extends DpmTestBase { assertThat(owners.getProfileOwnerFile(11).exists()).isTrue(); String previousDeviceOwnerPackageName = owners.getDeviceOwnerPackageName(); owners.setDeviceOwnerType(previousDeviceOwnerPackageName, DEVICE_OWNER_TYPE_FINANCED); owners.setDeviceOwnerType(previousDeviceOwnerPackageName, DEVICE_OWNER_TYPE_FINANCED, /* isAdminTestOnly= */ false); assertThat(owners.getDeviceOwnerType(previousDeviceOwnerPackageName)).isEqualTo( DEVICE_OWNER_TYPE_FINANCED); owners.setDeviceOwnerProtectedPackages( Loading Loading
services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +7 −3 Original line number Diff line number Diff line Loading @@ -18330,12 +18330,16 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { private void setDeviceOwnerTypeLocked(ComponentName admin, @DeviceOwnerType int deviceOwnerType) { String packageName = admin.getPackageName(); boolean isAdminTestOnly; verifyDeviceOwnerTypePreconditionsLocked(admin); Preconditions.checkState(!mOwners.isDeviceOwnerTypeSetForDeviceOwner(packageName), "The device owner type has already been set for " + packageName); mOwners.setDeviceOwnerType(packageName, deviceOwnerType); isAdminTestOnly = isAdminTestOnlyLocked(admin, mOwners.getDeviceOwnerUserId()); Preconditions.checkState(isAdminTestOnly || !mOwners.isDeviceOwnerTypeSetForDeviceOwner(packageName), "Test only admins can only set the device owner type more than once"); mOwners.setDeviceOwnerType(packageName, deviceOwnerType, isAdminTestOnly); } @Override
services/devicepolicy/java/com/android/server/devicepolicy/Owners.java +5 −3 Original line number Diff line number Diff line Loading @@ -637,13 +637,15 @@ class Owners { } } void setDeviceOwnerType(String packageName, @DeviceOwnerType int deviceOwnerType) { void setDeviceOwnerType(String packageName, @DeviceOwnerType int deviceOwnerType, boolean isAdminTestOnly) { synchronized (mLock) { if (!hasDeviceOwner()) { Slog.e(TAG, "Attempting to set a device owner type when there is no device owner"); return; } else if (isDeviceOwnerTypeSetForDeviceOwner(packageName)) { Slog.e(TAG, "Device owner type for " + packageName + " has already been set"); } else if (!isAdminTestOnly && isDeviceOwnerTypeSetForDeviceOwner(packageName)) { Slog.e(TAG, "Setting the device owner type more than once is only allowed" + " for test only admins"); return; } Loading
services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java +5 −15 Original line number Diff line number Diff line Loading @@ -7737,30 +7737,20 @@ public class DevicePolicyManagerTest extends DpmTestBase { dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); int returnedDeviceOwnerType = dpm.getDeviceOwnerType(admin1); assertThat(dpms.mOwners.hasDeviceOwner()).isTrue(); assertThat(returnedDeviceOwnerType).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); assertThat(dpm.getDeviceOwnerType(admin1)).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); initializeDpms(); returnedDeviceOwnerType = dpm.getDeviceOwnerType(admin1); assertThat(dpms.mOwners.hasDeviceOwner()).isTrue(); assertThat(returnedDeviceOwnerType).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); assertThat(dpm.getDeviceOwnerType(admin1)).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); } @Test public void testSetDeviceOwnerType_asDeviceOwner_throwsExceptionWhenSetDeviceOwnerTypeAgain() public void testSetDeviceOwnerType_asDeviceOwner_setDeviceOwnerTypeTwice_success() throws Exception { setDeviceOwner(); dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_DEFAULT); dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_FINANCED); int returnedDeviceOwnerType = dpm.getDeviceOwnerType(admin1); assertThat(dpms.mOwners.hasDeviceOwner()).isTrue(); assertThat(returnedDeviceOwnerType).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); assertThrows(IllegalStateException.class, () -> dpm.setDeviceOwnerType(admin1, DEVICE_OWNER_TYPE_DEFAULT)); assertThat(dpm.getDeviceOwnerType(admin1)).isEqualTo(DEVICE_OWNER_TYPE_FINANCED); } @Test Loading
services/tests/servicestests/src/com/android/server/devicepolicy/OwnersTest.java +5 −4 Original line number Diff line number Diff line Loading @@ -93,7 +93,7 @@ public class OwnersTest extends DpmTestBase { assertThat(owners.getProfileOwnerUserRestrictionsNeedsMigration(21)).isFalse(); owners.setDeviceOwnerType(owners.getDeviceOwnerPackageName(), DEVICE_OWNER_TYPE_FINANCED); DEVICE_OWNER_TYPE_FINANCED, /* isAdminTestOnly= */ false); // There is no device owner, so the default owner type should be returned. assertThat(owners.getDeviceOwnerType(owners.getDeviceOwnerPackageName())).isEqualTo( DEVICE_OWNER_TYPE_DEFAULT); Loading Loading @@ -367,7 +367,7 @@ public class OwnersTest extends DpmTestBase { owners.setDeviceOwnerUserRestrictionsMigrated(); owners.setDeviceOwnerType(owners.getDeviceOwnerPackageName(), DEVICE_OWNER_TYPE_FINANCED); DEVICE_OWNER_TYPE_FINANCED, /* isAdminTestOnly= */ false); assertThat(owners.getDeviceOwnerType(owners.getDeviceOwnerPackageName())).isEqualTo( DEVICE_OWNER_TYPE_FINANCED); Loading Loading @@ -399,7 +399,7 @@ public class OwnersTest extends DpmTestBase { owners.setProfileOwnerUserRestrictionsMigrated(11); owners.setDeviceOwnerType(owners.getDeviceOwnerPackageName(), DEVICE_OWNER_TYPE_DEFAULT); DEVICE_OWNER_TYPE_DEFAULT, /* isAdminTestOnly= */ false); // The previous device owner type should persist. assertThat(owners.getDeviceOwnerType(owners.getDeviceOwnerPackageName())).isEqualTo( DEVICE_OWNER_TYPE_FINANCED); Loading Loading @@ -585,7 +585,8 @@ public class OwnersTest extends DpmTestBase { assertThat(owners.getProfileOwnerFile(11).exists()).isTrue(); String previousDeviceOwnerPackageName = owners.getDeviceOwnerPackageName(); owners.setDeviceOwnerType(previousDeviceOwnerPackageName, DEVICE_OWNER_TYPE_FINANCED); owners.setDeviceOwnerType(previousDeviceOwnerPackageName, DEVICE_OWNER_TYPE_FINANCED, /* isAdminTestOnly= */ false); assertThat(owners.getDeviceOwnerType(previousDeviceOwnerPackageName)).isEqualTo( DEVICE_OWNER_TYPE_FINANCED); owners.setDeviceOwnerProtectedPackages( Loading