More steps to isolate animation.

- Create class to transfer state from WindowAnimator to
WindowManagerService.

- Detached wallpaper state was shared between the two classes. This
CL isolates it.

Change-Id: I7bcee348bf9f9f8f0228f36c53d75e5c92fd84cb
diff --git a/services/java/com/android/server/wm/WindowAnimator.java b/services/java/com/android/server/wm/WindowAnimator.java
index 758b6e7..e65d5d2 100644
--- a/services/java/com/android/server/wm/WindowAnimator.java
+++ b/services/java/com/android/server/wm/WindowAnimator.java
@@ -38,10 +38,18 @@
     ArrayList<WindowStateAnimator> mWinAnimators = new ArrayList<WindowStateAnimator>();
 
     boolean mAnimating;
-    boolean mTokenMayBeDrawn;
-    boolean mForceHiding;
-    WindowState mWindowAnimationBackground;
-    int mWindowAnimationBackgroundColor;
+
+    /** Variables only intended to be valid within each pass through animate(). Does not contain
+     * persistent state. */
+    private class InnerLoopParams {
+        boolean mTokenMayBeDrawn;
+        boolean mForceHiding;
+        WindowState mDetachedWallpaper = null;
+        WindowState mWindowAnimationBackground;
+        int mWindowAnimationBackgroundColor;
+    }
+    InnerLoopParams mInner = new InnerLoopParams();
+
     int mAdjResult;
 
     int mPendingLayoutChanges;
@@ -65,9 +73,9 @@
     // Window currently running an animation that has requested it be detached
     // from the wallpaper.  This means we need to ensure the wallpaper is
     // visible behind it in case it animates in a way that would allow it to be
-    // seen.
+    // seen. If multiple windows satisfy this, use the lowest window.
     WindowState mWindowDetachedWallpaper = null;
-    WindowState mDetachedWallpaper = null;
+
     DimSurface mWindowAnimationBackgroundSurface = null;
 
     int mBulkUpdateParams = 0;
