WindowAnimator.java revision 99b69286f1e22575aa4807d63f01662477baedd5
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        for (int i = windows.size() - 1; i >= 0; i--) {
256            WindowState win = windows.get(i);
257            WindowStateAnimator winAnimator = win.mWinAnimator;
258            final int flags = win.mAttrs.flags;
259
260            if (winAnimator.mSurfaceControl != null) {
261                final boolean wasAnimating = winAnimator.mWasAnimating;
262                final boolean nowAnimating = winAnimator.stepAnimationLocked(mCurrentTime);
263
264                if (WindowManagerService.DEBUG_WALLPAPER) {
265                    Slog.v(TAG, win + ": wasAnimating=" + wasAnimating +
266                            ", nowAnimating=" + nowAnimating);
267                }
268
269                if (wasAnimating && !winAnimator.mAnimating && mService.mWallpaperTarget == win) {
270                    mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
271                    setPendingLayoutChanges(Display.DEFAULT_DISPLAY,
272                            WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
273                    if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
274                        mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 2",
275                                getPendingLayoutChanges(Display.DEFAULT_DISPLAY));
276                    }
277                }
278
279                if (mPolicy.isForceHiding(win.mAttrs)) {
280                    if (!wasAnimating && nowAnimating) {
281                        if (WindowManagerService.DEBUG_ANIM ||
282                                WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG,
283                                "Animation started that could impact force hide: " + win);
284                        mBulkUpdateParams |= SET_FORCE_HIDING_CHANGED;
285                        setPendingLayoutChanges(displayId,
286                                WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
287                        if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
288                            mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 3",
289                                    getPendingLayoutChanges(displayId));
290                        }
291                        mService.mFocusMayChange = true;
292                    } else if (mKeyguardGoingAway && !nowAnimating) {
293                        // Timeout!!
294                        Slog.e(TAG, "Timeout waiting for animation to startup");
295                        mPolicy.startKeyguardExitAnimation(0, 0);
296                        mKeyguardGoingAway = false;
297                    }
298                    if (win.isReadyForDisplay()) {
299                        if (nowAnimating) {
300                            if (winAnimator.mAnimationIsEntrance) {
301                                mForceHiding = KEYGUARD_ANIMATING_IN;
302                            } else {
303                                mForceHiding = KEYGUARD_ANIMATING_OUT;
304                            }
305                        } else {
306                            mForceHiding = win.isDrawnLw() ? KEYGUARD_SHOWN : KEYGUARD_NOT_SHOWN;
307                        }
308                    }
309                    if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG,
310                            "Force hide " + forceHidingToString()
311                            + " hasSurface=" + win.mHasSurface
312                            + " policyVis=" + win.mPolicyVisibility
313                            + " destroying=" + win.mDestroying
314                            + " attHidden=" + win.mAttachedHidden
315                            + " vis=" + win.mViewVisibility
316                            + " hidden=" + win.mRootToken.hidden
317                            + " anim=" + win.mWinAnimator.mAnimation);
318                } else if (mPolicy.canBeForceHidden(win, win.mAttrs)) {
319                    final boolean hideWhenLocked = (flags & FLAG_SHOW_WHEN_LOCKED) == 0 &&
320                            !(win.mIsImWindow && showImeOverKeyguard);
321                    final boolean changed;
322                    if (((mForceHiding == KEYGUARD_ANIMATING_IN)
323                                && (!winAnimator.isAnimating() || hideWhenLocked))
324                            || ((mForceHiding == KEYGUARD_SHOWN) && hideWhenLocked)) {
325                        changed = win.hideLw(false, false);
326                        if (WindowManagerService.DEBUG_VISIBILITY && changed) Slog.v(TAG,
327                                "Now policy hidden: " + win);
328                    } else {
329                        changed = win.showLw(false, false);
330                        if (WindowManagerService.DEBUG_VISIBILITY && changed) Slog.v(TAG,
331                                "Now policy shown: " + win);
332                        if (changed) {
333                            if ((mBulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0
334                                    && win.isVisibleNow() /*w.isReadyForDisplay()*/) {
335                                if (unForceHiding == null) {
336                                    unForceHiding = new ArrayList<WindowStateAnimator>();
337                                }
338                                unForceHiding.add(winAnimator);
339                                if ((flags & FLAG_SHOW_WALLPAPER) != 0) {
340                                    wallpaperInUnForceHiding = true;
341                                }
342                            }
343                            final WindowState currentFocus = mService.mCurrentFocus;
344                            if (currentFocus == null || currentFocus.mLayer < win.mLayer) {
345                                // We are showing on to of the current
346                                // focus, so re-evaluate focus to make
347                                // sure it is correct.
348                                if (WindowManagerService.DEBUG_FOCUS_LIGHT) Slog.v(TAG,
349                                        "updateWindowsLocked: setting mFocusMayChange true");
350                                mService.mFocusMayChange = true;
351                            }
352                        }
353                    }
354                    if (changed && (flags & FLAG_SHOW_WALLPAPER) != 0) {
355                        mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
356                        setPendingLayoutChanges(Display.DEFAULT_DISPLAY,
357                                WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
358                        if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
359                            mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 4",
360                                    getPendingLayoutChanges(Display.DEFAULT_DISPLAY));
361                        }
362                    }
363                }
364            }
365
366            final AppWindowToken atoken = win.mAppToken;
367            if (winAnimator.mDrawState == WindowStateAnimator.READY_TO_SHOW) {
368                if (atoken == null || atoken.allDrawn) {
369                    if (winAnimator.performShowLocked()) {
370                        setPendingLayoutChanges(displayId,
371                                WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
372                        if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
373                            mService.debugLayoutRepeats("updateWindowsAndWallpaperLocked 5",
374                                    getPendingLayoutChanges(displayId));
375                        }
376                    }
377                }
378            }
379            final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
380            if (appAnimator != null && appAnimator.thumbnail != null) {
381                if (appAnimator.thumbnailTransactionSeq != mAnimTransactionSequence) {
382                    appAnimator.thumbnailTransactionSeq = mAnimTransactionSequence;
383                    appAnimator.thumbnailLayer = 0;
384                }
385                if (appAnimator.thumbnailLayer < winAnimator.mAnimLayer) {
386                    appAnimator.thumbnailLayer = winAnimator.mAnimLayer;
387                }
388            }
389            if (win.mIsWallpaper) {
390                wallpaper = win;
391            }
392        } // end forall windows
393
394        // If we have windows that are being show due to them no longer
395        // being force-hidden, apply the appropriate animation to them.
396        if (unForceHiding != null) {
397            boolean startKeyguardExit = true;
398            for (int i=unForceHiding.size()-1; i>=0; i--) {
399                Animation a = null;
400                if (!mKeyguardGoingAwayDisableWindowAnimations) {
401                    a = mPolicy.createForceHideEnterAnimation(wallpaperInUnForceHiding,
402                            mKeyguardGoingAwayToNotificationShade);
403                }
404                if (a != null) {
405                    final WindowStateAnimator winAnimator = unForceHiding.get(i);
406                    winAnimator.setAnimation(a);
407                    winAnimator.keyguardGoingAwayAnimation = true;
408                    if (startKeyguardExit && mKeyguardGoingAway) {
409                        // Do one time only.
410                        mPolicy.startKeyguardExitAnimation(mCurrentTime + a.getStartOffset(),
411                                a.getDuration());
412                        mKeyguardGoingAway = false;
413                        startKeyguardExit = false;
414                    }
415                }
416            }
417
418            // Wallpaper is going away in un-force-hide motion, animate it as well.
419            if (!wallpaperInUnForceHiding && wallpaper != null
420                    && !mKeyguardGoingAwayDisableWindowAnimations) {
421                Animation a = mPolicy.createForceHideWallpaperExitAnimation(
422                        mKeyguardGoingAwayToNotificationShade);
423                if (a != null) {
424                    WindowStateAnimator animator = wallpaper.mWinAnimator;
425                    animator.setAnimation(a);
426                }
427            }
428        }
429    }
430
431    private void updateWallpaperLocked(int displayId) {
432        mService.getDisplayContentLocked(displayId).resetAnimationBackgroundAnimator();
433
434        final WindowList windows = mService.getWindowListLocked(displayId);
435        WindowState detachedWallpaper = null;
436
437        for (int i = windows.size() - 1; i >= 0; i--) {
438            final WindowState win = windows.get(i);
439            WindowStateAnimator winAnimator = win.mWinAnimator;
440            if (winAnimator.mSurfaceControl == null) {
441                continue;
442            }
443
444            final int flags = win.mAttrs.flags;
445
446            // If this window is animating, make a note that we have
447            // an animating window and take care of a request to run
448            // a detached wallpaper animation.
449            if (winAnimator.mAnimating) {
450                if (winAnimator.mAnimation != null) {
451                    if ((flags & FLAG_SHOW_WALLPAPER) != 0
452                            && winAnimator.mAnimation.getDetachWallpaper()) {
453                        detachedWallpaper = win;
454                    }
455                    final int color = winAnimator.mAnimation.getBackgroundColor();
456                    if (color != 0) {
457                        win.getStack().setAnimationBackground(winAnimator, color);
458                    }
459                }
460                mAnimating = true;
461            }
462
463            // If this window's app token is running a detached wallpaper
464            // animation, make a note so we can ensure the wallpaper is
465            // displayed behind it.
466            final AppWindowAnimator appAnimator = winAnimator.mAppAnimator;
467            if (appAnimator != null && appAnimator.animation != null
468                    && appAnimator.animating) {
469                if ((flags & FLAG_SHOW_WALLPAPER) != 0
470                        && appAnimator.animation.getDetachWallpaper()) {
471                    detachedWallpaper = win;
472                }
473
474                final int color = appAnimator.animation.getBackgroundColor();
475                if (color != 0) {
476                    win.getStack().setAnimationBackground(winAnimator, color);
477                }
478            }
479        } // end forall windows
480
481        if (mWindowDetachedWallpaper != detachedWallpaper) {
482            if (WindowManagerService.DEBUG_WALLPAPER) Slog.v(TAG,
483                    "Detached wallpaper changed from " + mWindowDetachedWallpaper
484                    + " to " + detachedWallpaper);
485            mWindowDetachedWallpaper = detachedWallpaper;
486            mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
487        }
488    }
489
490    /** See if any windows have been drawn, so they (and others associated with them) can now be
491     *  shown. */
492    private void testTokenMayBeDrawnLocked(int displayId) {
493        // See if any windows have been drawn, so they (and others
494        // associated with them) can now be shown.
495        final ArrayList<Task> tasks = mService.getDisplayContentLocked(displayId).getTasks();
496        final int numTasks = tasks.size();
497        for (int taskNdx = 0; taskNdx < numTasks; ++taskNdx) {
498            final AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
499            final int numTokens = tokens.size();
500            for (int tokenNdx = 0; tokenNdx < numTokens; ++tokenNdx) {
501                final AppWindowToken wtoken = tokens.get(tokenNdx);
502                AppWindowAnimator appAnimator = wtoken.mAppAnimator;
503                final boolean allDrawn = wtoken.allDrawn;
504                if (allDrawn != appAnimator.allDrawn) {
505                    appAnimator.allDrawn = allDrawn;
506                    if (allDrawn) {
507                        // The token has now changed state to having all
508                        // windows shown...  what to do, what to do?
509                        if (appAnimator.freezingScreen) {
510                            appAnimator.showAllWindowsLocked();
511                            mService.unsetAppFreezingScreenLocked(wtoken, false, true);
512                            if (WindowManagerService.DEBUG_ORIENTATION) Slog.i(TAG,
513                                    "Setting mOrientationChangeComplete=true because wtoken "
514                                    + wtoken + " numInteresting=" + wtoken.numInterestingWindows
515                                    + " numDrawn=" + wtoken.numDrawnWindows);
516                            // This will set mOrientationChangeComplete and cause a pass through layout.
517                            setAppLayoutChanges(appAnimator,
518                                    WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER,
519                                    "testTokenMayBeDrawnLocked: freezingScreen");
520                        } else {
521                            setAppLayoutChanges(appAnimator,
522                                    WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM,
523                                    "testTokenMayBeDrawnLocked");
524
525                            // We can now show all of the drawn windows!
526                            if (!mService.mOpeningApps.contains(wtoken)) {
527                                mAnimating |= appAnimator.showAllWindowsLocked();
528                            }
529                        }
530                    }
531                }
532            }
533        }
534    }
535
536
537    /** Locked on mService.mWindowMap. */
538    private void animateLocked() {
539        if (!mInitialized) {
540            return;
541        }
542
543        mCurrentTime = SystemClock.uptimeMillis();
544        mBulkUpdateParams = SET_ORIENTATION_CHANGE_COMPLETE;
545        boolean wasAnimating = mAnimating;
546        mAnimating = false;
547        if (WindowManagerService.DEBUG_WINDOW_TRACE) {
548            Slog.i(TAG, "!!! animate: entry time=" + mCurrentTime);
549        }
550
551        if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
552                TAG, ">>> OPEN TRANSACTION animateLocked");
553        SurfaceControl.openTransaction();
554        SurfaceControl.setAnimationTransaction();
555        try {
556            final int numDisplays = mDisplayContentsAnimators.size();
557            for (int i = 0; i < numDisplays; i++) {
558                final int displayId = mDisplayContentsAnimators.keyAt(i);
559                updateAppWindowsLocked(displayId);
560                DisplayContentsAnimator displayAnimator = mDisplayContentsAnimators.valueAt(i);
561
562                final ScreenRotationAnimation screenRotationAnimation =
563                        displayAnimator.mScreenRotationAnimation;
564                if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
565                    if (screenRotationAnimation.stepAnimationLocked(mCurrentTime)) {
566                        mAnimating = true;
567                    } else {
568                        mBulkUpdateParams |= SET_UPDATE_ROTATION;
569                        screenRotationAnimation.kill();
570                        displayAnimator.mScreenRotationAnimation = null;
571                    }
572                }
573
574                // Update animations of all applications, including those
575                // associated with exiting/removed apps
576                updateWindowsLocked(displayId);
577                updateWallpaperLocked(displayId);
578
579                final WindowList windows = mService.getWindowListLocked(displayId);
580                final int N = windows.size();
581                for (int j = 0; j < N; j++) {
582                    windows.get(j).mWinAnimator.prepareSurfaceLocked(true);
583                }
584            }
585
586            for (int i = 0; i < numDisplays; i++) {
587                final int displayId = mDisplayContentsAnimators.keyAt(i);
588
589                testTokenMayBeDrawnLocked(displayId);
590
591                final ScreenRotationAnimation screenRotationAnimation =
592                        mDisplayContentsAnimators.valueAt(i).mScreenRotationAnimation;
593                if (screenRotationAnimation != null) {
594                    screenRotationAnimation.updateSurfacesInTransaction();
595                }
596
597                mAnimating |= mService.getDisplayContentLocked(displayId).animateDimLayers();
598
599                //TODO (multidisplay): Magnification is supported only for the default display.
600                if (mService.mAccessibilityController != null
601                        && displayId == Display.DEFAULT_DISPLAY) {
602                    mService.mAccessibilityController.drawMagnifiedRegionBorderIfNeededLocked();
603                }
604            }
605
606            if (mAnimating) {
607                mService.scheduleAnimationLocked();
608            }
609
610            mService.setFocusedStackLayer();
611
612            if (mService.mWatermark != null) {
613                mService.mWatermark.drawIfNeeded();
614            }
615        } catch (RuntimeException e) {
616            Log.wtf(TAG, "Unhandled exception in Window Manager", e);
617        } finally {
618            SurfaceControl.closeTransaction();
619            if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(
620                    TAG, "<<< CLOSE TRANSACTION animateLocked");
621        }
622
623        boolean hasPendingLayoutChanges = false;
624        final int numDisplays = mService.mDisplayContents.size();
625        for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
626            final DisplayContent displayContent = mService.mDisplayContents.valueAt(displayNdx);
627            final int pendingChanges = getPendingLayoutChanges(displayContent.getDisplayId());
628            if ((pendingChanges & WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
629                mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
630            }
631            if (pendingChanges != 0) {
632                hasPendingLayoutChanges = true;
633            }
634        }
635
636        boolean doRequest = false;
637        if (mBulkUpdateParams != 0) {
638            doRequest = mService.copyAnimToLayoutParamsLocked();
639        }
640
641        if (hasPendingLayoutChanges || doRequest) {
642            mService.requestTraversalLocked();
643        }
644
645        if (!mAnimating && wasAnimating) {
646            mService.requestTraversalLocked();
647        }
648        if (WindowManagerService.DEBUG_WINDOW_TRACE) {
649            Slog.i(TAG, "!!! animate: exit mAnimating=" + mAnimating
650                + " mBulkUpdateParams=" + Integer.toHexString(mBulkUpdateParams)
651                + " mPendingLayoutChanges(DEFAULT_DISPLAY)="
652                + Integer.toHexString(getPendingLayoutChanges(Display.DEFAULT_DISPLAY)));
653        }
654    }
655
656    static String bulkUpdateParamsToString(int bulkUpdateParams) {
657        StringBuilder builder = new StringBuilder(128);
658        if ((bulkUpdateParams & LayoutFields.SET_UPDATE_ROTATION) != 0) {
659            builder.append(" UPDATE_ROTATION");
660        }
661        if ((bulkUpdateParams & LayoutFields.SET_WALLPAPER_MAY_CHANGE) != 0) {
662            builder.append(" WALLPAPER_MAY_CHANGE");
663        }
664        if ((bulkUpdateParams & LayoutFields.SET_FORCE_HIDING_CHANGED) != 0) {
665            builder.append(" FORCE_HIDING_CHANGED");
666        }
667        if ((bulkUpdateParams & LayoutFields.SET_ORIENTATION_CHANGE_COMPLETE) != 0) {
668            builder.append(" ORIENTATION_CHANGE_COMPLETE");
669        }
670        if ((bulkUpdateParams & LayoutFields.SET_TURN_ON_SCREEN) != 0) {
671            builder.append(" TURN_ON_SCREEN");
672        }
673        return builder.toString();
674    }
675
676    public void dumpLocked(PrintWriter pw, String prefix, boolean dumpAll) {
677        final String subPrefix = "  " + prefix;
678        final String subSubPrefix = "  " + subPrefix;
679
680        for (int i = 0; i < mDisplayContentsAnimators.size(); i++) {
681            pw.print(prefix); pw.print("DisplayContentsAnimator #");
682                    pw.print(mDisplayContentsAnimators.keyAt(i));
683                    pw.println(":");
684            DisplayContentsAnimator displayAnimator = mDisplayContentsAnimators.valueAt(i);
685            final WindowList windows =
686                    mService.getWindowListLocked(mDisplayContentsAnimators.keyAt(i));
687            final int N = windows.size();
688            for (int j = 0; j < N; j++) {
689                WindowStateAnimator wanim = windows.get(j).mWinAnimator;
690                pw.print(subPrefix); pw.print("Window #"); pw.print(j);
691                        pw.print(": "); pw.println(wanim);
692            }
693            if (displayAnimator.mScreenRotationAnimation != null) {
694                pw.print(subPrefix); pw.println("mScreenRotationAnimation:");
695                displayAnimator.mScreenRotationAnimation.printTo(subSubPrefix, pw);
696            } else if (dumpAll) {
697                pw.print(subPrefix); pw.println("no ScreenRotationAnimation ");
698            }
699        }
700
701        pw.println();
702
703        if (dumpAll) {
704            pw.print(prefix); pw.print("mAnimTransactionSequence=");
705                    pw.print(mAnimTransactionSequence);
706                    pw.print(" mForceHiding="); pw.println(forceHidingToString());
707            pw.print(prefix); pw.print("mCurrentTime=");
708                    pw.println(TimeUtils.formatUptime(mCurrentTime));
709        }
710        if (mBulkUpdateParams != 0) {
711            pw.print(prefix); pw.print("mBulkUpdateParams=0x");
712                    pw.print(Integer.toHexString(mBulkUpdateParams));
713                    pw.println(bulkUpdateParamsToString(mBulkUpdateParams));
714        }
715        if (mWindowDetachedWallpaper != null) {
716            pw.print(prefix); pw.print("mWindowDetachedWallpaper=");
717                pw.println(mWindowDetachedWallpaper);
718        }
719        if (mUniverseBackground != null) {
720            pw.print(prefix); pw.print("mUniverseBackground="); pw.print(mUniverseBackground);
721                    pw.print(" mAboveUniverseLayer="); pw.println(mAboveUniverseLayer);
722        }
723    }
724
725    int getPendingLayoutChanges(final int displayId) {
726        if (displayId < 0) {
727            return 0;
728        }
729        return mService.getDisplayContentLocked(displayId).pendingLayoutChanges;
730    }
731
732    void setPendingLayoutChanges(final int displayId, final int changes) {
733        if (displayId >= 0) {
734            mService.getDisplayContentLocked(displayId).pendingLayoutChanges |= changes;
735        }
736    }
737
738    void setAppLayoutChanges(final AppWindowAnimator appAnimator, final int changes, String s) {
739        // Used to track which displays layout changes have been done.
740        SparseIntArray displays = new SparseIntArray(2);
741        WindowList windows = appAnimator.mAppToken.allAppWindows;
742        for (int i = windows.size() - 1; i >= 0; i--) {
743            final int displayId = windows.get(i).getDisplayId();
744            if (displayId >= 0 && displays.indexOfKey(displayId) < 0) {
745                setPendingLayoutChanges(displayId, changes);
746                if (WindowManagerService.DEBUG_LAYOUT_REPEATS) {
747                    mService.debugLayoutRepeats(s, getPendingLayoutChanges(displayId));
748                }
749                // Keep from processing this display again.
750                displays.put(displayId, changes);
751            }
752        }
753    }
754
755    private DisplayContentsAnimator getDisplayContentsAnimatorLocked(int displayId) {
756        DisplayContentsAnimator displayAnimator = mDisplayContentsAnimators.get(displayId);
757        if (displayAnimator == null) {
758            displayAnimator = new DisplayContentsAnimator();
759            mDisplayContentsAnimators.put(displayId, displayAnimator);
760        }
761        return displayAnimator;
762    }
763
764    void setScreenRotationAnimationLocked(int displayId, ScreenRotationAnimation animation) {
765        if (displayId >= 0) {
766            getDisplayContentsAnimatorLocked(displayId).mScreenRotationAnimation = animation;
767        }
768    }
769
770    ScreenRotationAnimation getScreenRotationAnimationLocked(int displayId) {
771        if (displayId < 0) {
772            return null;
773        }
774        return getDisplayContentsAnimatorLocked(displayId).mScreenRotationAnimation;
775    }
776
777    private class DisplayContentsAnimator {
778        ScreenRotationAnimation mScreenRotationAnimation = null;
779    }
780}
781