Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ac0a1235 authored by Adam Bookatz's avatar Adam Bookatz
Browse files

UM.canAddMoreUsers(userType)

Introduces an API for whether more users can be added of the given user
type.

Bug: 192577100
Bug: 175795666
Bug: 189937255
Bug: 205100630
Test: atest com.android.server.pm.UserManagerTest#testAddTooManyUsers
Change-Id: I1c705d65d1c083a60fd4e058456dcdc4ffa0fffb
parent d7c142e1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ interface IUserManager {
    List<UserInfo> getUsers(boolean excludePartial, boolean excludeDying, boolean excludePreCreated);
    List<UserInfo> getProfiles(int userId, boolean enabledOnly);
    int[] getProfileIds(int userId, boolean enabledOnly);
    boolean canAddMoreUsersOfType(in String userType);
    boolean canAddMoreProfilesToUser(in String userType, int userId, boolean allowedToRemoveOne);
    boolean canAddMoreManagedProfiles(int userId, boolean allowedToRemoveOne);
    UserInfo getProfileParent(int userId);
+28 −3
Original line number Diff line number Diff line
@@ -3846,13 +3846,19 @@ public class UserManager {
    }

    /**
     * Checks whether it's possible to add more users. Caller must hold the MANAGE_USERS
     * permission.
     * Checks whether it's possible to add more users.
     *
     * @return true if more users can be added, false if limit has been reached.
     *
     * @deprecated use {@link #canAddMoreUsers(String)} instead.
     *
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.MANAGE_USERS)
    @Deprecated
    @RequiresPermission(anyOf = {
            android.Manifest.permission.MANAGE_USERS,
            android.Manifest.permission.CREATE_USERS
    })
    public boolean canAddMoreUsers() {
        // TODO(b/142482943): UMS has different logic, excluding Demo and Profile from counting. Why
        //                    not here? The logic is inconsistent. See UMS.canAddMoreManagedProfiles
@@ -3868,6 +3874,25 @@ public class UserManager {
        return aliveUserCount < getMaxSupportedUsers();
    }

    /**
     * Checks whether it is possible to add more users of the given user type.
     *
     * @param userType the type of user, such as {@link UserManager#USER_TYPE_FULL_SECONDARY}.
     * @return true if more users of the given type can be added, otherwise false.
     * @hide
     */
    @RequiresPermission(anyOf = {
            android.Manifest.permission.MANAGE_USERS,
            android.Manifest.permission.CREATE_USERS
    })
    public boolean canAddMoreUsers(@NonNull String userType) {
        try {
            return canAddMoreUsers() && mService.canAddMoreUsersOfType(userType);
        } catch (RemoteException re) {
            throw re.rethrowFromSystemServer();
        }
    }

    /**
     * Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
     * permission.
+4 −4
Original line number Diff line number Diff line
@@ -23,12 +23,10 @@ import android.accounts.AccountManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.UserInfo;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

@@ -44,6 +42,8 @@ public class ConfirmUserCreationActivity extends AlertActivity

    private static final String TAG = "CreateUser";

    private static final String USER_TYPE = UserManager.USER_TYPE_FULL_SECONDARY;

    private String mUserName;
    private String mAccountName;
    private String mAccountType;
@@ -103,7 +103,7 @@ public class ConfirmUserCreationActivity extends AlertActivity
        boolean cantCreateUser = mUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER)
                || !mUserManager.isAdminUser();
        // Check the system state and user count
        boolean cantCreateAnyMoreUsers = !mUserManager.canAddMoreUsers();
        boolean cantCreateAnyMoreUsers = !mUserManager.canAddMoreUsers(USER_TYPE);
        // Check the account existence
        final Account account = new Account(mAccountName, mAccountType);
        boolean accountExists = mAccountName != null && mAccountType != null
@@ -130,7 +130,7 @@ public class ConfirmUserCreationActivity extends AlertActivity
        setResult(RESULT_CANCELED);
        if (which == BUTTON_POSITIVE && mCanProceed) {
            Log.i(TAG, "Ok, creating user");
            UserInfo user = mUserManager.createUser(mUserName, 0);
            UserInfo user = mUserManager.createUser(mUserName, USER_TYPE, 0);
            if (user == null) {
                Log.e(TAG, "Couldn't create user");
                finish();
+1 −1
Original line number Diff line number Diff line
@@ -351,7 +351,7 @@ public class UserSwitcherController implements Dumpable {
                boolean canCreateGuest = (currentUserCanCreateUsers || anyoneCanCreateUsers)
                        && guestRecord == null;
                boolean canCreateUser = (currentUserCanCreateUsers || anyoneCanCreateUsers)
                        && mUserManager.canAddMoreUsers();
                        && mUserManager.canAddMoreUsers(UserManager.USER_TYPE_FULL_SECONDARY);
                boolean createIsRestricted = !addUsersWhenLocked;

                if (guestRecord == null) {
+2 −1
Original line number Diff line number Diff line
@@ -124,7 +124,8 @@ class UserSwitcherControllerTest : SysuiTestCase() {
        mContext.addMockSystemService(Context.FINGERPRINT_SERVICE,
                mock(FingerprintManager::class.java))

        `when`(userManager.canAddMoreUsers()).thenReturn(true)
        `when`(userManager.canAddMoreUsers(eq(UserManager.USER_TYPE_FULL_SECONDARY)))
                .thenReturn(true)
        `when`(notificationShadeWindowView.context).thenReturn(context)

        userSwitcherController = UserSwitcherController(
Loading