blob: b2a12bef52838987cc6626485adf623082f2b4d9 [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
Winson Chung4a526c12017-05-16 13:35:43 -070023import android.animation.AnimationHandler;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080024import android.animation.Animator;
25import android.animation.ValueAnimator;
Winson Chung8bca9e42017-04-16 15:59:43 -070026import android.annotation.IntDef;
Winson Chungbaa7b722017-03-03 21:33:44 -080027import android.content.Context;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080028import android.graphics.Rect;
Wale Ogunwale2ba71292016-05-05 09:16:47 -070029import android.os.Handler;
Robert Carrf9aa2a92016-04-26 14:22:19 -070030import android.os.IBinder;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080031import android.os.Debug;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080032import android.util.ArrayMap;
33import android.util.Slog;
Winson Chungbaa7b722017-03-03 21:33:44 -080034import android.view.animation.AnimationUtils;
35import android.view.animation.Interpolator;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080036
Winson Chung19953ca2017-04-11 11:19:23 -070037import com.android.internal.annotations.VisibleForTesting;
38
Winson Chung8bca9e42017-04-16 15:59:43 -070039import java.lang.annotation.Retention;
40import java.lang.annotation.RetentionPolicy;
41
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080042/**
43 * Enables animating bounds of objects.
44 *
45 * In multi-window world bounds of both stack and tasks can change. When we need these bounds to
46 * change smoothly and not require the app to relaunch (e.g. because it handles resizes and
47 * relaunching it would cause poorer experience), these class provides a way to directly animate
48 * the bounds of the resized object.
49 *
Winson Chung8bca9e42017-04-16 15:59:43 -070050 * The object that is resized needs to implement {@link BoundsAnimationTarget} interface.
Wale Ogunwale2ba71292016-05-05 09:16:47 -070051 *
Winson Chung4a526c12017-05-16 13:35:43 -070052 * NOTE: All calls to methods in this class should be done on the Animation thread
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080053 */
54public class BoundsAnimationController {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080055 private static final boolean DEBUG_LOCAL = false;
56 private static final boolean DEBUG = DEBUG_LOCAL || DEBUG_ANIM;
57 private static final String TAG = TAG_WITH_CLASS_NAME || DEBUG_LOCAL
58 ? "BoundsAnimationController" : TAG_WM;
Wale Ogunwalece144522016-02-05 22:51:01 -080059 private static final int DEBUG_ANIMATION_SLOW_DOWN_FACTOR = 1;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080060
Winson Chungbaa7b722017-03-03 21:33:44 -080061 private static final int DEFAULT_TRANSITION_DURATION = 425;
62
Winson Chung8bca9e42017-04-16 15:59:43 -070063 @Retention(RetentionPolicy.SOURCE)
64 @IntDef({NO_PIP_MODE_CHANGED_CALLBACKS, SCHEDULE_PIP_MODE_CHANGED_ON_START,
65 SCHEDULE_PIP_MODE_CHANGED_ON_END})
66 public @interface SchedulePipModeChangedState {}
67 /** Do not schedule any PiP mode changed callbacks as a part of this animation. */
68 public static final int NO_PIP_MODE_CHANGED_CALLBACKS = 0;
69 /** Schedule a PiP mode changed callback when this animation starts. */
70 public static final int SCHEDULE_PIP_MODE_CHANGED_ON_START = 1;
71 /** Schedule a PiP mode changed callback when this animation ends. */
72 public static final int SCHEDULE_PIP_MODE_CHANGED_ON_END = 2;
73
Wale Ogunwalece144522016-02-05 22:51:01 -080074 // Only accessed on UI thread.
Winson Chung8bca9e42017-04-16 15:59:43 -070075 private ArrayMap<BoundsAnimationTarget, BoundsAnimator> mRunningAnimations = new ArrayMap<>();
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080076
Wale Ogunwale2ba71292016-05-05 09:16:47 -070077 private final class AppTransitionNotifier
78 extends WindowManagerInternal.AppTransitionListener implements Runnable {
Robert Carrf9aa2a92016-04-26 14:22:19 -070079
Wale Ogunwale2ba71292016-05-05 09:16:47 -070080 public void onAppTransitionCancelledLocked() {
Winson Chung87e5d552017-04-05 11:49:38 -070081 if (DEBUG) Slog.d(TAG, "onAppTransitionCancelledLocked:"
82 + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition);
Wale Ogunwale2ba71292016-05-05 09:16:47 -070083 animationFinished();
84 }
85 public void onAppTransitionFinishedLocked(IBinder token) {
Winson Chung87e5d552017-04-05 11:49:38 -070086 if (DEBUG) Slog.d(TAG, "onAppTransitionFinishedLocked:"
87 + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition);
Wale Ogunwale2ba71292016-05-05 09:16:47 -070088 animationFinished();
89 }
90 private void animationFinished() {
91 if (mFinishAnimationAfterTransition) {
92 mHandler.removeCallbacks(this);
Winson Chung87e5d552017-04-05 11:49:38 -070093 // This might end up calling into activity manager which will be bad since we have
94 // the window manager lock held at this point. Post a message to take care of the
95 // processing so we don't deadlock.
Wale Ogunwale2ba71292016-05-05 09:16:47 -070096 mHandler.post(this);
97 }
98 }
99
100 @Override
101 public void run() {
102 for (int i = 0; i < mRunningAnimations.size(); i++) {
103 final BoundsAnimator b = mRunningAnimations.valueAt(i);
104 b.onAnimationEnd(null);
105 }
106 }
107 }
108
109 private final Handler mHandler;
Robert Carrf9aa2a92016-04-26 14:22:19 -0700110 private final AppTransition mAppTransition;
Wale Ogunwale2ba71292016-05-05 09:16:47 -0700111 private final AppTransitionNotifier mAppTransitionNotifier = new AppTransitionNotifier();
Winson Chungbaa7b722017-03-03 21:33:44 -0800112 private final Interpolator mFastOutSlowInInterpolator;
Robert Carrf9aa2a92016-04-26 14:22:19 -0700113 private boolean mFinishAnimationAfterTransition = false;
Winson Chung4a526c12017-05-16 13:35:43 -0700114 private final AnimationHandler mAnimationHandler;
Robert Carrf9aa2a92016-04-26 14:22:19 -0700115
Robert Carrecc06b32017-04-18 14:25:10 -0700116 private static final int WAIT_FOR_DRAW_TIMEOUT_MS = 3000;
117
Winson Chung4a526c12017-05-16 13:35:43 -0700118 BoundsAnimationController(Context context, AppTransition transition, Handler handler,
119 AnimationHandler animationHandler) {
Wale Ogunwale2ba71292016-05-05 09:16:47 -0700120 mHandler = handler;
Robert Carrf9aa2a92016-04-26 14:22:19 -0700121 mAppTransition = transition;
122 mAppTransition.registerListenerLocked(mAppTransitionNotifier);
Winson Chungbaa7b722017-03-03 21:33:44 -0800123 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
124 com.android.internal.R.interpolator.fast_out_slow_in);
Winson Chung4a526c12017-05-16 13:35:43 -0700125 mAnimationHandler = animationHandler;
Robert Carrf9aa2a92016-04-26 14:22:19 -0700126 }
127
Winson Chung19953ca2017-04-11 11:19:23 -0700128 @VisibleForTesting
129 final class BoundsAnimator extends ValueAnimator
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800130 implements ValueAnimator.AnimatorUpdateListener, ValueAnimator.AnimatorListener {
Winson Chung4a526c12017-05-16 13:35:43 -0700131
Winson Chung8bca9e42017-04-16 15:59:43 -0700132 private final BoundsAnimationTarget mTarget;
Winson Chung84a38342016-11-08 16:15:10 -0800133 private final Rect mFrom = new Rect();
134 private final Rect mTo = new Rect();
Robert Carr0d00c2e2016-02-29 17:45:02 -0800135 private final Rect mTmpRect = new Rect();
136 private final Rect mTmpTaskBounds = new Rect();
Winson Chung8bca9e42017-04-16 15:59:43 -0700137
138 // True if this this animation was canceled and will be replaced the another animation from
139 // the same {@link #BoundsAnimationTarget} target.
Winson Chung19953ca2017-04-11 11:19:23 -0700140 private boolean mSkipFinalResize;
Winson Chung8bca9e42017-04-16 15:59:43 -0700141 // True if this animation was canceled by the user, not as a part of a replacing animation
Winson Chung19953ca2017-04-11 11:19:23 -0700142 private boolean mSkipAnimationEnd;
Winson Chunge7ba6862017-05-24 12:13:33 -0700143
144 // True if the animation target is animating from the fullscreen. Only one of
145 // {@link mMoveToFullscreen} or {@link mMoveFromFullscreen} can be true at any time in the
146 // animation.
147 private boolean mMoveFromFullscreen;
Winson Chung8bca9e42017-04-16 15:59:43 -0700148 // True if the animation target should be moved to the fullscreen stack at the end of this
Winson Chunge7ba6862017-05-24 12:13:33 -0700149 // animation. Only one of {@link mMoveToFullscreen} or {@link mMoveFromFullscreen} can be
150 // true at any time in the animation.
Winson Chung8bca9e42017-04-16 15:59:43 -0700151 private boolean mMoveToFullscreen;
152
153 // Whether to schedule PiP mode changes on animation start/end
154 private @SchedulePipModeChangedState int mSchedulePipModeChangedState;
Winson Chungab76bbc2017-08-14 13:33:51 -0700155 private @SchedulePipModeChangedState int mPrevSchedulePipModeChangedState;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800156
Robert Carr0d00c2e2016-02-29 17:45:02 -0800157 // Depending on whether we are animating from
158 // a smaller to a larger size
159 private final int mFrozenTaskWidth;
160 private final int mFrozenTaskHeight;
161
Winson Chunge7ba6862017-05-24 12:13:33 -0700162 // Timeout callback to ensure we continue the animation if waiting for resuming or app
163 // windows drawn fails
Winson Chung6a38fca2018-03-28 17:57:09 -0700164 private final Runnable mResumeRunnable = () -> {
165 if (DEBUG) Slog.d(TAG, "pause: timed out waiting for windows drawn");
166 resume();
167 };
Winson Chunge7ba6862017-05-24 12:13:33 -0700168
Winson Chung8bca9e42017-04-16 15:59:43 -0700169 BoundsAnimator(BoundsAnimationTarget target, Rect from, Rect to,
170 @SchedulePipModeChangedState int schedulePipModeChangedState,
Winson Chungab76bbc2017-08-14 13:33:51 -0700171 @SchedulePipModeChangedState int prevShedulePipModeChangedState,
172 boolean moveFromFullscreen, boolean moveToFullscreen) {
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800173 super();
174 mTarget = target;
Winson Chung84a38342016-11-08 16:15:10 -0800175 mFrom.set(from);
176 mTo.set(to);
Winson Chung8bca9e42017-04-16 15:59:43 -0700177 mSchedulePipModeChangedState = schedulePipModeChangedState;
Winson Chungab76bbc2017-08-14 13:33:51 -0700178 mPrevSchedulePipModeChangedState = prevShedulePipModeChangedState;
Winson Chunge7ba6862017-05-24 12:13:33 -0700179 mMoveFromFullscreen = moveFromFullscreen;
Winson Chung8bca9e42017-04-16 15:59:43 -0700180 mMoveToFullscreen = moveToFullscreen;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800181 addUpdateListener(this);
182 addListener(this);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800183
184 // If we are animating from smaller to larger, we want to change the task bounds
185 // to their final size immediately so we can use scaling to make the window
186 // larger. Likewise if we are going from bigger to smaller, we want to wait until
187 // the end so we don't have to upscale from the smaller finished size.
188 if (animatingToLargerSize()) {
189 mFrozenTaskWidth = mTo.width();
190 mFrozenTaskHeight = mTo.height();
191 } else {
192 mFrozenTaskWidth = mFrom.width();
193 mFrozenTaskHeight = mFrom.height();
194 }
195 }
196
Winson Chung5af42fc2017-03-24 17:11:33 -0700197 @Override
198 public void onAnimationStart(Animator animation) {
199 if (DEBUG) Slog.d(TAG, "onAnimationStart: mTarget=" + mTarget
Winson Chungab76bbc2017-08-14 13:33:51 -0700200 + " mPrevSchedulePipModeChangedState=" + mPrevSchedulePipModeChangedState
Winson Chung8bca9e42017-04-16 15:59:43 -0700201 + " mSchedulePipModeChangedState=" + mSchedulePipModeChangedState);
Winson Chung5af42fc2017-03-24 17:11:33 -0700202 mFinishAnimationAfterTransition = false;
203 mTmpRect.set(mFrom.left, mFrom.top, mFrom.left + mFrozenTaskWidth,
204 mFrom.top + mFrozenTaskHeight);
205
Winson Chung1ec3e262017-06-09 12:01:20 -0700206 // Boost the thread priority of the animation thread while the bounds animation is
207 // running
208 updateBooster();
209
Winson Chungab76bbc2017-08-14 13:33:51 -0700210 // Ensure that we have prepared the target for animation before we trigger any size
211 // changes, so it can swap surfaces in to appropriate modes, or do as it wishes
212 // otherwise.
213 if (mPrevSchedulePipModeChangedState == NO_PIP_MODE_CHANGED_CALLBACKS) {
Winson Chung8bca9e42017-04-16 15:59:43 -0700214 mTarget.onAnimationStart(mSchedulePipModeChangedState ==
Winson Chungab76bbc2017-08-14 13:33:51 -0700215 SCHEDULE_PIP_MODE_CHANGED_ON_START, false /* forceUpdate */);
Winson Chunge7ba6862017-05-24 12:13:33 -0700216
217 // When starting an animation from fullscreen, pause here and wait for the
218 // windows-drawn signal before we start the rest of the transition down into PiP.
Winson Chung6a38fca2018-03-28 17:57:09 -0700219 if (mMoveFromFullscreen && mTarget.shouldDeferStartOnMoveToFullscreen()) {
Winson Chunge7ba6862017-05-24 12:13:33 -0700220 pause();
221 }
Winson Chungab76bbc2017-08-14 13:33:51 -0700222 } else if (mPrevSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_END &&
223 mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) {
224 // We are replacing a running animation into PiP, but since it hasn't completed, the
225 // client will not currently receive any picture-in-picture mode change callbacks.
226 // However, we still need to report to them that they are leaving PiP, so this will
227 // force an update via a mode changed callback.
228 mTarget.onAnimationStart(true /* schedulePipModeChangedCallback */,
229 true /* forceUpdate */);
Winson Chung5af42fc2017-03-24 17:11:33 -0700230 }
231
232 // Immediately update the task bounds if they have to become larger, but preserve
233 // the starting position so we don't jump at the beginning of the animation.
234 if (animatingToLargerSize()) {
235 mTarget.setPinnedStackSize(mFrom, mTmpRect);
Robert Carrecc06b32017-04-18 14:25:10 -0700236
237 // We pause the animation until the app has drawn at the new size.
238 // The target will notify us via BoundsAnimationController#resume.
239 // We do this here and pause the animation, rather than just defer starting it
240 // so we can enter the animating state and have WindowStateAnimator apply the
241 // correct logic to make this resize seamless.
242 if (mMoveToFullscreen) {
243 pause();
Robert Carrecc06b32017-04-18 14:25:10 -0700244 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700245 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800246 }
247
248 @Override
Winson Chunge7ba6862017-05-24 12:13:33 -0700249 public void pause() {
250 if (DEBUG) Slog.d(TAG, "pause: waiting for windows drawn");
251 super.pause();
252 mHandler.postDelayed(mResumeRunnable, WAIT_FOR_DRAW_TIMEOUT_MS);
253 }
254
255 @Override
Robert Carrecc06b32017-04-18 14:25:10 -0700256 public void resume() {
Winson Chunge7ba6862017-05-24 12:13:33 -0700257 if (DEBUG) Slog.d(TAG, "resume:");
Robert Carrecc06b32017-04-18 14:25:10 -0700258 mHandler.removeCallbacks(mResumeRunnable);
259 super.resume();
260 }
261
262 @Override
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800263 public void onAnimationUpdate(ValueAnimator animation) {
264 final float value = (Float) animation.getAnimatedValue();
265 final float remains = 1 - value;
Wale Ogunwalece144522016-02-05 22:51:01 -0800266 mTmpRect.left = (int) (mFrom.left * remains + mTo.left * value + 0.5f);
267 mTmpRect.top = (int) (mFrom.top * remains + mTo.top * value + 0.5f);
268 mTmpRect.right = (int) (mFrom.right * remains + mTo.right * value + 0.5f);
269 mTmpRect.bottom = (int) (mFrom.bottom * remains + mTo.bottom * value + 0.5f);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800270 if (DEBUG) Slog.d(TAG, "animateUpdate: mTarget=" + mTarget + " mBounds="
271 + mTmpRect + " from=" + mFrom + " mTo=" + mTo + " value=" + value
272 + " remains=" + remains);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800273
Robert Carrc7294602016-05-13 11:32:05 -0700274 mTmpTaskBounds.set(mTmpRect.left, mTmpRect.top,
275 mTmpRect.left + mFrozenTaskWidth, mTmpRect.top + mFrozenTaskHeight);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800276
Robert Carrc7294602016-05-13 11:32:05 -0700277 if (!mTarget.setPinnedStackSize(mTmpRect, mTmpTaskBounds)) {
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800278 // Whoops, the target doesn't feel like animating anymore. Let's immediately finish
279 // any further animation.
Winson Chung87e5d552017-04-05 11:49:38 -0700280 if (DEBUG) Slog.d(TAG, "animateUpdate: cancelled");
Winson Chung19953ca2017-04-11 11:19:23 -0700281
Winson Chung8bca9e42017-04-16 15:59:43 -0700282 // If we have already scheduled a PiP mode changed at the start of the animation,
283 // then we need to clean up and schedule one at the end, since we have canceled the
284 // animation to the final state.
285 if (mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) {
286 mSchedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END;
287 }
288
Winson Chung19953ca2017-04-11 11:19:23 -0700289 // Since we are cancelling immediately without a replacement animation, send the
290 // animation end to maintain callback parity, but also skip any further resizes
Winson Chung8bca9e42017-04-16 15:59:43 -0700291 cancelAndCallAnimationEnd();
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800292 }
293 }
294
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800295 @Override
296 public void onAnimationEnd(Animator animation) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800297 if (DEBUG) Slog.d(TAG, "onAnimationEnd: mTarget=" + mTarget
Winson Chung19953ca2017-04-11 11:19:23 -0700298 + " mSkipFinalResize=" + mSkipFinalResize
Winson Chung87e5d552017-04-05 11:49:38 -0700299 + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition
Winson Chung8bca9e42017-04-16 15:59:43 -0700300 + " mAppTransitionIsRunning=" + mAppTransition.isRunning()
301 + " callers=" + Debug.getCallers(2));
Jaewan Kimb0033642016-04-22 18:41:37 +0900302
Robert Carrf9aa2a92016-04-26 14:22:19 -0700303 // There could be another animation running. For example in the
304 // move to fullscreen case, recents will also be closing while the
305 // previous task will be taking its place in the fullscreen stack.
306 // we have to ensure this is completed before we finish the animation
307 // and take our place in the fullscreen stack.
308 if (mAppTransition.isRunning() && !mFinishAnimationAfterTransition) {
309 mFinishAnimationAfterTransition = true;
310 return;
311 }
312
Winson Chung8bca9e42017-04-16 15:59:43 -0700313 if (!mSkipAnimationEnd) {
314 // If this animation has already scheduled the picture-in-picture mode on start, and
315 // we are not skipping the final resize due to being canceled, then move the PiP to
316 // fullscreen once the animation ends
317 if (DEBUG) Slog.d(TAG, "onAnimationEnd: mTarget=" + mTarget
318 + " moveToFullscreen=" + mMoveToFullscreen);
319 mTarget.onAnimationEnd(mSchedulePipModeChangedState ==
320 SCHEDULE_PIP_MODE_CHANGED_ON_END, !mSkipFinalResize ? mTo : null,
321 mMoveToFullscreen);
Winson Chung19953ca2017-04-11 11:19:23 -0700322 }
323
Winson Chung8bca9e42017-04-16 15:59:43 -0700324 // Clean up this animation
325 removeListener(this);
326 removeUpdateListener(this);
327 mRunningAnimations.remove(mTarget);
Winson Chung1ec3e262017-06-09 12:01:20 -0700328
329 // Reset the thread priority of the animation thread after the bounds animation is done
330 updateBooster();
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800331 }
332
333 @Override
334 public void onAnimationCancel(Animator animation) {
Winson Chung8bca9e42017-04-16 15:59:43 -0700335 // Always skip the final resize when the animation is canceled
336 mSkipFinalResize = true;
337 mMoveToFullscreen = false;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800338 }
339
Winson Chung8bca9e42017-04-16 15:59:43 -0700340 private void cancelAndCallAnimationEnd() {
341 if (DEBUG) Slog.d(TAG, "cancelAndCallAnimationEnd: mTarget=" + mTarget);
342 mSkipAnimationEnd = false;
343 super.cancel();
Winson Chung19953ca2017-04-11 11:19:23 -0700344 }
345
Wale Ogunwalece144522016-02-05 22:51:01 -0800346 @Override
347 public void cancel() {
Winson Chung19953ca2017-04-11 11:19:23 -0700348 if (DEBUG) Slog.d(TAG, "cancel: mTarget=" + mTarget);
Winson Chung8bca9e42017-04-16 15:59:43 -0700349 mSkipAnimationEnd = true;
Wale Ogunwalece144522016-02-05 22:51:01 -0800350 super.cancel();
351 }
352
Winson Chung8bca9e42017-04-16 15:59:43 -0700353 /**
354 * @return true if the animation target is the same as the input bounds.
355 */
Winson Chung5af42fc2017-03-24 17:11:33 -0700356 boolean isAnimatingTo(Rect bounds) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800357 return mTo.equals(bounds);
358 }
359
Winson Chung8bca9e42017-04-16 15:59:43 -0700360 /**
361 * @return true if we are animating to a larger surface size
362 */
363 @VisibleForTesting
364 boolean animatingToLargerSize() {
365 // TODO: Fix this check for aspect ratio changes
366 return (mFrom.width() * mFrom.height() <= mTo.width() * mTo.height());
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800367 }
368
369 @Override
370 public void onAnimationRepeat(Animator animation) {
Winson Chung5af42fc2017-03-24 17:11:33 -0700371 // Do nothing
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800372 }
Winson Chung4a526c12017-05-16 13:35:43 -0700373
374 @Override
375 public AnimationHandler getAnimationHandler() {
376 if (mAnimationHandler != null) {
377 return mAnimationHandler;
378 }
379 return super.getAnimationHandler();
380 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800381 }
382
Winson Chung8bca9e42017-04-16 15:59:43 -0700383 public void animateBounds(final BoundsAnimationTarget target, Rect from, Rect to,
384 int animationDuration, @SchedulePipModeChangedState int schedulePipModeChangedState,
Winson Chunge7ba6862017-05-24 12:13:33 -0700385 boolean moveFromFullscreen, boolean moveToFullscreen) {
Winson Chung8bca9e42017-04-16 15:59:43 -0700386 animateBoundsImpl(target, from, to, animationDuration, schedulePipModeChangedState,
Winson Chunge7ba6862017-05-24 12:13:33 -0700387 moveFromFullscreen, moveToFullscreen);
Winson Chung19953ca2017-04-11 11:19:23 -0700388 }
389
390 @VisibleForTesting
Winson Chung8bca9e42017-04-16 15:59:43 -0700391 BoundsAnimator animateBoundsImpl(final BoundsAnimationTarget target, Rect from, Rect to,
392 int animationDuration, @SchedulePipModeChangedState int schedulePipModeChangedState,
Winson Chunge7ba6862017-05-24 12:13:33 -0700393 boolean moveFromFullscreen, boolean moveToFullscreen) {
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800394 final BoundsAnimator existing = mRunningAnimations.get(target);
Wale Ogunwalece144522016-02-05 22:51:01 -0800395 final boolean replacing = existing != null;
Winson Chungab76bbc2017-08-14 13:33:51 -0700396 @SchedulePipModeChangedState int prevSchedulePipModeChangedState =
397 NO_PIP_MODE_CHANGED_CALLBACKS;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800398
399 if (DEBUG) Slog.d(TAG, "animateBounds: target=" + target + " from=" + from + " to=" + to
Winson Chung8bca9e42017-04-16 15:59:43 -0700400 + " schedulePipModeChangedState=" + schedulePipModeChangedState
401 + " replacing=" + replacing);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800402
Wale Ogunwalece144522016-02-05 22:51:01 -0800403 if (replacing) {
Adrian Roose43c4e42018-04-25 18:51:00 +0200404 if (existing.isAnimatingTo(to) && (!moveToFullscreen || existing.mMoveToFullscreen)
405 && (!moveFromFullscreen || existing.mMoveFromFullscreen)) {
Winson Chung19953ca2017-04-11 11:19:23 -0700406 // Just let the current animation complete if it has the same destination as the
Adrian Roose43c4e42018-04-25 18:51:00 +0200407 // one we are trying to start, and, if moveTo/FromFullscreen was requested, already
408 // has that flag set.
409 if (DEBUG) Slog.d(TAG, "animateBounds: same destination and moveTo/From flags as "
410 + "existing=" + existing + ", ignoring...");
Winson Chung19953ca2017-04-11 11:19:23 -0700411 return existing;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800412 }
Winson Chung8bca9e42017-04-16 15:59:43 -0700413
Winson Chungab76bbc2017-08-14 13:33:51 -0700414 // Save the previous state
415 prevSchedulePipModeChangedState = existing.mSchedulePipModeChangedState;
416
Winson Chung8bca9e42017-04-16 15:59:43 -0700417 // Update the PiP callback states if we are replacing the animation
418 if (existing.mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) {
419 if (schedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) {
420 if (DEBUG) Slog.d(TAG, "animateBounds: still animating to fullscreen, keep"
421 + " existing deferred state");
422 } else {
423 if (DEBUG) Slog.d(TAG, "animateBounds: fullscreen animation canceled, callback"
424 + " on start already processed, schedule deferred update on end");
425 schedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END;
426 }
427 } else if (existing.mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_END) {
428 if (schedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) {
429 if (DEBUG) Slog.d(TAG, "animateBounds: non-fullscreen animation canceled,"
430 + " callback on start will be processed");
431 } else {
432 if (DEBUG) Slog.d(TAG, "animateBounds: still animating from fullscreen, keep"
433 + " existing deferred state");
434 schedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END;
435 }
436 }
437
Adrian Roose43c4e42018-04-25 18:51:00 +0200438 // We need to keep the previous moveTo/FromFullscreen flag, unless the new animation
439 // specifies a direction.
440 if (!moveFromFullscreen && !moveToFullscreen) {
441 moveToFullscreen = existing.mMoveToFullscreen;
442 moveFromFullscreen = existing.mMoveFromFullscreen;
443 }
444
Winson Chung8bca9e42017-04-16 15:59:43 -0700445 // Since we are replacing, we skip both animation start and end callbacks
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800446 existing.cancel();
447 }
Winson Chung8bca9e42017-04-16 15:59:43 -0700448 final BoundsAnimator animator = new BoundsAnimator(target, from, to,
Winson Chungab76bbc2017-08-14 13:33:51 -0700449 schedulePipModeChangedState, prevSchedulePipModeChangedState,
450 moveFromFullscreen, moveToFullscreen);
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800451 mRunningAnimations.put(target, animator);
452 animator.setFloatValues(0f, 1f);
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -0700453 animator.setDuration((animationDuration != -1 ? animationDuration
Winson Chungbaa7b722017-03-03 21:33:44 -0800454 : DEFAULT_TRANSITION_DURATION) * DEBUG_ANIMATION_SLOW_DOWN_FACTOR);
455 animator.setInterpolator(mFastOutSlowInInterpolator);
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800456 animator.start();
Winson Chung19953ca2017-04-11 11:19:23 -0700457 return animator;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800458 }
Robert Carrecc06b32017-04-18 14:25:10 -0700459
Winson Chunge7ba6862017-05-24 12:13:33 -0700460 public Handler getHandler() {
461 return mHandler;
462 }
463
464 public void onAllWindowsDrawn() {
465 if (DEBUG) Slog.d(TAG, "onAllWindowsDrawn:");
466 mHandler.post(this::resume);
467 }
468
Robert Carrecc06b32017-04-18 14:25:10 -0700469 private void resume() {
470 for (int i = 0; i < mRunningAnimations.size(); i++) {
471 final BoundsAnimator b = mRunningAnimations.valueAt(i);
472 b.resume();
473 }
474 }
Winson Chung1ec3e262017-06-09 12:01:20 -0700475
476 private void updateBooster() {
477 WindowManagerService.sThreadPriorityBooster.setBoundsAnimationRunning(
478 !mRunningAnimations.isEmpty());
479 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800480}