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