Merge "Adding user accessor functions to UserManagerHelper." into pi-dev
am: c3724b5e91
Change-Id: If7acce092d6360ca64f1a4115ebd59ef8d185435
This commit is contained in:
@@ -86,7 +86,7 @@ public final class UserManagerHelper {
|
|||||||
* @return List of {@code UserInfo} for each user that is not the current user.
|
* @return List of {@code UserInfo} for each user that is not the current user.
|
||||||
*/
|
*/
|
||||||
public List<UserInfo> getAllUsersExcludesCurrentUser() {
|
public List<UserInfo> getAllUsersExcludesCurrentUser() {
|
||||||
List<UserInfo> others = mUserManager.getUsers(true);
|
List<UserInfo> others = getAllUsers();
|
||||||
|
|
||||||
for (Iterator<UserInfo> iterator = others.iterator(); iterator.hasNext(); ) {
|
for (Iterator<UserInfo> iterator = others.iterator(); iterator.hasNext(); ) {
|
||||||
UserInfo userInfo = iterator.next();
|
UserInfo userInfo = iterator.next();
|
||||||
@@ -98,6 +98,31 @@ public final class UserManagerHelper {
|
|||||||
return others;
|
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
|
// User information accessors
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -224,7 +249,7 @@ public final class UserManagerHelper {
|
|||||||
* @return {@code true} if user is successfully removed, {@code false} otherwise.
|
* @return {@code true} if user is successfully removed, {@code false} otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean removeUser(UserInfo userInfo) {
|
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.");
|
Log.w(TAG, "User " + userInfo.id + " is system user, could not be removed.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,6 +112,54 @@ public class UserManagerHelperTest {
|
|||||||
assertThat(mHelper.getAllUsersExcludesCurrentUser()).contains(otherUser3);
|
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
|
@Test
|
||||||
public void testUserCanBeRemoved() {
|
public void testUserCanBeRemoved() {
|
||||||
UserInfo testInfo = new UserInfo();
|
UserInfo testInfo = new UserInfo();
|
||||||
@@ -208,11 +256,6 @@ public class UserManagerHelperTest {
|
|||||||
// Cannot remove system user.
|
// Cannot remove system user.
|
||||||
assertThat(mHelper.removeUser(mSystemUser)).isFalse();
|
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.
|
// Removing non-current, non-system user, simply calls removeUser.
|
||||||
UserInfo userToRemove = createUserInfoForId(mCurrentUser.id + 2);
|
UserInfo userToRemove = createUserInfoForId(mCurrentUser.id + 2);
|
||||||
mHelper.removeUser(userToRemove);
|
mHelper.removeUser(userToRemove);
|
||||||
|
|||||||
Reference in New Issue
Block a user