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