diff options
| author | 2018-01-17 18:26:03 +0000 | |
|---|---|---|
| committer | 2018-01-19 15:17:00 +0000 | |
| commit | 2c082aeeb7410e8341e57562b8e1bad8e88300ec (patch) | |
| tree | 33137b62656027676bcc7fc762b0f4b42632101f | |
| parent | cfd1b4b263da8fac7bda724fae0e42e618256871 (diff) | |
Notify device owner when user is started / stopped / switched
- Similar to onUserAdded / onUserRemoved, notify DPC when user is started / stopped / switched, as these operation is asynchronous.
Use cases:
1. As a asynchronous callback for startUserInBackground / stopUser / logoutUser / switchUser
2. For COMP, can be used to detect work mode turning on or off
Bug: 72092112
Test: com.android.cts.devicepolicy.DeviceOwnerTest#testCreateAndManageUser_StopOnStart
Test: com.android.cts.devicepolicy.DeviceOwnerTest#testCreateAndManageUser_LogoutOnStart
Change-Id: I6fa28711419053694131d7792f3e0d1d2e76ac29
| -rw-r--r-- | api/current.txt | 3 | ||||
| -rw-r--r-- | core/java/android/app/admin/DeviceAdminReceiver.java | 74 | ||||
| -rw-r--r-- | services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java | 15 |
3 files changed, 87 insertions, 5 deletions
diff --git a/api/current.txt b/api/current.txt index 5515b010a4c3..31038a4daf79 100644 --- a/api/current.txt +++ b/api/current.txt @@ -6346,6 +6346,9 @@ package android.app.admin { method public void onTransferOwnershipComplete(android.content.Context, android.os.PersistableBundle); method public void onUserAdded(android.content.Context, android.content.Intent, android.os.UserHandle); method public void onUserRemoved(android.content.Context, android.content.Intent, android.os.UserHandle); + method public void onUserStarted(android.content.Context, android.content.Intent, android.os.UserHandle); + method public void onUserStopped(android.content.Context, android.content.Intent, android.os.UserHandle); + method public void onUserSwitched(android.content.Context, android.content.Intent, android.os.UserHandle); field public static final java.lang.String ACTION_DEVICE_ADMIN_DISABLED = "android.app.action.DEVICE_ADMIN_DISABLED"; field public static final java.lang.String ACTION_DEVICE_ADMIN_DISABLE_REQUESTED = "android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED"; field public static final java.lang.String ACTION_DEVICE_ADMIN_ENABLED = "android.app.action.DEVICE_ADMIN_ENABLED"; diff --git a/core/java/android/app/admin/DeviceAdminReceiver.java b/core/java/android/app/admin/DeviceAdminReceiver.java index 302d52f1ae72..ffb3affb03b3 100644 --- a/core/java/android/app/admin/DeviceAdminReceiver.java +++ b/core/java/android/app/admin/DeviceAdminReceiver.java @@ -339,7 +339,7 @@ public class DeviceAdminReceiver extends BroadcastReceiver { /** * Broadcast action: notify the device owner that a user or profile has been removed. * Carries an extra {@link Intent#EXTRA_USER} that has the {@link UserHandle} of - * the new user. + * the user. * @hide */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) @@ -347,6 +347,36 @@ public class DeviceAdminReceiver extends BroadcastReceiver { public static final String ACTION_USER_REMOVED = "android.app.action.USER_REMOVED"; /** + * Broadcast action: notify the device owner that a user or profile has been started. + * Carries an extra {@link Intent#EXTRA_USER} that has the {@link UserHandle} of + * the user. + * @hide + */ + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) + @BroadcastBehavior(explicitOnly = true) + public static final String ACTION_USER_STARTED = "android.app.action.USER_STARTED"; + + /** + * Broadcast action: notify the device owner that a user or profile has been stopped. + * Carries an extra {@link Intent#EXTRA_USER} that has the {@link UserHandle} of + * the user. + * @hide + */ + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) + @BroadcastBehavior(explicitOnly = true) + public static final String ACTION_USER_STOPPED = "android.app.action.USER_STOPPED"; + + /** + * Broadcast action: notify the device owner that a user or profile has been switched to. + * Carries an extra {@link Intent#EXTRA_USER} that has the {@link UserHandle} of + * the user. + * @hide + */ + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) + @BroadcastBehavior(explicitOnly = true) + public static final String ACTION_USER_SWITCHED = "android.app.action.USER_SWITCHED"; + + /** * A string containing the SHA-256 hash of the bugreport file. * * @see #ACTION_BUGREPORT_SHARE @@ -914,6 +944,42 @@ public class DeviceAdminReceiver extends BroadcastReceiver { } /** + * Called when a user or profile is started. + * + * <p>This callback is only applicable to device owners. + * + * @param context The running context as per {@link #onReceive}. + * @param intent The received intent as per {@link #onReceive}. + * @param startedUser The {@link UserHandle} of the user that has just been started. + */ + public void onUserStarted(Context context, Intent intent, UserHandle startedUser) { + } + + /** + * Called when a user or profile is stopped. + * + * <p>This callback is only applicable to device owners. + * + * @param context The running context as per {@link #onReceive}. + * @param intent The received intent as per {@link #onReceive}. + * @param stoppedUser The {@link UserHandle} of the user that has just been stopped. + */ + public void onUserStopped(Context context, Intent intent, UserHandle stoppedUser) { + } + + /** + * Called when a user or profile is switched to. + * + * <p>This callback is only applicable to device owners. + * + * @param context The running context as per {@link #onReceive}. + * @param intent The received intent as per {@link #onReceive}. + * @param switchedUser The {@link UserHandle} of the user that has just been switched to. + */ + public void onUserSwitched(Context context, Intent intent, UserHandle switchedUser) { + } + + /** * Called on the newly assigned owner (either device owner or profile owner) when the ownership * transfer has completed successfully. * @@ -989,6 +1055,12 @@ public class DeviceAdminReceiver extends BroadcastReceiver { onUserAdded(context, intent, intent.getParcelableExtra(Intent.EXTRA_USER)); } else if (ACTION_USER_REMOVED.equals(action)) { onUserRemoved(context, intent, intent.getParcelableExtra(Intent.EXTRA_USER)); + } else if (ACTION_USER_STARTED.equals(action)) { + onUserStarted(context, intent, intent.getParcelableExtra(Intent.EXTRA_USER)); + } else if (ACTION_USER_STOPPED.equals(action)) { + onUserStopped(context, intent, intent.getParcelableExtra(Intent.EXTRA_USER)); + } else if (ACTION_USER_SWITCHED.equals(action)) { + onUserSwitched(context, intent, intent.getParcelableExtra(Intent.EXTRA_USER)); } else if (ACTION_TRANSFER_OWNERSHIP_COMPLETE.equals(action)) { PersistableBundle bundle = intent.getParcelableExtra(EXTRA_TRANSFER_OWNER_ADMIN_EXTRAS_BUNDLE); diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index cb4f5c1177df..3581a78d0809 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -648,14 +648,14 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { } if (Intent.ACTION_USER_ADDED.equals(action)) { - sendUserAddedOrRemovedCommand(DeviceAdminReceiver.ACTION_USER_ADDED, userHandle); + sendDeviceOwnerUserCommand(DeviceAdminReceiver.ACTION_USER_ADDED, userHandle); synchronized (DevicePolicyManagerService.this) { // It might take a while for the user to become affiliated. Make security // and network logging unavailable in the meantime. maybePauseDeviceWideLoggingLocked(); } } else if (Intent.ACTION_USER_REMOVED.equals(action)) { - sendUserAddedOrRemovedCommand(DeviceAdminReceiver.ACTION_USER_REMOVED, userHandle); + sendDeviceOwnerUserCommand(DeviceAdminReceiver.ACTION_USER_REMOVED, userHandle); synchronized (DevicePolicyManagerService.this) { // Check whether the user is affiliated, *before* removing its data. boolean isRemovedUserAffiliated = isUserAffiliatedWithDeviceLocked(userHandle); @@ -669,12 +669,17 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { } } } else if (Intent.ACTION_USER_STARTED.equals(action)) { + sendDeviceOwnerUserCommand(DeviceAdminReceiver.ACTION_USER_STARTED, userHandle); synchronized (DevicePolicyManagerService.this) { maybeSendAdminEnabledBroadcastLocked(userHandle); // Reset the policy data mUserData.remove(userHandle); } handlePackagesChanged(null /* check all admins */, userHandle); + } else if (Intent.ACTION_USER_STOPPED.equals(action)) { + sendDeviceOwnerUserCommand(DeviceAdminReceiver.ACTION_USER_STOPPED, userHandle); + } else if (Intent.ACTION_USER_SWITCHED.equals(action)) { + sendDeviceOwnerUserCommand(DeviceAdminReceiver.ACTION_USER_SWITCHED, userHandle); } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) { synchronized (DevicePolicyManagerService.this) { maybeSendAdminEnabledBroadcastLocked(userHandle); @@ -683,7 +688,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { handlePackagesChanged(null /* check all admins */, userHandle); } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action) || (Intent.ACTION_PACKAGE_ADDED.equals(action) - && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))) { + && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))) { handlePackagesChanged(intent.getData().getSchemeSpecificPart(), userHandle); } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action) && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { @@ -693,7 +698,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { } } - private void sendUserAddedOrRemovedCommand(String action, int userHandle) { + private void sendDeviceOwnerUserCommand(String action, int userHandle) { synchronized (DevicePolicyManagerService.this) { ActiveAdmin deviceOwner = getDeviceOwnerAdminLocked(); if (deviceOwner != null) { @@ -2041,6 +2046,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { filter.addAction(Intent.ACTION_USER_ADDED); filter.addAction(Intent.ACTION_USER_REMOVED); filter.addAction(Intent.ACTION_USER_STARTED); + filter.addAction(Intent.ACTION_USER_STOPPED); + filter.addAction(Intent.ACTION_USER_SWITCHED); filter.addAction(Intent.ACTION_USER_UNLOCKED); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, mHandler); |