diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java index 4b63f0f713a01..b0e76e3ea851e 100644 --- a/core/java/android/os/UserManager.java +++ b/core/java/android/os/UserManager.java @@ -1466,6 +1466,48 @@ public class UserManager { }) public @interface UserSwitchabilityResult {} + /** + * A response code from {@link #removeUserOrSetEphemeral(int)} indicating that the specified + * user has been successfully removed. + * @hide + */ + public static final int REMOVE_RESULT_REMOVED = 0; + + /** + * A response code from {@link #removeUserOrSetEphemeral(int)} indicating that the specified + * user has had its {@link UserInfo#FLAG_EPHEMERAL} flag set to {@code true}, so that it will be + * removed when the user is stopped or on boot. + * @hide + */ + public static final int REMOVE_RESULT_SET_EPHEMERAL = 1; + + /** + * A response code from {@link #removeUserOrSetEphemeral(int)} indicating that the specified + * user is already in the process of being removed. + * @hide + */ + public static final int REMOVE_RESULT_ALREADY_BEING_REMOVED = 2; + + /** + * A response code from {@link #removeUserOrSetEphemeral(int)} indicating that an error occurred + * that prevented the user from being removed or set as ephemeral. + * @hide + */ + public static final int REMOVE_RESULT_ERROR = 3; + + /** + * Possible response codes from {@link #removeUserOrSetEphemeral(int)}. + * @hide + */ + @IntDef(prefix = { "REMOVE_RESULT_" }, value = { + REMOVE_RESULT_REMOVED, + REMOVE_RESULT_SET_EPHEMERAL, + REMOVE_RESULT_ALREADY_BEING_REMOVED, + REMOVE_RESULT_ERROR, + }) + @Retention(RetentionPolicy.SOURCE) + public @interface RemoveResult {} + /** * Indicates user operation is successful. */ @@ -3981,11 +4023,11 @@ public class UserManager { * the current user, then set the user as ephemeral so that it will be removed when it is * stopped. * - * @return the {@link com.android.server.pm.UserManagerService.RemoveResult} code + * @return the {@link RemoveResult} code * @hide */ @RequiresPermission(android.Manifest.permission.MANAGE_USERS) - public int removeUserOrSetEphemeral(@UserIdInt int userId) { + public @RemoveResult int removeUserOrSetEphemeral(@UserIdInt int userId) { try { return mService.removeUserOrSetEphemeral(userId); } catch (RemoteException re) { diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java index c46a7efaa704f..9aa1a621a7600 100644 --- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java +++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java @@ -2729,13 +2729,13 @@ class PackageManagerShellCommand extends ShellCommand { Slog.i(TAG, "Removing " + userId + " or set as ephemeral if in use."); int result = um.removeUserOrSetEphemeral(userId); switch (result) { - case UserManagerService.REMOVE_RESULT_REMOVED: + case UserManager.REMOVE_RESULT_REMOVED: getOutPrintWriter().printf("Success: user %d removed\n", userId); return 0; - case UserManagerService.REMOVE_RESULT_SET_EPHEMERAL: + case UserManager.REMOVE_RESULT_SET_EPHEMERAL: getOutPrintWriter().printf("Success: user %d set as ephemeral\n", userId); return 0; - case UserManagerService.REMOVE_RESULT_ALREADY_BEING_REMOVED: + case UserManager.REMOVE_RESULT_ALREADY_BEING_REMOVED: getOutPrintWriter().printf("Success: user %d is already being removed\n", userId); return 0; default: diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java index 66e84b1805881..a0344e27f96c9 100644 --- a/services/core/java/com/android/server/pm/UserManagerService.java +++ b/services/core/java/com/android/server/pm/UserManagerService.java @@ -22,7 +22,6 @@ import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; import android.Manifest; import android.annotation.ColorRes; import android.annotation.DrawableRes; -import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.StringRes; @@ -132,8 +131,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; @@ -248,43 +245,6 @@ public class UserManagerService extends IUserManager.Stub { static final int WRITE_USER_MSG = 1; static final int WRITE_USER_DELAY = 2*1000; // 2 seconds - /** - * A response code from {@link #removeUserOrSetEphemeral(int)} indicating that the specified - * user has been successfully removed. - */ - public static final int REMOVE_RESULT_REMOVED = 0; - - /** - * A response code from {@link #removeUserOrSetEphemeral(int)} indicating that the specified - * user has had its {@link UserInfo#FLAG_EPHEMERAL} flag set to {@code true}, so that it will be - * removed when the user is stopped or on boot. - */ - public static final int REMOVE_RESULT_SET_EPHEMERAL = 1; - - /** - * A response code from {@link #removeUserOrSetEphemeral(int)} indicating that the specified - * user is already in the process of being removed. - */ - public static final int REMOVE_RESULT_ALREADY_BEING_REMOVED = 2; - - /** - * A response code from {@link #removeUserOrSetEphemeral(int)} indicating that an error occurred - * that prevented the user from being removed or set as ephemeral. - */ - public static final int REMOVE_RESULT_ERROR = 3; - - /** - * Possible response codes from {@link #removeUserOrSetEphemeral(int)}. - */ - @IntDef(prefix = { "REMOVE_RESULT_" }, value = { - REMOVE_RESULT_REMOVED, - REMOVE_RESULT_SET_EPHEMERAL, - REMOVE_RESULT_ALREADY_BEING_REMOVED, - REMOVE_RESULT_ERROR, - }) - @Retention(RetentionPolicy.SOURCE) - public @interface RemoveResult {} - // Tron counters private static final String TRON_GUEST_CREATED = "users_guest_created"; private static final String TRON_USER_CREATED = "users_user_created"; @@ -4031,17 +3991,17 @@ public class UserManagerService extends IUserManager.Stub { } @Override - public @RemoveResult int removeUserOrSetEphemeral(@UserIdInt int userId) { + public @UserManager.RemoveResult int removeUserOrSetEphemeral(@UserIdInt int userId) { Slog.i(LOG_TAG, "removeUserOrSetEphemeral u" + userId); checkManageUsersPermission("Only the system can remove users"); final String restriction = getUserRemovalRestriction(userId); if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(restriction, false)) { Slog.w(LOG_TAG, "Cannot remove user. " + restriction + " is enabled."); - return REMOVE_RESULT_ERROR; + return UserManager.REMOVE_RESULT_ERROR; } if (userId == UserHandle.USER_SYSTEM) { Slog.e(LOG_TAG, "System user cannot be removed."); - return REMOVE_RESULT_ERROR; + return UserManager.REMOVE_RESULT_ERROR; } final long ident = Binder.clearCallingIdentity(); @@ -4053,12 +4013,12 @@ public class UserManagerService extends IUserManager.Stub { if (userData == null) { Slog.e(LOG_TAG, "Cannot remove user " + userId + ", invalid user id provided."); - return REMOVE_RESULT_ERROR; + return UserManager.REMOVE_RESULT_ERROR; } if (mRemovingUserIds.get(userId)) { Slog.e(LOG_TAG, "User " + userId + " is already scheduled for removal."); - return REMOVE_RESULT_ALREADY_BEING_REMOVED; + return UserManager.REMOVE_RESULT_ALREADY_BEING_REMOVED; } } @@ -4067,7 +4027,7 @@ public class UserManagerService extends IUserManager.Stub { if (currentUser != userId) { // Attempt to remove the user. This will fail if the user is the current user if (removeUser(userId)) { - return REMOVE_RESULT_REMOVED; + return UserManager.REMOVE_RESULT_REMOVED; } Slog.w(LOG_TAG, "Unable to immediately remove non-current user: " + userId @@ -4081,7 +4041,7 @@ public class UserManagerService extends IUserManager.Stub { userData.info.flags |= UserInfo.FLAG_EPHEMERAL; writeUserLP(userData); - return REMOVE_RESULT_SET_EPHEMERAL; + return UserManager.REMOVE_RESULT_SET_EPHEMERAL; } } finally { Binder.restoreCallingIdentity(ident); diff --git a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java index 75799562abfa2..b190339b129b9 100644 --- a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java @@ -220,7 +220,7 @@ public final class UserManagerTest { asHandle(currentUser)); try { assertThat(mUserManager.removeUserOrSetEphemeral(user1.id)).isEqualTo( - UserManagerService.REMOVE_RESULT_ERROR); + UserManager.REMOVE_RESULT_ERROR); } finally { mUserManager.setUserRestriction(UserManager.DISALLOW_REMOVE_USER, /* value= */ false, asHandle(currentUser)); @@ -234,7 +234,7 @@ public final class UserManagerTest { @Test public void testRemoveUserOrSetEphemeral_systemUserReturnsError() throws Exception { assertThat(mUserManager.removeUserOrSetEphemeral(UserHandle.USER_SYSTEM)).isEqualTo( - UserManagerService.REMOVE_RESULT_ERROR); + UserManager.REMOVE_RESULT_ERROR); assertThat(hasUser(UserHandle.USER_SYSTEM)).isTrue(); } @@ -244,7 +244,7 @@ public final class UserManagerTest { public void testRemoveUserOrSetEphemeral_invalidUserReturnsError() throws Exception { assertThat(hasUser(Integer.MAX_VALUE)).isFalse(); assertThat(mUserManager.removeUserOrSetEphemeral(Integer.MAX_VALUE)).isEqualTo( - UserManagerService.REMOVE_RESULT_ERROR); + UserManager.REMOVE_RESULT_ERROR); } @MediumTest @@ -256,7 +256,7 @@ public final class UserManagerTest { switchUser(user1.id, null, /* ignoreHandle= */ true); assertThat(mUserManager.removeUserOrSetEphemeral(user1.id)).isEqualTo( - UserManagerService.REMOVE_RESULT_SET_EPHEMERAL); + UserManager.REMOVE_RESULT_SET_EPHEMERAL); assertThat(hasUser(user1.id)).isTrue(); assertThat(getUser(user1.id).isEphemeral()).isTrue(); @@ -277,7 +277,7 @@ public final class UserManagerTest { final UserInfo user1 = createUser("User 1", /* flags= */ 0); synchronized (mUserRemoveLock) { assertThat(mUserManager.removeUserOrSetEphemeral(user1.id)).isEqualTo( - UserManagerService.REMOVE_RESULT_REMOVED); + UserManager.REMOVE_RESULT_REMOVED); waitForUserRemovalLocked(user1.id); }