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