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