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