blob: 153359204d5fde9c4d15e866ccf8fc16a55a6933 [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 Wang221fe3d2020-03-26 13:13:04 -070052 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 Wangdf8bb002020-03-03 17:41:02 -080056
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)
Hongwei Wang221fe3d2020-03-26 13:13:04 -070064 public @interface TransitionDirection {}
Hongwei Wangdf8bb002020-03-03 17:41:02 -080065
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 Wang85cf41f2020-01-15 15:14:47 -0800113 private PipTransitionAnimator setupPipTransitionAnimator(PipTransitionAnimator animator) {
Hongwei Wangec3cb3c2020-03-09 10:43:21 -0700114 animator.setSurfaceTransactionHelper(mSurfaceTransactionHelper);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800115 animator.setInterpolator(mFastOutSlowInInterpolator);
116 animator.setFloatValues(FRACTION_START, FRACTION_END);
117 return animator;
118 }
119
120 /**
121 * Additional callback interface for PiP animation
122 */
123 public static class PipAnimationCallback {
124 /**
125 * Called when PiP animation is started.
126 */
Winson Chung55701472020-03-04 19:30:30 -0800127 public void onPipAnimationStart(PipTransitionAnimator animator) {}
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800128
129 /**
130 * Called when PiP animation is ended.
131 */
Winson Chung55701472020-03-04 19:30:30 -0800132 public void onPipAnimationEnd(SurfaceControl.Transaction tx,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800133 PipTransitionAnimator animator) {}
134
135 /**
136 * Called when PiP animation is cancelled.
137 */
Winson Chung55701472020-03-04 19:30:30 -0800138 public void onPipAnimationCancel(PipTransitionAnimator animator) {}
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800139 }
140
141 /**
142 * Animator for PiP transition animation which supports both alpha and bounds animation.
143 * @param <T> Type of property to animate, either alpha (float) or bounds (Rect)
144 */
145 public abstract static class PipTransitionAnimator<T> extends ValueAnimator implements
146 ValueAnimator.AnimatorUpdateListener,
147 ValueAnimator.AnimatorListener {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800148 private final SurfaceControl mLeash;
149 private final @AnimationType int mAnimationType;
150 private final Rect mDestinationBounds = new Rect();
151
152 private T mStartValue;
153 private T mEndValue;
154 private T mCurrentValue;
155 private PipAnimationCallback mPipAnimationCallback;
Hongwei Wangec3cb3c2020-03-09 10:43:21 -0700156 private PipSurfaceTransactionHelper.SurfaceControlTransactionFactory
157 mSurfaceControlTransactionFactory;
158 private PipSurfaceTransactionHelper mSurfaceTransactionHelper;
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800159 private @TransitionDirection int mTransitionDirection;
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800160
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800161 private PipTransitionAnimator(SurfaceControl leash, @AnimationType int animationType,
162 Rect destinationBounds, T startValue, T endValue) {
Winson Chung55701472020-03-04 19:30:30 -0800163 mLeash = leash;
164 mAnimationType = animationType;
165 mDestinationBounds.set(destinationBounds);
166 mStartValue = startValue;
167 mEndValue = endValue;
168 addListener(this);
169 addUpdateListener(this);
170 mSurfaceControlTransactionFactory = SurfaceControl.Transaction::new;
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800171 mTransitionDirection = TRANSITION_DIRECTION_NONE;
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800172 }
173
174 @Override
175 public void onAnimationStart(Animator animation) {
176 mCurrentValue = mStartValue;
Hongwei Wang09c1f042020-03-18 13:47:45 -0700177 onStartTransaction(mLeash, newSurfaceControlTransaction());
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800178 if (mPipAnimationCallback != null) {
Winson Chung55701472020-03-04 19:30:30 -0800179 mPipAnimationCallback.onPipAnimationStart(this);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800180 }
181 }
182
183 @Override
184 public void onAnimationUpdate(ValueAnimator animation) {
185 applySurfaceControlTransaction(mLeash, newSurfaceControlTransaction(),
186 animation.getAnimatedFraction());
187 }
188
189 @Override
190 public void onAnimationEnd(Animator animation) {
191 mCurrentValue = mEndValue;
192 final SurfaceControl.Transaction tx = newSurfaceControlTransaction();
Hongwei Wang09c1f042020-03-18 13:47:45 -0700193 onEndTransaction(mLeash, tx);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800194 if (mPipAnimationCallback != null) {
Winson Chung55701472020-03-04 19:30:30 -0800195 mPipAnimationCallback.onPipAnimationEnd(tx, this);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800196 }
197 }
198
199 @Override
200 public void onAnimationCancel(Animator animation) {
201 if (mPipAnimationCallback != null) {
Winson Chung55701472020-03-04 19:30:30 -0800202 mPipAnimationCallback.onPipAnimationCancel(this);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800203 }
204 }
205
206 @Override public void onAnimationRepeat(Animator animation) {}
207
208 @AnimationType int getAnimationType() {
209 return mAnimationType;
210 }
211
212 PipTransitionAnimator<T> setPipAnimationCallback(PipAnimationCallback callback) {
213 mPipAnimationCallback = callback;
214 return this;
215 }
216
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800217 @TransitionDirection int getTransitionDirection() {
218 return mTransitionDirection;
219 }
220
221 PipTransitionAnimator<T> setTransitionDirection(@TransitionDirection int direction) {
222 if (direction != TRANSITION_DIRECTION_SAME) {
223 mTransitionDirection = direction;
224 }
225 return this;
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800226 }
227
228 T getStartValue() {
229 return mStartValue;
230 }
231
232 T getEndValue() {
233 return mEndValue;
234 }
235
236 Rect getDestinationBounds() {
237 return mDestinationBounds;
238 }
239
240 void setDestinationBounds(Rect destinationBounds) {
241 mDestinationBounds.set(destinationBounds);
242 }
243
244 void setCurrentValue(T value) {
245 mCurrentValue = value;
246 }
247
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800248 boolean shouldApplyCornerRadius() {
249 return mTransitionDirection != TRANSITION_DIRECTION_TO_FULLSCREEN;
250 }
251
Hongwei Wang09c1f042020-03-18 13:47:45 -0700252 boolean inScaleTransition() {
253 if (mAnimationType != ANIM_TYPE_BOUNDS) return false;
254 final int direction = getTransitionDirection();
255 return direction != TRANSITION_DIRECTION_TO_FULLSCREEN
256 && direction != TRANSITION_DIRECTION_TO_PIP;
257 }
258
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800259 /**
260 * Updates the {@link #mEndValue}.
261 *
262 * NOTE: Do not forget to call {@link #setDestinationBounds(Rect)} for bounds animation.
263 * This is typically used when we receive a shelf height adjustment during the bounds
264 * animation. In which case we can update the end bounds and keep the existing animation
265 * running instead of cancelling it.
266 */
267 void updateEndValue(T endValue) {
268 mEndValue = endValue;
269 mStartValue = mCurrentValue;
270 }
271
272 SurfaceControl.Transaction newSurfaceControlTransaction() {
273 return mSurfaceControlTransactionFactory.getTransaction();
274 }
275
276 @VisibleForTesting
Hongwei Wangec3cb3c2020-03-09 10:43:21 -0700277 void setSurfaceControlTransactionFactory(
278 PipSurfaceTransactionHelper.SurfaceControlTransactionFactory factory) {
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800279 mSurfaceControlTransactionFactory = factory;
280 }
281
Hongwei Wangec3cb3c2020-03-09 10:43:21 -0700282 PipSurfaceTransactionHelper getSurfaceTransactionHelper() {
283 return mSurfaceTransactionHelper;
284 }
285
286 void setSurfaceTransactionHelper(PipSurfaceTransactionHelper helper) {
287 mSurfaceTransactionHelper = helper;
288 }
289
Hongwei Wang09c1f042020-03-18 13:47:45 -0700290 void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {}
291
292 void onEndTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {}
293
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800294 abstract void applySurfaceControlTransaction(SurfaceControl leash,
295 SurfaceControl.Transaction tx, float fraction);
296
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800297 static PipTransitionAnimator<Float> ofAlpha(SurfaceControl leash,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800298 Rect destinationBounds, float startValue, float endValue) {
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800299 return new PipTransitionAnimator<Float>(leash, ANIM_TYPE_ALPHA,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800300 destinationBounds, startValue, endValue) {
301 @Override
302 void applySurfaceControlTransaction(SurfaceControl leash,
303 SurfaceControl.Transaction tx, float fraction) {
304 final float alpha = getStartValue() * (1 - fraction) + getEndValue() * fraction;
305 setCurrentValue(alpha);
Hongwei Wangec3cb3c2020-03-09 10:43:21 -0700306 getSurfaceTransactionHelper().alpha(tx, leash, alpha);
Hongwei Wang09c1f042020-03-18 13:47:45 -0700307 tx.apply();
308 }
309
310 @Override
311 void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {
312 getSurfaceTransactionHelper()
313 .crop(tx, leash, getDestinationBounds())
314 .round(tx, leash, shouldApplyCornerRadius());
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800315 tx.apply();
316 }
317 };
318 }
319
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800320 static PipTransitionAnimator<Rect> ofBounds(SurfaceControl leash,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800321 Rect startValue, Rect endValue) {
322 // construct new Rect instances in case they are recycled
Hongwei Wangdf8bb002020-03-03 17:41:02 -0800323 return new PipTransitionAnimator<Rect>(leash, ANIM_TYPE_BOUNDS,
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800324 endValue, new Rect(startValue), new Rect(endValue)) {
325 private final Rect mTmpRect = new Rect();
326
327 private int getCastedFractionValue(float start, float end, float fraction) {
328 return (int) (start * (1 - fraction) + end * fraction + .5f);
329 }
330
331 @Override
332 void applySurfaceControlTransaction(SurfaceControl leash,
333 SurfaceControl.Transaction tx, float fraction) {
334 final Rect start = getStartValue();
335 final Rect end = getEndValue();
336 mTmpRect.set(
337 getCastedFractionValue(start.left, end.left, fraction),
338 getCastedFractionValue(start.top, end.top, fraction),
339 getCastedFractionValue(start.right, end.right, fraction),
340 getCastedFractionValue(start.bottom, end.bottom, fraction));
341 setCurrentValue(mTmpRect);
Hongwei Wang09c1f042020-03-18 13:47:45 -0700342 if (inScaleTransition()) {
343 getSurfaceTransactionHelper().scale(tx, leash, start, mTmpRect);
344 } else {
345 getSurfaceTransactionHelper().crop(tx, leash, mTmpRect);
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800346 }
347 tx.apply();
348 }
Hongwei Wang09c1f042020-03-18 13:47:45 -0700349
350 @Override
351 void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {
352 getSurfaceTransactionHelper()
353 .alpha(tx, leash, 1f)
354 .round(tx, leash, shouldApplyCornerRadius());
355 tx.apply();
356 }
357
358 @Override
359 void onEndTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {
360 if (!inScaleTransition()) return;
361 // NOTE: intentionally does not apply the transaction here.
362 // this end transaction should get executed synchronously with the final
363 // WindowContainerTransaction in task organizer
364 getSurfaceTransactionHelper().resetScale(tx, leash, getDestinationBounds());
365 }
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800366 };
367 }
368 }
Hongwei Wang85cf41f2020-01-15 15:14:47 -0800369}