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

Commit 6cf3567b authored by Brahim Chikhaoui's avatar Brahim Chikhaoui
Browse files

Refactor getBootUserUnchecked() to extract a couple of methods

Bug: 374926694
Change-Id: I5e2dd362e3ed5905182fba6de7f0fdd3edab93f1
Flag: EXEMPT refactor of the code
Test: manually build and deploy in DUT
Test: atest FrameworksMockingServicesTests:com.android.server.pm.UserManagerServiceTest
parent 7b474b64
Loading
Loading
Loading
Loading
+35 −22
Original line number Original line Diff line number Diff line
@@ -1395,6 +1395,14 @@ public class UserManagerService extends IUserManager.Stub {
                    .getBoolean(com.android.internal.R.bool.config_bootToHeadlessSystemUser)) {
                    .getBoolean(com.android.internal.R.bool.config_bootToHeadlessSystemUser)) {
                return UserHandle.USER_SYSTEM;
                return UserHandle.USER_SYSTEM;
            }
            }
            return getPreviousOrFirstSwitchableUser();
        }
        // Not HSUM, return system user.
        return UserHandle.USER_SYSTEM;
    }

    private @UserIdInt int getPreviousOrFirstSwitchableUser()
            throws UserManager.CheckedUserOperationException {
        // Return the previous foreground user, if there is one.
        // Return the previous foreground user, if there is one.
        final int previousUser = getPreviousFullUserToEnterForeground();
        final int previousUser = getPreviousFullUserToEnterForeground();
        if (previousUser != UserHandle.USER_NULL) {
        if (previousUser != UserHandle.USER_NULL) {
@@ -1402,24 +1410,29 @@ public class UserManagerService extends IUserManager.Stub {
            return previousUser;
            return previousUser;
        }
        }
        // No previous user. Return the first switchable user if there is one.
        // No previous user. Return the first switchable user if there is one.
        final int firstSwitchableUser = getFirstSwitchableUser();
        if (firstSwitchableUser != UserHandle.USER_NULL) {
            Slogf.i(LOG_TAG,
                    "Boot user is first switchable user %d", firstSwitchableUser);
            return firstSwitchableUser;
        }
        // No switchable users found. Uh oh!
        throw new UserManager.CheckedUserOperationException(
            "No switchable users found", USER_OPERATION_ERROR_UNKNOWN);
    }

    private @UserIdInt int getFirstSwitchableUser() {
        synchronized (mUsersLock) {
        synchronized (mUsersLock) {
            final int userSize = mUsers.size();
            final int userSize = mUsers.size();
            for (int i = 0; i < userSize; i++) {
            for (int i = 0; i < userSize; i++) {
                final UserData userData = mUsers.valueAt(i);
                final UserData userData = mUsers.valueAt(i);
                if (userData.info.supportsSwitchToByUser()) {
                if (userData.info.supportsSwitchToByUser()) {
                    int firstSwitchable = userData.info.id;
                    int firstSwitchable = userData.info.id;
                        Slogf.i(LOG_TAG,
                                "Boot user is first switchable user %d", firstSwitchable);
                    return firstSwitchable;
                    return firstSwitchable;
                }
                }
            }
            }
        }
        }
            // No switchable users found. Uh oh!
       return UserHandle.USER_NULL;
            throw new UserManager.CheckedUserOperationException(
                    "No switchable users found", USER_OPERATION_ERROR_UNKNOWN);
        }
        // Not HSUM, return system user.
        return UserHandle.USER_SYSTEM;
   }
   }