| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.pip; |
| 18 |
|
| 19 |
import static android.util.RotationUtils.rotateBounds; |
| 20 |
import static android.view.Surface.ROTATION_270; |
| 21 |
import static android.view.Surface.ROTATION_90; |
| 22 |
|
| 23 |
import android.animation.AnimationHandler; |
| 24 |
import android.animation.Animator; |
| 25 |
import android.animation.RectEvaluator; |
| 26 |
import android.animation.ValueAnimator; |
| 27 |
import android.annotation.IntDef; |
| 28 |
import android.annotation.NonNull; |
| 29 |
import android.app.TaskInfo; |
| 30 |
import android.content.Context; |
| 31 |
import android.graphics.Rect; |
| 32 |
import android.view.Surface; |
| 33 |
import android.view.SurfaceControl; |
| 34 |
import android.window.TaskSnapshot; |
| 35 |
|
| 36 |
import com.android.internal.annotations.VisibleForTesting; |
| 37 |
import com.android.internal.graphics.SfVsyncFrameCallbackProvider; |
| 38 |
import com.android.wm.shell.animation.Interpolators; |
| 39 |
import com.android.wm.shell.transition.Transitions; |
| 40 |
|
| 41 |
import java.lang.annotation.Retention; |
| 42 |
import java.lang.annotation.RetentionPolicy; |
| 43 |
import java.util.Objects; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class PipAnimationController { |
| 49 |
static final float FRACTION_START = 0f; |
| 50 |
private static final float FRACTION_END = 1f; |
| 51 |
|
| 52 |
public static final int ANIM_TYPE_BOUNDS = 0; |
| 53 |
public static final int ANIM_TYPE_ALPHA = 1; |
| 54 |
|
| 55 |
@IntDef(prefix = { "ANIM_TYPE_" }, value = { |
| 56 |
ANIM_TYPE_BOUNDS, |
| 57 |
ANIM_TYPE_ALPHA |
| 58 |
}) |
| 59 |
@Retention(RetentionPolicy.SOURCE) |
| 60 |
public @interface AnimationType {} |
| 61 |
|
| 62 |
public static final int TRANSITION_DIRECTION_NONE = 0; |
| 63 |
public static final int TRANSITION_DIRECTION_SAME = 1; |
| 64 |
public static final int TRANSITION_DIRECTION_TO_PIP = 2; |
| 65 |
public static final int TRANSITION_DIRECTION_LEAVE_PIP = 3; |
| 66 |
public static final int TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN = 4; |
| 67 |
public static final int TRANSITION_DIRECTION_REMOVE_STACK = 5; |
| 68 |
public static final int TRANSITION_DIRECTION_SNAP_AFTER_RESIZE = 6; |
| 69 |
public static final int TRANSITION_DIRECTION_USER_RESIZE = 7; |
| 70 |
public static final int TRANSITION_DIRECTION_EXPAND_OR_UNEXPAND = 8; |
| 71 |
|
| 72 |
@IntDef(prefix = { "TRANSITION_DIRECTION_" }, value = { |
| 73 |
TRANSITION_DIRECTION_NONE, |
| 74 |
TRANSITION_DIRECTION_SAME, |
| 75 |
TRANSITION_DIRECTION_TO_PIP, |
| 76 |
TRANSITION_DIRECTION_LEAVE_PIP, |
| 77 |
TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN, |
| 78 |
TRANSITION_DIRECTION_REMOVE_STACK, |
| 79 |
TRANSITION_DIRECTION_SNAP_AFTER_RESIZE, |
| 80 |
TRANSITION_DIRECTION_USER_RESIZE, |
| 81 |
TRANSITION_DIRECTION_EXPAND_OR_UNEXPAND |
| 82 |
}) |
| 83 |
@Retention(RetentionPolicy.SOURCE) |
| 84 |
public @interface TransitionDirection {} |
| 85 |
|
| 86 |
public static boolean isInPipDirection(@TransitionDirection int direction) { |
| 87 |
return direction == TRANSITION_DIRECTION_TO_PIP; |
| 88 |
} |
| 89 |
|
| 90 |
public static boolean isOutPipDirection(@TransitionDirection int direction) { |
| 91 |
return direction == TRANSITION_DIRECTION_LEAVE_PIP |
| 92 |
|| direction == TRANSITION_DIRECTION_LEAVE_PIP_TO_SPLIT_SCREEN; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public static boolean isRemovePipDirection(@TransitionDirection int direction) { |
| 97 |
return direction == TRANSITION_DIRECTION_REMOVE_STACK; |
| 98 |
} |
| 99 |
|
| 100 |
private final PipSurfaceTransactionHelper mSurfaceTransactionHelper; |
| 101 |
|
| 102 |
private final ThreadLocal<AnimationHandler> mSfAnimationHandlerThreadLocal = |
| 103 |
ThreadLocal.withInitial(() -> { |
| 104 |
AnimationHandler handler = new AnimationHandler(); |
| 105 |
handler.setProvider(new SfVsyncFrameCallbackProvider()); |
| 106 |
return handler; |
| 107 |
}); |
| 108 |
|
| 109 |
private PipTransitionAnimator mCurrentAnimator; |
| 110 |
|
| 111 |
public PipAnimationController(PipSurfaceTransactionHelper helper) { |
| 112 |
mSurfaceTransactionHelper = helper; |
| 113 |
} |
| 114 |
|
| 115 |
@SuppressWarnings("unchecked") |
| 116 |
@VisibleForTesting |
| 117 |
public PipTransitionAnimator getAnimator(TaskInfo taskInfo, SurfaceControl leash, |
| 118 |
Rect destinationBounds, float alphaStart, float alphaEnd) { |
| 119 |
if (mCurrentAnimator == null) { |
| 120 |
mCurrentAnimator = setupPipTransitionAnimator( |
| 121 |
PipTransitionAnimator.ofAlpha(taskInfo, leash, destinationBounds, alphaStart, |
| 122 |
alphaEnd)); |
| 123 |
} else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_ALPHA |
| 124 |
&& Objects.equals(destinationBounds, mCurrentAnimator.getDestinationBounds()) |
| 125 |
&& mCurrentAnimator.isRunning()) { |
| 126 |
mCurrentAnimator.updateEndValue(alphaEnd); |
| 127 |
} else { |
| 128 |
mCurrentAnimator.cancel(); |
| 129 |
mCurrentAnimator = setupPipTransitionAnimator( |
| 130 |
PipTransitionAnimator.ofAlpha(taskInfo, leash, destinationBounds, alphaStart, |
| 131 |
alphaEnd)); |
| 132 |
} |
| 133 |
return mCurrentAnimator; |
| 134 |
} |
| 135 |
|
| 136 |
@SuppressWarnings("unchecked") |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
@VisibleForTesting |
| 156 |
public PipTransitionAnimator getAnimator(TaskInfo taskInfo, SurfaceControl leash, |
| 157 |
Rect baseBounds, Rect startBounds, Rect endBounds, Rect sourceHintRect, |
| 158 |
@PipAnimationController.TransitionDirection int direction, float startingAngle, |
| 159 |
@Surface.Rotation int rotationDelta) { |
| 160 |
if (mCurrentAnimator == null) { |
| 161 |
mCurrentAnimator = setupPipTransitionAnimator( |
| 162 |
PipTransitionAnimator.ofBounds(taskInfo, leash, startBounds, startBounds, |
| 163 |
endBounds, sourceHintRect, direction, 0 , |
| 164 |
rotationDelta)); |
| 165 |
} else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_ALPHA |
| 166 |
&& mCurrentAnimator.isRunning()) { |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
mCurrentAnimator.setDestinationBounds(endBounds); |
| 171 |
} else if (mCurrentAnimator.getAnimationType() == ANIM_TYPE_BOUNDS |
| 172 |
&& mCurrentAnimator.isRunning()) { |
| 173 |
mCurrentAnimator.setDestinationBounds(endBounds); |
| 174 |
|
| 175 |
mCurrentAnimator.updateEndValue(new Rect(endBounds)); |
| 176 |
} else { |
| 177 |
mCurrentAnimator.cancel(); |
| 178 |
mCurrentAnimator = setupPipTransitionAnimator( |
| 179 |
PipTransitionAnimator.ofBounds(taskInfo, leash, baseBounds, startBounds, |
| 180 |
endBounds, sourceHintRect, direction, startingAngle, rotationDelta)); |
| 181 |
} |
| 182 |
return mCurrentAnimator; |
| 183 |
} |
| 184 |
|
| 185 |
public PipTransitionAnimator getCurrentAnimator() { |
| 186 |
return mCurrentAnimator; |
| 187 |
} |
| 188 |
|
| 189 |
private PipTransitionAnimator setupPipTransitionAnimator(PipTransitionAnimator animator) { |
| 190 |
animator.setSurfaceTransactionHelper(mSurfaceTransactionHelper); |
| 191 |
animator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN); |
| 192 |
animator.setFloatValues(FRACTION_START, FRACTION_END); |
| 193 |
animator.setAnimationHandler(mSfAnimationHandlerThreadLocal.get()); |
| 194 |
return animator; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public boolean isAnimating() { |
| 201 |
PipAnimationController.PipTransitionAnimator animator = getCurrentAnimator(); |
| 202 |
if (animator != null && animator.isRunning()) { |
| 203 |
return true; |
| 204 |
} |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static void quietCancel(@NonNull ValueAnimator animator) { |
| 212 |
animator.removeAllUpdateListeners(); |
| 213 |
animator.removeAllListeners(); |
| 214 |
animator.cancel(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public static class PipAnimationCallback { |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public void onPipAnimationStart(TaskInfo taskInfo, PipTransitionAnimator animator) {} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public void onPipAnimationEnd(TaskInfo taskInfo, SurfaceControl.Transaction tx, |
| 230 |
PipTransitionAnimator animator) {} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public void onPipAnimationCancel(TaskInfo taskInfo, PipTransitionAnimator animator) {} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public static class PipTransactionHandler { |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public boolean handlePipTransaction(SurfaceControl leash, SurfaceControl.Transaction tx, |
| 251 |
Rect destinationBounds) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public abstract static class PipTransitionAnimator<T> extends ValueAnimator implements |
| 261 |
ValueAnimator.AnimatorUpdateListener, |
| 262 |
ValueAnimator.AnimatorListener { |
| 263 |
private final TaskInfo mTaskInfo; |
| 264 |
private final SurfaceControl mLeash; |
| 265 |
private final @AnimationType int mAnimationType; |
| 266 |
private final Rect mDestinationBounds = new Rect(); |
| 267 |
|
| 268 |
private T mBaseValue; |
| 269 |
protected T mCurrentValue; |
| 270 |
protected T mStartValue; |
| 271 |
private T mEndValue; |
| 272 |
private PipAnimationCallback mPipAnimationCallback; |
| 273 |
private PipTransactionHandler mPipTransactionHandler; |
| 274 |
private PipSurfaceTransactionHelper.SurfaceControlTransactionFactory |
| 275 |
mSurfaceControlTransactionFactory; |
| 276 |
private PipSurfaceTransactionHelper mSurfaceTransactionHelper; |
| 277 |
private @TransitionDirection int mTransitionDirection; |
| 278 |
protected PipContentOverlay mContentOverlay; |
| 279 |
|
| 280 |
private PipTransitionAnimator(TaskInfo taskInfo, SurfaceControl leash, |
| 281 |
@AnimationType int animationType, |
| 282 |
Rect destinationBounds, T baseValue, T startValue, T endValue) { |
| 283 |
mTaskInfo = taskInfo; |
| 284 |
mLeash = leash; |
| 285 |
mAnimationType = animationType; |
| 286 |
mDestinationBounds.set(destinationBounds); |
| 287 |
mBaseValue = baseValue; |
| 288 |
mStartValue = startValue; |
| 289 |
mEndValue = endValue; |
| 290 |
addListener(this); |
| 291 |
addUpdateListener(this); |
| 292 |
mSurfaceControlTransactionFactory = |
| 293 |
new PipSurfaceTransactionHelper.VsyncSurfaceControlTransactionFactory(); |
| 294 |
mTransitionDirection = TRANSITION_DIRECTION_NONE; |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public void onAnimationStart(Animator animation) { |
| 299 |
mCurrentValue = mStartValue; |
| 300 |
onStartTransaction(mLeash, mSurfaceControlTransactionFactory.getTransaction()); |
| 301 |
if (mPipAnimationCallback != null) { |
| 302 |
mPipAnimationCallback.onPipAnimationStart(mTaskInfo, this); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
public void onAnimationUpdate(ValueAnimator animation) { |
| 308 |
applySurfaceControlTransaction(mLeash, |
| 309 |
mSurfaceControlTransactionFactory.getTransaction(), |
| 310 |
animation.getAnimatedFraction()); |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public void onAnimationEnd(Animator animation) { |
| 315 |
mCurrentValue = mEndValue; |
| 316 |
final SurfaceControl.Transaction tx = |
| 317 |
mSurfaceControlTransactionFactory.getTransaction(); |
| 318 |
onEndTransaction(mLeash, tx, mTransitionDirection); |
| 319 |
if (mPipAnimationCallback != null) { |
| 320 |
mPipAnimationCallback.onPipAnimationEnd(mTaskInfo, tx, this); |
| 321 |
} |
| 322 |
mTransitionDirection = TRANSITION_DIRECTION_NONE; |
| 323 |
} |
| 324 |
|
| 325 |
@Override |
| 326 |
public void onAnimationCancel(Animator animation) { |
| 327 |
if (mPipAnimationCallback != null) { |
| 328 |
mPipAnimationCallback.onPipAnimationCancel(mTaskInfo, this); |
| 329 |
} |
| 330 |
mTransitionDirection = TRANSITION_DIRECTION_NONE; |
| 331 |
} |
| 332 |
|
| 333 |
@Override public void onAnimationRepeat(Animator animation) {} |
| 334 |
|
| 335 |
@VisibleForTesting |
| 336 |
@AnimationType public int getAnimationType() { |
| 337 |
return mAnimationType; |
| 338 |
} |
| 339 |
|
| 340 |
@VisibleForTesting |
| 341 |
public PipTransitionAnimator<T> setPipAnimationCallback(PipAnimationCallback callback) { |
| 342 |
mPipAnimationCallback = callback; |
| 343 |
return this; |
| 344 |
} |
| 345 |
|
| 346 |
PipTransitionAnimator<T> setPipTransactionHandler(PipTransactionHandler handler) { |
| 347 |
mPipTransactionHandler = handler; |
| 348 |
return this; |
| 349 |
} |
| 350 |
|
| 351 |
boolean handlePipTransaction(SurfaceControl leash, SurfaceControl.Transaction tx, |
| 352 |
Rect destinationBounds) { |
| 353 |
if (mPipTransactionHandler != null) { |
| 354 |
return mPipTransactionHandler.handlePipTransaction(leash, tx, destinationBounds); |
| 355 |
} |
| 356 |
return false; |
| 357 |
} |
| 358 |
|
| 359 |
SurfaceControl getContentOverlayLeash() { |
| 360 |
return mContentOverlay == null ? null : mContentOverlay.mLeash; |
| 361 |
} |
| 362 |
|
| 363 |
void setColorContentOverlay(Context context) { |
| 364 |
final SurfaceControl.Transaction tx = |
| 365 |
mSurfaceControlTransactionFactory.getTransaction(); |
| 366 |
if (mContentOverlay != null) { |
| 367 |
mContentOverlay.detach(tx); |
| 368 |
} |
| 369 |
mContentOverlay = new PipContentOverlay.PipColorOverlay(context); |
| 370 |
mContentOverlay.attach(tx, mLeash); |
| 371 |
} |
| 372 |
|
| 373 |
void setSnapshotContentOverlay(TaskSnapshot snapshot, Rect sourceRectHint) { |
| 374 |
final SurfaceControl.Transaction tx = |
| 375 |
mSurfaceControlTransactionFactory.getTransaction(); |
| 376 |
if (mContentOverlay != null) { |
| 377 |
mContentOverlay.detach(tx); |
| 378 |
} |
| 379 |
mContentOverlay = new PipContentOverlay.PipSnapshotOverlay(snapshot, sourceRectHint); |
| 380 |
mContentOverlay.attach(tx, mLeash); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
void clearContentOverlay() { |
| 388 |
mContentOverlay = null; |
| 389 |
} |
| 390 |
|
| 391 |
@VisibleForTesting |
| 392 |
@TransitionDirection public int getTransitionDirection() { |
| 393 |
return mTransitionDirection; |
| 394 |
} |
| 395 |
|
| 396 |
@VisibleForTesting |
| 397 |
public PipTransitionAnimator<T> setTransitionDirection(@TransitionDirection int direction) { |
| 398 |
if (direction != TRANSITION_DIRECTION_SAME) { |
| 399 |
mTransitionDirection = direction; |
| 400 |
} |
| 401 |
return this; |
| 402 |
} |
| 403 |
|
| 404 |
T getStartValue() { |
| 405 |
return mStartValue; |
| 406 |
} |
| 407 |
|
| 408 |
T getBaseValue() { |
| 409 |
return mBaseValue; |
| 410 |
} |
| 411 |
|
| 412 |
@VisibleForTesting |
| 413 |
public T getEndValue() { |
| 414 |
return mEndValue; |
| 415 |
} |
| 416 |
|
| 417 |
Rect getDestinationBounds() { |
| 418 |
return mDestinationBounds; |
| 419 |
} |
| 420 |
|
| 421 |
void setDestinationBounds(Rect destinationBounds) { |
| 422 |
mDestinationBounds.set(destinationBounds); |
| 423 |
if (mAnimationType == ANIM_TYPE_ALPHA) { |
| 424 |
onStartTransaction(mLeash, mSurfaceControlTransactionFactory.getTransaction()); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
void setCurrentValue(T value) { |
| 429 |
mCurrentValue = value; |
| 430 |
} |
| 431 |
|
| 432 |
boolean shouldApplyCornerRadius() { |
| 433 |
return !isOutPipDirection(mTransitionDirection); |
| 434 |
} |
| 435 |
|
| 436 |
boolean shouldApplyShadowRadius() { |
| 437 |
return !isOutPipDirection(mTransitionDirection) |
| 438 |
&& !isRemovePipDirection(mTransitionDirection); |
| 439 |
} |
| 440 |
|
| 441 |
boolean inScaleTransition() { |
| 442 |
if (mAnimationType != ANIM_TYPE_BOUNDS) return false; |
| 443 |
final int direction = getTransitionDirection(); |
| 444 |
return !isInPipDirection(direction) && !isOutPipDirection(direction); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public void updateEndValue(T endValue) { |
| 456 |
mEndValue = endValue; |
| 457 |
} |
| 458 |
|
| 459 |
@VisibleForTesting |
| 460 |
public void setSurfaceControlTransactionFactory( |
| 461 |
PipSurfaceTransactionHelper.SurfaceControlTransactionFactory factory) { |
| 462 |
mSurfaceControlTransactionFactory = factory; |
| 463 |
} |
| 464 |
|
| 465 |
PipSurfaceTransactionHelper getSurfaceTransactionHelper() { |
| 466 |
return mSurfaceTransactionHelper; |
| 467 |
} |
| 468 |
|
| 469 |
void setSurfaceTransactionHelper(PipSurfaceTransactionHelper helper) { |
| 470 |
mSurfaceTransactionHelper = helper; |
| 471 |
} |
| 472 |
|
| 473 |
void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) {} |
| 474 |
|
| 475 |
void onEndTransaction(SurfaceControl leash, SurfaceControl.Transaction tx, |
| 476 |
@TransitionDirection int transitionDirection) {} |
| 477 |
|
| 478 |
abstract void applySurfaceControlTransaction(SurfaceControl leash, |
| 479 |
SurfaceControl.Transaction tx, float fraction); |
| 480 |
|
| 481 |
static PipTransitionAnimator<Float> ofAlpha(TaskInfo taskInfo, SurfaceControl leash, |
| 482 |
Rect destinationBounds, float startValue, float endValue) { |
| 483 |
return new PipTransitionAnimator<Float>(taskInfo, leash, ANIM_TYPE_ALPHA, |
| 484 |
destinationBounds, startValue, startValue, endValue) { |
| 485 |
@Override |
| 486 |
void applySurfaceControlTransaction(SurfaceControl leash, |
| 487 |
SurfaceControl.Transaction tx, float fraction) { |
| 488 |
final float alpha = getStartValue() * (1 - fraction) + getEndValue() * fraction; |
| 489 |
setCurrentValue(alpha); |
| 490 |
getSurfaceTransactionHelper().alpha(tx, leash, alpha) |
| 491 |
.round(tx, leash, shouldApplyCornerRadius()) |
| 492 |
.shadow(tx, leash, shouldApplyShadowRadius()); |
| 493 |
tx.apply(); |
| 494 |
} |
| 495 |
|
| 496 |
@Override |
| 497 |
void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) { |
| 498 |
if (getTransitionDirection() == TRANSITION_DIRECTION_REMOVE_STACK) { |
| 499 |
|
| 500 |
return; |
| 501 |
} |
| 502 |
getSurfaceTransactionHelper() |
| 503 |
.resetScale(tx, leash, getDestinationBounds()) |
| 504 |
.crop(tx, leash, getDestinationBounds()) |
| 505 |
.round(tx, leash, shouldApplyCornerRadius()) |
| 506 |
.shadow(tx, leash, shouldApplyShadowRadius()); |
| 507 |
tx.show(leash); |
| 508 |
tx.apply(); |
| 509 |
} |
| 510 |
|
| 511 |
@Override |
| 512 |
public void updateEndValue(Float endValue) { |
| 513 |
super.updateEndValue(endValue); |
| 514 |
mStartValue = mCurrentValue; |
| 515 |
} |
| 516 |
}; |
| 517 |
} |
| 518 |
|
| 519 |
static PipTransitionAnimator<Rect> ofBounds(TaskInfo taskInfo, SurfaceControl leash, |
| 520 |
Rect baseValue, Rect startValue, Rect endValue, Rect sourceHintRect, |
| 521 |
@PipAnimationController.TransitionDirection int direction, float startingAngle, |
| 522 |
@Surface.Rotation int rotationDelta) { |
| 523 |
final boolean isOutPipDirection = isOutPipDirection(direction); |
| 524 |
final boolean isInPipDirection = isInPipDirection(direction); |
| 525 |
|
| 526 |
|
| 527 |
final Rect initialSourceValue; |
| 528 |
if (isOutPipDirection) { |
| 529 |
initialSourceValue = new Rect(endValue); |
| 530 |
} else { |
| 531 |
initialSourceValue = new Rect(baseValue); |
| 532 |
} |
| 533 |
|
| 534 |
final Rect rotatedEndRect; |
| 535 |
final Rect lastEndRect; |
| 536 |
final Rect initialContainerRect; |
| 537 |
if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) { |
| 538 |
lastEndRect = new Rect(endValue); |
| 539 |
rotatedEndRect = new Rect(endValue); |
| 540 |
|
| 541 |
|
| 542 |
rotateBounds(rotatedEndRect, initialSourceValue, rotationDelta); |
| 543 |
|
| 544 |
initialContainerRect = isOutPipDirection ? rotatedEndRect : initialSourceValue; |
| 545 |
} else { |
| 546 |
rotatedEndRect = lastEndRect = null; |
| 547 |
initialContainerRect = initialSourceValue; |
| 548 |
} |
| 549 |
|
| 550 |
final Rect sourceHintRectInsets; |
| 551 |
if (sourceHintRect == null) { |
| 552 |
sourceHintRectInsets = null; |
| 553 |
} else { |
| 554 |
sourceHintRectInsets = new Rect(sourceHintRect.left - initialContainerRect.left, |
| 555 |
sourceHintRect.top - initialContainerRect.top, |
| 556 |
initialContainerRect.right - sourceHintRect.right, |
| 557 |
initialContainerRect.bottom - sourceHintRect.bottom); |
| 558 |
} |
| 559 |
final Rect zeroInsets = new Rect(0, 0, 0, 0); |
| 560 |
|
| 561 |
|
| 562 |
return new PipTransitionAnimator<Rect>(taskInfo, leash, ANIM_TYPE_BOUNDS, |
| 563 |
endValue, new Rect(baseValue), new Rect(startValue), new Rect(endValue)) { |
| 564 |
private final RectEvaluator mRectEvaluator = new RectEvaluator(new Rect()); |
| 565 |
private final RectEvaluator mInsetsEvaluator = new RectEvaluator(new Rect()); |
| 566 |
|
| 567 |
@Override |
| 568 |
void applySurfaceControlTransaction(SurfaceControl leash, |
| 569 |
SurfaceControl.Transaction tx, float fraction) { |
| 570 |
final Rect base = getBaseValue(); |
| 571 |
final Rect start = getStartValue(); |
| 572 |
final Rect end = getEndValue(); |
| 573 |
if (mContentOverlay != null) { |
| 574 |
mContentOverlay.onAnimationUpdate(tx, fraction); |
| 575 |
} |
| 576 |
if (rotatedEndRect != null) { |
| 577 |
|
| 578 |
|
| 579 |
applyRotation(tx, leash, fraction, start, end); |
| 580 |
return; |
| 581 |
} |
| 582 |
Rect bounds = mRectEvaluator.evaluate(fraction, start, end); |
| 583 |
float angle = (1.0f - fraction) * startingAngle; |
| 584 |
setCurrentValue(bounds); |
| 585 |
if (inScaleTransition() || sourceHintRect == null) { |
| 586 |
if (isOutPipDirection) { |
| 587 |
getSurfaceTransactionHelper().crop(tx, leash, end) |
| 588 |
.scale(tx, leash, end, bounds); |
| 589 |
} else { |
| 590 |
getSurfaceTransactionHelper().crop(tx, leash, base) |
| 591 |
.scale(tx, leash, base, bounds, angle) |
| 592 |
.round(tx, leash, base, bounds) |
| 593 |
.shadow(tx, leash, shouldApplyShadowRadius()); |
| 594 |
} |
| 595 |
} else { |
| 596 |
final Rect insets = computeInsets(fraction); |
| 597 |
getSurfaceTransactionHelper().scaleAndCrop(tx, leash, |
| 598 |
sourceHintRect, initialSourceValue, bounds, insets, |
| 599 |
isInPipDirection, fraction); |
| 600 |
if (shouldApplyCornerRadius()) { |
| 601 |
final Rect sourceBounds = new Rect(initialContainerRect); |
| 602 |
sourceBounds.inset(insets); |
| 603 |
getSurfaceTransactionHelper() |
| 604 |
.round(tx, leash, sourceBounds, bounds) |
| 605 |
.shadow(tx, leash, shouldApplyShadowRadius()); |
| 606 |
} |
| 607 |
} |
| 608 |
if (!handlePipTransaction(leash, tx, bounds)) { |
| 609 |
tx.apply(); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
private void applyRotation(SurfaceControl.Transaction tx, SurfaceControl leash, |
| 614 |
float fraction, Rect start, Rect end) { |
| 615 |
if (!end.equals(lastEndRect)) { |
| 616 |
|
| 617 |
|
| 618 |
rotatedEndRect.set(endValue); |
| 619 |
rotateBounds(rotatedEndRect, initialSourceValue, rotationDelta); |
| 620 |
lastEndRect.set(end); |
| 621 |
} |
| 622 |
final Rect bounds = mRectEvaluator.evaluate(fraction, start, rotatedEndRect); |
| 623 |
setCurrentValue(bounds); |
| 624 |
final Rect insets = computeInsets(fraction); |
| 625 |
final float degree, x, y; |
| 626 |
if (Transitions.SHELL_TRANSITIONS_ROTATION) { |
| 627 |
if (rotationDelta == ROTATION_90) { |
| 628 |
degree = 90 * (1 - fraction); |
| 629 |
x = fraction * (end.left - start.left) |
| 630 |
+ start.left + start.width() * (1 - fraction); |
| 631 |
y = fraction * (end.top - start.top) + start.top; |
| 632 |
} else { |
| 633 |
degree = -90 * (1 - fraction); |
| 634 |
x = fraction * (end.left - start.left) + start.left; |
| 635 |
y = fraction * (end.top - start.top) |
| 636 |
+ start.top + start.height() * (1 - fraction); |
| 637 |
} |
| 638 |
} else { |
| 639 |
if (rotationDelta == ROTATION_90) { |
| 640 |
degree = 90 * fraction; |
| 641 |
x = fraction * (end.right - start.left) + start.left; |
| 642 |
y = fraction * (end.top - start.top) + start.top; |
| 643 |
} else { |
| 644 |
degree = -90 * fraction; |
| 645 |
x = fraction * (end.left - start.left) + start.left; |
| 646 |
y = fraction * (end.bottom - start.top) + start.top; |
| 647 |
} |
| 648 |
} |
| 649 |
final Rect sourceBounds = new Rect(initialContainerRect); |
| 650 |
sourceBounds.inset(insets); |
| 651 |
getSurfaceTransactionHelper() |
| 652 |
.rotateAndScaleWithCrop(tx, leash, initialContainerRect, bounds, |
| 653 |
insets, degree, x, y, isOutPipDirection, |
| 654 |
rotationDelta == ROTATION_270 ); |
| 655 |
if (shouldApplyCornerRadius()) { |
| 656 |
getSurfaceTransactionHelper() |
| 657 |
.round(tx, leash, sourceBounds, bounds) |
| 658 |
.shadow(tx, leash, shouldApplyShadowRadius()); |
| 659 |
} |
| 660 |
tx.apply(); |
| 661 |
} |
| 662 |
|
| 663 |
private Rect computeInsets(float fraction) { |
| 664 |
if (sourceHintRectInsets == null) { |
| 665 |
return zeroInsets; |
| 666 |
} |
| 667 |
final Rect startRect = isOutPipDirection ? sourceHintRectInsets : zeroInsets; |
| 668 |
final Rect endRect = isOutPipDirection ? zeroInsets : sourceHintRectInsets; |
| 669 |
return mInsetsEvaluator.evaluate(fraction, startRect, endRect); |
| 670 |
} |
| 671 |
|
| 672 |
@Override |
| 673 |
void onStartTransaction(SurfaceControl leash, SurfaceControl.Transaction tx) { |
| 674 |
getSurfaceTransactionHelper() |
| 675 |
.alpha(tx, leash, 1f) |
| 676 |
.round(tx, leash, shouldApplyCornerRadius()) |
| 677 |
.shadow(tx, leash, shouldApplyShadowRadius()); |
| 678 |
|
| 679 |
|
| 680 |
if (isInPipDirection(direction)) { |
| 681 |
tx.setWindowCrop(leash, getStartValue()); |
| 682 |
} |
| 683 |
tx.show(leash); |
| 684 |
tx.apply(); |
| 685 |
} |
| 686 |
|
| 687 |
@Override |
| 688 |
void onEndTransaction(SurfaceControl leash, SurfaceControl.Transaction tx, |
| 689 |
int transitionDirection) { |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
final Rect destBounds = getDestinationBounds(); |
| 694 |
getSurfaceTransactionHelper().resetScale(tx, leash, destBounds); |
| 695 |
if (isOutPipDirection(transitionDirection)) { |
| 696 |
|
| 697 |
tx.setMatrix(leash, 1, 0, 0, 1); |
| 698 |
tx.setPosition(leash, 0, 0); |
| 699 |
tx.setWindowCrop(leash, 0, 0); |
| 700 |
} else { |
| 701 |
getSurfaceTransactionHelper().crop(tx, leash, destBounds); |
| 702 |
} |
| 703 |
if (mContentOverlay != null) { |
| 704 |
mContentOverlay.onAnimationEnd(tx, destBounds); |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
@Override |
| 709 |
public void updateEndValue(Rect endValue) { |
| 710 |
super.updateEndValue(endValue); |
| 711 |
if (mStartValue != null && mCurrentValue != null) { |
| 712 |
mStartValue.set(mCurrentValue); |
| 713 |
} |
| 714 |
} |
| 715 |
}; |
| 716 |
} |
| 717 |
} |
| 718 |
} |