blob: 7392e803a1254df660ee88e236ad18f0fff9c7c0 [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;
Winson Chunga89ffed2018-01-25 17:46:11 +000022import static android.view.WindowManager.INPUT_CONSUMER_RECENTS_ANIMATION;
Winson Chunge2d72172018-01-25 17:46:20 +000023import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
24import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
25import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Jorim Jaggif75d1612018-02-27 15:05:21 +010026import static com.android.server.wm.proto.RemoteAnimationAdapterWrapperProto.TARGET;
27import static com.android.server.wm.proto.AnimationAdapterProto.REMOTE;
Winson Chunge2d72172018-01-25 17:46:20 +000028
Winson Chunge2d72172018-01-25 17:46:20 +000029import android.app.ActivityManager.TaskSnapshot;
30import android.app.WindowConfiguration;
Winson Chunge2d72172018-01-25 17:46:20 +000031import android.graphics.Point;
32import android.graphics.Rect;
33import android.os.Binder;
34import android.os.RemoteException;
35import android.os.SystemClock;
Winson Chung23aa7b12018-02-01 11:41:43 -080036import android.util.ArraySet;
Winson Chunge2d72172018-01-25 17:46:20 +000037import android.util.Log;
Jorim Jaggif75d1612018-02-27 15:05:21 +010038import android.util.Slog;import android.util.proto.ProtoOutputStream;
Vadim Tryshev593e9562018-03-08 17:15:45 -080039import android.util.SparseBooleanArray;
Jorim Jaggif75d1612018-02-27 15:05:21 +010040import android.util.proto.ProtoOutputStream;
Winson Chunge2d72172018-01-25 17:46:20 +000041import android.view.IRecentsAnimationController;
42import android.view.IRecentsAnimationRunner;
43import android.view.RemoteAnimationTarget;
44import android.view.SurfaceControl;
45import android.view.SurfaceControl.Transaction;
Jorim Jaggif75d1612018-02-27 15:05:21 +010046
Winson Chung23aa7b12018-02-01 11:41:43 -080047import com.google.android.collect.Sets;
Jorim Jaggif75d1612018-02-27 15:05:21 +010048
49import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
50
Winson Chunge2d72172018-01-25 17:46:20 +000051import java.io.PrintWriter;
52import java.util.ArrayList;
Winson Chunge2d72172018-01-25 17:46:20 +000053/**
54 * Controls a single instance of the remote driven recents animation. In particular, this allows
55 * the calling SystemUI to animate the visible task windows as a part of the transition. The remote
56 * runner is provided an animation controller which allows it to take screenshots and to notify
57 * window manager when the animation is completed. In addition, window manager may also notify the
58 * app if it requires the animation to be canceled at any time (ie. due to timeout, etc.)
59 */
60public class RecentsAnimationController {
61 private static final String TAG = TAG_WITH_CLASS_NAME ? "RecentsAnimationController" : TAG_WM;
62 private static final boolean DEBUG = false;
63
64 private final WindowManagerService mService;
65 private final IRecentsAnimationRunner mRunner;
66 private final RecentsAnimationCallbacks mCallbacks;
67 private final ArrayList<TaskAnimationAdapter> mPendingAnimations = new ArrayList<>();
Winson Chungddf62972018-02-12 11:10:04 -080068 private final int mDisplayId;
Winson Chunge2d72172018-01-25 17:46:20 +000069
70 // The recents component app token that is shown behind the visibile tasks
71 private AppWindowToken mHomeAppToken;
Winson Chung584d6522018-02-07 23:57:38 +000072 private Rect mMinimizedHomeBounds = new Rect();
Winson Chunge2d72172018-01-25 17:46:20 +000073
74 // We start the RecentsAnimationController in a pending-start state since we need to wait for
75 // the wallpaper/activity to draw before we can give control to the handler to start animating
76 // the visible task surfaces
77 private boolean mPendingStart = true;
78
79 // Set when the animation has been canceled
80 private boolean mCanceled = false;
81
82 // Whether or not the input consumer is enabled. The input consumer must be both registered and
83 // enabled for it to start intercepting touch events.
84 private boolean mInputConsumerEnabled;
85
Winson Chunga89ffed2018-01-25 17:46:11 +000086 private Rect mTmpRect = new Rect();
87
Winson Chunge2d72172018-01-25 17:46:20 +000088 public interface RecentsAnimationCallbacks {
89 void onAnimationFinished(boolean moveHomeToTop);
90 }
91
92 private final IRecentsAnimationController mController =
93 new IRecentsAnimationController.Stub() {
94
95 @Override
96 public TaskSnapshot screenshotTask(int taskId) {
97 if (DEBUG) Log.d(TAG, "screenshotTask(" + taskId + "): mCanceled=" + mCanceled);
98 long token = Binder.clearCallingIdentity();
99 try {
100 synchronized (mService.getWindowManagerLock()) {
101 if (mCanceled) {
102 return null;
103 }
104 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
105 final TaskAnimationAdapter adapter = mPendingAnimations.get(i);
106 final Task task = adapter.mTask;
107 if (task.mTaskId == taskId) {
Winson Chung23aa7b12018-02-01 11:41:43 -0800108 final TaskSnapshotController snapshotController =
109 mService.mTaskSnapshotController;
110 final ArraySet<Task> tasks = Sets.newArraySet(task);
111 snapshotController.snapshotTasks(tasks);
112 snapshotController.addSkipClosingAppSnapshotTasks(tasks);
113 return snapshotController.getSnapshot(taskId, 0 /* userId */,
114 false /* restoreFromDisk */, false /* reducedResolution */);
Winson Chunge2d72172018-01-25 17:46:20 +0000115 }
116 }
117 return null;
118 }
119 } finally {
120 Binder.restoreCallingIdentity(token);
121 }
122 }
123
124 @Override
125 public void finish(boolean moveHomeToTop) {
126 if (DEBUG) Log.d(TAG, "finish(" + moveHomeToTop + "): mCanceled=" + mCanceled);
127 long token = Binder.clearCallingIdentity();
128 try {
129 synchronized (mService.getWindowManagerLock()) {
130 if (mCanceled) {
131 return;
132 }
133 }
134
135 // Note, the callback will handle its own synchronization, do not lock on WM lock
136 // prior to calling the callback
137 mCallbacks.onAnimationFinished(moveHomeToTop);
138 } finally {
139 Binder.restoreCallingIdentity(token);
140 }
141 }
142
143 @Override
144 public void setInputConsumerEnabled(boolean enabled) {
145 if (DEBUG) Log.d(TAG, "setInputConsumerEnabled(" + enabled + "): mCanceled="
146 + mCanceled);
147 long token = Binder.clearCallingIdentity();
148 try {
149 synchronized (mService.getWindowManagerLock()) {
150 if (mCanceled) {
151 return;
152 }
153
154 mInputConsumerEnabled = enabled;
155 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
156 mService.scheduleAnimationLocked();
157 }
158 } finally {
159 Binder.restoreCallingIdentity(token);
160 }
161 }
162 };
163
164 /**
Winson Chunge2d72172018-01-25 17:46:20 +0000165 * @param remoteAnimationRunner The remote runner which should be notified when the animation is
166 * ready to start or has been canceled
167 * @param callbacks Callbacks to be made when the animation finishes
Winson Chunge2d72172018-01-25 17:46:20 +0000168 */
169 RecentsAnimationController(WindowManagerService service,
170 IRecentsAnimationRunner remoteAnimationRunner, RecentsAnimationCallbacks callbacks,
171 int displayId) {
172 mService = service;
173 mRunner = remoteAnimationRunner;
174 mCallbacks = callbacks;
Winson Chungddf62972018-02-12 11:10:04 -0800175 mDisplayId = displayId;
176 }
Winson Chunge2d72172018-01-25 17:46:20 +0000177
Winson Chungddf62972018-02-12 11:10:04 -0800178 /**
179 * Initializes the recents animation controller. This is a separate call from the constructor
180 * because it may call cancelAnimation() which needs to properly clean up the controller
181 * in the window manager.
182 */
Vadim Tryshev593e9562018-03-08 17:15:45 -0800183 public void initialize(SparseBooleanArray recentTaskIds) {
Winson Chunge2d72172018-01-25 17:46:20 +0000184 // Make leashes for each of the visible tasks and add it to the recents animation to be
185 // started
Winson Chungddf62972018-02-12 11:10:04 -0800186 final DisplayContent dc = mService.mRoot.getDisplayContent(mDisplayId);
187 final ArrayList<Task> visibleTasks = dc.getVisibleTasks();
Winson Chunge2d72172018-01-25 17:46:20 +0000188 final int taskCount = visibleTasks.size();
189 for (int i = 0; i < taskCount; i++) {
190 final Task task = visibleTasks.get(i);
191 final WindowConfiguration config = task.getWindowConfiguration();
192 if (config.tasksAreFloating()
193 || config.getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY
194 || config.getActivityType() == ACTIVITY_TYPE_HOME) {
195 continue;
196 }
Vadim Tryshev593e9562018-03-08 17:15:45 -0800197 addAnimation(task, !recentTaskIds.get(task.mTaskId));
Winson Chunge2d72172018-01-25 17:46:20 +0000198 }
199
Winson Chungddf62972018-02-12 11:10:04 -0800200 // Skip the animation if there is nothing to animate
201 if (mPendingAnimations.isEmpty()) {
202 cancelAnimation();
203 return;
204 }
205
Winson Chunge2d72172018-01-25 17:46:20 +0000206 // Adjust the wallpaper visibility for the showing home activity
207 final AppWindowToken recentsComponentAppToken =
208 dc.getHomeStack().getTopChild().getTopFullscreenAppToken();
209 if (recentsComponentAppToken != null) {
210 if (DEBUG) Log.d(TAG, "setHomeApp(" + recentsComponentAppToken.getName() + ")");
211 mHomeAppToken = recentsComponentAppToken;
Winson Chunge2d72172018-01-25 17:46:20 +0000212 if (recentsComponentAppToken.windowsCanBeWallpaperTarget()) {
213 dc.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
214 dc.setLayoutNeeded();
215 }
216 }
217
Winson Chung584d6522018-02-07 23:57:38 +0000218 // Save the minimized home height
219 dc.getDockedDividerController().getHomeStackBoundsInDockedMode(mMinimizedHomeBounds);
220
Winson Chunge2d72172018-01-25 17:46:20 +0000221 mService.mWindowPlacerLocked.performSurfacePlacement();
222 }
223
Vadim Tryshev593e9562018-03-08 17:15:45 -0800224 private void addAnimation(Task task, boolean isRecentTaskInvisible) {
Winson Chunge2d72172018-01-25 17:46:20 +0000225 if (DEBUG) Log.d(TAG, "addAnimation(" + task.getName() + ")");
226 final SurfaceAnimator anim = new SurfaceAnimator(task, null /* animationFinishedCallback */,
Chavi Weingartenb736e322018-02-23 00:27:54 +0000227 mService);
Vadim Tryshev593e9562018-03-08 17:15:45 -0800228 final TaskAnimationAdapter taskAdapter = new TaskAnimationAdapter(task,
229 isRecentTaskInvisible);
Winson Chunge2d72172018-01-25 17:46:20 +0000230 anim.startAnimation(task.getPendingTransaction(), taskAdapter, false /* hidden */);
231 task.commitPendingTransaction();
232 mPendingAnimations.add(taskAdapter);
233 }
234
235 void startAnimation() {
Winson Chungddf62972018-02-12 11:10:04 -0800236 if (DEBUG) Log.d(TAG, "startAnimation(): mPendingStart=" + mPendingStart
237 + " mCanceled=" + mCanceled);
238 if (!mPendingStart || mCanceled) {
239 // Skip starting if we've already started or canceled the animation
Winson Chunge2d72172018-01-25 17:46:20 +0000240 return;
241 }
242 try {
Winson Chung2dc37362018-03-12 17:57:06 -0700243 final ArrayList<RemoteAnimationTarget> appAnimations = new ArrayList<>();
Winson Chunge2d72172018-01-25 17:46:20 +0000244 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
Winson Chung2dc37362018-03-12 17:57:06 -0700245 final RemoteAnimationTarget target =
246 mPendingAnimations.get(i).createRemoteAnimationApp();
247 if (target != null) {
248 appAnimations.add(target);
249 }
Winson Chunge2d72172018-01-25 17:46:20 +0000250 }
Winson Chung2dc37362018-03-12 17:57:06 -0700251 final RemoteAnimationTarget[] appTargets = appAnimations.toArray(
252 new RemoteAnimationTarget[appAnimations.size()]);
Winson Chunge2d72172018-01-25 17:46:20 +0000253 mPendingStart = false;
Winson Chung584d6522018-02-07 23:57:38 +0000254
255 final Rect minimizedHomeBounds =
256 mHomeAppToken != null && mHomeAppToken.inSplitScreenSecondaryWindowingMode()
257 ? mMinimizedHomeBounds : null;
258 final Rect contentInsets =
259 mHomeAppToken != null && mHomeAppToken.findMainWindow() != null
260 ? mHomeAppToken.findMainWindow().mContentInsets : null;
Winson Chung2dc37362018-03-12 17:57:06 -0700261 mRunner.onAnimationStart_New(mController, appTargets, contentInsets,
Winson Chung584d6522018-02-07 23:57:38 +0000262 minimizedHomeBounds);
Winson Chunge2d72172018-01-25 17:46:20 +0000263 } catch (RemoteException e) {
264 Slog.e(TAG, "Failed to start recents animation", e);
265 }
266 }
267
268 void cancelAnimation() {
269 if (DEBUG) Log.d(TAG, "cancelAnimation()");
Winson Chung65fc89a2018-02-28 08:32:12 -0800270 synchronized (mService.getWindowManagerLock()) {
271 if (mCanceled) {
272 // We've already canceled the animation
273 return;
274 }
275 mCanceled = true;
276 try {
277 mRunner.onAnimationCanceled();
278 } catch (RemoteException e) {
279 Slog.e(TAG, "Failed to cancel recents animation", e);
280 }
Winson Chunge2d72172018-01-25 17:46:20 +0000281 }
Winson Chunge2d72172018-01-25 17:46:20 +0000282 // Clean up and return to the previous app
Winson Chung65fc89a2018-02-28 08:32:12 -0800283 // Don't hold the WM lock here as it calls back to AM/RecentsAnimation
Winson Chunge2d72172018-01-25 17:46:20 +0000284 mCallbacks.onAnimationFinished(false /* moveHomeToTop */);
285 }
286
287 void cleanupAnimation() {
288 if (DEBUG) Log.d(TAG, "cleanupAnimation(): mPendingAnimations="
289 + mPendingAnimations.size());
290 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
291 final TaskAnimationAdapter adapter = mPendingAnimations.get(i);
292 adapter.mCapturedFinishCallback.onAnimationFinished(adapter);
293 }
294 mPendingAnimations.clear();
295
296 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
Winson Chunga89ffed2018-01-25 17:46:11 +0000297 mService.destroyInputConsumer(INPUT_CONSUMER_RECENTS_ANIMATION);
Winson Chunge2d72172018-01-25 17:46:20 +0000298 }
299
300 void checkAnimationReady(WallpaperController wallpaperController) {
301 if (mPendingStart) {
302 final boolean wallpaperReady = !isHomeAppOverWallpaper()
303 || (wallpaperController.getWallpaperTarget() != null
304 && wallpaperController.wallpaperTransitionReady());
305 if (wallpaperReady) {
306 mService.getRecentsAnimationController().startAnimation();
307 }
308 }
309 }
310
311 boolean isWallpaperVisible(WindowState w) {
312 return w != null && w.mAppToken != null && mHomeAppToken == w.mAppToken
313 && isHomeAppOverWallpaper();
314 }
315
Winson Chunga89ffed2018-01-25 17:46:11 +0000316 boolean hasInputConsumerForApp(AppWindowToken appToken) {
317 return mInputConsumerEnabled && isAnimatingApp(appToken);
318 }
319
320 boolean updateInputConsumerForApp(InputConsumerImpl recentsAnimationInputConsumer,
321 boolean hasFocus) {
322 // Update the input consumer touchable region to match the home app main window
323 final WindowState homeAppMainWindow = mHomeAppToken != null
324 ? mHomeAppToken.findMainWindow()
325 : null;
326 if (homeAppMainWindow != null) {
327 homeAppMainWindow.getBounds(mTmpRect);
328 recentsAnimationInputConsumer.mWindowHandle.hasFocus = hasFocus;
329 recentsAnimationInputConsumer.mWindowHandle.touchableRegion.set(mTmpRect);
330 return true;
331 }
332 return false;
333 }
334
335 private boolean isHomeAppOverWallpaper() {
Winson Chunge2d72172018-01-25 17:46:20 +0000336 if (mHomeAppToken == null) {
337 return false;
338 }
339 return mHomeAppToken.windowsCanBeWallpaperTarget();
340 }
341
Winson Chunga89ffed2018-01-25 17:46:11 +0000342 private boolean isAnimatingApp(AppWindowToken appToken) {
Winson Chunge2d72172018-01-25 17:46:20 +0000343 for (int i = mPendingAnimations.size() - 1; i >= 0; i--) {
344 final Task task = mPendingAnimations.get(i).mTask;
345 for (int j = task.getChildCount() - 1; j >= 0; j--) {
346 final AppWindowToken app = task.getChildAt(j);
347 if (app == appToken) {
348 return true;
349 }
350 }
351 }
352 return false;
353 }
354
Winson Chunge2d72172018-01-25 17:46:20 +0000355 private class TaskAnimationAdapter implements AnimationAdapter {
356
Vadim Tryshev593e9562018-03-08 17:15:45 -0800357 private final Task mTask;
Winson Chunge2d72172018-01-25 17:46:20 +0000358 private SurfaceControl mCapturedLeash;
359 private OnAnimationFinishedCallback mCapturedFinishCallback;
Vadim Tryshev593e9562018-03-08 17:15:45 -0800360 private final boolean mIsRecentTaskInvisible;
Jorim Jaggif75d1612018-02-27 15:05:21 +0100361 private RemoteAnimationTarget mTarget;
Winson Chunge2d72172018-01-25 17:46:20 +0000362
Vadim Tryshev593e9562018-03-08 17:15:45 -0800363 TaskAnimationAdapter(Task task, boolean isRecentTaskInvisible) {
Winson Chunge2d72172018-01-25 17:46:20 +0000364 mTask = task;
Vadim Tryshev593e9562018-03-08 17:15:45 -0800365 mIsRecentTaskInvisible = isRecentTaskInvisible;
Winson Chunge2d72172018-01-25 17:46:20 +0000366 }
367
368 RemoteAnimationTarget createRemoteAnimationApp() {
Winson Chung584d6522018-02-07 23:57:38 +0000369 final Point position = new Point();
370 final Rect bounds = new Rect();
371 final WindowContainer container = mTask.getParent();
372 container.getRelativePosition(position);
373 container.getBounds(bounds);
374 final WindowState mainWindow = mTask.getTopVisibleAppMainWindow();
Winson Chung2dc37362018-03-12 17:57:06 -0700375 if (mainWindow == null) {
376 return null;
377 }
Jorim Jaggif75d1612018-02-27 15:05:21 +0100378 mTarget = new RemoteAnimationTarget(mTask.mTaskId, MODE_CLOSING, mCapturedLeash,
Winson Chung584d6522018-02-07 23:57:38 +0000379 !mTask.fillsParent(), mainWindow.mWinAnimator.mLastClipRect,
380 mainWindow.mContentInsets, mTask.getPrefixOrderIndex(), position, bounds,
Vadim Tryshev593e9562018-03-08 17:15:45 -0800381 mTask.getWindowConfiguration(), mIsRecentTaskInvisible);
Jorim Jaggif75d1612018-02-27 15:05:21 +0100382 return mTarget;
Winson Chunge2d72172018-01-25 17:46:20 +0000383 }
384
385 @Override
386 public boolean getDetachWallpaper() {
387 return false;
388 }
389
390 @Override
Jorim Jaggi82c17862018-02-21 17:50:18 +0100391 public boolean getShowWallpaper() {
392 return false;
393 }
394
395 @Override
Winson Chunge2d72172018-01-25 17:46:20 +0000396 public int getBackgroundColor() {
397 return 0;
398 }
399
400 @Override
401 public void startAnimation(SurfaceControl animationLeash, Transaction t,
402 OnAnimationFinishedCallback finishCallback) {
403 mCapturedLeash = animationLeash;
404 mCapturedFinishCallback = finishCallback;
405 }
406
407 @Override
408 public void onAnimationCancelled(SurfaceControl animationLeash) {
409 cancelAnimation();
410 }
411
412 @Override
413 public long getDurationHint() {
414 return 0;
415 }
416
417 @Override
418 public long getStatusBarTransitionsStartTime() {
419 return SystemClock.uptimeMillis();
420 }
Jorim Jaggif75d1612018-02-27 15:05:21 +0100421
422 @Override
423 public void dump(PrintWriter pw, String prefix) {
424 pw.print(prefix); pw.println("task=" + mTask);
425 if (mTarget != null) {
426 pw.print(prefix); pw.println("Target:");
427 mTarget.dump(pw, prefix + " ");
428 } else {
429 pw.print(prefix); pw.println("Target: null");
430 }
431 }
432
433 @Override
434 public void writeToProto(ProtoOutputStream proto) {
435 final long token = proto.start(REMOTE);
436 if (mTarget != null) {
437 mTarget.writeToProto(proto, TARGET);
438 }
439 proto.end(token);
440 }
Winson Chunge2d72172018-01-25 17:46:20 +0000441 }
442
443 public void dump(PrintWriter pw, String prefix) {
444 final String innerPrefix = prefix + " ";
445 pw.print(prefix); pw.println(RecentsAnimationController.class.getSimpleName() + ":");
446 pw.print(innerPrefix); pw.println("mPendingStart=" + mPendingStart);
447 pw.print(innerPrefix); pw.println("mHomeAppToken=" + mHomeAppToken);
448 }
449}