| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.activityembedding; |
| 18 |
|
| 19 |
import static android.view.WindowManager.TRANSIT_CHANGE; |
| 20 |
import static android.view.WindowManager.TRANSIT_CLOSE; |
| 21 |
import static android.view.WindowManagerPolicyConstants.TYPE_LAYER_OFFSET; |
| 22 |
import static android.window.TransitionInfo.FLAG_IS_BEHIND_STARTING_WINDOW; |
| 23 |
|
| 24 |
import static com.android.wm.shell.transition.TransitionAnimationHelper.addBackgroundToTransition; |
| 25 |
import static com.android.wm.shell.transition.TransitionAnimationHelper.edgeExtendWindow; |
| 26 |
import static com.android.wm.shell.transition.TransitionAnimationHelper.getTransitionBackgroundColorIfSet; |
| 27 |
|
| 28 |
import android.animation.Animator; |
| 29 |
import android.animation.ValueAnimator; |
| 30 |
import android.content.Context; |
| 31 |
import android.graphics.Rect; |
| 32 |
import android.os.IBinder; |
| 33 |
import android.util.ArraySet; |
| 34 |
import android.util.Log; |
| 35 |
import android.view.SurfaceControl; |
| 36 |
import android.view.animation.Animation; |
| 37 |
import android.window.TransitionInfo; |
| 38 |
import android.window.WindowContainerToken; |
| 39 |
|
| 40 |
import androidx.annotation.NonNull; |
| 41 |
import androidx.annotation.Nullable; |
| 42 |
|
| 43 |
import com.android.internal.annotations.VisibleForTesting; |
| 44 |
import com.android.wm.shell.common.ScreenshotUtils; |
| 45 |
import com.android.wm.shell.transition.Transitions; |
| 46 |
|
| 47 |
import java.util.ArrayList; |
| 48 |
import java.util.List; |
| 49 |
import java.util.Set; |
| 50 |
import java.util.function.Consumer; |
| 51 |
|
| 52 |
|
| 53 |
class ActivityEmbeddingAnimationRunner { |
| 54 |
|
| 55 |
private static final String TAG = "ActivityEmbeddingAnimR"; |
| 56 |
|
| 57 |
private final ActivityEmbeddingController mController; |
| 58 |
@VisibleForTesting |
| 59 |
final ActivityEmbeddingAnimationSpec mAnimationSpec; |
| 60 |
|
| 61 |
ActivityEmbeddingAnimationRunner(@NonNull Context context, |
| 62 |
@NonNull ActivityEmbeddingController controller) { |
| 63 |
mController = controller; |
| 64 |
mAnimationSpec = new ActivityEmbeddingAnimationSpec(context); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
void startAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info, |
| 69 |
@NonNull SurfaceControl.Transaction startTransaction, |
| 70 |
@NonNull SurfaceControl.Transaction finishTransaction) { |
| 71 |
|
| 72 |
|
| 73 |
final List<Consumer<SurfaceControl.Transaction>> postStartTransactionCallbacks = |
| 74 |
new ArrayList<>(); |
| 75 |
final Animator animator = createAnimator(info, startTransaction, finishTransaction, |
| 76 |
() -> mController.onAnimationFinished(transition), postStartTransactionCallbacks); |
| 77 |
|
| 78 |
|
| 79 |
if (!postStartTransactionCallbacks.isEmpty()) { |
| 80 |
|
| 81 |
|
| 82 |
startTransaction.apply(true ); |
| 83 |
|
| 84 |
|
| 85 |
final SurfaceControl.Transaction t = new SurfaceControl.Transaction(); |
| 86 |
for (Consumer<SurfaceControl.Transaction> postStartTransactionCallback : |
| 87 |
postStartTransactionCallbacks) { |
| 88 |
postStartTransactionCallback.accept(t); |
| 89 |
} |
| 90 |
t.apply(); |
| 91 |
animator.start(); |
| 92 |
} else { |
| 93 |
startTransaction.apply(); |
| 94 |
animator.start(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
void setAnimScaleSetting(float scale) { |
| 103 |
mAnimationSpec.setAnimScaleSetting(scale); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
@VisibleForTesting |
| 108 |
@NonNull |
| 109 |
Animator createAnimator(@NonNull TransitionInfo info, |
| 110 |
@NonNull SurfaceControl.Transaction startTransaction, |
| 111 |
@NonNull SurfaceControl.Transaction finishTransaction, |
| 112 |
@NonNull Runnable animationFinishCallback, |
| 113 |
@NonNull List<Consumer<SurfaceControl.Transaction>> postStartTransactionCallbacks) { |
| 114 |
final List<ActivityEmbeddingAnimationAdapter> adapters = createAnimationAdapters(info, |
| 115 |
startTransaction); |
| 116 |
final ValueAnimator animator = ValueAnimator.ofFloat(0, 1); |
| 117 |
long duration = 0; |
| 118 |
if (adapters.isEmpty()) { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
prepareForJumpCut(info, startTransaction); |
| 123 |
} else { |
| 124 |
addEdgeExtensionIfNeeded(startTransaction, finishTransaction, |
| 125 |
postStartTransactionCallbacks, adapters); |
| 126 |
addBackgroundColorIfNeeded(info, startTransaction, finishTransaction, adapters); |
| 127 |
for (ActivityEmbeddingAnimationAdapter adapter : adapters) { |
| 128 |
duration = Math.max(duration, adapter.getDurationHint()); |
| 129 |
} |
| 130 |
animator.addUpdateListener((anim) -> { |
| 131 |
|
| 132 |
final SurfaceControl.Transaction t = new SurfaceControl.Transaction(); |
| 133 |
for (ActivityEmbeddingAnimationAdapter adapter : adapters) { |
| 134 |
adapter.onAnimationUpdate(t, animator.getCurrentPlayTime()); |
| 135 |
} |
| 136 |
t.apply(); |
| 137 |
}); |
| 138 |
} |
| 139 |
animator.setDuration(duration); |
| 140 |
animator.addListener(new Animator.AnimatorListener() { |
| 141 |
@Override |
| 142 |
public void onAnimationStart(Animator animation) {} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void onAnimationEnd(Animator animation) { |
| 146 |
final SurfaceControl.Transaction t = new SurfaceControl.Transaction(); |
| 147 |
for (ActivityEmbeddingAnimationAdapter adapter : adapters) { |
| 148 |
adapter.onAnimationEnd(t); |
| 149 |
} |
| 150 |
t.apply(); |
| 151 |
animationFinishCallback.run(); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void onAnimationCancel(Animator animation) {} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void onAnimationRepeat(Animator animation) {} |
| 159 |
}); |
| 160 |
return animator; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
@NonNull |
| 168 |
private List<ActivityEmbeddingAnimationAdapter> createAnimationAdapters( |
| 169 |
@NonNull TransitionInfo info, @NonNull SurfaceControl.Transaction startTransaction) { |
| 170 |
boolean isChangeTransition = false; |
| 171 |
for (TransitionInfo.Change change : info.getChanges()) { |
| 172 |
if (change.hasFlags(FLAG_IS_BEHIND_STARTING_WINDOW)) { |
| 173 |
|
| 174 |
return new ArrayList<>(); |
| 175 |
} |
| 176 |
if (!isChangeTransition && change.getMode() == TRANSIT_CHANGE |
| 177 |
&& !change.getStartAbsBounds().equals(change.getEndAbsBounds())) { |
| 178 |
isChangeTransition = true; |
| 179 |
} |
| 180 |
} |
| 181 |
if (isChangeTransition) { |
| 182 |
return createChangeAnimationAdapters(info, startTransaction); |
| 183 |
} |
| 184 |
if (Transitions.isClosingType(info.getType())) { |
| 185 |
return createCloseAnimationAdapters(info); |
| 186 |
} |
| 187 |
return createOpenAnimationAdapters(info); |
| 188 |
} |
| 189 |
|
| 190 |
@NonNull |
| 191 |
private List<ActivityEmbeddingAnimationAdapter> createOpenAnimationAdapters( |
| 192 |
@NonNull TransitionInfo info) { |
| 193 |
return createOpenCloseAnimationAdapters(info, true , |
| 194 |
mAnimationSpec::loadOpenAnimation); |
| 195 |
} |
| 196 |
|
| 197 |
@NonNull |
| 198 |
private List<ActivityEmbeddingAnimationAdapter> createCloseAnimationAdapters( |
| 199 |
@NonNull TransitionInfo info) { |
| 200 |
return createOpenCloseAnimationAdapters(info, false , |
| 201 |
mAnimationSpec::loadCloseAnimation); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
@NonNull |
| 209 |
private List<ActivityEmbeddingAnimationAdapter> createOpenCloseAnimationAdapters( |
| 210 |
@NonNull TransitionInfo info, boolean isOpening, |
| 211 |
@NonNull AnimationProvider animationProvider) { |
| 212 |
|
| 213 |
|
| 214 |
final List<TransitionInfo.Change> openingChanges = new ArrayList<>(); |
| 215 |
final List<TransitionInfo.Change> closingChanges = new ArrayList<>(); |
| 216 |
final Rect openingWholeScreenBounds = new Rect(); |
| 217 |
final Rect closingWholeScreenBounds = new Rect(); |
| 218 |
for (TransitionInfo.Change change : info.getChanges()) { |
| 219 |
if (Transitions.isOpeningType(change.getMode())) { |
| 220 |
openingChanges.add(change); |
| 221 |
openingWholeScreenBounds.union(change.getEndAbsBounds()); |
| 222 |
} else { |
| 223 |
closingChanges.add(change); |
| 224 |
closingWholeScreenBounds.union(change.getEndAbsBounds()); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
int offsetLayer = TYPE_LAYER_OFFSET; |
| 231 |
final List<ActivityEmbeddingAnimationAdapter> adapters = new ArrayList<>(); |
| 232 |
for (TransitionInfo.Change change : openingChanges) { |
| 233 |
final ActivityEmbeddingAnimationAdapter adapter = createOpenCloseAnimationAdapter( |
| 234 |
info, change, animationProvider, openingWholeScreenBounds); |
| 235 |
if (isOpening) { |
| 236 |
adapter.overrideLayer(offsetLayer++); |
| 237 |
} |
| 238 |
adapters.add(adapter); |
| 239 |
} |
| 240 |
for (TransitionInfo.Change change : closingChanges) { |
| 241 |
final ActivityEmbeddingAnimationAdapter adapter = createOpenCloseAnimationAdapter( |
| 242 |
info, change, animationProvider, closingWholeScreenBounds); |
| 243 |
if (!isOpening) { |
| 244 |
adapter.overrideLayer(offsetLayer++); |
| 245 |
} |
| 246 |
adapters.add(adapter); |
| 247 |
} |
| 248 |
return adapters; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
private void addEdgeExtensionIfNeeded(@NonNull SurfaceControl.Transaction startTransaction, |
| 253 |
@NonNull SurfaceControl.Transaction finishTransaction, |
| 254 |
@NonNull List<Consumer<SurfaceControl.Transaction>> postStartTransactionCallbacks, |
| 255 |
@NonNull List<ActivityEmbeddingAnimationAdapter> adapters) { |
| 256 |
for (ActivityEmbeddingAnimationAdapter adapter : adapters) { |
| 257 |
final Animation animation = adapter.mAnimation; |
| 258 |
if (!animation.hasExtension()) { |
| 259 |
continue; |
| 260 |
} |
| 261 |
final TransitionInfo.Change change = adapter.mChange; |
| 262 |
if (Transitions.isOpeningType(adapter.mChange.getMode())) { |
| 263 |
|
| 264 |
|
| 265 |
postStartTransactionCallbacks.add( |
| 266 |
t -> edgeExtendWindow(change, animation, t, finishTransaction)); |
| 267 |
} else { |
| 268 |
|
| 269 |
edgeExtendWindow(change, animation, startTransaction, finishTransaction); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
private void addBackgroundColorIfNeeded(@NonNull TransitionInfo info, |
| 276 |
@NonNull SurfaceControl.Transaction startTransaction, |
| 277 |
@NonNull SurfaceControl.Transaction finishTransaction, |
| 278 |
@NonNull List<ActivityEmbeddingAnimationAdapter> adapters) { |
| 279 |
for (ActivityEmbeddingAnimationAdapter adapter : adapters) { |
| 280 |
final int backgroundColor = getTransitionBackgroundColorIfSet(info, adapter.mChange, |
| 281 |
adapter.mAnimation, 0 ); |
| 282 |
if (backgroundColor != 0) { |
| 283 |
|
| 284 |
addBackgroundToTransition(info.getRootLeash(), backgroundColor, startTransaction, |
| 285 |
finishTransaction); |
| 286 |
return; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
@NonNull |
| 292 |
private ActivityEmbeddingAnimationAdapter createOpenCloseAnimationAdapter( |
| 293 |
@NonNull TransitionInfo info, @NonNull TransitionInfo.Change change, |
| 294 |
@NonNull AnimationProvider animationProvider, @NonNull Rect wholeAnimationBounds) { |
| 295 |
final Animation animation = animationProvider.get(info, change, wholeAnimationBounds); |
| 296 |
return new ActivityEmbeddingAnimationAdapter(animation, change, change.getLeash(), |
| 297 |
wholeAnimationBounds); |
| 298 |
} |
| 299 |
|
| 300 |
@NonNull |
| 301 |
private List<ActivityEmbeddingAnimationAdapter> createChangeAnimationAdapters( |
| 302 |
@NonNull TransitionInfo info, @NonNull SurfaceControl.Transaction startTransaction) { |
| 303 |
if (shouldUseJumpCutForChangeTransition(info)) { |
| 304 |
return new ArrayList<>(); |
| 305 |
} |
| 306 |
|
| 307 |
final List<ActivityEmbeddingAnimationAdapter> adapters = new ArrayList<>(); |
| 308 |
final Set<TransitionInfo.Change> handledChanges = new ArraySet<>(); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
Animation changeAnimation = null; |
| 319 |
Rect parentBounds = new Rect(); |
| 320 |
for (TransitionInfo.Change change : info.getChanges()) { |
| 321 |
if (change.getMode() != TRANSIT_CHANGE |
| 322 |
|| change.getStartAbsBounds().equals(change.getEndAbsBounds())) { |
| 323 |
continue; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
handledChanges.add(change); |
| 328 |
final WindowContainerToken parentToken = change.getParent(); |
| 329 |
TransitionInfo.Change boundsAnimationChange = change; |
| 330 |
if (parentToken != null) { |
| 331 |
|
| 332 |
|
| 333 |
final TransitionInfo.Change parentChange = info.getChange(parentToken); |
| 334 |
if (parentChange != null && Transitions.isOpeningType(parentChange.getMode())) { |
| 335 |
|
| 336 |
|
| 337 |
handledChanges.add(parentChange); |
| 338 |
boundsAnimationChange = parentChange; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
parentBounds.union(boundsAnimationChange.getStartAbsBounds()); |
| 345 |
parentBounds.union(boundsAnimationChange.getEndAbsBounds()); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
final Animation[] animations = mAnimationSpec.createChangeBoundsChangeAnimations(change, |
| 350 |
parentBounds); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
changeAnimation = animations[1]; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
final SurfaceControl screenshotLeash = getOrCreateScreenshot(change, |
| 359 |
boundsAnimationChange, startTransaction); |
| 360 |
if (screenshotLeash != null) { |
| 361 |
|
| 362 |
|
| 363 |
adapters.add(new ActivityEmbeddingAnimationAdapter.SnapshotAdapter( |
| 364 |
animations[0], change, screenshotLeash)); |
| 365 |
} else { |
| 366 |
Log.e(TAG, "Failed to take screenshot for change=" + change); |
| 367 |
} |
| 368 |
|
| 369 |
adapters.add(new ActivityEmbeddingAnimationAdapter.BoundsChangeAdapter( |
| 370 |
animations[1], boundsAnimationChange)); |
| 371 |
} |
| 372 |
|
| 373 |
if (parentBounds.isEmpty()) { |
| 374 |
throw new IllegalStateException( |
| 375 |
"There should be at least one changing window to play the change animation"); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
boolean shouldShouldBackgroundColor = true; |
| 381 |
|
| 382 |
for (TransitionInfo.Change change : info.getChanges()) { |
| 383 |
if (handledChanges.contains(change)) { |
| 384 |
|
| 385 |
continue; |
| 386 |
} |
| 387 |
|
| 388 |
final Animation animation; |
| 389 |
if ((change.getParent() != null |
| 390 |
&& handledChanges.contains(info.getChange(change.getParent()))) |
| 391 |
|| change.getMode() == TRANSIT_CHANGE) { |
| 392 |
|
| 393 |
|
| 394 |
animation = ActivityEmbeddingAnimationSpec.createNoopAnimation(change); |
| 395 |
} else if (Transitions.isClosingType(change.getMode())) { |
| 396 |
animation = mAnimationSpec.createChangeBoundsCloseAnimation(change, parentBounds); |
| 397 |
shouldShouldBackgroundColor = false; |
| 398 |
} else { |
| 399 |
animation = mAnimationSpec.createChangeBoundsOpenAnimation(change, parentBounds); |
| 400 |
shouldShouldBackgroundColor = false; |
| 401 |
} |
| 402 |
adapters.add(new ActivityEmbeddingAnimationAdapter(animation, change)); |
| 403 |
} |
| 404 |
|
| 405 |
if (shouldShouldBackgroundColor && changeAnimation != null) { |
| 406 |
|
| 407 |
|
| 408 |
changeAnimation.setShowBackdrop(true); |
| 409 |
} |
| 410 |
|
| 411 |
return adapters; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
@Nullable |
| 420 |
private SurfaceControl getOrCreateScreenshot(@NonNull TransitionInfo.Change screenshotChange, |
| 421 |
@NonNull TransitionInfo.Change animationChange, |
| 422 |
@NonNull SurfaceControl.Transaction t) { |
| 423 |
final SurfaceControl screenshotLeash = screenshotChange.getSnapshot(); |
| 424 |
if (screenshotLeash != null) { |
| 425 |
|
| 426 |
|
| 427 |
t.reparent(screenshotLeash, animationChange.getLeash()); |
| 428 |
return screenshotLeash; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
final Rect cropBounds = new Rect(screenshotChange.getStartAbsBounds()); |
| 433 |
cropBounds.offsetTo(0, 0); |
| 434 |
return ScreenshotUtils.takeScreenshot(t, screenshotChange.getLeash(), |
| 435 |
animationChange.getLeash(), cropBounds, Integer.MAX_VALUE); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
private boolean shouldUseJumpCutForChangeTransition(@NonNull TransitionInfo info) { |
| 446 |
|
| 447 |
|
| 448 |
final List<TransitionInfo.Change> changingChanges = new ArrayList<>(); |
| 449 |
for (TransitionInfo.Change change : info.getChanges()) { |
| 450 |
if (change.getMode() != TRANSIT_CHANGE |
| 451 |
|| change.getStartAbsBounds().equals(change.getEndAbsBounds())) { |
| 452 |
continue; |
| 453 |
} |
| 454 |
changingChanges.add(change); |
| 455 |
final WindowContainerToken parentToken = change.getParent(); |
| 456 |
if (parentToken != null) { |
| 457 |
|
| 458 |
|
| 459 |
final TransitionInfo.Change parentChange = info.getChange(parentToken); |
| 460 |
if (parentChange != null && Transitions.isOpeningType(parentChange.getMode())) { |
| 461 |
changingChanges.add(parentChange); |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
if (changingChanges.isEmpty()) { |
| 466 |
|
| 467 |
return true; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
boolean hasOpeningWindow = false; |
| 472 |
boolean hasClosingWindow = false; |
| 473 |
for (TransitionInfo.Change change : info.getChanges()) { |
| 474 |
if (changingChanges.contains(change)) { |
| 475 |
continue; |
| 476 |
} |
| 477 |
if (change.getParent() != null |
| 478 |
&& changingChanges.contains(info.getChange(change.getParent()))) { |
| 479 |
|
| 480 |
continue; |
| 481 |
} |
| 482 |
hasOpeningWindow |= Transitions.isOpeningType(change.getMode()); |
| 483 |
hasClosingWindow |= Transitions.isClosingType(change.getMode()); |
| 484 |
} |
| 485 |
return hasOpeningWindow && hasClosingWindow; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
private void prepareForJumpCut(@NonNull TransitionInfo info, |
| 490 |
@NonNull SurfaceControl.Transaction startTransaction) { |
| 491 |
for (TransitionInfo.Change change : info.getChanges()) { |
| 492 |
final SurfaceControl leash = change.getLeash(); |
| 493 |
startTransaction.setPosition(leash, |
| 494 |
change.getEndRelOffset().x, change.getEndRelOffset().y); |
| 495 |
startTransaction.setWindowCrop(leash, |
| 496 |
change.getEndAbsBounds().width(), change.getEndAbsBounds().height()); |
| 497 |
if (change.getMode() == TRANSIT_CLOSE) { |
| 498 |
startTransaction.hide(leash); |
| 499 |
} else { |
| 500 |
startTransaction.show(leash); |
| 501 |
startTransaction.setAlpha(leash, 1f); |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
private interface AnimationProvider { |
| 508 |
Animation get(@NonNull TransitionInfo info, @NonNull TransitionInfo.Change change, |
| 509 |
@NonNull Rect animationBounds); |
| 510 |
} |
| 511 |
} |