Added UserManagerInternal.getDisplayAssignedToUser(userId).
Currently, to check if a user is visible, internal services
need to get a context to the user, which is not only more work
(and more expensive), but would not work if the user doesn't exist.
Test: called by Car stack
Bug: 243864134
Change-Id: I25d0915c94ac670cfd126b94ec988d2c4107bc1c
diff --git a/services/core/java/com/android/server/pm/UserManagerInternal.java b/services/core/java/com/android/server/pm/UserManagerInternal.java
index 297439f..8f89fdd 100644
--- a/services/core/java/com/android/server/pm/UserManagerInternal.java
+++ b/services/core/java/com/android/server/pm/UserManagerInternal.java
@@ -321,4 +321,15 @@
* {@link UserManager#isUserVisible()} in the given display.
*/
public abstract boolean isUserVisible(@UserIdInt int userId, int displayId);
+
+ /**
+ * Returns the display id assigned to the user, or {@code Display.INVALID_DISPLAY} if the
+ * user is not assigned to any display.
+ *
+ * <p>The current foreground user is associated with the main display, while other users would
+ * only assigned to a display if they were started with
+ * {@code ActivityManager.startUserInBackgroundOnSecondaryDisplay()}. If the user is a profile
+ * and is running, it's assigned to its parent display.
+ */
+ public abstract int getDisplayAssignedToUser(@UserIdInt int userId);
}
diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java
index d8b7fda..3b898f8 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -1731,6 +1731,18 @@
return false;
}
+ // TODO(b/239982558): add unit test
+ private int getDisplayAssignedToUser(@UserIdInt int userId) {
+ if (isCurrentUserOrRunningProfileOfCurrentUser(userId)) {
+ return Display.DEFAULT_DISPLAY;
+ }
+ synchronized (mUsersLock) {
+ return mUsersOnSecondaryDisplays == null
+ ? Display.INVALID_DISPLAY
+ : mUsersOnSecondaryDisplays.get(userId, Display.INVALID_DISPLAY);
+ }
+ }
+
private boolean isCurrentUserOrRunningProfileOfCurrentUser(@UserIdInt int userId) {
int currentUserId = Binder.withCleanCallingIdentity(() -> ActivityManager.getCurrentUser());
@@ -6695,6 +6707,11 @@
public boolean isUserVisible(int userId, int displayId) {
return isUserVisibleOnDisplay(userId, displayId);
}
+
+ @Override
+ public int getDisplayAssignedToUser(int userId) {
+ return UserManagerService.this.getDisplayAssignedToUser(userId);
+ }
} // class LocalService
/**