From 953a49b58133e558154588da84363b1122c381e0 Mon Sep 17 00:00:00 2001 From: Adam Bookatz Date: Fri, 1 Oct 2021 09:04:42 -0700 Subject: [PATCH] Introduce UM.getProcessUserId UM is being made @UserHandleAware, so using an explicit method avoids ambiguity. Ideally, we would just use UserHandle.myUserId(), with no need to go via UserManager. However, this method is static and is therefore very difficult to mock in all the various places that mock it, even with extended mockito. It's much much simpler to simply switch the name of the hidden API. Bug: 201679968 Test: (No-op) compiles and treehugger Change-Id: If93eda899b52d0acae995ec96f2491f5ea1a992d --- core/java/android/os/UserManager.java | 32 ++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java index da0937531c03c..a16d2ba5e457f 100644 --- a/core/java/android/os/UserManager.java +++ b/core/java/android/os/UserManager.java @@ -1848,7 +1848,7 @@ public class UserManager { Manifest.permission.MANAGE_USERS}, // Can be INTERACT_ACROSS_USERS instead. conditional = true) @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) - @UserHandleAware() + @UserHandleAware public boolean canSwitchUsers() { boolean allowUserSwitchingWhenSystemUserLocked = Settings.Global.getInt( mContext.getContentResolver(), @@ -1929,8 +1929,13 @@ public class UserManager { * Returns the userId for the context user. * * @return the userId of the context user. + * + * @deprecated To get the calling user, use {@link UserHandle#myUserId()}. + * To get the context user, get it directly from the context. + * * @hide */ + @Deprecated @UnsupportedAppUsage @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.TIRAMISU) // *** Do NOT use this in UserManager. Instead always use mUserId. *** @@ -1938,6 +1943,21 @@ public class UserManager { return getContextUserIfAppropriate(); } + /** + * Returns the userId for the user that this process is running under + * (not the context user). + * + * @return the userId of this process. + * + * @deprecated Use {@link UserHandle#myUserId()} + * @hide + */ + @Deprecated + // NOT @UserHandleAware + public @UserIdInt int getProcessUserId() { + return UserHandle.myUserId(); + } + /** * @return the user type of the context user. * @hide @@ -2012,7 +2032,7 @@ public class UserManager { return false; } // Caution: This is NOT @UserHandleAware (because mContext is getApplicationContext and - // holds a different userId), but for R+ it returns false, so it doesn't matter anyway. + // can hold a different userId), but for R+ it returns false, so it doesn't matter anyway. return mContext.getPackageManager() .isPackageAvailable("com.coffeestainstudios.goatsimulator"); } @@ -2447,7 +2467,7 @@ public class UserManager { */ @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, Manifest.permission.CREATE_USERS}) - @UserHandleAware() + @UserHandleAware public boolean isEphemeralUser() { return isUserEphemeral(mUserId); } @@ -3315,7 +3335,7 @@ public class UserManager { @TestApi @RequiresPermission(anyOf = {Manifest.permission.MANAGE_USERS, Manifest.permission.CREATE_USERS}) - @UserHandleAware() + @UserHandleAware public @Nullable UserInfo createRestrictedProfile(@Nullable String name) { try { final int parentUserId = mUserId; @@ -4605,7 +4625,7 @@ public class UserManager { android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.CREATE_USERS // And INTERACT_ if diff profile group }) - @UserHandleAware() + @UserHandleAware public boolean isUserSwitcherEnabled() { return isUserSwitcherEnabled(true); } @@ -4622,7 +4642,7 @@ public class UserManager { android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.CREATE_USERS // And INTERACT_ if diff profile group }) - @UserHandleAware() + @UserHandleAware public boolean isUserSwitcherEnabled(boolean showEvenIfNotActionable) { if (!supportsMultipleUsers()) { return false;