2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.users;
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;
28 public class UserCapabilities {
29 boolean mEnabled = true;
30 boolean mCanAddUser = true;
31 boolean mCanAddRestrictedProfile = true;
35 boolean mDisallowAddUser;
36 boolean mDisallowAddUserSetByAdmin;
37 boolean mDisallowSwitchUser;
38 RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
40 private UserCapabilities() {}
42 public static UserCapabilities create(Context context) {
43 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
44 UserCapabilities caps = new UserCapabilities();
46 if (!UserManager.supportsMultipleUsers() || Utils.isMonkeyRunning()) {
47 caps.mEnabled = false;
51 final UserInfo myUserInfo = userManager.getUserInfo(UserHandle.myUserId());
52 caps.mIsGuest = myUserInfo.isGuest();
53 caps.mIsAdmin = myUserInfo.isAdmin();
54 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
55 Context.DEVICE_POLICY_SERVICE);
56 // No restricted profiles for tablets with a device owner, or phones.
57 if (dpm.isDeviceManaged() || Utils.isVoiceCapable(context)) {
58 caps.mCanAddRestrictedProfile = false;
60 caps.updateAddUserCapabilities(context);
64 public void updateAddUserCapabilities(Context context) {
65 mEnforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(context,
66 UserManager.DISALLOW_ADD_USER, UserHandle.myUserId());
67 final boolean hasBaseUserRestriction = RestrictedLockUtils.hasBaseUserRestriction(
68 context, UserManager.DISALLOW_ADD_USER, UserHandle.myUserId());
69 mDisallowAddUserSetByAdmin =
70 mEnforcedAdmin != null && !hasBaseUserRestriction;
72 (mEnforcedAdmin != null || hasBaseUserRestriction);
74 if (!mIsAdmin || UserManager.getMaxSupportedUsers() < 2
75 || !UserManager.supportsMultipleUsers()
76 || mDisallowAddUser) {
80 final boolean canAddUsersWhenLocked = mIsAdmin || Settings.Global.getInt(
81 context.getContentResolver(), Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1;
82 mCanAddGuest = !mIsGuest && !mDisallowAddUser && canAddUsersWhenLocked;
84 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
85 mDisallowSwitchUser = userManager.hasUserRestriction(UserManager.DISALLOW_USER_SWITCH);
88 public boolean isAdmin() {
92 public boolean disallowAddUser() {
93 return mDisallowAddUser;
96 public boolean disallowAddUserSetByAdmin() {
97 return mDisallowAddUserSetByAdmin;
100 public RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin() {
101 return mEnforcedAdmin;
106 public String toString() {
107 return "UserCapabilities{" +
108 "mEnabled=" + mEnabled +
109 ", mCanAddUser=" + mCanAddUser +
110 ", mCanAddRestrictedProfile=" + mCanAddRestrictedProfile +
111 ", mIsAdmin=" + mIsAdmin +
112 ", mIsGuest=" + mIsGuest +
113 ", mCanAddGuest=" + mCanAddGuest +
114 ", mDisallowAddUser=" + mDisallowAddUser +
115 ", mEnforcedAdmin=" + mEnforcedAdmin +
116 ", mDisallowSwitchUser=" + mDisallowSwitchUser +