blob: c7ad17b8699bc86a508af4a1c50ce594e01ee757 [file] [log] [blame]
Winson Chunge2d72172018-01-25 17:46:20 +00001/*
2 * Copyright (C) 2017 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
17package com.android.server.wm;
18
19import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
20import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
21import static android.view.RemoteAnimationTarget.MODE_CLOSING;
22import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
23import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
24import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
25
26import android.app.ActivityManager;
27import android.app.ActivityManager.TaskSnapshot;
28import android.app.WindowConfiguration;
29import android.graphics.GraphicBuffer;
30import android.graphics.Point;
31import android.graphics.Rect;
32import android.os.Binder;
33import android.os.RemoteException;
34import android.os.SystemClock;
35import android.util.Log;
36import android.util.Slog;
37import android.view.IRecentsAnimationController;
38import android.view.IRecentsAnimationRunner;
39import android.view.RemoteAnimationTarget;
40import android.view.SurfaceControl;
41import android.view.SurfaceControl.Transaction;
42import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
43import java.io.PrintWriter;
44import java.util.ArrayList;
45
46/**
47 * Controls a single instance of the remote driven recents animation. In particular, this allows
48 * the calling SystemUI to animate the visible task windows as a part of the transition. The remote
49 * runner is provided an animation controller which allows it to take screenshots and to notify
50 * window manager when the animation is completed. In addition, window manager may also notify the
51 * app if it requires the animation to be canceled at any time (ie. due to timeout, etc.)
52 */
53public class RecentsAnimationController {
54 private static final String TAG = TAG_WITH_CLASS_NAME ? "RecentsAnimationController" : TAG_WM;
55 private static final boolean DEBUG = false;
56
57 private final WindowManagerService mService;
58 private final IRecentsAnimationRunner mRunner;
59 private final RecentsAnimationCallbacks mCallbacks;
60 private final ArrayList<TaskAnimationAdapter> mPendingAnimations = new ArrayList<>();
61
62 // The recents component app token that is shown behind the visibile tasks
63 private AppWindowToken mHomeAppToken;
64
65 // We start the RecentsAnimationController in a pending-start state since we need to wait for
66 // the wallpaper/activity to draw before we can give control to the handler to start animating
67 // the visible task surfaces
68 private boolean mPendingStart = true;
69
70 // Set when the animation has been canceled
71 private boolean mCanceled = false;
72
73 // Whether or not the input consumer is enabled. The input consumer must be both registered and
74 // enabled for it to start intercepting touch events.
75 private boolean mInputConsumerEnabled;
76
77 public interface RecentsAnimationCallbacks {
78 void onAnimationFinished(boolean moveHomeToTop);
79 }
80
81 private final IRecentsAnimationController mController =
82 new IRecentsAnimationController.Stub() {
83
84 @Override
85 public TaskSnapshot screenshotTask(int taskId) {
86 if (DEBUG) Log.d(TAG, "screenshotTask(" + taskId + "): mCanceled=" + mCanceled);
87 long token = Binder.clearCallingIdentity();
88 try {
89 synchronized (mService.getWindowManagerLock()) {
90 if (mCanceled) {
91 return null;
92 }
93 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
94 final TaskAnimationAdapter adapter = mPendingAnimations.get(i);
95 final Task task = adapter.mTask;
96 if (task.mTaskId == taskId) {
97 // TODO: Save this screenshot as the task snapshot?
98 final Rect taskFrame = new Rect();
99 task.getBounds(taskFrame);
100 final GraphicBuffer buffer = SurfaceControl.captureLayers(
101 task.getSurfaceControl().getHandle(), taskFrame, 1f);
102 final AppWindowToken topChild = task.getTopChild();
103 final WindowState mainWindow = topChild.findMainWindow();
104 return new TaskSnapshot(buffer, topChild.getConfiguration().orientation,
105 mainWindow.mStableInsets,
106 ActivityManager.isLowRamDeviceStatic() /* reduced */,
107 1.0f /* scale */);
108 }
109 }
110 return null;
111 }
112 } finally {
113 Binder.restoreCallingIdentity(token);
114 }
115 }
116
117 @Override
118 public void finish(boolean moveHomeToTop) {
119 if (DEBUG) Log.d(TAG, "finish(" + moveHomeToTop + "): mCanceled=" + mCanceled);
120 long token = Binder.clearCallingIdentity();
121 try {
122 synchronized (mService.getWindowManagerLock()) {
123 if (mCanceled) {
124 return;
125 }
126 }
127
128 // Note, the callback will handle its own synchronization, do not lock on WM lock
129 // prior to calling the callback
130 mCallbacks.onAnimationFinished(moveHomeToTop);
131 } finally {
132 Binder.restoreCallingIdentity(token);
133 }
134 }
135
136 @Override
137 public void setInputConsumerEnabled(boolean enabled) {
138 if (DEBUG) Log.d(TAG, "setInputConsumerEnabled(" + enabled + "): mCanceled="
139 + mCanceled);
140 long token = Binder.clearCallingIdentity();
141 try {
142 synchronized (mService.getWindowManagerLock()) {
143 if (mCanceled) {
144 return;
145 }
146
147 mInputConsumerEnabled = enabled;
148 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
149 mService.scheduleAnimationLocked();
150 }
151 } finally {
152 Binder.restoreCallingIdentity(token);
153 }
154 }
155 };
156
157 /**
158 * Initializes a new RecentsAnimationController.
159 *
160 * @param remoteAnimationRunner The remote runner which should be notified when the animation is
161 * ready to start or has been canceled
162 * @param callbacks Callbacks to be made when the animation finishes
163 * @param restoreHomeBehindStackId The stack id to restore the home stack behind once the
164 * animation is complete. Will be passed to the callback.
165 */
166 RecentsAnimationController(WindowManagerService service,
167 IRecentsAnimationRunner remoteAnimationRunner, RecentsAnimationCallbacks callbacks,
168 int displayId) {
169 mService = service;
170 mRunner = remoteAnimationRunner;
171 mCallbacks = callbacks;
172
173 final DisplayContent dc = mService.mRoot.getDisplayContent(displayId);
174 final ArrayList<Task> visibleTasks = dc.getVisibleTasks();
175 if (visibleTasks.isEmpty()) {
176 cancelAnimation();
177 return;
178 }
179
180 // Make leashes for each of the visible tasks and add it to the recents animation to be
181 // started
182 final int taskCount = visibleTasks.size();
183 for (int i = 0; i < taskCount; i++) {
184 final Task task = visibleTasks.get(i);
185 final WindowConfiguration config = task.getWindowConfiguration();
186 if (config.tasksAreFloating()
187 || config.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
188 || config.getActivityType() == ACTIVITY_TYPE_HOME) {
189 continue;
190 }
191 addAnimation(task);
192 }
193
194 // Adjust the wallpaper visibility for the showing home activity
195 final AppWindowToken recentsComponentAppToken =
196 dc.getHomeStack().getTopChild().getTopFullscreenAppToken();
197 if (recentsComponentAppToken != null) {
198 if (DEBUG) Log.d(TAG, "setHomeApp(" + recentsComponentAppToken.getName() + ")");
199 mHomeAppToken = recentsComponentAppToken;
200 final WallpaperController wc = dc.mWallpaperController;
201 if (recentsComponentAppToken.windowsCanBeWallpaperTarget()) {
202 dc.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
203 dc.setLayoutNeeded();
204 }
205 }
206
207 mService.mWindowPlacerLocked.performSurfacePlacement();
208 }
209
210 private void addAnimation(Task task) {
211 if (DEBUG) Log.d(TAG, "addAnimation(" + task.getName() + ")");
212 final SurfaceAnimator anim = new SurfaceAnimator(task, null /* animationFinishedCallback */,
213 mService.mAnimator::addAfterPrepareSurfacesRunnable, mService);
214 final TaskAnimationAdapter taskAdapter = new TaskAnimationAdapter(task);
215 anim.startAnimation(task.getPendingTransaction(), taskAdapter, false /* hidden */);
216 task.commitPendingTransaction();
217 mPendingAnimations.add(taskAdapter);
218 }
219
220 void startAnimation() {
221 if (DEBUG) Log.d(TAG, "startAnimation(): mPendingStart=" + mPendingStart);
222 if (!mPendingStart) {
223 return;
224 }
225 try {
226 final RemoteAnimationTarget[] appAnimations =
227 new RemoteAnimationTarget[mPendingAnimations.size()];
228 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
229 appAnimations[i] = mPendingAnimations.get(i).createRemoteAnimationApp();
230 }
231 mPendingStart = false;
232 mRunner.onAnimationStart(mController, appAnimations);
233 } catch (RemoteException e) {
234 Slog.e(TAG, "Failed to start recents animation", e);
235 }
236 }
237
238 void cancelAnimation() {
239 if (DEBUG) Log.d(TAG, "cancelAnimation()");
240 if (mCanceled) {
241 // We've already canceled the animation
242 return;
243 }
244 mCanceled = true;
245 try {
246 mRunner.onAnimationCanceled();
247 } catch (RemoteException e) {
248 Slog.e(TAG, "Failed to cancel recents animation", e);
249 }
250
251 // Clean up and return to the previous app
252 mCallbacks.onAnimationFinished(false /* moveHomeToTop */);
253 }
254
255 void cleanupAnimation() {
256 if (DEBUG) Log.d(TAG, "cleanupAnimation(): mPendingAnimations="
257 + mPendingAnimations.size());
258 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
259 final TaskAnimationAdapter adapter = mPendingAnimations.get(i);
260 adapter.mCapturedFinishCallback.onAnimationFinished(adapter);
261 }
262 mPendingAnimations.clear();
263
264 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
265 mService.scheduleAnimationLocked();
266 }
267
268 void checkAnimationReady(WallpaperController wallpaperController) {
269 if (mPendingStart) {
270 final boolean wallpaperReady = !isHomeAppOverWallpaper()
271 || (wallpaperController.getWallpaperTarget() != null
272 && wallpaperController.wallpaperTransitionReady());
273 if (wallpaperReady) {
274 mService.getRecentsAnimationController().startAnimation();
275 }
276 }
277 }
278
279 boolean isWallpaperVisible(WindowState w) {
280 return w != null && w.mAppToken != null && mHomeAppToken == w.mAppToken
281 && isHomeAppOverWallpaper();
282 }
283
284 boolean isHomeAppOverWallpaper() {
285 if (mHomeAppToken == null) {
286 return false;
287 }
288 return mHomeAppToken.windowsCanBeWallpaperTarget();
289 }
290
291 WindowState getHomeAppMainWindow() {
292 if (mHomeAppToken == null) {
293 return null;
294 }
295 return mHomeAppToken.findMainWindow();
296 }
297
298 boolean isAnimatingApp(AppWindowToken appToken) {
299 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
300 final Task task = mPendingAnimations.get(i).mTask;
301 for (int j = task.getChildCount() - 1; j >= 0; j--) {
302 final AppWindowToken app = task.getChildAt(j);
303 if (app == appToken) {
304 return true;
305 }
306 }
307 }
308 return false;
309 }
310
311 boolean isInputConsumerEnabled() {
312 return mInputConsumerEnabled;
313 }
314
315 private class TaskAnimationAdapter implements AnimationAdapter {
316
317 private Task mTask;
318 private SurfaceControl mCapturedLeash;
319 private OnAnimationFinishedCallback mCapturedFinishCallback;
320
321 TaskAnimationAdapter(Task task) {
322 mTask = task;
323 }
324
325 RemoteAnimationTarget createRemoteAnimationApp() {
326 // TODO: Do we need position and stack bounds?
327 return new RemoteAnimationTarget(mTask.mTaskId, MODE_CLOSING, mCapturedLeash,
328 !mTask.fillsParent(),
329 mTask.getTopVisibleAppMainWindow().mWinAnimator.mLastClipRect,
330 mTask.getPrefixOrderIndex(), new Point(), new Rect(),
331 mTask.getWindowConfiguration());
332 }
333
334 @Override
335 public boolean getDetachWallpaper() {
336 return false;
337 }
338
339 @Override
340 public int getBackgroundColor() {
341 return 0;
342 }
343
344 @Override
345 public void startAnimation(SurfaceControl animationLeash, Transaction t,
346 OnAnimationFinishedCallback finishCallback) {
347 mCapturedLeash = animationLeash;
348 mCapturedFinishCallback = finishCallback;
349 }
350
351 @Override
352 public void onAnimationCancelled(SurfaceControl animationLeash) {
353 cancelAnimation();
354 }
355
356 @Override
357 public long getDurationHint() {
358 return 0;
359 }
360
361 @Override
362 public long getStatusBarTransitionsStartTime() {
363 return SystemClock.uptimeMillis();
364 }
365 }
366
367 public void dump(PrintWriter pw, String prefix) {
368 final String innerPrefix = prefix + " ";
369 pw.print(prefix); pw.println(RecentsAnimationController.class.getSimpleName() + ":");
370 pw.print(innerPrefix); pw.println("mPendingStart=" + mPendingStart);
371 pw.print(innerPrefix); pw.println("mHomeAppToken=" + mHomeAppToken);
372 }
373}