Wallpaper target selection during animation

In case of double tapping from Home, there could be two set of
transitions, first is launcher->Recents, followed shortly by
Recents->app. On the second transition, it should be running
a wallpaper close, but because of the way we pick wallpaper
target, sometimes we pick launcher if both launcher & Recents
are animating away. This makes us to run a different animation
which could involve dimming the wallpaper briefly.

Also, findWallpaperTarget() sometimes could toggle between two
valid wallpaper targets, picking either of them gives correct
result for showing the wallpaper, but could result in different
exit transition.

This change prefers the visible target if there is only one
visible, otherwise the one in opening or closing app list. This
helps maybeUpdateTransitToWallpaper to better choose transition.

bug: 30831873
Change-Id: I0eaa89bdc35f5f51875d5cbeceba11ce40f4791f
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index 1f385df..b065392 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -340,6 +340,7 @@
     }
 
     void clearAnimatingFlags() {
+        boolean wallpaperMightChange = false;
         for (int i = allAppWindows.size() - 1; i >= 0; i--) {
             final WindowState win = allAppWindows.get(i);
             // We don't want to clear it out for windows that get replaced, because the
@@ -350,7 +351,6 @@
             // by the client. We should let animation proceed and not clear this flag or
             // they won't eventually be removed by WindowStateAnimator#finishExit.
             if (!win.mWillReplaceWindow && !win.mRemoveOnExit) {
-                win.mAnimatingExit = false;
                 // Clear mAnimating flag together with mAnimatingExit. When animation
                 // changes from exiting to entering, we need to clear this flag until the
                 // new animation gets applied, so that isAnimationStarting() becomes true
@@ -358,15 +358,24 @@
                 // Otherwise applySurfaceChangesTransaction will faill to skip surface
                 // placement for this window during this period, one or more frame will
                 // show up with wrong position or scale.
-                win.mWinAnimator.mAnimating = false;
-
+                if (win.mAnimatingExit) {
+                    win.mAnimatingExit = false;
+                    wallpaperMightChange = true;
+                }
+                if (win.mWinAnimator.mAnimating) {
+                    win.mWinAnimator.mAnimating = false;
+                    wallpaperMightChange = true;
+                }
                 if (win.mDestroying) {
                     win.mDestroying = false;
                     service.mDestroySurface.remove(win);
+                    wallpaperMightChange = true;
                 }
             }
         }
-        requestUpdateWallpaperIfNeeded();
+        if (wallpaperMightChange) {
+            requestUpdateWallpaperIfNeeded();
+        }
     }
 
     void destroySurfaces() {