diff --git a/packages/SettingsLib/src/com/android/settingslib/users/UserManagerHelper.java b/packages/SettingsLib/src/com/android/settingslib/users/UserManagerHelper.java index cc69e0ec28318..c4ca339b39be0 100644 --- a/packages/SettingsLib/src/com/android/settingslib/users/UserManagerHelper.java +++ b/packages/SettingsLib/src/com/android/settingslib/users/UserManagerHelper.java @@ -86,7 +86,7 @@ public final class UserManagerHelper { * @return List of {@code UserInfo} for each user that is not the current user. */ public List getAllUsersExcludesCurrentUser() { - List others = mUserManager.getUsers(true); + List others = getAllUsers(); for (Iterator 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 getAllUsersExcludesSystemUser() { + List others = getAllUsers(); + + for (Iterator 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 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; } diff --git a/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/UserManagerHelperTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/UserManagerHelperTest.java index 325ef3a68aa6d..3f1fcbb2966b0 100644 --- a/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/UserManagerHelperTest.java +++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/users/UserManagerHelperTest.java @@ -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 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 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);