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