Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/activityembedding/ActivityEmbeddingAnimationRunner.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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.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 /** To run the ActivityEmbedding animations. */
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 /** Creates and starts animation for ActivityEmbedding transition. */
68 void startAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info,
69 @NonNull SurfaceControl.Transaction startTransaction,
70 @NonNull SurfaceControl.Transaction finishTransaction) {
71 // There may be some surface change that we want to apply after the start transaction is
72 // applied to make sure the surface is ready.
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 // Start the animation.
79 if (!postStartTransactionCallbacks.isEmpty()) {
80 // postStartTransactionCallbacks require that the start transaction is already
81 // applied to run otherwise they may result in flickers and UI inconsistencies.
82 startTransaction.apply(true /* sync */);
83
84 // Run tasks that require startTransaction to already be applied
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 * Sets transition animation scale settings value.
100 * @param scale The setting value of transition animation scale.
101 */
102 void setAnimScaleSetting(float scale) {
103 mAnimationSpec.setAnimScaleSetting(scale);
104 }
105
106 /** Creates the animator for the given {@link TransitionInfo}. */
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 // Jump cut
120 // No need to modify the animator, but to update the startTransaction with the changes'
121 // ending states.
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 // Update all adapters in the same transaction.
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 * Creates list of {@link ActivityEmbeddingAnimationAdapter} to handle animations on all window
165 * changes.
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 // Skip the animation if the windows are behind an app starting window.
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 /* isOpening */,
194 mAnimationSpec::loadOpenAnimation);
195 }
196
197 @NonNull
198 private List<ActivityEmbeddingAnimationAdapter> createCloseAnimationAdapters(
199 @NonNull TransitionInfo info) {
200 return createOpenCloseAnimationAdapters(info, false /* isOpening */,
201 mAnimationSpec::loadCloseAnimation);
202 }
203
204 /**
205 * Creates {@link ActivityEmbeddingAnimationAdapter} for OPEN and CLOSE types of transition.
206 * @param isOpening {@code true} for OPEN type, {@code false} for CLOSE type.
207 */
208 @NonNull
209 private List<ActivityEmbeddingAnimationAdapter> createOpenCloseAnimationAdapters(
210 @NonNull TransitionInfo info, boolean isOpening,
211 @NonNull AnimationProvider animationProvider) {
212 // We need to know if the change window is only a partial of the whole animation screen.
213 // If so, we will need to adjust it to make the whole animation screen looks like one.
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 // For OPEN transition, open windows should be above close windows.
229 // For CLOSE transition, open windows should be below close windows.
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 /** Adds edge extension to the surfaces that have such an animation property. */
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 // Need to screenshot after startTransaction is applied otherwise activity
264 // may not be visible or ready yet.
265 postStartTransactionCallbacks.add(
266 t -> edgeExtendWindow(change, animation, t, finishTransaction));
267 } else {
268 // Can screenshot now (before startTransaction is applied)
269 edgeExtendWindow(change, animation, startTransaction, finishTransaction);
270 }
271 }
272 }
273
274 /** Adds background color to the transition if any animation has such a property. */
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 /* defaultColor */);
282 if (backgroundColor != 0) {
283 // We only need to show one color.
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 // For the first iteration, we prepare the animation for the change type windows. This is
311 // needed because there may be window that is reparented while resizing. In such case, we
312 // will do the following:
313 // 1. Capture a screenshot from the Activity surface.
314 // 2. Attach the screenshot surface to the top of TaskFragment (Activity's parent) surface.
315 // 3. Animate the TaskFragment using Activity Change info (start/end bounds).
316 // This is because the TaskFragment surface/change won't contain the Activity's before its
317 // reparent.
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 // This is the window with bounds change.
327 handledChanges.add(change);
328 final WindowContainerToken parentToken = change.getParent();
329 TransitionInfo.Change boundsAnimationChange = change;
330 if (parentToken != null) {
331 // When the parent window is also included in the transition as an opening window,
332 // we would like to animate the parent window instead.
333 final TransitionInfo.Change parentChange = info.getChange(parentToken);
334 if (parentChange != null && Transitions.isOpeningType(parentChange.getMode())) {
335 // We won't create a separate animation for the parent, but to animate the
336 // parent for the child resizing.
337 handledChanges.add(parentChange);
338 boundsAnimationChange = parentChange;
339 }
340 }
341
342 // The TaskFragment may be enter/exit split, so we take the union of both as the parent
343 // size.
344 parentBounds.union(boundsAnimationChange.getStartAbsBounds());
345 parentBounds.union(boundsAnimationChange.getEndAbsBounds());
346
347 // There are two animations in the array. The first one is for the start leash
348 // (snapshot), and the second one is for the end leash (TaskFragment).
349 final Animation[] animations = mAnimationSpec.createChangeBoundsChangeAnimations(change,
350 parentBounds);
351 // Keep track as we might need to add background color for the animation.
352 // Although there may be multiple change animation, record one of them is sufficient
353 // because the background color will be added to the root leash for the whole animation.
354 changeAnimation = animations[1];
355
356 // Create a screenshot based on change, but attach it to the top of the
357 // boundsAnimationChange.
358 final SurfaceControl screenshotLeash = getOrCreateScreenshot(change,
359 boundsAnimationChange, startTransaction);
360 if (screenshotLeash != null) {
361 // Adapter for the starting screenshot leash.
362 // The screenshot leash will be removed in SnapshotAdapter#onAnimationEnd
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 // Adapter for the ending bounds changed leash.
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 // If there is no corresponding open/close window with the change, we should show background
379 // color to cover the empty part of the screen.
380 boolean shouldShouldBackgroundColor = true;
381 // Handle the other windows that don't have bounds change in the same transition.
382 for (TransitionInfo.Change change : info.getChanges()) {
383 if (handledChanges.contains(change)) {
384 // Skip windows that we have already handled in the previous iteration.
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 // No-op if it will be covered by the changing parent window, or it is a changing
393 // window without bounds change.
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 // Change animation may leave part of the screen empty. Show background color to cover
407 // that.
408 changeAnimation.setShowBackdrop(true);
409 }
410
411 return adapters;
412 }
413
414 /**
415 * Takes a screenshot of the given {@code screenshotChange} surface if WM Core hasn't taken one.
416 * The screenshot leash should be attached to the {@code animationChange} surface which we will
417 * animate later.
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 // If WM Core has already taken a screenshot, make sure it is reparented to the
426 // animation leash.
427 t.reparent(screenshotLeash, animationChange.getLeash());
428 return screenshotLeash;
429 }
430
431 // If WM Core hasn't taken a screenshot, take a screenshot now.
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 * Whether we should use jump cut for the change transition.
440 * This normally happens when opening a new secondary with the existing primary using a
441 * different split layout. This can be complicated, like from horizontal to vertical split with
442 * new split pairs.
443 * Uses a jump cut animation to simplify.
444 */
445 private boolean shouldUseJumpCutForChangeTransition(@NonNull TransitionInfo info) {
446 // There can be reparenting of changing Activity to new open TaskFragment, so we need to
447 // exclude both in the first iteration.
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 // When the parent window is also included in the transition as an opening window,
458 // we would like to animate the parent window instead.
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 // No changing target found.
467 return true;
468 }
469
470 // Check if the transition contains both opening and closing windows.
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 // No-op if it will be covered by the changing parent window.
480 continue;
481 }
482 hasOpeningWindow |= Transitions.isOpeningType(change.getMode());
483 hasClosingWindow |= Transitions.isClosingType(change.getMode());
484 }
485 return hasOpeningWindow && hasClosingWindow;
486 }
487
488 /** Updates the changes to end states in {@code startTransaction} for jump cut animation. */
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 /** To provide an {@link Animation} based on the transition infos. */
507 private interface AnimationProvider {
508 Animation get(@NonNull TransitionInfo info, @NonNull TransitionInfo.Change change,
509 @NonNull Rect animationBounds);
510 }
511 }