diff options
| author | TreeHugger Robot <treehugger-gerrit@google.com> | 2018-02-28 00:39:33 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2018-02-28 00:39:33 +0000 |
| commit | 7e96e0a1376ea10d795d50e9790ac96080cea2ed (patch) | |
| tree | aae3034c66c1a5d3a4566810cda83a2e2de542b2 /core/java/android | |
| parent | 2016c0f4f6de20411257ed84d4fc1aa3d6226d41 (diff) | |
| parent | 89386bacc61a4478559fd3e6263bb1d2576158c8 (diff) | |
Merge "createAndManageUser should communicate reason of failure to caller"
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/app/admin/DevicePolicyManager.java | 97 | ||||
| -rw-r--r-- | core/java/android/os/UserManager.java | 79 |
2 files changed, 102 insertions, 74 deletions
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java index 1c3f34a2c8db..7b6a28810f3b 100644 --- a/core/java/android/app/admin/DevicePolicyManager.java +++ b/core/java/android/app/admin/DevicePolicyManager.java @@ -56,8 +56,11 @@ import android.os.PersistableBundle; import android.os.Process; import android.os.RemoteCallback; import android.os.RemoteException; +import android.os.ServiceSpecificException; import android.os.UserHandle; import android.os.UserManager; +import android.os.UserManager.UserOperationException; +import android.os.UserManager.UserOperationResult; import android.provider.ContactsContract.Directory; import android.provider.Settings; import android.security.AttestedKeyPair; @@ -6572,6 +6575,9 @@ public class DevicePolicyManager { * <p> * If the adminExtras are not null, they will be stored on the device until the user is started * for the first time. Then the extras will be passed to the admin when onEnable is called. + * <p>From {@link android.os.Build.VERSION_CODES#P} onwards, if targeting + * {@link android.os.Build.VERSION_CODES#P}, throws {@link UserOperationException} instead of + * returning {@code null} on failure. * * @param admin Which {@link DeviceAdminReceiver} this request is associated with. * @param name The user's name. @@ -6586,6 +6592,9 @@ public class DevicePolicyManager { * @return the {@link android.os.UserHandle} object for the created user, or {@code null} if the * user could not be created. * @throws SecurityException if {@code admin} is not a device owner. + * @throws UserOperationException if the user could not be created and the calling app is + * targeting {@link android.os.Build.VERSION_CODES#P} and running on + * {@link android.os.Build.VERSION_CODES#P}. */ public @Nullable UserHandle createAndManageUser(@NonNull ComponentName admin, @NonNull String name, @@ -6594,6 +6603,8 @@ public class DevicePolicyManager { throwIfParentInstance("createAndManageUser"); try { return mService.createAndManageUser(admin, name, profileOwner, adminExtras, flags); + } catch (ServiceSpecificException e) { + throw new UserOperationException(e.getMessage(), e.errorCode); } catch (RemoteException re) { throw re.rethrowFromSystemServer(); } @@ -6637,77 +6648,15 @@ public class DevicePolicyManager { } /** - * Indicates user operation is successful. - * - * @see #startUserInBackground(ComponentName, UserHandle) - * @see #stopUser(ComponentName, UserHandle) - * @see #logoutUser(ComponentName) - */ - public static final int USER_OPERATION_SUCCESS = 0; - - /** - * Indicates user operation failed for unknown reason. - * - * @see #startUserInBackground(ComponentName, UserHandle) - * @see #stopUser(ComponentName, UserHandle) - * @see #logoutUser(ComponentName) - */ - public static final int USER_OPERATION_ERROR_UNKNOWN = 1; - - /** - * Indicates user operation failed because target user is a managed profile. - * - * @see #startUserInBackground(ComponentName, UserHandle) - * @see #stopUser(ComponentName, UserHandle) - * @see #logoutUser(ComponentName) - */ - public static final int USER_OPERATION_ERROR_MANAGED_PROFILE = 2; - - /** - * Indicates user operation failed because maximum running user limit has reached. - * - * @see #startUserInBackground(ComponentName, UserHandle) - */ - public static final int USER_OPERATION_ERROR_MAX_RUNNING_USERS = 3; - - /** - * Indicates user operation failed because the target user is in foreground. - * - * @see #stopUser(ComponentName, UserHandle) - * @see #logoutUser(ComponentName) - */ - public static final int USER_OPERATION_ERROR_CURRENT_USER = 4; - - /** - * Result returned from - * <ul> - * <li>{@link #startUserInBackground(ComponentName, UserHandle)}</li> - * <li>{@link #stopUser(ComponentName, UserHandle)}</li> - * <li>{@link #logoutUser(ComponentName)}</li> - * </ul> - * - * @hide - */ - @Retention(RetentionPolicy.SOURCE) - @IntDef(prefix = { "USER_OPERATION_" }, value = { - USER_OPERATION_SUCCESS, - USER_OPERATION_ERROR_UNKNOWN, - USER_OPERATION_ERROR_MANAGED_PROFILE, - USER_OPERATION_ERROR_MAX_RUNNING_USERS, - USER_OPERATION_ERROR_CURRENT_USER - }) - public @interface UserOperationResult {} - - /** * Called by a device owner to start the specified secondary user in background. * * @param admin Which {@link DeviceAdminReceiver} this request is associated with. * @param userHandle the user to be started in background. * @return one of the following result codes: - * {@link #USER_OPERATION_ERROR_UNKNOWN}, - * {@link #USER_OPERATION_SUCCESS}, - * {@link #USER_OPERATION_ERROR_MANAGED_PROFILE}, - * {@link #USER_OPERATION_ERROR_MAX_RUNNING_USERS}, + * {@link UserManager#USER_OPERATION_ERROR_UNKNOWN}, + * {@link UserManager#USER_OPERATION_SUCCESS}, + * {@link UserManager#USER_OPERATION_ERROR_MANAGED_PROFILE}, + * {@link UserManager#USER_OPERATION_ERROR_MAX_RUNNING_USERS}, * @throws SecurityException if {@code admin} is not a device owner. * @see #getSecondaryUsers(ComponentName) */ @@ -6727,10 +6676,10 @@ public class DevicePolicyManager { * @param admin Which {@link DeviceAdminReceiver} this request is associated with. * @param userHandle the user to be stopped. * @return one of the following result codes: - * {@link #USER_OPERATION_ERROR_UNKNOWN}, - * {@link #USER_OPERATION_SUCCESS}, - * {@link #USER_OPERATION_ERROR_MANAGED_PROFILE}, - * {@link #USER_OPERATION_ERROR_CURRENT_USER} + * {@link UserManager#USER_OPERATION_ERROR_UNKNOWN}, + * {@link UserManager#USER_OPERATION_SUCCESS}, + * {@link UserManager#USER_OPERATION_ERROR_MANAGED_PROFILE}, + * {@link UserManager#USER_OPERATION_ERROR_CURRENT_USER} * @throws SecurityException if {@code admin} is not a device owner. * @see #getSecondaryUsers(ComponentName) */ @@ -6750,10 +6699,10 @@ public class DevicePolicyManager { * * @param admin Which {@link DeviceAdminReceiver} this request is associated with. * @return one of the following result codes: - * {@link #USER_OPERATION_ERROR_UNKNOWN}, - * {@link #USER_OPERATION_SUCCESS}, - * {@link #USER_OPERATION_ERROR_MANAGED_PROFILE}, - * {@link #USER_OPERATION_ERROR_CURRENT_USER} + * {@link UserManager#USER_OPERATION_ERROR_UNKNOWN}, + * {@link UserManager#USER_OPERATION_SUCCESS}, + * {@link UserManager#USER_OPERATION_ERROR_MANAGED_PROFILE}, + * {@link UserManager#USER_OPERATION_ERROR_CURRENT_USER} * @throws SecurityException if {@code admin} is not a profile owner affiliated with the device. * @see #getSecondaryUsers(ComponentName) */ diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java index 41cc9f8bd411..2693bab5203e 100644 --- a/core/java/android/os/UserManager.java +++ b/core/java/android/os/UserManager.java @@ -1042,6 +1042,85 @@ public class UserManager { */ public static final int USER_CREATION_FAILED_NO_MORE_USERS = Activity.RESULT_FIRST_USER + 1; + /** + * Indicates user operation is successful. + */ + public static final int USER_OPERATION_SUCCESS = 0; + + /** + * Indicates user operation failed for unknown reason. + */ + public static final int USER_OPERATION_ERROR_UNKNOWN = 1; + + /** + * Indicates user operation failed because target user is a managed profile. + */ + public static final int USER_OPERATION_ERROR_MANAGED_PROFILE = 2; + + /** + * Indicates user operation failed because maximum running user limit has been reached. + */ + public static final int USER_OPERATION_ERROR_MAX_RUNNING_USERS = 3; + + /** + * Indicates user operation failed because the target user is in the foreground. + */ + public static final int USER_OPERATION_ERROR_CURRENT_USER = 4; + + /** + * Indicates user operation failed because device has low data storage. + */ + public static final int USER_OPERATION_ERROR_LOW_STORAGE = 5; + + /** + * Indicates user operation failed because maximum user limit has been reached. + */ + public static final int USER_OPERATION_ERROR_MAX_USERS = 6; + + /** + * Result returned from various user operations. + * + * @hide + */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(prefix = { "USER_OPERATION_" }, value = { + USER_OPERATION_SUCCESS, + USER_OPERATION_ERROR_UNKNOWN, + USER_OPERATION_ERROR_MANAGED_PROFILE, + USER_OPERATION_ERROR_MAX_RUNNING_USERS, + USER_OPERATION_ERROR_CURRENT_USER, + USER_OPERATION_ERROR_LOW_STORAGE, + USER_OPERATION_ERROR_MAX_USERS + }) + public @interface UserOperationResult {} + + /** + * Thrown to indicate user operation failed. + */ + public static class UserOperationException extends RuntimeException { + private final @UserOperationResult int mUserOperationResult; + + /** + * Constructs a UserOperationException with specific result code. + * + * @param message the detail message + * @param userOperationResult the result code + * @hide + */ + public UserOperationException(String message, + @UserOperationResult int userOperationResult) { + super(message); + mUserOperationResult = userOperationResult; + } + + /** + * Returns the operation result code. + */ + public @UserOperationResult int getUserOperationResult() { + return mUserOperationResult; + } + } + /** @hide */ public static UserManager get(Context context) { return (UserManager) context.getSystemService(Context.USER_SERVICE); |
