Switch isUserUnlocked/isUserRunning to use UMS.mUserStates
UserManager.isUserUnlocked/isUserRunning now returns state from
UMS.mUserStates that is pushed from ActivityManager.
Test: manual
Bug: 33232933
Bug: 28957340
Change-Id: Ic5f56d7d577bc336acd2bf44fab7f74feac4f7b3
diff --git a/core/java/android/os/IUserManager.aidl b/core/java/android/os/IUserManager.aidl
index 9dafe29..d443b66 100644
--- a/core/java/android/os/IUserManager.aidl
+++ b/core/java/android/os/IUserManager.aidl
@@ -87,4 +87,6 @@
in String[] disallowedPackages);
boolean isUserUnlockingOrUnlocked(int userId);
int getManagedProfileBadge(int userId);
+ boolean isUserUnlocked(int userId);
+ boolean isUserRunning(int userId);
}
diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java
index a79b0c4..4ab5d65 100644
--- a/core/java/android/os/UserManager.java
+++ b/core/java/android/os/UserManager.java
@@ -996,10 +996,9 @@
}
/** {@hide} */
- public boolean isUserRunning(int userId) {
- // TODO Switch to using UMS internal isUserRunning
+ public boolean isUserRunning(@UserIdInt int userId) {
try {
- return ActivityManager.getService().isUserRunning(userId, 0);
+ return mService.isUserRunning(userId);
} catch (RemoteException re) {
throw re.rethrowFromSystemServer();
}
@@ -1096,8 +1095,7 @@
/** {@hide} */
public boolean isUserUnlocked(@UserIdInt int userId) {
try {
- return ActivityManager.getService().isUserRunning(userId,
- ActivityManager.FLAG_AND_UNLOCKED);
+ return mService.isUserUnlocked(userId);
} catch (RemoteException re) {
throw re.rethrowFromSystemServer();
}
diff --git a/core/java/android/os/UserManagerInternal.java b/core/java/android/os/UserManagerInternal.java
index 4bdb92b..1447e7d 100644
--- a/core/java/android/os/UserManagerInternal.java
+++ b/core/java/android/os/UserManagerInternal.java
@@ -142,6 +142,12 @@
public abstract boolean isUserUnlockingOrUnlocked(int userId);
/**
+ * Return whether the given user is running in an
+ * {@code UserState.STATE_RUNNING_UNLOCKED} state.
+ */
+ public abstract boolean isUserUnlocked(int userId);
+
+ /**
* Return whether the given user is running
*/
public abstract boolean isUserRunning(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 20afed7..af16c5b 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -913,17 +913,36 @@
@Override
public boolean isUserUnlockingOrUnlocked(int userId) {
- int callingUserId = UserHandle.getCallingUserId();
- if (callingUserId != userId && !hasManageUsersPermission()) {
- if (!isSameProfileGroupNoChecks(callingUserId, userId)) {
- throw new SecurityException(
- "You need MANAGE_USERS permission to: check isUserUnlockingOrUnlocked");
- }
- }
+ checkManageOrInteractPermIfCallerInOtherProfileGroup(userId, "isUserUnlockingOrUnlocked");
return mLocalService.isUserUnlockingOrUnlocked(userId);
}
@Override
+ public boolean isUserUnlocked(int userId) {
+ checkManageOrInteractPermIfCallerInOtherProfileGroup(userId, "isUserUnlocked");
+ return mLocalService.isUserUnlockingOrUnlocked(userId);
+ }
+
+ @Override
+ public boolean isUserRunning(int userId) {
+ checkManageOrInteractPermIfCallerInOtherProfileGroup(userId, "isUserRunning");
+ return mLocalService.isUserRunning(userId);
+ }
+
+ private void checkManageOrInteractPermIfCallerInOtherProfileGroup(int userId, String name) {
+ int callingUserId = UserHandle.getCallingUserId();
+ if (callingUserId == userId || isSameProfileGroupNoChecks(callingUserId, userId) ||
+ hasManageUsersPermission()) {
+ return;
+ }
+ if (ActivityManager.checkComponentPermission(Manifest.permission.INTERACT_ACROSS_USERS,
+ Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) {
+ throw new SecurityException("You need INTERACT_ACROSS_USERS or MANAGE_USERS permission "
+ + "to: check " + name);
+ }
+ }
+
+ @Override
public boolean isDemoUser(int userId) {
int callingUserId = UserHandle.getCallingUserId();
if (callingUserId != userId && !hasManageUsersPermission()) {
@@ -3641,6 +3660,14 @@
|| (state == UserState.STATE_RUNNING_UNLOCKED);
}
}
+
+ @Override
+ public boolean isUserUnlocked(int userId) {
+ synchronized (mUserStates) {
+ int state = mUserStates.get(userId, -1);
+ return state == UserState.STATE_RUNNING_UNLOCKED;
+ }
+ }
}
/* Remove all the users except of the system one. */