Home | History | Annotate | Download | only in users
      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     boolean mDisallowSwitchUser;
     38     RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
     39 
     40     private UserCapabilities() {}
     41 
     42     public static UserCapabilities create(Context context) {
     43         UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
     44         UserCapabilities caps = new UserCapabilities();
     45 
     46         if (!UserManager.supportsMultipleUsers() || Utils.isMonkeyRunning()) {
     47             caps.mEnabled = false;
     48             return caps;
     49         }
     50 
     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;
     59         }
     60         caps.updateAddUserCapabilities(context);
     61         return caps;
     62     }
     63 
     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;
     71         mDisallowAddUser =
     72                 (mEnforcedAdmin != null || hasBaseUserRestriction);
     73         mCanAddUser = true;
     74         if (!mIsAdmin || UserManager.getMaxSupportedUsers() < 2
     75                 || !UserManager.supportsMultipleUsers()
     76                 || mDisallowAddUser) {
     77             mCanAddUser = false;
     78         }
     79 
     80         final boolean canAddUsersWhenLocked = mIsAdmin || Settings.Global.getInt(
     81                 context.getContentResolver(), Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1;
     82         mCanAddGuest = !mIsGuest && !mDisallowAddUser && canAddUsersWhenLocked;
     83 
     84         UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
     85         mDisallowSwitchUser = userManager.hasUserRestriction(UserManager.DISALLOW_USER_SWITCH);
     86     }
     87 
     88     public boolean isAdmin() {
     89         return mIsAdmin;
     90     }
     91 
     92     public boolean disallowAddUser() {
     93         return mDisallowAddUser;
     94     }
     95 
     96     public boolean disallowAddUserSetByAdmin() {
     97         return mDisallowAddUserSetByAdmin;
     98     }
     99 
    100     public RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin() {
    101         return mEnforcedAdmin;
    102     }
    103 
    104 
    105     @Override
    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 +
    117                 '}';
    118     }
    119 }
    120