From c35f61818c8477c848089b4ba39dd151c3b232c8 Mon Sep 17 00:00:00 2001 From: arangelov Date: Wed, 14 Apr 2021 14:02:15 +0100 Subject: [PATCH] Allow provisioning state transition _PROFILE_FINALIZED->_USER_UNMANAGED This is needed to allow ManagedProvisioning reset the provisioning state when the work profile has been removed. Test: tests in the other CL Test: atest DevicePolicyManagerTest Bug: 185097355 Change-Id: I1dcbb73f9728e031716f66800882b6e94bea78c0 --- .../devicepolicy/DevicePolicyManagerService.java | 10 +++++++++- .../server/devicepolicy/DevicePolicyManagerTest.java | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index 66e640d666ed2..548e2065ac6a1 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -96,6 +96,7 @@ import static android.app.admin.DevicePolicyManager.PROVISIONING_RESULT_REMOVE_N import static android.app.admin.DevicePolicyManager.PROVISIONING_RESULT_SETTING_PROFILE_OWNER_FAILED; import static android.app.admin.DevicePolicyManager.PROVISIONING_RESULT_SET_DEVICE_OWNER_FAILED; import static android.app.admin.DevicePolicyManager.PROVISIONING_RESULT_STARTING_PROFILE_FAILED; +import static android.app.admin.DevicePolicyManager.STATE_USER_UNMANAGED; import static android.app.admin.DevicePolicyManager.WIPE_EUICC; import static android.app.admin.DevicePolicyManager.WIPE_EXTERNAL_STORAGE; import static android.app.admin.DevicePolicyManager.WIPE_RESET_PROTECTION_DATA; @@ -8749,7 +8750,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { final CallerIdentity caller = getCallerIdentity(); if (userHandle != mOwners.getDeviceOwnerUserId() && !mOwners.hasProfileOwner(userHandle) - && getManagedUserId(userHandle) == -1) { + && getManagedUserId(userHandle) == -1 + && newState != STATE_USER_UNMANAGED) { // No managed device, user or profile, so setting provisioning state makes no sense. throw new IllegalStateException("Not allowed to change provisioning state unless a " + "device or profile owner is set."); @@ -8812,6 +8814,12 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { case DevicePolicyManager.STATE_USER_SETUP_FINALIZED: // Cannot transition out of finalized. break; + case DevicePolicyManager.STATE_USER_PROFILE_FINALIZED: + // Should only move to an unmanaged state after removing the work profile. + if (newState == DevicePolicyManager.STATE_USER_UNMANAGED) { + return; + } + break; } // Didn't meet any of the accepted state transition checks above, throw appropriate error. 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 78e2dee7acf8c..2925861061e21 100644 --- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java @@ -3158,6 +3158,16 @@ public class DevicePolicyManagerTest extends DpmTestBase { DevicePolicyManager.STATE_USER_UNMANAGED)); } + @Test + public void testSetUserProvisioningState_profileFinalized_canTransitionToUserUnmanaged() + throws Exception { + setupProfileOwner(); + + exerciseUserProvisioningTransitions(CALLER_USER_HANDLE, + DevicePolicyManager.STATE_USER_PROFILE_FINALIZED, + DevicePolicyManager.STATE_USER_UNMANAGED); + } + @Test public void testSetUserProvisioningState_illegalTransitionToAnotherInProgressState() throws Exception {