1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package com.android.systemui.user.data.repository 19 20 import android.content.pm.UserInfo 21 import android.os.UserHandle 22 import com.android.systemui.user.data.model.SelectedUserModel 23 import com.android.systemui.user.data.model.SelectionStatus 24 import com.android.systemui.user.data.model.UserSwitcherSettingsModel 25 import java.util.concurrent.atomic.AtomicBoolean 26 import kotlinx.coroutines.flow.Flow 27 import kotlinx.coroutines.flow.MutableStateFlow 28 import kotlinx.coroutines.flow.asStateFlow 29 import kotlinx.coroutines.flow.map 30 import kotlinx.coroutines.yield 31 32 class FakeUserRepository : UserRepository { 33 companion object { 34 // User id to represent a non system (human) user id. We presume this is the main user. 35 private const val MAIN_USER_ID = 10 36 37 private val DEFAULT_SELECTED_USER = 0 38 private val DEFAULT_SELECTED_USER_INFO = 39 UserInfo( 40 /* id= */ DEFAULT_SELECTED_USER, 41 /* name= */ "default selected user", 42 /* flags= */ 0, 43 ) 44 } 45 46 private val _userSwitcherSettings = MutableStateFlow(UserSwitcherSettingsModel()) 47 override val userSwitcherSettings: Flow<UserSwitcherSettingsModel> = 48 _userSwitcherSettings.asStateFlow() 49 50 private val _userInfos = MutableStateFlow<List<UserInfo>>(emptyList()) 51 override val userInfos: Flow<List<UserInfo>> = _userInfos.asStateFlow() 52 53 override val selectedUser = 54 MutableStateFlow( 55 SelectedUserModel(DEFAULT_SELECTED_USER_INFO, SelectionStatus.SELECTION_COMPLETE) 56 ) 57 override val selectedUserInfo: Flow<UserInfo> = selectedUser.map { it.userInfo } 58 59 private val _userSwitchingInProgress = MutableStateFlow(false) 60 override val userSwitchingInProgress: Flow<Boolean> 61 get() = _userSwitchingInProgress 62 63 override var mainUserId: Int = MAIN_USER_ID 64 override var lastSelectedNonGuestUserId: Int = mainUserId 65 66 private var _isGuestUserAutoCreated: Boolean = false 67 override val isGuestUserAutoCreated: Boolean 68 get() = _isGuestUserAutoCreated 69 70 override var isGuestUserResetting: Boolean = false 71 72 override val isGuestUserCreationScheduled = AtomicBoolean() 73 74 override var isStatusBarUserChipEnabled: Boolean = false 75 76 override var secondaryUserId: Int = UserHandle.USER_NULL 77 78 override var isRefreshUsersPaused: Boolean = false 79 80 var refreshUsersCallCount: Int = 0 81 private set 82 83 override fun refreshUsers() { 84 refreshUsersCallCount++ 85 } 86 87 override fun getSelectedUserInfo(): UserInfo { 88 return selectedUser.value.userInfo 89 } 90 91 override fun isSimpleUserSwitcher(): Boolean { 92 return _userSwitcherSettings.value.isSimpleUserSwitcher 93 } 94 95 override fun isUserSwitcherEnabled(): Boolean { 96 return _userSwitcherSettings.value.isUserSwitcherEnabled 97 } 98 99 fun setUserInfos(infos: List<UserInfo>) { 100 _userInfos.value = infos 101 } 102 103 suspend fun setSelectedUserInfo( 104 userInfo: UserInfo, 105 selectionStatus: SelectionStatus = SelectionStatus.SELECTION_COMPLETE, 106 ) { 107 check(_userInfos.value.contains(userInfo)) { 108 "Cannot select the following user, it is not in the list of user infos: $userInfo!" 109 } 110 111 selectedUser.value = SelectedUserModel(userInfo, selectionStatus) 112 yield() 113 } 114 115 suspend fun setSettings(settings: UserSwitcherSettingsModel) { 116 _userSwitcherSettings.value = settings 117 yield() 118 } 119 120 fun setGuestUserAutoCreated(value: Boolean) { 121 _isGuestUserAutoCreated = value 122 } 123 124 fun setUserSwitching(value: Boolean) { 125 _userSwitchingInProgress.value = value 126 } 127 } 128