WindowAnimator.java revision e29b2dbc762bfa66093d76f5a65f55328d8753c9
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
19import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
20import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
21
22import static com.android.server.wm.WindowManagerService.LayoutFields.SET_UPDATE_ROTATION;
23import static com.android.server.wm.WindowManagerService.LayoutFields.SET_WALLPAPER_MAY_CHANGE;
24import static com.android.server.wm.WindowManagerService.LayoutFields.SET_FORCE_HIDING_CHANGED;
25import static com.android.server.wm.WindowManagerService.LayoutFields.SET_ORIENTATION_CHANGE_COMPLETE;
26import static com.android.server.wm.WindowManagerService.LayoutFields.SET_WALLPAPER_ACTION_PENDING;
27
28import android.content.Context;
29import android.os.Debug;
30import android.os.SystemClock;
31import android.util.Log;
32import android.util.Slog;
33import android.util.SparseArray;
34import android.util.SparseIntArray;
35import android.util.TimeUtils;
36import android.view.Display;
37import android.view.SurfaceControl;
38import android.view.WindowManagerPolicy;
39import android.view.animation.AlphaAnimation;
40import android.view.animation.Animation;
41
42import com.android.server.wm.WindowManagerService.LayoutFields;
43
44import java.io.PrintWriter;
45import java.util.ArrayList;
46
47/**
48 * Singleton class that carries out the animations and Surface operations in a separate task
49 * on behalf of WindowManagerService.
50 */
51public class WindowAnimator {
52    private static final String TAG = "WindowAnimator";
53
54    /** How long to give statusbar to clear the private keyguard flag when animating out */
55    private static final long KEYGUARD_ANIM_TIMEOUT_MS = 1000;
56
57    final WindowManagerService mService;
58    final Context mContext;
59    final WindowManagerPolicy mPolicy;
60
61    boolean mAnimating;
62
63    final Runnable mAnimationRunnable;
64
65    /** Time of current animation step. Reset on each iteration */
66    long mCurrentTime;
67
68    /** Skip repeated AppWindowTokens initialization. Note that AppWindowsToken's version of this
69     * is a long initialized to Long.MIN_VALUE so that it doesn't match this value on startup. */
70    private int mAnimTransactionSequence;
71
72    /** Window currently running an animation that has requested it be detached
73     * from the wallpaper.  This means we need to ensure the wallpaper is
74     * visible behind it in case it animates in a way that would allow it to be
75     * seen. If multiple windows satisfy this, use the lowest window. */
76    WindowState mWindowDetachedWallpaper = null;
77
78    WindowStateAnimator mUniverseBackground = null;
79    int mAboveUniverseLayer = 0;
80
81    int mBulkUpdateParams = 0;
82    Object mLastWindowFreezeSource;
83
84    SparseArray<DisplayContentsAnimator> mDisplayContentsAnimators =
85            new SparseArray<DisplayContentsAnimator>(2);
86
87    boolean mInitialized = false;
88
89    boolean mKeyguardGoingAway;
90
91    // forceHiding states.
92    static final int KEYGUARD_NOT_SHOWN     = 0;
93    static final int KEYGUARD_ANIMATING_IN  = 1;
94    static final int KEYGUARD_SHOWN         = 2;
95    static final int KEYGUARD_ANIMATING_OUT = 3;
96    int mForceHiding = KEYGUARD_NOT_SHOWN;
97
98    private String forceHidingToString() {
99        switch (mForceHiding) {
100            case KEYGUARD_NOT_SHOWN:    return "KEYGUARD_NOT_SHOWN";
101            case KEYGUARD_ANIMATING_IN: return "KEYGUARD_ANIMATING_IN";
102            case KEYGUARD_SHOWN:        return "KEYGUARD_SHOWN";
103            case KEYGUARD_ANIMATING_OUT:return "KEYGUARD_ANIMATING_OUT";
104            default: return "KEYGUARD STATE UNKNOWN " + mForceHiding;
105        }
106    }
107
108    WindowAnimator(final WindowManagerService service) {
109        mService = service;
110        mContext = service.mContext;
111        mPolicy = service.mPolicy;
112
113        mAnimationRunnable = new Runnable() {
114            @Override
115            public void run() {
116                synchronized (mService.mWindowMap) {
117                    mService.mAnimationScheduled = false;
118                    animateLocked();
119                }
120            }
121        };
122    }
123
124    void addDisplayLocked(final int displayId) {
125        // Create the DisplayContentsAnimator object by retrieving it.
126        getDisplayContentsAnimatorLocked(displayId);
127        if (displayId == Display.DEFAULT_DISPLAY) {
128            mInitialized = true;
129        }
130    }
131
132    void removeDisplayLocked(final int displayId) {
133        final DisplayContentsAnimator displayAnimator = mDisplayContentsAnimators.get(displayId);
134        if (displayAnimator != null) {
135            if (displayAnimator.mScreenRotationAnimation != null) {
136                displayAnimator.mScreenRotationAnimation.kill();
137                displayAnimator.mScreenRotationAnimation = null;
138            }
139        }
140
141        mDisplayContentsAnimators.delete(displayId);
142    }
143
144    void hideWallpapersLocked(final WindowState w) {
145        final WindowState wallpaperTarget = mService.mWallpaperTarget;
146        final WindowState lowerWallpaperTarget = mService.mLowerWallpaperTarget;
147        final ArrayList<WindowToken> wallpaperTokens = mService.mWallpaperTokens;
148
149        if ((wallpaperTarget == w && lowerWallpaperTarget == null) || wallpaperTarget == null) {
150            final int numTokens = wallpaperTokens.size();
151            for (int i = numTokens - 1; i >= 0; i--) {
152                final WindowToken token = wallpaperTokens.get(i);
153                final int numWindows = token.windows.size();
154                for (int j = numWindows - 1; j >= 0; j--) {
155                    final WindowState wallpaper = token.windows.get(j);
156                    final WindowStateAnimator winAnimator = wallpaper.mWinAnimator;
157                    if (!winAnimator.mLastHidden) {
158                        winAnimator.hide();
159                        mService.dispatchWallpaperVisibility(wallpaper, false);
160                        setPendingLayoutChanges(Display.DEFAULT_DISPLAY,
161                                WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
162                    }
163                }
164                if (WindowManagerService.DEBUG_WALLPAPER_LIGHT && !token.hidden) Slog.d(TAG,
165                        "Hiding wallpaper " + token + " from " + w
166                        + " target=" + wallpaperTarget + " lower=" + lowerWallpaperTarget
167                        + "\n" + Debug.getCallers(5, "  "));
168                token.hidden = true;
169            }
170        }
171    }
172
173    private void updateAppWindowsLocked(int displayId) {
174        ArrayList<TaskStack> stacks = mService.getDisplayContentLocked(displayId).getStacks();
175        for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
176            final TaskStack stack = stacks.get(stackNdx);
177            final ArrayList<Task> tasks = stack.getTasks();
178            for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
179                final AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
180                for (int tokenNdx = tokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
181                    final AppWindowAnimator appAnimator = tokens.get(tokenNdx).mAppAnimator;
182                    final boolean wasAnimating = appAnimator.animation != null
183                            && appAnimator.animation != AppWindowAnimator.sDummyAnimation;
184                    if (appAnimator.stepAnimationLocked(mCurrentTime)) {
185                        mAnimating = true;
186                    } else if (wasAnimating) {
187                        // stopped animating, do one more pass through the layout
188                        setAppLayoutChanges(appAnimator,
189                                WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER,
190                                "appToken " + appAnimator.mAppToken + " done");
191                        if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
192                                "updateWindowsApps...: done animating " + appAnimator.mAppToken);
193                    }
194                }
195            }
196
197            final AppTokenList exitingAppTokens = stack.mExitingAppTokens;
198            final int NEAT = exitingAppTokens.size();
199            for (int i = 0; i < NEAT; i++) {
200                final AppWindowAnimator appAnimator = exitingAppTokens.get(i).mAppAnimator;
201                final boolean wasAnimating = appAnimator.animation != null
202                        && appAnimator.animation != AppWindowAnimator.sDummyAnimation;
203                if (appAnimator.stepAnimationLocked(mCurrentTime)) {
204                    mAnimating = true;
205                } else if (wasAnimating) {
206                    // stopped animating, do one more pass through the layout
207                    setAppLayoutChanges(appAnimator, WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER,
208                        "exiting appToken " + appAnimator.mAppToken + " done");
209                    if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
210                            "updateWindowsApps...: done animating exiting " + appAnimator.mAppToken);
211                }
212            }
213        }
214    }
215
216    private void updateWindowsLocked(final int displayId) {
217        ++mAnimTransactionSequence;
218
219        final WindowList windows = mService.getWindowListLocked(displayId);
220        ArrayList<WindowStateAnimator> unForceHiding = null;
221        boolean wallpaperInUnForceHiding = false;
222
223        if (mKeyguardGoingAway) {
224            for (int i = windows.size() - 1; i >= 0; i--) {
225                WindowState win = windows.get(i);
226                if (!mPolicy.isKeyguardHostWindow(win.mAttrs)) {
227                    continue;
228                }
229                final WindowStateAnimator winAnimator = win.mWinAnimator;
230                if (mPolicy.doesForceHide(win.mAttrs)) {
231                    if (!winAnimator.mAnimating) {
232                        // Create a new animation to delay until keyguard is gone on its own.
233                        winAnimator.mAnimation = new AlphaAnimation(1.0f, 1.0f);
234                        winAnimator.mAnimation.setDuration(KEYGUARD_ANIM_TIMEOUT_MS);
235                        winAnimator.mAnimationIsEntrance = false;
236                    }
237                } else {
238                    mKeyguardGoingAway = false;
239                    winAnimator.clearAnimation();
240                }
241                break;
242            }
243        }
244
245        mForceHiding = KEYGUARD_NOT_SHOWN;
246
247        for (int i = windows.size() - 1; i >= 0; i--) {
248            WindowState win = windows.get(i);
249            WindowStateAnimator winAnimator = win.mWinAnimator;
250            final int flags = winAnimator.mAttrFlags;
251
252            if (winAnimator.mSurfaceControl != null) {
253                final boolean wasAnimating = winAnimator.mWasAnimating;
254                final boolean nowAnimating = winAnimator.stepAnimationLocked(mCurrentTime);
255
256                if (WindowManagerService.DEBUG_WALLPAPER) {
257                    Slog.v(TAG, win + ": wasAnimating=" + wasAnimating +
258                            ", nowAnimating=" + nowAnimating);
259                }
260
261                if (wasAnimating && !winAnimator.mAnimating && mService.mWallpaperTarget == win) {
262                    mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
263                    setPendingLayoutChanges(Display.DEFAULT_DISPLAY,
264                            WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
265                    if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
266                        mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 2",
267                                getPendingLayoutChanges(Display.DEFAULT_DISPLAY));
268                    }
269                }
270
271                if (mPolicy.doesForceHide(win.mAttrs)) {
272                    if (!wasAnimating && nowAnimating) {
273                        if (WindowManagerService.DEBUG_ANIM ||
274                                WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG,
275                                "Animation started that could impact force hide: " + win);
276                        mBulkUpdateParams |= SET_FORCE_HIDING_CHANGED;
277                        setPendingLayoutChanges(displayId,
278                                WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
279                        if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
280                            mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 3",
281                                    getPendingLayoutChanges(displayId));
282                        }
283                        mService.mFocusMayChange = true;
284                    } else if (mKeyguardGoingAway && !nowAnimating) {
285                        // Timeout!!
286                        Slog.e(TAG, "Timeout waiting for animation to startup");
287                        mPolicy.startKeyguardExitAnimation(0, 0);
288                        mKeyguardGoingAway = false;
289                    }
290                    if (win.isReadyForDisplay()) {
291                        if (nowAnimating) {
292                            if (winAnimator.mAnimationIsEntrance) {
293                                mForceHiding = KEYGUARD_ANIMATING_IN;
294                            } else {
295                                mForceHiding = KEYGUARD_ANIMATING_OUT;
296                            }
297                        } else {
298                            mForceHiding = win.isDrawnLw() ? KEYGUARD_SHOWN : KEYGUARD_NOT_SHOWN;
299                        }
300                    }
301                    if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG,
302                            "Force hide " + forceHidingToString()
303                            + " hasSurface=" + win.mHasSurface
304                            + " policyVis=" + win.mPolicyVisibility
305                            + " destroying=" + win.mDestroying
306                            + " attHidden=" + win.mAttachedHidden
307                            + " vis=" + win.mViewVisibility
308                            + " hidden=" + win.mRootToken.hidden
309                            + " anim=" + win.mWinAnimator.mAnimation);
310                } else if (mPolicy.canBeForceHidden(win, win.mAttrs)) {
311                    final boolean hideWhenLocked =
312                            (winAnimator.mAttrFlags & FLAG_SHOW_WHEN_LOCKED) == 0;
313                    final boolean changed;
314                    if (((mForceHiding == KEYGUARD_ANIMATING_IN)
315                                && (!winAnimator.isAnimating() || hideWhenLocked))
316                            || ((mForceHiding == KEYGUARD_SHOWN) && hideWhenLocked)) {
317                        changed = win.hideLw(false, false);
318                        if (WindowManagerService.DEBUG_VISIBILITY && changed) Slog.v(TAG,
319                                "Now policy hidden: " + win);
320                    } else {
321                        changed = win.showLw(false, false);
322                        if (WindowManagerService.DEBUG_VISIBILITY && changed) Slog.v(TAG,
323                                "Now policy shown: " + win);
324                        if (changed) {
325                            if ((mBulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0
326                                    && win.isVisibleNow() /*w.isReadyForDisplay()*/) {
327                                if (unForceHiding == null) {
328                                    unForceHiding = new ArrayList<WindowStateAnimator>();
329                                }
330                                unForceHiding.add(winAnimator);
331                                if ((flags & FLAG_SHOW_WALLPAPER) != 0) {
332                                    wallpaperInUnForceHiding = true;
333                                }
334                            }
335                            final WindowState currentFocus = mService.mCurrentFocus;
336                            if (currentFocus == null || currentFocus.mLayer < win.mLayer) {
337                                // We are showing on to of the current
338                                // focus, so re-evaluate focus to make
339                                // sure it is correct.
340                                if (WindowManagerService.DEBUG_FOCUS_LIGHT) Slog.v(TAG,
341                                        "updateWindowsLocked: setting mFocusMayChange true");
342                                mService.mFocusMayChange = true;
343                            }
344                        }
345                    }
346                    if (changed && (flags & FLAG_SHOW_WALLPAPER) != 0) {
347                        mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
348                        setPendingLayoutChanges(Display.DEFAULT_DISPLAY,
349                                WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
350                        if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
351                            mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 4",
352                                    getPendingLayoutChanges(Display.DEFAULT_DISPLAY));
353                        }
354                    }
355                }
356            }
357
358            final AppWindowToken atoken = win.mAppToken;
359            if (winAnimator.mDrawState == WindowStateAnimator.READY_TO_SHOW) {
360                if (atoken == null || atoken.allDrawn) {
361                    if (winAnimator.performShowLocked()) {
362                        setPendingLayoutChanges(displayId,
363                                WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
364                        if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
365                            mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 5",
366                                    getPendingLayoutChanges(displayId));
367                        }
368                    }
369                }
370            }
371            final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
372            if (appAnimator != null && appAnimator.thumbnail != null) {
373                if (appAnimator.thumbnailTransactionSeq != mAnimTransactionSequence) {
374                    appAnimator.thumbnailTransactionSeq = mAnimTransactionSequence;
375                    appAnimator.thumbnailLayer = 0;
376                }
377                if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
378                    appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
379                }
380            }
381        } // end forall windows
382
383        // If we have windows that are being show due to them no longer
384        // being force-hidden, apply the appropriate animation to them.
385        if (unForceHiding != null) {
386            boolean startKeyguardExit = true;
387            for (int i=unForceHiding.size()-1; i>=0; i--) {
388                Animation a = mPolicy.createForceHideEnterAnimation(wallpaperInUnForceHiding);
389                if (a != null) {
390                    final WindowStateAnimator winAnimator = unForceHiding.get(i);
391                    winAnimator.setAnimation(a);
392                    winAnimator.mAnimationIsEntrance = true;
393                    if (startKeyguardExit) {
394                        // Do one time only.
395                        mPolicy.startKeyguardExitAnimation(mCurrentTime + a.getStartOffset(),
396                                a.getDuration());
397                        mKeyguardGoingAway = false;
398                        startKeyguardExit = false;
399                    }
400                }
401            }
402        }
403    }
404
405    private void updateWallpaperLocked(int displayId) {
406        mService.getDisplayContentLocked(displayId).resetAnimationBackgroundAnimator();
407
408        final WindowList windows = mService.getWindowListLocked(displayId);
409        WindowState detachedWallpaper = null;
410
411        for (int i = windows.size() - 1; i >= 0; i--) {
412            final WindowState win = windows.get(i);
413            WindowStateAnimator winAnimator = win.mWinAnimator;
414            if (winAnimator.mSurfaceControl == null) {
415                continue;
416            }
417
418            final int flags = winAnimator.mAttrFlags;
419
420            // If this window is animating, make a note that we have
421            // an animating window and take care of a request to run
422            // a detached wallpaper animation.
423            if (winAnimator.mAnimating) {
424                if (winAnimator.mAnimation != null) {
425                    if ((flags & FLAG_SHOW_WALLPAPER) != 0
426                            && winAnimator.mAnimation.getDetachWallpaper()) {
427                        detachedWallpaper = win;
428                    }
429                    final int color = winAnimator.mAnimation.getBackgroundColor();
430                    if (color != 0) {
431                        win.getStack().setAnimationBackground(winAnimator, color);
432                    }
433                }
434                mAnimating = true;
435            }
436
437            // If this window's app token is running a detached wallpaper
438            // animation, make a note so we can ensure the wallpaper is
439            // displayed behind it.
440            final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
441            if (appAnimator != null && appAnimator.animation != null
442                    && appAnimator.animating) {
443                if ((flags & FLAG_SHOW_WALLPAPER) != 0
444                        && appAnimator.animation.getDetachWallpaper()) {
445                    detachedWallpaper = win;
446                }
447
448                final int color = appAnimator.animation.getBackgroundColor();
449                if (color != 0) {
450                    win.getStack().setAnimationBackground(winAnimator, color);
451                }
452            }
453        } // end forall windows
454
455        if (mWindowDetachedWallpaper != detachedWallpaper) {
456            if (WindowManagerService.DEBUG_WALLPAPER) Slog.v(TAG,
457                    "Detached wallpaper changed from " + mWindowDetachedWallpaper
458                    + " to " + detachedWallpaper);
459            mWindowDetachedWallpaper = detachedWallpaper;
460            mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
461        }
462    }
463
464    /** See if any windows have been drawn, so they (and others associated with them) can now be
465     *  shown. */
466    private void testTokenMayBeDrawnLocked(int displayId) {
467        // See if any windows have been drawn, so they (and others
468        // associated with them) can now be shown.
469        final ArrayList<Task> tasks = mService.getDisplayContentLocked(displayId).getTasks();
470        final int numTasks = tasks.size();
471        for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
472            final AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
473            final int numTokens = tokens.size();
474            for (int tokenNdx = 0; tokenNdx < numTokens; ++tokenNdx) {
475                final AppWindowToken wtoken = tokens.get(tokenNdx);
476                AppWindowAnimator appAnimator = wtoken.mAppAnimator;
477                final boolean allDrawn = wtoken.allDrawn;
478                if (allDrawn != appAnimator.allDrawn) {
479                    appAnimator.allDrawn = allDrawn;
480                    if (allDrawn) {
481                        // The token has now changed state to having all
482                        // windows shown...  what to do, what to do?
483                        if (appAnimator.freezingScreen) {
484                            appAnimator.showAllWindowsLocked();
485                            mService.unsetAppFreezingScreenLocked(wtoken, false, true);
486                            if (WindowManagerService.DEBUG_ORIENTATION) Slog.i(TAG,
487                                    "Setting mOrientationChangeComplete=true because wtoken "
488                                    + wtoken + " numInteresting=" + wtoken.numInterestingWindows
489                                    + " numDrawn=" + wtoken.numDrawnWindows);
490                            // This will set mOrientationChangeComplete and cause a pass through layout.
491                            setAppLayoutChanges(appAnimator,
492                                    WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER,
493                                    "testTokenMayBeDrawnLocked: freezingScreen");
494                        } else {
495                            setAppLayoutChanges(appAnimator,
496                                    WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM,
497                                    "testTokenMayBeDrawnLocked");
498
499                            // We can now show all of the drawn windows!
500                            if (!mService.mOpeningApps.contains(wtoken)) {
501                                mAnimating |= appAnimator.showAllWindowsLocked();
502                            }
503                        }
504                    }
505                }
506            }
507        }
508    }
509
510
511    /** Locked on mService.mWindowMap. */
512    private void animateLocked() {
513        if (!mInitialized) {
514            return;
515        }
516
517        mCurrentTime = SystemClock.uptimeMillis();
518        mBulkUpdateParams = SET_ORIENTATION_CHANGE_COMPLETE;
519        boolean wasAnimating = mAnimating;
520        mAnimating = false;
521        if (WindowManagerService.DEBUG_WINDOW_TRACE) {
522            Slog.i(TAG, "!!! animate: entry time=" + mCurrentTime);
523        }
524
525        if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
526                TAG, ">>> OPEN TRANSACTION animateLocked");
527        SurfaceControl.openTransaction();
528        SurfaceControl.setAnimationTransaction();
529        try {
530            final int numDisplays = mDisplayContentsAnimators.size();
531            for (int i = 0; i < numDisplays; i++) {
532                final int displayId = mDisplayContentsAnimators.keyAt(i);
533                updateAppWindowsLocked(displayId);
534                DisplayContentsAnimator displayAnimator = mDisplayContentsAnimators.valueAt(i);
535
536                final ScreenRotationAnimation screenRotationAnimation =
537                        displayAnimator.mScreenRotationAnimation;
538                if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
539                    if (screenRotationAnimation.stepAnimationLocked(mCurrentTime)) {
540                        mAnimating = true;
541                    } else {
542                        mBulkUpdateParams |= SET_UPDATE_ROTATION;
543                        screenRotationAnimation.kill();
544                        displayAnimator.mScreenRotationAnimation = null;
545                    }
546                }
547
548                // Update animations of all applications, including those
549                // associated with exiting/removed apps
550                updateWindowsLocked(displayId);
551                updateWallpaperLocked(displayId);
552
553                final WindowList windows = mService.getWindowListLocked(displayId);
554                final int N = windows.size();
555                for (int j = 0; j < N; j++) {
556                    windows.get(j).mWinAnimator.prepareSurfaceLocked(true);
557                }
558            }
559
560            for (int i = 0; i < numDisplays; i++) {
561                final int displayId = mDisplayContentsAnimators.keyAt(i);
562
563                testTokenMayBeDrawnLocked(displayId);
564
565                final ScreenRotationAnimation screenRotationAnimation =
566                        mDisplayContentsAnimators.valueAt(i).mScreenRotationAnimation;
567                if (screenRotationAnimation != null) {
568                    screenRotationAnimation.updateSurfacesInTransaction();
569                }
570
571                mAnimating |= mService.getDisplayContentLocked(displayId).animateDimLayers();
572
573                //TODO (multidisplay): Magnification is supported only for the default display.
574                if (mService.mAccessibilityController != null
575                        && displayId == Display.DEFAULT_DISPLAY) {
576                    mService.mAccessibilityController.drawMagnifiedRegionBorderIfNeededLocked();
577                }
578            }
579
580            if (mAnimating) {
581                mService.scheduleAnimationLocked();
582            }
583
584            mService.setFocusedStackLayer();
585
586            if (mService.mWatermark != null) {
587                mService.mWatermark.drawIfNeeded();
588            }
589        } catch (RuntimeException e) {
590            Log.wtf(TAG, "Unhandled exception in Window Manager", e);
591        } finally {
592            SurfaceControl.closeTransaction();
593            if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
594                    TAG, "<<< CLOSE TRANSACTION animateLocked");
595        }
596
597        boolean hasPendingLayoutChanges = false;
598        final int numDisplays = mService.mDisplayContents.size();
599        for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
600            final DisplayContent displayContent = mService.mDisplayContents.valueAt(displayNdx);
601            final int pendingChanges = getPendingLayoutChanges(displayContent.getDisplayId());
602            if ((pendingChanges & WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
603                mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
604            }
605            if (pendingChanges != 0) {
606                hasPendingLayoutChanges = true;
607            }
608        }
609
610        boolean doRequest = false;
611        if (mBulkUpdateParams != 0) {
612            doRequest = mService.copyAnimToLayoutParamsLocked();
613        }
614
615        if (hasPendingLayoutChanges || doRequest) {
616            mService.requestTraversalLocked();
617        }
618
619        if (!mAnimating && wasAnimating) {
620            mService.requestTraversalLocked();
621        }
622        if (WindowManagerService.DEBUG_WINDOW_TRACE) {
623            Slog.i(TAG, "!!! animate: exit mAnimating=" + mAnimating
624                + " mBulkUpdateParams=" + Integer.toHexString(mBulkUpdateParams)
625                + " mPendingLayoutChanges(DEFAULT_DISPLAY)="
626                + Integer.toHexString(getPendingLayoutChanges(Display.DEFAULT_DISPLAY)));
627        }
628    }
629
630    static String bulkUpdateParamsToString(int bulkUpdateParams) {
631        StringBuilder builder = new StringBuilder(128);
632        if ((bulkUpdateParams & LayoutFields.SET_UPDATE_ROTATION) != 0) {
633            builder.append(" UPDATE_ROTATION");
634        }
635        if ((bulkUpdateParams & LayoutFields.SET_WALLPAPER_MAY_CHANGE) != 0) {
636            builder.append(" WALLPAPER_MAY_CHANGE");
637        }
638        if ((bulkUpdateParams & LayoutFields.SET_FORCE_HIDING_CHANGED) != 0) {
639            builder.append(" FORCE_HIDING_CHANGED");
640        }
641        if ((bulkUpdateParams & LayoutFields.SET_ORIENTATION_CHANGE_COMPLETE) != 0) {
642            builder.append(" ORIENTATION_CHANGE_COMPLETE");
643        }
644        if ((bulkUpdateParams & LayoutFields.SET_TURN_ON_SCREEN) != 0) {
645            builder.append(" TURN_ON_SCREEN");
646        }
647        return builder.toString();
648    }
649
650    public void dumpLocked(PrintWriter pw, String prefix, boolean dumpAll) {
651        final String subPrefix = "  " + prefix;
652        final String subSubPrefix = "  " + subPrefix;
653
654        for (int i = 0; i < mDisplayContentsAnimators.size(); i++) {
655            pw.print(prefix); pw.print("DisplayContentsAnimator #");
656                    pw.print(mDisplayContentsAnimators.keyAt(i));
657                    pw.println(":");
658            DisplayContentsAnimator displayAnimator = mDisplayContentsAnimators.valueAt(i);
659            final WindowList windows =
660                    mService.getWindowListLocked(mDisplayContentsAnimators.keyAt(i));
661            final int N = windows.size();
662            for (int j = 0; j < N; j++) {
663                WindowStateAnimator wanim = windows.get(j).mWinAnimator;
664                pw.print(subPrefix); pw.print("Window #"); pw.print(j);
665                        pw.print(": "); pw.println(wanim);
666            }
667            if (displayAnimator.mScreenRotationAnimation != null) {
668                pw.print(subPrefix); pw.println("mScreenRotationAnimation:");
669                displayAnimator.mScreenRotationAnimation.printTo(subSubPrefix, pw);
670            } else if (dumpAll) {
671                pw.print(subPrefix); pw.println("no ScreenRotationAnimation ");
672            }
673        }
674
675        pw.println();
676
677        if (dumpAll) {
678            pw.print(prefix); pw.print("mAnimTransactionSequence=");
679                    pw.print(mAnimTransactionSequence);
680                    pw.print(" mForceHiding="); pw.println(forceHidingToString());
681            pw.print(prefix); pw.print("mCurrentTime=");
682                    pw.println(TimeUtils.formatUptime(mCurrentTime));
683        }
684        if (mBulkUpdateParams != 0) {
685            pw.print(prefix); pw.print("mBulkUpdateParams=0x");
686                    pw.print(Integer.toHexString(mBulkUpdateParams));
687                    pw.println(bulkUpdateParamsToString(mBulkUpdateParams));
688        }
689        if (mWindowDetachedWallpaper != null) {
690            pw.print(prefix); pw.print("mWindowDetachedWallpaper=");
691                pw.println(mWindowDetachedWallpaper);
692        }
693        if (mUniverseBackground != null) {
694            pw.print(prefix); pw.print("mUniverseBackground="); pw.print(mUniverseBackground);
695                    pw.print(" mAboveUniverseLayer="); pw.println(mAboveUniverseLayer);
696        }
697    }
698
699    int getPendingLayoutChanges(final int displayId) {
700        if (displayId < 0) {
701            return 0;
702        }
703        return mService.getDisplayContentLocked(displayId).pendingLayoutChanges;
704    }
705
706    void setPendingLayoutChanges(final int displayId, final int changes) {
707        if (displayId >= 0) {
708            mService.getDisplayContentLocked(displayId).pendingLayoutChanges |= changes;
709        }
710    }
711
712    void setAppLayoutChanges(final AppWindowAnimator appAnimator, final int changes, String s) {
713        // Used to track which displays layout changes have been done.
714        SparseIntArray displays = new SparseIntArray(2);
715        WindowList windows = appAnimator.mAppToken.allAppWindows;
716        for (int i = windows.size() - 1; i >= 0; i--) {
717            final int displayId = windows.get(i).getDisplayId();
718            if (displayId >= 0 && displays.indexOfKey(displayId) < 0) {
719                setPendingLayoutChanges(displayId, changes);
720                if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
721                    mService.debugLayoutRepeats(s, getPendingLayoutChanges(displayId));
722                }
723                // Keep from processing this display again.
724                displays.put(displayId, changes);
725            }
726        }
727    }
728
729    private DisplayContentsAnimator getDisplayContentsAnimatorLocked(int displayId) {
730        DisplayContentsAnimator displayAnimator = mDisplayContentsAnimators.get(displayId);
731        if (displayAnimator == null) {
732            displayAnimator = new DisplayContentsAnimator();
733            mDisplayContentsAnimators.put(displayId, displayAnimator);
734        }
735        return displayAnimator;
736    }
737
738    void setScreenRotationAnimationLocked(int displayId, ScreenRotationAnimation animation) {
739        if (displayId >= 0) {
740            getDisplayContentsAnimatorLocked(displayId).mScreenRotationAnimation = animation;
741        }
742    }
743
744    ScreenRotationAnimation getScreenRotationAnimationLocked(int displayId) {
745        if (displayId < 0) {
746            return null;
747        }
748        return getDisplayContentsAnimatorLocked(displayId).mScreenRotationAnimation;
749    }
750
751    private class DisplayContentsAnimator {
752        ScreenRotationAnimation mScreenRotationAnimation = null;
753    }
754}
755