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