Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/unfold/UnfoldAnimationController.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.unfold;
18
19 import android.annotation.NonNull;
20 import android.app.ActivityManager.RunningTaskInfo;
21 import android.app.TaskInfo;
22 import android.util.SparseArray;
23 import android.view.SurfaceControl;
24
25 import com.android.wm.shell.common.ShellExecutor;
26 import com.android.wm.shell.common.TransactionPool;
27 import com.android.wm.shell.sysui.ShellInit;
28 import com.android.wm.shell.unfold.ShellUnfoldProgressProvider.UnfoldListener;
29 import com.android.wm.shell.unfold.animation.UnfoldTaskAnimator;
30
31 import java.util.List;
32 import java.util.Optional;
33
34 import dagger.Lazy;
35
36 /**
37 * Manages fold/unfold animations of tasks on foldable devices.
38 * When folding or unfolding a foldable device we play animations that
39 * transform task cropping/scaling/rounded corners.
40 *
41 * This controller manages:
42 * 1) Folding/unfolding when Shell transitions disabled
43 * 2) Folding when Shell transitions enabled, unfolding is managed by
44 * {@link com.android.wm.shell.unfold.UnfoldTransitionHandler}
45 */
46 public class UnfoldAnimationController implements UnfoldListener {
47
48 private final ShellUnfoldProgressProvider mUnfoldProgressProvider;
49 private final ShellExecutor mExecutor;
50 private final TransactionPool mTransactionPool;
51 private final List<UnfoldTaskAnimator> mAnimators;
52 private final Lazy<Optional<UnfoldTransitionHandler>> mUnfoldTransitionHandler;
53
54 private final SparseArray<SurfaceControl> mTaskSurfaces = new SparseArray<>();
55 private final SparseArray<UnfoldTaskAnimator> mAnimatorsByTaskId = new SparseArray<>();
56
57 /**
58 * Indicates whether we're in stage change process. This should be set to {@code true} in
59 * {@link #onStateChangeStarted()} and {@code false} in {@link #onStateChangeFinished()}.
60 */
61 private boolean mIsInStageChange;
62
63 public UnfoldAnimationController(
64 @NonNull ShellInit shellInit,
65 @NonNull TransactionPool transactionPool,
66 @NonNull ShellUnfoldProgressProvider unfoldProgressProvider,
67 @NonNull List<UnfoldTaskAnimator> animators,
68 @NonNull Lazy<Optional<UnfoldTransitionHandler>> unfoldTransitionHandler,
69 @NonNull ShellExecutor executor) {
70 mUnfoldProgressProvider = unfoldProgressProvider;
71 mUnfoldTransitionHandler = unfoldTransitionHandler;
72 mTransactionPool = transactionPool;
73 mExecutor = executor;
74 mAnimators = animators;
75 // TODO(b/238217847): Temporarily add this check here until we can remove the dynamic
76 // override for this controller from the base module
77 if (unfoldProgressProvider != ShellUnfoldProgressProvider.NO_PROVIDER) {
78 shellInit.addInitCallback(this::onInit, this);
79 }
80 }
81
82 /**
83 * Initializes the controller, starts listening for the external events
84 */
85 public void onInit() {
86 mUnfoldProgressProvider.addListener(mExecutor, this);
87
88 for (int i = 0; i < mAnimators.size(); i++) {
89 final UnfoldTaskAnimator animator = mAnimators.get(i);
90 animator.init();
91 // TODO(b/238217847): See #provideSplitTaskUnfoldAnimatorBase
92 mExecutor.executeDelayed(animator::start, 0);
93 }
94 }
95
96 /**
97 * Called when a task appeared
98 * @param taskInfo info for the appeared task
99 * @param leash surface leash for the appeared task
100 */
101 public void onTaskAppeared(RunningTaskInfo taskInfo, SurfaceControl leash) {
102 mTaskSurfaces.put(taskInfo.taskId, leash);
103
104 // Find the first matching animator
105 for (int i = 0; i < mAnimators.size(); i++) {
106 final UnfoldTaskAnimator animator = mAnimators.get(i);
107 if (animator.isApplicableTask(taskInfo)) {
108 mAnimatorsByTaskId.put(taskInfo.taskId, animator);
109 animator.onTaskAppeared(taskInfo, leash);
110 break;
111 }
112 }
113 }
114
115 /**
116 * Called when task info changed
117 * @param taskInfo info for the changed task
118 */
119 public void onTaskInfoChanged(RunningTaskInfo taskInfo) {
120 final UnfoldTaskAnimator animator = mAnimatorsByTaskId.get(taskInfo.taskId);
121 final boolean isCurrentlyApplicable = animator != null;
122
123 if (isCurrentlyApplicable) {
124 final boolean isApplicable = animator.isApplicableTask(taskInfo);
125 if (isApplicable) {
126 // Still applicable, send update
127 animator.onTaskChanged(taskInfo);
128 } else {
129 // Became inapplicable
130 maybeResetTask(animator, taskInfo);
131 animator.onTaskVanished(taskInfo);
132 mAnimatorsByTaskId.remove(taskInfo.taskId);
133 }
134 } else {
135 // Find the first matching animator
136 for (int i = 0; i < mAnimators.size(); i++) {
137 final UnfoldTaskAnimator currentAnimator = mAnimators.get(i);
138 if (currentAnimator.isApplicableTask(taskInfo)) {
139 // Became applicable
140 mAnimatorsByTaskId.put(taskInfo.taskId, currentAnimator);
141
142 SurfaceControl leash = mTaskSurfaces.get(taskInfo.taskId);
143 currentAnimator.onTaskAppeared(taskInfo, leash);
144 break;
145 }
146 }
147 }
148 }
149
150 /**
151 * Called when a task vanished
152 * @param taskInfo info for the vanished task
153 */
154 public void onTaskVanished(RunningTaskInfo taskInfo) {
155 mTaskSurfaces.remove(taskInfo.taskId);
156
157 final UnfoldTaskAnimator animator = mAnimatorsByTaskId.get(taskInfo.taskId);
158 final boolean isCurrentlyApplicable = animator != null;
159
160 if (isCurrentlyApplicable) {
161 maybeResetTask(animator, taskInfo);
162 animator.onTaskVanished(taskInfo);
163 mAnimatorsByTaskId.remove(taskInfo.taskId);
164 }
165 }
166
167 @Override
168 public void onStateChangeStarted() {
169 if (mUnfoldTransitionHandler.get().get().willHandleTransition()) {
170 return;
171 }
172
173 mIsInStageChange = true;
174 SurfaceControl.Transaction transaction = null;
175 for (int i = 0; i < mAnimators.size(); i++) {
176 final UnfoldTaskAnimator animator = mAnimators.get(i);
177 if (animator.hasActiveTasks()) {
178 if (transaction == null) transaction = mTransactionPool.acquire();
179 animator.prepareStartTransaction(transaction);
180 }
181 }
182
183 if (transaction != null) {
184 transaction.apply();
185 mTransactionPool.release(transaction);
186 }
187 }
188
189 @Override
190 public void onStateChangeProgress(float progress) {
191 if (mUnfoldTransitionHandler.get().get().willHandleTransition()) {
192 return;
193 }
194
195 SurfaceControl.Transaction transaction = null;
196 for (int i = 0; i < mAnimators.size(); i++) {
197 final UnfoldTaskAnimator animator = mAnimators.get(i);
198 if (animator.hasActiveTasks()) {
199 if (transaction == null) transaction = mTransactionPool.acquire();
200 animator.applyAnimationProgress(progress, transaction);
201 }
202 }
203
204 if (transaction != null) {
205 transaction.apply();
206 mTransactionPool.release(transaction);
207 }
208 }
209
210 @Override
211 public void onStateChangeFinished() {
212 if (mUnfoldTransitionHandler.get().get().willHandleTransition()) {
213 return;
214 }
215
216 final SurfaceControl.Transaction transaction = mTransactionPool.acquire();
217
218 for (int i = 0; i < mAnimators.size(); i++) {
219 final UnfoldTaskAnimator animator = mAnimators.get(i);
220 animator.resetAllSurfaces(transaction);
221 animator.prepareFinishTransaction(transaction);
222 }
223
224 transaction.apply();
225
226 mTransactionPool.release(transaction);
227 mIsInStageChange = false;
228 }
229
230 private void maybeResetTask(UnfoldTaskAnimator animator, TaskInfo taskInfo) {
231 if (!mIsInStageChange) {
232 // No need to resetTask if there is no ongoing state change.
233 return;
234 }
235 final SurfaceControl.Transaction transaction = mTransactionPool.acquire();
236 animator.resetSurface(taskInfo, transaction);
237 transaction.apply();
238 mTransactionPool.release(transaction);
239 }
240 }