Create a flag to allow starting managed user in background
Currently DPM.createAndManageUser does not start the user in the
background, leading to a potential race between user having access to
the secondary user and admin having time to push policies. To mitigate
this we're adding a flag that allows secondary users to be started in
background as part of the API. The admin can then apply policies before
switching to that user.
Bug: 64382185
Test: cts-tradefed run singleCommand cts -m DevicePolicyManager --test
com.android.cts.devicepolicy.DeviceOwnerTest#testCreateAndManageUser_StartUserInBackground
--abi arm64-v8a
Change-Id: Id6f6ab7584a249680c8554c21977cbb69a220332
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index d8da8c5..b0c9850 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -5980,6 +5980,26 @@
public static final int MAKE_USER_DEMO = 0x0004;
/**
+ * Flag used by {@link #createAndManageUser} to specificy that the newly created user should be
+ * started in the background as part of the user creation.
+ */
+ // TODO: Investigate solutions for the case where reboot happens before setup is completed.
+ public static final int START_USER_IN_BACKGROUND = 0x0008;
+
+ /**
+ * @hide
+ */
+ @IntDef(
+ flag = true,
+ prefix = {"SKIP_", "MAKE_USER_", "START_"},
+ value = {SKIP_SETUP_WIZARD, MAKE_USER_EPHEMERAL, MAKE_USER_DEMO,
+ START_USER_IN_BACKGROUND}
+ )
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface CreateAndManageUserFlags {}
+
+
+ /**
* Called by a device owner to create a user with the specified name and a given component of
* the calling package as profile owner. The UserHandle returned by this method should not be
* persisted as user handles are recycled as users are removed and created. If you need to
@@ -6010,7 +6030,7 @@
public @Nullable UserHandle createAndManageUser(@NonNull ComponentName admin,
@NonNull String name,
@NonNull ComponentName profileOwner, @Nullable PersistableBundle adminExtras,
- int flags) {
+ @CreateAndManageUserFlags int flags) {
throwIfParentInstance("createAndManageUser");
try {
return mService.createAndManageUser(admin, name, profileOwner, adminExtras, flags);