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