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