From d1638a8b626aa861d3b11821becf39e0eb2db0e5 Mon Sep 17 00:00:00 2001 From: Aaron Liu Date: Fri, 4 Nov 2022 10:30:46 -0700 Subject: [PATCH] [User Switcher] Fix username sync in bouncer. The user name is not updated when userinfo changes (name and avatar). Updated usertracker to listen to this intent and updated repository to listen to these changes. Corresponding tests have also been updated. Fixes: 254678959 Test: Observed username in bouncer. I unlock the device and change the username for the current user. Turn device screen off and on and go back to bouncer. Observe that the username is the same as the one changed in settings. Change-Id: Ie6e289dfc65c14f9b0a35f031c46122d10dca9d0 --- .../systemui/settings/UserTrackerImpl.kt | 2 ++ .../user/data/repository/UserRepository.kt | 4 +++ .../systemui/settings/UserTrackerImplTest.kt | 31 +++++++++++++++++++ .../UserRepositoryImplRefactoredTest.kt | 19 ++++++++++++ .../systemui/settings/FakeUserTracker.kt | 4 +++ 5 files changed, 60 insertions(+) diff --git a/packages/SystemUI/src/com/android/systemui/settings/UserTrackerImpl.kt b/packages/SystemUI/src/com/android/systemui/settings/UserTrackerImpl.kt index 47bed461e3719..28da38b701bc1 100644 --- a/packages/SystemUI/src/com/android/systemui/settings/UserTrackerImpl.kt +++ b/packages/SystemUI/src/com/android/systemui/settings/UserTrackerImpl.kt @@ -108,6 +108,7 @@ class UserTrackerImpl internal constructor( val filter = IntentFilter().apply { addAction(Intent.ACTION_USER_SWITCHED) + addAction(Intent.ACTION_USER_INFO_CHANGED) // These get called when a managed profile goes in or out of quiet mode. addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE) addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE) @@ -125,6 +126,7 @@ class UserTrackerImpl internal constructor( Intent.ACTION_USER_SWITCHED -> { handleSwitchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL)) } + Intent.ACTION_USER_INFO_CHANGED, Intent.ACTION_MANAGED_PROFILE_AVAILABLE, Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE, Intent.ACTION_MANAGED_PROFILE_REMOVED, diff --git a/packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt b/packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt index 6a2326036ec09..ffaf524bb0d11 100644 --- a/packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt +++ b/packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt @@ -250,6 +250,10 @@ constructor( override fun onUserChanged(newUser: Int, userContext: Context) { send() } + + override fun onProfilesChanged(profiles: List) { + send() + } } tracker.addCallback(callback, mainDispatcher.asExecutor()) diff --git a/packages/SystemUI/tests/src/com/android/systemui/settings/UserTrackerImplTest.kt b/packages/SystemUI/tests/src/com/android/systemui/settings/UserTrackerImplTest.kt index bd4b94eef2dd8..52462c7186d4a 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/settings/UserTrackerImplTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/settings/UserTrackerImplTest.kt @@ -310,6 +310,37 @@ class UserTrackerImplTest : SysuiTestCase() { assertThat(callback.lastUserProfiles.map { it.id }).containsExactly(0, profileID) } + @Test + fun testCallbackCalledOnUserInfoChanged() { + tracker.initialize(0) + val callback = TestCallback() + tracker.addCallback(callback, executor) + val profileID = tracker.userId + 10 + + `when`(userManager.getProfiles(anyInt())).thenAnswer { invocation -> + val id = invocation.getArgument(0) + val info = UserInfo(id, "", UserInfo.FLAG_FULL) + val infoProfile = UserInfo( + id + 10, + "", + "", + UserInfo.FLAG_MANAGED_PROFILE, + UserManager.USER_TYPE_PROFILE_MANAGED + ) + infoProfile.profileGroupId = id + listOf(info, infoProfile) + } + + val intent = Intent(Intent.ACTION_USER_INFO_CHANGED) + .putExtra(Intent.EXTRA_USER, UserHandle.of(profileID)) + + tracker.onReceive(context, intent) + + assertThat(callback.calledOnUserChanged).isEqualTo(0) + assertThat(callback.calledOnProfilesChanged).isEqualTo(1) + assertThat(callback.lastUserProfiles.map { it.id }).containsExactly(0, profileID) + } + @Test fun testCallbackRemoved() { tracker.initialize(0) diff --git a/packages/SystemUI/tests/src/com/android/systemui/user/data/repository/UserRepositoryImplRefactoredTest.kt b/packages/SystemUI/tests/src/com/android/systemui/user/data/repository/UserRepositoryImplRefactoredTest.kt index 525d8371c9ff5..7c7f0e1e0e12d 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/user/data/repository/UserRepositoryImplRefactoredTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/user/data/repository/UserRepositoryImplRefactoredTest.kt @@ -145,6 +145,25 @@ class UserRepositoryImplRefactoredTest : UserRepositoryImplTest() { assertThat(userInfos).isEqualTo(expectedUsers) } + @Test + fun `userTrackerCallback - updates selectedUserInfo`() = runSelfCancelingTest { + underTest = create(this) + var selectedUserInfo: UserInfo? = null + underTest.selectedUserInfo.onEach { selectedUserInfo = it }.launchIn(this) + setUpUsers( + count = 2, + selectedIndex = 0, + ) + tracker.onProfileChanged() + assertThat(selectedUserInfo?.id == 0) + setUpUsers( + count = 2, + selectedIndex = 1, + ) + tracker.onProfileChanged() + assertThat(selectedUserInfo?.id == 1) + } + private fun setUpUsers( count: Int, isLastGuestUser: Boolean = false, 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 9726bf83b263e..a7eadba60ddc2 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 @@ -68,4 +68,8 @@ class FakeUserTracker( callbacks.forEach { it.onUserChanged(_userId, userContext) } } + + fun onProfileChanged() { + callbacks.forEach { it.onProfilesChanged(_userProfiles) } + } }