Use UM.canAddMoreUsers and UM.isUserTypeEnabled

Settings shouldn't allow creating users if the type is diabled, and it
shouldn't allow creating more of a particular user type if no more can
be created (even if more can be created of other user types).
Previously, Settings didn't take into account the user type when
querying whether more users could be created.

Bug: 192577100
Test: com.android.settings.users.UserSettingsTest
Change-Id: I065478fd14779f528be4edce8ae215391a752ef4
This commit is contained in:
Adam Bookatz
2021-11-23 14:55:28 -08:00
parent e4e252aa95
commit 8ca17560e5
3 changed files with 24 additions and 10 deletions

View File

@@ -58,7 +58,9 @@ public class UserCapabilities {
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
Context.DEVICE_POLICY_SERVICE);
// No restricted profiles for tablets with a device owner, or phones.
if (dpm.isDeviceManaged() || Utils.isVoiceCapable(context)) {
if (dpm.isDeviceManaged()
|| Utils.isVoiceCapable(context)
|| !userManager.isUserTypeEnabled(UserManager.USER_TYPE_FULL_RESTRICTED)) {
caps.mCanAddRestrictedProfile = false;
}
caps.updateAddUserCapabilities(context);
@@ -76,15 +78,19 @@ public class UserCapabilities {
mDisallowAddUser = (mEnforcedAdmin != null || hasBaseUserRestriction);
mUserSwitcherEnabled = userManager.isUserSwitcherEnabled();
mCanAddUser = true;
if (!mIsAdmin || UserManager.getMaxSupportedUsers() < 2
if (!mIsAdmin
|| UserManager.getMaxSupportedUsers() < 2
|| !UserManager.supportsMultipleUsers()
|| mDisallowAddUser) {
|| mDisallowAddUser
|| (!userManager.isUserTypeEnabled(UserManager.USER_TYPE_FULL_SECONDARY)
&& !mCanAddRestrictedProfile)) {
mCanAddUser = false;
}
final boolean canAddUsersWhenLocked = mIsAdmin || Settings.Global.getInt(
context.getContentResolver(), Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1;
mCanAddGuest = !mIsGuest && !mDisallowAddUser && canAddUsersWhenLocked;
mCanAddGuest = !mIsGuest && !mDisallowAddUser && canAddUsersWhenLocked
&& userManager.isUserTypeEnabled(UserManager.USER_TYPE_FULL_GUEST);
mDisallowSwitchUser = userManager.hasUserRestriction(UserManager.DISALLOW_USER_SWITCH);
}