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