Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [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 | |
Winson Chung | 48b2565 | 2018-10-22 14:04:30 -0700 | [diff] [blame] | 19 | import static android.graphics.Bitmap.CompressFormat.JPEG; |
Tim Murray | af85392 | 2018-10-15 16:50:23 -0700 | [diff] [blame] | 20 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 21 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 22 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 23 | |
| 24 | import android.annotation.TestApi; |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 26 | import android.app.ActivityManager.TaskSnapshot; |
| 27 | import android.graphics.Bitmap; |
Jorim Jaggi | 2dae855 | 2017-05-02 14:10:58 +0200 | [diff] [blame] | 28 | import android.graphics.Bitmap.Config; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 29 | import android.os.Process; |
| 30 | import android.os.SystemClock; |
| 31 | import android.util.ArraySet; |
Al Sutton | 7407e2c | 2019-09-06 09:17:01 +0100 | [diff] [blame] | 32 | import android.util.AtomicFile; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 33 | import android.util.Slog; |
| 34 | |
| 35 | import com.android.internal.annotations.GuardedBy; |
| 36 | import com.android.internal.annotations.VisibleForTesting; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 37 | import com.android.server.wm.nano.WindowManagerProtos.TaskSnapshotProto; |
| 38 | |
| 39 | import java.io.File; |
| 40 | import java.io.FileOutputStream; |
| 41 | import java.io.IOException; |
| 42 | import java.util.ArrayDeque; |
Jorim Jaggi | 2b78b1f | 2018-06-18 15:48:06 +0200 | [diff] [blame] | 43 | import java.util.Arrays; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Persists {@link TaskSnapshot}s to disk. |
| 47 | * <p> |
| 48 | * Test class: {@link TaskSnapshotPersisterLoaderTest} |
| 49 | */ |
| 50 | class TaskSnapshotPersister { |
| 51 | |
| 52 | private static final String TAG = TAG_WITH_CLASS_NAME ? "TaskSnapshotPersister" : TAG_WM; |
| 53 | private static final String SNAPSHOTS_DIRNAME = "snapshots"; |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 54 | private static final String REDUCED_POSTFIX = "_reduced"; |
Kevin | 716a9db | 2018-12-18 16:53:39 -0800 | [diff] [blame] | 55 | private static final float REDUCED_SCALE = .5f; |
Peter Kalauskas | 5e59b44 | 2020-01-07 14:18:41 -0800 | [diff] [blame] | 56 | private static final float LOW_RAM_REDUCED_SCALE = .8f; |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 57 | static final boolean DISABLE_FULL_SIZED_BITMAPS = ActivityManager.isLowRamDeviceStatic(); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 58 | private static final long DELAY_MS = 100; |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 59 | private static final int QUALITY = 95; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 60 | private static final String PROTO_EXTENSION = ".proto"; |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 61 | private static final String BITMAP_EXTENSION = ".jpg"; |
Jorim Jaggi | ef3651c | 2017-05-18 23:58:09 +0200 | [diff] [blame] | 62 | private static final int MAX_STORE_QUEUE_DEPTH = 2; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 63 | |
| 64 | @GuardedBy("mLock") |
| 65 | private final ArrayDeque<WriteQueueItem> mWriteQueue = new ArrayDeque<>(); |
| 66 | @GuardedBy("mLock") |
Jorim Jaggi | ef3651c | 2017-05-18 23:58:09 +0200 | [diff] [blame] | 67 | private final ArrayDeque<StoreWriteQueueItem> mStoreQueueItems = new ArrayDeque<>(); |
| 68 | @GuardedBy("mLock") |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 69 | private boolean mQueueIdling; |
Jorim Jaggi | a41b729 | 2017-05-11 23:50:34 +0200 | [diff] [blame] | 70 | @GuardedBy("mLock") |
| 71 | private boolean mPaused; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 72 | private boolean mStarted; |
| 73 | private final Object mLock = new Object(); |
| 74 | private final DirectoryResolver mDirectoryResolver; |
Kevin | 716a9db | 2018-12-18 16:53:39 -0800 | [diff] [blame] | 75 | private final float mReducedScale; |
Chiawei Wang | 02202d1 | 2019-01-03 18:12:13 +0800 | [diff] [blame] | 76 | private final boolean mUse16BitFormat; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * The list of ids of the tasks that have been persisted since {@link #removeObsoleteFiles} was |
| 80 | * called. |
| 81 | */ |
| 82 | @GuardedBy("mLock") |
| 83 | private final ArraySet<Integer> mPersistedTaskIdsSinceLastRemoveObsolete = new ArraySet<>(); |
| 84 | |
Kevin | 716a9db | 2018-12-18 16:53:39 -0800 | [diff] [blame] | 85 | TaskSnapshotPersister(WindowManagerService service, DirectoryResolver resolver) { |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 86 | mDirectoryResolver = resolver; |
Peter Kalauskas | d756395 | 2019-11-21 13:00:44 -0800 | [diff] [blame] | 87 | mReducedScale = ActivityManager.isLowRamDeviceStatic() |
| 88 | ? LOW_RAM_REDUCED_SCALE : REDUCED_SCALE; |
Chiawei Wang | 02202d1 | 2019-01-03 18:12:13 +0800 | [diff] [blame] | 89 | mUse16BitFormat = service.mContext.getResources().getBoolean( |
| 90 | com.android.internal.R.bool.config_use16BitTaskSnapshotPixelFormat); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Starts persisting. |
| 95 | */ |
| 96 | void start() { |
| 97 | if (!mStarted) { |
| 98 | mStarted = true; |
| 99 | mPersister.start(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Persists a snapshot of a task to disk. |
| 105 | * |
| 106 | * @param taskId The id of the task that needs to be persisted. |
| 107 | * @param userId The id of the user this tasks belongs to. |
| 108 | * @param snapshot The snapshot to persist. |
| 109 | */ |
| 110 | void persistSnapshot(int taskId, int userId, TaskSnapshot snapshot) { |
| 111 | synchronized (mLock) { |
| 112 | mPersistedTaskIdsSinceLastRemoveObsolete.add(taskId); |
| 113 | sendToQueueLocked(new StoreWriteQueueItem(taskId, userId, snapshot)); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Callend when a task has been removed. |
| 119 | * |
| 120 | * @param taskId The id of task that has been removed. |
| 121 | * @param userId The id of the user the task belonged to. |
| 122 | */ |
| 123 | void onTaskRemovedFromRecents(int taskId, int userId) { |
| 124 | synchronized (mLock) { |
| 125 | mPersistedTaskIdsSinceLastRemoveObsolete.remove(taskId); |
| 126 | sendToQueueLocked(new DeleteWriteQueueItem(taskId, userId)); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * In case a write/delete operation was lost because the system crashed, this makes sure to |
| 132 | * clean up the directory to remove obsolete files. |
| 133 | * |
| 134 | * @param persistentTaskIds A set of task ids that exist in our in-memory model. |
| 135 | * @param runningUserIds The ids of the list of users that have tasks loaded in our in-memory |
| 136 | * model. |
| 137 | */ |
| 138 | void removeObsoleteFiles(ArraySet<Integer> persistentTaskIds, int[] runningUserIds) { |
| 139 | synchronized (mLock) { |
| 140 | mPersistedTaskIdsSinceLastRemoveObsolete.clear(); |
| 141 | sendToQueueLocked(new RemoveObsoleteFilesQueueItem(persistentTaskIds, runningUserIds)); |
| 142 | } |
| 143 | } |
| 144 | |
Jorim Jaggi | a41b729 | 2017-05-11 23:50:34 +0200 | [diff] [blame] | 145 | void setPaused(boolean paused) { |
| 146 | synchronized (mLock) { |
| 147 | mPaused = paused; |
| 148 | if (!paused) { |
| 149 | mLock.notifyAll(); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Kevin | 716a9db | 2018-12-18 16:53:39 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Gets the scaling the persister uses for low resolution task snapshots. |
| 156 | * |
| 157 | * @return the reduced scale of task snapshots when they are set to be low res |
| 158 | */ |
| 159 | float getReducedScale() { |
| 160 | return mReducedScale; |
| 161 | } |
| 162 | |
Chiawei Wang | 02202d1 | 2019-01-03 18:12:13 +0800 | [diff] [blame] | 163 | /** |
| 164 | * Return if task snapshots are stored in 16 bit pixel format. |
| 165 | * |
| 166 | * @return true if task snapshots are stored in 16 bit pixel format. |
| 167 | */ |
| 168 | boolean use16BitFormat() { |
| 169 | return mUse16BitFormat; |
| 170 | } |
| 171 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 172 | @TestApi |
| 173 | void waitForQueueEmpty() { |
| 174 | while (true) { |
| 175 | synchronized (mLock) { |
| 176 | if (mWriteQueue.isEmpty() && mQueueIdling) { |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | SystemClock.sleep(100); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | @GuardedBy("mLock") |
| 185 | private void sendToQueueLocked(WriteQueueItem item) { |
| 186 | mWriteQueue.offer(item); |
Jorim Jaggi | ef3651c | 2017-05-18 23:58:09 +0200 | [diff] [blame] | 187 | item.onQueuedLocked(); |
| 188 | ensureStoreQueueDepthLocked(); |
Jorim Jaggi | a41b729 | 2017-05-11 23:50:34 +0200 | [diff] [blame] | 189 | if (!mPaused) { |
| 190 | mLock.notifyAll(); |
| 191 | } |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Jorim Jaggi | ef3651c | 2017-05-18 23:58:09 +0200 | [diff] [blame] | 194 | @GuardedBy("mLock") |
| 195 | private void ensureStoreQueueDepthLocked() { |
| 196 | while (mStoreQueueItems.size() > MAX_STORE_QUEUE_DEPTH) { |
| 197 | final StoreWriteQueueItem item = mStoreQueueItems.poll(); |
| 198 | mWriteQueue.remove(item); |
| 199 | Slog.i(TAG, "Queue is too deep! Purged item with taskid=" + item.mTaskId); |
| 200 | } |
| 201 | } |
| 202 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 203 | private File getDirectory(int userId) { |
| 204 | return new File(mDirectoryResolver.getSystemDirectoryForUser(userId), SNAPSHOTS_DIRNAME); |
| 205 | } |
| 206 | |
| 207 | File getProtoFile(int taskId, int userId) { |
| 208 | return new File(getDirectory(userId), taskId + PROTO_EXTENSION); |
| 209 | } |
| 210 | |
| 211 | File getBitmapFile(int taskId, int userId) { |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 212 | // Full sized bitmaps are disabled on low ram devices |
| 213 | if (DISABLE_FULL_SIZED_BITMAPS) { |
| 214 | Slog.wtf(TAG, "This device does not support full sized resolution bitmaps."); |
| 215 | return null; |
| 216 | } |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 217 | return new File(getDirectory(userId), taskId + BITMAP_EXTENSION); |
| 218 | } |
| 219 | |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 220 | File getReducedResolutionBitmapFile(int taskId, int userId) { |
| 221 | return new File(getDirectory(userId), taskId + REDUCED_POSTFIX + BITMAP_EXTENSION); |
| 222 | } |
| 223 | |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 224 | private boolean createDirectory(int userId) { |
| 225 | final File dir = getDirectory(userId); |
| 226 | return dir.exists() || dir.mkdirs(); |
| 227 | } |
| 228 | |
| 229 | private void deleteSnapshot(int taskId, int userId) { |
| 230 | final File protoFile = getProtoFile(taskId, userId); |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 231 | final File bitmapReducedFile = getReducedResolutionBitmapFile(taskId, userId); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 232 | protoFile.delete(); |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 233 | bitmapReducedFile.delete(); |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 234 | |
| 235 | // Low ram devices do not have a full sized file to delete |
| 236 | if (!DISABLE_FULL_SIZED_BITMAPS) { |
| 237 | final File bitmapFile = getBitmapFile(taskId, userId); |
| 238 | bitmapFile.delete(); |
| 239 | } |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | interface DirectoryResolver { |
| 243 | File getSystemDirectoryForUser(int userId); |
| 244 | } |
| 245 | |
| 246 | private Thread mPersister = new Thread("TaskSnapshotPersister") { |
| 247 | public void run() { |
| 248 | android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 249 | while (true) { |
| 250 | WriteQueueItem next; |
| 251 | synchronized (mLock) { |
Jorim Jaggi | a41b729 | 2017-05-11 23:50:34 +0200 | [diff] [blame] | 252 | if (mPaused) { |
| 253 | next = null; |
| 254 | } else { |
| 255 | next = mWriteQueue.poll(); |
Jorim Jaggi | ef3651c | 2017-05-18 23:58:09 +0200 | [diff] [blame] | 256 | if (next != null) { |
| 257 | next.onDequeuedLocked(); |
| 258 | } |
Jorim Jaggi | a41b729 | 2017-05-11 23:50:34 +0200 | [diff] [blame] | 259 | } |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 260 | } |
| 261 | if (next != null) { |
| 262 | next.write(); |
| 263 | SystemClock.sleep(DELAY_MS); |
| 264 | } |
| 265 | synchronized (mLock) { |
Jorim Jaggi | 2f9c7a2 | 2017-05-16 14:03:08 +0200 | [diff] [blame] | 266 | final boolean writeQueueEmpty = mWriteQueue.isEmpty(); |
| 267 | if (!writeQueueEmpty && !mPaused) { |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 268 | continue; |
| 269 | } |
| 270 | try { |
Jorim Jaggi | 2f9c7a2 | 2017-05-16 14:03:08 +0200 | [diff] [blame] | 271 | mQueueIdling = writeQueueEmpty; |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 272 | mLock.wait(); |
| 273 | mQueueIdling = false; |
| 274 | } catch (InterruptedException e) { |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | }; |
| 280 | |
| 281 | private abstract class WriteQueueItem { |
| 282 | abstract void write(); |
Jorim Jaggi | ef3651c | 2017-05-18 23:58:09 +0200 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | * Called when this queue item has been put into the queue. |
| 286 | */ |
| 287 | void onQueuedLocked() { |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Called when this queue item has been taken out of the queue. |
| 292 | */ |
| 293 | void onDequeuedLocked() { |
| 294 | } |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | private class StoreWriteQueueItem extends WriteQueueItem { |
| 298 | private final int mTaskId; |
| 299 | private final int mUserId; |
| 300 | private final TaskSnapshot mSnapshot; |
| 301 | |
| 302 | StoreWriteQueueItem(int taskId, int userId, TaskSnapshot snapshot) { |
| 303 | mTaskId = taskId; |
| 304 | mUserId = userId; |
| 305 | mSnapshot = snapshot; |
| 306 | } |
| 307 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 308 | @GuardedBy("mLock") |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 309 | @Override |
Jorim Jaggi | ef3651c | 2017-05-18 23:58:09 +0200 | [diff] [blame] | 310 | void onQueuedLocked() { |
| 311 | mStoreQueueItems.offer(this); |
| 312 | } |
| 313 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 314 | @GuardedBy("mLock") |
Jorim Jaggi | ef3651c | 2017-05-18 23:58:09 +0200 | [diff] [blame] | 315 | @Override |
| 316 | void onDequeuedLocked() { |
| 317 | mStoreQueueItems.remove(this); |
| 318 | } |
| 319 | |
| 320 | @Override |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 321 | void write() { |
| 322 | if (!createDirectory(mUserId)) { |
| 323 | Slog.e(TAG, "Unable to create snapshot directory for user dir=" |
| 324 | + getDirectory(mUserId)); |
| 325 | } |
| 326 | boolean failed = false; |
| 327 | if (!writeProto()) { |
| 328 | failed = true; |
| 329 | } |
| 330 | if (!writeBuffer()) { |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 331 | failed = true; |
| 332 | } |
| 333 | if (failed) { |
| 334 | deleteSnapshot(mTaskId, mUserId); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | boolean writeProto() { |
| 339 | final TaskSnapshotProto proto = new TaskSnapshotProto(); |
| 340 | proto.orientation = mSnapshot.getOrientation(); |
Vinit Nayak | ffd9dff | 2019-11-05 15:20:11 -0800 | [diff] [blame^] | 341 | proto.rotation = mSnapshot.getRotation(); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 342 | proto.insetLeft = mSnapshot.getContentInsets().left; |
| 343 | proto.insetTop = mSnapshot.getContentInsets().top; |
| 344 | proto.insetRight = mSnapshot.getContentInsets().right; |
| 345 | proto.insetBottom = mSnapshot.getContentInsets().bottom; |
Winson Chung | f3e412e | 2018-03-08 11:07:40 -0800 | [diff] [blame] | 346 | proto.isRealSnapshot = mSnapshot.isRealSnapshot(); |
Winson Chung | a4fa8d5 | 2018-04-20 15:54:51 -0700 | [diff] [blame] | 347 | proto.windowingMode = mSnapshot.getWindowingMode(); |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 348 | proto.systemUiVisibility = mSnapshot.getSystemUiVisibility(); |
| 349 | proto.isTranslucent = mSnapshot.isTranslucent(); |
Winson Chung | 48b2565 | 2018-10-22 14:04:30 -0700 | [diff] [blame] | 350 | proto.topActivityComponent = mSnapshot.getTopActivityComponent().flattenToString(); |
Winson Chung | ffde2ea | 2019-06-17 17:19:13 -0700 | [diff] [blame] | 351 | proto.scale = mSnapshot.getScale(); |
Hongwei Wang | 3b9bdcf | 2019-07-15 10:23:03 -0700 | [diff] [blame] | 352 | proto.id = mSnapshot.getId(); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 353 | final byte[] bytes = TaskSnapshotProto.toByteArray(proto); |
| 354 | final File file = getProtoFile(mTaskId, mUserId); |
| 355 | final AtomicFile atomicFile = new AtomicFile(file); |
| 356 | FileOutputStream fos = null; |
| 357 | try { |
| 358 | fos = atomicFile.startWrite(); |
| 359 | fos.write(bytes); |
| 360 | atomicFile.finishWrite(fos); |
| 361 | } catch (IOException e) { |
| 362 | atomicFile.failWrite(fos); |
| 363 | Slog.e(TAG, "Unable to open " + file + " for persisting. " + e); |
| 364 | return false; |
| 365 | } |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | boolean writeBuffer() { |
Peiyong Lin | 9d42740 | 2019-01-23 18:39:06 -0800 | [diff] [blame] | 370 | final Bitmap bitmap = Bitmap.wrapHardwareBuffer( |
Sunny Goyal | 62915b2 | 2019-04-10 12:28:47 -0700 | [diff] [blame] | 371 | mSnapshot.getSnapshot(), mSnapshot.getColorSpace()); |
Winson Chung | dab1673 | 2017-06-19 17:00:40 -0700 | [diff] [blame] | 372 | if (bitmap == null) { |
Winson Chung | 3e13ef8 | 2017-06-29 12:41:14 -0700 | [diff] [blame] | 373 | Slog.e(TAG, "Invalid task snapshot hw bitmap"); |
Winson Chung | dab1673 | 2017-06-19 17:00:40 -0700 | [diff] [blame] | 374 | return false; |
| 375 | } |
| 376 | |
Jorim Jaggi | 2dae855 | 2017-05-02 14:10:58 +0200 | [diff] [blame] | 377 | final Bitmap swBitmap = bitmap.copy(Config.ARGB_8888, false /* isMutable */); |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 378 | final Bitmap reduced = mSnapshot.isReducedResolution() |
| 379 | ? swBitmap |
| 380 | : Bitmap.createScaledBitmap(swBitmap, |
Kevin | 716a9db | 2018-12-18 16:53:39 -0800 | [diff] [blame] | 381 | (int) (bitmap.getWidth() * mReducedScale), |
| 382 | (int) (bitmap.getHeight() * mReducedScale), true /* filter */); |
Winson Chung | ffde2ea | 2019-06-17 17:19:13 -0700 | [diff] [blame] | 383 | |
| 384 | final File reducedFile = getReducedResolutionBitmapFile(mTaskId, mUserId); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 385 | try { |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 386 | FileOutputStream reducedFos = new FileOutputStream(reducedFile); |
| 387 | reduced.compress(JPEG, QUALITY, reducedFos); |
| 388 | reducedFos.close(); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 389 | } catch (IOException e) { |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 390 | Slog.e(TAG, "Unable to open " + reducedFile +" for persisting.", e); |
| 391 | return false; |
| 392 | } |
Winson Chung | ffde2ea | 2019-06-17 17:19:13 -0700 | [diff] [blame] | 393 | reduced.recycle(); |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 394 | |
| 395 | // For snapshots with reduced resolution, do not create or save full sized bitmaps |
| 396 | if (mSnapshot.isReducedResolution()) { |
Tim Murray | af85392 | 2018-10-15 16:50:23 -0700 | [diff] [blame] | 397 | swBitmap.recycle(); |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 398 | return true; |
| 399 | } |
| 400 | |
Matthew Ng | bd51856 | 2017-08-29 11:24:58 -0700 | [diff] [blame] | 401 | final File file = getBitmapFile(mTaskId, mUserId); |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 402 | try { |
| 403 | FileOutputStream fos = new FileOutputStream(file); |
| 404 | swBitmap.compress(JPEG, QUALITY, fos); |
| 405 | fos.close(); |
| 406 | } catch (IOException e) { |
| 407 | Slog.e(TAG, "Unable to open " + file + " for persisting.", e); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 408 | return false; |
| 409 | } |
Tim Murray | af85392 | 2018-10-15 16:50:23 -0700 | [diff] [blame] | 410 | swBitmap.recycle(); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 411 | return true; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | private class DeleteWriteQueueItem extends WriteQueueItem { |
| 416 | private final int mTaskId; |
| 417 | private final int mUserId; |
| 418 | |
| 419 | DeleteWriteQueueItem(int taskId, int userId) { |
| 420 | mTaskId = taskId; |
| 421 | mUserId = userId; |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | void write() { |
| 426 | deleteSnapshot(mTaskId, mUserId); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | @VisibleForTesting |
| 431 | class RemoveObsoleteFilesQueueItem extends WriteQueueItem { |
| 432 | private final ArraySet<Integer> mPersistentTaskIds; |
| 433 | private final int[] mRunningUserIds; |
| 434 | |
| 435 | @VisibleForTesting |
| 436 | RemoveObsoleteFilesQueueItem(ArraySet<Integer> persistentTaskIds, |
| 437 | int[] runningUserIds) { |
Jorim Jaggi | 2b78b1f | 2018-06-18 15:48:06 +0200 | [diff] [blame] | 438 | mPersistentTaskIds = new ArraySet<>(persistentTaskIds); |
| 439 | mRunningUserIds = Arrays.copyOf(runningUserIds, runningUserIds.length); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | @Override |
| 443 | void write() { |
| 444 | final ArraySet<Integer> newPersistedTaskIds; |
| 445 | synchronized (mLock) { |
| 446 | newPersistedTaskIds = new ArraySet<>(mPersistedTaskIdsSinceLastRemoveObsolete); |
| 447 | } |
| 448 | for (int userId : mRunningUserIds) { |
| 449 | final File dir = getDirectory(userId); |
| 450 | final String[] files = dir.list(); |
| 451 | if (files == null) { |
| 452 | continue; |
| 453 | } |
| 454 | for (String file : files) { |
| 455 | final int taskId = getTaskId(file); |
| 456 | if (!mPersistentTaskIds.contains(taskId) |
| 457 | && !newPersistedTaskIds.contains(taskId)) { |
| 458 | new File(dir, file).delete(); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | @VisibleForTesting |
| 465 | int getTaskId(String fileName) { |
| 466 | if (!fileName.endsWith(PROTO_EXTENSION) && !fileName.endsWith(BITMAP_EXTENSION)) { |
| 467 | return -1; |
| 468 | } |
| 469 | final int end = fileName.lastIndexOf('.'); |
| 470 | if (end == -1) { |
| 471 | return -1; |
| 472 | } |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 473 | String name = fileName.substring(0, end); |
| 474 | if (name.endsWith(REDUCED_POSTFIX)) { |
| 475 | name = name.substring(0, name.length() - REDUCED_POSTFIX.length()); |
| 476 | } |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 477 | try { |
Jorim Jaggi | 35e3f53 | 2017-03-17 17:06:50 +0100 | [diff] [blame] | 478 | return Integer.parseInt(name); |
Jorim Jaggi | f9084ec | 2017-01-16 13:16:59 +0100 | [diff] [blame] | 479 | } catch (NumberFormatException e) { |
| 480 | return -1; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | } |