Merge "Fix issue #5321282: Force Stop Button in Battery Screen Not disabled correctly"
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index ed960d6..59ed80f 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -3334,6 +3334,11 @@
             if ((samePackage || r.task == lastTask)
                     && (r.app == null || evenPersistent || !r.app.persistent)) {
                 if (!doit) {
+                    if (r.finishing) {
+                        // If this activity is just finishing, then it is not
+                        // interesting as far as something to stop.
+                        continue;
+                    }
                     return true;
                 }
                 didSomething = true;
@@ -3399,6 +3404,7 @@
                 }
             }
             mMainStack.resumeTopActivityLocked(null);
+            mMainStack.scheduleIdleLocked();
         }
         
         return didSomething;
diff --git a/services/java/com/android/server/am/ActivityStack.java b/services/java/com/android/server/am/ActivityStack.java
index a0aedf9..35dee3c 100644
--- a/services/java/com/android/server/am/ActivityStack.java
+++ b/services/java/com/android/server/am/ActivityStack.java
@@ -765,9 +765,7 @@
                 // Still need to tell some activities to stop; can't sleep yet.
                 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still need to stop "
                         + mStoppingActivities.size() + " activities");
-                Message msg = Message.obtain();
-                msg.what = IDLE_NOW_MSG;
-                mHandler.sendMessage(msg);
+                scheduleIdleLocked();
                 return;
             }
 
@@ -978,9 +976,7 @@
                         // then give up on things going idle and start clearing
                         // them out.
                         if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
-                        Message msg = Message.obtain();
-                        msg.what = IDLE_NOW_MSG;
-                        mHandler.sendMessage(msg);
+                        scheduleIdleLocked();
                     } else {
                         checkReadyForSleepLocked();
                     }
@@ -3103,6 +3099,12 @@
         return stops;
     }
 
+    final void scheduleIdleLocked() {
+        Message msg = Message.obtain();
+        msg.what = IDLE_NOW_MSG;
+        mHandler.sendMessage(msg);
+    }
+
     final ActivityRecord activityIdleInternal(IBinder token, boolean fromTimeout,
             Configuration config) {
         if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
@@ -3413,9 +3415,7 @@
                     // If we already have a few activities waiting to stop,
                     // then give up on things going idle and start clearing
                     // them out.
-                    Message msg = Message.obtain();
-                    msg.what = IDLE_NOW_MSG;
-                    mHandler.sendMessage(msg);
+                    scheduleIdleLocked();
                 } else {
                     checkReadyForSleepLocked();
                 }
diff --git a/services/java/com/android/server/pm/PackageManagerService.java b/services/java/com/android/server/pm/PackageManagerService.java
index e60b307..05f7cf0 100644
--- a/services/java/com/android/server/pm/PackageManagerService.java
+++ b/services/java/com/android/server/pm/PackageManagerService.java
@@ -4352,7 +4352,10 @@
             if (p != null) {
                 PackageSetting ps = (PackageSetting)p.mExtras;
                 if (ps != null) {
-                    return ps.stopped;
+                    // System apps are never considered stopped for purposes of
+                    // filtering, because there may be no way for the user to
+                    // actually re-launch them.
+                    return ps.stopped && (ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0;
                 }
             }
             return false;
@@ -4524,7 +4527,10 @@
             if (p != null) {
                 PackageSetting ps = (PackageSetting)p.mExtras;
                 if (ps != null) {
-                    return ps.stopped;
+                    // System apps are never considered stopped for purposes of
+                    // filtering, because there may be no way for the user to
+                    // actually re-launch them.
+                    return ps.stopped && (ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0;
                 }
             }
             return false;