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 | |
| 19 | import static com.android.server.wm.AppTransition.DEFAULT_APP_TRANSITION_DURATION; |
| 20 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM; |
| 21 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 22 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 23 | |
| 24 | import android.animation.Animator; |
| 25 | import android.animation.ValueAnimator; |
| 26 | import android.graphics.Rect; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame^] | 27 | import android.os.IBinder; |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 28 | import android.os.Debug; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 29 | import android.util.ArrayMap; |
| 30 | import android.util.Slog; |
| 31 | import android.view.animation.LinearInterpolator; |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame^] | 32 | import android.view.WindowManagerInternal; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Enables animating bounds of objects. |
| 36 | * |
| 37 | * In multi-window world bounds of both stack and tasks can change. When we need these bounds to |
| 38 | * change smoothly and not require the app to relaunch (e.g. because it handles resizes and |
| 39 | * relaunching it would cause poorer experience), these class provides a way to directly animate |
| 40 | * the bounds of the resized object. |
| 41 | * |
| 42 | * The object that is resized needs to implement {@link AnimateBoundsUser} interface. |
| 43 | */ |
| 44 | public class BoundsAnimationController { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 45 | private static final boolean DEBUG_LOCAL = false; |
| 46 | private static final boolean DEBUG = DEBUG_LOCAL || DEBUG_ANIM; |
| 47 | private static final String TAG = TAG_WITH_CLASS_NAME || DEBUG_LOCAL |
| 48 | ? "BoundsAnimationController" : TAG_WM; |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 49 | private static final int DEBUG_ANIMATION_SLOW_DOWN_FACTOR = 1; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 50 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 51 | // Only accessed on UI thread. |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 52 | private ArrayMap<AnimateBoundsUser, BoundsAnimator> mRunningAnimations = new ArrayMap<>(); |
| 53 | |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame^] | 54 | private final WindowManagerInternal.AppTransitionListener mAppTransitionNotifier |
| 55 | = new WindowManagerInternal.AppTransitionListener() { |
| 56 | public void onAppTransitionCancelledLocked() { |
| 57 | animationFinished(); |
| 58 | } |
| 59 | public void onAppTransitionFinishedLocked(IBinder token) { |
| 60 | animationFinished(); |
| 61 | } |
| 62 | private void animationFinished() { |
| 63 | if (mFinishAnimationAfterTransition) { |
| 64 | for (int i = 0; i < mRunningAnimations.size(); i++) { |
| 65 | BoundsAnimator b = mRunningAnimations.valueAt(i); |
| 66 | b.onAnimationEnd(null); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | private final AppTransition mAppTransition; |
| 73 | private boolean mFinishAnimationAfterTransition = false; |
| 74 | |
| 75 | BoundsAnimationController(AppTransition transition) { |
| 76 | mAppTransition = transition; |
| 77 | mAppTransition.registerListenerLocked(mAppTransitionNotifier); |
| 78 | } |
| 79 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 80 | private final class BoundsAnimator extends ValueAnimator |
| 81 | implements ValueAnimator.AnimatorUpdateListener, ValueAnimator.AnimatorListener { |
| 82 | private final AnimateBoundsUser mTarget; |
| 83 | private final Rect mFrom; |
| 84 | private final Rect mTo; |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 85 | private final Rect mTmpRect = new Rect(); |
| 86 | private final Rect mTmpTaskBounds = new Rect(); |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 87 | private final boolean mMoveToFullScreen; |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 88 | // True if this this animation was cancelled and will be replaced the another animation from |
| 89 | // the same {@link #AnimateBoundsUser} target. |
| 90 | private boolean mWillReplace; |
| 91 | // True to true if this animation replaced a previous animation of the same |
| 92 | // {@link #AnimateBoundsUser} target. |
| 93 | private final boolean mReplacement; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 94 | |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 95 | // Depending on whether we are animating from |
| 96 | // a smaller to a larger size |
| 97 | private final int mFrozenTaskWidth; |
| 98 | private final int mFrozenTaskHeight; |
| 99 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 100 | BoundsAnimator(AnimateBoundsUser target, Rect from, Rect to, |
| 101 | boolean moveToFullScreen, boolean replacement) { |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 102 | super(); |
| 103 | mTarget = target; |
| 104 | mFrom = from; |
| 105 | mTo = to; |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 106 | mMoveToFullScreen = moveToFullScreen; |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 107 | mReplacement = replacement; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 108 | addUpdateListener(this); |
| 109 | addListener(this); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 110 | |
| 111 | // If we are animating from smaller to larger, we want to change the task bounds |
| 112 | // to their final size immediately so we can use scaling to make the window |
| 113 | // larger. Likewise if we are going from bigger to smaller, we want to wait until |
| 114 | // the end so we don't have to upscale from the smaller finished size. |
| 115 | if (animatingToLargerSize()) { |
| 116 | mFrozenTaskWidth = mTo.width(); |
| 117 | mFrozenTaskHeight = mTo.height(); |
| 118 | } else { |
| 119 | mFrozenTaskWidth = mFrom.width(); |
| 120 | mFrozenTaskHeight = mFrom.height(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | boolean animatingToLargerSize() { |
| 125 | if (mFrom.width() * mFrom.height() > mTo.width() * mTo.height()) { |
| 126 | return false; |
| 127 | } |
| 128 | return true; |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onAnimationUpdate(ValueAnimator animation) { |
| 133 | final float value = (Float) animation.getAnimatedValue(); |
| 134 | final float remains = 1 - value; |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 135 | mTmpRect.left = (int) (mFrom.left * remains + mTo.left * value + 0.5f); |
| 136 | mTmpRect.top = (int) (mFrom.top * remains + mTo.top * value + 0.5f); |
| 137 | mTmpRect.right = (int) (mFrom.right * remains + mTo.right * value + 0.5f); |
| 138 | mTmpRect.bottom = (int) (mFrom.bottom * remains + mTo.bottom * value + 0.5f); |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 139 | if (DEBUG) Slog.d(TAG, "animateUpdate: mTarget=" + mTarget + " mBounds=" |
| 140 | + mTmpRect + " from=" + mFrom + " mTo=" + mTo + " value=" + value |
| 141 | + " remains=" + remains); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 142 | |
| 143 | if (remains != 0) { |
| 144 | mTmpTaskBounds.set(mTmpRect.left, mTmpRect.top, |
| 145 | mTmpRect.left + mFrozenTaskWidth, mTmpRect.top + mFrozenTaskHeight); |
| 146 | } |
| 147 | |
| 148 | if (!mTarget.setPinnedStackSize(mTmpRect, remains != 0 ? mTmpTaskBounds : null)) { |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 149 | // Whoops, the target doesn't feel like animating anymore. Let's immediately finish |
| 150 | // any further animation. |
| 151 | animation.cancel(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | @Override |
| 157 | public void onAnimationStart(Animator animation) { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 158 | if (DEBUG) Slog.d(TAG, "onAnimationStart: mTarget=" + mTarget |
| 159 | + " mReplacement=" + mReplacement); |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame^] | 160 | mFinishAnimationAfterTransition = false; |
Robert Carr | 69016e3 | 2016-04-18 11:52:21 -0700 | [diff] [blame] | 161 | // Ensure that we have prepared the target for animation before |
| 162 | // we trigger any size changes, so it can swap surfaces |
| 163 | // in to appropriate modes, or do as it wishes otherwise. |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 164 | if (!mReplacement) { |
| 165 | mTarget.onAnimationStart(); |
| 166 | } |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 167 | |
Robert Carr | 69016e3 | 2016-04-18 11:52:21 -0700 | [diff] [blame] | 168 | // Immediately update the task bounds if they have to become larger, but preserve |
| 169 | // the starting position so we don't jump at the beginning of the animation. |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 170 | if (animatingToLargerSize()) { |
Robert Carr | 69016e3 | 2016-04-18 11:52:21 -0700 | [diff] [blame] | 171 | mTmpRect.set(mFrom.left, mFrom.top, |
| 172 | mFrom.left + mFrozenTaskWidth, mFrom.top + mFrozenTaskHeight); |
| 173 | mTarget.setPinnedStackSize(mFrom, mTmpRect); |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 174 | } |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void onAnimationEnd(Animator animation) { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 179 | if (DEBUG) Slog.d(TAG, "onAnimationEnd: mTarget=" + mTarget |
| 180 | + " mMoveToFullScreen=" + mMoveToFullScreen + " mWillReplace=" + mWillReplace); |
Jaewan Kim | b003364 | 2016-04-22 18:41:37 +0900 | [diff] [blame] | 181 | |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame^] | 182 | // There could be another animation running. For example in the |
| 183 | // move to fullscreen case, recents will also be closing while the |
| 184 | // previous task will be taking its place in the fullscreen stack. |
| 185 | // we have to ensure this is completed before we finish the animation |
| 186 | // and take our place in the fullscreen stack. |
| 187 | if (mAppTransition.isRunning() && !mFinishAnimationAfterTransition) { |
| 188 | mFinishAnimationAfterTransition = true; |
| 189 | return; |
| 190 | } |
| 191 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 192 | if (mMoveToFullScreen && !mWillReplace) { |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 193 | mTarget.moveToFullscreen(); |
| 194 | } |
Robert Carr | f9aa2a9 | 2016-04-26 14:22:19 -0700 | [diff] [blame^] | 195 | |
| 196 | finishAnimation(); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void onAnimationCancel(Animator animation) { |
| 201 | finishAnimation(); |
| 202 | } |
| 203 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 204 | @Override |
| 205 | public void cancel() { |
| 206 | mWillReplace = true; |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 207 | if (DEBUG) Slog.d(TAG, "cancel: willReplace mTarget=" + mTarget); |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 208 | super.cancel(); |
| 209 | } |
| 210 | |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 211 | /** Returns true if the animation target is the same as the input bounds. */ |
| 212 | public boolean isAnimatingTo(Rect bounds) { |
| 213 | return mTo.equals(bounds); |
| 214 | } |
| 215 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 216 | private void finishAnimation() { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 217 | if (DEBUG) Slog.d(TAG, "finishAnimation: mTarget=" + mTarget |
| 218 | + " callers" + Debug.getCallers(2)); |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 219 | if (!mWillReplace) { |
| 220 | mTarget.onAnimationEnd(); |
| 221 | } |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 222 | removeListener(this); |
| 223 | removeUpdateListener(this); |
| 224 | mRunningAnimations.remove(mTarget); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void onAnimationRepeat(Animator animation) { |
| 229 | |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | public interface AnimateBoundsUser { |
| 234 | /** |
| 235 | * Asks the target to directly (without any intermediate steps, like scheduling animation) |
| 236 | * resize its bounds. |
| 237 | * |
| 238 | * @return Whether the target still wants to be animated and successfully finished the |
| 239 | * operation. If it returns false, the animation will immediately be cancelled. The target |
| 240 | * should return false when something abnormal happened, e.g. it was completely removed |
| 241 | * from the hierarchy and is not valid anymore. |
| 242 | */ |
| 243 | boolean setSize(Rect bounds); |
Robert Carr | 0d00c2e | 2016-02-29 17:45:02 -0800 | [diff] [blame] | 244 | /** |
| 245 | * Behaves as setSize, but freezes the bounds of any tasks in the target at taskBounds, |
| 246 | * to allow for more flexibility during resizing. Only |
| 247 | * works for the pinned stack at the moment. |
| 248 | */ |
| 249 | boolean setPinnedStackSize(Rect bounds, Rect taskBounds); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 250 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 251 | void onAnimationStart(); |
| 252 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 253 | /** |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 254 | * 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] | 255 | * necessary cleanup. |
| 256 | */ |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 257 | void onAnimationEnd(); |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 258 | |
| 259 | void moveToFullscreen(); |
| 260 | |
| 261 | void getFullScreenBounds(Rect bounds); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Wale Ogunwale | e75a9ad | 2016-03-18 20:43:49 -0700 | [diff] [blame] | 264 | void animateBounds(final AnimateBoundsUser target, Rect from, Rect to, int animationDuration) { |
Filip Gruszczynski | c17d8b7 | 2016-02-03 16:52:59 -0800 | [diff] [blame] | 265 | boolean moveToFullscreen = false; |
| 266 | if (to == null) { |
| 267 | to = new Rect(); |
| 268 | target.getFullScreenBounds(to); |
| 269 | moveToFullscreen = true; |
| 270 | } |
| 271 | |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 272 | final BoundsAnimator existing = mRunningAnimations.get(target); |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 273 | final boolean replacing = existing != null; |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 274 | |
| 275 | if (DEBUG) Slog.d(TAG, "animateBounds: target=" + target + " from=" + from + " to=" + to |
| 276 | + " moveToFullscreen=" + moveToFullscreen + " replacing=" + replacing); |
| 277 | |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 278 | if (replacing) { |
Wale Ogunwale | 5658e4b | 2016-02-12 12:22:19 -0800 | [diff] [blame] | 279 | if (existing.isAnimatingTo(to)) { |
| 280 | // Just les the current animation complete if it has the same destination as the |
| 281 | // one we are trying to start. |
| 282 | if (DEBUG) Slog.d(TAG, "animateBounds: same destination as existing=" + existing |
| 283 | + " ignoring..."); |
| 284 | return; |
| 285 | } |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 286 | existing.cancel(); |
| 287 | } |
Wale Ogunwale | ce14452 | 2016-02-05 22:51:01 -0800 | [diff] [blame] | 288 | final BoundsAnimator animator = |
| 289 | new BoundsAnimator(target, from, to, moveToFullscreen, replacing); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 290 | mRunningAnimations.put(target, animator); |
| 291 | animator.setFloatValues(0f, 1f); |
Wale Ogunwale | e75a9ad | 2016-03-18 20:43:49 -0700 | [diff] [blame] | 292 | animator.setDuration((animationDuration != -1 ? animationDuration |
| 293 | : DEFAULT_APP_TRANSITION_DURATION) * DEBUG_ANIMATION_SLOW_DOWN_FACTOR); |
Filip Gruszczynski | 84fa335 | 2016-01-25 16:28:49 -0800 | [diff] [blame] | 294 | animator.setInterpolator(new LinearInterpolator()); |
| 295 | animator.start(); |
| 296 | } |
| 297 | } |