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, |
| 108 | mainWindow.mStableInsets, |
| 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 |
| 166 | * @param restoreHomeBehindStackId The stack id to restore the home stack behind once the |
| 167 | * animation is complete. Will be passed to the callback. |
| 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; |
| 203 | final WallpaperController wc = dc.mWallpaperController; |
| 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; |
| 235 | mRunner.onAnimationStart(mController, appAnimations); |
| 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*/); |
| 268 | mService.scheduleAnimationLocked(); |
Winson Chung | a89ffed | 2018-01-25 17:46:11 +0000 | [diff] [blame^] | 269 | mService.destroyInputConsumer(INPUT_CONSUMER_RECENTS_ANIMATION); |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void checkAnimationReady(WallpaperController wallpaperController) { |
| 273 | if (mPendingStart) { |
| 274 | final boolean wallpaperReady = !isHomeAppOverWallpaper() |
| 275 | || (wallpaperController.getWallpaperTarget() != null |
| 276 | && wallpaperController.wallpaperTransitionReady()); |
| 277 | if (wallpaperReady) { |
| 278 | mService.getRecentsAnimationController().startAnimation(); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | boolean isWallpaperVisible(WindowState w) { |
| 284 | return w != null && w.mAppToken != null && mHomeAppToken == w.mAppToken |
| 285 | && isHomeAppOverWallpaper(); |
| 286 | } |
| 287 | |
Winson Chung | a89ffed | 2018-01-25 17:46:11 +0000 | [diff] [blame^] | 288 | boolean hasInputConsumerForApp(AppWindowToken appToken) { |
| 289 | return mInputConsumerEnabled && isAnimatingApp(appToken); |
| 290 | } |
| 291 | |
| 292 | boolean updateInputConsumerForApp(InputConsumerImpl recentsAnimationInputConsumer, |
| 293 | boolean hasFocus) { |
| 294 | // Update the input consumer touchable region to match the home app main window |
| 295 | final WindowState homeAppMainWindow = mHomeAppToken != null |
| 296 | ? mHomeAppToken.findMainWindow() |
| 297 | : null; |
| 298 | if (homeAppMainWindow != null) { |
| 299 | homeAppMainWindow.getBounds(mTmpRect); |
| 300 | recentsAnimationInputConsumer.mWindowHandle.hasFocus = hasFocus; |
| 301 | recentsAnimationInputConsumer.mWindowHandle.touchableRegion.set(mTmpRect); |
| 302 | return true; |
| 303 | } |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | private boolean isHomeAppOverWallpaper() { |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 308 | if (mHomeAppToken == null) { |
| 309 | return false; |
| 310 | } |
| 311 | return mHomeAppToken.windowsCanBeWallpaperTarget(); |
| 312 | } |
| 313 | |
Winson Chung | a89ffed | 2018-01-25 17:46:11 +0000 | [diff] [blame^] | 314 | private boolean isAnimatingApp(AppWindowToken appToken) { |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 315 | for (int i = mPendingAnimations.size() - 1; i >= 0; i--) { |
| 316 | final Task task = mPendingAnimations.get(i).mTask; |
| 317 | for (int j = task.getChildCount() - 1; j >= 0; j--) { |
| 318 | final AppWindowToken app = task.getChildAt(j); |
| 319 | if (app == appToken) { |
| 320 | return true; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | return false; |
| 325 | } |
| 326 | |
Winson Chung | e2d7217 | 2018-01-25 17:46:20 +0000 | [diff] [blame] | 327 | private class TaskAnimationAdapter implements AnimationAdapter { |
| 328 | |
| 329 | private Task mTask; |
| 330 | private SurfaceControl mCapturedLeash; |
| 331 | private OnAnimationFinishedCallback mCapturedFinishCallback; |
| 332 | |
| 333 | TaskAnimationAdapter(Task task) { |
| 334 | mTask = task; |
| 335 | } |
| 336 | |
| 337 | RemoteAnimationTarget createRemoteAnimationApp() { |
| 338 | // TODO: Do we need position and stack bounds? |
| 339 | return new RemoteAnimationTarget(mTask.mTaskId, MODE_CLOSING, mCapturedLeash, |
| 340 | !mTask.fillsParent(), |
| 341 | mTask.getTopVisibleAppMainWindow().mWinAnimator.mLastClipRect, |
| 342 | mTask.getPrefixOrderIndex(), new Point(), new Rect(), |
| 343 | mTask.getWindowConfiguration()); |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public boolean getDetachWallpaper() { |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public int getBackgroundColor() { |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public void startAnimation(SurfaceControl animationLeash, Transaction t, |
| 358 | OnAnimationFinishedCallback finishCallback) { |
| 359 | mCapturedLeash = animationLeash; |
| 360 | mCapturedFinishCallback = finishCallback; |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public void onAnimationCancelled(SurfaceControl animationLeash) { |
| 365 | cancelAnimation(); |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public long getDurationHint() { |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public long getStatusBarTransitionsStartTime() { |
| 375 | return SystemClock.uptimeMillis(); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | public void dump(PrintWriter pw, String prefix) { |
| 380 | final String innerPrefix = prefix + " "; |
| 381 | pw.print(prefix); pw.println(RecentsAnimationController.class.getSimpleName() + ":"); |
| 382 | pw.print(innerPrefix); pw.println("mPendingStart=" + mPendingStart); |
| 383 | pw.print(innerPrefix); pw.println("mHomeAppToken=" + mHomeAppToken); |
| 384 | } |
| 385 | } |