Revert "Don't wait for current activity to pause before resuming next."

This reverts commit cbb722ed06092a9e2be37150aa8bc635f0fe21da.
diff --git a/services/java/com/android/server/am/ActivityStack.java b/services/java/com/android/server/am/ActivityStack.java
index e8c8275..7b8bc26 100644
--- a/services/java/com/android/server/am/ActivityStack.java
+++ b/services/java/com/android/server/am/ActivityStack.java
@@ -227,13 +227,7 @@
      * When we are in the process of pausing an activity, before starting the
      * next one, this variable holds the activity that is currently being paused.
      */
-    final ArrayList<ActivityRecord> mPausingActivities = new ArrayList<ActivityRecord>();
-
-    /**
-     * These activities currently have their input paused, as they want for
-     * the next top activity to have its windows visible.
-     */
-    final ArrayList<ActivityRecord> mInputPausedActivities = new ArrayList<ActivityRecord>();
+    ActivityRecord mPausingActivity = null;
 
     /**
      * This is the last activity that we put into the paused state.  This is
@@ -813,9 +807,9 @@
                 startPausingLocked(false, true);
                 return;
             }
-            if (mPausingActivities.size() > 0) {
+            if (mPausingActivity != null) {
                 // Still waiting for something to pause; can't sleep yet.
-                if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivities);
+                if (DEBUG_PAUSE) Slog.v(TAG, "Sleep still waiting to pause " + mPausingActivity);
                 return;
             }
 
@@ -878,6 +872,11 @@
     }
 
     private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
+        if (mPausingActivity != null) {
+            RuntimeException e = new RuntimeException();
+            Slog.e(TAG, "Trying to pause when pause is already pending for "
+                  + mPausingActivity, e);
+        }
         ActivityRecord prev = mResumedActivity;
         if (prev == null) {
             RuntimeException e = new RuntimeException();
@@ -885,25 +884,19 @@
             resumeTopActivityLocked(null);
             return;
         }
-        if (mPausingActivities.contains(prev)) {
-            RuntimeException e = new RuntimeException();
-            Slog.e(TAG, "Trying to pause when pause when already pausing " + prev, e);
-        }
         if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSING: " + prev);
         else if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
         mResumedActivity = null;
-        mPausingActivities.add(prev);
+        mPausingActivity = prev;
         mLastPausedActivity = prev;
         prev.state = ActivityState.PAUSING;
         prev.task.touchActiveTime();
         prev.updateThumbnail(screenshotActivities(prev), null);
 
         mService.updateCpuStats();
-        ActivityRecord pausing;
         
         if (prev.app != null && prev.app.thread != null) {
             if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
-            pausing = prev;
             try {
                 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
                         System.identityHashCode(prev),
@@ -916,14 +909,12 @@
             } catch (Exception e) {
                 // Ignore exception, if process died other code will cleanup.
                 Slog.w(TAG, "Exception thrown during pause", e);
-                mPausingActivities.remove(prev);
+                mPausingActivity = null;
                 mLastPausedActivity = null;
-                pausing = null;
             }
         } else {
-            mPausingActivities.remove(prev);
+            mPausingActivity = null;
             mLastPausedActivity = null;
-            pausing = null;
         }
 
         // If we are not going to sleep, we want to ensure the device is
@@ -937,28 +928,18 @@
             }
         }
 
-        if (pausing != null) {
+
+        if (mPausingActivity != null) {
             // Have the window manager pause its key dispatching until the new
             // activity has started.  If we're pausing the activity just because
             // the screen is being turned off and the UI is sleeping, don't interrupt
             // key dispatch; the same activity will pick it up again on wakeup.
             if (!uiSleeping) {
-                pausing.pauseKeyDispatchingLocked();
-                mInputPausedActivities.add(prev);
+                prev.pauseKeyDispatchingLocked();
             } else {
                 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
             }
 
-            if (pausing.configDestroy) {
-                // The previous is being paused because the configuration
-                // is changing, which means it is actually stopping...
-                // To juggle the fact that we are also starting a new
-                // instance right now, we need to first completely stop
-                // the current instance before starting the new one.
-                if (DEBUG_PAUSE) Slog.v(TAG, "Destroying at pause: " + prev);
-                destroyActivityLocked(pausing, true, false, "pause-config");
-            }
-
             // Schedule a pause timeout in case the app doesn't respond.
             // We don't give it much time because this directly impacts the
             // responsiveness seen by the user.
@@ -970,10 +951,7 @@
             // This activity failed to schedule the
             // pause, so just treat it as being paused now.
             if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
-        }
-
-        if (!mService.isSleeping()) {
-            resumeTopActivityLocked(pausing);
+            resumeTopActivityLocked(null);
         }
     }
     
@@ -988,17 +966,16 @@
             if (index >= 0) {
                 r = mHistory.get(index);
                 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
-                if (mPausingActivities.contains(r)) {
+                if (mPausingActivity == r) {
                     if (DEBUG_STATES) Slog.v(TAG, "Moving to PAUSED: " + r
                             + (timeout ? " (due to timeout)" : " (pause complete)"));
                     r.state = ActivityState.PAUSED;
-                    completePauseLocked(r);
+                    completePauseLocked();
                 } else {
-                    ActivityRecord old = mPausingActivities.size() > 0
-                            ? mPausingActivities.get(0) : null;
                     EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
                             System.identityHashCode(r), r.shortComponentName, 
-                            old != null ? old.shortComponentName : "(none)");
+                            mPausingActivity != null
+                                ? mPausingActivity.shortComponentName : "(none)");
                 }
             }
         }
@@ -1024,14 +1001,8 @@
                 ProcessRecord fgApp = null;
                 if (mResumedActivity != null) {
                     fgApp = mResumedActivity.app;
-                } else {
-                    for (int i=mPausingActivities.size()-1; i>=0; i--) {
-                        ActivityRecord pausing = mPausingActivities.get(i);
-                        if (pausing.app == r.app) {
-                            fgApp = pausing.app;
-                            break;
-                        }
-                    }
+                } else if (mPausingActivity != null) {
+                    fgApp = mPausingActivity.app;
                 }
                 if (r.app != null && fgApp != null && r.app != fgApp
                         && r.lastVisibleTime > mService.mPreviousProcessVisibleTime
@@ -1043,49 +1014,58 @@
         }
     }
 
-    private final void completePauseLocked(ActivityRecord prev) {
+    private final void completePauseLocked() {
+        ActivityRecord prev = mPausingActivity;
         if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
         
-        if (prev.finishing) {
-            if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
-            prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE);
-        } else if (prev.app != null) {
-            if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
-            if (prev.waitingVisible) {
-                prev.waitingVisible = false;
-                mWaitingVisibleActivities.remove(prev);
-                if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
-                        TAG, "Complete pause, no longer waiting: " + prev);
-            }
-            if (prev.configDestroy) {
-                // The previous is being paused because the configuration
-                // is changing, which means it is actually stopping...
-                // To juggle the fact that we are also starting a new
-                // instance right now, we need to first completely stop
-                // the current instance before starting the new one.
-                if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
-                destroyActivityLocked(prev, true, false, "pause-config");
-            } else {
-                mStoppingActivities.add(prev);
-                if (mStoppingActivities.size() > 3) {
-                    // If we already have a few activities waiting to stop,
-                    // then give up on things going idle and start clearing
-                    // them out.
-                    if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
-                    scheduleIdleLocked();
-                } else {
-                    checkReadyForSleepLocked();
+        if (prev != null) {
+            if (prev.finishing) {
+                if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
+                prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE);
+            } else if (prev.app != null) {
+                if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
+                if (prev.waitingVisible) {
+                    prev.waitingVisible = false;
+                    mWaitingVisibleActivities.remove(prev);
+                    if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
+                            TAG, "Complete pause, no longer waiting: " + prev);
                 }
+                if (prev.configDestroy) {
+                    // The previous is being paused because the configuration
+                    // is changing, which means it is actually stopping...
+                    // To juggle the fact that we are also starting a new
+                    // instance right now, we need to first completely stop
+                    // the current instance before starting the new one.
+                    if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
+                    destroyActivityLocked(prev, true, false, "pause-config");
+                } else {
+                    mStoppingActivities.add(prev);
+                    if (mStoppingActivities.size() > 3) {
+                        // If we already have a few activities waiting to stop,
+                        // then give up on things going idle and start clearing
+                        // them out.
+                        if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
+                        scheduleIdleLocked();
+                    } else {
+                        checkReadyForSleepLocked();
+                    }
+                }
+            } else {
+                if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
+                prev = null;
             }
-        } else {
-            if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
-            prev = null;
+            mPausingActivity = null;
         }
-        mPausingActivities.remove(prev);
 
-        if (mService.isSleeping()) {
+        if (!mService.isSleeping()) {
+            resumeTopActivityLocked(prev);
+        } else {
             checkReadyForSleepLocked();
         }
+        
+        if (prev != null) {
+            prev.resumeKeyDispatchingLocked();
+        }
 
         if (prev.app != null && prev.cpuTimeAtResume > 0
                 && mService.mBatteryStatsService.isOnBattery()) {
@@ -1142,9 +1122,7 @@
         if (mMainStack) {
             mService.setFocusedActivityLocked(next);
         }
-        if (mInputPausedActivities.remove(next)) {
-            next.resumeKeyDispatchingLocked();
-        }
+        next.resumeKeyDispatchingLocked();
         ensureActivitiesVisibleLocked(null, 0);
         mService.mWindowManager.executeAppTransition();
         mNoAnimActivities.clear();
@@ -1374,6 +1352,13 @@
 
         if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
 
+        // If we are currently pausing an activity, then don't do anything
+        // until that is done.
+        if (mPausingActivity != null) {
+            if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: pausing=" + mPausingActivity);
+            return false;
+        }
+
         // Okay we are now going to start a switch, to 'next'.  We may first
         // have to pause the current activity, but this is an important point
         // where we have decided to go to 'next' so keep track of that.
@@ -2455,7 +2440,7 @@
         
         err = startActivityUncheckedLocked(r, sourceRecord,
                 grantedUriPermissions, grantedMode, onlyIfNeeded, true);
-        if (mDismissKeyguardOnNextActivity && mPausingActivities.size() == 0) {
+        if (mDismissKeyguardOnNextActivity && mPausingActivity == null) {
             // Someone asked to have the keyguard dismissed on the next
             // activity start, but we are not actually doing an activity
             // switch...  just dismiss the keyguard now, because we
@@ -3126,18 +3111,7 @@
         }
         mService.notifyAll();
     }
-
-    void reportActivityDrawnLocked(ActivityRecord r) {
-        if (mResumedActivity == r) {
-            // Once the resumed activity has been drawn, we can stop
-            // pausing input on all other activities.
-            for (int i=mInputPausedActivities.size()-1; i>=0; i--) {
-                mInputPausedActivities.get(i).resumeKeyDispatchingLocked();
-            }
-            mInputPausedActivities.clear();
-        }
-    }
-
+    
     void reportActivityVisibleLocked(ActivityRecord r) {
         for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
             WaitResult w = mWaitingActivityVisible.get(i);
@@ -3196,9 +3170,7 @@
                     mService.setFocusedActivityLocked(topRunningActivityLocked(null));
                 }
             }
-            if (mInputPausedActivities.remove(r)) {
-                r.resumeKeyDispatchingLocked();
-            }
+            r.resumeKeyDispatchingLocked();
             try {
                 r.stopped = false;
                 if (DEBUG_STATES) Slog.v(TAG, "Moving to STOPPING: " + r
@@ -3524,7 +3496,7 @@
             // Tell window manager to prepare for this one to be removed.
             mService.mWindowManager.setAppVisibility(r.appToken, false);
                 
-            if (!mPausingActivities.contains(r)) {
+            if (mPausingActivity == null) {
                 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
                 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
                 startPausingLocked(false, false);
@@ -3608,20 +3580,6 @@
         return r;
     }
 
-    final void appDiedLocked(ProcessRecord app) {
-        for (int i=mPausingActivities.size()-1; i>=0; i--) {
-            ActivityRecord r = mPausingActivities.get(i);
-            if (r.app == app) {
-                if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " + r);
-                mPausingActivities.remove(i);
-                mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
-            }
-        }
-        if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
-            mLastPausedActivity = null;
-        }
-    }
-
     /**
      * Perform the common clean-up of an activity record.  This is called both
      * as part of destroyActivityLocked() (when destroying the client-side