Loading packages/SystemUI/src/com/android/systemui/user/domain/interactor/UserInteractor.kt +51 −44 Original line number Diff line number Diff line Loading @@ -66,7 +66,6 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach Loading Loading @@ -138,18 +137,12 @@ constructor( /** The currently-selected user. */ val selectedUser: Flow<UserModel> get() = combine( repository.selectedUserInfo, repository.userSwitcherSettings, ) { selectedUserInfo, settings -> repository.selectedUserInfo.map { selectedUserInfo -> val selectedUserId = selectedUserInfo.id checkNotNull( toUserModel( userInfo = selectedUserInfo, selectedUserId = selectedUserId, canSwitchUsers = canSwitchUsers(selectedUserId), isUserSwitcherEnabled = settings.isUserSwitcherEnabled, ) canSwitchUsers = canSwitchUsers(selectedUserId) ) } Loading Loading @@ -629,7 +622,7 @@ constructor( // The guest user should go in the last position. .sortedBy { it.isGuest } .mapNotNull { userInfo -> toUserModel( filterAndMapToUserModel( userInfo = userInfo, selectedUserId = selectedUserId, canSwitchUsers = canSwitchUsers, Loading @@ -638,22 +631,37 @@ constructor( } } private suspend fun toUserModel( /** * Maps UserInfo to UserModel based on some parameters and return null under certain conditions * to be filtered out. */ private suspend fun filterAndMapToUserModel( userInfo: UserInfo, selectedUserId: Int, canSwitchUsers: Boolean, isUserSwitcherEnabled: Boolean, ): UserModel? { val userId = userInfo.id val isSelected = userId == selectedUserId return when { // When the user switcher is not enabled in settings, we only show the primary user. !isUserSwitcherEnabled && !userInfo.isPrimary -> null // We avoid showing disabled users. !userInfo.isEnabled -> null userInfo.isGuest -> // We meet the conditions to return the UserModel. userInfo.isGuest || userInfo.supportsSwitchToByUser() -> toUserModel(userInfo, selectedUserId, canSwitchUsers) else -> null } } /** Maps UserInfo to UserModel based on some parameters. */ private suspend fun toUserModel( userInfo: UserInfo, selectedUserId: Int, canSwitchUsers: Boolean ): UserModel { val userId = userInfo.id val isSelected = userId == selectedUserId return if (userInfo.isGuest) { UserModel( id = userId, name = Text.Loaded(userInfo.name), Loading @@ -666,7 +674,7 @@ constructor( isSelectable = canSwitchUsers, isGuest = true, ) userInfo.supportsSwitchToByUser() -> } else { UserModel( id = userId, name = Text.Loaded(userInfo.name), Loading @@ -679,7 +687,6 @@ constructor( isSelectable = canSwitchUsers || isSelected, isGuest = false, ) else -> null } } Loading packages/SystemUI/tests/src/com/android/systemui/user/domain/interactor/UserInteractorTest.kt +13 −0 Original line number Diff line number Diff line Loading @@ -793,6 +793,19 @@ class UserInteractorTest : SysuiTestCase() { job.cancel() } @Test fun `current user is not primary and user switcher is disabled`() = runBlocking(IMMEDIATE) { val userInfos = createUserInfos(count = 2, includeGuest = false) userRepository.setUserInfos(userInfos) userRepository.setSelectedUserInfo(userInfos[1]) userRepository.setSettings(UserSwitcherSettingsModel(isUserSwitcherEnabled = false)) var selectedUser: UserModel? = null val job = underTest.selectedUser.onEach { selectedUser = it }.launchIn(this) assertThat(selectedUser).isNotNull() job.cancel() } private fun assertUsers( models: List<UserModel>?, count: Int, Loading Loading
packages/SystemUI/src/com/android/systemui/user/domain/interactor/UserInteractor.kt +51 −44 Original line number Diff line number Diff line Loading @@ -66,7 +66,6 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach Loading Loading @@ -138,18 +137,12 @@ constructor( /** The currently-selected user. */ val selectedUser: Flow<UserModel> get() = combine( repository.selectedUserInfo, repository.userSwitcherSettings, ) { selectedUserInfo, settings -> repository.selectedUserInfo.map { selectedUserInfo -> val selectedUserId = selectedUserInfo.id checkNotNull( toUserModel( userInfo = selectedUserInfo, selectedUserId = selectedUserId, canSwitchUsers = canSwitchUsers(selectedUserId), isUserSwitcherEnabled = settings.isUserSwitcherEnabled, ) canSwitchUsers = canSwitchUsers(selectedUserId) ) } Loading Loading @@ -629,7 +622,7 @@ constructor( // The guest user should go in the last position. .sortedBy { it.isGuest } .mapNotNull { userInfo -> toUserModel( filterAndMapToUserModel( userInfo = userInfo, selectedUserId = selectedUserId, canSwitchUsers = canSwitchUsers, Loading @@ -638,22 +631,37 @@ constructor( } } private suspend fun toUserModel( /** * Maps UserInfo to UserModel based on some parameters and return null under certain conditions * to be filtered out. */ private suspend fun filterAndMapToUserModel( userInfo: UserInfo, selectedUserId: Int, canSwitchUsers: Boolean, isUserSwitcherEnabled: Boolean, ): UserModel? { val userId = userInfo.id val isSelected = userId == selectedUserId return when { // When the user switcher is not enabled in settings, we only show the primary user. !isUserSwitcherEnabled && !userInfo.isPrimary -> null // We avoid showing disabled users. !userInfo.isEnabled -> null userInfo.isGuest -> // We meet the conditions to return the UserModel. userInfo.isGuest || userInfo.supportsSwitchToByUser() -> toUserModel(userInfo, selectedUserId, canSwitchUsers) else -> null } } /** Maps UserInfo to UserModel based on some parameters. */ private suspend fun toUserModel( userInfo: UserInfo, selectedUserId: Int, canSwitchUsers: Boolean ): UserModel { val userId = userInfo.id val isSelected = userId == selectedUserId return if (userInfo.isGuest) { UserModel( id = userId, name = Text.Loaded(userInfo.name), Loading @@ -666,7 +674,7 @@ constructor( isSelectable = canSwitchUsers, isGuest = true, ) userInfo.supportsSwitchToByUser() -> } else { UserModel( id = userId, name = Text.Loaded(userInfo.name), Loading @@ -679,7 +687,6 @@ constructor( isSelectable = canSwitchUsers || isSelected, isGuest = false, ) else -> null } } Loading
packages/SystemUI/tests/src/com/android/systemui/user/domain/interactor/UserInteractorTest.kt +13 −0 Original line number Diff line number Diff line Loading @@ -793,6 +793,19 @@ class UserInteractorTest : SysuiTestCase() { job.cancel() } @Test fun `current user is not primary and user switcher is disabled`() = runBlocking(IMMEDIATE) { val userInfos = createUserInfos(count = 2, includeGuest = false) userRepository.setUserInfos(userInfos) userRepository.setSelectedUserInfo(userInfos[1]) userRepository.setSettings(UserSwitcherSettingsModel(isUserSwitcherEnabled = false)) var selectedUser: UserModel? = null val job = underTest.selectedUser.onEach { selectedUser = it }.launchIn(this) assertThat(selectedUser).isNotNull() job.cancel() } private fun assertUsers( models: List<UserModel>?, count: Int, Loading