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