Fix issue #7311376: Add API to allow apps to know if they are...

...running as the foreground user

Add UserManager.isUserRunning() which is the public version of the
existing method on ActivityManager.

Also add UserManager.isUserRunningOrStopping() since that seems like
it will be useful.

And fix the internal function that returns the array of currently
running users to not include stopped users.

Change-Id: I84672fa8748fc027fd402729586b5603f640e498
This commit is contained in:
Dianne Hackborn
2012-10-09 15:36:59 -07:00
parent 3b9e7f3238
commit a8a9bd65bf
7 changed files with 83 additions and 13 deletions

View File

@@ -3585,7 +3585,7 @@ public final class ActivityManagerService extends ActivityManagerNative
Slog.w(TAG, "Failed trying to unstop package "
+ packageName + ": " + e);
}
if (isUserRunningLocked(user)) {
if (isUserRunningLocked(user, false)) {
forceStopPackageLocked(packageName, pkgUid);
}
}
@@ -9338,6 +9338,12 @@ public final class ActivityManagerService extends ActivityManagerNative
pw.print(" User #"); pw.print(uss.mHandle.getIdentifier());
pw.print(": "); uss.dump("", pw);
}
pw.print(" mStartedUserArray: [");
for (int i=0; i<mStartedUserArray.length; i++) {
if (i > 0) pw.print(", ");
pw.print(mStartedUserArray[i]);
}
pw.println("]");
pw.print(" mUserLru: [");
for (int i=0; i<mUserLru.size(); i++) {
if (i > 0) pw.print(", ");
@@ -14134,10 +14140,12 @@ public final class ActivityManagerService extends ActivityManagerNative
// so we can just fairly silently bring the user back from
// the almost-dead.
uss.mState = UserStartedState.STATE_RUNNING;
updateStartedUserArrayLocked();
} else if (uss.mState == UserStartedState.STATE_SHUTDOWN) {
// This means ACTION_SHUTDOWN has been sent, so we will
// need to treat this as a new boot of the user.
uss.mState = UserStartedState.STATE_BOOTING;
updateStartedUserArrayLocked();
}
mHandler.removeMessages(REPORT_USER_SWITCH_MSG);
@@ -14318,8 +14326,7 @@ public final class ActivityManagerService extends ActivityManagerNative
void finishUserSwitch(UserStartedState uss) {
synchronized (this) {
if ((uss.mState == UserStartedState.STATE_BOOTING
|| uss.mState == UserStartedState.STATE_SHUTDOWN)
if (uss.mState == UserStartedState.STATE_BOOTING
&& mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) {
uss.mState = UserStartedState.STATE_RUNNING;
final int userId = uss.mHandle.getIdentifier();
@@ -14410,6 +14417,7 @@ public final class ActivityManagerService extends ActivityManagerNative
if (uss.mState != UserStartedState.STATE_STOPPING
&& uss.mState != UserStartedState.STATE_SHUTDOWN) {
uss.mState = UserStartedState.STATE_STOPPING;
updateStartedUserArrayLocked();
long ident = Binder.clearCallingIdentity();
try {
@@ -14514,7 +14522,7 @@ public final class ActivityManagerService extends ActivityManagerNative
}
@Override
public boolean isUserRunning(int userId) {
public boolean isUserRunning(int userId, boolean orStopped) {
if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS)
!= PackageManager.PERMISSION_GRANTED) {
String msg = "Permission Denial: isUserRunning() from pid="
@@ -14525,13 +14533,19 @@ public final class ActivityManagerService extends ActivityManagerNative
throw new SecurityException(msg);
}
synchronized (this) {
return isUserRunningLocked(userId);
return isUserRunningLocked(userId, orStopped);
}
}
boolean isUserRunningLocked(int userId) {
boolean isUserRunningLocked(int userId, boolean orStopped) {
UserStartedState state = mStartedUsers.get(userId);
return state != null && state.mState != UserStartedState.STATE_STOPPING
if (state == null) {
return false;
}
if (orStopped) {
return true;
}
return state.mState != UserStartedState.STATE_STOPPING
&& state.mState != UserStartedState.STATE_SHUTDOWN;
}
@@ -14552,9 +14566,24 @@ public final class ActivityManagerService extends ActivityManagerNative
}
private void updateStartedUserArrayLocked() {
mStartedUserArray = new int[mStartedUsers.size()];
int num = 0;
for (int i=0; i<mStartedUsers.size(); i++) {
mStartedUserArray[i] = mStartedUsers.keyAt(i);
UserStartedState uss = mStartedUsers.valueAt(i);
// This list does not include stopping users.
if (uss.mState != UserStartedState.STATE_STOPPING
&& uss.mState != UserStartedState.STATE_SHUTDOWN) {
num++;
}
}
mStartedUserArray = new int[num];
num = 0;
for (int i=0; i<mStartedUsers.size(); i++) {
UserStartedState uss = mStartedUsers.valueAt(i);
if (uss.mState != UserStartedState.STATE_STOPPING
&& uss.mState != UserStartedState.STATE_SHUTDOWN) {
mStartedUserArray[num] = mStartedUsers.keyAt(i);
num++;
}
}
}