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.domain.interactor
19 
20 import android.os.UserHandle
21 import android.os.UserManager
22 import com.android.systemui.user.data.repository.UserRepository
23 
24 /** Utilities related to user management actions. */
25 object UserActionsUtil {
26 
27     /** Returns `true` if it's possible to add a guest user to the device; `false` otherwise. */
28     fun canCreateGuest(
29         manager: UserManager,
30         repository: UserRepository,
31         isUserSwitcherEnabled: Boolean,
32         isAddUsersFromLockScreenEnabled: Boolean,
33     ): Boolean {
34         if (!isUserSwitcherEnabled) {
35             return false
36         }
37 
38         return currentUserCanCreateUsers(manager, repository) ||
39             anyoneCanCreateUsers(manager, isAddUsersFromLockScreenEnabled)
40     }
41 
42     /** Returns `true` if it's possible to add a user to the device; `false` otherwise. */
43     fun canCreateUser(
44         manager: UserManager,
45         repository: UserRepository,
46         isUserSwitcherEnabled: Boolean,
47         isAddUsersFromLockScreenEnabled: Boolean,
48     ): Boolean {
49         if (!isUserSwitcherEnabled) {
50             return false
51         }
52 
53         if (
54             !currentUserCanCreateUsers(manager, repository) &&
55                 !anyoneCanCreateUsers(manager, isAddUsersFromLockScreenEnabled)
56         ) {
57             return false
58         }
59 
60         return manager.canAddMoreUsers(UserManager.USER_TYPE_FULL_SECONDARY)
61     }
62 
63     /**
64      * Returns `true` if it's possible to add a supervised user to the device; `false` otherwise.
65      */
66     fun canCreateSupervisedUser(
67         manager: UserManager,
68         repository: UserRepository,
69         isUserSwitcherEnabled: Boolean,
70         isAddUsersFromLockScreenEnabled: Boolean,
71         supervisedUserPackageName: String?
72     ): Boolean {
73         if (supervisedUserPackageName.isNullOrEmpty()) {
74             return false
75         }
76 
77         return canCreateUser(
78             manager,
79             repository,
80             isUserSwitcherEnabled,
81             isAddUsersFromLockScreenEnabled
82         )
83     }
84 
85     fun canManageUsers(
86         repository: UserRepository,
87         isUserSwitcherEnabled: Boolean,
88         isAddUsersFromLockScreenEnabled: Boolean,
89     ): Boolean {
90         return isUserSwitcherEnabled &&
91             (repository.getSelectedUserInfo().isAdmin || isAddUsersFromLockScreenEnabled)
92     }
93 
94     /**
95      * Returns `true` if the current user is allowed to add users to the device; `false` otherwise.
96      */
97     private fun currentUserCanCreateUsers(
98         manager: UserManager,
99         repository: UserRepository,
100     ): Boolean {
101         val currentUser = repository.getSelectedUserInfo()
102         if (!currentUser.isAdmin && currentUser.id != UserHandle.USER_SYSTEM) {
103             return false
104         }
105 
106         return systemCanCreateUsers(manager)
107     }
108 
109     /** Returns `true` if the system can add users to the device; `false` otherwise. */
110     private fun systemCanCreateUsers(
111         manager: UserManager,
112     ): Boolean {
113         return !manager.hasBaseUserRestriction(UserManager.DISALLOW_ADD_USER, UserHandle.SYSTEM)
114     }
115 
116     /** Returns `true` if it's allowed to add users to the device at all; `false` otherwise. */
117     private fun anyoneCanCreateUsers(
118         manager: UserManager,
119         isAddUsersFromLockScreenEnabled: Boolean,
120     ): Boolean {
121         return systemCanCreateUsers(manager) && isAddUsersFromLockScreenEnabled
122     }
123 }
124