From b37f9cc6f41f86b9533fae19229f7438776a79ee Mon Sep 17 00:00:00 2001 From: Shawn Lin Date: Wed, 11 Oct 2023 07:55:42 +0000 Subject: Support onBeforeUserSwitching in UserTracker.Callback Bug: 303589201 Test: atest KeyguardZenAlarmViewControllerTest NextClockAlarmControllerTest Change-Id: I9ecc23bd7a04569897b9a031f3b246a637736b30 --- packages/SystemUI/src/com/android/systemui/settings/UserTracker.kt | 4 ++++ .../SystemUI/src/com/android/systemui/settings/UserTrackerImpl.kt | 7 +++++++ .../utils/src/com/android/systemui/settings/FakeUserTracker.kt | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/packages/SystemUI/src/com/android/systemui/settings/UserTracker.kt b/packages/SystemUI/src/com/android/systemui/settings/UserTracker.kt index cf1fbe3685e3..d6f1ed9c3334 100644 --- a/packages/SystemUI/src/com/android/systemui/settings/UserTracker.kt +++ b/packages/SystemUI/src/com/android/systemui/settings/UserTracker.kt @@ -68,6 +68,10 @@ interface UserTracker : UserContentResolverProvider, UserContextProvider { */ @WeaklyReferencedCallback interface Callback { + /** + * Notifies that the current user will be changed. + */ + fun onBeforeUserSwitching(newUser: Int) {} /** * Same as {@link onUserChanging(Int, Context, Runnable)} but the callback will be diff --git a/packages/SystemUI/src/com/android/systemui/settings/UserTrackerImpl.kt b/packages/SystemUI/src/com/android/systemui/settings/UserTrackerImpl.kt index 393a698bcdb7..096e37f01255 100644 --- a/packages/SystemUI/src/com/android/systemui/settings/UserTrackerImpl.kt +++ b/packages/SystemUI/src/com/android/systemui/settings/UserTrackerImpl.kt @@ -226,6 +226,13 @@ open class UserTrackerImpl internal constructor( protected open fun handleBeforeUserSwitching(newUserId: Int) { Assert.isNotMainThread() setUserIdInternal(newUserId) + + val list = synchronized(callbacks) { + callbacks.toList() + } + list.forEach { + it.callback.get()?.onBeforeUserSwitching(newUserId) + } } @WorkerThread diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/settings/FakeUserTracker.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/settings/FakeUserTracker.kt index 4307ff980be4..7494ccf32a2c 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/settings/FakeUserTracker.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/settings/FakeUserTracker.kt @@ -69,10 +69,16 @@ class FakeUserTracker( _userId = _userInfo.id _userHandle = UserHandle.of(_userId) + onBeforeUserSwitching() onUserChanging() onUserChanged() } + fun onBeforeUserSwitching(userId: Int = _userId) { + val copy = callbacks.toList() + copy.forEach { it.onBeforeUserSwitching(userId) } + } + fun onUserChanging(userId: Int = _userId) { val copy = callbacks.toList() copy.forEach { it.onUserChanging(userId, userContext) {} } -- cgit v1.2.3-59-g8ed1b