Loading packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt +11 −1 Original line number Diff line number Diff line Loading @@ -76,6 +76,9 @@ interface UserRepository { /** Whether user switching is currently in progress. */ val userSwitchingInProgress: Flow<Boolean> /** User ID of the main user. */ val mainUserId: Int /** User ID of the last non-guest selected user. */ val lastSelectedNonGuestUserId: Int Loading Loading @@ -130,7 +133,9 @@ constructor( private val _selectedUserInfo = MutableStateFlow<UserInfo?>(null) override val selectedUserInfo: Flow<UserInfo> = _selectedUserInfo.filterNotNull() override var lastSelectedNonGuestUserId: Int = UserHandle.USER_SYSTEM override var mainUserId: Int = UserHandle.USER_NULL private set override var lastSelectedNonGuestUserId: Int = UserHandle.USER_NULL private set override val isGuestUserAutoCreated: Boolean = Loading Loading @@ -172,6 +177,11 @@ constructor( // The guest user is always last, regardless of creation time. .sortedBy { it.isGuest } } if (mainUserId == UserHandle.USER_NULL) { val mainUser = withContext(backgroundDispatcher) { manager.mainUser } mainUser?.let { mainUserId = it.identifier } } } } Loading packages/SystemUI/src/com/android/systemui/user/domain/interactor/GuestUserInteractor.kt +7 −4 Original line number Diff line number Diff line Loading @@ -139,11 +139,11 @@ constructor( } applicationScope.launch { var newUserId = UserHandle.USER_SYSTEM var newUserId = repository.mainUserId if (targetUserId == UserHandle.USER_NULL) { // When a target user is not specified switch to last non guest user: val lastSelectedNonGuestUserHandle = repository.lastSelectedNonGuestUserId if (lastSelectedNonGuestUserHandle != UserHandle.USER_SYSTEM) { if (lastSelectedNonGuestUserHandle != repository.mainUserId) { val info = withContext(backgroundDispatcher) { manager.getUserInfo(lastSelectedNonGuestUserHandle) Loading Loading @@ -215,8 +215,11 @@ constructor( // Create a new guest in the foreground, and then immediately switch to it val newGuestId = create(showDialog, dismissDialog) if (newGuestId == UserHandle.USER_NULL) { Log.e(TAG, "Could not create new guest, switching back to system user") switchUser(UserHandle.USER_SYSTEM) Log.e(TAG, "Could not create new guest, switching back to main user") val mainUser = withContext(backgroundDispatcher) { manager.mainUser?.identifier } mainUser?.let { switchUser(it) } withContext(backgroundDispatcher) { manager.removeUserWhenPossible( UserHandle.of(currentUser.id), Loading packages/SystemUI/tests/src/com/android/systemui/user/data/repository/UserRepositoryImplTest.kt +6 −0 Original line number Diff line number Diff line Loading @@ -128,6 +128,11 @@ class UserRepositoryImplTest : SysuiTestCase() { @Test fun refreshUsers() = runSelfCancelingTest { val mainUserId = 10 val mainUser = mock(UserHandle::class.java) whenever(manager.mainUser).thenReturn(mainUser) whenever(mainUser.identifier).thenReturn(mainUserId) underTest = create(this) val initialExpectedValue = setUpUsers( Loading Loading @@ -166,6 +171,7 @@ class UserRepositoryImplTest : SysuiTestCase() { assertThat(selectedUserInfo).isEqualTo(thirdExpectedValue[1]) assertThat(selectedUserInfo?.isGuest).isTrue() assertThat(underTest.lastSelectedNonGuestUserId).isEqualTo(selectedNonGuestUserId) assertThat(underTest.mainUserId).isEqualTo(mainUserId) } @Test Loading packages/SystemUI/tests/src/com/android/systemui/user/domain/interactor/GuestUserInteractorTest.kt +4 −2 Original line number Diff line number Diff line Loading @@ -204,10 +204,12 @@ class GuestUserInteractorTest : SysuiTestCase() { } @Test fun `exit - last non-guest was removed - returns to system`() = fun `exit - last non-guest was removed - returns to main user`() = runBlocking(IMMEDIATE) { val removedUserId = 310 val mainUserId = 10 repository.lastSelectedNonGuestUserId = removedUserId repository.mainUserId = mainUserId repository.setSelectedUserInfo(GUEST_USER_INFO) underTest.exit( Loading @@ -221,7 +223,7 @@ class GuestUserInteractorTest : SysuiTestCase() { verify(manager, never()).markGuestForDeletion(anyInt()) verify(manager, never()).removeUser(anyInt()) verify(switchUser).invoke(UserHandle.USER_SYSTEM) verify(switchUser).invoke(mainUserId) } @Test Loading packages/SystemUI/tests/utils/src/com/android/systemui/user/data/repository/FakeUserRepository.kt +6 −1 Original line number Diff line number Diff line Loading @@ -28,6 +28,10 @@ import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.yield class FakeUserRepository : UserRepository { companion object { // User id to represent a non system (human) user id. We presume this is the main user. private const val MAIN_USER_ID = 10 } private val _userSwitcherSettings = MutableStateFlow(UserSwitcherSettingsModel()) override val userSwitcherSettings: Flow<UserSwitcherSettingsModel> = Loading @@ -43,7 +47,8 @@ class FakeUserRepository : UserRepository { override val userSwitchingInProgress: Flow<Boolean> get() = _userSwitchingInProgress override var lastSelectedNonGuestUserId: Int = UserHandle.USER_SYSTEM override var mainUserId: Int = MAIN_USER_ID override var lastSelectedNonGuestUserId: Int = mainUserId private var _isGuestUserAutoCreated: Boolean = false override val isGuestUserAutoCreated: Boolean Loading Loading
packages/SystemUI/src/com/android/systemui/user/data/repository/UserRepository.kt +11 −1 Original line number Diff line number Diff line Loading @@ -76,6 +76,9 @@ interface UserRepository { /** Whether user switching is currently in progress. */ val userSwitchingInProgress: Flow<Boolean> /** User ID of the main user. */ val mainUserId: Int /** User ID of the last non-guest selected user. */ val lastSelectedNonGuestUserId: Int Loading Loading @@ -130,7 +133,9 @@ constructor( private val _selectedUserInfo = MutableStateFlow<UserInfo?>(null) override val selectedUserInfo: Flow<UserInfo> = _selectedUserInfo.filterNotNull() override var lastSelectedNonGuestUserId: Int = UserHandle.USER_SYSTEM override var mainUserId: Int = UserHandle.USER_NULL private set override var lastSelectedNonGuestUserId: Int = UserHandle.USER_NULL private set override val isGuestUserAutoCreated: Boolean = Loading Loading @@ -172,6 +177,11 @@ constructor( // The guest user is always last, regardless of creation time. .sortedBy { it.isGuest } } if (mainUserId == UserHandle.USER_NULL) { val mainUser = withContext(backgroundDispatcher) { manager.mainUser } mainUser?.let { mainUserId = it.identifier } } } } Loading
packages/SystemUI/src/com/android/systemui/user/domain/interactor/GuestUserInteractor.kt +7 −4 Original line number Diff line number Diff line Loading @@ -139,11 +139,11 @@ constructor( } applicationScope.launch { var newUserId = UserHandle.USER_SYSTEM var newUserId = repository.mainUserId if (targetUserId == UserHandle.USER_NULL) { // When a target user is not specified switch to last non guest user: val lastSelectedNonGuestUserHandle = repository.lastSelectedNonGuestUserId if (lastSelectedNonGuestUserHandle != UserHandle.USER_SYSTEM) { if (lastSelectedNonGuestUserHandle != repository.mainUserId) { val info = withContext(backgroundDispatcher) { manager.getUserInfo(lastSelectedNonGuestUserHandle) Loading Loading @@ -215,8 +215,11 @@ constructor( // Create a new guest in the foreground, and then immediately switch to it val newGuestId = create(showDialog, dismissDialog) if (newGuestId == UserHandle.USER_NULL) { Log.e(TAG, "Could not create new guest, switching back to system user") switchUser(UserHandle.USER_SYSTEM) Log.e(TAG, "Could not create new guest, switching back to main user") val mainUser = withContext(backgroundDispatcher) { manager.mainUser?.identifier } mainUser?.let { switchUser(it) } withContext(backgroundDispatcher) { manager.removeUserWhenPossible( UserHandle.of(currentUser.id), Loading
packages/SystemUI/tests/src/com/android/systemui/user/data/repository/UserRepositoryImplTest.kt +6 −0 Original line number Diff line number Diff line Loading @@ -128,6 +128,11 @@ class UserRepositoryImplTest : SysuiTestCase() { @Test fun refreshUsers() = runSelfCancelingTest { val mainUserId = 10 val mainUser = mock(UserHandle::class.java) whenever(manager.mainUser).thenReturn(mainUser) whenever(mainUser.identifier).thenReturn(mainUserId) underTest = create(this) val initialExpectedValue = setUpUsers( Loading Loading @@ -166,6 +171,7 @@ class UserRepositoryImplTest : SysuiTestCase() { assertThat(selectedUserInfo).isEqualTo(thirdExpectedValue[1]) assertThat(selectedUserInfo?.isGuest).isTrue() assertThat(underTest.lastSelectedNonGuestUserId).isEqualTo(selectedNonGuestUserId) assertThat(underTest.mainUserId).isEqualTo(mainUserId) } @Test Loading
packages/SystemUI/tests/src/com/android/systemui/user/domain/interactor/GuestUserInteractorTest.kt +4 −2 Original line number Diff line number Diff line Loading @@ -204,10 +204,12 @@ class GuestUserInteractorTest : SysuiTestCase() { } @Test fun `exit - last non-guest was removed - returns to system`() = fun `exit - last non-guest was removed - returns to main user`() = runBlocking(IMMEDIATE) { val removedUserId = 310 val mainUserId = 10 repository.lastSelectedNonGuestUserId = removedUserId repository.mainUserId = mainUserId repository.setSelectedUserInfo(GUEST_USER_INFO) underTest.exit( Loading @@ -221,7 +223,7 @@ class GuestUserInteractorTest : SysuiTestCase() { verify(manager, never()).markGuestForDeletion(anyInt()) verify(manager, never()).removeUser(anyInt()) verify(switchUser).invoke(UserHandle.USER_SYSTEM) verify(switchUser).invoke(mainUserId) } @Test Loading
packages/SystemUI/tests/utils/src/com/android/systemui/user/data/repository/FakeUserRepository.kt +6 −1 Original line number Diff line number Diff line Loading @@ -28,6 +28,10 @@ import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.yield class FakeUserRepository : UserRepository { companion object { // User id to represent a non system (human) user id. We presume this is the main user. private const val MAIN_USER_ID = 10 } private val _userSwitcherSettings = MutableStateFlow(UserSwitcherSettingsModel()) override val userSwitcherSettings: Flow<UserSwitcherSettingsModel> = Loading @@ -43,7 +47,8 @@ class FakeUserRepository : UserRepository { override val userSwitchingInProgress: Flow<Boolean> get() = _userSwitchingInProgress override var lastSelectedNonGuestUserId: Int = UserHandle.USER_SYSTEM override var mainUserId: Int = MAIN_USER_ID override var lastSelectedNonGuestUserId: Int = mainUserId private var _isGuestUserAutoCreated: Boolean = false override val isGuestUserAutoCreated: Boolean Loading