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