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

Commit c3724b5e authored by Jovana Knezevic's avatar Jovana Knezevic Committed by Android (Google) Code Review
Browse files

Merge "Adding user accessor functions to UserManagerHelper." into pi-dev

parents 285797a4 12f105e5
Loading
Loading
Loading
Loading
+27 −2
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ public final class UserManagerHelper {
     * @return List of {@code UserInfo} for each user that is not the current user.
     */
    public List<UserInfo> getAllUsersExcludesCurrentUser() {
        List<UserInfo> others = mUserManager.getUsers(true);
        List<UserInfo> others = getAllUsers();

        for (Iterator<UserInfo> iterator = others.iterator(); iterator.hasNext(); ) {
            UserInfo userInfo = iterator.next();
@@ -98,6 +98,31 @@ public final class UserManagerHelper {
        return others;
    }

    /**
     * Gets all the other users on the system that are not the system user.
     *
     * @return List of {@code UserInfo} for each user that is not the system user.
     */
    public List<UserInfo> getAllUsersExcludesSystemUser() {
        List<UserInfo> others = getAllUsers();

        for (Iterator<UserInfo> iterator = others.iterator(); iterator.hasNext(); ) {
            UserInfo userInfo = iterator.next();
            if (userIsSystemUser(userInfo)) {
                // Remove system user from the list.
                iterator.remove();
            }
        }
        return others;
    }

    /**
     * Gets all the users on the system that are not currently being removed.
     */
    public List<UserInfo> getAllUsers() {
        return mUserManager.getUsers(true /* excludeDying */);
    }

    // User information accessors

    /**
@@ -224,7 +249,7 @@ public final class UserManagerHelper {
     * @return {@code true} if user is successfully removed, {@code false} otherwise.
     */
    public boolean removeUser(UserInfo userInfo) {
        if (userInfo.id == UserHandle.USER_SYSTEM) {
        if (userIsSystemUser(userInfo)) {
            Log.w(TAG, "User " + userInfo.id + " is system user, could not be removed.");
            return false;
        }
+48 −5
Original line number Diff line number Diff line
@@ -112,6 +112,54 @@ public class UserManagerHelperTest {
        assertThat(mHelper.getAllUsersExcludesCurrentUser()).contains(otherUser3);
    }

    @Test
    public void testGetAllUsersExcludesSystemUser() {
        UserInfo otherUser1 = createUserInfoForId(10);
        UserInfo otherUser2 = createUserInfoForId(11);
        UserInfo otherUser3 = createUserInfoForId(12);

        List<UserInfo> testUsers = new ArrayList<>();
        testUsers.add(otherUser1);
        testUsers.add(otherUser2);
        testUsers.add(mSystemUser);
        testUsers.add(otherUser3);

        when(mUserManager.getUsers(true)).thenReturn(testUsers);

        // Should return 3 users that don't have SYSTEM USER id.
        assertThat(mHelper.getAllUsersExcludesSystemUser().size()).isEqualTo(3);
        // Should not contain system user.
        assertThat(mHelper.getAllUsersExcludesSystemUser()).doesNotContain(mSystemUser);
        // Should contain non-system users.
        assertThat(mHelper.getAllUsersExcludesSystemUser()).contains(otherUser1);
        assertThat(mHelper.getAllUsersExcludesSystemUser()).contains(otherUser2);
        assertThat(mHelper.getAllUsersExcludesSystemUser()).contains(otherUser3);
    }

    @Test
    public void testGetAllUsers() {
        int currentUser = UserHandle.myUserId();

        UserInfo otherUser1 = createUserInfoForId(currentUser + 1);
        UserInfo otherUser2 = createUserInfoForId(currentUser - 1);
        UserInfo otherUser3 = createUserInfoForId(currentUser + 2);

        List<UserInfo> testUsers = new ArrayList<>();
        testUsers.add(otherUser1);
        testUsers.add(otherUser2);
        testUsers.add(mCurrentUser);
        testUsers.add(otherUser3);

        when(mUserManager.getUsers(true)).thenReturn(testUsers);

        // Should return 3 users that don't have currentUser id.
        assertThat(mHelper.getAllUsers().size()).isEqualTo(4);
        assertThat(mHelper.getAllUsers()).contains(mCurrentUser);
        assertThat(mHelper.getAllUsers()).contains(otherUser1);
        assertThat(mHelper.getAllUsers()).contains(otherUser2);
        assertThat(mHelper.getAllUsers()).contains(otherUser3);
    }

    @Test
    public void testUserCanBeRemoved() {
        UserInfo testInfo = new UserInfo();
@@ -208,11 +256,6 @@ public class UserManagerHelperTest {
        // Cannot remove system user.
        assertThat(mHelper.removeUser(mSystemUser)).isFalse();

        // Removing current user, calls "switch" to system user.
        mHelper.removeUser(mCurrentUser);
        verify(mActivityManager).switchUser(UserHandle.USER_SYSTEM);
        verify(mUserManager).removeUser(mCurrentUser.id);

        // Removing non-current, non-system user, simply calls removeUser.
        UserInfo userToRemove = createUserInfoForId(mCurrentUser.id + 2);
        mHelper.removeUser(userToRemove);