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