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