Minor adjustements on UserManager.isUserForeground().

- Uses context user id instead of calling user id.
- Annotated with @UserHandleAware.
- Updated javadoc.

Test: m update-apis
Test: atest CtsMultiUserTestCases
Bug: 173541467

Change-Id: Id61fb73f2b7313265554cff9f496eb118a7e6a1e
This commit is contained in:
Felipe Leme
2021-02-17 18:47:53 -08:00
parent 745e7ee830
commit 0c2d8b897e
3 changed files with 15 additions and 7 deletions

View File

@@ -113,7 +113,7 @@ interface IUserManager {
boolean hasBadge(int userId);
boolean isUserUnlocked(int userId);
boolean isUserRunning(int userId);
boolean isUserForeground();
boolean isUserForeground(int userId);
boolean isUserNameSet(int userId);
boolean hasRestrictedProfiles();
boolean requestQuietModeEnabled(String callingPackage, boolean enableQuietMode, int userId, in IntentSender target, int flags);

View File

@@ -2300,13 +2300,14 @@ public class UserManager {
}
/**
* Checks if the calling user is running on foreground.
* Checks if the context user is running in the foreground.
*
* @return whether the calling user is running on foreground.
* @return whether the context user is running in the foreground.
*/
@UserHandleAware
public boolean isUserForeground() {
try {
return mService.isUserForeground();
return mService.isUserForeground(mUserId);
} catch (RemoteException re) {
throw re.rethrowFromSystemServer();
}

View File

@@ -1522,11 +1522,18 @@ public class UserManagerService extends IUserManager.Stub {
}
@Override
public boolean isUserForeground() {
int callingUserId = Binder.getCallingUserHandle().getIdentifier();
public boolean isUserForeground(@UserIdInt int userId) {
final int callingUserId = UserHandle.getCallingUserId();
if (callingUserId != userId
&& !hasManageUsersOrPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)) {
throw new SecurityException("Caller from user " + callingUserId + " needs MANAGE_USERS "
+ "or INTERACT_ACROSS_USERS permission to check if another user (" + userId
+ ") is running in the foreground");
}
int currentUser = Binder.withCleanCallingIdentity(() -> ActivityManager.getCurrentUser());
// TODO(b/179163496): should return true for profile users of the current user as well
return currentUser == callingUserId;
return currentUser == userId;
}
@Override