blob: 9deabb3d6de98d3a09ee6471611a2894ee826b59 [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;
Winson Chung7900bee2017-03-10 08:59:25 -080058import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
59
60import static com.android.server.am.ActivityStackSupervisor.MATCH_TASK_IN_STACKS_OR_RECENT_TASKS;
Wale Ogunwale625ed0c2016-10-18 08:50:31 -070061
Craig Mautner21d24a22014-04-23 11:45:37 -070062public class TaskPersister {
63 static final String TAG = "TaskPersister";
Stefan Kuhnee88d1e52015-05-18 10:33:45 -070064 static final boolean DEBUG = false;
Craig Mautner21d24a22014-04-23 11:45:37 -070065
Craig Mautnerf4f8bb72014-07-29 10:41:40 -070066 /** When not flushing don't write out files faster than this */
67 private static final long INTER_WRITE_DELAY_MS = 500;
68
Suprabh Shukla23593142015-11-03 17:31:15 -080069 /**
70 * When not flushing delay this long before writing the first file out. This gives the next task
71 * being launched a chance to load its resources without this occupying IO bandwidth.
72 */
Craig Mautnerf4f8bb72014-07-29 10:41:40 -070073 private static final long PRE_TASK_DELAY_MS = 3000;
Craig Mautner21d24a22014-04-23 11:45:37 -070074
Craig Mautner63f10902014-09-16 23:57:21 -070075 /** The maximum number of entries to keep in the queue before draining it automatically. */
76 private static final int MAX_WRITE_QUEUE_LENGTH = 6;
77
78 /** Special value for mWriteTime to mean don't wait, just write */
79 private static final long FLUSH_QUEUE = -1;
80
Craig Mautner21d24a22014-04-23 11:45:37 -070081 private static final String RECENTS_FILENAME = "_task";
82 private static final String TASKS_DIRNAME = "recent_tasks";
83 private static final String TASK_EXTENSION = ".xml";
84 private static final String IMAGES_DIRNAME = "recent_images";
Suprabh Shukla4bccb462016-02-10 18:45:12 -080085 private static final String PERSISTED_TASK_IDS_FILENAME = "persisted_taskIds.txt";
Craig Mautnerc0ffce52014-07-01 12:38:52 -070086 static final String IMAGE_EXTENSION = ".png";
Craig Mautner21d24a22014-04-23 11:45:37 -070087
Winson Chung36f3f032016-09-08 23:29:43 +000088 private static final String TAG_TASK = "task";
Craig Mautner21d24a22014-04-23 11:45:37 -070089
Craig Mautner21d24a22014-04-23 11:45:37 -070090 private final ActivityManagerService mService;
91 private final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -080092 private final RecentTasks mRecentTasks;
Suprabh Shukla4bccb462016-02-10 18:45:12 -080093 private final SparseArray<SparseBooleanArray> mTaskIdsInFile = new SparseArray<>();
Suprabh Shuklaf50b4582016-02-23 17:44:22 -080094 private final File mTaskIdsDir;
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -070095 // To lock file operations in TaskPersister
96 private final Object mIoLock = new Object();
Craig Mautner21d24a22014-04-23 11:45:37 -070097
Suprabh Shukla23593142015-11-03 17:31:15 -080098 /**
99 * Value determines write delay mode as follows: < 0 We are Flushing. No delays between writes
100 * until the image queue is drained and all tasks needing persisting are written to disk. There
101 * is no delay between writes. == 0 We are Idle. Next writes will be delayed by
102 * #PRE_TASK_DELAY_MS. > 0 We are Actively writing. Next write will be at this time. Subsequent
103 * writes will be delayed by #INTER_WRITE_DELAY_MS.
104 */
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700105 private long mNextWriteTime = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -0700106
107 private final LazyTaskWriterThread mLazyTaskWriterThread;
108
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700109 private static class WriteQueueItem {}
Suprabh Shukla23593142015-11-03 17:31:15 -0800110
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700111 private static class TaskWriteQueueItem extends WriteQueueItem {
112 final TaskRecord mTask;
Winsonc809cbb2015-11-02 12:06:15 -0800113
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700114 TaskWriteQueueItem(TaskRecord task) {
115 mTask = task;
116 }
117 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800118
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700119 private static class ImageWriteQueueItem extends WriteQueueItem {
Suprabh Shukla23593142015-11-03 17:31:15 -0800120 final String mFilePath;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700121 Bitmap mImage;
Winsonc809cbb2015-11-02 12:06:15 -0800122
Suprabh Shukla23593142015-11-03 17:31:15 -0800123 ImageWriteQueueItem(String filePath, Bitmap image) {
124 mFilePath = filePath;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700125 mImage = image;
126 }
127 }
128
129 ArrayList<WriteQueueItem> mWriteQueue = new ArrayList<WriteQueueItem>();
130
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800131 TaskPersister(File systemDir, ActivityStackSupervisor stackSupervisor,
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800132 ActivityManagerService service, RecentTasks recentTasks) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800133
134 final File legacyImagesDir = new File(systemDir, IMAGES_DIRNAME);
135 if (legacyImagesDir.exists()) {
136 if (!FileUtils.deleteContents(legacyImagesDir) || !legacyImagesDir.delete()) {
137 Slog.i(TAG, "Failure deleting legacy images directory: " + legacyImagesDir);
Craig Mautner21d24a22014-04-23 11:45:37 -0700138 }
139 }
140
Suprabh Shukla23593142015-11-03 17:31:15 -0800141 final File legacyTasksDir = new File(systemDir, TASKS_DIRNAME);
142 if (legacyTasksDir.exists()) {
143 if (!FileUtils.deleteContents(legacyTasksDir) || !legacyTasksDir.delete()) {
144 Slog.i(TAG, "Failure deleting legacy tasks directory: " + legacyTasksDir);
Craig Mautner21d24a22014-04-23 11:45:37 -0700145 }
146 }
147
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800148 mTaskIdsDir = new File(Environment.getDataDirectory(), "system_de");
Craig Mautner21d24a22014-04-23 11:45:37 -0700149 mStackSupervisor = stackSupervisor;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800150 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800151 mRecentTasks = recentTasks;
Craig Mautner21d24a22014-04-23 11:45:37 -0700152 mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThread");
153 }
154
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800155 @VisibleForTesting
156 TaskPersister(File workingDir) {
157 mTaskIdsDir = workingDir;
158 mStackSupervisor = null;
159 mService = null;
160 mRecentTasks = null;
161 mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThreadTest");
162 }
163
Craig Mautner21d24a22014-04-23 11:45:37 -0700164 void startPersisting() {
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800165 if (!mLazyTaskWriterThread.isAlive()) {
166 mLazyTaskWriterThread.start();
167 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700168 }
169
Craig Mautner63f10902014-09-16 23:57:21 -0700170 private void removeThumbnails(TaskRecord task) {
171 final String taskString = Integer.toString(task.taskId);
172 for (int queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
173 final WriteQueueItem item = mWriteQueue.get(queueNdx);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800174 if (item instanceof ImageWriteQueueItem) {
175 final File thumbnailFile = new File(((ImageWriteQueueItem) item).mFilePath);
176 if (thumbnailFile.getName().startsWith(taskString)) {
177 if (DEBUG) {
178 Slog.d(TAG, "Removing " + ((ImageWriteQueueItem) item).mFilePath +
179 " from write queue");
180 }
181 mWriteQueue.remove(queueNdx);
182 }
Craig Mautner63f10902014-09-16 23:57:21 -0700183 }
184 }
185 }
186
187 private void yieldIfQueueTooDeep() {
188 boolean stall = false;
189 synchronized (this) {
190 if (mNextWriteTime == FLUSH_QUEUE) {
191 stall = true;
192 }
193 }
194 if (stall) {
195 Thread.yield();
196 }
197 }
198
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800199 @NonNull
200 SparseBooleanArray loadPersistedTaskIdsForUser(int userId) {
201 if (mTaskIdsInFile.get(userId) != null) {
202 return mTaskIdsInFile.get(userId).clone();
203 }
204 final SparseBooleanArray persistedTaskIds = new SparseBooleanArray();
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700205 synchronized (mIoLock) {
206 BufferedReader reader = null;
207 String line;
208 try {
209 reader = new BufferedReader(new FileReader(getUserPersistedTaskIdsFile(userId)));
210 while ((line = reader.readLine()) != null) {
211 for (String taskIdString : line.split("\\s+")) {
212 int id = Integer.parseInt(taskIdString);
213 persistedTaskIds.put(id, true);
214 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800215 }
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700216 } catch (FileNotFoundException e) {
217 // File doesn't exist. Ignore.
218 } catch (Exception e) {
219 Slog.e(TAG, "Error while reading taskIds file for user " + userId, e);
220 } finally {
221 IoUtils.closeQuietly(reader);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800222 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800223 }
224 mTaskIdsInFile.put(userId, persistedTaskIds);
225 return persistedTaskIds.clone();
226 }
227
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700228
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800229 @VisibleForTesting
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700230 void writePersistedTaskIdsForUser(@NonNull SparseBooleanArray taskIds, int userId) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800231 if (userId < 0) {
232 return;
233 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800234 final File persistedTaskIdsFile = getUserPersistedTaskIdsFile(userId);
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700235 synchronized (mIoLock) {
236 BufferedWriter writer = null;
237 try {
238 writer = new BufferedWriter(new FileWriter(persistedTaskIdsFile));
239 for (int i = 0; i < taskIds.size(); i++) {
240 if (taskIds.valueAt(i)) {
241 writer.write(String.valueOf(taskIds.keyAt(i)));
242 writer.newLine();
243 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800244 }
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700245 } catch (Exception e) {
246 Slog.e(TAG, "Error while writing taskIds file for user " + userId, e);
247 } finally {
248 IoUtils.closeQuietly(writer);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800249 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800250 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800251 }
252
253 void unloadUserDataFromMemory(int userId) {
254 mTaskIdsInFile.delete(userId);
255 }
256
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700257 void wakeup(TaskRecord task, boolean flush) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700258 synchronized (this) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700259 if (task != null) {
260 int queueNdx;
261 for (queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
262 final WriteQueueItem item = mWriteQueue.get(queueNdx);
263 if (item instanceof TaskWriteQueueItem &&
264 ((TaskWriteQueueItem) item).mTask == task) {
Craig Mautner63f10902014-09-16 23:57:21 -0700265 if (!task.inRecents) {
266 // This task is being removed.
267 removeThumbnails(task);
268 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700269 break;
270 }
271 }
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700272 if (queueNdx < 0 && task.isPersistable) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700273 mWriteQueue.add(new TaskWriteQueueItem(task));
274 }
275 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800276 // Dummy. Ensures removeObsoleteFiles is called when LazyTaskThreadWriter is
277 // notified.
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700278 mWriteQueue.add(new WriteQueueItem());
279 }
Craig Mautner63f10902014-09-16 23:57:21 -0700280 if (flush || mWriteQueue.size() > MAX_WRITE_QUEUE_LENGTH) {
281 mNextWriteTime = FLUSH_QUEUE;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700282 } else if (mNextWriteTime == 0) {
283 mNextWriteTime = SystemClock.uptimeMillis() + PRE_TASK_DELAY_MS;
284 }
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700285 if (DEBUG) Slog.d(TAG, "wakeup: task=" + task + " flush=" + flush + " mNextWriteTime="
286 + mNextWriteTime + " mWriteQueue.size=" + mWriteQueue.size()
287 + " Callers=" + Debug.getCallers(4));
Craig Mautner21d24a22014-04-23 11:45:37 -0700288 notifyAll();
289 }
Craig Mautner63f10902014-09-16 23:57:21 -0700290
291 yieldIfQueueTooDeep();
Craig Mautner21d24a22014-04-23 11:45:37 -0700292 }
293
Dianne Hackbornce0fd762014-09-19 12:58:15 -0700294 void flush() {
295 synchronized (this) {
296 mNextWriteTime = FLUSH_QUEUE;
297 notifyAll();
298 do {
299 try {
300 wait();
301 } catch (InterruptedException e) {
302 }
303 } while (mNextWriteTime == FLUSH_QUEUE);
304 }
305 }
306
Suprabh Shukla23593142015-11-03 17:31:15 -0800307 void saveImage(Bitmap image, String filePath) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700308 synchronized (this) {
309 int queueNdx;
310 for (queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
311 final WriteQueueItem item = mWriteQueue.get(queueNdx);
312 if (item instanceof ImageWriteQueueItem) {
313 ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
Suprabh Shukla23593142015-11-03 17:31:15 -0800314 if (imageWriteQueueItem.mFilePath.equals(filePath)) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700315 // replace the Bitmap with the new one.
316 imageWriteQueueItem.mImage = image;
317 break;
318 }
319 }
320 }
321 if (queueNdx < 0) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800322 mWriteQueue.add(new ImageWriteQueueItem(filePath, image));
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700323 }
Craig Mautner63f10902014-09-16 23:57:21 -0700324 if (mWriteQueue.size() > MAX_WRITE_QUEUE_LENGTH) {
325 mNextWriteTime = FLUSH_QUEUE;
326 } else if (mNextWriteTime == 0) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700327 mNextWriteTime = SystemClock.uptimeMillis() + PRE_TASK_DELAY_MS;
328 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800329 if (DEBUG) Slog.d(TAG, "saveImage: filePath=" + filePath + " now=" +
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700330 SystemClock.uptimeMillis() + " mNextWriteTime=" +
331 mNextWriteTime + " Callers=" + Debug.getCallers(4));
332 notifyAll();
333 }
Craig Mautner63f10902014-09-16 23:57:21 -0700334
335 yieldIfQueueTooDeep();
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700336 }
337
Suprabh Shukla23593142015-11-03 17:31:15 -0800338 Bitmap getTaskDescriptionIcon(String filePath) {
Craig Mautner648f69b2014-09-18 14:16:26 -0700339 // See if it is in the write queue
Suprabh Shukla23593142015-11-03 17:31:15 -0800340 final Bitmap icon = getImageFromWriteQueue(filePath);
Craig Mautner648f69b2014-09-18 14:16:26 -0700341 if (icon != null) {
342 return icon;
343 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800344 return restoreImage(filePath);
Craig Mautner648f69b2014-09-18 14:16:26 -0700345 }
346
Suprabh Shukla23593142015-11-03 17:31:15 -0800347 Bitmap getImageFromWriteQueue(String filePath) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700348 synchronized (this) {
349 for (int queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
350 final WriteQueueItem item = mWriteQueue.get(queueNdx);
351 if (item instanceof ImageWriteQueueItem) {
352 ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
Suprabh Shukla23593142015-11-03 17:31:15 -0800353 if (imageWriteQueueItem.mFilePath.equals(filePath)) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700354 return imageWriteQueueItem.mImage;
355 }
356 }
357 }
358 return null;
359 }
360 }
361
Craig Mautner21d24a22014-04-23 11:45:37 -0700362 private StringWriter saveToXml(TaskRecord task) throws IOException, XmlPullParserException {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700363 if (DEBUG) Slog.d(TAG, "saveToXml: task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -0700364 final XmlSerializer xmlSerializer = new FastXmlSerializer();
365 StringWriter stringWriter = new StringWriter();
366 xmlSerializer.setOutput(stringWriter);
367
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700368 if (DEBUG) xmlSerializer.setFeature(
Suprabh Shukla23593142015-11-03 17:31:15 -0800369 "http://xmlpull.org/v1/doc/features.html#indent-output", true);
Craig Mautner21d24a22014-04-23 11:45:37 -0700370
371 // save task
372 xmlSerializer.startDocument(null, true);
373
374 xmlSerializer.startTag(null, TAG_TASK);
375 task.saveToXml(xmlSerializer);
376 xmlSerializer.endTag(null, TAG_TASK);
377
378 xmlSerializer.endDocument();
379 xmlSerializer.flush();
380
381 return stringWriter;
382 }
383
Craig Mautner77b04262014-06-27 15:22:12 -0700384 private String fileToString(File file) {
385 final String newline = System.lineSeparator();
386 try {
387 BufferedReader reader = new BufferedReader(new FileReader(file));
388 StringBuffer sb = new StringBuffer((int) file.length() * 2);
389 String line;
390 while ((line = reader.readLine()) != null) {
391 sb.append(line + newline);
392 }
393 reader.close();
394 return sb.toString();
395 } catch (IOException ioe) {
396 Slog.e(TAG, "Couldn't read file " + file.getName());
397 return null;
398 }
399 }
400
Craig Mautnera228ae92014-07-09 05:44:55 -0700401 private TaskRecord taskIdToTask(int taskId, ArrayList<TaskRecord> tasks) {
402 if (taskId < 0) {
403 return null;
404 }
405 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
406 final TaskRecord task = tasks.get(taskNdx);
407 if (task.taskId == taskId) {
408 return task;
409 }
410 }
411 Slog.e(TAG, "Restore affiliation error looking for taskId=" + taskId);
412 return null;
413 }
414
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800415 List<TaskRecord> restoreTasksForUserLocked(final int userId) {
416 final ArrayList<TaskRecord> tasks = new ArrayList<TaskRecord>();
Craig Mautner21d24a22014-04-23 11:45:37 -0700417 ArraySet<Integer> recoveredTaskIds = new ArraySet<Integer>();
418
Suprabh Shukla23593142015-11-03 17:31:15 -0800419 File userTasksDir = getUserTasksDir(userId);
Winson Chung36f3f032016-09-08 23:29:43 +0000420
Suprabh Shukla23593142015-11-03 17:31:15 -0800421 File[] recentFiles = userTasksDir.listFiles();
Craig Mautner21d24a22014-04-23 11:45:37 -0700422 if (recentFiles == null) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800423 Slog.e(TAG, "restoreTasksForUserLocked: Unable to list files from " + userTasksDir);
Craig Mautner21d24a22014-04-23 11:45:37 -0700424 return tasks;
425 }
426
427 for (int taskNdx = 0; taskNdx < recentFiles.length; ++taskNdx) {
428 File taskFile = recentFiles[taskNdx];
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800429 if (DEBUG) {
430 Slog.d(TAG, "restoreTasksForUserLocked: userId=" + userId
431 + ", taskFile=" + taskFile.getName());
432 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700433 BufferedReader reader = null;
Craig Mautnere0129b32014-05-25 16:41:09 -0700434 boolean deleteFile = false;
Craig Mautner21d24a22014-04-23 11:45:37 -0700435 try {
436 reader = new BufferedReader(new FileReader(taskFile));
437 final XmlPullParser in = Xml.newPullParser();
438 in.setInput(reader);
439
440 int event;
441 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
442 event != XmlPullParser.END_TAG) {
443 final String name = in.getName();
444 if (event == XmlPullParser.START_TAG) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800445 if (DEBUG) Slog.d(TAG, "restoreTasksForUserLocked: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -0700446 if (TAG_TASK.equals(name)) {
Winson Chung36f3f032016-09-08 23:29:43 +0000447 final TaskRecord task = TaskRecord.restoreFromXml(in, mStackSupervisor);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800448 if (DEBUG) Slog.d(TAG, "restoreTasksForUserLocked: restored task="
Suprabh Shukla23593142015-11-03 17:31:15 -0800449 + task);
Craig Mautner21d24a22014-04-23 11:45:37 -0700450 if (task != null) {
Winson Chung36f3f032016-09-08 23:29:43 +0000451 // XXX Don't add to write queue... there is no reason to write
452 // out the stuff we just read, if we don't write it we will
453 // read the same thing again.
454 // mWriteQueue.add(new TaskWriteQueueItem(task));
455
Craig Mautner21d24a22014-04-23 11:45:37 -0700456 final int taskId = task.taskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800457 if (mStackSupervisor.anyTaskForIdLocked(taskId,
Winson Chung7900bee2017-03-10 08:59:25 -0800458 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
459 INVALID_STACK_ID) != null) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800460 // Should not happen.
461 Slog.wtf(TAG, "Existing task with taskId " + taskId + "found");
462 } else if (userId != task.userId) {
463 // Should not happen.
464 Slog.wtf(TAG, "Task with userId " + task.userId + " found in "
465 + userTasksDir.getAbsolutePath());
466 } else {
467 // Looks fine.
468 mStackSupervisor.setNextTaskIdForUserLocked(taskId, userId);
Amith Yamasani515d4062015-09-28 11:30:06 -0700469 task.isPersistable = true;
470 tasks.add(task);
471 recoveredTaskIds.add(taskId);
472 }
Craig Mautner77b04262014-06-27 15:22:12 -0700473 } else {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800474 Slog.e(TAG, "restoreTasksForUserLocked: Unable to restore taskFile="
Suprabh Shukla23593142015-11-03 17:31:15 -0800475 + taskFile + ": " + fileToString(taskFile));
Craig Mautner21d24a22014-04-23 11:45:37 -0700476 }
477 } else {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800478 Slog.wtf(TAG, "restoreTasksForUserLocked: Unknown xml event=" + event
Suprabh Shukla23593142015-11-03 17:31:15 -0800479 + " name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -0700480 }
481 }
482 XmlUtils.skipCurrentTag(in);
483 }
Craig Mautnere0129b32014-05-25 16:41:09 -0700484 } catch (Exception e) {
Craig Mautnera228ae92014-07-09 05:44:55 -0700485 Slog.wtf(TAG, "Unable to parse " + taskFile + ". Error ", e);
Craig Mautner77b04262014-06-27 15:22:12 -0700486 Slog.e(TAG, "Failing file: " + fileToString(taskFile));
Craig Mautnere0129b32014-05-25 16:41:09 -0700487 deleteFile = true;
Craig Mautner21d24a22014-04-23 11:45:37 -0700488 } finally {
Wale Ogunwale18795a22014-12-03 11:38:33 -0800489 IoUtils.closeQuietly(reader);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700490 if (deleteFile) {
491 if (DEBUG) Slog.d(TAG, "Deleting file=" + taskFile.getName());
Craig Mautnere0129b32014-05-25 16:41:09 -0700492 taskFile.delete();
493 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700494 }
495 }
496
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700497 if (!DEBUG) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800498 removeObsoleteFiles(recoveredTaskIds, userTasksDir.listFiles());
499 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700500
Suprabh Shukla23593142015-11-03 17:31:15 -0800501 // Fix up task affiliation from taskIds
Craig Mautnera228ae92014-07-09 05:44:55 -0700502 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
503 final TaskRecord task = tasks.get(taskNdx);
504 task.setPrevAffiliate(taskIdToTask(task.mPrevAffiliateTaskId, tasks));
505 task.setNextAffiliate(taskIdToTask(task.mNextAffiliateTaskId, tasks));
506 }
507
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800508 Collections.sort(tasks, new Comparator<TaskRecord>() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700509 @Override
510 public int compare(TaskRecord lhs, TaskRecord rhs) {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700511 final long diff = rhs.mLastTimeMoved - lhs.mLastTimeMoved;
Craig Mautner21d24a22014-04-23 11:45:37 -0700512 if (diff < 0) {
513 return -1;
514 } else if (diff > 0) {
515 return +1;
516 } else {
517 return 0;
518 }
519 }
520 });
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800521 return tasks;
Craig Mautner21d24a22014-04-23 11:45:37 -0700522 }
523
Craig Mautnere0129b32014-05-25 16:41:09 -0700524 private static void removeObsoleteFiles(ArraySet<Integer> persistentTaskIds, File[] files) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800525 if (DEBUG) Slog.d(TAG, "removeObsoleteFiles: persistentTaskIds=" + persistentTaskIds +
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700526 " files=" + files);
Craig Mautnera5badf02014-09-11 12:47:03 -0700527 if (files == null) {
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700528 Slog.e(TAG, "File error accessing recents directory (directory doesn't exist?).");
Craig Mautnera5badf02014-09-11 12:47:03 -0700529 return;
530 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700531 for (int fileNdx = 0; fileNdx < files.length; ++fileNdx) {
532 File file = files[fileNdx];
533 String filename = file.getName();
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700534 final int taskIdEnd = filename.indexOf('_');
Craig Mautner21d24a22014-04-23 11:45:37 -0700535 if (taskIdEnd > 0) {
536 final int taskId;
537 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +0100538 taskId = Integer.parseInt(filename.substring(0, taskIdEnd));
Suprabh Shukla23593142015-11-03 17:31:15 -0800539 if (DEBUG) Slog.d(TAG, "removeObsoleteFiles: Found taskId=" + taskId);
Craig Mautner21d24a22014-04-23 11:45:37 -0700540 } catch (Exception e) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800541 Slog.wtf(TAG, "removeObsoleteFiles: Can't parse file=" + file.getName());
Craig Mautner21d24a22014-04-23 11:45:37 -0700542 file.delete();
543 continue;
544 }
545 if (!persistentTaskIds.contains(taskId)) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800546 if (DEBUG) Slog.d(TAG, "removeObsoleteFiles: deleting file=" + file.getName());
Craig Mautner21d24a22014-04-23 11:45:37 -0700547 file.delete();
548 }
549 }
550 }
551 }
552
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800553 private void writeTaskIdsFiles() {
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700554 SparseArray<SparseBooleanArray> changedTaskIdsPerUser = new SparseArray<>();
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800555 synchronized (mService) {
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700556 for (int userId : mRecentTasks.usersWithRecentsLoadedLocked()) {
557 SparseBooleanArray taskIdsToSave = mRecentTasks.mPersistedTaskIds.get(userId);
558 SparseBooleanArray persistedIdsInFile = mTaskIdsInFile.get(userId);
559 if (persistedIdsInFile != null && persistedIdsInFile.equals(taskIdsToSave)) {
560 continue;
561 } else {
562 SparseBooleanArray taskIdsToSaveCopy = taskIdsToSave.clone();
563 mTaskIdsInFile.put(userId, taskIdsToSaveCopy);
564 changedTaskIdsPerUser.put(userId, taskIdsToSaveCopy);
565 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800566 }
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700567 }
568 for (int i = 0; i < changedTaskIdsPerUser.size(); i++) {
569 writePersistedTaskIdsForUser(changedTaskIdsPerUser.valueAt(i),
570 changedTaskIdsPerUser.keyAt(i));
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800571 }
572 }
573
Craig Mautner21d24a22014-04-23 11:45:37 -0700574 private void removeObsoleteFiles(ArraySet<Integer> persistentTaskIds) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800575 int[] candidateUserIds;
576 synchronized (mService) {
577 // Remove only from directories of the users who have recents in memory synchronized
578 // with persistent storage.
579 candidateUserIds = mRecentTasks.usersWithRecentsLoadedLocked();
580 }
581 for (int userId : candidateUserIds) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800582 removeObsoleteFiles(persistentTaskIds, getUserImagesDir(userId).listFiles());
583 removeObsoleteFiles(persistentTaskIds, getUserTasksDir(userId).listFiles());
584 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700585 }
586
587 static Bitmap restoreImage(String filename) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700588 if (DEBUG) Slog.d(TAG, "restoreImage: restoring " + filename);
Suprabh Shukla23593142015-11-03 17:31:15 -0800589 return BitmapFactory.decodeFile(filename);
590 }
591
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800592 private File getUserPersistedTaskIdsFile(int userId) {
593 File userTaskIdsDir = new File(mTaskIdsDir, String.valueOf(userId));
594 if (!userTaskIdsDir.exists() && !userTaskIdsDir.mkdirs()) {
595 Slog.e(TAG, "Error while creating user directory: " + userTaskIdsDir);
596 }
597 return new File(userTaskIdsDir, PERSISTED_TASK_IDS_FILENAME);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800598 }
599
Suprabh Shukla23593142015-11-03 17:31:15 -0800600 static File getUserTasksDir(int userId) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800601 File userTasksDir = new File(Environment.getDataSystemCeDirectory(userId), TASKS_DIRNAME);
Suprabh Shukla23593142015-11-03 17:31:15 -0800602
603 if (!userTasksDir.exists()) {
604 if (!userTasksDir.mkdir()) {
605 Slog.e(TAG, "Failure creating tasks directory for user " + userId + ": "
606 + userTasksDir);
607 }
608 }
609 return userTasksDir;
610 }
611
612 static File getUserImagesDir(int userId) {
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700613 return new File(Environment.getDataSystemCeDirectory(userId), IMAGES_DIRNAME);
614 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800615
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700616 private static boolean createParentDirectory(String filePath) {
617 File parentDir = new File(filePath).getParentFile();
618 return parentDir.exists() || parentDir.mkdirs();
Craig Mautner21d24a22014-04-23 11:45:37 -0700619 }
620
621 private class LazyTaskWriterThread extends Thread {
Craig Mautner21d24a22014-04-23 11:45:37 -0700622
623 LazyTaskWriterThread(String name) {
624 super(name);
625 }
626
627 @Override
628 public void run() {
Riley Andrewsf16c2e82015-06-02 18:24:48 -0700629 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Craig Mautner21d24a22014-04-23 11:45:37 -0700630 ArraySet<Integer> persistentTaskIds = new ArraySet<Integer>();
631 while (true) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700632 // We can't lock mService while holding TaskPersister.this, but we don't want to
633 // call removeObsoleteFiles every time through the loop, only the last time before
634 // going to sleep. The risk is that we call removeObsoleteFiles() successively.
635 final boolean probablyDone;
Craig Mautner21d24a22014-04-23 11:45:37 -0700636 synchronized (TaskPersister.this) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700637 probablyDone = mWriteQueue.isEmpty();
638 }
639 if (probablyDone) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700640 if (DEBUG) Slog.d(TAG, "Looking for obsolete files.");
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700641 persistentTaskIds.clear();
642 synchronized (mService) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700643 if (DEBUG) Slog.d(TAG, "mRecents=" + mRecentTasks);
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800644 for (int taskNdx = mRecentTasks.size() - 1; taskNdx >= 0; --taskNdx) {
645 final TaskRecord task = mRecentTasks.get(taskNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700646 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: task=" + task +
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700647 " persistable=" + task.isPersistable);
Andrii Kulian02b7a832016-10-06 23:11:56 -0700648 final ActivityStack stack = task.getStack();
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700649 if ((task.isPersistable || task.inRecents)
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800650 && (stack == null || !stack.isHomeOrRecentsStack())) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700651 if (DEBUG) Slog.d(TAG, "adding to persistentTaskIds task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700652 persistentTaskIds.add(task.taskId);
653 } else {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700654 if (DEBUG) Slog.d(TAG,
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700655 "omitting from persistentTaskIds task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700656 }
657 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100658 mService.mWindowManager.removeObsoleteTaskFiles(persistentTaskIds,
659 mRecentTasks.usersWithRecentsLoadedLocked());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700660 }
661 removeObsoleteFiles(persistentTaskIds);
662 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800663 writeTaskIdsFiles();
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700664
665 // If mNextWriteTime, then don't delay between each call to saveToXml().
666 final WriteQueueItem item;
667 synchronized (TaskPersister.this) {
Craig Mautner63f10902014-09-16 23:57:21 -0700668 if (mNextWriteTime != FLUSH_QUEUE) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700669 // The next write we don't have to wait so long.
670 mNextWriteTime = SystemClock.uptimeMillis() + INTER_WRITE_DELAY_MS;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700671 if (DEBUG) Slog.d(TAG, "Next write time may be in " +
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700672 INTER_WRITE_DELAY_MS + " msec. (" + mNextWriteTime + ")");
673 }
674
675 while (mWriteQueue.isEmpty()) {
Dianne Hackbornce0fd762014-09-19 12:58:15 -0700676 if (mNextWriteTime != 0) {
677 mNextWriteTime = 0; // idle.
678 TaskPersister.this.notifyAll(); // wake up flush() if needed.
679 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700680 try {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700681 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: waiting indefinitely.");
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700682 TaskPersister.this.wait();
683 } catch (InterruptedException e) {
684 }
Craig Mautner63f10902014-09-16 23:57:21 -0700685 // Invariant: mNextWriteTime is either FLUSH_QUEUE or PRE_WRITE_DELAY_MS
686 // from now.
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700687 }
688 item = mWriteQueue.remove(0);
689
Craig Mautner21d24a22014-04-23 11:45:37 -0700690 long now = SystemClock.uptimeMillis();
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700691 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: now=" + now + " mNextWriteTime=" +
692 mNextWriteTime + " mWriteQueue.size=" + mWriteQueue.size());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700693 while (now < mNextWriteTime) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700694 try {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700695 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: waiting " +
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700696 (mNextWriteTime - now));
697 TaskPersister.this.wait(mNextWriteTime - now);
Craig Mautner21d24a22014-04-23 11:45:37 -0700698 } catch (InterruptedException e) {
699 }
700 now = SystemClock.uptimeMillis();
701 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700702
703 // Got something to do.
Craig Mautner21d24a22014-04-23 11:45:37 -0700704 }
705
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700706 if (item instanceof ImageWriteQueueItem) {
707 ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
Suprabh Shukla23593142015-11-03 17:31:15 -0800708 final String filePath = imageWriteQueueItem.mFilePath;
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700709 if (!createParentDirectory(filePath)) {
710 Slog.e(TAG, "Error while creating images directory for file: " + filePath);
711 continue;
712 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700713 final Bitmap bitmap = imageWriteQueueItem.mImage;
Suprabh Shukla23593142015-11-03 17:31:15 -0800714 if (DEBUG) Slog.d(TAG, "writing bitmap: filename=" + filePath);
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700715 FileOutputStream imageFile = null;
716 try {
Suprabh Shukla23593142015-11-03 17:31:15 -0800717 imageFile = new FileOutputStream(new File(filePath));
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700718 bitmap.compress(Bitmap.CompressFormat.PNG, 100, imageFile);
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700719 } catch (Exception e) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800720 Slog.e(TAG, "saveImage: unable to save " + filePath, e);
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700721 } finally {
Wale Ogunwale18795a22014-12-03 11:38:33 -0800722 IoUtils.closeQuietly(imageFile);
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700723 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700724 } else if (item instanceof TaskWriteQueueItem) {
725 // Write out one task.
726 StringWriter stringWriter = null;
727 TaskRecord task = ((TaskWriteQueueItem) item).mTask;
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700728 if (DEBUG) Slog.d(TAG, "Writing task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700729 synchronized (mService) {
Craig Mautner63f10902014-09-16 23:57:21 -0700730 if (task.inRecents) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700731 // Still there.
Craig Mautner21d24a22014-04-23 11:45:37 -0700732 try {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700733 if (DEBUG) Slog.d(TAG, "Saving task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700734 stringWriter = saveToXml(task);
735 } catch (IOException e) {
736 } catch (XmlPullParserException e) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700737 }
738 }
Dianne Hackborn852975d2014-08-22 17:42:43 -0700739 }
740 if (stringWriter != null) {
741 // Write out xml file while not holding mService lock.
742 FileOutputStream file = null;
743 AtomicFile atomicFile = null;
744 try {
Suprabh Shukla23593142015-11-03 17:31:15 -0800745 atomicFile = new AtomicFile(new File(
746 getUserTasksDir(task.userId),
747 String.valueOf(task.taskId) + RECENTS_FILENAME
748 + TASK_EXTENSION));
Dianne Hackborn852975d2014-08-22 17:42:43 -0700749 file = atomicFile.startWrite();
750 file.write(stringWriter.toString().getBytes());
751 file.write('\n');
752 atomicFile.finishWrite(file);
Suprabh Shukla23593142015-11-03 17:31:15 -0800753
Dianne Hackborn852975d2014-08-22 17:42:43 -0700754 } catch (IOException e) {
755 if (file != null) {
756 atomicFile.failWrite(file);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700757 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800758 Slog.e(TAG,
759 "Unable to open " + atomicFile + " for persisting. " + e);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700760 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700761 }
762 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700763 }
764 }
765 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700766}