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     /**
28      * Returns `true` if it's possible for the given user to add a guest user to the device; `false`
29      * otherwise.
30      */
canCreateGuestnull31     fun canCreateGuest(
32         manager: UserManager,
33         repository: UserRepository,
34         isUserSwitcherEnabled: Boolean,
35         canAddUsersWhenLockedOrDeviceUnlocked: Boolean,
36     ): Boolean {
37         return canAddMoreUsers(
38             manager,
39             repository,
40             isUserSwitcherEnabled,
41             canAddUsersWhenLockedOrDeviceUnlocked,
42             UserManager.USER_TYPE_FULL_GUEST
43         )
44     }
45 
46     /**
47      * Returns `true` if it's possible for the given user to add a user to the device; `false`
48      * otherwise.
49      */
canCreateUsernull50     fun canCreateUser(
51         manager: UserManager,
52         repository: UserRepository,
53         isUserSwitcherEnabled: Boolean,
54         canAddUsersWhenLockedOrDeviceUnlocked: Boolean,
55     ): Boolean {
56         return canAddMoreUsers(
57             manager,
58             repository,
59             isUserSwitcherEnabled,
60             canAddUsersWhenLockedOrDeviceUnlocked,
61             UserManager.USER_TYPE_FULL_SECONDARY
62         )
63     }
64 
65     /**
66      * Returns `true` if it's possible to add a supervised user to the device given the current
67      * user; false` otherwise.
68      */
canCreateSupervisedUsernull69     fun canCreateSupervisedUser(
70         manager: UserManager,
71         repository: UserRepository,
72         isUserSwitcherEnabled: Boolean,
73         canAddUsersWhenLockedOrDeviceUnlocked: Boolean,
74         supervisedUserPackageName: String?
75     ): Boolean {
76         if (supervisedUserPackageName.isNullOrEmpty()) {
77             return false
78         }
79 
80         return canCreateUser(
81             manager,
82             repository,
83             isUserSwitcherEnabled,
84             canAddUsersWhenLockedOrDeviceUnlocked
85         )
86     }
87 
canManageUsersnull88     fun canManageUsers(repository: UserRepository, isUserSwitcherEnabled: Boolean): Boolean {
89         return isUserSwitcherEnabled && repository.getSelectedUserInfo().isAdmin
90     }
91 
92     /**
93      * Returns `true` if it's possible to add a user to the device for the given user type; false
94      * otherwise.
95      */
canAddMoreUsersnull96     private fun canAddMoreUsers(
97         manager: UserManager,
98         repository: UserRepository,
99         isUserSwitcherEnabled: Boolean,
100         canAddUsersWhenLockedOrDeviceUnlocked: Boolean,
101         userType: String
102     ): Boolean {
103         if (!isUserSwitcherEnabled || !canAddUsersWhenLockedOrDeviceUnlocked) {
104             return false
105         }
106 
107         return currentUserCanCreateUsers(manager, repository) && manager.canAddMoreUsers(userType)
108     }
109 
110     /**
111      * Returns `true` if the current user is allowed to add users to the device; `false` otherwise.
112      */
currentUserCanCreateUsersnull113     private fun currentUserCanCreateUsers(
114         manager: UserManager,
115         repository: UserRepository
116     ): Boolean {
117         val currentUser = repository.getSelectedUserInfo()
118         if (!currentUser.isAdmin && currentUser.id != UserHandle.USER_SYSTEM) {
119             return false
120         }
121         return !manager.hasUserRestrictionForUser(
122             UserManager.DISALLOW_ADD_USER,
123             UserHandle.of(currentUser.id)
124         ) && !manager.hasUserRestrictionForUser(UserManager.DISALLOW_ADD_USER, UserHandle.SYSTEM)
125     }
126 }
127