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 | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 25 | import android.annotation.IntDef; |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 26 | import android.content.Context; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 27 | import android.graphics.Rect; |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 29 | import android.os.IBinder; |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 30 | import android.os.Debug; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 31 | import android.util.ArrayMap; |
| 32 | import android.util.Slog; |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 33 | import android.view.animation.AnimationUtils; |
| 34 | import android.view.animation.Interpolator; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 35 | import android.view.WindowManagerInternal; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 36 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 37 | import com.android.internal.annotations.VisibleForTesting; |
| 38 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
| 41 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 42 | /** |
| 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 Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 50 | * The object that is resized needs to implement {@link BoundsAnimationTarget} interface. |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 51 | * |
| 52 | * 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] | 53 | */ |
| 54 | public class BoundsAnimationController { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 55 | 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 Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 59 | private static final int DEBUG_ANIMATION_SLOW_DOWN_FACTOR = 1; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 60 | |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 61 | private static final int DEFAULT_TRANSITION_DURATION = 425; |
| 62 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 63 | @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 Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 74 | // Only accessed on UI thread. |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 75 | private ArrayMap<BoundsAnimationTarget, BoundsAnimator> mRunningAnimations = new ArrayMap<>(); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 76 | |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 77 | private final class AppTransitionNotifier |
| 78 | extends WindowManagerInternal.AppTransitionListener implements Runnable { |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 79 | |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 80 | public void onAppTransitionCancelledLocked() { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 81 | if (DEBUG) Slog.d(TAG, "onAppTransitionCancelledLocked:" |
| 82 | + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition); |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 83 | animationFinished(); |
| 84 | } |
| 85 | public void onAppTransitionFinishedLocked(IBinder token) { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 86 | if (DEBUG) Slog.d(TAG, "onAppTransitionFinishedLocked:" |
| 87 | + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition); |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 88 | animationFinished(); |
| 89 | } |
| 90 | private void animationFinished() { |
| 91 | if (mFinishAnimationAfterTransition) { |
| 92 | mHandler.removeCallbacks(this); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 93 | // 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 Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 96 | 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 Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 110 | private final AppTransition mAppTransition; |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 111 | private final AppTransitionNotifier mAppTransitionNotifier = new AppTransitionNotifier(); |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 112 | private final Interpolator mFastOutSlowInInterpolator; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 113 | private boolean mFinishAnimationAfterTransition = false; |
| 114 | |
Robert Carr | ecc06b3 | 2017-04-18 14:25:10 -0700 | [diff] [blame^] | 115 | private static final int WAIT_FOR_DRAW_TIMEOUT_MS = 3000; |
| 116 | |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 117 | BoundsAnimationController(Context context, AppTransition transition, Handler handler) { |
Wale Ogunwale | 2ba7129 | 2016-05-05 09:16:47 -0700 | [diff] [blame] | 118 | mHandler = handler; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 119 | mAppTransition = transition; |
| 120 | mAppTransition.registerListenerLocked(mAppTransitionNotifier); |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 121 | mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context, |
| 122 | com.android.internal.R.interpolator.fast_out_slow_in); |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 125 | @VisibleForTesting |
| 126 | final class BoundsAnimator extends ValueAnimator |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 127 | implements ValueAnimator.AnimatorUpdateListener, ValueAnimator.AnimatorListener { |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 128 | private final BoundsAnimationTarget mTarget; |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 129 | private final Rect mFrom = new Rect(); |
| 130 | private final Rect mTo = new Rect(); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 131 | private final Rect mTmpRect = new Rect(); |
| 132 | private final Rect mTmpTaskBounds = new Rect(); |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 133 | |
| 134 | // True if this this animation was canceled and will be replaced the another animation from |
| 135 | // the same {@link #BoundsAnimationTarget} target. |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 136 | private boolean mSkipFinalResize; |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 137 | // True if this animation replaced a previous animation of the same |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 138 | // {@link #BoundsAnimationTarget} target. |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 139 | private final boolean mSkipAnimationStart; |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 140 | // True if this animation was canceled by the user, not as a part of a replacing animation |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 141 | private boolean mSkipAnimationEnd; |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 142 | // True if the animation target should be moved to the fullscreen stack at the end of this |
| 143 | // animation |
| 144 | private boolean mMoveToFullscreen; |
| 145 | |
| 146 | // Whether to schedule PiP mode changes on animation start/end |
| 147 | private @SchedulePipModeChangedState int mSchedulePipModeChangedState; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 148 | |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 149 | // Depending on whether we are animating from |
| 150 | // a smaller to a larger size |
| 151 | private final int mFrozenTaskWidth; |
| 152 | private final int mFrozenTaskHeight; |
| 153 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 154 | BoundsAnimator(BoundsAnimationTarget target, Rect from, Rect to, |
| 155 | @SchedulePipModeChangedState int schedulePipModeChangedState, |
| 156 | boolean moveToFullscreen, boolean replacingExistingAnimation) { |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 157 | super(); |
| 158 | mTarget = target; |
Winson Chung | 84a3834 | 2016-11-08 16:15:10 -0800 | [diff] [blame] | 159 | mFrom.set(from); |
| 160 | mTo.set(to); |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 161 | mSkipAnimationStart = replacingExistingAnimation; |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 162 | mSchedulePipModeChangedState = schedulePipModeChangedState; |
| 163 | mMoveToFullscreen = moveToFullscreen; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 164 | addUpdateListener(this); |
| 165 | addListener(this); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 166 | |
| 167 | // If we are animating from smaller to larger, we want to change the task bounds |
| 168 | // to their final size immediately so we can use scaling to make the window |
| 169 | // larger. Likewise if we are going from bigger to smaller, we want to wait until |
| 170 | // the end so we don't have to upscale from the smaller finished size. |
| 171 | if (animatingToLargerSize()) { |
| 172 | mFrozenTaskWidth = mTo.width(); |
| 173 | mFrozenTaskHeight = mTo.height(); |
| 174 | } else { |
| 175 | mFrozenTaskWidth = mFrom.width(); |
| 176 | mFrozenTaskHeight = mFrom.height(); |
| 177 | } |
| 178 | } |
| 179 | |
Robert Carr | ecc06b3 | 2017-04-18 14:25:10 -0700 | [diff] [blame^] | 180 | final Runnable mResumeRunnable = new Runnable() { |
| 181 | @Override |
| 182 | public void run() { |
| 183 | resume(); |
| 184 | } |
| 185 | }; |
| 186 | |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 187 | @Override |
| 188 | public void onAnimationStart(Animator animation) { |
| 189 | if (DEBUG) Slog.d(TAG, "onAnimationStart: mTarget=" + mTarget |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 190 | + " mSkipAnimationStart=" + mSkipAnimationStart |
| 191 | + " mSchedulePipModeChangedState=" + mSchedulePipModeChangedState); |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 192 | mFinishAnimationAfterTransition = false; |
| 193 | mTmpRect.set(mFrom.left, mFrom.top, mFrom.left + mFrozenTaskWidth, |
| 194 | mFrom.top + mFrozenTaskHeight); |
| 195 | |
| 196 | // Ensure that we have prepared the target for animation before |
| 197 | // we trigger any size changes, so it can swap surfaces |
| 198 | // in to appropriate modes, or do as it wishes otherwise. |
| 199 | if (!mSkipAnimationStart) { |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 200 | mTarget.onAnimationStart(mSchedulePipModeChangedState == |
| 201 | SCHEDULE_PIP_MODE_CHANGED_ON_START); |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Immediately update the task bounds if they have to become larger, but preserve |
| 205 | // the starting position so we don't jump at the beginning of the animation. |
| 206 | if (animatingToLargerSize()) { |
| 207 | mTarget.setPinnedStackSize(mFrom, mTmpRect); |
Robert Carr | ecc06b3 | 2017-04-18 14:25:10 -0700 | [diff] [blame^] | 208 | |
| 209 | // We pause the animation until the app has drawn at the new size. |
| 210 | // The target will notify us via BoundsAnimationController#resume. |
| 211 | // We do this here and pause the animation, rather than just defer starting it |
| 212 | // so we can enter the animating state and have WindowStateAnimator apply the |
| 213 | // correct logic to make this resize seamless. |
| 214 | if (mMoveToFullscreen) { |
| 215 | pause(); |
| 216 | mHandler.postDelayed(mResumeRunnable, WAIT_FOR_DRAW_TIMEOUT_MS); |
| 217 | } |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 218 | } |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
Robert Carr | ecc06b3 | 2017-04-18 14:25:10 -0700 | [diff] [blame^] | 222 | public void resume() { |
| 223 | mHandler.removeCallbacks(mResumeRunnable); |
| 224 | super.resume(); |
| 225 | } |
| 226 | |
| 227 | @Override |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 228 | public void onAnimationUpdate(ValueAnimator animation) { |
| 229 | final float value = (Float) animation.getAnimatedValue(); |
| 230 | final float remains = 1 - value; |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 231 | mTmpRect.left = (int) (mFrom.left * remains + mTo.left * value + 0.5f); |
| 232 | mTmpRect.top = (int) (mFrom.top * remains + mTo.top * value + 0.5f); |
| 233 | mTmpRect.right = (int) (mFrom.right * remains + mTo.right * value + 0.5f); |
| 234 | mTmpRect.bottom = (int) (mFrom.bottom * remains + mTo.bottom * value + 0.5f); |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 235 | if (DEBUG) Slog.d(TAG, "animateUpdate: mTarget=" + mTarget + " mBounds=" |
| 236 | + mTmpRect + " from=" + mFrom + " mTo=" + mTo + " value=" + value |
| 237 | + " remains=" + remains); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 238 | |
Robert Carr | c729460 | 2016-05-13 11:32:05 -0700 | [diff] [blame] | 239 | mTmpTaskBounds.set(mTmpRect.left, mTmpRect.top, |
| 240 | mTmpRect.left + mFrozenTaskWidth, mTmpRect.top + mFrozenTaskHeight); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 241 | |
Robert Carr | c729460 | 2016-05-13 11:32:05 -0700 | [diff] [blame] | 242 | if (!mTarget.setPinnedStackSize(mTmpRect, mTmpTaskBounds)) { |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 243 | // Whoops, the target doesn't feel like animating anymore. Let's immediately finish |
| 244 | // any further animation. |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 245 | if (DEBUG) Slog.d(TAG, "animateUpdate: cancelled"); |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 246 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 247 | // If we have already scheduled a PiP mode changed at the start of the animation, |
| 248 | // then we need to clean up and schedule one at the end, since we have canceled the |
| 249 | // animation to the final state. |
| 250 | if (mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) { |
| 251 | mSchedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END; |
| 252 | } |
| 253 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 254 | // Since we are cancelling immediately without a replacement animation, send the |
| 255 | // animation end to maintain callback parity, but also skip any further resizes |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 256 | cancelAndCallAnimationEnd(); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 260 | @Override |
| 261 | public void onAnimationEnd(Animator animation) { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 262 | if (DEBUG) Slog.d(TAG, "onAnimationEnd: mTarget=" + mTarget |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 263 | + " mSkipFinalResize=" + mSkipFinalResize |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 264 | + " mFinishAnimationAfterTransition=" + mFinishAnimationAfterTransition |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 265 | + " mAppTransitionIsRunning=" + mAppTransition.isRunning() |
| 266 | + " callers=" + Debug.getCallers(2)); |
Jaewan Kim | b003364 | 2016-04-22 18:41:37 +0900 | [diff] [blame] | 267 | |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame] | 268 | // There could be another animation running. For example in the |
| 269 | // move to fullscreen case, recents will also be closing while the |
| 270 | // previous task will be taking its place in the fullscreen stack. |
| 271 | // we have to ensure this is completed before we finish the animation |
| 272 | // and take our place in the fullscreen stack. |
| 273 | if (mAppTransition.isRunning() && !mFinishAnimationAfterTransition) { |
| 274 | mFinishAnimationAfterTransition = true; |
| 275 | return; |
| 276 | } |
| 277 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 278 | if (!mSkipAnimationEnd) { |
| 279 | // If this animation has already scheduled the picture-in-picture mode on start, and |
| 280 | // we are not skipping the final resize due to being canceled, then move the PiP to |
| 281 | // fullscreen once the animation ends |
| 282 | if (DEBUG) Slog.d(TAG, "onAnimationEnd: mTarget=" + mTarget |
| 283 | + " moveToFullscreen=" + mMoveToFullscreen); |
| 284 | mTarget.onAnimationEnd(mSchedulePipModeChangedState == |
| 285 | SCHEDULE_PIP_MODE_CHANGED_ON_END, !mSkipFinalResize ? mTo : null, |
| 286 | mMoveToFullscreen); |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 289 | // Clean up this animation |
| 290 | removeListener(this); |
| 291 | removeUpdateListener(this); |
| 292 | mRunningAnimations.remove(mTarget); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void onAnimationCancel(Animator animation) { |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 297 | // Always skip the final resize when the animation is canceled |
| 298 | mSkipFinalResize = true; |
| 299 | mMoveToFullscreen = false; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 302 | private void cancelAndCallAnimationEnd() { |
| 303 | if (DEBUG) Slog.d(TAG, "cancelAndCallAnimationEnd: mTarget=" + mTarget); |
| 304 | mSkipAnimationEnd = false; |
| 305 | super.cancel(); |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 308 | @Override |
| 309 | public void cancel() { |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 310 | if (DEBUG) Slog.d(TAG, "cancel: mTarget=" + mTarget); |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 311 | mSkipAnimationEnd = true; |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 312 | super.cancel(); |
| 313 | } |
| 314 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 315 | /** |
| 316 | * @return true if the animation target is the same as the input bounds. |
| 317 | */ |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 318 | boolean isAnimatingTo(Rect bounds) { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 319 | return mTo.equals(bounds); |
| 320 | } |
| 321 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 322 | /** |
| 323 | * @return true if we are animating to a larger surface size |
| 324 | */ |
| 325 | @VisibleForTesting |
| 326 | boolean animatingToLargerSize() { |
| 327 | // TODO: Fix this check for aspect ratio changes |
| 328 | return (mFrom.width() * mFrom.height() <= mTo.width() * mTo.height()); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void onAnimationRepeat(Animator animation) { |
Winson Chung | 5af42fc | 2017-03-24 17:11:33 -0700 | [diff] [blame] | 333 | // Do nothing |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 337 | public void animateBounds(final BoundsAnimationTarget target, Rect from, Rect to, |
| 338 | int animationDuration, @SchedulePipModeChangedState int schedulePipModeChangedState, |
| 339 | boolean moveToFullscreen) { |
| 340 | animateBoundsImpl(target, from, to, animationDuration, schedulePipModeChangedState, |
| 341 | moveToFullscreen); |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | @VisibleForTesting |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 345 | BoundsAnimator animateBoundsImpl(final BoundsAnimationTarget target, Rect from, Rect to, |
| 346 | int animationDuration, @SchedulePipModeChangedState int schedulePipModeChangedState, |
| 347 | boolean moveToFullscreen) { |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 348 | final BoundsAnimator existing = mRunningAnimations.get(target); |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 349 | final boolean replacing = existing != null; |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 350 | |
| 351 | if (DEBUG) Slog.d(TAG, "animateBounds: target=" + target + " from=" + from + " to=" + to |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 352 | + " schedulePipModeChangedState=" + schedulePipModeChangedState |
| 353 | + " replacing=" + replacing); |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 354 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 355 | if (replacing) { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 356 | if (existing.isAnimatingTo(to)) { |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 357 | // 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] | 358 | // one we are trying to start. |
| 359 | if (DEBUG) Slog.d(TAG, "animateBounds: same destination as existing=" + existing |
| 360 | + " ignoring..."); |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 361 | |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 362 | return existing; |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 363 | } |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 364 | |
| 365 | // Update the PiP callback states if we are replacing the animation |
| 366 | if (existing.mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) { |
| 367 | if (schedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) { |
| 368 | if (DEBUG) Slog.d(TAG, "animateBounds: still animating to fullscreen, keep" |
| 369 | + " existing deferred state"); |
| 370 | } else { |
| 371 | if (DEBUG) Slog.d(TAG, "animateBounds: fullscreen animation canceled, callback" |
| 372 | + " on start already processed, schedule deferred update on end"); |
| 373 | schedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END; |
| 374 | } |
| 375 | } else if (existing.mSchedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_END) { |
| 376 | if (schedulePipModeChangedState == SCHEDULE_PIP_MODE_CHANGED_ON_START) { |
| 377 | if (DEBUG) Slog.d(TAG, "animateBounds: non-fullscreen animation canceled," |
| 378 | + " callback on start will be processed"); |
| 379 | } else { |
| 380 | if (DEBUG) Slog.d(TAG, "animateBounds: still animating from fullscreen, keep" |
| 381 | + " existing deferred state"); |
| 382 | schedulePipModeChangedState = SCHEDULE_PIP_MODE_CHANGED_ON_END; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // Since we are replacing, we skip both animation start and end callbacks |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 387 | existing.cancel(); |
| 388 | } |
Winson Chung | 8bca9e4 | 2017-04-16 15:59:43 -0700 | [diff] [blame] | 389 | final BoundsAnimator animator = new BoundsAnimator(target, from, to, |
| 390 | schedulePipModeChangedState, moveToFullscreen, replacing); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 391 | mRunningAnimations.put(target, animator); |
| 392 | animator.setFloatValues(0f, 1f); |
Wale Ogunwale | e75a9ad | 2016-03-18 20:43:49 -0700 | [diff] [blame] | 393 | animator.setDuration((animationDuration != -1 ? animationDuration |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 394 | : DEFAULT_TRANSITION_DURATION) * DEBUG_ANIMATION_SLOW_DOWN_FACTOR); |
| 395 | animator.setInterpolator(mFastOutSlowInInterpolator); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 396 | animator.start(); |
Winson Chung | 19953ca | 2017-04-11 11:19:23 -0700 | [diff] [blame] | 397 | return animator; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 398 | } |
Robert Carr | ecc06b3 | 2017-04-18 14:25:10 -0700 | [diff] [blame^] | 399 | |
| 400 | private void resume() { |
| 401 | for (int i = 0; i < mRunningAnimations.size(); i++) { |
| 402 | final BoundsAnimator b = mRunningAnimations.valueAt(i); |
| 403 | b.resume(); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | public void onAllWindowsDrawn() { |
| 408 | mHandler.post(this::resume); |
| 409 | } |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 410 | } |