@@ -103,19 +111,20 @@
     }
 
     private void testWallpaperAndBackgroundLocked() {
-        if (mWindowDetachedWallpaper != mDetachedWallpaper) {
+        final WindowState detachedWallpaper = mInner.mDetachedWallpaper;
+        if (mWindowDetachedWallpaper != detachedWallpaper) {
             if (WindowManagerService.DEBUG_WALLPAPER) Slog.v(TAG,
                     "Detached wallpaper changed from " + mWindowDetachedWallpaper
-                    + " to " + mDetachedWallpaper);
-            mWindowDetachedWallpaper = mDetachedWallpaper;
+                    + " to " + detachedWallpaper);
+            mWindowDetachedWallpaper = detachedWallpaper;
             mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
         }
 
-        if (mWindowAnimationBackgroundColor != 0) {
+        if (mInner.mWindowAnimationBackgroundColor != 0) {
             // If the window that wants black is the current wallpaper
             // target, then the black goes *below* the wallpaper so we
             // don't cause the wallpaper to suddenly disappear.
-            WindowState target = mWindowAnimationBackground;
+            WindowState target = mInner.mWindowAnimationBackground;
             if (mService.mWallpaperTarget == target
                     || mService.mLowerWallpaperTarget == target
                     || mService.mUpperWallpaperTarget == target) {
@@ -135,7 +144,7 @@
             final int dh = mDh;
             mWindowAnimationBackgroundSurface.show(dw, dh,
                     target.mWinAnimator.mAnimLayer - WindowManagerService.LAYER_OFFSET_DIM,
-                    mWindowAnimationBackgroundColor);
+                    mInner.mWindowAnimationBackgroundColor);
         } else if (mWindowAnimationBackgroundSurface != null) {
             mWindowAnimationBackgroundSurface.hide();
         }
@@ -199,9 +208,9 @@
         ArrayList<WindowStateAnimator> unForceHiding = null;
         boolean wallpaperInUnForceHiding = false;
 
-        for (int i = mService.mWindows.size() - 1; i >= 0; i--) {
-            WindowState win = mService.mWindows.get(i);
-            WindowStateAnimator winAnimator = win.mWinAnimator;
+        for (int i = mWinAnimators.size() - 1; i >= 0; i--) {
+            WindowStateAnimator winAnimator = mWinAnimators.get(i);
+            WindowState win = winAnimator.mWin;
             final int flags = winAnimator.mAttrFlags;
 
             if (winAnimator.mSurface != null) {
@@ -220,15 +229,15 @@
                     if (winAnimator.mAnimation != null) {
                         if ((flags & FLAG_SHOW_WALLPAPER) != 0
                                 && winAnimator.mAnimation.getDetachWallpaper()) {
-                            mDetachedWallpaper = win;
+                            mInner.mDetachedWallpaper = win;
                         }
                         final int backgroundColor = winAnimator.mAnimation.getBackgroundColor();
                         if (backgroundColor != 0) {
-                            if (mWindowAnimationBackground == null
-                                    || (winAnimator.mAnimLayer <
-                                            mWindowAnimationBackground.mWinAnimator.mAnimLayer)) {
-                                mWindowAnimationBackground = win;
-                                mWindowAnimationBackgroundColor = backgroundColor;
+                            final WindowState background = mInner.mWindowAnimationBackground;
+                            if (background == null || (winAnimator.mAnimLayer <
+                                    background.mWinAnimator.mAnimLayer)) {
+                                mInner.mWindowAnimationBackground = win;
+                                mInner.mWindowAnimationBackgroundColor = backgroundColor;
                             }
                         }
                     }
@@ -244,15 +253,15 @@
                         && appAnimator.animating) {
                     if ((flags & FLAG_SHOW_WALLPAPER) != 0
                             && appAnimator.animation.getDetachWallpaper()) {
-                        mDetachedWallpaper = win;
+                        mInner.mDetachedWallpaper = win;
                     }
                     final int backgroundColor = appAnimator.animation.getBackgroundColor();
                     if (backgroundColor != 0) {
-                        if (mWindowAnimationBackground == null
-                                || (winAnimator.mAnimLayer <
-                                        mWindowAnimationBackground.mWinAnimator.mAnimLayer)) {
-                            mWindowAnimationBackground = win;
-                            mWindowAnimationBackgroundColor = backgroundColor;
+                        final WindowState background = mInner.mWindowAnimationBackground;
+                        if (background == null || (winAnimator.mAnimLayer <
+                                background.mWinAnimator.mAnimLayer)) {
+                            mInner.mWindowAnimationBackground = win;
+                            mInner.mWindowAnimationBackgroundColor = backgroundColor;
                         }
                     }
                 }
@@ -280,10 +289,10 @@
                         mService.mFocusMayChange = true;
                     }
                     if (win.isReadyForDisplay() && !winAnimator.isAnimating()) {
-                        mForceHiding = true;
+                        mInner.mForceHiding = true;
                     }
                     if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG,
-                            "Force hide " + mForceHiding
+                            "Force hide " + mInner.mForceHiding
                             + " hasSurface=" + win.mHasSurface
                             + " policyVis=" + win.mPolicyVisibility
                             + " destroying=" + win.mDestroying
@@ -293,7 +302,7 @@
                             + " anim=" + win.mWinAnimator.mAnimation);
                 } else if (mPolicy.canBeForceHidden(win, win.mAttrs)) {
                     final boolean changed;
-                    if (mForceHiding && !winAnimator.isAnimating()) {
+                    if (mInner.mForceHiding && !winAnimator.isAnimating()) {
                         changed = win.hideLw(false, false);
                         if (WindowManagerService.DEBUG_VISIBILITY && changed) Slog.v(TAG,
                                 "Now policy hidden: " + win);
@@ -308,7 +317,7 @@
                                     unForceHiding = new ArrayList<WindowStateAnimator>();
                                 }
                                 unForceHiding.add(winAnimator);
-                                if ((win.mAttrs.flags&WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
+                                if ((flags & FLAG_SHOW_WALLPAPER) != 0) {
                                     wallpaperInUnForceHiding = true;
                                 }
                             }
@@ -320,7 +329,7 @@
                             }
                         }
                     }
-                    if (changed && (flags & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER) != 0) {
+                    if (changed && (flags & FLAG_SHOW_WALLPAPER) != 0) {
                         mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
                         mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
                         if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
@@ -364,7 +373,7 @@
                                         "tokenMayBeDrawn: " + atoken
                                         + " freezingScreen=" + atoken.mAppAnimator.freezingScreen
                                         + " mAppFreezing=" + win.mAppFreezing);
-                                mTokenMayBeDrawn = true;
+                                mInner.mTokenMayBeDrawn = true;
                             }
                         }
                     } else if (win.isDrawnLw()) {
@@ -454,18 +463,18 @@
     }
 
     private void performAnimationsLocked() {
-        mTokenMayBeDrawn = false;
-        mForceHiding = false;
-        mDetachedWallpaper = null;
-        mWindowAnimationBackground = null;
-        mWindowAnimationBackgroundColor = 0;
+        mInner.mTokenMayBeDrawn = false;
+        mInner.mForceHiding = false;
+        mInner.mDetachedWallpaper = null;
+        mInner.mWindowAnimationBackground = null;
+        mInner.mWindowAnimationBackgroundColor = 0;
 
         updateWindowsAndWallpaperLocked();
         if ((mPendingLayoutChanges & WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
             mPendingActions |= WALLPAPER_ACTION_PENDING;
         }
 
-        if (mTokenMayBeDrawn) {
+        if (mInner.mTokenMayBeDrawn) {
             testTokenMayBeDrawnLocked();
         }
     }
@@ -526,7 +535,15 @@
             Surface.closeTransaction();
         }
 
-        mService.bulkSetParameters(mBulkUpdateParams, mPendingLayoutChanges);
+        if (mBulkUpdateParams != 0 || mPendingLayoutChanges != 0) {
+            final WindowManagerService.AnimatorToLayoutParams animToLayout = mService.mAnimToLayout;
+            synchronized (animToLayout) {
+                animToLayout.mBulkUpdateParams = mBulkUpdateParams;
+                animToLayout.mPendingLayoutChanges = mPendingLayoutChanges;
+                animToLayout.mWindowDetachedWallpaper = mWindowDetachedWallpaper;
+                mService.setAnimatorParameters();
+            }
+        }
 
         if (mAnimating) {
             mService.scheduleAnimationLocked();
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index 6c8d969..cf7a196 100755
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -507,9 +507,9 @@
 
     // State while inside of layoutAndPlaceSurfacesLocked().
     boolean mFocusMayChange;
-    
+
     Configuration mCurConfiguration = new Configuration();
-    
+
     // This is held as long as we have the screen frozen, to give us time to
     // perform a rotation animation when turning off shows the lock screen which
     // changes the orientation.
@@ -636,7 +636,17 @@
         private float mButtonBrightness = -1;
         private boolean mUpdateRotation = false;
     }
-    LayoutFields mInnerFields = new LayoutFields();
+    final LayoutFields mInnerFields = new LayoutFields();
+
+    static class AnimatorToLayoutParams {
+      int mBulkUpdateParams;
+      int mPendingLayoutChanges;
+      WindowState mWindowDetachedWallpaper;
+    }
+    final AnimatorToLayoutParams mAnimToLayout = new AnimatorToLayoutParams();
+
+    /** The lowest wallpaper target with a detached wallpaper animation on it. */
+    WindowState mWindowDetachedWallpaper = null;
 
     /** Only do a maximum of 6 repeated layouts. After that quit */
     private int mLayoutRepeatCount;
@@ -667,7 +677,7 @@
     }
     final AnimationRunnable mAnimationRunnable = new AnimationRunnable();
     boolean mAnimationScheduled;
-    
+
     final WindowAnimator mAnimator;
 
     final class DragInputEventReceiver extends InputEventReceiver {
@@ -1572,9 +1582,9 @@
                     Slog.v(TAG, "List with no IM target:");
                     logWindowList("  ");
                 }
-                if (DN > 0) moveInputMethodDialogsLocked(-1);;
+                if (DN > 0) moveInputMethodDialogsLocked(-1);
             } else {
-                moveInputMethodDialogsLocked(-1);;
+                moveInputMethodDialogsLocked(-1);
             }
 
         }
