blob: 9f0ed2100462422145123efbc6f4828ed5f15666 [file] [log] [blame]
Filip Gruszczynski84fa3352016-01-25 16:28:49 -08001/*
2 * Copyright (C) 2016 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
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080019import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
20import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
21import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
22
23import android.animation.Animator;
24import android.animation.ValueAnimator;
Winson Chungbaa7b722017-03-03 21:33:44 -080025import android.content.Context;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080026import android.graphics.Rect;
Wale Ogunwale2ba71292016-05-05 09:16:47 -070027import android.os.Handler;
Robert Carrf9aa2a92016-04-26 14:22:19 -070028import android.os.IBinder;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080029import android.os.Debug;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080030import android.util.ArrayMap;
31import android.util.Slog;
Winson Chungbaa7b722017-03-03 21:33:44 -080032import android.view.animation.AnimationUtils;
33import android.view.animation.Interpolator;
Robert Carrf9aa2a92016-04-26 14:22:19 -070034import android.view.WindowManagerInternal;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080035
36/**
37 * Enables animating bounds of objects.
38 *
39 * In multi-window world bounds of both stack and tasks can change. When we need these bounds to
40 * change smoothly and not require the app to relaunch (e.g. because it handles resizes and
41 * relaunching it would cause poorer experience), these class provides a way to directly animate
42 * the bounds of the resized object.
43 *
44 * The object that is resized needs to implement {@link AnimateBoundsUser} interface.
Wale Ogunwale2ba71292016-05-05 09:16:47 -070045 *
46 * NOTE: All calls to methods in this class should be done on the UI thread
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080047 */
48public class BoundsAnimationController {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080049 private static final boolean DEBUG_LOCAL = false;
50 private static final boolean DEBUG = DEBUG_LOCAL || DEBUG_ANIM;
51 private static final String TAG = TAG_WITH_CLASS_NAME || DEBUG_LOCAL
52 ? "BoundsAnimationController" : TAG_WM;
Wale Ogunwalece144522016-02-05 22:51:01 -080053 private static final int DEBUG_ANIMATION_SLOW_DOWN_FACTOR = 1;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080054
Winson Chungbaa7b722017-03-03 21:33:44 -080055 private static final int DEFAULT_TRANSITION_DURATION = 425;
56
Wale Ogunwalece144522016-02-05 22:51:01 -080057 // Only accessed on UI thread.
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080058 private ArrayMap<AnimateBoundsUser, BoundsAnimator> mRunningAnimations = new ArrayMap<>();
59
Wale Ogunwale2ba71292016-05-05 09:16:47 -070060 private final class AppTransitionNotifier
61 extends WindowManagerInternal.AppTransitionListener implements Runnable {
Robert Carrf9aa2a92016-04-26 14:22:19 -070062
Wale Ogunwale2ba71292016-05-05 09:16:47 -070063 public void onAppTransitionCancelledLocked() {
64 animationFinished();
65 }
66 public void onAppTransitionFinishedLocked(IBinder token) {
67 animationFinished();
68 }
69 private void animationFinished() {
70 if (mFinishAnimationAfterTransition) {
71 mHandler.removeCallbacks(this);
72 // This might end up calling into activity manager which will be bad since we have the
73 // window manager lock held at this point. Post a message to take care of the processing
74 // so we don't deadlock.
75 mHandler.post(this);
76 }
77 }
78
79 @Override
80 public void run() {
81 for (int i = 0; i < mRunningAnimations.size(); i++) {
82 final BoundsAnimator b = mRunningAnimations.valueAt(i);
83 b.onAnimationEnd(null);
84 }
85 }
86 }
87
88 private final Handler mHandler;
Robert Carrf9aa2a92016-04-26 14:22:19 -070089 private final AppTransition mAppTransition;
Wale Ogunwale2ba71292016-05-05 09:16:47 -070090 private final AppTransitionNotifier mAppTransitionNotifier = new AppTransitionNotifier();
Winson Chungbaa7b722017-03-03 21:33:44 -080091 private final Interpolator mFastOutSlowInInterpolator;
Robert Carrf9aa2a92016-04-26 14:22:19 -070092 private boolean mFinishAnimationAfterTransition = false;
93
Winson Chungbaa7b722017-03-03 21:33:44 -080094 BoundsAnimationController(Context context, AppTransition transition, Handler handler) {
Wale Ogunwale2ba71292016-05-05 09:16:47 -070095 mHandler = handler;
Robert Carrf9aa2a92016-04-26 14:22:19 -070096 mAppTransition = transition;
97 mAppTransition.registerListenerLocked(mAppTransitionNotifier);
Winson Chungbaa7b722017-03-03 21:33:44 -080098 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
99 com.android.internal.R.interpolator.fast_out_slow_in);
Robert Carrf9aa2a92016-04-26 14:22:19 -0700100 }
101
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800102 private final class BoundsAnimator extends ValueAnimator
103 implements ValueAnimator.AnimatorUpdateListener, ValueAnimator.AnimatorListener {
104 private final AnimateBoundsUser mTarget;
Winson Chung84a38342016-11-08 16:15:10 -0800105 private final Rect mFrom = new Rect();
106 private final Rect mTo = new Rect();
Robert Carr0d00c2e2016-02-29 17:45:02 -0800107 private final Rect mTmpRect = new Rect();
108 private final Rect mTmpTaskBounds = new Rect();
Filip Gruszczynskic17d8b72016-02-03 16:52:59 -0800109 private final boolean mMoveToFullScreen;
Wale Ogunwalece144522016-02-05 22:51:01 -0800110 // True if this this animation was cancelled and will be replaced the another animation from
111 // the same {@link #AnimateBoundsUser} target.
Winson Chung5af42fc2017-03-24 17:11:33 -0700112 private boolean mSkipAnimationEnd;
113 // True if this animation replaced a previous animation of the same
Wale Ogunwalece144522016-02-05 22:51:01 -0800114 // {@link #AnimateBoundsUser} target.
Winson Chung5af42fc2017-03-24 17:11:33 -0700115 private final boolean mSkipAnimationStart;
116 // True if this animation is not replacing a previous animation, or if the previous
117 // animation is animating to a different fullscreen state than the current animation.
118 // We use this to ensure that we always provide a consistent set/order of callbacks when we
119 // transition to/from PiP.
120 private final boolean mAnimatingToNewFullscreenState;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800121
Robert Carr0d00c2e2016-02-29 17:45:02 -0800122 // Depending on whether we are animating from
123 // a smaller to a larger size
124 private final int mFrozenTaskWidth;
125 private final int mFrozenTaskHeight;
126
Winson Chung08f81892017-03-02 15:40:51 -0800127 BoundsAnimator(AnimateBoundsUser target, Rect from, Rect to, boolean moveToFullScreen,
Winson Chung5af42fc2017-03-24 17:11:33 -0700128 boolean replacingExistingAnimation, boolean animatingToNewFullscreenState) {
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800129 super();
130 mTarget = target;
Winson Chung84a38342016-11-08 16:15:10 -0800131 mFrom.set(from);
132 mTo.set(to);
Filip Gruszczynskic17d8b72016-02-03 16:52:59 -0800133 mMoveToFullScreen = moveToFullScreen;
Winson Chung5af42fc2017-03-24 17:11:33 -0700134 mSkipAnimationStart = replacingExistingAnimation;
135 mAnimatingToNewFullscreenState = animatingToNewFullscreenState;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800136 addUpdateListener(this);
137 addListener(this);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800138
139 // If we are animating from smaller to larger, we want to change the task bounds
140 // to their final size immediately so we can use scaling to make the window
141 // larger. Likewise if we are going from bigger to smaller, we want to wait until
142 // the end so we don't have to upscale from the smaller finished size.
143 if (animatingToLargerSize()) {
144 mFrozenTaskWidth = mTo.width();
145 mFrozenTaskHeight = mTo.height();
146 } else {
147 mFrozenTaskWidth = mFrom.width();
148 mFrozenTaskHeight = mFrom.height();
149 }
150 }
151
Winson Chung5af42fc2017-03-24 17:11:33 -0700152 @Override
153 public void onAnimationStart(Animator animation) {
154 if (DEBUG) Slog.d(TAG, "onAnimationStart: mTarget=" + mTarget
155 + " mSkipAnimationStart=" + mSkipAnimationStart);
156 mFinishAnimationAfterTransition = false;
157 mTmpRect.set(mFrom.left, mFrom.top, mFrom.left + mFrozenTaskWidth,
158 mFrom.top + mFrozenTaskHeight);
159
160 // Ensure that we have prepared the target for animation before
161 // we trigger any size changes, so it can swap surfaces
162 // in to appropriate modes, or do as it wishes otherwise.
163 if (!mSkipAnimationStart) {
164 mTarget.onAnimationStart(mMoveToFullScreen);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800165 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700166
167 // If we are animating to a new fullscreen state (either to/from fullscreen), then
168 // notify the target of the change with the new frozen task bounds
169 if (mAnimatingToNewFullscreenState) {
170 mTarget.updatePictureInPictureMode(mMoveToFullScreen ? null : mTo);
171 }
172
173 // Immediately update the task bounds if they have to become larger, but preserve
174 // the starting position so we don't jump at the beginning of the animation.
175 if (animatingToLargerSize()) {
176 mTarget.setPinnedStackSize(mFrom, mTmpRect);
177 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800178 }
179
180 @Override
181 public void onAnimationUpdate(ValueAnimator animation) {
182 final float value = (Float) animation.getAnimatedValue();
183 final float remains = 1 - value;
Wale Ogunwalece144522016-02-05 22:51:01 -0800184 mTmpRect.left = (int) (mFrom.left * remains + mTo.left * value + 0.5f);
185 mTmpRect.top = (int) (mFrom.top * remains + mTo.top * value + 0.5f);
186 mTmpRect.right = (int) (mFrom.right * remains + mTo.right * value + 0.5f);
187 mTmpRect.bottom = (int) (mFrom.bottom * remains + mTo.bottom * value + 0.5f);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800188 if (DEBUG) Slog.d(TAG, "animateUpdate: mTarget=" + mTarget + " mBounds="
189 + mTmpRect + " from=" + mFrom + " mTo=" + mTo + " value=" + value
190 + " remains=" + remains);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800191
Robert Carrc7294602016-05-13 11:32:05 -0700192 mTmpTaskBounds.set(mTmpRect.left, mTmpRect.top,
193 mTmpRect.left + mFrozenTaskWidth, mTmpRect.top + mFrozenTaskHeight);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800194
Robert Carrc7294602016-05-13 11:32:05 -0700195 if (!mTarget.setPinnedStackSize(mTmpRect, mTmpTaskBounds)) {
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800196 // Whoops, the target doesn't feel like animating anymore. Let's immediately finish
197 // any further animation.
198 animation.cancel();
199 }
200 }
201
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800202 @Override
203 public void onAnimationEnd(Animator animation) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800204 if (DEBUG) Slog.d(TAG, "onAnimationEnd: mTarget=" + mTarget
Winson Chung5af42fc2017-03-24 17:11:33 -0700205 + " mMoveToFullScreen=" + mMoveToFullScreen
206 + " mSkipAnimationEnd=" + mSkipAnimationEnd);
Jaewan Kimb0033642016-04-22 18:41:37 +0900207
Robert Carrf9aa2a92016-04-26 14:22:19 -0700208 // There could be another animation running. For example in the
209 // move to fullscreen case, recents will also be closing while the
210 // previous task will be taking its place in the fullscreen stack.
211 // we have to ensure this is completed before we finish the animation
212 // and take our place in the fullscreen stack.
213 if (mAppTransition.isRunning() && !mFinishAnimationAfterTransition) {
214 mFinishAnimationAfterTransition = true;
215 return;
216 }
217
Robert Carrc7294602016-05-13 11:32:05 -0700218 finishAnimation();
219
220 mTarget.setPinnedStackSize(mTo, null);
Winson Chung5af42fc2017-03-24 17:11:33 -0700221 if (mMoveToFullScreen && !mSkipAnimationEnd) {
Filip Gruszczynskic17d8b72016-02-03 16:52:59 -0800222 mTarget.moveToFullscreen();
223 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800224 }
225
226 @Override
227 public void onAnimationCancel(Animator animation) {
228 finishAnimation();
229 }
230
Wale Ogunwalece144522016-02-05 22:51:01 -0800231 @Override
232 public void cancel() {
Winson Chung5af42fc2017-03-24 17:11:33 -0700233 mSkipAnimationEnd = true;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800234 if (DEBUG) Slog.d(TAG, "cancel: willReplace mTarget=" + mTarget);
Wale Ogunwalece144522016-02-05 22:51:01 -0800235 super.cancel();
236 }
237
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800238 /** Returns true if the animation target is the same as the input bounds. */
Winson Chung5af42fc2017-03-24 17:11:33 -0700239 boolean isAnimatingTo(Rect bounds) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800240 return mTo.equals(bounds);
241 }
242
Winson Chung5af42fc2017-03-24 17:11:33 -0700243 private boolean animatingToLargerSize() {
244 if (mFrom.width() * mFrom.height() > mTo.width() * mTo.height()) {
245 return false;
246 }
247 return true;
248 }
249
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800250 private void finishAnimation() {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800251 if (DEBUG) Slog.d(TAG, "finishAnimation: mTarget=" + mTarget
252 + " callers" + Debug.getCallers(2));
Winson Chung5af42fc2017-03-24 17:11:33 -0700253 if (!mSkipAnimationEnd) {
Wale Ogunwalece144522016-02-05 22:51:01 -0800254 mTarget.onAnimationEnd();
255 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800256 removeListener(this);
257 removeUpdateListener(this);
258 mRunningAnimations.remove(mTarget);
259 }
260
261 @Override
262 public void onAnimationRepeat(Animator animation) {
Winson Chung5af42fc2017-03-24 17:11:33 -0700263 // Do nothing
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800264 }
265 }
266
267 public interface AnimateBoundsUser {
268 /**
269 * Asks the target to directly (without any intermediate steps, like scheduling animation)
270 * resize its bounds.
271 *
272 * @return Whether the target still wants to be animated and successfully finished the
273 * operation. If it returns false, the animation will immediately be cancelled. The target
274 * should return false when something abnormal happened, e.g. it was completely removed
275 * from the hierarchy and is not valid anymore.
276 */
277 boolean setSize(Rect bounds);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800278 /**
279 * Behaves as setSize, but freezes the bounds of any tasks in the target at taskBounds,
Winson Chung5af42fc2017-03-24 17:11:33 -0700280 * to allow for more flexibility during resizing. Only works for the pinned stack at the
281 * moment.
Robert Carr0d00c2e2016-02-29 17:45:02 -0800282 */
283 boolean setPinnedStackSize(Rect bounds, Rect taskBounds);
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800284
Winson Chung5af42fc2017-03-24 17:11:33 -0700285 /**
286 * Callback for the target to inform it that the animation has started, so it can do some
287 * necessary preparation.
288 */
Robert Carr7e4c90e2017-02-15 19:52:38 -0800289 void onAnimationStart(boolean toFullscreen);
Wale Ogunwalece144522016-02-05 22:51:01 -0800290
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800291 /**
Winson Chung5af42fc2017-03-24 17:11:33 -0700292 * Callback for the target to inform it that the animation is going to a new fullscreen
293 * state and should update the picture-in-picture mode accordingly.
294 *
295 * @param targetStackBounds the target stack bounds we are animating to, can be null if
296 * we are animating to fullscreen
297 */
298 void updatePictureInPictureMode(Rect targetStackBounds);
299
300 /**
Wale Ogunwalece144522016-02-05 22:51:01 -0800301 * Callback for the target to inform it that the animation has ended, so it can do some
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800302 * necessary cleanup.
303 */
Wale Ogunwalece144522016-02-05 22:51:01 -0800304 void onAnimationEnd();
Filip Gruszczynskic17d8b72016-02-03 16:52:59 -0800305
306 void moveToFullscreen();
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800307 }
308
Winson Chung55893332017-02-17 17:13:10 -0800309 void animateBounds(final AnimateBoundsUser target, Rect from, Rect to, int animationDuration,
310 boolean moveToFullscreen) {
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800311 final BoundsAnimator existing = mRunningAnimations.get(target);
Wale Ogunwalece144522016-02-05 22:51:01 -0800312 final boolean replacing = existing != null;
Winson Chung5af42fc2017-03-24 17:11:33 -0700313 final boolean animatingToNewFullscreenState = (existing == null) ||
314 (existing.mMoveToFullScreen != moveToFullscreen);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800315
316 if (DEBUG) Slog.d(TAG, "animateBounds: target=" + target + " from=" + from + " to=" + to
Winson Chung5af42fc2017-03-24 17:11:33 -0700317 + " moveToFullscreen=" + moveToFullscreen + " replacing=" + replacing
318 + " animatingToNewFullscreenState=" + animatingToNewFullscreenState);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800319
Wale Ogunwalece144522016-02-05 22:51:01 -0800320 if (replacing) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800321 if (existing.isAnimatingTo(to)) {
322 // Just les the current animation complete if it has the same destination as the
323 // one we are trying to start.
324 if (DEBUG) Slog.d(TAG, "animateBounds: same destination as existing=" + existing
325 + " ignoring...");
326 return;
327 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800328 existing.cancel();
329 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700330 final BoundsAnimator animator = new BoundsAnimator(target, from, to, moveToFullscreen,
331 replacing, animatingToNewFullscreenState);
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800332 mRunningAnimations.put(target, animator);
333 animator.setFloatValues(0f, 1f);
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -0700334 animator.setDuration((animationDuration != -1 ? animationDuration
Winson Chungbaa7b722017-03-03 21:33:44 -0800335 : DEFAULT_TRANSITION_DURATION) * DEBUG_ANIMATION_SLOW_DOWN_FACTOR);
336 animator.setInterpolator(mFastOutSlowInInterpolator);
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800337 animator.start();
338 }
339}