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

Commit 0cb474a6 authored by Adam Bookatz's avatar Adam Bookatz
Browse files

Clarify getProfiles

* Updates the javadoc of getProfiles, and similar methods, to make it
  clear that they return all users in the same profile group (not just
  the user's profiles, but also its parent and siblings, if applicable).
* Update the name of isProfileOf() to make it clear that it's actually
  bidirectional
* Fixes a confusing oddity in isSameProfileGroupNoChecks() in which it
  would return false if the user has an empty profile group, even if
  the two users are literally identical. This seems unexpected and is
  just waiting for an error. It currently doesn't matter because
  * almost all callers already explicitly handled the equal-users case
  * the one caller that didn't, actually should have, although it didn't
    matter because full users cannot have quiet mode anyway.
* Effectively removes hasProfile(). This method cannot handle all
  users, so it needs to be significantly modified. But it is no longer
  ever called anyway (the caller is behind an old flag), so we make it
  clear that is is effectively removed.

Test: atest UserManagerTest UserManagerServiceTest
Bug: 390707384
Flag: EXEMPT bugfix
Change-Id: I51b9f846ad61de8a37efe47a4645013c95996fb3
parent cc373044
Loading
Loading
Loading
Loading
+31 −15
Original line number Diff line number Diff line
@@ -5326,7 +5326,9 @@ public class UserManager {
    }

    /**
     * Returns list of the profiles of userId including userId itself.
     * Returns a list of the users that are associated with userId, including userId itself. This
     * includes the user, its profiles, its parent, and its parent's other profiles, as applicable.
     *
     * Note that this returns both enabled and not enabled profiles. See
     * {@link #getEnabledProfiles(int)} if you need only the enabled ones.
     * <p>Note that this includes all profile types (not including Restricted profiles).
@@ -5334,7 +5336,7 @@ public class UserManager {
     * <p>Requires {@link android.Manifest.permission#MANAGE_USERS} or
     * {@link android.Manifest.permission#CREATE_USERS} or
     * {@link android.Manifest.permission#QUERY_USERS} if userId is not the calling user.
     * @param userId profiles of this user will be returned.
     * @param userId profiles associated with this user (including itself) will be returned.
     * @return the list of profiles.
     * @hide
     */
@@ -5358,12 +5360,13 @@ public class UserManager {
    }

    /**
     * Returns list of the profiles of the given user, including userId itself, as well as the
     * communal profile, if there is one.
     * Returns a list of the users that are associated with userId, including userId itself,
     * as well as the communal profile, if there is one.
     *
     * <p>Note that this returns both enabled and not enabled profiles.
     * <p>Note that this includes all profile types (not including Restricted profiles).
     *
     * @see #getProfiles(int)
     * @hide
     */
    @FlaggedApi(android.multiuser.Flags.FLAG_SUPPORT_COMMUNAL_PROFILE)
@@ -5419,7 +5422,10 @@ public class UserManager {
    }

    /**
     * Returns list of the profiles of userId including userId itself.
     * Returns a list of the enabled users that are associated with userId, including userId itself.
     * This includes the user, its profiles, its parent, and its parent's other profiles, as
     * applicable.
     *
     * Note that this returns only {@link UserInfo#isEnabled() enabled} profiles.
     * <p>Note that this includes all profile types (not including Restricted profiles).
     *
@@ -5447,8 +5453,10 @@ public class UserManager {
    }

    /**
     * Returns a list of UserHandles for profiles associated with the context user, including the
     * user itself.
     * Returns a list of the users that are associated with the context user, including the user
     * itself. This includes the user, its profiles, its parent, and its parent's other profiles,
     * as applicable.
     *
     * <p>Note that this includes all profile types (not including Restricted profiles).
     *
     * @return A non-empty list of UserHandles associated with the context user.
@@ -5465,8 +5473,10 @@ public class UserManager {
    }

    /**
     * Returns a list of ids for enabled profiles associated with the context user including the
     * user itself.
     * Returns a list of the enabled users that are associated with the context user, including the
     * user itself. This includes the user, its profiles, its parent, and its parent's other
     * profiles, as applicable.
     *
     * <p>Note that this includes all profile types (not including Restricted profiles).
     *
     * @return A non-empty list of UserHandles associated with the context user.
@@ -5483,8 +5493,10 @@ public class UserManager {
    }

    /**
     * Returns a list of ids for all profiles associated with the context user including the user
     * itself.
     * Returns a list of all users that are associated with the context user, including the user
     * itself. This includes the user, its profiles, its parent, and its parent's other profiles,
     * as applicable.
     *
     * <p>Note that this includes all profile types (not including Restricted profiles).
     *
     * @return A non-empty list of UserHandles associated with the context user.
@@ -5501,8 +5513,10 @@ public class UserManager {
    }

    /**
     * Returns a list of ids for profiles associated with the context user including the user
     * itself.
     * Returns a list of the users that are associated with the context user, including the user
     * itself. This includes the user, its profiles, its parent, and its parent's other profiles, as
     * applicable.
     *
     * <p>Note that this includes all profile types (not including Restricted profiles).
     *
     * @param enabledOnly whether to return only {@link UserInfo#isEnabled() enabled} profiles
@@ -5528,8 +5542,10 @@ public class UserManager {
    }

    /**
     * Returns a list of ids for profiles associated with the specified user including the user
     * itself.
     * Returns a list of the users that are associated with the specified user, including the user
     * itself. This includes the user, its profiles, its parent, and its parent's other profiles,
     * as applicable.
     *
     * <p>Note that this includes all profile types (not including Restricted profiles).
     *
     * @param userId      id of the user to return profiles for
+4 −2
Original line number Diff line number Diff line
@@ -351,8 +351,10 @@ public abstract class UserManagerInternal {
            boolean excludePreCreated);

    /**
     * Returns an array of ids for profiles associated with the specified user including the user
     * itself.
     * Returns a list of the users that are associated with the specified user, including the user
     * itself. This includes the user, its profiles, its parent, and its parent's other profiles,
     * as applicable.
     *
     * <p>Note that this includes all profile types (not including Restricted profiles).
     *
     * @param userId      id of the user to return profiles for
+26 −24
Original line number Diff line number Diff line
@@ -1638,7 +1638,7 @@ public class UserManagerService extends IUserManager.Stub {
        final int userSize = mUsers.size();
        for (int i = 0; i < userSize; i++) {
            UserInfo profile = mUsers.valueAt(i).info;
            if (!isProfileOf(user, profile)) {
            if (!isSameProfileGroup(user, profile)) {
                continue;
            }
            if (enabledOnly && !profile.isEnabled()) {
@@ -1706,22 +1706,18 @@ public class UserManagerService extends IUserManager.Stub {
        return isSameProfileGroupNoChecks(userId, otherUserId);
    }

    /**
     * Returns whether users are in the same non-empty profile group.
     * Currently, false if empty profile group, even if they are the same user, for whatever reason.
     */
    /** Returns whether users are in the same profile group. */
    private boolean isSameProfileGroupNoChecks(@UserIdInt int userId, int otherUserId) {
        synchronized (mUsersLock) {
            UserInfo userInfo = getUserInfoLU(userId);
            if (userInfo == null || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
            if (userInfo == null) {
                return false;
            }
            UserInfo otherUserInfo = getUserInfoLU(otherUserId);
            if (otherUserInfo == null
                    || otherUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
            if (otherUserInfo == null) {
                return false;
            }
            return userInfo.profileGroupId == otherUserInfo.profileGroupId;
            return isSameProfileGroup(userInfo, otherUserInfo);
        }
    }

@@ -1780,10 +1776,10 @@ public class UserManagerService extends IUserManager.Stub {
        }
    }

    private static boolean isProfileOf(UserInfo user, UserInfo profile) {
        return user.id == profile.id ||
                (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
                && user.profileGroupId == profile.profileGroupId);
    private static boolean isSameProfileGroup(@NonNull UserInfo user1, @NonNull UserInfo user2) {
        return user1.id == user2.id ||
                (user1.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
                && user1.profileGroupId == user2.profileGroupId);
    }

    private String getAvailabilityIntentAction(boolean enableQuietMode, boolean useManagedActions) {
@@ -8413,22 +8409,28 @@ public class UserManagerService extends IUserManager.Stub {
    }

    /**
     * Checks if the given user has a profile associated with it.
     * @param userId The parent user
     * @return
     * Formerly: Checks if the given user has a profile associated with it.
     * Now: Just throws. Do not use it.
     * @param userId The parent user (passing in a profile user is not supported)
     * @deprecated
     */
    boolean hasProfile(@UserIdInt int userId) {
        if (!android.content.pm.Flags.removeCrossUserPermissionHack()) {
            synchronized (mUsersLock) {
                UserInfo userInfo = getUserInfoLU(userId);
                final int userSize = mUsers.size();
                for (int i = 0; i < userSize; i++) {
                    UserInfo profile = mUsers.valueAt(i).info;
                if (userId != profile.id && isProfileOf(userInfo, profile)) {
                    if (userId != profile.id && isSameProfileGroup(userInfo, profile)) {
                        return true;
                    }
                }
                return false;
            }
        } else {
            // TODO(b/332664521): Remove this method entirely. It is no longer used.
            throw new UnsupportedOperationException();
        }
    }

    /**