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