Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d78c69f7 authored by Aaron Liu's avatar Aaron Liu Committed by Android (Google) Code Review
Browse files

Merge "[User Switcher] Fix IllegalStateException in..." into tm-qpr-dev

parents c9b69ab9 9a4fb797
Loading
Loading
Loading
Loading
+51 −44
Original line number Diff line number Diff line
@@ -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
@@ -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)
                )
            }

@@ -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,
@@ -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),
@@ -666,7 +674,7 @@ constructor(
                isSelectable = canSwitchUsers,
                isGuest = true,
            )
            userInfo.supportsSwitchToByUser() ->
        } else {
            UserModel(
                id = userId,
                name = Text.Loaded(userInfo.name),
@@ -679,7 +687,6 @@ constructor(
                isSelectable = canSwitchUsers || isSelected,
                isGuest = false,
            )
            else -> null
        }
    }

+13 −0
Original line number Diff line number Diff line
@@ -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,