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