Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/activityembedding/ActivityEmbeddingController.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.window.TransitionInfo.FLAG_FILLS_TASK;
20 import static android.window.TransitionInfo.FLAG_IN_TASK_WITH_EMBEDDED_ACTIVITY;
21
22 import static java.util.Objects.requireNonNull;
23
24 import android.content.Context;
25 import android.os.IBinder;
26 import android.util.ArrayMap;
27 import android.view.SurfaceControl;
28 import android.window.TransitionInfo;
29 import android.window.TransitionRequestInfo;
30 import android.window.WindowContainerTransaction;
31
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34
35 import com.android.internal.annotations.VisibleForTesting;
36 import com.android.wm.shell.sysui.ShellInit;
37 import com.android.wm.shell.transition.Transitions;
38
39 /**
40 * Responsible for handling ActivityEmbedding related transitions.
41 */
42 public class ActivityEmbeddingController implements Transitions.TransitionHandler {
43
44 private final Context mContext;
45 @VisibleForTesting
46 final Transitions mTransitions;
47 @VisibleForTesting
48 final ActivityEmbeddingAnimationRunner mAnimationRunner;
49
50 /**
51 * Keeps track of the currently-running transition callback associated with each transition
52 * token.
53 */
54 private final ArrayMap<IBinder, Transitions.TransitionFinishCallback> mTransitionCallbacks =
55 new ArrayMap<>();
56
57 private ActivityEmbeddingController(@NonNull Context context, @NonNull ShellInit shellInit,
58 @NonNull Transitions transitions) {
59 mContext = requireNonNull(context);
60 mTransitions = requireNonNull(transitions);
61 mAnimationRunner = new ActivityEmbeddingAnimationRunner(context, this);
62
63 shellInit.addInitCallback(this::onInit, this);
64 }
65
66 /**
67 * Creates {@link ActivityEmbeddingController}, returns {@code null} if the feature is not
68 * supported.
69 */
70 @Nullable
71 public static ActivityEmbeddingController create(@NonNull Context context,
72 @NonNull ShellInit shellInit, @NonNull Transitions transitions) {
73 return Transitions.ENABLE_SHELL_TRANSITIONS
74 ? new ActivityEmbeddingController(context, shellInit, transitions)
75 : null;
76 }
77
78 /** Registers to handle transitions. */
79 public void onInit() {
80 mTransitions.addHandler(this);
81 }
82
83 @Override
84 public boolean startAnimation(@NonNull IBinder transition, @NonNull TransitionInfo info,
85 @NonNull SurfaceControl.Transaction startTransaction,
86 @NonNull SurfaceControl.Transaction finishTransaction,
87 @NonNull Transitions.TransitionFinishCallback finishCallback) {
88 boolean containsEmbeddingSplit = false;
89 for (TransitionInfo.Change change : info.getChanges()) {
90 if (!change.hasFlags(FLAG_IN_TASK_WITH_EMBEDDED_ACTIVITY)) {
91 // Only animate the transition if all changes are in a Task with ActivityEmbedding.
92 return false;
93 }
94 if (!containsEmbeddingSplit && !change.hasFlags(FLAG_FILLS_TASK)) {
95 // Whether the Task contains any ActivityEmbedding split before or after the
96 // transition.
97 containsEmbeddingSplit = true;
98 }
99 }
100 if (!containsEmbeddingSplit) {
101 // Let the system to play the default animation if there is no ActivityEmbedding split
102 // window. This allows to play the app customized animation when there is no embedding,
103 // such as the device is in a folded state.
104 return false;
105 }
106
107 // Start ActivityEmbedding animation.
108 mTransitionCallbacks.put(transition, finishCallback);
109 mAnimationRunner.startAnimation(transition, info, startTransaction, finishTransaction);
110 return true;
111 }
112
113 @Nullable
114 @Override
115 public WindowContainerTransaction handleRequest(@NonNull IBinder transition,
116 @NonNull TransitionRequestInfo request) {
117 return null;
118 }
119
120 @Override
121 public void setAnimScaleSetting(float scale) {
122 mAnimationRunner.setAnimScaleSetting(scale);
123 }
124
125 /** Called when the animation is finished. */
126 void onAnimationFinished(@NonNull IBinder transition) {
127 final Transitions.TransitionFinishCallback callback =
128 mTransitionCallbacks.remove(transition);
129 if (callback == null) {
130 throw new IllegalStateException("No finish callback found");
131 }
132 callback.onTransitionFinished(null /* wct */, null /* wctCB */);
133 }
134 }