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