Merge "Allow creating a managed profile if there is only one user."

This commit is contained in:
Fyodor Kupolov
2015-05-19 17:47:02 +00:00
committed by Gerrit Code Review
3 changed files with 50 additions and 10 deletions

View File

@@ -35,6 +35,7 @@ interface IUserManager {
Bitmap getUserIcon(int userHandle);
List<UserInfo> getUsers(boolean excludeDying);
List<UserInfo> getProfiles(int userHandle, boolean enabledOnly);
boolean canAddMoreManagedProfiles();
UserInfo getProfileParent(int userHandle);
UserInfo getUserInfo(int userHandle);
boolean isRestricted();

View File

@@ -885,6 +885,22 @@ public class UserManager {
return aliveUserCount < getMaxSupportedUsers();
}
/**
* Checks whether it's possible to add more managed profiles. Caller must hold the MANAGE_USERS
* permission.
*
* @return true if more managed profiles can be added, false if limit has been reached.
* @hide
*/
public boolean canAddMoreManagedProfiles() {
try {
return mService.canAddMoreManagedProfiles();
} catch (RemoteException re) {
Log.w(TAG, "Could not check if we can add more managed profiles", re);
return false;
}
}
/**
* Returns list of the profiles of userHandle including
* userHandle itself.

View File

@@ -537,6 +537,28 @@ public class UserManagerService extends IUserManager.Stub {
* Check if we've hit the limit of how many users can be created.
*/
private boolean isUserLimitReachedLocked() {
return getAliveUsersExcludingGuestsCountLocked() >= UserManager.getMaxSupportedUsers();
}
@Override
public boolean canAddMoreManagedProfiles() {
checkManageUsersPermission("check if more managed profiles can be added.");
if (ActivityManager.isLowRamDeviceStatic()) {
return false;
}
synchronized(mPackagesLock) {
// Limit number of managed profiles that can be created
if (numberOfUsersOfTypeLocked(UserInfo.FLAG_MANAGED_PROFILE, true)
>= MAX_MANAGED_PROFILES) {
return false;
}
int usersCount = getAliveUsersExcludingGuestsCountLocked();
// We allow creating a managed profile in the special case where there is only one user.
return usersCount == 1 || usersCount < UserManager.getMaxSupportedUsers();
}
}
private int getAliveUsersExcludingGuestsCountLocked() {
int aliveUserCount = 0;
final int totalUserCount = mUsers.size();
// Skip over users being removed
@@ -547,7 +569,7 @@ public class UserManagerService extends IUserManager.Stub {
aliveUserCount++;
}
}
return aliveUserCount >= UserManager.getMaxSupportedUsers();
return aliveUserCount;
}
/**
@@ -1152,7 +1174,11 @@ public class UserManagerService extends IUserManager.Stub {
Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled.");
return null;
}
if (ActivityManager.isLowRamDeviceStatic()) {
return null;
}
final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0;
final long ident = Binder.clearCallingIdentity();
UserInfo userInfo = null;
try {
@@ -1163,21 +1189,18 @@ public class UserManagerService extends IUserManager.Stub {
parent = getUserInfoLocked(parentId);
if (parent == null) return null;
}
// If we're not adding a guest user and the limit has been reached,
// cannot add a user.
if (!isGuest && isUserLimitReachedLocked()) {
if (isManagedProfile && !canAddMoreManagedProfiles()) {
return null;
}
if (!isGuest && !isManagedProfile && isUserLimitReachedLocked()) {
// If we're not adding a guest user or a managed profile and the limit has
// been reached, cannot add a user.
return null;
}
// If we're adding a guest and there already exists one, bail.
if (isGuest && findCurrentGuestUserLocked() != null) {
return null;
}
// Limit number of managed profiles that can be created
if ((flags & UserInfo.FLAG_MANAGED_PROFILE) != 0
&& numberOfUsersOfTypeLocked(UserInfo.FLAG_MANAGED_PROFILE, true)
>= MAX_MANAGED_PROFILES) {
return null;
}
int userId = getNextAvailableIdLocked();
userInfo = new UserInfo(userId, name, null, flags);
File userPath = new File(mBaseUserPath, Integer.toString(userId));