1 package com.android.systemui.user.domain.interactor
2 
3 import android.annotation.UserIdInt
4 import android.content.pm.UserInfo
5 import android.os.UserManager
6 import com.android.keyguard.KeyguardUpdateMonitor
7 import com.android.systemui.Flags.refactorGetCurrentUser
8 import com.android.systemui.dagger.SysUISingleton
9 import com.android.systemui.user.data.repository.UserRepository
10 import javax.inject.Inject
11 import kotlinx.coroutines.flow.distinctUntilChanged
12 import kotlinx.coroutines.flow.map
13 
14 /** Encapsulates business logic to interact the selected user */
15 @SysUISingleton
16 class SelectedUserInteractor @Inject constructor(private val repository: UserRepository) {
17 
18     /** Flow providing the ID of the currently selected user. */
<lambda>null19     val selectedUser = repository.selectedUserInfo.map { it.id }.distinctUntilChanged()
20 
21     /** Flow providing the [UserInfo] of the currently selected user. */
22     val selectedUserInfo = repository.selectedUserInfo
23 
24     /**
25      * Returns the ID of the currently-selected user.
26      *
27      * @param bypassFlag this will ignore the feature flag and get the data from the repository
28      *   instead. This is used for refactored methods that were previously pointing to `userTracker`
29      *   and therefore should not be routed back to KeyguardUpdateMonitor when flag is disabled.
30      *   KeyguardUpdateMonitor.getCurrentUser() is deprecated and will be removed soon (together
31      *   with this flag).
32      */
33     @UserIdInt
34     @JvmOverloads
getSelectedUserIdnull35     fun getSelectedUserId(bypassFlag: Boolean = false): Int {
36         return if (bypassFlag || refactorGetCurrentUser()) {
37             repository.getSelectedUserInfo().id
38         } else {
39             KeyguardUpdateMonitor.getCurrentUser()
40         }
41     }
42 
43     /**
44      * Returns the user ID of the "main user" of the device. This user may have access to certain
45      * features which are limited to at most one user. There will never be more than one main user
46      * on a device.
47      *
48      * <p>Currently, on most form factors the first human user on the device will be the main user;
49      * in the future, the concept may be transferable, so a different user (or even no user at all)
50      * may be designated the main user instead. On other form factors there might not be a main
51      * user.
52      *
53      * <p> When the device doesn't have a main user, this will return {@code null}.
54      *
55      * @see [UserManager.getMainUser]
56      */
57     @UserIdInt
getMainUserIdnull58     fun getMainUserId(): Int? {
59         return repository.mainUserId
60     }
61 }
62