1 /*
2  * Copyright (C) 2016 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 package com.android.settings.users;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 import android.os.UserHandle;
23 import android.os.UserManager;
24 import android.provider.Settings;
25 import com.android.settings.Utils;
26 import com.android.settingslib.RestrictedLockUtils;
27 
28 public class UserCapabilities {
29     boolean mEnabled = true;
30     boolean mCanAddUser = true;
31     boolean mCanAddRestrictedProfile = true;
32     boolean mIsAdmin;
33     boolean mIsGuest;
34     boolean mCanAddGuest;
35     boolean mDisallowAddUser;
36     boolean mDisallowAddUserSetByAdmin;
37     RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
38 
UserCapabilities()39     private UserCapabilities() {}
40 
create(Context context)41     public static UserCapabilities create(Context context) {
42         UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
43         UserCapabilities caps = new UserCapabilities();
44         if (!UserManager.supportsMultipleUsers() || Utils.isMonkeyRunning()) {
45             caps.mEnabled = false;
46             return caps;
47         }
48 
49         final UserInfo myUserInfo = userManager.getUserInfo(UserHandle.myUserId());
50         caps.mIsGuest = myUserInfo.isGuest();
51         caps.mIsAdmin = myUserInfo.isAdmin();
52         DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
53                 Context.DEVICE_POLICY_SERVICE);
54         // No restricted profiles for tablets with a device owner, or phones.
55         if (dpm.isDeviceManaged() || Utils.isVoiceCapable(context)) {
56             caps.mCanAddRestrictedProfile = false;
57         }
58         caps.updateAddUserCapabilities(context);
59         return caps;
60     }
61 
updateAddUserCapabilities(Context context)62     public void updateAddUserCapabilities(Context context) {
63         mEnforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(context,
64                 UserManager.DISALLOW_ADD_USER, UserHandle.myUserId());
65         final boolean hasBaseUserRestriction = RestrictedLockUtils.hasBaseUserRestriction(
66                 context, UserManager.DISALLOW_ADD_USER, UserHandle.myUserId());
67         mDisallowAddUserSetByAdmin =
68                 mEnforcedAdmin != null && !hasBaseUserRestriction;
69         mDisallowAddUser =
70                 (mEnforcedAdmin != null || hasBaseUserRestriction);
71         mCanAddUser = true;
72         if (!mIsAdmin || UserManager.getMaxSupportedUsers() < 2
73                 || !UserManager.supportsMultipleUsers()
74                 || mDisallowAddUser) {
75             mCanAddUser = false;
76         }
77 
78         final boolean canAddUsersWhenLocked = mIsAdmin || Settings.Global.getInt(
79                 context.getContentResolver(), Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1;
80         mCanAddGuest = !mIsGuest && !mDisallowAddUser && canAddUsersWhenLocked;
81     }
82 
isAdmin()83     public boolean isAdmin() {
84         return mIsAdmin;
85     }
86 
disallowAddUser()87     public boolean disallowAddUser() {
88         return mDisallowAddUser;
89     }
90 
disallowAddUserSetByAdmin()91     public boolean disallowAddUserSetByAdmin() {
92         return mDisallowAddUserSetByAdmin;
93     }
94 
getEnforcedAdmin()95     public RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin() {
96         return mEnforcedAdmin;
97     }
98 
99 
100     @Override
toString()101     public String toString() {
102         return "UserCapabilities{" +
103                 "mEnabled=" + mEnabled +
104                 ", mCanAddUser=" + mCanAddUser +
105                 ", mCanAddRestrictedProfile=" + mCanAddRestrictedProfile +
106                 ", mIsAdmin=" + mIsAdmin +
107                 ", mIsGuest=" + mIsGuest +
108                 ", mCanAddGuest=" + mCanAddGuest +
109                 ", mDisallowAddUser=" + mDisallowAddUser +
110                 ", mEnforcedAdmin=" + mEnforcedAdmin +
111                 '}';
112     }
113 }
114