summaryrefslogtreecommitdiff
path: root/service/java
diff options
context:
space:
mode:
author Richard MacGregor <rmacgregor@google.com> 2024-11-04 23:17:36 +0000
committer Richard MacGregor <rmacgregor@google.com> 2024-11-04 23:17:36 +0000
commit85661e97416695913c77f0ea1b2657527ae764b4 (patch)
treeb7db8c4746806e6ceac6e54e4983c5527ad40a0a /service/java
parentc2ea6b1bd9fa99ceba8a629a3b8dd5c1cc716009 (diff)
Move shared User util methods to UserUtils
LOW_COVERAGE_REASON=b/377295697 RelNote: N/A Bug: 376728836 Flag: EXEMPT refactor Test: build Change-Id: Ia5e2949e21193a833b2861b273b2d13eaac55dbf
Diffstat (limited to 'service/java')
-rw-r--r--service/java/com/android/permission/util/UserUtils.java40
-rw-r--r--service/java/com/android/safetycenter/UserProfileGroup.java42
2 files changed, 42 insertions, 40 deletions
diff --git a/service/java/com/android/permission/util/UserUtils.java b/service/java/com/android/permission/util/UserUtils.java
index 33389a88f..639c7aacb 100644
--- a/service/java/com/android/permission/util/UserUtils.java
+++ b/service/java/com/android/permission/util/UserUtils.java
@@ -19,6 +19,7 @@ package com.android.permission.util;
import android.annotation.NonNull;
import android.annotation.UserIdInt;
import android.content.Context;
+import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Process;
import android.os.UserHandle;
@@ -30,6 +31,7 @@ import com.android.permission.compat.UserHandleCompat;
import com.android.permission.flags.Flags;
import java.util.List;
+import java.util.Objects;
/** Utility class to deal with Android users. */
public final class UserUtils {
@@ -81,6 +83,32 @@ public final class UserUtils {
}
}
+ /** Returns all the enabled user profiles on the device. */
+ @NonNull
+ public static List<UserHandle> getUserProfiles(@NonNull Context context) {
+ UserManager userManager = context.getSystemService(UserManager.class);
+ // This call requires the QUERY_USERS permission.
+ final long identity = Binder.clearCallingIdentity();
+ try {
+ return userManager.getUserProfiles();
+ } finally {
+ Binder.restoreCallingIdentity(identity);
+ }
+ }
+
+ /** Returns the parent of a given user. */
+ public static UserHandle getProfileParent(@UserIdInt int userId, @NonNull Context context) {
+ Context userContext = getUserContext(userId, context);
+ UserManager userManager = userContext.getSystemService(UserManager.class);
+ // This call requires the INTERACT_ACROSS_USERS permission.
+ final long identity = Binder.clearCallingIdentity();
+ try {
+ return userManager.getProfileParent(UserHandle.of(userId));
+ } finally {
+ Binder.restoreCallingIdentity(identity);
+ }
+ }
+
/** Returns whether a given {@code userId} corresponds to a managed profile. */
public static boolean isManagedProfile(@UserIdInt int userId, @NonNull Context context) {
UserManager userManager = context.getSystemService(UserManager.class);
@@ -107,8 +135,7 @@ public final class UserUtils {
// MANAGE_USERS, QUERY_USERS, or INTERACT_ACROSS_USERS.
final long identity = Binder.clearCallingIdentity();
try {
- Context userContext = context
- .createContextAsUser(UserHandle.of(userId), /* flags= */ 0);
+ Context userContext = getUserContext(userId, context);
UserManager userManager = userContext.getSystemService(UserManager.class);
return userManager != null && userManager.isPrivateProfile();
} finally {
@@ -141,4 +168,13 @@ public final class UserUtils {
Binder.restoreCallingIdentity(identity);
}
}
+
+ @NonNull
+ public static Context getUserContext(@UserIdInt int userId, @NonNull Context context) {
+ if (SdkLevel.isAtLeastS() && context.getUser().getIdentifier() == userId) {
+ return context;
+ } else {
+ return context.createContextAsUser(UserHandle.of(userId), 0);
+ }
+ }
}
diff --git a/service/java/com/android/safetycenter/UserProfileGroup.java b/service/java/com/android/safetycenter/UserProfileGroup.java
index 46a440bf7..3202c3776 100644
--- a/service/java/com/android/safetycenter/UserProfileGroup.java
+++ b/service/java/com/android/safetycenter/UserProfileGroup.java
@@ -134,9 +134,9 @@ public final class UserProfileGroup {
* is disabled.
*/
public static UserProfileGroup fromUser(Context context, @UserIdInt int userId) {
- UserManager userManager = getUserManagerForUser(userId, context);
- List<UserHandle> userProfiles = getEnabledUserProfiles(userManager);
- UserHandle profileParent = getProfileParent(userManager, userId);
+ Context userContext = UserUtils.getUserContext(userId, context);
+ List<UserHandle> userProfiles = UserUtils.getUserProfiles(userContext);
+ UserHandle profileParent = UserUtils.getProfileParent(userId, userContext);
int profileParentUserId = userId;
if (profileParent != null) {
profileParentUserId = profileParent.getIdentifier();
@@ -192,23 +192,10 @@ public final class UserProfileGroup {
}
private static UserManager getUserManagerForUser(@UserIdInt int userId, Context context) {
- Context userContext = getUserContext(context, UserHandle.of(userId));
+ Context userContext = UserUtils.getUserContext(userId, context);
return requireNonNull(userContext.getSystemService(UserManager.class));
}
- private static Context getUserContext(Context context, UserHandle userHandle) {
- if (Process.myUserHandle().equals(userHandle)) {
- return context;
- } else {
- try {
- return context.createPackageContextAsUser(
- context.getPackageName(), /* flags= */ 0, userHandle);
- } catch (PackageManager.NameNotFoundException doesNotHappen) {
- throw new IllegalStateException(doesNotHappen);
- }
- }
- }
-
private static boolean isProfile(@UserIdInt int userId, Context context) {
// This call requires the INTERACT_ACROSS_USERS permission.
final long callingId = Binder.clearCallingIdentity();
@@ -220,27 +207,6 @@ public final class UserProfileGroup {
}
}
- private static List<UserHandle> getEnabledUserProfiles(UserManager userManager) {
- // This call requires the QUERY_USERS permission.
- final long callingId = Binder.clearCallingIdentity();
- try {
- return userManager.getUserProfiles();
- } finally {
- Binder.restoreCallingIdentity(callingId);
- }
- }
-
- @Nullable
- private static UserHandle getProfileParent(UserManager userManager, @UserIdInt int userId) {
- // This call requires the INTERACT_ACROSS_USERS permission.
- final long callingId = Binder.clearCallingIdentity();
- try {
- return userManager.getProfileParent(UserHandle.of(userId));
- } finally {
- Binder.restoreCallingIdentity(callingId);
- }
- }
-
/** Returns the profile parent user id of the {@link UserProfileGroup}. */
public int getProfileParentUserId() {
return mProfileParentUserId;