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