Address API council suggestions for UserManager APIs

Test: atest UserManagerTest
Bug: 149742406
Change-Id: I8b408a17de0282b91fcb5c0b66e9d0404fda54b1
This commit is contained in:
Valentin Iftime
2020-02-19 17:51:17 +01:00
parent fd33a24650
commit 4bf82c8206
3 changed files with 41 additions and 11 deletions

View File

@@ -33,8 +33,8 @@ import android.annotation.UserIdInt;
import android.annotation.WorkerThread;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.app.PropertyInvalidatedCache;
import android.app.admin.DevicePolicyManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.ComponentName;
import android.content.Context;
@@ -63,6 +63,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Manages users and user details on a multi-user system. There are two major categories of
@@ -2706,10 +2707,11 @@ public class UserManager {
Manifest.permission.CREATE_USERS})
@UserHandleAware
public @Nullable UserHandle createProfile(@NonNull String name, @NonNull String userType,
@Nullable String[] disallowedPackages) throws UserOperationException {
@NonNull Set<String> disallowedPackages) throws UserOperationException {
try {
return mService.createProfileForUserWithThrow(name, userType, 0,
mUserId, disallowedPackages).getUserHandle();
mUserId, disallowedPackages.toArray(
new String[disallowedPackages.size()])).getUserHandle();
} catch (ServiceSpecificException e) {
return returnNullOrThrowUserOperationException(e,
mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R);
@@ -3343,19 +3345,46 @@ public class UserManager {
}
/**
* Returns a list of ids for profiles associated with the context user including the user
* itself.
* Returns a list of ids for enabled profiles associated with the context user including the
* user itself.
*
* @param enabledOnly whether to return only {@link UserInfo#isEnabled() enabled} profiles
* @return A non-empty list of UserHandles associated with the calling user.
*
* @hide
*/
@SystemApi
@RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
Manifest.permission.CREATE_USERS}, conditional = true)
@UserHandleAware
public @NonNull List<UserHandle> getUserProfiles(boolean enabledOnly) {
public @NonNull List<UserHandle> getEnabledProfiles() {
return getProfiles(true);
}
/**
* Returns a list of ids for all profiles associated with the context user including the user
* itself.
*
* @return A non-empty list of UserHandles associated with the calling user.
* @hide
*/
@SystemApi
@RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
Manifest.permission.CREATE_USERS}, conditional = true)
@UserHandleAware
public @NonNull List<UserHandle> getAllProfiles() {
return getProfiles(false);
}
/**
* Returns a list of ids for profiles associated with the context user including the user
* itself.
*
* @param enabledOnly whether to return only {@link UserInfo#isEnabled() enabled} profiles
* @return A non-empty list of UserHandles associated with the calling user.
*/
@RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS,
Manifest.permission.CREATE_USERS}, conditional = true)
@UserHandleAware
private @NonNull List<UserHandle> getProfiles(boolean enabledOnly) {
final int[] userIds = getProfileIds(mUserId, enabledOnly);
final List<UserHandle> result = new ArrayList<>(userIds.length);
for (int userId : userIds) {