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