@@ -1636,7 +1646,7 @@
                 continue;
             }
             topCurW = null;
-            if (w != mAnimator.mWindowDetachedWallpaper && w.mAppToken != null) {
+            if (w != mWindowDetachedWallpaper && w.mAppToken != null) {
                 // If this window's app token is hidden and not animating,
                 // it is of no interest to us.
                 if (w.mAppToken.hidden && w.mAppToken.mAppAnimator.animation == null) {
@@ -1662,7 +1672,7 @@
                     continue;
                 }
                 break;
-            } else if (w == mAnimator.mWindowDetachedWallpaper) {
+            } else if (w == mWindowDetachedWallpaper) {
                 windowDetachedI = i;
             }
         }
@@ -3104,12 +3114,11 @@
             a.setDetachWallpaper(true);
             a.setDuration(duration);
             return a;
-        } else {
-            // For normal animations, the exiting element just holds in place.
-            Animation a = new AlphaAnimation(1, 1);
-            a.setDuration(duration);
-            return a;
         }
+        // For normal animations, the exiting element just holds in place.
+        Animation a = new AlphaAnimation(1, 1);
+        a.setDuration(duration);
+        return a;
     }
 
     /**
@@ -6822,7 +6831,7 @@
         public static final int REPORT_HARD_KEYBOARD_STATUS_CHANGE = 22;
         public static final int BOOT_TIMEOUT = 23;
         public static final int WAITING_FOR_DRAWN_TIMEOUT = 24;
-        public static final int BULK_UPDATE_PARAMETERS = 25;
+        public static final int UPDATE_ANIM_PARAMETERS = 25;
         public static final int SHOW_STRICT_MODE_VIOLATION = 26;
         public static final int DO_ANIMATION_CALLBACK = 27;
 
@@ -7249,44 +7258,49 @@
                     break;
                 }
 
-                case BULK_UPDATE_PARAMETERS: {
+                case UPDATE_ANIM_PARAMETERS: {
                     // Used to send multiple changes from the animation side to the layout side.
                     synchronized (mWindowMap) {
-                        boolean doRequest = false;
-                        // TODO(cmautner): As the number of bits grows, use masks of bit groups to
-                        //  eliminate unnecessary tests.
-                        if ((msg.arg1 & LayoutFields.SET_UPDATE_ROTATION) != 0) {
-                            mInnerFields.mUpdateRotation = true;
-                            doRequest = true;
-                        }
-                        if ((msg.arg1 & LayoutFields.SET_WALLPAPER_MAY_CHANGE) != 0) {
-                            mInnerFields.mWallpaperMayChange = true;
-                            doRequest = true;
-                        }
-                        if ((msg.arg1 & LayoutFields.SET_FORCE_HIDING_CHANGED) != 0) {
-                            mInnerFields.mWallpaperForceHidingChanged = true;
-                            doRequest = true;
-                        }
-                        if ((msg.arg1 & LayoutFields.CLEAR_ORIENTATION_CHANGE_COMPLETE) != 0) {
-                            mInnerFields.mOrientationChangeComplete = false;
-                        } else {
-                            mInnerFields.mOrientationChangeComplete = true;
-                            if (mWindowsFreezingScreen) {
+                        synchronized (mAnimToLayout) {
+                            boolean doRequest = false;
+                            final int bulkUpdateParams = mAnimToLayout.mBulkUpdateParams;
+                            // TODO(cmautner): As the number of bits grows, use masks of bit groups to
+                            //  eliminate unnecessary tests.
+                            if ((bulkUpdateParams & LayoutFields.SET_UPDATE_ROTATION) != 0) {
+                                mInnerFields.mUpdateRotation = true;
                                 doRequest = true;
                             }
-                        }
-                        if ((msg.arg1 & LayoutFields.SET_TURN_ON_SCREEN) != 0) {
-                            mTurnOnScreen = true;
-                        }
+                            if ((bulkUpdateParams & LayoutFields.SET_WALLPAPER_MAY_CHANGE) != 0) {
+                                mInnerFields.mWallpaperMayChange = true;
+                                doRequest = true;
+                            }
+                            if ((bulkUpdateParams & LayoutFields.SET_FORCE_HIDING_CHANGED) != 0) {
+                                mInnerFields.mWallpaperForceHidingChanged = true;
+                                doRequest = true;
+                            }
+                            if ((bulkUpdateParams & LayoutFields.CLEAR_ORIENTATION_CHANGE_COMPLETE) != 0) {
+                                mInnerFields.mOrientationChangeComplete = false;
+                            } else {
+                                mInnerFields.mOrientationChangeComplete = true;
+                                if (mWindowsFreezingScreen) {
+                                    doRequest = true;
+                                }
+                            }
+                            if ((bulkUpdateParams & LayoutFields.SET_TURN_ON_SCREEN) != 0) {
+                                mTurnOnScreen = true;
+                            }
 
-                        mPendingLayoutChanges |= msg.arg2;
-                        if (mPendingLayoutChanges != 0) {
-                            doRequest = true;
-                        }
+                            mPendingLayoutChanges |= mAnimToLayout.mPendingLayoutChanges;
+                            if (mPendingLayoutChanges != 0) {
+                                doRequest = true;
+                            }
 
-                        if (doRequest) {
-                            mH.sendEmptyMessage(CLEAR_PENDING_ACTIONS);
-                            performLayoutAndPlaceSurfacesLocked();
+                            mWindowDetachedWallpaper = mAnimToLayout.mWindowDetachedWallpaper;
+
+                            if (doRequest) {
+                                mH.sendEmptyMessage(CLEAR_PENDING_ACTIONS);
+                                performLayoutAndPlaceSurfacesLocked();
+                            }
                         }
                     }
                     break;
@@ -9957,8 +9971,7 @@
         }
     }
 
-    void bulkSetParameters(final int bulkUpdateParams, int pendingLayoutChanges) {
-        mH.sendMessage(mH.obtainMessage(H.BULK_UPDATE_PARAMETERS, bulkUpdateParams,
-                pendingLayoutChanges));
+    void setAnimatorParameters() {
+        mH.sendMessage(mH.obtainMessage(H.UPDATE_ANIM_PARAMETERS));
     }
 }