diff options
author | jovanak <jovanak@google.com> | 2018-06-26 18:45:30 -0700 |
---|---|---|
committer | jovanak <jovanak@google.com> | 2018-06-28 09:37:06 -0700 |
commit | 10e6ff0f6ccadc9661ccbab12e625d9a6648bf27 (patch) | |
tree | b9d743b24ff9abef34e4861efe3828aac6a7d410 /car-lib | |
parent | 1ab32b66ef7467e536df727b0b9419dd31556a8e (diff) | |
download | Car-10e6ff0f6ccadc9661ccbab12e625d9a6648bf27.tar.gz |
Adding methods for keeping track of user limits on the device.
Bug:80197830
Test: atest CarUserManagerHelperTest
Change-Id: I32f06fdff713c3f0b71e9c39c3a6db8804643b8f
Diffstat (limited to 'car-lib')
-rw-r--r-- | car-lib/src/android/car/user/CarUserManagerHelper.java | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/car-lib/src/android/car/user/CarUserManagerHelper.java b/car-lib/src/android/car/user/CarUserManagerHelper.java index c5885477ab..94c701d2b9 100644 --- a/car-lib/src/android/car/user/CarUserManagerHelper.java +++ b/car-lib/src/android/car/user/CarUserManagerHelper.java @@ -184,7 +184,7 @@ public class CarUserManagerHelper { /** * Get user id for the initial user to boot into. This is only applicable for headless - * user 0 model. + * system user model. * * <p>If failed to retrieve the id stored in global settings or the retrieved id does not * exist on device, then return the user with smallest user id. @@ -339,6 +339,24 @@ public class CarUserManagerHelper { } /** + * Gets all users that are not guests. + * + * @return List of {@code UserInfo} for all users who are not guest users. + */ + public List<UserInfo> getAllUsersExceptGuests() { + List<UserInfo> users = getAllUsers(); + + for (Iterator<UserInfo> iterator = users.iterator(); iterator.hasNext(); ) { + UserInfo userInfo = iterator.next(); + if (userInfo.isGuest()) { + // Remove guests. + iterator.remove(); + } + } + return users; + } + + /** * Get all the users except the one with userId passed in. * * @param userId of the user not to be returned. @@ -376,6 +394,62 @@ public class CarUserManagerHelper { return users; } + /** + * Maximum number of users allowed on the device. This includes real users, managed profiles + * and restricted users, but excludes guests. + * + * <p> It excludes system user in headless system user model. + * + * @return Maximum number of users that can be present on the device. + */ + public int getMaxSupportedUsers() { + if (isHeadlessSystemUser()) { + return UserManager.getMaxSupportedUsers() - 1; + } + return UserManager.getMaxSupportedUsers(); + } + + /** + * Get the maximum number of real (non-guest, non-managed profile) users that can be created on + * the device. This is a dynamic value and it decreases with the increase of the number of + * managed profiles on the device. + * + * <p> It excludes system user in headless system user model. + * + * @return Maximum number of real users that can be created. + */ + public int getMaxSupportedRealUsers() { + return getMaxSupportedUsers() - getManagedProfilesCount(); + } + + /** + * Returns true if the maximum number of users on the device has been reached, false otherwise. + */ + public boolean isUserLimitReached() { + int countNonGuestUsers = getAllUsersExceptGuests().size(); + int maxSupportedUsers = UserManager.getMaxSupportedUsers(); + + if (countNonGuestUsers > maxSupportedUsers) { + Log.e(TAG, "There are more users on the device than allowed."); + return true; + } + + return getAllUsersExceptGuests().size() == UserManager.getMaxSupportedUsers(); + } + + private int getManagedProfilesCount() { + List<UserInfo> users = getAllUsers(); + + // Count all users that are managed profiles of another user. + int managedProfilesCount = 0; + for (UserInfo user : users) { + if (user.isManagedProfile()) { + managedProfilesCount++; + } + } + return managedProfilesCount; + } + // User information accessors /** |