Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 19 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM; |
| 20 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 21 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 22 | |
| 23 | import android.animation.Animator; |
| 24 | import android.animation.ValueAnimator; |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 26 | import android.graphics.Rect; |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 29 | import android.os.Debug; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 30 | import android.util.ArrayMap; |
| 31 | import android.util.Slog; |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 32 | import android.view.animation.AnimationUtils; |
| 33 | import android.view.animation.Interpolator; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 34 | import android.view.WindowManagerInternal; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 35 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 36 | import com.android.internal.annotations.VisibleForTesting; |
| 37 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Enables animating bounds of objects. |
| 40 | * |
| 41 | * In multi-window world bounds of both stack and tasks can change. When we need these bounds to |
| 42 | * change smoothly and not require the app to relaunch (e.g. because it handles resizes and |
| 43 | * relaunching it would cause poorer experience), these class provides a way to directly animate |
| 44 | * the bounds of the resized object. |
| 45 | * |
| 46 | * The object that is resized needs to implement {@link AnimateBoundsUser} interface. |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 47 | * |
| 48 | * NOTE: All calls to methods in this class should be done on the UI thread |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 49 | */ |
| 50 | public class BoundsAnimationController { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 51 | private static final boolean DEBUG_LOCAL = false; |
| 52 | private static final boolean DEBUG = DEBUG_LOCAL || DEBUG_ANIM; |
| 53 | private static final String TAG = TAG_WITH_CLASS_NAME || DEBUG_LOCAL |
| 54 | ? "BoundsAnimationController" : TAG_WM; |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 55 | private static final int DEBUG_ANIMATION_SLOW_DOWN_FACTOR = 1; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 56 | |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 57 | private static final int DEFAULT_TRANSITION_DURATION = 425; |
| 58 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 59 | // Only accessed on UI thread. |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 60 | private ArrayMap<AnimateBoundsUser, BoundsAnimator> mRunningAnimations = new ArrayMap<>(); |
| 61 | |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 62 | private final class AppTransitionNotifier |
| 63 | extends WindowManagerInternal.AppTransitionListener implements Runnable { |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 64 | |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 65 | public void onAppTransitionCancelledLocked() { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 66 | if (DEBUG) Slog.d(TAG, "onAppTransitionCancelledLocked:" |
| 67 | + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition); |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 68 | animationFinished(); |
| 69 | } |
| 70 | public void onAppTransitionFinishedLocked(IBinder token) { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 71 | if (DEBUG) Slog.d(TAG, "onAppTransitionFinishedLocked:" |
| 72 | + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition); |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 73 | animationFinished(); |
| 74 | } |
| 75 | private void animationFinished() { |
| 76 | if (mFinishAnimationAfterTransition) { |
| 77 | mHandler.removeCallbacks(this); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 78 | // This might end up calling into activity manager which will be bad since we have |
| 79 | // the window manager lock held at this point. Post a message to take care of the |
| 80 | // processing so we don't deadlock. |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 81 | mHandler.post(this); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void run() { |
| 87 | for (int i = 0; i < mRunningAnimations.size(); i++) { |
| 88 | final BoundsAnimator b = mRunningAnimations.valueAt(i); |
| 89 | b.onAnimationEnd(null); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private final Handler mHandler; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 95 | private final AppTransition mAppTransition; |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 96 | private final AppTransitionNotifier mAppTransitionNotifier = new AppTransitionNotifier(); |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 97 | private final Interpolator mFastOutSlowInInterpolator; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 98 | private boolean mFinishAnimationAfterTransition = false; |
| 99 | |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 100 | BoundsAnimationController(Context context, AppTransition transition, Handler handler) { |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 101 | mHandler = handler; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 102 | mAppTransition = transition; |
| 103 | mAppTransition.registerListenerLocked(mAppTransitionNotifier); |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 104 | mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context, |
| 105 | com.android.internal.R.interpolator.fast_out_slow_in); |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 108 | @VisibleForTesting |
| 109 | final class BoundsAnimator extends ValueAnimator |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 110 | implements ValueAnimator.AnimatorUpdateListener, ValueAnimator.AnimatorListener { |
| 111 | private final AnimateBoundsUser mTarget; |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 112 | private final Rect mFrom = new Rect(); |
| 113 | private final Rect mTo = new Rect(); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 114 | private final Rect mTmpRect = new Rect(); |
| 115 | private final Rect mTmpTaskBounds = new Rect(); |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 116 | private final boolean mMoveToFullScreen; |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 117 | // True if this this animation was cancelled and will be replaced the another animation from |
| 118 | // the same {@link #AnimateBoundsUser} target. |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 119 | private boolean mSkipFinalResize; |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 120 | // True if this animation replaced a previous animation of the same |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 121 | // {@link #AnimateBoundsUser} target. |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 122 | private final boolean mSkipAnimationStart; |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 123 | // True if this animation was cancelled by the user, not as a part of a replacing animation |
| 124 | private boolean mSkipAnimationEnd; |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 125 | // True if this animation is not replacing a previous animation, or if the previous |
| 126 | // animation is animating to a different fullscreen state than the current animation. |
| 127 | // We use this to ensure that we always provide a consistent set/order of callbacks when we |
| 128 | // transition to/from PiP. |
| 129 | private final boolean mAnimatingToNewFullscreenState; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 130 | |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 131 | // Depending on whether we are animating from |
| 132 | // a smaller to a larger size |
| 133 | private final int mFrozenTaskWidth; |
| 134 | private final int mFrozenTaskHeight; |
| 135 | |
Winson Chung | 08f8189 | 2017-03-02 15:40:51 -0800 | [diff] [blame] | 136 | BoundsAnimator(AnimateBoundsUser target, Rect from, Rect to, boolean moveToFullScreen, |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 137 | boolean replacingExistingAnimation, boolean animatingToNewFullscreenState) { |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 138 | super(); |
| 139 | mTarget = target; |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 140 | mFrom.set(from); |
| 141 | mTo.set(to); |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 142 | mMoveToFullScreen = moveToFullScreen; |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 143 | mSkipAnimationStart = replacingExistingAnimation; |
| 144 | mAnimatingToNewFullscreenState = animatingToNewFullscreenState; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 145 | addUpdateListener(this); |
| 146 | addListener(this); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 147 | |
| 148 | // If we are animating from smaller to larger, we want to change the task bounds |
| 149 | // to their final size immediately so we can use scaling to make the window |
| 150 | // larger. Likewise if we are going from bigger to smaller, we want to wait until |
| 151 | // the end so we don't have to upscale from the smaller finished size. |
| 152 | if (animatingToLargerSize()) { |
| 153 | mFrozenTaskWidth = mTo.width(); |
| 154 | mFrozenTaskHeight = mTo.height(); |
| 155 | } else { |
| 156 | mFrozenTaskWidth = mFrom.width(); |
| 157 | mFrozenTaskHeight = mFrom.height(); |
| 158 | } |
| 159 | } |
| 160 | |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public void onAnimationStart(Animator animation) { |
| 163 | if (DEBUG) Slog.d(TAG, "onAnimationStart: mTarget=" + mTarget |
| 164 | + " mSkipAnimationStart=" + mSkipAnimationStart); |
| 165 | mFinishAnimationAfterTransition = false; |
| 166 | mTmpRect.set(mFrom.left, mFrom.top, mFrom.left + mFrozenTaskWidth, |
| 167 | mFrom.top + mFrozenTaskHeight); |
| 168 | |
| 169 | // Ensure that we have prepared the target for animation before |
| 170 | // we trigger any size changes, so it can swap surfaces |
| 171 | // in to appropriate modes, or do as it wishes otherwise. |
| 172 | if (!mSkipAnimationStart) { |
| 173 | mTarget.onAnimationStart(mMoveToFullScreen); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 174 | } |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 175 | |
| 176 | // If we are animating to a new fullscreen state (either to/from fullscreen), then |
| 177 | // notify the target of the change with the new frozen task bounds |
Winson Chung | 4790065 | 2017-04-06 18:44:25 -0700 | [diff] [blame] | 178 | if (mAnimatingToNewFullscreenState && mMoveToFullScreen) { |
| 179 | mTarget.updatePictureInPictureMode(null); |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Immediately update the task bounds if they have to become larger, but preserve |
| 183 | // the starting position so we don't jump at the beginning of the animation. |
| 184 | if (animatingToLargerSize()) { |
| 185 | mTarget.setPinnedStackSize(mFrom, mTmpRect); |
| 186 | } |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void onAnimationUpdate(ValueAnimator animation) { |
| 191 | final float value = (Float) animation.getAnimatedValue(); |
| 192 | final float remains = 1 - value; |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 193 | mTmpRect.left = (int) (mFrom.left * remains + mTo.left * value + 0.5f); |
| 194 | mTmpRect.top = (int) (mFrom.top * remains + mTo.top * value + 0.5f); |
| 195 | mTmpRect.right = (int) (mFrom.right * remains + mTo.right * value + 0.5f); |
| 196 | mTmpRect.bottom = (int) (mFrom.bottom * remains + mTo.bottom * value + 0.5f); |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 197 | if (DEBUG) Slog.d(TAG, "animateUpdate: mTarget=" + mTarget + " mBounds=" |
| 198 | + mTmpRect + " from=" + mFrom + " mTo=" + mTo + " value=" + value |
| 199 | + " remains=" + remains); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 200 | |
Robert Carr | c729460 | 2016-05-13 11:32:05 -0700 | [diff] [blame] | 201 | mTmpTaskBounds.set(mTmpRect.left, mTmpRect.top, |
| 202 | mTmpRect.left + mFrozenTaskWidth, mTmpRect.top + mFrozenTaskHeight); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 203 | |
Robert Carr | c729460 | 2016-05-13 11:32:05 -0700 | [diff] [blame] | 204 | if (!mTarget.setPinnedStackSize(mTmpRect, mTmpTaskBounds)) { |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 205 | // Whoops, the target doesn't feel like animating anymore. Let's immediately finish |
| 206 | // any further animation. |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 207 | if (DEBUG) Slog.d(TAG, "animateUpdate: cancelled"); |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 208 | |
| 209 | // Since we are cancelling immediately without a replacement animation, send the |
| 210 | // animation end to maintain callback parity, but also skip any further resizes |
| 211 | prepareCancel(false /* skipAnimationEnd */, true /* skipFinalResize */); |
| 212 | cancel(); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 216 | @Override |
| 217 | public void onAnimationEnd(Animator animation) { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 218 | if (DEBUG) Slog.d(TAG, "onAnimationEnd: mTarget=" + mTarget |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 219 | + " mMoveToFullScreen=" + mMoveToFullScreen |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 220 | + " mSkipFinalResize=" + mSkipFinalResize |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 221 | + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition |
| 222 | + " mAppTransitionIsRunning=" + mAppTransition.isRunning()); |
Jaewan Kim | b003364 | 2016-04-22 18:41:37 +0900 | [diff] [blame] | 223 | |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 224 | // There could be another animation running. For example in the |
| 225 | // move to fullscreen case, recents will also be closing while the |
| 226 | // previous task will be taking its place in the fullscreen stack. |
| 227 | // we have to ensure this is completed before we finish the animation |
| 228 | // and take our place in the fullscreen stack. |
| 229 | if (mAppTransition.isRunning() && !mFinishAnimationAfterTransition) { |
| 230 | mFinishAnimationAfterTransition = true; |
| 231 | return; |
| 232 | } |
| 233 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 234 | if (!mSkipFinalResize) { |
| 235 | // If not cancelled, resize the pinned stack to the final size. All calls to |
| 236 | // setPinnedStackSize() must be done between onAnimationStart() and onAnimationEnd() |
| 237 | mTarget.setPinnedStackSize(mTo, null); |
| 238 | } |
| 239 | |
Robert Carr | c729460 | 2016-05-13 11:32:05 -0700 | [diff] [blame] | 240 | finishAnimation(); |
| 241 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 242 | if (mMoveToFullScreen && !mSkipFinalResize) { |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 243 | mTarget.moveToFullscreen(); |
| 244 | } |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void onAnimationCancel(Animator animation) { |
| 249 | finishAnimation(); |
| 250 | } |
| 251 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 252 | public void prepareCancel(boolean skipAnimationEnd, boolean skipFinalResize) { |
| 253 | if (DEBUG) Slog.d(TAG, "prepareCancel: skipAnimationEnd=" + skipAnimationEnd |
| 254 | + " skipFinalResize=" + skipFinalResize); |
| 255 | mSkipAnimationEnd = skipAnimationEnd; |
| 256 | mSkipFinalResize = skipFinalResize; |
| 257 | } |
| 258 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 259 | @Override |
| 260 | public void cancel() { |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 261 | if (DEBUG) Slog.d(TAG, "cancel: mTarget=" + mTarget); |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 262 | super.cancel(); |
| 263 | } |
| 264 | |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 265 | /** Returns true if the animation target is the same as the input bounds. */ |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 266 | boolean isAnimatingTo(Rect bounds) { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 267 | return mTo.equals(bounds); |
| 268 | } |
| 269 | |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 270 | private boolean animatingToLargerSize() { |
| 271 | if (mFrom.width() * mFrom.height() > mTo.width() * mTo.height()) { |
| 272 | return false; |
| 273 | } |
| 274 | return true; |
| 275 | } |
| 276 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 277 | private void finishAnimation() { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 278 | if (DEBUG) Slog.d(TAG, "finishAnimation: mTarget=" + mTarget |
| 279 | + " callers" + Debug.getCallers(2)); |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 280 | if (!mSkipAnimationEnd) { |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 281 | mTarget.onAnimationEnd(); |
| 282 | } |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 283 | removeListener(this); |
| 284 | removeUpdateListener(this); |
| 285 | mRunningAnimations.remove(mTarget); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void onAnimationRepeat(Animator animation) { |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 290 | // Do nothing |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | public interface AnimateBoundsUser { |
| 295 | /** |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 296 | * Sets the size of the target (without any intermediate steps, like scheduling animation) |
| 297 | * but freezes the bounds of any tasks in the target at taskBounds, |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 298 | * to allow for more flexibility during resizing. Only works for the pinned stack at the |
| 299 | * moment. |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 300 | * |
| 301 | * @return Whether the target should continue to be animated and this call was successful. |
| 302 | * If false, the animation will be cancelled because the user has determined that the |
| 303 | * animation is now invalid and not required. In such a case, the cancel will trigger the |
| 304 | * animation end callback as well, but will not send any further size changes. |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 305 | */ |
| 306 | boolean setPinnedStackSize(Rect bounds, Rect taskBounds); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 307 | |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 308 | /** |
| 309 | * Callback for the target to inform it that the animation has started, so it can do some |
| 310 | * necessary preparation. |
| 311 | */ |
Robert Carr | 7e4c90e | 2017-02-15 19:52:38 -0800 | [diff] [blame] | 312 | void onAnimationStart(boolean toFullscreen); |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 313 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 314 | /** |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 315 | * Callback for the target to inform it that the animation is going to a new fullscreen |
| 316 | * state and should update the picture-in-picture mode accordingly. |
| 317 | * |
| 318 | * @param targetStackBounds the target stack bounds we are animating to, can be null if |
| 319 | * we are animating to fullscreen |
| 320 | */ |
| 321 | void updatePictureInPictureMode(Rect targetStackBounds); |
| 322 | |
| 323 | /** |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 324 | * Callback for the target to inform it that the animation has ended, so it can do some |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 325 | * necessary cleanup. |
| 326 | */ |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 327 | void onAnimationEnd(); |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 328 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 329 | /** |
| 330 | * Callback for the target to inform it to reparent to the fullscreen stack. |
| 331 | */ |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 332 | void moveToFullscreen(); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 335 | public void animateBounds(final AnimateBoundsUser target, Rect from, Rect to, |
| 336 | int animationDuration, boolean moveToFullscreen) { |
| 337 | animateBoundsImpl(target, from, to, animationDuration, moveToFullscreen); |
| 338 | } |
| 339 | |
| 340 | @VisibleForTesting |
| 341 | BoundsAnimator animateBoundsImpl(final AnimateBoundsUser target, Rect from, Rect to, |
| 342 | int animationDuration, boolean moveToFullscreen) { |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 343 | final BoundsAnimator existing = mRunningAnimations.get(target); |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 344 | final boolean replacing = existing != null; |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 345 | final boolean animatingToNewFullscreenState = (existing == null) || |
| 346 | (existing.mMoveToFullScreen != moveToFullscreen); |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 347 | |
| 348 | if (DEBUG) Slog.d(TAG, "animateBounds: target=" + target + " from=" + from + " to=" + to |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 349 | + " moveToFullscreen=" + moveToFullscreen + " replacing=" + replacing |
| 350 | + " animatingToNewFullscreenState=" + animatingToNewFullscreenState); |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 351 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 352 | if (replacing) { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 353 | if (existing.isAnimatingTo(to)) { |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 354 | // Just let the current animation complete if it has the same destination as the |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 355 | // one we are trying to start. |
| 356 | if (DEBUG) Slog.d(TAG, "animateBounds: same destination as existing=" + existing |
| 357 | + " ignoring..."); |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 358 | return existing; |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 359 | } |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 360 | // Since we are replacing, we skip both animation start and end callbacks, and don't |
| 361 | // animate to the final bounds when cancelling |
| 362 | existing.prepareCancel(true /* skipAnimationEnd */, true /* skipFinalResize */); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 363 | existing.cancel(); |
| 364 | } |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 365 | final BoundsAnimator animator = new BoundsAnimator(target, from, to, moveToFullscreen, |
| 366 | replacing, animatingToNewFullscreenState); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 367 | mRunningAnimations.put(target, animator); |
| 368 | animator.setFloatValues(0f, 1f); |
Wale Ogunwale | e75a9ad | 2016-03-18 20:43:49 -0700 | [diff] [blame] | 369 | animator.setDuration((animationDuration != -1 ? animationDuration |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 370 | : DEFAULT_TRANSITION_DURATION) * DEBUG_ANIMATION_SLOW_DOWN_FACTOR); |
| 371 | animator.setInterpolator(mFastOutSlowInInterpolator); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 372 | animator.start(); |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame^] | 373 | return animator; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 374 | } |
| 375 | } |