blob: 4bfcf2229e3e19495fa1a8ab6637da83f3f3a62f [file] [log] [blame]
Hongwei Wang85cf41f2020-01-15 15:14:47 -08001/*
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
17package com.android.systemui.pip;
18
19import android.animation.Animator;
20import android.animation.ValueAnimator;
21import android.annotation.IntDef;
Hongwei Wang85cf41f2020-01-15 15:14:47 -080022import android.content.Context;
23import android.graphics.Rect;
Hongwei Wang85cf41f2020-01-15 15:14:47 -080024import android.view.SurfaceControl;
25import android.view.animation.AnimationUtils;
26import android.view.animation.Interpolator;
27
28import com.android.internal.annotations.VisibleForTesting;
29
30import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
32
Hongwei Wangec3cb3c2020-03-09 10:43:21 -070033import javax.inject.Inject;
34
Hongwei Wang85cf41f2020-01-15 15:14:47 -080035/**
36 * Controller class of PiP animations (both from and to PiP mode).
37 */
38public class PipAnimationController {
39 private static final float FRACTION_START = 0f;
40 private static final float FRACTION_END = 1f;
41
Hongwei Wang85cf41f2020-01-15 15:14:47 -080042 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 Wangdf8bb002020-03-03 17:41:02 -080052 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 Wang85cf41f2020-01-15 15:14:47 -080066 private final Interpolator mFastOutSlowInInterpolator;
Hongwei Wangec3cb3c2020-03-09 10:43:21 -070067 private final PipSurfaceTransactionHelper mSurfaceTransactionHelper;
Hongwei Wang85cf41f2020-01-15 15:14:47 -080068
69 private PipTransitionAnimator mCurrentAnimator;
70
Hongwei Wangec3cb3c2020-03-09 10:43:21 -070071 @Inject
72 PipAnimationController(Context context, PipSurfaceTransactionHelper helper) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -080073 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
74 com.android.internal.R.interpolator.fast_out_slow_in);
Hongwei Wangec3cb3c2020-03-09 10:43:21 -070075 mSurfaceTransactionHelper = helper;
Hongwei Wang85cf41f2020-01-15 15:14:47 -080076 }
77
Hongwei Wangdf8bb002020-03-03 17:41:02 -080078 @SuppressWarnings("unchecked")
79 PipTransitionAnimator getAnimator(SurfaceControl leash,
Hongwei Wang85cf41f2020-01-15 15:14:47 -080080 Rect destinationBounds, float alphaStart, float alphaEnd) {
81 if (mCurrentAnimator == null) {
82 mCurrentAnimator = setupPipTransitionAnimator(
Hongwei Wangdf8bb002020-03-03 17:41:02 -080083 PipTransitionAnimator.ofAlpha(leash, destinationBounds, alphaStart, alphaEnd));
Hongwei Wang85cf41f2020-01-15 15:14:47 -080084 } else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_ALPHA
85 && mCurrentAnimator.isRunning()) {
86 mCurrentAnimator.updateEndValue(alphaEnd);
87 } else {
88 mCurrentAnimator.cancel();
89 mCurrentAnimator = setupPipTransitionAnimator(
Hongwei Wangdf8bb002020-03-03 17:41:02 -080090 PipTransitionAnimator.ofAlpha(leash, destinationBounds, alphaStart, alphaEnd));
Hongwei Wang85cf41f2020-01-15 15:14:47 -080091 }
92 return mCurrentAnimator;
93 }
94
Hongwei Wangdf8bb002020-03-03 17:41:02 -080095 @SuppressWarnings("unchecked")
96 PipTransitionAnimator getAnimator(SurfaceControl leash, Rect startBounds, Rect endBounds) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -080097 if (mCurrentAnimator == null) {
98 mCurrentAnimator = setupPipTransitionAnimator(
Hongwei Wangdf8bb002020-03-03 17:41:02 -080099 PipTransitionAnimator.ofBounds(leash, startBounds, endBounds));
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800100 } 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 Wangdf8bb002020-03-03 17:41:02 -0800108 PipTransitionAnimator.ofBounds(leash, startBounds, endBounds));
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800109 }
110 return mCurrentAnimator;
111 }
112
Hongwei Wangea2ae852020-02-27 13:47:52 -0800113 PipTransitionAnimator getCurrentAnimator() {
114 return mCurrentAnimator;
115 }
116
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800117 private PipTransitionAnimator setupPipTransitionAnimator(PipTransitionAnimator animator) {
Hongwei Wangec3cb3c2020-03-09 10:43:21 -0700118 animator.setSurfaceTransactionHelper(mSurfaceTransactionHelper);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800119 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 Chung55701472020-03-04 19:30:30 -0800131 public void onPipAnimationStart(PipTransitionAnimator animator) {}
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800132
133 /**
134 * Called when PiP animation is ended.
135 */
Winson Chung55701472020-03-04 19:30:30 -0800136 public void onPipAnimationEnd(SurfaceControl.Transaction tx,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800137 PipTransitionAnimator animator) {}
138
139 /**
140 * Called when PiP animation is cancelled.
141 */
Winson Chung55701472020-03-04 19:30:30 -0800142 public void onPipAnimationCancel(PipTransitionAnimator animator) {}
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800143 }
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 Wang85cf41f2020-01-15 15:14:47 -0800152 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 Wangec3cb3c2020-03-09 10:43:21 -0700160 private PipSurfaceTransactionHelper.SurfaceControlTransactionFactory
161 mSurfaceControlTransactionFactory;
162 private PipSurfaceTransactionHelper mSurfaceTransactionHelper;
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800163 private @TransitionDirection int mTransitionDirection;
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800164
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800165 private PipTransitionAnimator(SurfaceControl leash, @AnimationType int animationType,
166 Rect destinationBounds, T startValue, T endValue) {
Winson Chung55701472020-03-04 19:30:30 -0800167 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 Wangdf8bb002020-03-03 17:41:02 -0800175 mTransitionDirection = TRANSITION_DIRECTION_NONE;
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800176 }
177
178 @Override
179 public void onAnimationStart(Animator animation) {
180 mCurrentValue = mStartValue;
181 applySurfaceControlTransaction(mLeash, newSurfaceControlTransaction(), FRACTION_START);
182 if (mPipAnimationCallback != null) {
Winson Chung55701472020-03-04 19:30:30 -0800183 mPipAnimationCallback.onPipAnimationStart(this);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800184 }
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 Chung55701472020-03-04 19:30:30 -0800199 mPipAnimationCallback.onPipAnimationEnd(tx, this);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800200 }
201 }
202
203 @Override
204 public void onAnimationCancel(Animator animation) {
205 if (mPipAnimationCallback != null) {
Winson Chung55701472020-03-04 19:30:30 -0800206 mPipAnimationCallback.onPipAnimationCancel(this);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800207 }
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 Wangdf8bb002020-03-03 17:41:02 -0800221 @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 Wang85cf41f2020-01-15 15:14:47 -0800230 }
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 Wangdf8bb002020-03-03 17:41:02 -0800252 boolean shouldApplyCornerRadius() {
253 return mTransitionDirection != TRANSITION_DIRECTION_TO_FULLSCREEN;
254 }
255
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800256 /**
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 Wangec3cb3c2020-03-09 10:43:21 -0700274 void setSurfaceControlTransactionFactory(
275 PipSurfaceTransactionHelper.SurfaceControlTransactionFactory factory) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800276 mSurfaceControlTransactionFactory = factory;
277 }
278
Hongwei Wangec3cb3c2020-03-09 10:43:21 -0700279 PipSurfaceTransactionHelper getSurfaceTransactionHelper() {
280 return mSurfaceTransactionHelper;
281 }
282
283 void setSurfaceTransactionHelper(PipSurfaceTransactionHelper helper) {
284 mSurfaceTransactionHelper = helper;
285 }
286
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800287 abstract void applySurfaceControlTransaction(SurfaceControl leash,
288 SurfaceControl.Transaction tx, float fraction);
289
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800290 static PipTransitionAnimator<Float> ofAlpha(SurfaceControl leash,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800291 Rect destinationBounds, float startValue, float endValue) {
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800292 return new PipTransitionAnimator<Float>(leash, ANIM_TYPE_ALPHA,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800293 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 Wangec3cb3c2020-03-09 10:43:21 -0700299 getSurfaceTransactionHelper().alpha(tx, leash, alpha);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800300 if (Float.compare(fraction, FRACTION_START) == 0) {
Hongwei Wangec3cb3c2020-03-09 10:43:21 -0700301 getSurfaceTransactionHelper()
302 .crop(tx, leash, getDestinationBounds())
303 .round(tx, leash, shouldApplyCornerRadius());
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800304 }
305 tx.apply();
306 }
307 };
308 }
309
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800310 static PipTransitionAnimator<Rect> ofBounds(SurfaceControl leash,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800311 Rect startValue, Rect endValue) {
312 // construct new Rect instances in case they are recycled
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800313 return new PipTransitionAnimator<Rect>(leash, ANIM_TYPE_BOUNDS,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800314 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 Wangec3cb3c2020-03-09 10:43:21 -0700332 getSurfaceTransactionHelper().crop(tx, leash, mTmpRect);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800333 if (Float.compare(fraction, FRACTION_START) == 0) {
334 // Ensure the start condition
Hongwei Wangec3cb3c2020-03-09 10:43:21 -0700335 getSurfaceTransactionHelper()
336 .alpha(tx, leash, 1f)
337 .round(tx, leash, shouldApplyCornerRadius());
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800338 }
339 tx.apply();
340 }
341 };
342 }
343 }
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800344}