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.dagger.SysUISingleton 23 import com.android.systemui.user.data.model.SelectedUserModel 24 import com.android.systemui.user.data.model.SelectionStatus 25 import com.android.systemui.user.data.model.UserSwitcherSettingsModel 26 import dagger.Binds 27 import dagger.Module 28 import java.util.concurrent.atomic.AtomicBoolean 29 import javax.inject.Inject 30 import kotlinx.coroutines.flow.Flow 31 import kotlinx.coroutines.flow.MutableStateFlow 32 import kotlinx.coroutines.flow.asStateFlow 33 import kotlinx.coroutines.flow.map 34 import kotlinx.coroutines.yield 35 36 @SysUISingleton 37 class FakeUserRepository @Inject constructor() : UserRepository { 38 companion object { 39 // User id to represent a non system (human) user id. We presume this is the main user. 40 const val MAIN_USER_ID = 10 41 42 private const val DEFAULT_SELECTED_USER = 0 43 private val DEFAULT_SELECTED_USER_INFO = 44 UserInfo( 45 /* id= */ DEFAULT_SELECTED_USER, 46 /* name= */ "default selected user", 47 /* flags= */ 0, 48 ) 49 private val MAIN_USER = 50 UserInfo( 51 /* id= */ MAIN_USER_ID, 52 /* name= */ "main user", 53 /* flags= */ UserInfo.FLAG_MAIN, 54 ) 55 } 56 57 private val _userSwitcherSettings = MutableStateFlow(UserSwitcherSettingsModel()) 58 override val userSwitcherSettings: Flow<UserSwitcherSettingsModel> = 59 _userSwitcherSettings.asStateFlow() 60 61 private val _userInfos = MutableStateFlow<List<UserInfo>>(emptyList()) 62 override val userInfos: Flow<List<UserInfo>> = _userInfos.asStateFlow() 63 64 override val selectedUser = 65 MutableStateFlow( 66 SelectedUserModel(DEFAULT_SELECTED_USER_INFO, SelectionStatus.SELECTION_COMPLETE) 67 ) <lambda>null68 override val selectedUserInfo: Flow<UserInfo> = selectedUser.map { it.userInfo } 69 70 override var mainUserId: Int = MAIN_USER_ID 71 override var lastSelectedNonGuestUserId: Int = mainUserId 72 73 private var _isGuestUserAutoCreated: Boolean = false 74 override val isGuestUserAutoCreated: Boolean 75 get() = _isGuestUserAutoCreated 76 77 override var isGuestUserResetting: Boolean = false 78 79 override val isGuestUserCreationScheduled = AtomicBoolean() 80 81 override var isStatusBarUserChipEnabled: Boolean = false 82 83 override var secondaryUserId: Int = UserHandle.USER_NULL 84 85 override var isRefreshUsersPaused: Boolean = false 86 getMainUserIdnull87 override suspend fun getMainUserId(): Int? { 88 return MAIN_USER_ID 89 } 90 91 var refreshUsersCallCount: Int = 0 92 private set 93 refreshUsersnull94 override fun refreshUsers() { 95 refreshUsersCallCount++ 96 } 97 getSelectedUserInfonull98 override fun getSelectedUserInfo(): UserInfo { 99 return selectedUser.value.userInfo 100 } 101 isSimpleUserSwitchernull102 override fun isSimpleUserSwitcher(): Boolean { 103 return _userSwitcherSettings.value.isSimpleUserSwitcher 104 } 105 isUserSwitcherEnablednull106 override fun isUserSwitcherEnabled(): Boolean { 107 return _userSwitcherSettings.value.isUserSwitcherEnabled 108 } 109 setUserInfosnull110 fun setUserInfos(infos: List<UserInfo>) { 111 _userInfos.value = infos 112 } 113 setSelectedUserInfonull114 suspend fun setSelectedUserInfo( 115 userInfo: UserInfo, 116 selectionStatus: SelectionStatus = SelectionStatus.SELECTION_COMPLETE, 117 ) { 118 check(_userInfos.value.contains(userInfo)) { 119 "Cannot select the following user, it is not in the list of user infos: $userInfo!" 120 } 121 122 selectedUser.value = SelectedUserModel(userInfo, selectionStatus) 123 yield() 124 } 125 126 /** Resets the current user to the default of [DEFAULT_SELECTED_USER_INFO]. */ asDefaultUsernull127 suspend fun asDefaultUser(): UserInfo { 128 setUserInfos(listOf(DEFAULT_SELECTED_USER_INFO)) 129 setSelectedUserInfo(DEFAULT_SELECTED_USER_INFO) 130 return DEFAULT_SELECTED_USER_INFO 131 } 132 133 /** Makes the current user [MAIN_USER]. */ asMainUsernull134 suspend fun asMainUser(): UserInfo { 135 setUserInfos(listOf(MAIN_USER)) 136 setSelectedUserInfo(MAIN_USER) 137 return MAIN_USER 138 } 139 setSettingsnull140 suspend fun setSettings(settings: UserSwitcherSettingsModel) { 141 _userSwitcherSettings.value = settings 142 yield() 143 } 144 setGuestUserAutoCreatednull145 fun setGuestUserAutoCreated(value: Boolean) { 146 _isGuestUserAutoCreated = value 147 } 148 } 149 150 @Module 151 interface FakeUserRepositoryModule { bindFakenull152 @Binds fun bindFake(fake: FakeUserRepository): UserRepository 153 } 154