Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.systemui.pip; |
| 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.ValueAnimator; |
| 21 | import android.annotation.IntDef; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.graphics.Rect; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 24 | import android.view.SurfaceControl; |
| 25 | import android.view.animation.AnimationUtils; |
| 26 | import android.view.animation.Interpolator; |
| 27 | |
| 28 | import com.android.internal.annotations.VisibleForTesting; |
| 29 | |
| 30 | import java.lang.annotation.Retention; |
| 31 | import java.lang.annotation.RetentionPolicy; |
| 32 | |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 33 | import javax.inject.Inject; |
| 34 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Controller class of PiP animations (both from and to PiP mode). |
| 37 | */ |
| 38 | public class PipAnimationController { |
| 39 | private static final float FRACTION_START = 0f; |
| 40 | private static final float FRACTION_END = 1f; |
| 41 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 42 | public static final int ANIM_TYPE_BOUNDS = 0; |
| 43 | public static final int ANIM_TYPE_ALPHA = 1; |
| 44 | |
| 45 | @IntDef(prefix = { "ANIM_TYPE_" }, value = { |
| 46 | ANIM_TYPE_BOUNDS, |
| 47 | ANIM_TYPE_ALPHA |
| 48 | }) |
| 49 | @Retention(RetentionPolicy.SOURCE) |
| 50 | public @interface AnimationType {} |
| 51 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 52 | static final int TRANSITION_DIRECTION_NONE = 0; |
| 53 | static final int TRANSITION_DIRECTION_SAME = 1; |
| 54 | static final int TRANSITION_DIRECTION_TO_PIP = 2; |
| 55 | static final int TRANSITION_DIRECTION_TO_FULLSCREEN = 3; |
| 56 | |
| 57 | @IntDef(prefix = { "TRANSITION_DIRECTION_" }, value = { |
| 58 | TRANSITION_DIRECTION_NONE, |
| 59 | TRANSITION_DIRECTION_SAME, |
| 60 | TRANSITION_DIRECTION_TO_PIP, |
| 61 | TRANSITION_DIRECTION_TO_FULLSCREEN |
| 62 | }) |
| 63 | @Retention(RetentionPolicy.SOURCE) |
| 64 | @interface TransitionDirection {} |
| 65 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 66 | private final Interpolator mFastOutSlowInInterpolator; |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 67 | private final PipSurfaceTransactionHelper mSurfaceTransactionHelper; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 68 | |
| 69 | private PipTransitionAnimator mCurrentAnimator; |
| 70 | |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 71 | @Inject |
| 72 | PipAnimationController(Context context, PipSurfaceTransactionHelper helper) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 73 | mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context, |
| 74 | com.android.internal.R.interpolator.fast_out_slow_in); |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 75 | mSurfaceTransactionHelper = helper; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 78 | @SuppressWarnings("unchecked") |
| 79 | PipTransitionAnimator getAnimator(SurfaceControl leash, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 80 | Rect destinationBounds, float alphaStart, float alphaEnd) { |
| 81 | if (mCurrentAnimator == null) { |
| 82 | mCurrentAnimator = setupPipTransitionAnimator( |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 83 | PipTransitionAnimator.ofAlpha(leash, destinationBounds, alphaStart, alphaEnd)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 84 | } else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_ALPHA |
| 85 | && mCurrentAnimator.isRunning()) { |
| 86 | mCurrentAnimator.updateEndValue(alphaEnd); |
| 87 | } else { |
| 88 | mCurrentAnimator.cancel(); |
| 89 | mCurrentAnimator = setupPipTransitionAnimator( |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 90 | PipTransitionAnimator.ofAlpha(leash, destinationBounds, alphaStart, alphaEnd)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 91 | } |
| 92 | return mCurrentAnimator; |
| 93 | } |
| 94 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 95 | @SuppressWarnings("unchecked") |
| 96 | PipTransitionAnimator getAnimator(SurfaceControl leash, Rect startBounds, Rect endBounds) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 97 | if (mCurrentAnimator == null) { |
| 98 | mCurrentAnimator = setupPipTransitionAnimator( |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 99 | PipTransitionAnimator.ofBounds(leash, startBounds, endBounds)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 100 | } else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_BOUNDS |
| 101 | && mCurrentAnimator.isRunning()) { |
| 102 | mCurrentAnimator.setDestinationBounds(endBounds); |
| 103 | // construct new Rect instances in case they are recycled |
| 104 | mCurrentAnimator.updateEndValue(new Rect(endBounds)); |
| 105 | } else { |
| 106 | mCurrentAnimator.cancel(); |
| 107 | mCurrentAnimator = setupPipTransitionAnimator( |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 108 | PipTransitionAnimator.ofBounds(leash, startBounds, endBounds)); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 109 | } |
| 110 | return mCurrentAnimator; |
| 111 | } |
| 112 | |
Hongwei Wang | ea2ae85 | 2020-02-27 13:47:52 -0800 | [diff] [blame] | 113 | PipTransitionAnimator getCurrentAnimator() { |
| 114 | return mCurrentAnimator; |
| 115 | } |
| 116 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 117 | private PipTransitionAnimator setupPipTransitionAnimator(PipTransitionAnimator animator) { |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 118 | animator.setSurfaceTransactionHelper(mSurfaceTransactionHelper); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 119 | animator.setInterpolator(mFastOutSlowInInterpolator); |
| 120 | animator.setFloatValues(FRACTION_START, FRACTION_END); |
| 121 | return animator; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Additional callback interface for PiP animation |
| 126 | */ |
| 127 | public static class PipAnimationCallback { |
| 128 | /** |
| 129 | * Called when PiP animation is started. |
| 130 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 131 | public void onPipAnimationStart(PipTransitionAnimator animator) {} |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Called when PiP animation is ended. |
| 135 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 136 | public void onPipAnimationEnd(SurfaceControl.Transaction tx, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 137 | PipTransitionAnimator animator) {} |
| 138 | |
| 139 | /** |
| 140 | * Called when PiP animation is cancelled. |
| 141 | */ |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 142 | public void onPipAnimationCancel(PipTransitionAnimator animator) {} |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Animator for PiP transition animation which supports both alpha and bounds animation. |
| 147 | * @param <T> Type of property to animate, either alpha (float) or bounds (Rect) |
| 148 | */ |
| 149 | public abstract static class PipTransitionAnimator<T> extends ValueAnimator implements |
| 150 | ValueAnimator.AnimatorUpdateListener, |
| 151 | ValueAnimator.AnimatorListener { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 152 | private final SurfaceControl mLeash; |
| 153 | private final @AnimationType int mAnimationType; |
| 154 | private final Rect mDestinationBounds = new Rect(); |
| 155 | |
| 156 | private T mStartValue; |
| 157 | private T mEndValue; |
| 158 | private T mCurrentValue; |
| 159 | private PipAnimationCallback mPipAnimationCallback; |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 160 | private PipSurfaceTransactionHelper.SurfaceControlTransactionFactory |
| 161 | mSurfaceControlTransactionFactory; |
| 162 | private PipSurfaceTransactionHelper mSurfaceTransactionHelper; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 163 | private @TransitionDirection int mTransitionDirection; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 164 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 165 | private PipTransitionAnimator(SurfaceControl leash, @AnimationType int animationType, |
| 166 | Rect destinationBounds, T startValue, T endValue) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 167 | mLeash = leash; |
| 168 | mAnimationType = animationType; |
| 169 | mDestinationBounds.set(destinationBounds); |
| 170 | mStartValue = startValue; |
| 171 | mEndValue = endValue; |
| 172 | addListener(this); |
| 173 | addUpdateListener(this); |
| 174 | mSurfaceControlTransactionFactory = SurfaceControl.Transaction::new; |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 175 | mTransitionDirection = TRANSITION_DIRECTION_NONE; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void onAnimationStart(Animator animation) { |
| 180 | mCurrentValue = mStartValue; |
| 181 | applySurfaceControlTransaction(mLeash, newSurfaceControlTransaction(), FRACTION_START); |
| 182 | if (mPipAnimationCallback != null) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 183 | mPipAnimationCallback.onPipAnimationStart(this); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void onAnimationUpdate(ValueAnimator animation) { |
| 189 | applySurfaceControlTransaction(mLeash, newSurfaceControlTransaction(), |
| 190 | animation.getAnimatedFraction()); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void onAnimationEnd(Animator animation) { |
| 195 | mCurrentValue = mEndValue; |
| 196 | final SurfaceControl.Transaction tx = newSurfaceControlTransaction(); |
| 197 | applySurfaceControlTransaction(mLeash, tx, FRACTION_END); |
| 198 | if (mPipAnimationCallback != null) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 199 | mPipAnimationCallback.onPipAnimationEnd(tx, this); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void onAnimationCancel(Animator animation) { |
| 205 | if (mPipAnimationCallback != null) { |
Winson Chung | 5570147 | 2020-03-04 19:30:30 -0800 | [diff] [blame] | 206 | mPipAnimationCallback.onPipAnimationCancel(this); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | @Override public void onAnimationRepeat(Animator animation) {} |
| 211 | |
| 212 | @AnimationType int getAnimationType() { |
| 213 | return mAnimationType; |
| 214 | } |
| 215 | |
| 216 | PipTransitionAnimator<T> setPipAnimationCallback(PipAnimationCallback callback) { |
| 217 | mPipAnimationCallback = callback; |
| 218 | return this; |
| 219 | } |
| 220 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 221 | @TransitionDirection int getTransitionDirection() { |
| 222 | return mTransitionDirection; |
| 223 | } |
| 224 | |
| 225 | PipTransitionAnimator<T> setTransitionDirection(@TransitionDirection int direction) { |
| 226 | if (direction != TRANSITION_DIRECTION_SAME) { |
| 227 | mTransitionDirection = direction; |
| 228 | } |
| 229 | return this; |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | T getStartValue() { |
| 233 | return mStartValue; |
| 234 | } |
| 235 | |
| 236 | T getEndValue() { |
| 237 | return mEndValue; |
| 238 | } |
| 239 | |
| 240 | Rect getDestinationBounds() { |
| 241 | return mDestinationBounds; |
| 242 | } |
| 243 | |
| 244 | void setDestinationBounds(Rect destinationBounds) { |
| 245 | mDestinationBounds.set(destinationBounds); |
| 246 | } |
| 247 | |
| 248 | void setCurrentValue(T value) { |
| 249 | mCurrentValue = value; |
| 250 | } |
| 251 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 252 | boolean shouldApplyCornerRadius() { |
| 253 | return mTransitionDirection != TRANSITION_DIRECTION_TO_FULLSCREEN; |
| 254 | } |
| 255 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Updates the {@link #mEndValue}. |
| 258 | * |
| 259 | * NOTE: Do not forget to call {@link #setDestinationBounds(Rect)} for bounds animation. |
| 260 | * This is typically used when we receive a shelf height adjustment during the bounds |
| 261 | * animation. In which case we can update the end bounds and keep the existing animation |
| 262 | * running instead of cancelling it. |
| 263 | */ |
| 264 | void updateEndValue(T endValue) { |
| 265 | mEndValue = endValue; |
| 266 | mStartValue = mCurrentValue; |
| 267 | } |
| 268 | |
| 269 | SurfaceControl.Transaction newSurfaceControlTransaction() { |
| 270 | return mSurfaceControlTransactionFactory.getTransaction(); |
| 271 | } |
| 272 | |
| 273 | @VisibleForTesting |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 274 | void setSurfaceControlTransactionFactory( |
| 275 | PipSurfaceTransactionHelper.SurfaceControlTransactionFactory factory) { |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 276 | mSurfaceControlTransactionFactory = factory; |
| 277 | } |
| 278 | |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 279 | PipSurfaceTransactionHelper getSurfaceTransactionHelper() { |
| 280 | return mSurfaceTransactionHelper; |
| 281 | } |
| 282 | |
| 283 | void setSurfaceTransactionHelper(PipSurfaceTransactionHelper helper) { |
| 284 | mSurfaceTransactionHelper = helper; |
| 285 | } |
| 286 | |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 287 | abstract void applySurfaceControlTransaction(SurfaceControl leash, |
| 288 | SurfaceControl.Transaction tx, float fraction); |
| 289 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 290 | static PipTransitionAnimator<Float> ofAlpha(SurfaceControl leash, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 291 | Rect destinationBounds, float startValue, float endValue) { |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 292 | return new PipTransitionAnimator<Float>(leash, ANIM_TYPE_ALPHA, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 293 | destinationBounds, startValue, endValue) { |
| 294 | @Override |
| 295 | void applySurfaceControlTransaction(SurfaceControl leash, |
| 296 | SurfaceControl.Transaction tx, float fraction) { |
| 297 | final float alpha = getStartValue() * (1 - fraction) + getEndValue() * fraction; |
| 298 | setCurrentValue(alpha); |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 299 | getSurfaceTransactionHelper().alpha(tx, leash, alpha); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 300 | if (Float.compare(fraction, FRACTION_START) == 0) { |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 301 | getSurfaceTransactionHelper() |
| 302 | .crop(tx, leash, getDestinationBounds()) |
| 303 | .round(tx, leash, shouldApplyCornerRadius()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 304 | } |
| 305 | tx.apply(); |
| 306 | } |
| 307 | }; |
| 308 | } |
| 309 | |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 310 | static PipTransitionAnimator<Rect> ofBounds(SurfaceControl leash, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 311 | Rect startValue, Rect endValue) { |
| 312 | // construct new Rect instances in case they are recycled |
Hongwei Wang | df8bb00 | 2020-03-03 17:41:02 -0800 | [diff] [blame] | 313 | return new PipTransitionAnimator<Rect>(leash, ANIM_TYPE_BOUNDS, |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 314 | endValue, new Rect(startValue), new Rect(endValue)) { |
| 315 | private final Rect mTmpRect = new Rect(); |
| 316 | |
| 317 | private int getCastedFractionValue(float start, float end, float fraction) { |
| 318 | return (int) (start * (1 - fraction) + end * fraction + .5f); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | void applySurfaceControlTransaction(SurfaceControl leash, |
| 323 | SurfaceControl.Transaction tx, float fraction) { |
| 324 | final Rect start = getStartValue(); |
| 325 | final Rect end = getEndValue(); |
| 326 | mTmpRect.set( |
| 327 | getCastedFractionValue(start.left, end.left, fraction), |
| 328 | getCastedFractionValue(start.top, end.top, fraction), |
| 329 | getCastedFractionValue(start.right, end.right, fraction), |
| 330 | getCastedFractionValue(start.bottom, end.bottom, fraction)); |
| 331 | setCurrentValue(mTmpRect); |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 332 | getSurfaceTransactionHelper().crop(tx, leash, mTmpRect); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 333 | if (Float.compare(fraction, FRACTION_START) == 0) { |
| 334 | // Ensure the start condition |
Hongwei Wang | ec3cb3c | 2020-03-09 10:43:21 -0700 | [diff] [blame^] | 335 | getSurfaceTransactionHelper() |
| 336 | .alpha(tx, leash, 1f) |
| 337 | .round(tx, leash, shouldApplyCornerRadius()); |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 338 | } |
| 339 | tx.apply(); |
| 340 | } |
| 341 | }; |
| 342 | } |
| 343 | } |
Hongwei Wang | 85cf41f | 2020-01-15 15:14:47 -0800 | [diff] [blame] | 344 | } |