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

Commit c305b5b2 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add some SpecialUsers annotations to UserManager" into main

parents f8e8edef 1f03fe89
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -30,6 +30,9 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SpecialUsers.CanBeALL;
import android.annotation.SpecialUsers.CanBeNULL;
import android.annotation.SpecialUsers.CannotBeSpecialUser;
import android.annotation.StringDef;
import android.annotation.SuppressAutoDoc;
import android.annotation.SuppressLint;
@@ -3913,7 +3916,8 @@ public class UserManager {
            android.Manifest.permission.MANAGE_USERS,
            android.Manifest.permission.QUERY_USERS,
            android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional = true)
    public @NonNull UserProperties getUserProperties(@NonNull UserHandle userHandle) {
    public @NonNull UserProperties getUserProperties(
            @CannotBeSpecialUser @NonNull UserHandle userHandle) {
        final int userId = userHandle.getIdentifier();

        if (userId < 0 && android.multiuser.Flags.fixGetUserPropertyCache()) {
@@ -6811,7 +6815,7 @@ public class UserManager {
     */
    @SystemApi
    public static final class EnforcingUser implements Parcelable {
        private final @UserIdInt int userId;
        private final @CanBeALL @CanBeNULL @UserIdInt int userId;
        private final @UserRestrictionSource int userRestrictionSource;

        /**
@@ -6856,7 +6860,7 @@ public class UserManager {
         *
         * <p> Will be UserHandle.USER_NULL when restriction is set by the system.
         */
        public UserHandle getUserHandle() {
        public @CanBeALL @CanBeNULL UserHandle getUserHandle() {
            return UserHandle.of(userId);
        }

+30 −22
Original line number Diff line number Diff line
@@ -57,6 +57,10 @@ import android.annotation.DrawableRes;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SpecialUsers.CanBeALL;
import android.annotation.SpecialUsers.CanBeCURRENT;
import android.annotation.SpecialUsers.CanBeNULL;
import android.annotation.SpecialUsers.CannotBeSpecialUser;
import android.annotation.StringRes;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
@@ -952,7 +956,7 @@ public class UserManagerService extends IUserManager.Stub {
    private final UserVisibilityMediator mUserVisibilityMediator;

    @GuardedBy("mUsersLock")
    private @UserIdInt int mBootUser = UserHandle.USER_NULL;
    private @CanBeNULL @UserIdInt int mBootUser = UserHandle.USER_NULL;

    private static UserManagerService sInstance;

@@ -1333,12 +1337,12 @@ public class UserManagerService extends IUserManager.Stub {
    }

    @Override
    public @UserIdInt int getMainUserId() {
    public @CanBeNULL @UserIdInt int getMainUserId() {
        checkQueryOrCreateUsersPermission("get main user id");
        return getMainUserIdUnchecked();
    }

    private @UserIdInt int getMainUserIdUnchecked() {
    private @CanBeNULL @UserIdInt int getMainUserIdUnchecked() {
        synchronized (mUsersLock) {
            final int userSize = mUsers.size();
            for (int i = 0; i < userSize; i++) {
@@ -1351,7 +1355,7 @@ public class UserManagerService extends IUserManager.Stub {
        return UserHandle.USER_NULL;
    }

    private @UserIdInt int getPrivateProfileUserId() {
    private @CanBeNULL @UserIdInt int getPrivateProfileUserId() {
        synchronized (mUsersLock) {
            for (int userId : getUserIds()) {
                UserInfo userInfo = getUserInfoLU(userId);
@@ -1455,7 +1459,7 @@ public class UserManagerService extends IUserManager.Stub {
            "No switchable users found", USER_OPERATION_ERROR_UNKNOWN);
    }

    private @UserIdInt int getFirstSwitchableUser(boolean fullUserOnly) {
    private @CanBeNULL @UserIdInt int getFirstSwitchableUser(boolean fullUserOnly) {
        synchronized (mUsersLock) {
            final int userSize = mUsers.size();
            for (int i = 0; i < userSize; i++) {
@@ -1472,7 +1476,7 @@ public class UserManagerService extends IUserManager.Stub {


    @Override
    public int getPreviousFullUserToEnterForeground() {
    public @CanBeNULL @UserIdInt int getPreviousFullUserToEnterForeground() {
        checkQueryOrCreateUsersPermission("get previous user");
        int previousUser = UserHandle.USER_NULL;
        long latestEnteredTime = 0;
@@ -1496,13 +1500,13 @@ public class UserManagerService extends IUserManager.Stub {
    }

    @Override
    public @UserIdInt int getCommunalProfileId() {
    public @CanBeNULL @UserIdInt int getCommunalProfileId() {
        checkQueryOrCreateUsersPermission("get communal profile user id");
        return getCommunalProfileIdUnchecked();
    }

    /** Returns the currently-designated communal profile, or USER_NULL if not present. */
    private @UserIdInt int getCommunalProfileIdUnchecked() {
    private @CanBeNULL @UserIdInt int getCommunalProfileIdUnchecked() {
        synchronized (mUsersLock) {
            final int userSize = mUsers.size();
            for (int i = 0; i < userSize; i++) {
@@ -2675,7 +2679,7 @@ public class UserManagerService extends IUserManager.Stub {
     * {@link ActivityManagerInternal} is not available yet.
     */
    @VisibleForTesting
    int getCurrentUserId() {
    @CanBeNULL @UserIdInt int getCurrentUserId() {
        ActivityManagerInternal activityManagerInternal = getActivityManagerInternal();
        if (activityManagerInternal == null) {
            Slog.w(LOG_TAG, "getCurrentUserId() called too early, ActivityManagerInternal"
@@ -2974,27 +2978,27 @@ public class UserManagerService extends IUserManager.Stub {
    }

    @VisibleForTesting
    boolean isUserSwitcherEnabled(@UserIdInt int mUserId) {
    boolean isUserSwitcherEnabled(@UserIdInt int userId) {
        boolean multiUserSettingOn = Settings.Global.getInt(mContext.getContentResolver(),
                Settings.Global.USER_SWITCHER_ENABLED,
                Resources.getSystem().getBoolean(com.android.internal
                        .R.bool.config_showUserSwitcherByDefault) ? 1 : 0) != 0;

        return UserManager.supportsMultipleUsers()
                && !hasUserRestriction(DISALLOW_USER_SWITCH, mUserId)
                && !hasUserRestriction(DISALLOW_USER_SWITCH, userId)
                && !UserManager.isDeviceInDemoMode(mContext)
                && multiUserSettingOn;
    }

    @Override
    public boolean isUserSwitcherEnabled(boolean showEvenIfNotActionable,
            @UserIdInt int mUserId) {
        if (!isUserSwitcherEnabled(mUserId)) {
            @UserIdInt int userId) {
        if (!isUserSwitcherEnabled(userId)) {
            return false;
        }
        // The feature is enabled. But is it worth showing?
        return showEvenIfNotActionable
                || !hasUserRestriction(UserManager.DISALLOW_ADD_USER, mUserId) // Can add new user
                || !hasUserRestriction(UserManager.DISALLOW_ADD_USER, userId) // Can add new user
                || areThereMultipleSwitchableUsers(); // There are switchable users
    }

@@ -3427,7 +3431,8 @@ public class UserManagerService extends IUserManager.Stub {
    }

    @VisibleForTesting
    void setUserRestrictionInner(int userId, @NonNull String key, boolean value) {
    void setUserRestrictionInner(
            @CanBeALL @UserIdInt int userId, @NonNull String key, boolean value) {
        if (!UserRestrictionsUtils.isValidRestriction(key)) {
            Slog.e(LOG_TAG, "Setting invalid restriction " + key);
            return;
@@ -3520,10 +3525,11 @@ public class UserManagerService extends IUserManager.Stub {
    /** @return a specific user restriction that's in effect currently. */
    @Override
    public boolean hasUserRestriction(String restrictionKey, @UserIdInt int userId) {
        checkManageOrInteractPermissionIfCallerInOtherProfileGroup(userId, "hasUserRestriction");
        // TODO(b/390455855): Should this be (!userExists(userId) && userId != UserHandle.USER_ALL)?
        if (!userExists(userId)) {
            return false;
        }
        checkManageOrInteractPermissionIfCallerInOtherProfileGroup(userId, "hasUserRestriction");
        return mLocalService.hasUserRestriction(restrictionKey, userId);
    }

@@ -5610,7 +5616,7 @@ public class UserManagerService extends IUserManager.Stub {

    private @NonNull UserInfo createUserInternal(
            @Nullable String name, @NonNull String userType,
            @UserInfoFlag int flags, @UserIdInt int parentId,
            @UserInfoFlag int flags, @CanBeNULL @UserIdInt int parentId,
            @Nullable String[] disallowedPackages)
            throws UserManager.CheckedUserOperationException {

@@ -5643,8 +5649,8 @@ public class UserManagerService extends IUserManager.Stub {

    private @NonNull UserInfo createUserInternalUnchecked(
            @Nullable String name, @NonNull String userType, @UserInfoFlag int flags,
            @UserIdInt int parentId, boolean preCreate, @Nullable String[] disallowedPackages,
            @Nullable Object token)
            @CanBeNULL @UserIdInt int parentId, boolean preCreate,
            @Nullable String[] disallowedPackages, @Nullable Object token)
            throws UserManager.CheckedUserOperationException {

        final int noneUserId = -1;
@@ -6227,9 +6233,11 @@ public class UserManagerService extends IUserManager.Stub {
            if (UserManager.getMaxSupportedUsers() > 1) {
                data.add(FrameworkStatsLog.buildStatsEvent(FrameworkStatsLog.MULTI_USER_INFO,
                        UserManager.getMaxSupportedUsers(),
                        // TODO(b/390455855): is USER_ALL really allowed here?
                        isUserSwitcherEnabled(UserHandle.USER_ALL),
                        UserManager.supportsMultipleUsers()
                                && !hasUserRestriction(UserManager.DISALLOW_ADD_USER,
                                // TODO(b/390455855): is USER_ALL really allowed here?
                                        UserHandle.USER_ALL)));
            }
        } else {
@@ -7580,8 +7588,8 @@ public class UserManagerService extends IUserManager.Stub {
        }
    }

    private void dumpUser(PrintWriter pw, @UserIdInt int userId, StringBuilder sb, long now,
            long nowRealtime) {
    private void dumpUser(PrintWriter pw, @CanBeCURRENT @UserIdInt int userId, StringBuilder sb,
            long now, long nowRealtime) {
        if (userId == UserHandle.USER_CURRENT) {
            final int currentUserId = getCurrentUserId();
            pw.print("Current user: ");
@@ -7755,7 +7763,7 @@ public class UserManagerService extends IUserManager.Stub {
        }

        @Override
        public void setUserRestriction(int userId, @NonNull String key, boolean value) {
        public void setUserRestriction(@UserIdInt int userId, @NonNull String key, boolean value) {
            UserManagerService.this.setUserRestrictionInner(userId, key, value);
        }