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.statusbar.policy 19 20 import android.content.Context 21 import android.content.Intent 22 import android.view.View 23 import com.android.systemui.dagger.SysUISingleton 24 import com.android.systemui.dagger.qualifiers.Application 25 import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor 26 import com.android.systemui.plugins.ActivityStarter 27 import com.android.systemui.qs.user.UserSwitchDialogController.DialogShower 28 import com.android.systemui.user.data.source.UserRecord 29 import com.android.systemui.user.domain.interactor.GuestUserInteractor 30 import com.android.systemui.user.domain.interactor.UserInteractor 31 import com.android.systemui.user.legacyhelper.ui.LegacyUserUiHelper 32 import dagger.Lazy 33 import java.io.PrintWriter 34 import java.lang.ref.WeakReference 35 import javax.inject.Inject 36 37 /** Access point into multi-user switching logic. */ 38 @Deprecated("Use UserInteractor or GuestUserInteractor instead.") 39 @SysUISingleton 40 class UserSwitcherController 41 @Inject 42 constructor( 43 @Application private val applicationContext: Context, 44 private val userInteractorLazy: Lazy<UserInteractor>, 45 private val guestUserInteractorLazy: Lazy<GuestUserInteractor>, 46 private val keyguardInteractorLazy: Lazy<KeyguardInteractor>, 47 private val activityStarter: ActivityStarter, 48 ) { 49 50 /** Defines interface for classes that can be called back when the user is switched. */ 51 fun interface UserSwitchCallback { 52 /** Notifies that the user has switched. */ 53 fun onUserSwitched() 54 } 55 56 private val userInteractor: UserInteractor by lazy { userInteractorLazy.get() } 57 private val guestUserInteractor: GuestUserInteractor by lazy { guestUserInteractorLazy.get() } 58 private val keyguardInteractor: KeyguardInteractor by lazy { keyguardInteractorLazy.get() } 59 60 private val callbackCompatMap = mutableMapOf<UserSwitchCallback, UserInteractor.UserCallback>() 61 62 /** The current list of [UserRecord]. */ 63 val users: ArrayList<UserRecord> 64 get() = userInteractor.userRecords.value 65 66 /** Whether the user switcher experience should use the simple experience. */ 67 val isSimpleUserSwitcher: Boolean 68 get() = userInteractor.isSimpleUserSwitcher 69 70 val isUserSwitcherEnabled: Boolean 71 get() = userInteractor.isUserSwitcherEnabled 72 73 /** The [UserRecord] of the current user or `null` when none. */ 74 val currentUserRecord: UserRecord? 75 get() = userInteractor.selectedUserRecord.value 76 77 /** The name of the current user of the device or `null`, when none is selected. */ 78 val currentUserName: String? 79 get() = 80 currentUserRecord?.let { 81 LegacyUserUiHelper.getUserRecordName( 82 context = applicationContext, 83 record = it, 84 isGuestUserAutoCreated = userInteractor.isGuestUserAutoCreated, 85 isGuestUserResetting = userInteractor.isGuestUserResetting, 86 ) 87 } 88 89 /** 90 * Notifies that a user has been selected. 91 * 92 * This will trigger the right user journeys to create a guest user, switch users, and/or 93 * navigate to the correct destination. 94 * 95 * If a user with the given ID is not found, this method is a no-op. 96 * 97 * @param userId The ID of the user to switch to. 98 * @param dialogShower An optional [DialogShower] in case we need to show dialogs. 99 */ 100 fun onUserSelected(userId: Int, dialogShower: DialogShower?) { 101 userInteractor.selectUser(userId, dialogShower) 102 } 103 104 /** Whether the guest user is configured to always be present on the device. */ 105 val isGuestUserAutoCreated: Boolean 106 get() = userInteractor.isGuestUserAutoCreated 107 108 /** Whether the guest user is currently being reset. */ 109 val isGuestUserResetting: Boolean 110 get() = userInteractor.isGuestUserResetting 111 112 /** Registers an adapter to notify when the users change. */ 113 fun addAdapter(adapter: WeakReference<BaseUserSwitcherAdapter>) { 114 userInteractor.addCallback( 115 object : UserInteractor.UserCallback { 116 override fun isEvictable(): Boolean { 117 return adapter.get() == null 118 } 119 120 override fun onUserStateChanged() { 121 adapter.get()?.notifyDataSetChanged() 122 } 123 } 124 ) 125 } 126 127 /** Notifies the item for a user has been clicked. */ 128 fun onUserListItemClicked( 129 record: UserRecord, 130 dialogShower: DialogShower?, 131 ) { 132 userInteractor.onRecordSelected(record, dialogShower) 133 } 134 135 /** 136 * Removes guest user and switches to target user. The guest must be the current user and its id 137 * must be `guestUserId`. 138 * 139 * If `targetUserId` is `UserHandle.USER_NULL`, then create a new guest user in the foreground, 140 * and immediately switch to it. This is used for wiping the current guest and replacing it with 141 * a new one. 142 * 143 * If `targetUserId` is specified, then remove the guest in the background while switching to 144 * `targetUserId`. 145 * 146 * If device is configured with `config_guestUserAutoCreated`, then after guest user is removed, 147 * a new one is created in the background. This has no effect if `targetUserId` is 148 * `UserHandle.USER_NULL`. 149 * 150 * @param guestUserId id of the guest user to remove 151 * @param targetUserId id of the user to switch to after guest is removed. If 152 * `UserHandle.USER_NULL`, then switch immediately to the newly created guest user. 153 */ 154 fun removeGuestUser(guestUserId: Int, targetUserId: Int) { 155 userInteractor.removeGuestUser( 156 guestUserId = guestUserId, 157 targetUserId = targetUserId, 158 ) 159 } 160 161 /** 162 * Exits guest user and switches to previous non-guest user. The guest must be the current user. 163 * 164 * @param guestUserId user id of the guest user to exit 165 * @param targetUserId user id of the guest user to exit, set to UserHandle#USER_NULL when 166 * target user id is not known 167 * @param forceRemoveGuestOnExit true: remove guest before switching user, false: remove guest 168 * only if its ephemeral, else keep guest 169 */ 170 fun exitGuestUser(guestUserId: Int, targetUserId: Int, forceRemoveGuestOnExit: Boolean) { 171 userInteractor.exitGuestUser(guestUserId, targetUserId, forceRemoveGuestOnExit) 172 } 173 174 /** 175 * Guarantee guest is present only if the device is provisioned. Otherwise, create a content 176 * observer to wait until the device is provisioned, then schedule the guest creation. 177 */ 178 fun schedulePostBootGuestCreation() { 179 guestUserInteractor.onDeviceBootCompleted() 180 } 181 182 /** Whether keyguard is showing. */ 183 val isKeyguardShowing: Boolean 184 get() = keyguardInteractor.isKeyguardShowing() 185 186 /** Starts an activity with the given [Intent]. */ 187 fun startActivity(intent: Intent) { 188 activityStarter.startActivity(intent, /* dismissShade= */ true) 189 } 190 191 /** 192 * Refreshes users from UserManager. 193 * 194 * The pictures are only loaded if they have not been loaded yet. 195 */ 196 fun refreshUsers() { 197 userInteractor.refreshUsers() 198 } 199 200 /** Adds a subscriber to when user switches. */ 201 fun addUserSwitchCallback(callback: UserSwitchCallback) { 202 val interactorCallback = 203 object : UserInteractor.UserCallback { 204 override fun onUserStateChanged() { 205 callback.onUserSwitched() 206 } 207 } 208 callbackCompatMap[callback] = interactorCallback 209 userInteractor.addCallback(interactorCallback) 210 } 211 212 /** Removes a previously-added subscriber. */ 213 fun removeUserSwitchCallback(callback: UserSwitchCallback) { 214 val interactorCallback = callbackCompatMap.remove(callback) 215 if (interactorCallback != null) { 216 userInteractor.removeCallback(interactorCallback) 217 } 218 } 219 220 fun dump(pw: PrintWriter, args: Array<out String>) { 221 userInteractor.dump(pw) 222 } 223 224 companion object { 225 /** Alpha value to apply to a user view in the user switcher when it's selectable. */ 226 private const val ENABLED_ALPHA = 227 LegacyUserUiHelper.USER_SWITCHER_USER_VIEW_SELECTABLE_ALPHA 228 229 /** Alpha value to apply to a user view in the user switcher when it's not selectable. */ 230 private const val DISABLED_ALPHA = 231 LegacyUserUiHelper.USER_SWITCHER_USER_VIEW_NOT_SELECTABLE_ALPHA 232 233 @JvmStatic 234 fun setSelectableAlpha(view: View) { 235 view.alpha = 236 if (view.isEnabled) { 237 ENABLED_ALPHA 238 } else { 239 DISABLED_ALPHA 240 } 241 } 242 } 243 } 244