Some fixes for saved surfaces

- If we have a saved surface, and app relayouts to visible before we
  started entry animation, we need to restore the saved surfaces.
  Otherwise the surface might get stuck in the saved state, because
  we may not get to run any animation after this relayout.

- Keep track of the allDrawn while we're using the saved surfaces,
  so that we can rely on allDrawn itself, instead of whether we're
  using saved surfaces.

- If the app is set to visible when it's exiting, clear the exiting
  flags. Also, save the surface if we cancel an exiting animation.

- More debug logging.

bug: 26819496
Change-Id: Ie42c6eea7879632d82f24ed09c3b6e737dd6d8a4
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index c0d34e8..6452fee 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -299,10 +299,10 @@
         }
     }
 
-    void markSurfacesExiting() {
+    void setWindowsExiting(boolean exiting) {
         for (int i = allAppWindows.size() - 1; i >= 0; i--) {
             WindowState win = allAppWindows.get(i);
-            win.mExiting = true;
+            win.mExiting = exiting;
         }
     }
 
@@ -312,11 +312,11 @@
      * @return true if the surfaces should be saved, false otherwise.
      */
     boolean shouldSaveSurface() {
-        // We want to save surface if the app's windows are "allDrawn", or if we're
-        // currently animating with save surfaces. (If the app didn't even finish
-        // drawing when the user exits, but we have a saved surface from last time,
-        // we still want to keep that surface.)
-        return allDrawn || mAnimatingWithSavedSurface;
+        // We want to save surface if the app's windows are "allDrawn".
+        // (If we started entering animation early with saved surfaces, allDrawn
+        // should have been restored to true. So we'll save again in that case
+        // even if app didn't actually finish drawing.)
+        return allDrawn;
     }
 
     boolean hasSavedSurface() {
@@ -334,13 +334,26 @@
             return;
         }
         mAnimatingWithSavedSurface = true;
+
+        // Check if we have enough drawn windows to mark allDrawn= true.
+        int numInteresting = 0;
+        int numDrawn = 0;
         for (int i = windows.size() - 1; i >= 0; i--) {
-            WindowState ws = windows.get(i);
-            ws.restoreSavedSurface();
+            WindowState w = windows.get(i);
+            w.restoreSavedSurface();
+            if (w != startingWindow && !w.mAppDied
+                    && (!mAppAnimator.freezingScreen || !w.mAppFreezing)) {
+                numInteresting++;
+                if (w.isDrawnLw()) {
+                    numDrawn++;
+                }
+            }
         }
-        // Mark the app allDrawn since it must be allDrawn at the time
-        // it was first saved.
-        allDrawn = true;
+
+        allDrawn |= (numInteresting == numDrawn);
+
+        if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
+                "restoreSavedSurfaces: " + appWindowToken + " allDrawn=" + allDrawn);
     }
 
     void destroySavedSurfaces() {
@@ -348,6 +361,7 @@
             WindowState win = windows.get(i);
             win.destroySavedSurface();
         }
+        mAnimatingWithSavedSurface = false;
     }
 
     @Override