blob: 410efcdb5038e424687c0fd413fc6441a8256dd4 [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 Chung8bca9e42017-04-16 15:59:43 -070025import android.annotation.IntDef;
Winson Chungbaa7b722017-03-03 21:33:44 -080026import android.content.Context;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080027import android.graphics.Rect;
Wale Ogunwale2ba71292016-05-05 09:16:47 -070028import android.os.Handler;
Robert Carrf9aa2a92016-04-26 14:22:19 -070029import android.os.IBinder;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -080030import android.os.Debug;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -080031import android.util.ArrayMap;
32import android.util.Slog;
Winson Chungbaa7b722017-03-03 21:33:44 -080033import android.view.animation.AnimationUtils;
34import android.view.animation.Interpolator;
Robert Carrf9aa2a92016-04-26 14:22:19 -070035import android.view.WindowManagerInternal;
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 *
52 * NOTE: All calls to methods in this class should be done on the UI 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;
114
Robert Carrecc06b32017-04-18 14:25:10 -0700115 private static final int WAIT_FOR_DRAW_TIMEOUT_MS = 3000;
116
Winson Chungbaa7b722017-03-03 21:33:44 -0800117 BoundsAnimationController(Context context, AppTransition transition, Handler handler) {
Wale Ogunwale2ba71292016-05-05 09:16:47 -0700118 mHandler = handler;
Robert Carrf9aa2a92016-04-26 14:22:19 -0700119 mAppTransition = transition;
120 mAppTransition.registerListenerLocked(mAppTransitionNotifier);
Winson Chungbaa7b722017-03-03 21:33:44 -0800121 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
122 com.android.internal.R.interpolator.fast_out_slow_in);
Robert Carrf9aa2a92016-04-26 14:22:19 -0700123 }
124
Winson Chung19953ca2017-04-11 11:19:23 -0700125 @VisibleForTesting
126 final class BoundsAnimator extends ValueAnimator
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800127 implements ValueAnimator.AnimatorUpdateListener, ValueAnimator.AnimatorListener {
Winson Chung8bca9e42017-04-16 15:59:43 -0700128 private final BoundsAnimationTarget mTarget;
Winson Chung84a38342016-11-08 16:15:10 -0800129 private final Rect mFrom = new Rect();
130 private final Rect mTo = new Rect();
Robert Carr0d00c2e2016-02-29 17:45:02 -0800131 private final Rect mTmpRect = new Rect();
132 private final Rect mTmpTaskBounds = new Rect();
Winson Chung8bca9e42017-04-16 15:59:43 -0700133
134 // True if this this animation was canceled and will be replaced the another animation from
135 // the same {@link #BoundsAnimationTarget} target.
Winson Chung19953ca2017-04-11 11:19:23 -0700136 private boolean mSkipFinalResize;
Winson Chung5af42fc2017-03-24 17:11:33 -0700137 // True if this animation replaced a previous animation of the same
Winson Chung8bca9e42017-04-16 15:59:43 -0700138 // {@link #BoundsAnimationTarget} target.
Winson Chung5af42fc2017-03-24 17:11:33 -0700139 private final boolean mSkipAnimationStart;
Winson Chung8bca9e42017-04-16 15:59:43 -0700140 // True if this animation was canceled by the user, not as a part of a replacing animation
Winson Chung19953ca2017-04-11 11:19:23 -0700141 private boolean mSkipAnimationEnd;
Winson Chunge7ba6862017-05-24 12:13:33 -0700142
143 // True if the animation target is animating from the fullscreen. Only one of
144 // {@link mMoveToFullscreen} or {@link mMoveFromFullscreen} can be true at any time in the
145 // animation.
146 private boolean mMoveFromFullscreen;
Winson Chung8bca9e42017-04-16 15:59:43 -0700147 // True if the animation target should be moved to the fullscreen stack at the end of this
Winson Chunge7ba6862017-05-24 12:13:33 -0700148 // animation. Only one of {@link mMoveToFullscreen} or {@link mMoveFromFullscreen} can be
149 // true at any time in the animation.
Winson Chung8bca9e42017-04-16 15:59:43 -0700150 private boolean mMoveToFullscreen;
151
152 // Whether to schedule PiP mode changes on animation start/end
153 private @SchedulePipModeChangedState int mSchedulePipModeChangedState;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800154
Robert Carr0d00c2e2016-02-29 17:45:02 -0800155 // Depending on whether we are animating from
156 // a smaller to a larger size
157 private final int mFrozenTaskWidth;
158 private final int mFrozenTaskHeight;
159
Winson Chunge7ba6862017-05-24 12:13:33 -0700160 // Timeout callback to ensure we continue the animation if waiting for resuming or app
161 // windows drawn fails
162 private final Runnable mResumeRunnable = () -> resume();
163
Winson Chung8bca9e42017-04-16 15:59:43 -0700164 BoundsAnimator(BoundsAnimationTarget target, Rect from, Rect to,
165 @SchedulePipModeChangedState int schedulePipModeChangedState,
Winson Chunge7ba6862017-05-24 12:13:33 -0700166 boolean moveFromFullscreen, boolean moveToFullscreen,
167 boolean replacingExistingAnimation) {
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800168 super();
169 mTarget = target;
Winson Chung84a38342016-11-08 16:15:10 -0800170 mFrom.set(from);
171 mTo.set(to);
Winson Chung5af42fc2017-03-24 17:11:33 -0700172 mSkipAnimationStart = replacingExistingAnimation;
Winson Chung8bca9e42017-04-16 15:59:43 -0700173 mSchedulePipModeChangedState = schedulePipModeChangedState;
Winson Chunge7ba6862017-05-24 12:13:33 -0700174 mMoveFromFullscreen = moveFromFullscreen;
Winson Chung8bca9e42017-04-16 15:59:43 -0700175 mMoveToFullscreen = moveToFullscreen;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800176 addUpdateListener(this);
177 addListener(this);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800178
179 // If we are animating from smaller to larger, we want to change the task bounds
180 // to their final size immediately so we can use scaling to make the window
181 // larger. Likewise if we are going from bigger to smaller, we want to wait until
182 // the end so we don't have to upscale from the smaller finished size.
183 if (animatingToLargerSize()) {
184 mFrozenTaskWidth = mTo.width();
185 mFrozenTaskHeight = mTo.height();
186 } else {
187 mFrozenTaskWidth = mFrom.width();
188 mFrozenTaskHeight = mFrom.height();
189 }
190 }
191
Winson Chung5af42fc2017-03-24 17:11:33 -0700192 @Override
193 public void onAnimationStart(Animator animation) {
194 if (DEBUG) Slog.d(TAG, "onAnimationStart: mTarget=" + mTarget
Winson Chung8bca9e42017-04-16 15:59:43 -0700195 + " mSkipAnimationStart=" + mSkipAnimationStart
196 + " mSchedulePipModeChangedState=" + mSchedulePipModeChangedState);
Winson Chung5af42fc2017-03-24 17:11:33 -0700197 mFinishAnimationAfterTransition = false;
198 mTmpRect.set(mFrom.left, mFrom.top, mFrom.left + mFrozenTaskWidth,
199 mFrom.top + mFrozenTaskHeight);
200
201 // Ensure that we have prepared the target for animation before
202 // we trigger any size changes, so it can swap surfaces
203 // in to appropriate modes, or do as it wishes otherwise.
204 if (!mSkipAnimationStart) {
Winson Chung8bca9e42017-04-16 15:59:43 -0700205 mTarget.onAnimationStart(mSchedulePipModeChangedState ==
206 SCHEDULE_PIP_MODE_CHANGED_ON_START);
Winson Chunge7ba6862017-05-24 12:13:33 -0700207
208 // When starting an animation from fullscreen, pause here and wait for the
209 // windows-drawn signal before we start the rest of the transition down into PiP.
210 if (mMoveFromFullscreen) {
211 pause();
212 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700213 }
214
215 // Immediately update the task bounds if they have to become larger, but preserve
216 // the starting position so we don't jump at the beginning of the animation.
217 if (animatingToLargerSize()) {
218 mTarget.setPinnedStackSize(mFrom, mTmpRect);
Robert Carrecc06b32017-04-18 14:25:10 -0700219
220 // We pause the animation until the app has drawn at the new size.
221 // The target will notify us via BoundsAnimationController#resume.
222 // We do this here and pause the animation, rather than just defer starting it
223 // so we can enter the animating state and have WindowStateAnimator apply the
224 // correct logic to make this resize seamless.
225 if (mMoveToFullscreen) {
226 pause();
Robert Carrecc06b32017-04-18 14:25:10 -0700227 }
Winson Chung5af42fc2017-03-24 17:11:33 -0700228 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800229 }
230
231 @Override
Winson Chunge7ba6862017-05-24 12:13:33 -0700232 public void pause() {
233 if (DEBUG) Slog.d(TAG, "pause: waiting for windows drawn");
234 super.pause();
235 mHandler.postDelayed(mResumeRunnable, WAIT_FOR_DRAW_TIMEOUT_MS);
236 }
237
238 @Override
Robert Carrecc06b32017-04-18 14:25:10 -0700239 public void resume() {
Winson Chunge7ba6862017-05-24 12:13:33 -0700240 if (DEBUG) Slog.d(TAG, "resume:");
Robert Carrecc06b32017-04-18 14:25:10 -0700241 mHandler.removeCallbacks(mResumeRunnable);
242 super.resume();
243 }
244
245 @Override
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800246 public void onAnimationUpdate(ValueAnimator animation) {
247 final float value = (Float) animation.getAnimatedValue();
248 final float remains = 1 - value;
Wale Ogunwalece144522016-02-05 22:51:01 -0800249 mTmpRect.left = (int) (mFrom.left * remains + mTo.left * value + 0.5f);
250 mTmpRect.top = (int) (mFrom.top * remains + mTo.top * value + 0.5f);
251 mTmpRect.right = (int) (mFrom.right * remains + mTo.right * value + 0.5f);
252 mTmpRect.bottom = (int) (mFrom.bottom * remains + mTo.bottom * value + 0.5f);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800253 if (DEBUG) Slog.d(TAG, "animateUpdate: mTarget=" + mTarget + " mBounds="
254 + mTmpRect + " from=" + mFrom + " mTo=" + mTo + " value=" + value
255 + " remains=" + remains);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800256
Robert Carrc7294602016-05-13 11:32:05 -0700257 mTmpTaskBounds.set(mTmpRect.left, mTmpRect.top,
258 mTmpRect.left + mFrozenTaskWidth, mTmpRect.top + mFrozenTaskHeight);
Robert Carr0d00c2e2016-02-29 17:45:02 -0800259
Robert Carrc7294602016-05-13 11:32:05 -0700260 if (!mTarget.setPinnedStackSize(mTmpRect, mTmpTaskBounds)) {
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800261 // Whoops, the target doesn't feel like animating anymore. Let's immediately finish
262 // any further animation.
Winson Chung87e5d552017-04-05 11:49:38 -0700263 if (DEBUG) Slog.d(TAG, "animateUpdate: cancelled");
Winson Chung19953ca2017-04-11 11:19:23 -0700264
Winson Chung8bca9e42017-04-16 15:59:43 -0700265 // If we have already scheduled a PiP mode changed at the start of the animation,
266 // then we need to clean up and schedule one at the end, since we have canceled the
267 // animation to the final state.
268 if (mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) {
269 mSchedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END;
270 }
271
Winson Chung19953ca2017-04-11 11:19:23 -0700272 // Since we are cancelling immediately without a replacement animation, send the
273 // animation end to maintain callback parity, but also skip any further resizes
Winson Chung8bca9e42017-04-16 15:59:43 -0700274 cancelAndCallAnimationEnd();
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800275 }
276 }
277
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800278 @Override
279 public void onAnimationEnd(Animator animation) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800280 if (DEBUG) Slog.d(TAG, "onAnimationEnd: mTarget=" + mTarget
Winson Chung19953ca2017-04-11 11:19:23 -0700281 + " mSkipFinalResize=" + mSkipFinalResize
Winson Chung87e5d552017-04-05 11:49:38 -0700282 + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition
Winson Chung8bca9e42017-04-16 15:59:43 -0700283 + " mAppTransitionIsRunning=" + mAppTransition.isRunning()
284 + " callers=" + Debug.getCallers(2));
Jaewan Kimb0033642016-04-22 18:41:37 +0900285
Robert Carrf9aa2a92016-04-26 14:22:19 -0700286 // There could be another animation running. For example in the
287 // move to fullscreen case, recents will also be closing while the
288 // previous task will be taking its place in the fullscreen stack.
289 // we have to ensure this is completed before we finish the animation
290 // and take our place in the fullscreen stack.
291 if (mAppTransition.isRunning() && !mFinishAnimationAfterTransition) {
292 mFinishAnimationAfterTransition = true;
293 return;
294 }
295
Winson Chung8bca9e42017-04-16 15:59:43 -0700296 if (!mSkipAnimationEnd) {
297 // If this animation has already scheduled the picture-in-picture mode on start, and
298 // we are not skipping the final resize due to being canceled, then move the PiP to
299 // fullscreen once the animation ends
300 if (DEBUG) Slog.d(TAG, "onAnimationEnd: mTarget=" + mTarget
301 + " moveToFullscreen=" + mMoveToFullscreen);
302 mTarget.onAnimationEnd(mSchedulePipModeChangedState ==
303 SCHEDULE_PIP_MODE_CHANGED_ON_END, !mSkipFinalResize ? mTo : null,
304 mMoveToFullscreen);
Winson Chung19953ca2017-04-11 11:19:23 -0700305 }
306
Winson Chung8bca9e42017-04-16 15:59:43 -0700307 // Clean up this animation
308 removeListener(this);
309 removeUpdateListener(this);
310 mRunningAnimations.remove(mTarget);
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800311 }
312
313 @Override
314 public void onAnimationCancel(Animator animation) {
Winson Chung8bca9e42017-04-16 15:59:43 -0700315 // Always skip the final resize when the animation is canceled
316 mSkipFinalResize = true;
317 mMoveToFullscreen = false;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800318 }
319
Winson Chung8bca9e42017-04-16 15:59:43 -0700320 private void cancelAndCallAnimationEnd() {
321 if (DEBUG) Slog.d(TAG, "cancelAndCallAnimationEnd: mTarget=" + mTarget);
322 mSkipAnimationEnd = false;
323 super.cancel();
Winson Chung19953ca2017-04-11 11:19:23 -0700324 }
325
Wale Ogunwalece144522016-02-05 22:51:01 -0800326 @Override
327 public void cancel() {
Winson Chung19953ca2017-04-11 11:19:23 -0700328 if (DEBUG) Slog.d(TAG, "cancel: mTarget=" + mTarget);
Winson Chung8bca9e42017-04-16 15:59:43 -0700329 mSkipAnimationEnd = true;
Wale Ogunwalece144522016-02-05 22:51:01 -0800330 super.cancel();
331 }
332
Winson Chung8bca9e42017-04-16 15:59:43 -0700333 /**
334 * @return true if the animation target is the same as the input bounds.
335 */
Winson Chung5af42fc2017-03-24 17:11:33 -0700336 boolean isAnimatingTo(Rect bounds) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800337 return mTo.equals(bounds);
338 }
339
Winson Chung8bca9e42017-04-16 15:59:43 -0700340 /**
341 * @return true if we are animating to a larger surface size
342 */
343 @VisibleForTesting
344 boolean animatingToLargerSize() {
345 // TODO: Fix this check for aspect ratio changes
346 return (mFrom.width() * mFrom.height() <= mTo.width() * mTo.height());
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800347 }
348
349 @Override
350 public void onAnimationRepeat(Animator animation) {
Winson Chung5af42fc2017-03-24 17:11:33 -0700351 // Do nothing
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800352 }
353 }
354
Winson Chung8bca9e42017-04-16 15:59:43 -0700355 public void animateBounds(final BoundsAnimationTarget target, Rect from, Rect to,
356 int animationDuration, @SchedulePipModeChangedState int schedulePipModeChangedState,
Winson Chunge7ba6862017-05-24 12:13:33 -0700357 boolean moveFromFullscreen, boolean moveToFullscreen) {
Winson Chung8bca9e42017-04-16 15:59:43 -0700358 animateBoundsImpl(target, from, to, animationDuration, schedulePipModeChangedState,
Winson Chunge7ba6862017-05-24 12:13:33 -0700359 moveFromFullscreen, moveToFullscreen);
Winson Chung19953ca2017-04-11 11:19:23 -0700360 }
361
362 @VisibleForTesting
Winson Chung8bca9e42017-04-16 15:59:43 -0700363 BoundsAnimator animateBoundsImpl(final BoundsAnimationTarget target, Rect from, Rect to,
364 int animationDuration, @SchedulePipModeChangedState int schedulePipModeChangedState,
Winson Chunge7ba6862017-05-24 12:13:33 -0700365 boolean moveFromFullscreen, boolean moveToFullscreen) {
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800366 final BoundsAnimator existing = mRunningAnimations.get(target);
Wale Ogunwalece144522016-02-05 22:51:01 -0800367 final boolean replacing = existing != null;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800368
369 if (DEBUG) Slog.d(TAG, "animateBounds: target=" + target + " from=" + from + " to=" + to
Winson Chung8bca9e42017-04-16 15:59:43 -0700370 + " schedulePipModeChangedState=" + schedulePipModeChangedState
371 + " replacing=" + replacing);
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800372
Wale Ogunwalece144522016-02-05 22:51:01 -0800373 if (replacing) {
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800374 if (existing.isAnimatingTo(to)) {
Winson Chung19953ca2017-04-11 11:19:23 -0700375 // Just let the current animation complete if it has the same destination as the
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800376 // one we are trying to start.
377 if (DEBUG) Slog.d(TAG, "animateBounds: same destination as existing=" + existing
378 + " ignoring...");
Winson Chung8bca9e42017-04-16 15:59:43 -0700379
Winson Chung19953ca2017-04-11 11:19:23 -0700380 return existing;
Wale Ogunwale5658e4b2016-02-12 12:22:19 -0800381 }
Winson Chung8bca9e42017-04-16 15:59:43 -0700382
383 // Update the PiP callback states if we are replacing the animation
384 if (existing.mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) {
385 if (schedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) {
386 if (DEBUG) Slog.d(TAG, "animateBounds: still animating to fullscreen, keep"
387 + " existing deferred state");
388 } else {
389 if (DEBUG) Slog.d(TAG, "animateBounds: fullscreen animation canceled, callback"
390 + " on start already processed, schedule deferred update on end");
391 schedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END;
392 }
393 } else if (existing.mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_END) {
394 if (schedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) {
395 if (DEBUG) Slog.d(TAG, "animateBounds: non-fullscreen animation canceled,"
396 + " callback on start will be processed");
397 } else {
398 if (DEBUG) Slog.d(TAG, "animateBounds: still animating from fullscreen, keep"
399 + " existing deferred state");
400 schedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END;
401 }
402 }
403
404 // Since we are replacing, we skip both animation start and end callbacks
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800405 existing.cancel();
406 }
Winson Chung8bca9e42017-04-16 15:59:43 -0700407 final BoundsAnimator animator = new BoundsAnimator(target, from, to,
Winson Chunge7ba6862017-05-24 12:13:33 -0700408 schedulePipModeChangedState, moveFromFullscreen, moveToFullscreen, replacing);
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800409 mRunningAnimations.put(target, animator);
410 animator.setFloatValues(0f, 1f);
Wale Ogunwalee75a9ad2016-03-18 20:43:49 -0700411 animator.setDuration((animationDuration != -1 ? animationDuration
Winson Chungbaa7b722017-03-03 21:33:44 -0800412 : DEFAULT_TRANSITION_DURATION) * DEBUG_ANIMATION_SLOW_DOWN_FACTOR);
413 animator.setInterpolator(mFastOutSlowInInterpolator);
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800414 animator.start();
Winson Chung19953ca2017-04-11 11:19:23 -0700415 return animator;
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800416 }
Robert Carrecc06b32017-04-18 14:25:10 -0700417
Winson Chunge7ba6862017-05-24 12:13:33 -0700418 public Handler getHandler() {
419 return mHandler;
420 }
421
422 public void onAllWindowsDrawn() {
423 if (DEBUG) Slog.d(TAG, "onAllWindowsDrawn:");
424 mHandler.post(this::resume);
425 }
426
Robert Carrecc06b32017-04-18 14:25:10 -0700427 private void resume() {
428 for (int i = 0; i < mRunningAnimations.size(); i++) {
429 final BoundsAnimator b = mRunningAnimations.valueAt(i);
430 b.resume();
431 }
432 }
Filip Gruszczynski84fa3352016-01-25 16:28:49 -0800433}