blob: 1ecb2e9916fa7fc1d3bb3f13fd4256e9c3ba5d8e [file] [log] [blame]
Craig Mautner21d24a22014-04-23 11:45:37 -07001/*
2 * Copyright (C) 2014 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
17package com.android.server.am;
18
Suprabh Shukla4bccb462016-02-10 18:45:12 -080019import android.annotation.NonNull;
Craig Mautner21d24a22014-04-23 11:45:37 -070020import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
22import android.os.Debug;
Suprabh Shukla23593142015-11-03 17:31:15 -080023import android.os.Environment;
24import android.os.FileUtils;
Suprabh Shukla09a88f52015-12-02 14:36:31 -080025import android.os.Process;
Craig Mautner21d24a22014-04-23 11:45:37 -070026import android.os.SystemClock;
27import android.util.ArraySet;
28import android.util.AtomicFile;
29import android.util.Slog;
Suprabh Shukla4bccb462016-02-10 18:45:12 -080030import android.util.SparseArray;
31import android.util.SparseBooleanArray;
Craig Mautner21d24a22014-04-23 11:45:37 -070032import android.util.Xml;
Wale Ogunwale18795a22014-12-03 11:38:33 -080033
Suprabh Shukla4bccb462016-02-10 18:45:12 -080034import com.android.internal.annotations.VisibleForTesting;
Craig Mautner21d24a22014-04-23 11:45:37 -070035import com.android.internal.util.FastXmlSerializer;
36import com.android.internal.util.XmlUtils;
Suprabh Shukla09a88f52015-12-02 14:36:31 -080037import libcore.io.IoUtils;
38
Craig Mautner21d24a22014-04-23 11:45:37 -070039import org.xmlpull.v1.XmlPullParser;
40import org.xmlpull.v1.XmlPullParserException;
41import org.xmlpull.v1.XmlSerializer;
42
43import java.io.BufferedReader;
Suprabh Shukla4bccb462016-02-10 18:45:12 -080044import java.io.BufferedWriter;
Craig Mautner21d24a22014-04-23 11:45:37 -070045import java.io.File;
Suprabh Shukla4bccb462016-02-10 18:45:12 -080046import java.io.FileNotFoundException;
Craig Mautner21d24a22014-04-23 11:45:37 -070047import java.io.FileOutputStream;
48import java.io.FileReader;
Suprabh Shukla4bccb462016-02-10 18:45:12 -080049import java.io.FileWriter;
Craig Mautner21d24a22014-04-23 11:45:37 -070050import java.io.IOException;
51import java.io.StringWriter;
52import java.util.ArrayList;
Suprabh Shukla09a88f52015-12-02 14:36:31 -080053import java.util.Collections;
Craig Mautner21d24a22014-04-23 11:45:37 -070054import java.util.Comparator;
Suprabh Shukla23593142015-11-03 17:31:15 -080055import java.util.List;
Wale Ogunwale18795a22014-12-03 11:38:33 -080056
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070057import static android.app.ActivityManager.StackId.HOME_STACK_ID;
58
Craig Mautner21d24a22014-04-23 11:45:37 -070059public class TaskPersister {
60 static final String TAG = "TaskPersister";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -070061 static final boolean DEBUG = false;
Craig Mautner21d24a22014-04-23 11:45:37 -070062
Craig Mautnerf4f8bb72014-07-29 10:41:40 -070063 /** When not flushing don't write out files faster than this */
64 private static final long INTER_WRITE_DELAY_MS = 500;
65
Suprabh Shukla23593142015-11-03 17:31:15 -080066 /**
67 * When not flushing delay this long before writing the first file out. This gives the next task
68 * being launched a chance to load its resources without this occupying IO bandwidth.
69 */
Craig Mautnerf4f8bb72014-07-29 10:41:40 -070070 private static final long PRE_TASK_DELAY_MS = 3000;
Craig Mautner21d24a22014-04-23 11:45:37 -070071
Craig Mautner63f10902014-09-16 23:57:21 -070072 /** The maximum number of entries to keep in the queue before draining it automatically. */
73 private static final int MAX_WRITE_QUEUE_LENGTH = 6;
74
75 /** Special value for mWriteTime to mean don't wait, just write */
76 private static final long FLUSH_QUEUE = -1;
77
Craig Mautner21d24a22014-04-23 11:45:37 -070078 private static final String RECENTS_FILENAME = "_task";
79 private static final String TASKS_DIRNAME = "recent_tasks";
80 private static final String TASK_EXTENSION = ".xml";
81 private static final String IMAGES_DIRNAME = "recent_images";
Suprabh Shukla4bccb462016-02-10 18:45:12 -080082 private static final String PERSISTED_TASK_IDS_FILENAME = "persisted_taskIds.txt";
Craig Mautnerc0ffce52014-07-01 12:38:52 -070083 static final String IMAGE_EXTENSION = ".png";
Craig Mautner21d24a22014-04-23 11:45:37 -070084
Winson Chung36f3f032016-09-08 23:29:43 +000085 private static final String TAG_TASK = "task";
Craig Mautner21d24a22014-04-23 11:45:37 -070086
Craig Mautner21d24a22014-04-23 11:45:37 -070087 private final ActivityManagerService mService;
88 private final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -080089 private final RecentTasks mRecentTasks;
Suprabh Shukla4bccb462016-02-10 18:45:12 -080090 private final SparseArray<SparseBooleanArray> mTaskIdsInFile = new SparseArray<>();
Suprabh Shuklaf50b4582016-02-23 17:44:22 -080091 private final File mTaskIdsDir;
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -070092 // To lock file operations in TaskPersister
93 private final Object mIoLock = new Object();
Craig Mautner21d24a22014-04-23 11:45:37 -070094
Suprabh Shukla23593142015-11-03 17:31:15 -080095 /**
96 * Value determines write delay mode as follows: < 0 We are Flushing. No delays between writes
97 * until the image queue is drained and all tasks needing persisting are written to disk. There
98 * is no delay between writes. == 0 We are Idle. Next writes will be delayed by
99 * #PRE_TASK_DELAY_MS. > 0 We are Actively writing. Next write will be at this time. Subsequent
100 * writes will be delayed by #INTER_WRITE_DELAY_MS.
101 */
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700102 private long mNextWriteTime = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -0700103
104 private final LazyTaskWriterThread mLazyTaskWriterThread;
105
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700106 private static class WriteQueueItem {}
Suprabh Shukla23593142015-11-03 17:31:15 -0800107
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700108 private static class TaskWriteQueueItem extends WriteQueueItem {
109 final TaskRecord mTask;
Winsonc809cbb2015-11-02 12:06:15 -0800110
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700111 TaskWriteQueueItem(TaskRecord task) {
112 mTask = task;
113 }
114 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800115
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700116 private static class ImageWriteQueueItem extends WriteQueueItem {
Suprabh Shukla23593142015-11-03 17:31:15 -0800117 final String mFilePath;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700118 Bitmap mImage;
Winsonc809cbb2015-11-02 12:06:15 -0800119
Suprabh Shukla23593142015-11-03 17:31:15 -0800120 ImageWriteQueueItem(String filePath, Bitmap image) {
121 mFilePath = filePath;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700122 mImage = image;
123 }
124 }
125
126 ArrayList<WriteQueueItem> mWriteQueue = new ArrayList<WriteQueueItem>();
127
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800128 TaskPersister(File systemDir, ActivityStackSupervisor stackSupervisor,
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800129 ActivityManagerService service, RecentTasks recentTasks) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800130
131 final File legacyImagesDir = new File(systemDir, IMAGES_DIRNAME);
132 if (legacyImagesDir.exists()) {
133 if (!FileUtils.deleteContents(legacyImagesDir) || !legacyImagesDir.delete()) {
134 Slog.i(TAG, "Failure deleting legacy images directory: " + legacyImagesDir);
Craig Mautner21d24a22014-04-23 11:45:37 -0700135 }
136 }
137
Suprabh Shukla23593142015-11-03 17:31:15 -0800138 final File legacyTasksDir = new File(systemDir, TASKS_DIRNAME);
139 if (legacyTasksDir.exists()) {
140 if (!FileUtils.deleteContents(legacyTasksDir) || !legacyTasksDir.delete()) {
141 Slog.i(TAG, "Failure deleting legacy tasks directory: " + legacyTasksDir);
Craig Mautner21d24a22014-04-23 11:45:37 -0700142 }
143 }
144
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800145 mTaskIdsDir = new File(Environment.getDataDirectory(), "system_de");
Craig Mautner21d24a22014-04-23 11:45:37 -0700146 mStackSupervisor = stackSupervisor;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800147 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800148 mRecentTasks = recentTasks;
Craig Mautner21d24a22014-04-23 11:45:37 -0700149 mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThread");
150 }
151
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800152 @VisibleForTesting
153 TaskPersister(File workingDir) {
154 mTaskIdsDir = workingDir;
155 mStackSupervisor = null;
156 mService = null;
157 mRecentTasks = null;
158 mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThreadTest");
159 }
160
Craig Mautner21d24a22014-04-23 11:45:37 -0700161 void startPersisting() {
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800162 if (!mLazyTaskWriterThread.isAlive()) {
163 mLazyTaskWriterThread.start();
164 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700165 }
166
Craig Mautner63f10902014-09-16 23:57:21 -0700167 private void removeThumbnails(TaskRecord task) {
168 final String taskString = Integer.toString(task.taskId);
169 for (int queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
170 final WriteQueueItem item = mWriteQueue.get(queueNdx);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800171 if (item instanceof ImageWriteQueueItem) {
172 final File thumbnailFile = new File(((ImageWriteQueueItem) item).mFilePath);
173 if (thumbnailFile.getName().startsWith(taskString)) {
174 if (DEBUG) {
175 Slog.d(TAG, "Removing " + ((ImageWriteQueueItem) item).mFilePath +
176 " from write queue");
177 }
178 mWriteQueue.remove(queueNdx);
179 }
Craig Mautner63f10902014-09-16 23:57:21 -0700180 }
181 }
182 }
183
184 private void yieldIfQueueTooDeep() {
185 boolean stall = false;
186 synchronized (this) {
187 if (mNextWriteTime == FLUSH_QUEUE) {
188 stall = true;
189 }
190 }
191 if (stall) {
192 Thread.yield();
193 }
194 }
195
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800196 @NonNull
197 SparseBooleanArray loadPersistedTaskIdsForUser(int userId) {
198 if (mTaskIdsInFile.get(userId) != null) {
199 return mTaskIdsInFile.get(userId).clone();
200 }
201 final SparseBooleanArray persistedTaskIds = new SparseBooleanArray();
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700202 synchronized (mIoLock) {
203 BufferedReader reader = null;
204 String line;
205 try {
206 reader = new BufferedReader(new FileReader(getUserPersistedTaskIdsFile(userId)));
207 while ((line = reader.readLine()) != null) {
208 for (String taskIdString : line.split("\\s+")) {
209 int id = Integer.parseInt(taskIdString);
210 persistedTaskIds.put(id, true);
211 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800212 }
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700213 } catch (FileNotFoundException e) {
214 // File doesn't exist. Ignore.
215 } catch (Exception e) {
216 Slog.e(TAG, "Error while reading taskIds file for user " + userId, e);
217 } finally {
218 IoUtils.closeQuietly(reader);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800219 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800220 }
221 mTaskIdsInFile.put(userId, persistedTaskIds);
222 return persistedTaskIds.clone();
223 }
224
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700225
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800226 @VisibleForTesting
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700227 void writePersistedTaskIdsForUser(@NonNull SparseBooleanArray taskIds, int userId) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800228 if (userId < 0) {
229 return;
230 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800231 final File persistedTaskIdsFile = getUserPersistedTaskIdsFile(userId);
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700232 synchronized (mIoLock) {
233 BufferedWriter writer = null;
234 try {
235 writer = new BufferedWriter(new FileWriter(persistedTaskIdsFile));
236 for (int i = 0; i < taskIds.size(); i++) {
237 if (taskIds.valueAt(i)) {
238 writer.write(String.valueOf(taskIds.keyAt(i)));
239 writer.newLine();
240 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800241 }
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700242 } catch (Exception e) {
243 Slog.e(TAG, "Error while writing taskIds file for user " + userId, e);
244 } finally {
245 IoUtils.closeQuietly(writer);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800246 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800247 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800248 }
249
250 void unloadUserDataFromMemory(int userId) {
251 mTaskIdsInFile.delete(userId);
252 }
253
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700254 void wakeup(TaskRecord task, boolean flush) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700255 synchronized (this) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700256 if (task != null) {
257 int queueNdx;
258 for (queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
259 final WriteQueueItem item = mWriteQueue.get(queueNdx);
260 if (item instanceof TaskWriteQueueItem &&
261 ((TaskWriteQueueItem) item).mTask == task) {
Craig Mautner63f10902014-09-16 23:57:21 -0700262 if (!task.inRecents) {
263 // This task is being removed.
264 removeThumbnails(task);
265 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700266 break;
267 }
268 }
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700269 if (queueNdx < 0 && task.isPersistable) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700270 mWriteQueue.add(new TaskWriteQueueItem(task));
271 }
272 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800273 // Dummy. Ensures removeObsoleteFiles is called when LazyTaskThreadWriter is
274 // notified.
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700275 mWriteQueue.add(new WriteQueueItem());
276 }
Craig Mautner63f10902014-09-16 23:57:21 -0700277 if (flush || mWriteQueue.size() > MAX_WRITE_QUEUE_LENGTH) {
278 mNextWriteTime = FLUSH_QUEUE;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700279 } else if (mNextWriteTime == 0) {
280 mNextWriteTime = SystemClock.uptimeMillis() + PRE_TASK_DELAY_MS;
281 }
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700282 if (DEBUG) Slog.d(TAG, "wakeup: task=" + task + " flush=" + flush + " mNextWriteTime="
283 + mNextWriteTime + " mWriteQueue.size=" + mWriteQueue.size()
284 + " Callers=" + Debug.getCallers(4));
Craig Mautner21d24a22014-04-23 11:45:37 -0700285 notifyAll();
286 }
Craig Mautner63f10902014-09-16 23:57:21 -0700287
288 yieldIfQueueTooDeep();
Craig Mautner21d24a22014-04-23 11:45:37 -0700289 }
290
Dianne Hackbornce0fd762014-09-19 12:58:15 -0700291 void flush() {
292 synchronized (this) {
293 mNextWriteTime = FLUSH_QUEUE;
294 notifyAll();
295 do {
296 try {
297 wait();
298 } catch (InterruptedException e) {
299 }
300 } while (mNextWriteTime == FLUSH_QUEUE);
301 }
302 }
303
Suprabh Shukla23593142015-11-03 17:31:15 -0800304 void saveImage(Bitmap image, String filePath) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700305 synchronized (this) {
306 int queueNdx;
307 for (queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
308 final WriteQueueItem item = mWriteQueue.get(queueNdx);
309 if (item instanceof ImageWriteQueueItem) {
310 ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
Suprabh Shukla23593142015-11-03 17:31:15 -0800311 if (imageWriteQueueItem.mFilePath.equals(filePath)) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700312 // replace the Bitmap with the new one.
313 imageWriteQueueItem.mImage = image;
314 break;
315 }
316 }
317 }
318 if (queueNdx < 0) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800319 mWriteQueue.add(new ImageWriteQueueItem(filePath, image));
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700320 }
Craig Mautner63f10902014-09-16 23:57:21 -0700321 if (mWriteQueue.size() > MAX_WRITE_QUEUE_LENGTH) {
322 mNextWriteTime = FLUSH_QUEUE;
323 } else if (mNextWriteTime == 0) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700324 mNextWriteTime = SystemClock.uptimeMillis() + PRE_TASK_DELAY_MS;
325 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800326 if (DEBUG) Slog.d(TAG, "saveImage: filePath=" + filePath + " now=" +
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700327 SystemClock.uptimeMillis() + " mNextWriteTime=" +
328 mNextWriteTime + " Callers=" + Debug.getCallers(4));
329 notifyAll();
330 }
Craig Mautner63f10902014-09-16 23:57:21 -0700331
332 yieldIfQueueTooDeep();
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700333 }
334
Suprabh Shukla23593142015-11-03 17:31:15 -0800335 Bitmap getTaskDescriptionIcon(String filePath) {
Craig Mautner648f69b2014-09-18 14:16:26 -0700336 // See if it is in the write queue
Suprabh Shukla23593142015-11-03 17:31:15 -0800337 final Bitmap icon = getImageFromWriteQueue(filePath);
Craig Mautner648f69b2014-09-18 14:16:26 -0700338 if (icon != null) {
339 return icon;
340 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800341 return restoreImage(filePath);
Craig Mautner648f69b2014-09-18 14:16:26 -0700342 }
343
Suprabh Shukla23593142015-11-03 17:31:15 -0800344 Bitmap getImageFromWriteQueue(String filePath) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700345 synchronized (this) {
346 for (int queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
347 final WriteQueueItem item = mWriteQueue.get(queueNdx);
348 if (item instanceof ImageWriteQueueItem) {
349 ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
Suprabh Shukla23593142015-11-03 17:31:15 -0800350 if (imageWriteQueueItem.mFilePath.equals(filePath)) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700351 return imageWriteQueueItem.mImage;
352 }
353 }
354 }
355 return null;
356 }
357 }
358
Craig Mautner21d24a22014-04-23 11:45:37 -0700359 private StringWriter saveToXml(TaskRecord task) throws IOException, XmlPullParserException {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700360 if (DEBUG) Slog.d(TAG, "saveToXml: task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -0700361 final XmlSerializer xmlSerializer = new FastXmlSerializer();
362 StringWriter stringWriter = new StringWriter();
363 xmlSerializer.setOutput(stringWriter);
364
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700365 if (DEBUG) xmlSerializer.setFeature(
Suprabh Shukla23593142015-11-03 17:31:15 -0800366 "http://xmlpull.org/v1/doc/features.html#indent-output", true);
Craig Mautner21d24a22014-04-23 11:45:37 -0700367
368 // save task
369 xmlSerializer.startDocument(null, true);
370
371 xmlSerializer.startTag(null, TAG_TASK);
372 task.saveToXml(xmlSerializer);
373 xmlSerializer.endTag(null, TAG_TASK);
374
375 xmlSerializer.endDocument();
376 xmlSerializer.flush();
377
378 return stringWriter;
379 }
380
Craig Mautner77b04262014-06-27 15:22:12 -0700381 private String fileToString(File file) {
382 final String newline = System.lineSeparator();
383 try {
384 BufferedReader reader = new BufferedReader(new FileReader(file));
385 StringBuffer sb = new StringBuffer((int) file.length() * 2);
386 String line;
387 while ((line = reader.readLine()) != null) {
388 sb.append(line + newline);
389 }
390 reader.close();
391 return sb.toString();
392 } catch (IOException ioe) {
393 Slog.e(TAG, "Couldn't read file " + file.getName());
394 return null;
395 }
396 }
397
Craig Mautnera228ae92014-07-09 05:44:55 -0700398 private TaskRecord taskIdToTask(int taskId, ArrayList<TaskRecord> tasks) {
399 if (taskId < 0) {
400 return null;
401 }
402 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
403 final TaskRecord task = tasks.get(taskNdx);
404 if (task.taskId == taskId) {
405 return task;
406 }
407 }
408 Slog.e(TAG, "Restore affiliation error looking for taskId=" + taskId);
409 return null;
410 }
411
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800412 List<TaskRecord> restoreTasksForUserLocked(final int userId) {
413 final ArrayList<TaskRecord> tasks = new ArrayList<TaskRecord>();
Craig Mautner21d24a22014-04-23 11:45:37 -0700414 ArraySet<Integer> recoveredTaskIds = new ArraySet<Integer>();
415
Suprabh Shukla23593142015-11-03 17:31:15 -0800416 File userTasksDir = getUserTasksDir(userId);
Winson Chung36f3f032016-09-08 23:29:43 +0000417
Suprabh Shukla23593142015-11-03 17:31:15 -0800418 File[] recentFiles = userTasksDir.listFiles();
Craig Mautner21d24a22014-04-23 11:45:37 -0700419 if (recentFiles == null) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800420 Slog.e(TAG, "restoreTasksForUserLocked: Unable to list files from " + userTasksDir);
Craig Mautner21d24a22014-04-23 11:45:37 -0700421 return tasks;
422 }
423
424 for (int taskNdx = 0; taskNdx < recentFiles.length; ++taskNdx) {
425 File taskFile = recentFiles[taskNdx];
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800426 if (DEBUG) {
427 Slog.d(TAG, "restoreTasksForUserLocked: userId=" + userId
428 + ", taskFile=" + taskFile.getName());
429 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700430 BufferedReader reader = null;
Craig Mautnere0129b32014-05-25 16:41:09 -0700431 boolean deleteFile = false;
Craig Mautner21d24a22014-04-23 11:45:37 -0700432 try {
433 reader = new BufferedReader(new FileReader(taskFile));
434 final XmlPullParser in = Xml.newPullParser();
435 in.setInput(reader);
436
437 int event;
438 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
439 event != XmlPullParser.END_TAG) {
440 final String name = in.getName();
441 if (event == XmlPullParser.START_TAG) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800442 if (DEBUG) Slog.d(TAG, "restoreTasksForUserLocked: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -0700443 if (TAG_TASK.equals(name)) {
Winson Chung36f3f032016-09-08 23:29:43 +0000444 final TaskRecord task = TaskRecord.restoreFromXml(in, mStackSupervisor);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800445 if (DEBUG) Slog.d(TAG, "restoreTasksForUserLocked: restored task="
Suprabh Shukla23593142015-11-03 17:31:15 -0800446 + task);
Craig Mautner21d24a22014-04-23 11:45:37 -0700447 if (task != null) {
Winson Chung36f3f032016-09-08 23:29:43 +0000448 // XXX Don't add to write queue... there is no reason to write
449 // out the stuff we just read, if we don't write it we will
450 // read the same thing again.
451 // mWriteQueue.add(new TaskWriteQueueItem(task));
452
Craig Mautner21d24a22014-04-23 11:45:37 -0700453 final int taskId = task.taskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800454 if (mStackSupervisor.anyTaskForIdLocked(taskId,
Wale Ogunwale625ed0c2016-10-18 08:50:31 -0700455 /* restoreFromRecents= */ false, HOME_STACK_ID) != null) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800456 // Should not happen.
457 Slog.wtf(TAG, "Existing task with taskId " + taskId + "found");
458 } else if (userId != task.userId) {
459 // Should not happen.
460 Slog.wtf(TAG, "Task with userId " + task.userId + " found in "
461 + userTasksDir.getAbsolutePath());
462 } else {
463 // Looks fine.
464 mStackSupervisor.setNextTaskIdForUserLocked(taskId, userId);
Amith Yamasani515d4062015-09-28 11:30:06 -0700465 task.isPersistable = true;
466 tasks.add(task);
467 recoveredTaskIds.add(taskId);
468 }
Craig Mautner77b04262014-06-27 15:22:12 -0700469 } else {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800470 Slog.e(TAG, "restoreTasksForUserLocked: Unable to restore taskFile="
Suprabh Shukla23593142015-11-03 17:31:15 -0800471 + taskFile + ": " + fileToString(taskFile));
Craig Mautner21d24a22014-04-23 11:45:37 -0700472 }
473 } else {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800474 Slog.wtf(TAG, "restoreTasksForUserLocked: Unknown xml event=" + event
Suprabh Shukla23593142015-11-03 17:31:15 -0800475 + " name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -0700476 }
477 }
478 XmlUtils.skipCurrentTag(in);
479 }
Craig Mautnere0129b32014-05-25 16:41:09 -0700480 } catch (Exception e) {
Craig Mautnera228ae92014-07-09 05:44:55 -0700481 Slog.wtf(TAG, "Unable to parse " + taskFile + ". Error ", e);
Craig Mautner77b04262014-06-27 15:22:12 -0700482 Slog.e(TAG, "Failing file: " + fileToString(taskFile));
Craig Mautnere0129b32014-05-25 16:41:09 -0700483 deleteFile = true;
Craig Mautner21d24a22014-04-23 11:45:37 -0700484 } finally {
Wale Ogunwale18795a22014-12-03 11:38:33 -0800485 IoUtils.closeQuietly(reader);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700486 if (deleteFile) {
487 if (DEBUG) Slog.d(TAG, "Deleting file=" + taskFile.getName());
Craig Mautnere0129b32014-05-25 16:41:09 -0700488 taskFile.delete();
489 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700490 }
491 }
492
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700493 if (!DEBUG) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800494 removeObsoleteFiles(recoveredTaskIds, userTasksDir.listFiles());
495 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700496
Suprabh Shukla23593142015-11-03 17:31:15 -0800497 // Fix up task affiliation from taskIds
Craig Mautnera228ae92014-07-09 05:44:55 -0700498 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
499 final TaskRecord task = tasks.get(taskNdx);
500 task.setPrevAffiliate(taskIdToTask(task.mPrevAffiliateTaskId, tasks));
501 task.setNextAffiliate(taskIdToTask(task.mNextAffiliateTaskId, tasks));
502 }
503
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800504 Collections.sort(tasks, new Comparator<TaskRecord>() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700505 @Override
506 public int compare(TaskRecord lhs, TaskRecord rhs) {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700507 final long diff = rhs.mLastTimeMoved - lhs.mLastTimeMoved;
Craig Mautner21d24a22014-04-23 11:45:37 -0700508 if (diff < 0) {
509 return -1;
510 } else if (diff > 0) {
511 return +1;
512 } else {
513 return 0;
514 }
515 }
516 });
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800517 return tasks;
Craig Mautner21d24a22014-04-23 11:45:37 -0700518 }
519
Craig Mautnere0129b32014-05-25 16:41:09 -0700520 private static void removeObsoleteFiles(ArraySet<Integer> persistentTaskIds, File[] files) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800521 if (DEBUG) Slog.d(TAG, "removeObsoleteFiles: persistentTaskIds=" + persistentTaskIds +
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700522 " files=" + files);
Craig Mautnera5badf02014-09-11 12:47:03 -0700523 if (files == null) {
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700524 Slog.e(TAG, "File error accessing recents directory (directory doesn't exist?).");
Craig Mautnera5badf02014-09-11 12:47:03 -0700525 return;
526 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700527 for (int fileNdx = 0; fileNdx < files.length; ++fileNdx) {
528 File file = files[fileNdx];
529 String filename = file.getName();
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700530 final int taskIdEnd = filename.indexOf('_');
Craig Mautner21d24a22014-04-23 11:45:37 -0700531 if (taskIdEnd > 0) {
532 final int taskId;
533 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +0100534 taskId = Integer.parseInt(filename.substring(0, taskIdEnd));
Suprabh Shukla23593142015-11-03 17:31:15 -0800535 if (DEBUG) Slog.d(TAG, "removeObsoleteFiles: Found taskId=" + taskId);
Craig Mautner21d24a22014-04-23 11:45:37 -0700536 } catch (Exception e) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800537 Slog.wtf(TAG, "removeObsoleteFiles: Can't parse file=" + file.getName());
Craig Mautner21d24a22014-04-23 11:45:37 -0700538 file.delete();
539 continue;
540 }
541 if (!persistentTaskIds.contains(taskId)) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800542 if (DEBUG) Slog.d(TAG, "removeObsoleteFiles: deleting file=" + file.getName());
Craig Mautner21d24a22014-04-23 11:45:37 -0700543 file.delete();
544 }
545 }
546 }
547 }
548
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800549 private void writeTaskIdsFiles() {
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700550 SparseArray<SparseBooleanArray> changedTaskIdsPerUser = new SparseArray<>();
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800551 synchronized (mService) {
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700552 for (int userId : mRecentTasks.usersWithRecentsLoadedLocked()) {
553 SparseBooleanArray taskIdsToSave = mRecentTasks.mPersistedTaskIds.get(userId);
554 SparseBooleanArray persistedIdsInFile = mTaskIdsInFile.get(userId);
555 if (persistedIdsInFile != null && persistedIdsInFile.equals(taskIdsToSave)) {
556 continue;
557 } else {
558 SparseBooleanArray taskIdsToSaveCopy = taskIdsToSave.clone();
559 mTaskIdsInFile.put(userId, taskIdsToSaveCopy);
560 changedTaskIdsPerUser.put(userId, taskIdsToSaveCopy);
561 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800562 }
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700563 }
564 for (int i = 0; i < changedTaskIdsPerUser.size(); i++) {
565 writePersistedTaskIdsForUser(changedTaskIdsPerUser.valueAt(i),
566 changedTaskIdsPerUser.keyAt(i));
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800567 }
568 }
569
Craig Mautner21d24a22014-04-23 11:45:37 -0700570 private void removeObsoleteFiles(ArraySet<Integer> persistentTaskIds) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800571 int[] candidateUserIds;
572 synchronized (mService) {
573 // Remove only from directories of the users who have recents in memory synchronized
574 // with persistent storage.
575 candidateUserIds = mRecentTasks.usersWithRecentsLoadedLocked();
576 }
577 for (int userId : candidateUserIds) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800578 removeObsoleteFiles(persistentTaskIds, getUserImagesDir(userId).listFiles());
579 removeObsoleteFiles(persistentTaskIds, getUserTasksDir(userId).listFiles());
580 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700581 }
582
583 static Bitmap restoreImage(String filename) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700584 if (DEBUG) Slog.d(TAG, "restoreImage: restoring " + filename);
Suprabh Shukla23593142015-11-03 17:31:15 -0800585 return BitmapFactory.decodeFile(filename);
586 }
587
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800588 private File getUserPersistedTaskIdsFile(int userId) {
589 File userTaskIdsDir = new File(mTaskIdsDir, String.valueOf(userId));
590 if (!userTaskIdsDir.exists() && !userTaskIdsDir.mkdirs()) {
591 Slog.e(TAG, "Error while creating user directory: " + userTaskIdsDir);
592 }
593 return new File(userTaskIdsDir, PERSISTED_TASK_IDS_FILENAME);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800594 }
595
Suprabh Shukla23593142015-11-03 17:31:15 -0800596 static File getUserTasksDir(int userId) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800597 File userTasksDir = new File(Environment.getDataSystemCeDirectory(userId), TASKS_DIRNAME);
Suprabh Shukla23593142015-11-03 17:31:15 -0800598
599 if (!userTasksDir.exists()) {
600 if (!userTasksDir.mkdir()) {
601 Slog.e(TAG, "Failure creating tasks directory for user " + userId + ": "
602 + userTasksDir);
603 }
604 }
605 return userTasksDir;
606 }
607
608 static File getUserImagesDir(int userId) {
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700609 return new File(Environment.getDataSystemCeDirectory(userId), IMAGES_DIRNAME);
610 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800611
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700612 private static boolean createParentDirectory(String filePath) {
613 File parentDir = new File(filePath).getParentFile();
614 return parentDir.exists() || parentDir.mkdirs();
Craig Mautner21d24a22014-04-23 11:45:37 -0700615 }
616
617 private class LazyTaskWriterThread extends Thread {
Craig Mautner21d24a22014-04-23 11:45:37 -0700618
619 LazyTaskWriterThread(String name) {
620 super(name);
621 }
622
623 @Override
624 public void run() {
Riley Andrewsf16c2e82015-06-02 18:24:48 -0700625 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Craig Mautner21d24a22014-04-23 11:45:37 -0700626 ArraySet<Integer> persistentTaskIds = new ArraySet<Integer>();
627 while (true) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700628 // We can't lock mService while holding TaskPersister.this, but we don't want to
629 // call removeObsoleteFiles every time through the loop, only the last time before
630 // going to sleep. The risk is that we call removeObsoleteFiles() successively.
631 final boolean probablyDone;
Craig Mautner21d24a22014-04-23 11:45:37 -0700632 synchronized (TaskPersister.this) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700633 probablyDone = mWriteQueue.isEmpty();
634 }
635 if (probablyDone) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700636 if (DEBUG) Slog.d(TAG, "Looking for obsolete files.");
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700637 persistentTaskIds.clear();
638 synchronized (mService) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700639 if (DEBUG) Slog.d(TAG, "mRecents=" + mRecentTasks);
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800640 for (int taskNdx = mRecentTasks.size() - 1; taskNdx >= 0; --taskNdx) {
641 final TaskRecord task = mRecentTasks.get(taskNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700642 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: task=" + task +
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700643 " persistable=" + task.isPersistable);
Andrii Kulian02b7a832016-10-06 23:11:56 -0700644 final ActivityStack stack = task.getStack();
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700645 if ((task.isPersistable || task.inRecents)
Andrii Kulian02b7a832016-10-06 23:11:56 -0700646 && (stack == null || !stack.isHomeStack())) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700647 if (DEBUG) Slog.d(TAG, "adding to persistentTaskIds task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700648 persistentTaskIds.add(task.taskId);
649 } else {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700650 if (DEBUG) Slog.d(TAG,
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700651 "omitting from persistentTaskIds task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700652 }
653 }
654 }
655 removeObsoleteFiles(persistentTaskIds);
656 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800657 writeTaskIdsFiles();
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700658
659 // If mNextWriteTime, then don't delay between each call to saveToXml().
660 final WriteQueueItem item;
661 synchronized (TaskPersister.this) {
Craig Mautner63f10902014-09-16 23:57:21 -0700662 if (mNextWriteTime != FLUSH_QUEUE) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700663 // The next write we don't have to wait so long.
664 mNextWriteTime = SystemClock.uptimeMillis() + INTER_WRITE_DELAY_MS;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700665 if (DEBUG) Slog.d(TAG, "Next write time may be in " +
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700666 INTER_WRITE_DELAY_MS + " msec. (" + mNextWriteTime + ")");
667 }
668
669 while (mWriteQueue.isEmpty()) {
Dianne Hackbornce0fd762014-09-19 12:58:15 -0700670 if (mNextWriteTime != 0) {
671 mNextWriteTime = 0; // idle.
672 TaskPersister.this.notifyAll(); // wake up flush() if needed.
673 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700674 try {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700675 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: waiting indefinitely.");
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700676 TaskPersister.this.wait();
677 } catch (InterruptedException e) {
678 }
Craig Mautner63f10902014-09-16 23:57:21 -0700679 // Invariant: mNextWriteTime is either FLUSH_QUEUE or PRE_WRITE_DELAY_MS
680 // from now.
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700681 }
682 item = mWriteQueue.remove(0);
683
Craig Mautner21d24a22014-04-23 11:45:37 -0700684 long now = SystemClock.uptimeMillis();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700685 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: now=" + now + " mNextWriteTime=" +
686 mNextWriteTime + " mWriteQueue.size=" + mWriteQueue.size());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700687 while (now < mNextWriteTime) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700688 try {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700689 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: waiting " +
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700690 (mNextWriteTime - now));
691 TaskPersister.this.wait(mNextWriteTime - now);
Craig Mautner21d24a22014-04-23 11:45:37 -0700692 } catch (InterruptedException e) {
693 }
694 now = SystemClock.uptimeMillis();
695 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700696
697 // Got something to do.
Craig Mautner21d24a22014-04-23 11:45:37 -0700698 }
699
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700700 if (item instanceof ImageWriteQueueItem) {
701 ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
Suprabh Shukla23593142015-11-03 17:31:15 -0800702 final String filePath = imageWriteQueueItem.mFilePath;
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700703 if (!createParentDirectory(filePath)) {
704 Slog.e(TAG, "Error while creating images directory for file: " + filePath);
705 continue;
706 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700707 final Bitmap bitmap = imageWriteQueueItem.mImage;
Suprabh Shukla23593142015-11-03 17:31:15 -0800708 if (DEBUG) Slog.d(TAG, "writing bitmap: filename=" + filePath);
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700709 FileOutputStream imageFile = null;
710 try {
Suprabh Shukla23593142015-11-03 17:31:15 -0800711 imageFile = new FileOutputStream(new File(filePath));
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700712 bitmap.compress(Bitmap.CompressFormat.PNG, 100, imageFile);
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700713 } catch (Exception e) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800714 Slog.e(TAG, "saveImage: unable to save " + filePath, e);
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700715 } finally {
Wale Ogunwale18795a22014-12-03 11:38:33 -0800716 IoUtils.closeQuietly(imageFile);
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700717 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700718 } else if (item instanceof TaskWriteQueueItem) {
719 // Write out one task.
720 StringWriter stringWriter = null;
721 TaskRecord task = ((TaskWriteQueueItem) item).mTask;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700722 if (DEBUG) Slog.d(TAG, "Writing task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700723 synchronized (mService) {
Craig Mautner63f10902014-09-16 23:57:21 -0700724 if (task.inRecents) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700725 // Still there.
Craig Mautner21d24a22014-04-23 11:45:37 -0700726 try {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700727 if (DEBUG) Slog.d(TAG, "Saving task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700728 stringWriter = saveToXml(task);
729 } catch (IOException e) {
730 } catch (XmlPullParserException e) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700731 }
732 }
Dianne Hackborn852975d2014-08-22 17:42:43 -0700733 }
734 if (stringWriter != null) {
735 // Write out xml file while not holding mService lock.
736 FileOutputStream file = null;
737 AtomicFile atomicFile = null;
738 try {
Suprabh Shukla23593142015-11-03 17:31:15 -0800739 atomicFile = new AtomicFile(new File(
740 getUserTasksDir(task.userId),
741 String.valueOf(task.taskId) + RECENTS_FILENAME
742 + TASK_EXTENSION));
Dianne Hackborn852975d2014-08-22 17:42:43 -0700743 file = atomicFile.startWrite();
744 file.write(stringWriter.toString().getBytes());
745 file.write('\n');
746 atomicFile.finishWrite(file);
Suprabh Shukla23593142015-11-03 17:31:15 -0800747
Dianne Hackborn852975d2014-08-22 17:42:43 -0700748 } catch (IOException e) {
749 if (file != null) {
750 atomicFile.failWrite(file);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700751 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800752 Slog.e(TAG,
753 "Unable to open " + atomicFile + " for persisting. " + e);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700754 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700755 }
756 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700757 }
758 }
759 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700760}