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