diff options
author | Felipe Leme <felipeal@google.com> | 2020-02-21 11:05:49 -0800 |
---|---|---|
committer | Felipe Leme <felipeal@google.com> | 2020-02-24 16:44:52 +0000 |
commit | 339b7149afa1bc53f01f2ba39c536a04e6a53132 (patch) | |
tree | 70441a30155f1e97afee26dde9681874b7f2cf36 | |
parent | b629d69db047c40f2b110f2d73ad8ceb1b32ea68 (diff) |
Improved user callbacks on SystemService:
* Renamed:
onStartUser() -> onUserStarting()
onStopUser() -> onUserStopping()
onSwitchUser() -> onUserSwitching()
onUnlockUser() -> onUserUnlocking()
onCleanupUser() -> onUserStopped()
isSupportedUser() -> isUserSupported()
* Added:
onUserUnlocked()
Tested on automotive, which uses the callbacks to dump user metrics:
$ adb shell dumpsys car_service --user-metrics
adb shell dumpsys car_service --user-metrics
* User Metrics *
no users starting
no users stopping
Last 10 started users
2020-02-21T19:44:47.101 - user=0 start=2020-02-21 19:44:45.357 unlocking=+885ms unlocked=+1s122ms
2020-02-21T19:44:56.376 - user=10 start=2020-02-21 19:44:47.380 switch=+209ms unlocking=+8s262ms unlocked=+8s994ms
2020-02-21T20:00:08.792 - user=11 start=2020-02-21 20:00:04.634 switch(from 10)=+351ms unlocking=+4s44ms unlocked=+4s156ms
Last 10 stopped users
2020-02-21T20:00:48.323 - user=10 stop=2020-02-21 20:00:16.741 shutdown=+31s581ms
Test: m update-api
Test: atest BackupManagerServiceTest
Test: manual verification (see above)
Fixes: 150001654
Exempt-From-Owner-Approval: approved on master right before rvc split
Merged-In: I8774c57620030b9aa8e7ec039a6a758ad56db368
Change-Id: I8774c57620030b9aa8e7ec039a6a758ad56db368
(cherry picked from commit 58e7458ac7204992c5408eb2d6420d767afc4eb8)
11 files changed, 83 insertions, 57 deletions
diff --git a/services/api/current.txt b/services/api/current.txt index 4a0a0d8e5aef..8c90165eeb4a 100644 --- a/services/api/current.txt +++ b/services/api/current.txt @@ -49,14 +49,15 @@ package com.android.server { public abstract class SystemService { ctor public SystemService(@NonNull android.content.Context); method @NonNull public final android.content.Context getContext(); - method public boolean isSupportedUser(@NonNull com.android.server.SystemService.TargetUser); + method public boolean isUserSupported(@NonNull com.android.server.SystemService.TargetUser); method public void onBootPhase(int); - method public void onCleanupUser(@NonNull com.android.server.SystemService.TargetUser); method public abstract void onStart(); - method public void onStartUser(@NonNull com.android.server.SystemService.TargetUser); - method public void onStopUser(@NonNull com.android.server.SystemService.TargetUser); - method public void onSwitchUser(@Nullable com.android.server.SystemService.TargetUser, @NonNull com.android.server.SystemService.TargetUser); - method public void onUnlockUser(@NonNull com.android.server.SystemService.TargetUser); + method public void onUserStarting(@NonNull com.android.server.SystemService.TargetUser); + method public void onUserStopped(@NonNull com.android.server.SystemService.TargetUser); + method public void onUserStopping(@NonNull com.android.server.SystemService.TargetUser); + method public void onUserSwitching(@Nullable com.android.server.SystemService.TargetUser, @NonNull com.android.server.SystemService.TargetUser); + method public void onUserUnlocked(@NonNull com.android.server.SystemService.TargetUser); + method public void onUserUnlocking(@NonNull com.android.server.SystemService.TargetUser); method protected final void publishBinderService(@NonNull String, @NonNull android.os.IBinder); method protected final void publishBinderService(@NonNull String, @NonNull android.os.IBinder, boolean); field public static final int PHASE_ACTIVITY_MANAGER_READY = 550; // 0x226 diff --git a/services/autofill/java/com/android/server/autofill/AutofillManagerService.java b/services/autofill/java/com/android/server/autofill/AutofillManagerService.java index 5405fc74dfb6..e49c1ed47c93 100644 --- a/services/autofill/java/com/android/server/autofill/AutofillManagerService.java +++ b/services/autofill/java/com/android/server/autofill/AutofillManagerService.java @@ -331,7 +331,7 @@ public final class AutofillManagerService } @Override // from SystemService - public boolean isSupportedUser(TargetUser user) { + public boolean isUserSupported(TargetUser user) { return user.getUserInfo().isFull() || user.getUserInfo().isManagedProfile(); } diff --git a/services/contentcapture/java/com/android/server/contentcapture/ContentCaptureManagerService.java b/services/contentcapture/java/com/android/server/contentcapture/ContentCaptureManagerService.java index 9a33fc9548a0..5d2b9f359381 100644 --- a/services/contentcapture/java/com/android/server/contentcapture/ContentCaptureManagerService.java +++ b/services/contentcapture/java/com/android/server/contentcapture/ContentCaptureManagerService.java @@ -201,7 +201,7 @@ public final class ContentCaptureManagerService extends } @Override // from SystemService - public boolean isSupportedUser(TargetUser user) { + public boolean isUserSupported(TargetUser user) { return user.getUserInfo().isFull() || user.getUserInfo().isManagedProfile(); } diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java index d86b2230ee7a..b1cb1380e4da 100644 --- a/services/core/java/com/android/server/StorageManagerService.java +++ b/services/core/java/com/android/server/StorageManagerService.java @@ -279,7 +279,7 @@ class StorageManagerService extends IStorageManager.Stub } @Override - public void onStartUser(TargetUser user) { + public void onUserStarting(TargetUser user) { mStorageManagerService.snapshotAndMonitorLegacyStorageAppOp(user.getUserHandle()); } } diff --git a/services/core/java/com/android/server/SystemService.java b/services/core/java/com/android/server/SystemService.java index aabe98b3afa0..e5c05408f628 100644 --- a/services/core/java/com/android/server/SystemService.java +++ b/services/core/java/com/android/server/SystemService.java @@ -238,12 +238,12 @@ public abstract class SystemService { * <p>By default returns {@code true}, but subclasses should extend for optimization, if they * don't support some types (like headless system user). */ - public boolean isSupportedUser(@NonNull TargetUser user) { + public boolean isUserSupported(@NonNull TargetUser user) { return true; } /** - * Helper method used to dump which users are {@link #onStartUser(TargetUser) supported}. + * Helper method used to dump which users are {@link #onUserStarting(TargetUser) supported}. * * @hide */ @@ -264,7 +264,7 @@ public abstract class SystemService { } /** - * @deprecated subclasses should extend {@link #onStartUser(TargetUser)} instead + * @deprecated subclasses should extend {@link #onUserStarting(TargetUser)} instead * (which by default calls this method). * * @hide @@ -273,7 +273,7 @@ public abstract class SystemService { public void onStartUser(@UserIdInt int userId) {} /** - * @deprecated subclasses should extend {@link #onStartUser(TargetUser)} instead + * @deprecated subclasses should extend {@link #onUserStarting(TargetUser)} instead * (which by default calls this method). * * @hide @@ -287,17 +287,17 @@ public abstract class SystemService { * Called when a new user is starting, for system services to initialize any per-user * state they maintain for running users. * - * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports} + * <p>This method is only called when the service {@link #isUserSupported(TargetUser) supports} * this user. * * @param user target user */ - public void onStartUser(@NonNull TargetUser user) { + public void onUserStarting(@NonNull TargetUser user) { onStartUser(user.getUserInfo()); } /** - * @deprecated subclasses should extend {@link #onUnlockUser(TargetUser)} instead (which by + * @deprecated subclasses should extend {@link #onUserUnlocking(TargetUser)} instead (which by * default calls this method). * * @hide @@ -306,7 +306,7 @@ public abstract class SystemService { public void onUnlockUser(@UserIdInt int userId) {} /** - * @deprecated subclasses should extend {@link #onUnlockUser(TargetUser)} instead (which by + * @deprecated subclasses should extend {@link #onUserUnlocking(TargetUser)} instead (which by * default calls this method). * * @hide @@ -326,19 +326,30 @@ public abstract class SystemService { * the user will transition into the {@code STATE_RUNNING_UNLOCKED} state. * Code written inside system services should use * {@link UserManager#isUserUnlockingOrUnlocked(int)} to handle both of - * these states. + * these states, or use {@link #onUserUnlocked(TargetUser)} instead. * <p> - * This method is only called when the service {@link #isSupportedUser(TargetUser) supports} + * This method is only called when the service {@link #isUserSupported(TargetUser) supports} * this user. * * @param user target user */ - public void onUnlockUser(@NonNull TargetUser user) { + public void onUserUnlocking(@NonNull TargetUser user) { onUnlockUser(user.getUserInfo()); } /** - * @deprecated subclasses should extend {@link #onSwitchUser(TargetUser, TargetUser)} instead + * Called after an existing user is unlocked. + * + * <p>This method is only called when the service {@link #isUserSupported(TargetUser) supports} + * this user. + * + * @param user target user + */ + public void onUserUnlocked(@NonNull TargetUser user) { + } + + /** + * @deprecated subclasses should extend {@link #onUserSwitching(TargetUser, TargetUser)} instead * (which by default calls this method). * * @hide @@ -347,7 +358,7 @@ public abstract class SystemService { public void onSwitchUser(@UserIdInt int toUserId) {} /** - * @deprecated subclasses should extend {@link #onSwitchUser(TargetUser, TargetUser)} instead + * @deprecated subclasses should extend {@link #onUserSwitching(TargetUser, TargetUser)} instead * (which by default calls this method). * * @hide @@ -362,7 +373,7 @@ public abstract class SystemService { * special behavior for whichever user is currently in the foreground. This is called * before any application processes are aware of the new user. * - * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports} + * <p>This method is only called when the service {@link #isUserSupported(TargetUser) supports} * either of the users ({@code from} or {@code to}). * * <b>NOTE: </b> both {@code from} and {@code to} are "live" objects @@ -371,12 +382,12 @@ public abstract class SystemService { * @param from the user switching from * @param to the user switching to */ - public void onSwitchUser(@Nullable TargetUser from, @NonNull TargetUser to) { + public void onUserSwitching(@Nullable TargetUser from, @NonNull TargetUser to) { onSwitchUser((from == null ? null : from.getUserInfo()), to.getUserInfo()); } /** - * @deprecated subclasses should extend {@link #onStopUser(TargetUser)} instead + * @deprecated subclasses should extend {@link #onUserStopping(TargetUser)} instead * (which by default calls this method). * * @hide @@ -385,7 +396,7 @@ public abstract class SystemService { public void onStopUser(@UserIdInt int userId) {} /** - * @deprecated subclasses should extend {@link #onStopUser(TargetUser)} instead + * @deprecated subclasses should extend {@link #onUserStopping(TargetUser)} instead * (which by default calls this method). * * @hide @@ -402,19 +413,19 @@ public abstract class SystemService { * broadcast to the user; it is a good place to stop making use of any resources of that * user (such as binding to a service running in the user). * - * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports} + * <p>This method is only called when the service {@link #isUserSupported(TargetUser) supports} * this user. * * <p>NOTE: This is the last callback where the callee may access the target user's CE storage. * * @param user target user */ - public void onStopUser(@NonNull TargetUser user) { + public void onUserStopping(@NonNull TargetUser user) { onStopUser(user.getUserInfo()); } /** - * @deprecated subclasses should extend {@link #onCleanupUser(TargetUser)} instead (which by + * @deprecated subclasses should extend {@link #onUserStopped(TargetUser)} instead (which by * default calls this method). * * @hide @@ -423,7 +434,7 @@ public abstract class SystemService { public void onCleanupUser(@UserIdInt int userId) {} /** - * @deprecated subclasses should extend {@link #onCleanupUser(TargetUser)} instead (which by + * @deprecated subclasses should extend {@link #onUserStopped(TargetUser)} instead (which by * default calls this method). * * @hide @@ -434,20 +445,16 @@ public abstract class SystemService { } /** - * Called when an existing user is stopping, for system services to finalize any per-user - * state they maintain for running users. This is called after all application process - * teardown of the user is complete. + * Called after an existing user is stopped. * - * <p>This method is only called when the service {@link #isSupportedUser(TargetUser) supports} - * this user. + * <p>This is called after all application process teardown of the user is complete. * - * <p>NOTE: When this callback is called, the CE storage for the target user may not be - * accessible already. Use {@link #onStopUser(TargetUser)} instead if you need to access the CE - * storage. + * <p>This method is only called when the service {@link #isUserSupported(TargetUser) supports} + * this user. * * @param user target user */ - public void onCleanupUser(@NonNull TargetUser user) { + public void onUserStopped(@NonNull TargetUser user) { onCleanupUser(user.getUserInfo()); } diff --git a/services/core/java/com/android/server/SystemServiceManager.java b/services/core/java/com/android/server/SystemServiceManager.java index e7f78462e6ee..f16f6ef2e72d 100644 --- a/services/core/java/com/android/server/SystemServiceManager.java +++ b/services/core/java/com/android/server/SystemServiceManager.java @@ -51,7 +51,8 @@ public class SystemServiceManager { // Constants used on onUser(...) private static final String START = "Start"; - private static final String UNLOCK = "Unlock"; + private static final String UNLOCKING = "Unlocking"; + private static final String UNLOCKED = "Unlocked"; private static final String SWITCH = "Switch"; private static final String STOP = "Stop"; private static final String CLEANUP = "Cleanup"; @@ -260,7 +261,14 @@ public class SystemServiceManager { * Unlocks the given user. */ public void unlockUser(final @UserIdInt int userHandle) { - onUser(UNLOCK, userHandle); + onUser(UNLOCKING, userHandle); + } + + /** + * Called after the user was unlocked. + */ + public void onUserUnlocked(final @UserIdInt int userHandle) { + onUser(UNLOCKED, userHandle); } /** @@ -304,12 +312,12 @@ public class SystemServiceManager { for (int i = 0; i < serviceLen; i++) { final SystemService service = mServices.get(i); final String serviceName = service.getClass().getName(); - boolean supported = service.isSupportedUser(curUser); + boolean supported = service.isUserSupported(curUser); // Must check if either curUser or prevUser is supported (for example, if switching from // unsupported to supported, we still need to notify the services) if (!supported && prevUser != null) { - supported = service.isSupportedUser(prevUser); + supported = service.isUserSupported(prevUser); } if (!supported) { @@ -328,19 +336,22 @@ public class SystemServiceManager { try { switch (onWhat) { case SWITCH: - service.onSwitchUser(prevUser, curUser); + service.onUserSwitching(prevUser, curUser); break; case START: - service.onStartUser(curUser); + service.onUserStarting(curUser); + break; + case UNLOCKING: + service.onUserUnlocking(curUser); break; - case UNLOCK: - service.onUnlockUser(curUser); + case UNLOCKED: + service.onUserUnlocked(curUser); break; case STOP: - service.onStopUser(curUser); + service.onUserStopping(curUser); break; case CLEANUP: - service.onCleanupUser(curUser); + service.onUserStopped(curUser); break; default: throw new IllegalArgumentException(onWhat + " what?"); diff --git a/services/core/java/com/android/server/am/UserController.java b/services/core/java/com/android/server/am/UserController.java index fb48db4a5921..a7125b4a1151 100644 --- a/services/core/java/com/android/server/am/UserController.java +++ b/services/core/java/com/android/server/am/UserController.java @@ -146,6 +146,7 @@ class UserController implements Handler.Callback { static final int REPORT_USER_SWITCH_COMPLETE_MSG = 80; static final int USER_SWITCH_CALLBACKS_TIMEOUT_MSG = 90; static final int USER_UNLOCK_MSG = 100; + static final int USER_UNLOCKED_MSG = 105; static final int REPORT_LOCKED_BOOT_COMPLETE_MSG = 110; static final int START_USER_SWITCH_FG_MSG = 120; @@ -625,6 +626,9 @@ class UserController implements Handler.Callback { FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__FRAMEWORK_BOOT_COMPLETED, elapsedTimeMs); } + + mHandler.obtainMessage(USER_UNLOCKED_MSG, userId, 0).sendToTarget(); + final Intent bootIntent = new Intent(Intent.ACTION_BOOT_COMPLETED, null); bootIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); bootIntent.addFlags(Intent.FLAG_RECEIVER_NO_ABORT @@ -2366,6 +2370,9 @@ class UserController implements Handler.Callback { }); finishUserUnlocked((UserState) msg.obj); break; + case USER_UNLOCKED_MSG: + mInjector.getSystemServiceManager().onUserUnlocked(msg.arg1); + break; case USER_CURRENT_MSG: mInjector.batteryStatsServiceNoteEvent( BatteryStats.HistoryItem.EVENT_USER_FOREGROUND_FINISH, diff --git a/services/core/java/com/android/server/infra/AbstractMasterSystemService.java b/services/core/java/com/android/server/infra/AbstractMasterSystemService.java index 8206fef90ab7..2672f848f192 100644 --- a/services/core/java/com/android/server/infra/AbstractMasterSystemService.java +++ b/services/core/java/com/android/server/infra/AbstractMasterSystemService.java @@ -713,7 +713,7 @@ public abstract class AbstractMasterSystemService<M extends AbstractMasterSystem /** * Gets a list of all supported users (i.e., those that pass the - * {@link #isSupportedUser(TargetUser)}check). + * {@link #isUserSupported(TargetUser)}check). */ @NonNull protected List<UserInfo> getSupportedUsers() { @@ -722,7 +722,7 @@ public abstract class AbstractMasterSystemService<M extends AbstractMasterSystem final List<UserInfo> supportedUsers = new ArrayList<>(size); for (int i = 0; i < size; i++) { final UserInfo userInfo = allUsers[i]; - if (isSupportedUser(new TargetUser(userInfo))) { + if (isUserSupported(new TargetUser(userInfo))) { supportedUsers.add(userInfo); } } diff --git a/services/people/java/com/android/server/people/PeopleService.java b/services/people/java/com/android/server/people/PeopleService.java index 2499614a3738..5d7f4e9e1c9d 100644 --- a/services/people/java/com/android/server/people/PeopleService.java +++ b/services/people/java/com/android/server/people/PeopleService.java @@ -71,12 +71,12 @@ public class PeopleService extends SystemService { } @Override - public void onUnlockUser(@NonNull TargetUser targetUser) { + public void onUserUnlocking(@NonNull TargetUser targetUser) { mDataManager.onUserUnlocked(targetUser.getUserIdentifier()); } @Override - public void onStopUser(@NonNull TargetUser targetUser) { + public void onUserStopping(@NonNull TargetUser targetUser) { mDataManager.onUserStopped(targetUser.getUserIdentifier()); } diff --git a/services/usb/java/com/android/server/usb/UsbService.java b/services/usb/java/com/android/server/usb/UsbService.java index 61f2c50ccecf..14c7f04b9e82 100644 --- a/services/usb/java/com/android/server/usb/UsbService.java +++ b/services/usb/java/com/android/server/usb/UsbService.java @@ -110,18 +110,18 @@ public class UsbService extends IUsbManager.Stub { } @Override - public void onSwitchUser(TargetUser from, TargetUser to) { + public void onUserSwitching(TargetUser from, TargetUser to) { FgThread.getHandler() .postAtFrontOfQueue(() -> mUsbService.onSwitchUser(to.getUserIdentifier())); } @Override - public void onStopUser(TargetUser userInfo) { + public void onUserStopping(TargetUser userInfo) { mUsbService.onStopUser(userInfo.getUserHandle()); } @Override - public void onUnlockUser(TargetUser userInfo) { + public void onUserUnlocking(TargetUser userInfo) { mUsbService.onUnlockUser(userInfo.getUserIdentifier()); } } diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java index 8378d8ed9f68..3c0e0af67969 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java @@ -167,7 +167,7 @@ public class VoiceInteractionManagerService extends SystemService { } @Override - public boolean isSupportedUser(TargetUser user) { + public boolean isUserSupported(TargetUser user) { return isSupported(user.getUserInfo()); } |