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