Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 19 | import static com.android.server.wm.TaskSnapshotPersister.DISABLE_FULL_SIZED_BITMAPS; |
| 20 | import static com.android.server.wm.TaskSnapshotPersister.REDUCED_SCALE; |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 21 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT; |
Winson Chung | 3e13ef8 | 2017-06-29 12:41:14 -0700 | [diff] [blame] | 22 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 23 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 24 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 25 | import android.annotation.Nullable; |
Jorim Jaggi | 3d73260 | 2017-02-22 17:56:40 +0100 | [diff] [blame] | 26 | import android.app.ActivityManager; |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 27 | import android.app.ActivityManager.TaskSnapshot; |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Jorim Jaggi | 6aead1c | 2017-05-23 15:07:44 +0200 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 30 | import android.graphics.GraphicBuffer; |
Winson Chung | 02378b4 | 2018-06-04 16:40:09 -0700 | [diff] [blame] | 31 | import android.graphics.PixelFormat; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 32 | import android.graphics.Rect; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 33 | import android.os.Environment; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 34 | import android.os.Handler; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 35 | import android.util.ArraySet; |
Winson Chung | 3e13ef8 | 2017-06-29 12:41:14 -0700 | [diff] [blame] | 36 | import android.util.Slog; |
Winson Chung | 9109276 | 2017-05-24 14:08:48 -0700 | [diff] [blame] | 37 | import android.view.DisplayListCanvas; |
| 38 | import android.view.RenderNode; |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 39 | import android.view.SurfaceControl; |
Winson Chung | 9109276 | 2017-05-24 14:08:48 -0700 | [diff] [blame] | 40 | import android.view.ThreadedRenderer; |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 41 | import android.view.View; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 42 | import android.view.WindowManager.LayoutParams; |
Guang Zhu | 09486a3 | 2017-06-06 03:27:44 +0000 | [diff] [blame] | 43 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 44 | import com.android.internal.annotations.VisibleForTesting; |
Winson Chung | 51f42d2 | 2018-02-01 14:59:38 -0800 | [diff] [blame] | 45 | import com.android.internal.graphics.ColorUtils; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 46 | import com.android.server.policy.WindowManagerPolicy.ScreenOffListener; |
| 47 | import com.android.server.policy.WindowManagerPolicy.StartingSurface; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 48 | import com.android.server.wm.TaskSnapshotSurface.SystemBarBackgroundPainter; |
Jorim Jaggi | 817ebdd | 2018-03-26 15:46:01 +0200 | [diff] [blame] | 49 | import com.android.server.wm.utils.InsetUtils; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 50 | |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 51 | import com.google.android.collect.Sets; |
| 52 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 53 | import java.io.PrintWriter; |
| 54 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 55 | /** |
| 56 | * When an app token becomes invisible, we take a snapshot (bitmap) of the corresponding task and |
| 57 | * put it into our cache. Internally we use gralloc buffers to be able to draw them wherever we |
| 58 | * like without any copying. |
| 59 | * <p> |
| 60 | * System applications may retrieve a snapshot to represent the current state of a task, and draw |
| 61 | * them in their own process. |
| 62 | * <p> |
| 63 | * When we task becomes visible again, we show a starting window with the snapshot as the content to |
| 64 | * make app transitions more responsive. |
| 65 | * <p> |
| 66 | * To access this class, acquire the global window manager lock. |
| 67 | */ |
| 68 | class TaskSnapshotController { |
Winson Chung | 3e13ef8 | 2017-06-29 12:41:14 -0700 | [diff] [blame] | 69 | private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskSnapshotController" : TAG_WM; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 70 | |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 71 | /** |
| 72 | * Return value for {@link #getSnapshotMode}: We are allowed to take a real screenshot to be |
| 73 | * used as the snapshot. |
| 74 | */ |
| 75 | @VisibleForTesting |
| 76 | static final int SNAPSHOT_MODE_REAL = 0; |
| 77 | |
| 78 | /** |
| 79 | * Return value for {@link #getSnapshotMode}: We are not allowed to take a real screenshot but |
| 80 | * we should try to use the app theme to create a dummy representation of the app. |
| 81 | */ |
| 82 | @VisibleForTesting |
| 83 | static final int SNAPSHOT_MODE_APP_THEME = 1; |
| 84 | |
| 85 | /** |
| 86 | * Return value for {@link #getSnapshotMode}: We aren't allowed to take any snapshot. |
| 87 | */ |
| 88 | @VisibleForTesting |
| 89 | static final int SNAPSHOT_MODE_NONE = 2; |
| 90 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 91 | private final WindowManagerService mService; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 92 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 93 | private final TaskSnapshotCache mCache; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 94 | private final TaskSnapshotPersister mPersister = new TaskSnapshotPersister( |
| 95 | Environment::getDataSystemCeDirectory); |
| 96 | private final TaskSnapshotLoader mLoader = new TaskSnapshotLoader(mPersister); |
Winson Chung | 23aa7b1 | 2018-02-01 11:41:43 -0800 | [diff] [blame] | 97 | private final ArraySet<Task> mSkipClosingAppSnapshotTasks = new ArraySet<>(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 98 | private final ArraySet<Task> mTmpTasks = new ArraySet<>(); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 99 | private final Handler mHandler = new Handler(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 100 | |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 101 | private final Rect mTmpRect = new Rect(); |
| 102 | |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Flag indicating whether we are running on an Android TV device. |
| 105 | */ |
| 106 | private final boolean mIsRunningOnTv; |
| 107 | |
Juho Ha | f5dd7cc | 2017-06-15 11:38:56 +0900 | [diff] [blame] | 108 | /** |
| 109 | * Flag indicating whether we are running on an IoT device. |
| 110 | */ |
| 111 | private final boolean mIsRunningOnIoT; |
| 112 | |
Matthew Ng | 9537819 | 2017-08-16 11:57:00 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Flag indicating whether we are running on an Android Wear device. |
| 115 | */ |
| 116 | private final boolean mIsRunningOnWear; |
| 117 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 118 | TaskSnapshotController(WindowManagerService service) { |
| 119 | mService = service; |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 120 | mCache = new TaskSnapshotCache(mService, mLoader); |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 121 | mIsRunningOnTv = mService.mContext.getPackageManager().hasSystemFeature( |
| 122 | PackageManager.FEATURE_LEANBACK); |
Juho Ha | f5dd7cc | 2017-06-15 11:38:56 +0900 | [diff] [blame] | 123 | mIsRunningOnIoT = mService.mContext.getPackageManager().hasSystemFeature( |
| 124 | PackageManager.FEATURE_EMBEDDED); |
Matthew Ng | 9537819 | 2017-08-16 11:57:00 -0700 | [diff] [blame] | 125 | mIsRunningOnWear = mService.mContext.getPackageManager().hasSystemFeature( |
| 126 | PackageManager.FEATURE_WATCH); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 129 | void systemReady() { |
| 130 | mPersister.start(); |
| 131 | } |
| 132 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 133 | void onTransitionStarting() { |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 134 | handleClosingApps(mService.mClosingApps); |
| 135 | } |
| 136 | |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 137 | /** |
| 138 | * Called when the visibility of an app changes outside of the regular app transition flow. |
| 139 | */ |
| 140 | void notifyAppVisibilityChanged(AppWindowToken appWindowToken, boolean visible) { |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 141 | if (!visible) { |
| 142 | handleClosingApps(Sets.newArraySet(appWindowToken)); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | private void handleClosingApps(ArraySet<AppWindowToken> closingApps) { |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 147 | if (shouldDisableSnapshots()) { |
Jorim Jaggi | 3d73260 | 2017-02-22 17:56:40 +0100 | [diff] [blame] | 148 | return; |
| 149 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 150 | |
| 151 | // We need to take a snapshot of the task if and only if all activities of the task are |
| 152 | // either closing or hidden. |
Jorim Jaggi | 8b702ed | 2017-01-20 16:59:03 +0100 | [diff] [blame] | 153 | getClosingTasks(closingApps, mTmpTasks); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 154 | snapshotTasks(mTmpTasks); |
Winson Chung | 23aa7b1 | 2018-02-01 11:41:43 -0800 | [diff] [blame] | 155 | mSkipClosingAppSnapshotTasks.clear(); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Winson Chung | 23aa7b1 | 2018-02-01 11:41:43 -0800 | [diff] [blame] | 158 | /** |
| 159 | * Adds the given {@param tasks} to the list of tasks which should not have their snapshots |
| 160 | * taken upon the next processing of the set of closing apps. The caller is responsible for |
| 161 | * calling {@link #snapshotTasks} to ensure that the task has an up-to-date snapshot. |
| 162 | */ |
| 163 | @VisibleForTesting |
| 164 | void addSkipClosingAppSnapshotTasks(ArraySet<Task> tasks) { |
| 165 | mSkipClosingAppSnapshotTasks.addAll(tasks); |
| 166 | } |
| 167 | |
| 168 | void snapshotTasks(ArraySet<Task> tasks) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 169 | for (int i = tasks.size() - 1; i >= 0; i--) { |
| 170 | final Task task = tasks.valueAt(i); |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 171 | final int mode = getSnapshotMode(task); |
| 172 | final TaskSnapshot snapshot; |
| 173 | switch (mode) { |
| 174 | case SNAPSHOT_MODE_NONE: |
| 175 | continue; |
| 176 | case SNAPSHOT_MODE_APP_THEME: |
| 177 | snapshot = drawAppThemeSnapshot(task); |
| 178 | break; |
| 179 | case SNAPSHOT_MODE_REAL: |
| 180 | snapshot = snapshotTask(task); |
| 181 | break; |
| 182 | default: |
| 183 | snapshot = null; |
| 184 | break; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 185 | } |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 186 | if (snapshot != null) { |
Winson Chung | 3e13ef8 | 2017-06-29 12:41:14 -0700 | [diff] [blame] | 187 | final GraphicBuffer buffer = snapshot.getSnapshot(); |
| 188 | if (buffer.getWidth() == 0 || buffer.getHeight() == 0) { |
| 189 | buffer.destroy(); |
| 190 | Slog.e(TAG, "Invalid task snapshot dimensions " + buffer.getWidth() + "x" |
| 191 | + buffer.getHeight()); |
| 192 | } else { |
| 193 | mCache.putSnapshot(task, snapshot); |
| 194 | mPersister.persistSnapshot(task.mTaskId, task.mUserId, snapshot); |
| 195 | if (task.getController() != null) { |
| 196 | task.getController().reportSnapshotChanged(snapshot); |
| 197 | } |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 198 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 203 | /** |
| 204 | * Retrieves a snapshot. If {@param restoreFromDisk} equals {@code true}, DO HOLD THE WINDOW |
| 205 | * MANAGER LOCK WHEN CALLING THIS METHOD! |
| 206 | */ |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 207 | @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk, |
| 208 | boolean reducedResolution) { |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 209 | return mCache.getSnapshot(taskId, userId, restoreFromDisk, reducedResolution |
| 210 | || DISABLE_FULL_SIZED_BITMAPS); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Creates a starting surface for {@param token} with {@param snapshot}. DO NOT HOLD THE WINDOW |
| 215 | * MANAGER LOCK WHEN CALLING THIS METHOD! |
| 216 | */ |
| 217 | StartingSurface createStartingSurface(AppWindowToken token, |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 218 | TaskSnapshot snapshot) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 219 | return TaskSnapshotSurface.create(mService, token, snapshot); |
| 220 | } |
| 221 | |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 222 | private TaskSnapshot snapshotTask(Task task) { |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 223 | final AppWindowToken top = task.getTopChild(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 224 | if (top == null) { |
| 225 | return null; |
| 226 | } |
Jorim Jaggi | dd5986e | 2017-04-11 15:50:19 -0700 | [diff] [blame] | 227 | final WindowState mainWindow = top.findMainWindow(); |
| 228 | if (mainWindow == null) { |
| 229 | return null; |
| 230 | } |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 231 | if (!mService.mPolicy.isScreenOn()) { |
| 232 | if (DEBUG_SCREENSHOT) { |
| 233 | Slog.i(TAG_WM, "Attempted to take screenshot while display was off."); |
| 234 | } |
| 235 | return null; |
| 236 | } |
| 237 | if (task.getSurfaceControl() == null) { |
| 238 | return null; |
| 239 | } |
| 240 | |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 241 | if (top.hasCommittedReparentToAnimationLeash()) { |
| 242 | if (DEBUG_SCREENSHOT) { |
| 243 | Slog.w(TAG_WM, "Failed to take screenshot. App is animating " + top); |
| 244 | } |
| 245 | return null; |
| 246 | } |
| 247 | |
| 248 | final boolean hasVisibleChild = top.forAllWindows( |
| 249 | // Ensure at least one window for the top app is visible before attempting to take |
| 250 | // a screenshot. Visible here means that the WSA surface is shown and has an alpha |
| 251 | // greater than 0. |
Jorim Jaggi | 77d0f36c | 2018-03-16 17:49:49 +0100 | [diff] [blame] | 252 | ws -> (ws.mAppToken == null || ws.mAppToken.isSurfaceShowing()) |
| 253 | && ws.mWinAnimator != null && ws.mWinAnimator.getShown() |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 254 | && ws.mWinAnimator.mLastAlpha > 0f, true); |
| 255 | |
| 256 | if (!hasVisibleChild) { |
| 257 | if (DEBUG_SCREENSHOT) { |
| 258 | Slog.w(TAG_WM, "Failed to take screenshot. No visible windows for " + task); |
| 259 | } |
| 260 | return null; |
| 261 | } |
| 262 | |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 263 | final boolean isLowRamDevice = ActivityManager.isLowRamDeviceStatic(); |
| 264 | final float scaleFraction = isLowRamDevice ? REDUCED_SCALE : 1f; |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 265 | task.getBounds(mTmpRect); |
| 266 | mTmpRect.offsetTo(0, 0); |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 267 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 268 | final GraphicBuffer buffer = SurfaceControl.captureLayers( |
chaviw | 23ee71c | 2017-12-18 11:29:41 -0800 | [diff] [blame] | 269 | task.getSurfaceControl().getHandle(), mTmpRect, scaleFraction); |
Winson Chung | 02378b4 | 2018-06-04 16:40:09 -0700 | [diff] [blame] | 270 | final boolean isWindowTranslucent = mainWindow.getAttrs().format != PixelFormat.OPAQUE; |
Juho Ha | d864b44 | 2017-06-12 20:15:31 +0900 | [diff] [blame] | 271 | if (buffer == null || buffer.getWidth() <= 1 || buffer.getHeight() <= 1) { |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 272 | if (DEBUG_SCREENSHOT) { |
chaviw | 7f1fa99 | 2018-01-10 13:52:12 -0800 | [diff] [blame] | 273 | Slog.w(TAG_WM, "Failed to take screenshot for " + task); |
chaviw | fbe47df | 2017-11-10 16:14:49 -0800 | [diff] [blame] | 274 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 275 | return null; |
| 276 | } |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 277 | return new TaskSnapshot(buffer, top.getConfiguration().orientation, |
Winson Chung | a4fa8d5 | 2018-04-20 15:54:51 -0700 | [diff] [blame] | 278 | getInsets(mainWindow), isLowRamDevice /* reduced */, scaleFraction /* scale */, |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 279 | true /* isRealSnapshot */, task.getWindowingMode(), getSystemUiVisibility(task), |
Winson Chung | 02378b4 | 2018-06-04 16:40:09 -0700 | [diff] [blame] | 280 | !top.fillsParent() || isWindowTranslucent); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 283 | private boolean shouldDisableSnapshots() { |
Jorim Jaggi | e7d2b85 | 2017-08-28 17:55:15 +0200 | [diff] [blame] | 284 | return mIsRunningOnWear || mIsRunningOnTv || mIsRunningOnIoT; |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Jorim Jaggi | 817ebdd | 2018-03-26 15:46:01 +0200 | [diff] [blame] | 287 | private Rect getInsets(WindowState state) { |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 288 | // XXX(b/72757033): These are insets relative to the window frame, but we're really |
| 289 | // interested in the insets relative to the task bounds. |
Jorim Jaggi | 817ebdd | 2018-03-26 15:46:01 +0200 | [diff] [blame] | 290 | final Rect insets = minRect(state.mContentInsets, state.mStableInsets); |
| 291 | InsetUtils.addInsets(insets, state.mAppToken.getLetterboxInsets()); |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 292 | return insets; |
| 293 | } |
Adrian Roos | 98a146d | 2017-11-29 16:39:44 +0100 | [diff] [blame] | 294 | |
Adrian Roos | 20e0789 | 2018-02-23 19:12:01 +0100 | [diff] [blame] | 295 | private Rect minRect(Rect rect1, Rect rect2) { |
| 296 | return new Rect(Math.min(rect1.left, rect2.left), |
| 297 | Math.min(rect1.top, rect2.top), |
| 298 | Math.min(rect1.right, rect2.right), |
| 299 | Math.min(rect1.bottom, rect2.bottom)); |
| 300 | } |
Adrian Roos | 98a146d | 2017-11-29 16:39:44 +0100 | [diff] [blame] | 301 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 302 | /** |
| 303 | * Retrieves all closing tasks based on the list of closing apps during an app transition. |
| 304 | */ |
| 305 | @VisibleForTesting |
| 306 | void getClosingTasks(ArraySet<AppWindowToken> closingApps, ArraySet<Task> outClosingTasks) { |
| 307 | outClosingTasks.clear(); |
| 308 | for (int i = closingApps.size() - 1; i >= 0; i--) { |
| 309 | final AppWindowToken atoken = closingApps.valueAt(i); |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 310 | final Task task = atoken.getTask(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 311 | |
| 312 | // If the task of the app is not visible anymore, it means no other app in that task |
| 313 | // is opening. Thus, the task is closing. |
Winson Chung | 23aa7b1 | 2018-02-01 11:41:43 -0800 | [diff] [blame] | 314 | if (task != null && !task.isVisible() && !mSkipClosingAppSnapshotTasks.contains(task)) { |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 315 | outClosingTasks.add(task); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Jorim Jaggi | 0fe7ce96 | 2017-02-22 16:45:48 +0100 | [diff] [blame] | 320 | @VisibleForTesting |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 321 | int getSnapshotMode(Task task) { |
Jorim Jaggi | 0fe7ce96 | 2017-02-22 16:45:48 +0100 | [diff] [blame] | 322 | final AppWindowToken topChild = task.getTopChild(); |
Winson Chung | bff1837 | 2018-04-27 16:12:54 -0700 | [diff] [blame] | 323 | if (!task.isActivityTypeStandardOrUndefined() && !task.isActivityTypeAssistant()) { |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 324 | return SNAPSHOT_MODE_NONE; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 325 | } else if (topChild != null && topChild.shouldUseAppThemeSnapshot()) { |
Jorim Jaggi | 8f4fe6e | 2017-03-14 18:21:40 +0100 | [diff] [blame] | 326 | return SNAPSHOT_MODE_APP_THEME; |
| 327 | } else { |
| 328 | return SNAPSHOT_MODE_REAL; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * If we are not allowed to take a real screenshot, this attempts to represent the app as best |
| 334 | * as possible by using the theme's window background. |
| 335 | */ |
| 336 | private TaskSnapshot drawAppThemeSnapshot(Task task) { |
| 337 | final AppWindowToken topChild = task.getTopChild(); |
| 338 | if (topChild == null) { |
| 339 | return null; |
| 340 | } |
| 341 | final WindowState mainWindow = topChild.findMainWindow(); |
| 342 | if (mainWindow == null) { |
| 343 | return null; |
| 344 | } |
Winson Chung | 51f42d2 | 2018-02-01 14:59:38 -0800 | [diff] [blame] | 345 | final int color = ColorUtils.setAlphaComponent( |
| 346 | task.getTaskDescription().getBackgroundColor(), 255); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 347 | final int statusBarColor = task.getTaskDescription().getStatusBarColor(); |
| 348 | final int navigationBarColor = task.getTaskDescription().getNavigationBarColor(); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 349 | final LayoutParams attrs = mainWindow.getAttrs(); |
| 350 | final SystemBarBackgroundPainter decorPainter = new SystemBarBackgroundPainter(attrs.flags, |
| 351 | attrs.privateFlags, attrs.systemUiVisibility, statusBarColor, navigationBarColor); |
Winson Chung | 9109276 | 2017-05-24 14:08:48 -0700 | [diff] [blame] | 352 | final int width = mainWindow.getFrameLw().width(); |
| 353 | final int height = mainWindow.getFrameLw().height(); |
| 354 | |
| 355 | final RenderNode node = RenderNode.create("TaskSnapshotController", null); |
| 356 | node.setLeftTopRightBottom(0, 0, width, height); |
| 357 | node.setClipToBounds(false); |
| 358 | final DisplayListCanvas c = node.start(width, height); |
| 359 | c.drawColor(color); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 360 | decorPainter.setInsets(mainWindow.mContentInsets, mainWindow.mStableInsets); |
| 361 | decorPainter.drawDecors(c, null /* statusBarExcludeFrame */); |
Winson Chung | 9109276 | 2017-05-24 14:08:48 -0700 | [diff] [blame] | 362 | node.end(c); |
| 363 | final Bitmap hwBitmap = ThreadedRenderer.createHardwareBitmap(node, width, height); |
Winson Chung | 6267f99 | 2017-10-06 14:01:10 -0700 | [diff] [blame] | 364 | if (hwBitmap == null) { |
| 365 | return null; |
| 366 | } |
Winson Chung | 02378b4 | 2018-06-04 16:40:09 -0700 | [diff] [blame] | 367 | // Note, the app theme snapshot is never translucent because we enforce a non-translucent |
| 368 | // color above |
Jorim Jaggi | 6aead1c | 2017-05-23 15:07:44 +0200 | [diff] [blame] | 369 | return new TaskSnapshot(hwBitmap.createGraphicBufferHandle(), |
| 370 | topChild.getConfiguration().orientation, mainWindow.mStableInsets, |
Winson Chung | f3e412e | 2018-03-08 11:07:40 -0800 | [diff] [blame] | 371 | ActivityManager.isLowRamDeviceStatic() /* reduced */, 1.0f /* scale */, |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 372 | false /* isRealSnapshot */, task.getWindowingMode(), getSystemUiVisibility(task), |
Winson Chung | 02378b4 | 2018-06-04 16:40:09 -0700 | [diff] [blame] | 373 | false); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 374 | } |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 375 | |
| 376 | /** |
| 377 | * Called when an {@link AppWindowToken} has been removed. |
| 378 | */ |
| 379 | void onAppRemoved(AppWindowToken wtoken) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 380 | mCache.onAppRemoved(wtoken); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Called when the process of an {@link AppWindowToken} has died. |
| 385 | */ |
| 386 | void onAppDied(AppWindowToken wtoken) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 387 | mCache.onAppDied(wtoken); |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 390 | void notifyTaskRemovedFromRecents(int taskId, int userId) { |
Jorim Jaggi | 7361bab | 2017-01-16 17:17:58 +0100 | [diff] [blame] | 391 | mCache.onTaskRemoved(taskId); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 392 | mPersister.onTaskRemovedFromRecents(taskId, userId); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * See {@link TaskSnapshotPersister#removeObsoleteFiles} |
| 397 | */ |
| 398 | void removeObsoleteTaskFiles(ArraySet<Integer> persistentTaskIds, int[] runningUserIds) { |
| 399 | mPersister.removeObsoleteFiles(persistentTaskIds, runningUserIds); |
| 400 | } |
| 401 | |
Jorim Jaggi | a41b729 | 2017-05-11 23:50:34 +0200 | [diff] [blame] | 402 | /** |
| 403 | * Temporarily pauses/unpauses persisting of task snapshots. |
| 404 | * |
| 405 | * @param paused Whether task snapshot persisting should be paused. |
| 406 | */ |
| 407 | void setPersisterPaused(boolean paused) { |
| 408 | mPersister.setPaused(paused); |
| 409 | } |
| 410 | |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 411 | /** |
| 412 | * Called when screen is being turned off. |
| 413 | */ |
| 414 | void screenTurningOff(ScreenOffListener listener) { |
Keyvan Amiri | e681cec | 2017-06-05 22:48:26 -0700 | [diff] [blame] | 415 | if (shouldDisableSnapshots()) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 416 | listener.onScreenOff(); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | // We can't take a snapshot when screen is off, so take a snapshot now! |
| 421 | mHandler.post(() -> { |
| 422 | try { |
| 423 | synchronized (mService.mWindowMap) { |
| 424 | mTmpTasks.clear(); |
| 425 | mService.mRoot.forAllTasks(task -> { |
| 426 | if (task.isVisible()) { |
| 427 | mTmpTasks.add(task); |
| 428 | } |
| 429 | }); |
| 430 | snapshotTasks(mTmpTasks); |
| 431 | } |
| 432 | } finally { |
| 433 | listener.onScreenOff(); |
| 434 | } |
| 435 | }); |
| 436 | } |
| 437 | |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 438 | /** |
| 439 | * @return The SystemUI visibility flags for the top fullscreen window in the given |
| 440 | * {@param task}. |
| 441 | */ |
| 442 | private int getSystemUiVisibility(Task task) { |
| 443 | final AppWindowToken topFullscreenToken = task.getTopFullscreenAppToken(); |
| 444 | final WindowState topFullscreenWindow = topFullscreenToken != null |
| 445 | ? topFullscreenToken.getTopFullscreenWindow() |
| 446 | : null; |
| 447 | if (topFullscreenWindow != null) { |
| 448 | return topFullscreenWindow.getSystemUiVisibility(); |
| 449 | } |
| 450 | return 0; |
| 451 | } |
| 452 | |
Jorim Jaggi | 10abe2f | 2017-01-03 16:44:46 +0100 | [diff] [blame] | 453 | void dump(PrintWriter pw, String prefix) { |
| 454 | mCache.dump(pw, prefix); |
| 455 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 456 | } |