blob: 3cccf1d534f239d8ec431f3baa6551b36cb2fd9b [file] [log] [blame]
Craig Mautner59431632012-04-04 11:56:44 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2
3package com.android.server.wm;
4
5import android.graphics.Matrix;
6import android.util.Slog;
Dianne Hackborna57c6952013-03-29 14:46:40 -07007import android.util.TimeUtils;
Craig Mautnera91f9e22012-09-14 16:22:08 -07008import android.view.Display;
Mathias Agopian3866f0d2013-02-11 22:08:48 -08009import android.view.SurfaceControl;
Craig Mautner59431632012-04-04 11:56:44 -070010import android.view.WindowManagerPolicy;
11import android.view.animation.Animation;
12import android.view.animation.Transformation;
13
14import java.io.PrintWriter;
Craig Mautner322e4032012-07-13 13:35:20 -070015import java.util.ArrayList;
Craig Mautner59431632012-04-04 11:56:44 -070016
Craig Mautner59431632012-04-04 11:56:44 -070017public class AppWindowAnimator {
Craig Mautnerfbf378c2012-04-23 17:24:21 -070018 static final String TAG = "AppWindowAnimator";
Craig Mautner59431632012-04-04 11:56:44 -070019
20 final AppWindowToken mAppToken;
21 final WindowManagerService mService;
22 final WindowAnimator mAnimator;
23
24 boolean animating;
25 Animation animation;
Craig Mautner59431632012-04-04 11:56:44 -070026 boolean hasTransformation;
27 final Transformation transformation = new Transformation();
28
29 // Have we been asked to have this token keep the screen frozen?
30 // Protect with mAnimator.
31 boolean freezingScreen;
32
Dianne Hackborna57c6952013-03-29 14:46:40 -070033 /**
34 * How long we last kept the screen frozen.
35 */
36 int lastFreezeDuration;
37
Craig Mautner59431632012-04-04 11:56:44 -070038 // Offset to the window of all layers in the token, for use by
39 // AppWindowToken animations.
40 int animLayerAdjustment;
41
Craig Mautner6fbda632012-07-03 09:26:39 -070042 // Propagated from AppWindowToken.allDrawn, to determine when
43 // the state changes.
44 boolean allDrawn;
45
Craig Mautner59431632012-04-04 11:56:44 -070046 // Special surface for thumbnail animation.
Mathias Agopian3866f0d2013-02-11 22:08:48 -080047 SurfaceControl thumbnail;
Craig Mautner59431632012-04-04 11:56:44 -070048 int thumbnailTransactionSeq;
49 int thumbnailX;
50 int thumbnailY;
51 int thumbnailLayer;
52 Animation thumbnailAnimation;
53 final Transformation thumbnailTransformation = new Transformation();
54
Craig Mautner322e4032012-07-13 13:35:20 -070055 /** WindowStateAnimator from mAppAnimator.allAppWindows as of last performLayout */
Craig Mautnerbea12bd2012-08-20 10:18:34 -070056 ArrayList<WindowStateAnimator> mAllAppWinAnimators = new ArrayList<WindowStateAnimator>();
Craig Mautner322e4032012-07-13 13:35:20 -070057
Craig Mautnerfbf378c2012-04-23 17:24:21 -070058 static final Animation sDummyAnimation = new DummyAnimation();
59
Craig Mautner322e4032012-07-13 13:35:20 -070060 public AppWindowAnimator(final AppWindowToken atoken) {
Craig Mautner59431632012-04-04 11:56:44 -070061 mAppToken = atoken;
Craig Mautner322e4032012-07-13 13:35:20 -070062 mService = atoken.service;
63 mAnimator = atoken.mAnimator;
Craig Mautner59431632012-04-04 11:56:44 -070064 }
65
Craig Mautner9339c402012-11-30 11:23:56 -080066 public void setAnimation(Animation anim, int width, int height) {
Craig Mautner72669d12012-12-18 17:23:54 -080067 if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting animation in " + mAppToken
68 + ": " + anim + " wxh=" + width + "x" + height
69 + " isVisible=" + mAppToken.isVisible());
Craig Mautner59431632012-04-04 11:56:44 -070070 animation = anim;
71 animating = false;
Craig Mautner9339c402012-11-30 11:23:56 -080072 if (!anim.isInitialized()) {
73 anim.initialize(width, height, width, height);
74 }
Craig Mautner59431632012-04-04 11:56:44 -070075 anim.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
76 anim.scaleCurrentDuration(mService.mTransitionAnimationScale);
77 int zorder = anim.getZAdjustment();
78 int adj = 0;
79 if (zorder == Animation.ZORDER_TOP) {
80 adj = WindowManagerService.TYPE_LAYER_OFFSET;
81 } else if (zorder == Animation.ZORDER_BOTTOM) {
82 adj = -WindowManagerService.TYPE_LAYER_OFFSET;
83 }
84
85 if (animLayerAdjustment != adj) {
86 animLayerAdjustment = adj;
87 updateLayers();
88 }
89 // Start out animation gone if window is gone, or visible if window is visible.
90 transformation.clear();
Craig Mautner72669d12012-12-18 17:23:54 -080091 transformation.setAlpha(mAppToken.isVisible() ? 1 : 0);
Craig Mautner59431632012-04-04 11:56:44 -070092 hasTransformation = true;
93 }
94
95 public void setDummyAnimation() {
Craig Mautner72669d12012-12-18 17:23:54 -080096 if (WindowManagerService.localLOGV) Slog.v(TAG, "Setting dummy animation in " + mAppToken
97 + " isVisible=" + mAppToken.isVisible());
Craig Mautner1d961d42012-05-27 12:02:11 -070098 animation = sDummyAnimation;
Craig Mautner94ef9df2012-05-02 17:08:39 -070099 hasTransformation = true;
100 transformation.clear();
Craig Mautner72669d12012-12-18 17:23:54 -0800101 transformation.setAlpha(mAppToken.isVisible() ? 1 : 0);
Craig Mautner59431632012-04-04 11:56:44 -0700102 }
103
104 public void clearAnimation() {
105 if (animation != null) {
106 animation = null;
107 animating = true;
Craig Mautner59431632012-04-04 11:56:44 -0700108 }
109 clearThumbnail();
Craig Mautner7636dfb2012-11-16 15:24:11 -0800110 if (mAppToken.deferClearAllDrawn) {
111 mAppToken.allDrawn = false;
112 mAppToken.deferClearAllDrawn = false;
113 }
Craig Mautner59431632012-04-04 11:56:44 -0700114 }
115
116 public void clearThumbnail() {
117 if (thumbnail != null) {
118 thumbnail.destroy();
119 thumbnail = null;
120 }
121 }
122
123 void updateLayers() {
124 final int N = mAppToken.allAppWindows.size();
125 final int adj = animLayerAdjustment;
126 thumbnailLayer = -1;
127 for (int i=0; i<N; i++) {
128 final WindowState w = mAppToken.allAppWindows.get(i);
129 final WindowStateAnimator winAnimator = w.mWinAnimator;
130 winAnimator.mAnimLayer = w.mLayer + adj;
131 if (winAnimator.mAnimLayer > thumbnailLayer) {
132 thumbnailLayer = winAnimator.mAnimLayer;
133 }
Craig Mautnerfbf378c2012-04-23 17:24:21 -0700134 if (WindowManagerService.DEBUG_LAYERS) Slog.v(TAG, "Updating layer " + w + ": "
Craig Mautner59431632012-04-04 11:56:44 -0700135 + winAnimator.mAnimLayer);
136 if (w == mService.mInputMethodTarget && !mService.mInputMethodTargetWaitingAnim) {
137 mService.setInputMethodAnimLayerAdjustment(adj);
138 }
Craig Mautner96868332012-12-04 14:29:11 -0800139 if (w == mService.mWallpaperTarget && mService.mLowerWallpaperTarget == null) {
Craig Mautner59431632012-04-04 11:56:44 -0700140 mService.setWallpaperAnimLayerAdjustmentLocked(adj);
141 }
142 }
143 }
144
145 private void stepThumbnailAnimation(long currentTime) {
146 thumbnailTransformation.clear();
147 thumbnailAnimation.getTransformation(currentTime, thumbnailTransformation);
148 thumbnailTransformation.getMatrix().preTranslate(thumbnailX, thumbnailY);
Craig Mautnera91f9e22012-09-14 16:22:08 -0700149
150 ScreenRotationAnimation screenRotationAnimation =
151 mAnimator.getScreenRotationAnimationLocked(Display.DEFAULT_DISPLAY);
152 final boolean screenAnimation = screenRotationAnimation != null
153 && screenRotationAnimation.isAnimating();
Craig Mautner59431632012-04-04 11:56:44 -0700154 if (screenAnimation) {
Craig Mautnera91f9e22012-09-14 16:22:08 -0700155 thumbnailTransformation.postCompose(screenRotationAnimation.getEnterTransformation());
Craig Mautner59431632012-04-04 11:56:44 -0700156 }
157 // cache often used attributes locally
158 final float tmpFloats[] = mService.mTmpFloats;
159 thumbnailTransformation.getMatrix().getValues(tmpFloats);
160 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail,
161 "thumbnail", "POS " + tmpFloats[Matrix.MTRANS_X]
162 + ", " + tmpFloats[Matrix.MTRANS_Y], null);
163 thumbnail.setPosition(tmpFloats[Matrix.MTRANS_X], tmpFloats[Matrix.MTRANS_Y]);
164 if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(thumbnail,
165 "thumbnail", "alpha=" + thumbnailTransformation.getAlpha()
166 + " layer=" + thumbnailLayer
167 + " matrix=[" + tmpFloats[Matrix.MSCALE_X]
168 + "," + tmpFloats[Matrix.MSKEW_Y]
169 + "][" + tmpFloats[Matrix.MSKEW_X]
170 + "," + tmpFloats[Matrix.MSCALE_Y] + "]", null);
171 thumbnail.setAlpha(thumbnailTransformation.getAlpha());
172 // The thumbnail is layered below the window immediately above this
173 // token's anim layer.
174 thumbnail.setLayer(thumbnailLayer + WindowManagerService.WINDOW_LAYER_MULTIPLIER
175 - WindowManagerService.LAYER_OFFSET_THUMBNAIL);
176 thumbnail.setMatrix(tmpFloats[Matrix.MSCALE_X], tmpFloats[Matrix.MSKEW_Y],
177 tmpFloats[Matrix.MSKEW_X], tmpFloats[Matrix.MSCALE_Y]);
178 }
179
180 private boolean stepAnimation(long currentTime) {
181 if (animation == null) {
182 return false;
183 }
184 transformation.clear();
185 final boolean more = animation.getTransformation(currentTime, transformation);
Craig Mautner9e809442012-06-22 17:13:04 -0700186 if (false && WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautner8a197a42012-04-24 16:59:36 -0700187 TAG, "Stepped animation in " + mAppToken + ": more=" + more + ", xform=" + transformation);
Craig Mautner59431632012-04-04 11:56:44 -0700188 if (!more) {
189 animation = null;
190 clearThumbnail();
191 if (WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautner8a197a42012-04-24 16:59:36 -0700192 TAG, "Finished animation in " + mAppToken + " @ " + currentTime);
Craig Mautner59431632012-04-04 11:56:44 -0700193 }
194 hasTransformation = more;
195 return more;
196 }
197
198 // This must be called while inside a transaction.
Craig Mautner9339c402012-11-30 11:23:56 -0800199 boolean stepAnimationLocked(long currentTime) {
Craig Mautner59431632012-04-04 11:56:44 -0700200 if (mService.okToDisplay()) {
201 // We will run animations as long as the display isn't frozen.
202
Craig Mautnerfbf378c2012-04-23 17:24:21 -0700203 if (animation == sDummyAnimation) {
Craig Mautner59431632012-04-04 11:56:44 -0700204 // This guy is going to animate, but not yet. For now count
205 // it as not animating for purposes of scheduling transactions;
206 // when it is really time to animate, this will be set to
207 // a real animation and the next call will execute normally.
208 return false;
209 }
210
211 if ((mAppToken.allDrawn || animating || mAppToken.startingDisplayed)
212 && animation != null) {
213 if (!animating) {
214 if (WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautner8a197a42012-04-24 16:59:36 -0700215 TAG, "Starting animation in " + mAppToken +
Craig Mautner9339c402012-11-30 11:23:56 -0800216 " @ " + currentTime + " scale=" + mService.mTransitionAnimationScale
Craig Mautner59431632012-04-04 11:56:44 -0700217 + " allDrawn=" + mAppToken.allDrawn + " animating=" + animating);
Craig Mautner59431632012-04-04 11:56:44 -0700218 animation.setStartTime(currentTime);
219 animating = true;
220 if (thumbnail != null) {
221 thumbnail.show();
222 thumbnailAnimation.setStartTime(currentTime);
223 }
224 }
225 if (stepAnimation(currentTime)) {
226 // animation isn't over, step any thumbnail and that's
227 // it for now.
228 if (thumbnail != null) {
229 stepThumbnailAnimation(currentTime);
230 }
231 return true;
232 }
233 }
234 } else if (animation != null) {
235 // If the display is frozen, and there is a pending animation,
236 // clear it and make sure we run the cleanup code.
237 animating = true;
238 animation = null;
239 }
240
241 hasTransformation = false;
242
Craig Mautner3de422f2012-04-06 18:04:13 -0700243 if (!animating && animation == null) {
Craig Mautner59431632012-04-04 11:56:44 -0700244 return false;
245 }
246
Craig Mautner76a71652012-09-03 23:23:58 -0700247 mAnimator.setAppLayoutChanges(this, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM,
248 "AppWindowToken");
Craig Mautner59431632012-04-04 11:56:44 -0700249
250 clearAnimation();
251 animating = false;
252 if (animLayerAdjustment != 0) {
253 animLayerAdjustment = 0;
254 updateLayers();
255 }
256 if (mService.mInputMethodTarget != null
257 && mService.mInputMethodTarget.mAppToken == mAppToken) {
258 mService.moveInputMethodWindowsIfNeededLocked(true);
259 }
260
261 if (WindowManagerService.DEBUG_ANIM) Slog.v(
Craig Mautner8a197a42012-04-24 16:59:36 -0700262 TAG, "Animation done in " + mAppToken
Craig Mautner59431632012-04-04 11:56:44 -0700263 + ": reportedVisible=" + mAppToken.reportedVisible);
264
265 transformation.clear();
266
Craig Mautner322e4032012-07-13 13:35:20 -0700267 final int N = mAllAppWinAnimators.size();
Craig Mautner59431632012-04-04 11:56:44 -0700268 for (int i=0; i<N; i++) {
Craig Mautner322e4032012-07-13 13:35:20 -0700269 mAllAppWinAnimators.get(i).finishExit();
Craig Mautner59431632012-04-04 11:56:44 -0700270 }
271 mAppToken.updateReportedVisibilityLocked();
272
273 return false;
274 }
275
Craig Mautnerbec53f72012-04-05 11:49:05 -0700276 boolean showAllWindowsLocked() {
277 boolean isAnimating = false;
Craig Mautner322e4032012-07-13 13:35:20 -0700278 final int NW = mAllAppWinAnimators.size();
Craig Mautnerbec53f72012-04-05 11:49:05 -0700279 for (int i=0; i<NW; i++) {
Craig Mautner322e4032012-07-13 13:35:20 -0700280 WindowStateAnimator winAnimator = mAllAppWinAnimators.get(i);
Craig Mautnerfbf378c2012-04-23 17:24:21 -0700281 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG,
Craig Mautnerbec53f72012-04-05 11:49:05 -0700282 "performing show on: " + winAnimator);
283 winAnimator.performShowLocked();
284 isAnimating |= winAnimator.isAnimating();
285 }
286 return isAnimating;
287 }
288
Dianne Hackborn529e7442012-11-01 14:22:28 -0700289 void dump(PrintWriter pw, String prefix, boolean dumpAll) {
290 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
291 pw.print(prefix); pw.print("mAnimator="); pw.println(mAnimator);
292 pw.print(prefix); pw.print("freezingScreen="); pw.print(freezingScreen);
293 pw.print(" allDrawn="); pw.print(allDrawn);
294 pw.print(" animLayerAdjustment="); pw.println(animLayerAdjustment);
Dianne Hackborna57c6952013-03-29 14:46:40 -0700295 if (lastFreezeDuration != 0) {
296 pw.print(prefix); pw.print("lastFreezeDuration=");
297 TimeUtils.formatDuration(lastFreezeDuration, pw); pw.println();
298 }
Craig Mautner59431632012-04-04 11:56:44 -0700299 if (animating || animation != null) {
Craig Mautner9339c402012-11-30 11:23:56 -0800300 pw.print(prefix); pw.print("animating="); pw.println(animating);
Dianne Hackborn529e7442012-11-01 14:22:28 -0700301 pw.print(prefix); pw.print("animation="); pw.println(animation);
Craig Mautner59431632012-04-04 11:56:44 -0700302 }
303 if (hasTransformation) {
304 pw.print(prefix); pw.print("XForm: ");
305 transformation.printShortString(pw);
306 pw.println();
307 }
Craig Mautner59431632012-04-04 11:56:44 -0700308 if (thumbnail != null) {
309 pw.print(prefix); pw.print("thumbnail="); pw.print(thumbnail);
310 pw.print(" x="); pw.print(thumbnailX);
311 pw.print(" y="); pw.print(thumbnailY);
312 pw.print(" layer="); pw.println(thumbnailLayer);
313 pw.print(prefix); pw.print("thumbnailAnimation="); pw.println(thumbnailAnimation);
314 pw.print(prefix); pw.print("thumbnailTransformation=");
315 pw.println(thumbnailTransformation.toShortString());
316 }
Dianne Hackborn529e7442012-11-01 14:22:28 -0700317 for (int i=0; i<mAllAppWinAnimators.size(); i++) {
318 WindowStateAnimator wanim = mAllAppWinAnimators.get(i);
319 pw.print(prefix); pw.print("App Win Anim #"); pw.print(i);
320 pw.print(": "); pw.println(wanim);
321 }
Craig Mautner59431632012-04-04 11:56:44 -0700322 }
Craig Mautnerfbf378c2012-04-23 17:24:21 -0700323
324 // This is an animation that does nothing: it just immediately finishes
325 // itself every time it is called. It is used as a stub animation in cases
326 // where we want to synchronize multiple things that may be animating.
327 static final class DummyAnimation extends Animation {
328 @Override
329 public boolean getTransformation(long currentTime, Transformation outTransformation) {
330 return false;
331 }
332 }
333
Craig Mautner59431632012-04-04 11:56:44 -0700334}