blob: 74c4826f583b7087b902ecb85200898a9d927fd6 [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 TASKS_DIRNAME = "recent_tasks";
Daichi Hirono4cb941e2017-03-31 14:30:41 +090082 private static final String TASK_FILENAME_SUFFIX = "_task.xml";
Craig Mautner21d24a22014-04-23 11:45:37 -070083 private static final String IMAGES_DIRNAME = "recent_images";
Suprabh Shukla4bccb462016-02-10 18:45:12 -080084 private static final String PERSISTED_TASK_IDS_FILENAME = "persisted_taskIds.txt";
Craig Mautnerc0ffce52014-07-01 12:38:52 -070085 static final String IMAGE_EXTENSION = ".png";
Craig Mautner21d24a22014-04-23 11:45:37 -070086
Winson Chung36f3f032016-09-08 23:29:43 +000087 private static final String TAG_TASK = "task";
Craig Mautner21d24a22014-04-23 11:45:37 -070088
Craig Mautner21d24a22014-04-23 11:45:37 -070089 private final ActivityManagerService mService;
90 private final ActivityStackSupervisor mStackSupervisor;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -080091 private final RecentTasks mRecentTasks;
Suprabh Shukla4bccb462016-02-10 18:45:12 -080092 private final SparseArray<SparseBooleanArray> mTaskIdsInFile = new SparseArray<>();
Suprabh Shuklaf50b4582016-02-23 17:44:22 -080093 private final File mTaskIdsDir;
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -070094 // To lock file operations in TaskPersister
95 private final Object mIoLock = new Object();
Craig Mautner21d24a22014-04-23 11:45:37 -070096
Suprabh Shukla23593142015-11-03 17:31:15 -080097 /**
98 * Value determines write delay mode as follows: < 0 We are Flushing. No delays between writes
99 * until the image queue is drained and all tasks needing persisting are written to disk. There
100 * is no delay between writes. == 0 We are Idle. Next writes will be delayed by
101 * #PRE_TASK_DELAY_MS. > 0 We are Actively writing. Next write will be at this time. Subsequent
102 * writes will be delayed by #INTER_WRITE_DELAY_MS.
103 */
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700104 private long mNextWriteTime = 0;
Craig Mautner21d24a22014-04-23 11:45:37 -0700105
106 private final LazyTaskWriterThread mLazyTaskWriterThread;
107
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700108 private static class WriteQueueItem {}
Suprabh Shukla23593142015-11-03 17:31:15 -0800109
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700110 private static class TaskWriteQueueItem extends WriteQueueItem {
111 final TaskRecord mTask;
Winsonc809cbb2015-11-02 12:06:15 -0800112
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700113 TaskWriteQueueItem(TaskRecord task) {
114 mTask = task;
115 }
116 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800117
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700118 private static class ImageWriteQueueItem extends WriteQueueItem {
Suprabh Shukla23593142015-11-03 17:31:15 -0800119 final String mFilePath;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700120 Bitmap mImage;
Winsonc809cbb2015-11-02 12:06:15 -0800121
Suprabh Shukla23593142015-11-03 17:31:15 -0800122 ImageWriteQueueItem(String filePath, Bitmap image) {
123 mFilePath = filePath;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700124 mImage = image;
125 }
126 }
127
128 ArrayList<WriteQueueItem> mWriteQueue = new ArrayList<WriteQueueItem>();
129
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800130 TaskPersister(File systemDir, ActivityStackSupervisor stackSupervisor,
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800131 ActivityManagerService service, RecentTasks recentTasks) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800132
133 final File legacyImagesDir = new File(systemDir, IMAGES_DIRNAME);
134 if (legacyImagesDir.exists()) {
135 if (!FileUtils.deleteContents(legacyImagesDir) || !legacyImagesDir.delete()) {
136 Slog.i(TAG, "Failure deleting legacy images directory: " + legacyImagesDir);
Craig Mautner21d24a22014-04-23 11:45:37 -0700137 }
138 }
139
Suprabh Shukla23593142015-11-03 17:31:15 -0800140 final File legacyTasksDir = new File(systemDir, TASKS_DIRNAME);
141 if (legacyTasksDir.exists()) {
142 if (!FileUtils.deleteContents(legacyTasksDir) || !legacyTasksDir.delete()) {
143 Slog.i(TAG, "Failure deleting legacy tasks directory: " + legacyTasksDir);
Craig Mautner21d24a22014-04-23 11:45:37 -0700144 }
145 }
146
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800147 mTaskIdsDir = new File(Environment.getDataDirectory(), "system_de");
Craig Mautner21d24a22014-04-23 11:45:37 -0700148 mStackSupervisor = stackSupervisor;
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800149 mService = service;
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800150 mRecentTasks = recentTasks;
Craig Mautner21d24a22014-04-23 11:45:37 -0700151 mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThread");
152 }
153
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800154 @VisibleForTesting
155 TaskPersister(File workingDir) {
156 mTaskIdsDir = workingDir;
157 mStackSupervisor = null;
158 mService = null;
159 mRecentTasks = null;
160 mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThreadTest");
161 }
162
Craig Mautner21d24a22014-04-23 11:45:37 -0700163 void startPersisting() {
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800164 if (!mLazyTaskWriterThread.isAlive()) {
165 mLazyTaskWriterThread.start();
166 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700167 }
168
Craig Mautner63f10902014-09-16 23:57:21 -0700169 private void removeThumbnails(TaskRecord task) {
170 final String taskString = Integer.toString(task.taskId);
171 for (int queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
172 final WriteQueueItem item = mWriteQueue.get(queueNdx);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800173 if (item instanceof ImageWriteQueueItem) {
174 final File thumbnailFile = new File(((ImageWriteQueueItem) item).mFilePath);
175 if (thumbnailFile.getName().startsWith(taskString)) {
176 if (DEBUG) {
177 Slog.d(TAG, "Removing " + ((ImageWriteQueueItem) item).mFilePath +
178 " from write queue");
179 }
180 mWriteQueue.remove(queueNdx);
181 }
Craig Mautner63f10902014-09-16 23:57:21 -0700182 }
183 }
184 }
185
186 private void yieldIfQueueTooDeep() {
187 boolean stall = false;
188 synchronized (this) {
189 if (mNextWriteTime == FLUSH_QUEUE) {
190 stall = true;
191 }
192 }
193 if (stall) {
194 Thread.yield();
195 }
196 }
197
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800198 @NonNull
199 SparseBooleanArray loadPersistedTaskIdsForUser(int userId) {
200 if (mTaskIdsInFile.get(userId) != null) {
201 return mTaskIdsInFile.get(userId).clone();
202 }
203 final SparseBooleanArray persistedTaskIds = new SparseBooleanArray();
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700204 synchronized (mIoLock) {
205 BufferedReader reader = null;
206 String line;
207 try {
208 reader = new BufferedReader(new FileReader(getUserPersistedTaskIdsFile(userId)));
209 while ((line = reader.readLine()) != null) {
210 for (String taskIdString : line.split("\\s+")) {
211 int id = Integer.parseInt(taskIdString);
212 persistedTaskIds.put(id, true);
213 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800214 }
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700215 } catch (FileNotFoundException e) {
216 // File doesn't exist. Ignore.
217 } catch (Exception e) {
218 Slog.e(TAG, "Error while reading taskIds file for user " + userId, e);
219 } finally {
220 IoUtils.closeQuietly(reader);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800221 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800222 }
223 mTaskIdsInFile.put(userId, persistedTaskIds);
224 return persistedTaskIds.clone();
225 }
226
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700227
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800228 @VisibleForTesting
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700229 void writePersistedTaskIdsForUser(@NonNull SparseBooleanArray taskIds, int userId) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800230 if (userId < 0) {
231 return;
232 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800233 final File persistedTaskIdsFile = getUserPersistedTaskIdsFile(userId);
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700234 synchronized (mIoLock) {
235 BufferedWriter writer = null;
236 try {
237 writer = new BufferedWriter(new FileWriter(persistedTaskIdsFile));
238 for (int i = 0; i < taskIds.size(); i++) {
239 if (taskIds.valueAt(i)) {
240 writer.write(String.valueOf(taskIds.keyAt(i)));
241 writer.newLine();
242 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800243 }
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700244 } catch (Exception e) {
245 Slog.e(TAG, "Error while writing taskIds file for user " + userId, e);
246 } finally {
247 IoUtils.closeQuietly(writer);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800248 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800249 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800250 }
251
252 void unloadUserDataFromMemory(int userId) {
253 mTaskIdsInFile.delete(userId);
254 }
255
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700256 void wakeup(TaskRecord task, boolean flush) {
Craig Mautner21d24a22014-04-23 11:45:37 -0700257 synchronized (this) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700258 if (task != null) {
259 int queueNdx;
260 for (queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
261 final WriteQueueItem item = mWriteQueue.get(queueNdx);
262 if (item instanceof TaskWriteQueueItem &&
263 ((TaskWriteQueueItem) item).mTask == task) {
Craig Mautner63f10902014-09-16 23:57:21 -0700264 if (!task.inRecents) {
265 // This task is being removed.
266 removeThumbnails(task);
267 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700268 break;
269 }
270 }
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700271 if (queueNdx < 0 && task.isPersistable) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700272 mWriteQueue.add(new TaskWriteQueueItem(task));
273 }
274 } else {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800275 // Dummy. Ensures removeObsoleteFiles is called when LazyTaskThreadWriter is
276 // notified.
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700277 mWriteQueue.add(new WriteQueueItem());
278 }
Craig Mautner63f10902014-09-16 23:57:21 -0700279 if (flush || mWriteQueue.size() > MAX_WRITE_QUEUE_LENGTH) {
280 mNextWriteTime = FLUSH_QUEUE;
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700281 } else if (mNextWriteTime == 0) {
282 mNextWriteTime = SystemClock.uptimeMillis() + PRE_TASK_DELAY_MS;
283 }
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700284 if (DEBUG) Slog.d(TAG, "wakeup: task=" + task + " flush=" + flush + " mNextWriteTime="
285 + mNextWriteTime + " mWriteQueue.size=" + mWriteQueue.size()
286 + " Callers=" + Debug.getCallers(4));
Craig Mautner21d24a22014-04-23 11:45:37 -0700287 notifyAll();
288 }
Craig Mautner63f10902014-09-16 23:57:21 -0700289
290 yieldIfQueueTooDeep();
Craig Mautner21d24a22014-04-23 11:45:37 -0700291 }
292
Dianne Hackbornce0fd762014-09-19 12:58:15 -0700293 void flush() {
294 synchronized (this) {
295 mNextWriteTime = FLUSH_QUEUE;
296 notifyAll();
297 do {
298 try {
299 wait();
300 } catch (InterruptedException e) {
301 }
302 } while (mNextWriteTime == FLUSH_QUEUE);
303 }
304 }
305
Suprabh Shukla23593142015-11-03 17:31:15 -0800306 void saveImage(Bitmap image, String filePath) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700307 synchronized (this) {
308 int queueNdx;
309 for (queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
310 final WriteQueueItem item = mWriteQueue.get(queueNdx);
311 if (item instanceof ImageWriteQueueItem) {
312 ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
Suprabh Shukla23593142015-11-03 17:31:15 -0800313 if (imageWriteQueueItem.mFilePath.equals(filePath)) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700314 // replace the Bitmap with the new one.
315 imageWriteQueueItem.mImage = image;
316 break;
317 }
318 }
319 }
320 if (queueNdx < 0) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800321 mWriteQueue.add(new ImageWriteQueueItem(filePath, image));
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700322 }
Craig Mautner63f10902014-09-16 23:57:21 -0700323 if (mWriteQueue.size() > MAX_WRITE_QUEUE_LENGTH) {
324 mNextWriteTime = FLUSH_QUEUE;
325 } else if (mNextWriteTime == 0) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700326 mNextWriteTime = SystemClock.uptimeMillis() + PRE_TASK_DELAY_MS;
327 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800328 if (DEBUG) Slog.d(TAG, "saveImage: filePath=" + filePath + " now=" +
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700329 SystemClock.uptimeMillis() + " mNextWriteTime=" +
330 mNextWriteTime + " Callers=" + Debug.getCallers(4));
331 notifyAll();
332 }
Craig Mautner63f10902014-09-16 23:57:21 -0700333
334 yieldIfQueueTooDeep();
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700335 }
336
Suprabh Shukla23593142015-11-03 17:31:15 -0800337 Bitmap getTaskDescriptionIcon(String filePath) {
Craig Mautner648f69b2014-09-18 14:16:26 -0700338 // See if it is in the write queue
Suprabh Shukla23593142015-11-03 17:31:15 -0800339 final Bitmap icon = getImageFromWriteQueue(filePath);
Craig Mautner648f69b2014-09-18 14:16:26 -0700340 if (icon != null) {
341 return icon;
342 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800343 return restoreImage(filePath);
Craig Mautner648f69b2014-09-18 14:16:26 -0700344 }
345
Suprabh Shukla23593142015-11-03 17:31:15 -0800346 Bitmap getImageFromWriteQueue(String filePath) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700347 synchronized (this) {
348 for (int queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
349 final WriteQueueItem item = mWriteQueue.get(queueNdx);
350 if (item instanceof ImageWriteQueueItem) {
351 ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
Suprabh Shukla23593142015-11-03 17:31:15 -0800352 if (imageWriteQueueItem.mFilePath.equals(filePath)) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700353 return imageWriteQueueItem.mImage;
354 }
355 }
356 }
357 return null;
358 }
359 }
360
Craig Mautner21d24a22014-04-23 11:45:37 -0700361 private StringWriter saveToXml(TaskRecord task) throws IOException, XmlPullParserException {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700362 if (DEBUG) Slog.d(TAG, "saveToXml: task=" + task);
Craig Mautner21d24a22014-04-23 11:45:37 -0700363 final XmlSerializer xmlSerializer = new FastXmlSerializer();
364 StringWriter stringWriter = new StringWriter();
365 xmlSerializer.setOutput(stringWriter);
366
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700367 if (DEBUG) xmlSerializer.setFeature(
Suprabh Shukla23593142015-11-03 17:31:15 -0800368 "http://xmlpull.org/v1/doc/features.html#indent-output", true);
Craig Mautner21d24a22014-04-23 11:45:37 -0700369
370 // save task
371 xmlSerializer.startDocument(null, true);
372
373 xmlSerializer.startTag(null, TAG_TASK);
374 task.saveToXml(xmlSerializer);
375 xmlSerializer.endTag(null, TAG_TASK);
376
377 xmlSerializer.endDocument();
378 xmlSerializer.flush();
379
380 return stringWriter;
381 }
382
Craig Mautner77b04262014-06-27 15:22:12 -0700383 private String fileToString(File file) {
384 final String newline = System.lineSeparator();
385 try {
386 BufferedReader reader = new BufferedReader(new FileReader(file));
387 StringBuffer sb = new StringBuffer((int) file.length() * 2);
388 String line;
389 while ((line = reader.readLine()) != null) {
390 sb.append(line + newline);
391 }
392 reader.close();
393 return sb.toString();
394 } catch (IOException ioe) {
395 Slog.e(TAG, "Couldn't read file " + file.getName());
396 return null;
397 }
398 }
399
Craig Mautnera228ae92014-07-09 05:44:55 -0700400 private TaskRecord taskIdToTask(int taskId, ArrayList<TaskRecord> tasks) {
401 if (taskId < 0) {
402 return null;
403 }
404 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
405 final TaskRecord task = tasks.get(taskNdx);
406 if (task.taskId == taskId) {
407 return task;
408 }
409 }
410 Slog.e(TAG, "Restore affiliation error looking for taskId=" + taskId);
411 return null;
412 }
413
Daichi Hirono4cb941e2017-03-31 14:30:41 +0900414 List<TaskRecord> restoreTasksForUserLocked(final int userId, SparseBooleanArray preaddedTasks) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800415 final ArrayList<TaskRecord> tasks = new ArrayList<TaskRecord>();
Craig Mautner21d24a22014-04-23 11:45:37 -0700416 ArraySet<Integer> recoveredTaskIds = new ArraySet<Integer>();
417
Suprabh Shukla23593142015-11-03 17:31:15 -0800418 File userTasksDir = getUserTasksDir(userId);
Winson Chung36f3f032016-09-08 23:29:43 +0000419
Suprabh Shukla23593142015-11-03 17:31:15 -0800420 File[] recentFiles = userTasksDir.listFiles();
Craig Mautner21d24a22014-04-23 11:45:37 -0700421 if (recentFiles == null) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800422 Slog.e(TAG, "restoreTasksForUserLocked: Unable to list files from " + userTasksDir);
Craig Mautner21d24a22014-04-23 11:45:37 -0700423 return tasks;
424 }
425
426 for (int taskNdx = 0; taskNdx < recentFiles.length; ++taskNdx) {
427 File taskFile = recentFiles[taskNdx];
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800428 if (DEBUG) {
429 Slog.d(TAG, "restoreTasksForUserLocked: userId=" + userId
430 + ", taskFile=" + taskFile.getName());
431 }
Daichi Hirono4cb941e2017-03-31 14:30:41 +0900432
433 if (!taskFile.getName().endsWith(TASK_FILENAME_SUFFIX)) {
434 continue;
435 }
436 try {
437 final int taskId = Integer.parseInt(taskFile.getName().substring(
438 0 /* beginIndex */,
439 taskFile.getName().length() - TASK_FILENAME_SUFFIX.length()));
440 if (preaddedTasks.get(taskId, false)) {
441 Slog.w(TAG, "Task #" + taskId +
442 " has already been created so we don't restore again");
443 continue;
444 }
445 } catch (NumberFormatException e) {
446 Slog.w(TAG, "Unexpected task file name", e);
447 continue;
448 }
449
Craig Mautner21d24a22014-04-23 11:45:37 -0700450 BufferedReader reader = null;
Craig Mautnere0129b32014-05-25 16:41:09 -0700451 boolean deleteFile = false;
Craig Mautner21d24a22014-04-23 11:45:37 -0700452 try {
453 reader = new BufferedReader(new FileReader(taskFile));
454 final XmlPullParser in = Xml.newPullParser();
455 in.setInput(reader);
456
457 int event;
458 while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
459 event != XmlPullParser.END_TAG) {
460 final String name = in.getName();
461 if (event == XmlPullParser.START_TAG) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800462 if (DEBUG) Slog.d(TAG, "restoreTasksForUserLocked: START_TAG name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -0700463 if (TAG_TASK.equals(name)) {
Winson Chung36f3f032016-09-08 23:29:43 +0000464 final TaskRecord task = TaskRecord.restoreFromXml(in, mStackSupervisor);
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800465 if (DEBUG) Slog.d(TAG, "restoreTasksForUserLocked: restored task="
Suprabh Shukla23593142015-11-03 17:31:15 -0800466 + task);
Craig Mautner21d24a22014-04-23 11:45:37 -0700467 if (task != null) {
Winson Chung36f3f032016-09-08 23:29:43 +0000468 // XXX Don't add to write queue... there is no reason to write
469 // out the stuff we just read, if we don't write it we will
470 // read the same thing again.
471 // mWriteQueue.add(new TaskWriteQueueItem(task));
472
Craig Mautner21d24a22014-04-23 11:45:37 -0700473 final int taskId = task.taskId;
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800474 if (mStackSupervisor.anyTaskForIdLocked(taskId,
Winson Chung7900bee2017-03-10 08:59:25 -0800475 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
476 INVALID_STACK_ID) != null) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800477 // Should not happen.
478 Slog.wtf(TAG, "Existing task with taskId " + taskId + "found");
479 } else if (userId != task.userId) {
480 // Should not happen.
481 Slog.wtf(TAG, "Task with userId " + task.userId + " found in "
482 + userTasksDir.getAbsolutePath());
483 } else {
484 // Looks fine.
485 mStackSupervisor.setNextTaskIdForUserLocked(taskId, userId);
Amith Yamasani515d4062015-09-28 11:30:06 -0700486 task.isPersistable = true;
487 tasks.add(task);
488 recoveredTaskIds.add(taskId);
489 }
Craig Mautner77b04262014-06-27 15:22:12 -0700490 } else {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800491 Slog.e(TAG, "restoreTasksForUserLocked: Unable to restore taskFile="
Suprabh Shukla23593142015-11-03 17:31:15 -0800492 + taskFile + ": " + fileToString(taskFile));
Craig Mautner21d24a22014-04-23 11:45:37 -0700493 }
494 } else {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800495 Slog.wtf(TAG, "restoreTasksForUserLocked: Unknown xml event=" + event
Suprabh Shukla23593142015-11-03 17:31:15 -0800496 + " name=" + name);
Craig Mautner21d24a22014-04-23 11:45:37 -0700497 }
498 }
499 XmlUtils.skipCurrentTag(in);
500 }
Craig Mautnere0129b32014-05-25 16:41:09 -0700501 } catch (Exception e) {
Craig Mautnera228ae92014-07-09 05:44:55 -0700502 Slog.wtf(TAG, "Unable to parse " + taskFile + ". Error ", e);
Craig Mautner77b04262014-06-27 15:22:12 -0700503 Slog.e(TAG, "Failing file: " + fileToString(taskFile));
Craig Mautnere0129b32014-05-25 16:41:09 -0700504 deleteFile = true;
Craig Mautner21d24a22014-04-23 11:45:37 -0700505 } finally {
Wale Ogunwale18795a22014-12-03 11:38:33 -0800506 IoUtils.closeQuietly(reader);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700507 if (deleteFile) {
508 if (DEBUG) Slog.d(TAG, "Deleting file=" + taskFile.getName());
Craig Mautnere0129b32014-05-25 16:41:09 -0700509 taskFile.delete();
510 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700511 }
512 }
513
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700514 if (!DEBUG) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800515 removeObsoleteFiles(recoveredTaskIds, userTasksDir.listFiles());
516 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700517
Suprabh Shukla23593142015-11-03 17:31:15 -0800518 // Fix up task affiliation from taskIds
Craig Mautnera228ae92014-07-09 05:44:55 -0700519 for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
520 final TaskRecord task = tasks.get(taskNdx);
521 task.setPrevAffiliate(taskIdToTask(task.mPrevAffiliateTaskId, tasks));
522 task.setNextAffiliate(taskIdToTask(task.mNextAffiliateTaskId, tasks));
523 }
524
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800525 Collections.sort(tasks, new Comparator<TaskRecord>() {
Craig Mautner21d24a22014-04-23 11:45:37 -0700526 @Override
527 public int compare(TaskRecord lhs, TaskRecord rhs) {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700528 final long diff = rhs.mLastTimeMoved - lhs.mLastTimeMoved;
Craig Mautner21d24a22014-04-23 11:45:37 -0700529 if (diff < 0) {
530 return -1;
531 } else if (diff > 0) {
532 return +1;
533 } else {
534 return 0;
535 }
536 }
537 });
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800538 return tasks;
Craig Mautner21d24a22014-04-23 11:45:37 -0700539 }
540
Craig Mautnere0129b32014-05-25 16:41:09 -0700541 private static void removeObsoleteFiles(ArraySet<Integer> persistentTaskIds, File[] files) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800542 if (DEBUG) Slog.d(TAG, "removeObsoleteFiles: persistentTaskIds=" + persistentTaskIds +
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700543 " files=" + files);
Craig Mautnera5badf02014-09-11 12:47:03 -0700544 if (files == null) {
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700545 Slog.e(TAG, "File error accessing recents directory (directory doesn't exist?).");
Craig Mautnera5badf02014-09-11 12:47:03 -0700546 return;
547 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700548 for (int fileNdx = 0; fileNdx < files.length; ++fileNdx) {
549 File file = files[fileNdx];
550 String filename = file.getName();
Craig Mautnerffcfcaa2014-06-05 09:54:38 -0700551 final int taskIdEnd = filename.indexOf('_');
Craig Mautner21d24a22014-04-23 11:45:37 -0700552 if (taskIdEnd > 0) {
553 final int taskId;
554 try {
Narayan Kamatha09b4d22016-04-15 18:32:45 +0100555 taskId = Integer.parseInt(filename.substring(0, taskIdEnd));
Suprabh Shukla23593142015-11-03 17:31:15 -0800556 if (DEBUG) Slog.d(TAG, "removeObsoleteFiles: Found taskId=" + taskId);
Craig Mautner21d24a22014-04-23 11:45:37 -0700557 } catch (Exception e) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800558 Slog.wtf(TAG, "removeObsoleteFiles: Can't parse file=" + file.getName());
Craig Mautner21d24a22014-04-23 11:45:37 -0700559 file.delete();
560 continue;
561 }
562 if (!persistentTaskIds.contains(taskId)) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800563 if (DEBUG) Slog.d(TAG, "removeObsoleteFiles: deleting file=" + file.getName());
Craig Mautner21d24a22014-04-23 11:45:37 -0700564 file.delete();
565 }
566 }
567 }
568 }
569
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800570 private void writeTaskIdsFiles() {
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700571 SparseArray<SparseBooleanArray> changedTaskIdsPerUser = new SparseArray<>();
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800572 synchronized (mService) {
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700573 for (int userId : mRecentTasks.usersWithRecentsLoadedLocked()) {
574 SparseBooleanArray taskIdsToSave = mRecentTasks.mPersistedTaskIds.get(userId);
575 SparseBooleanArray persistedIdsInFile = mTaskIdsInFile.get(userId);
576 if (persistedIdsInFile != null && persistedIdsInFile.equals(taskIdsToSave)) {
577 continue;
578 } else {
579 SparseBooleanArray taskIdsToSaveCopy = taskIdsToSave.clone();
580 mTaskIdsInFile.put(userId, taskIdsToSaveCopy);
581 changedTaskIdsPerUser.put(userId, taskIdsToSaveCopy);
582 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800583 }
Suprabh Shuklafd0bd4f2016-08-24 14:08:29 -0700584 }
585 for (int i = 0; i < changedTaskIdsPerUser.size(); i++) {
586 writePersistedTaskIdsForUser(changedTaskIdsPerUser.valueAt(i),
587 changedTaskIdsPerUser.keyAt(i));
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800588 }
589 }
590
Craig Mautner21d24a22014-04-23 11:45:37 -0700591 private void removeObsoleteFiles(ArraySet<Integer> persistentTaskIds) {
Suprabh Shukla09a88f52015-12-02 14:36:31 -0800592 int[] candidateUserIds;
593 synchronized (mService) {
594 // Remove only from directories of the users who have recents in memory synchronized
595 // with persistent storage.
596 candidateUserIds = mRecentTasks.usersWithRecentsLoadedLocked();
597 }
598 for (int userId : candidateUserIds) {
Suprabh Shukla23593142015-11-03 17:31:15 -0800599 removeObsoleteFiles(persistentTaskIds, getUserImagesDir(userId).listFiles());
600 removeObsoleteFiles(persistentTaskIds, getUserTasksDir(userId).listFiles());
601 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700602 }
603
604 static Bitmap restoreImage(String filename) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700605 if (DEBUG) Slog.d(TAG, "restoreImage: restoring " + filename);
Suprabh Shukla23593142015-11-03 17:31:15 -0800606 return BitmapFactory.decodeFile(filename);
607 }
608
Suprabh Shuklaf50b4582016-02-23 17:44:22 -0800609 private File getUserPersistedTaskIdsFile(int userId) {
610 File userTaskIdsDir = new File(mTaskIdsDir, String.valueOf(userId));
611 if (!userTaskIdsDir.exists() && !userTaskIdsDir.mkdirs()) {
612 Slog.e(TAG, "Error while creating user directory: " + userTaskIdsDir);
613 }
614 return new File(userTaskIdsDir, PERSISTED_TASK_IDS_FILENAME);
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800615 }
616
Suprabh Shukla23593142015-11-03 17:31:15 -0800617 static File getUserTasksDir(int userId) {
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800618 File userTasksDir = new File(Environment.getDataSystemCeDirectory(userId), TASKS_DIRNAME);
Suprabh Shukla23593142015-11-03 17:31:15 -0800619
620 if (!userTasksDir.exists()) {
621 if (!userTasksDir.mkdir()) {
622 Slog.e(TAG, "Failure creating tasks directory for user " + userId + ": "
623 + userTasksDir);
624 }
625 }
626 return userTasksDir;
627 }
628
629 static File getUserImagesDir(int userId) {
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700630 return new File(Environment.getDataSystemCeDirectory(userId), IMAGES_DIRNAME);
631 }
Suprabh Shukla23593142015-11-03 17:31:15 -0800632
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700633 private static boolean createParentDirectory(String filePath) {
634 File parentDir = new File(filePath).getParentFile();
635 return parentDir.exists() || parentDir.mkdirs();
Craig Mautner21d24a22014-04-23 11:45:37 -0700636 }
637
638 private class LazyTaskWriterThread extends Thread {
Craig Mautner21d24a22014-04-23 11:45:37 -0700639
640 LazyTaskWriterThread(String name) {
641 super(name);
642 }
643
644 @Override
645 public void run() {
Riley Andrewsf16c2e82015-06-02 18:24:48 -0700646 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Craig Mautner21d24a22014-04-23 11:45:37 -0700647 ArraySet<Integer> persistentTaskIds = new ArraySet<Integer>();
648 while (true) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700649 // We can't lock mService while holding TaskPersister.this, but we don't want to
650 // call removeObsoleteFiles every time through the loop, only the last time before
651 // going to sleep. The risk is that we call removeObsoleteFiles() successively.
652 final boolean probablyDone;
Craig Mautner21d24a22014-04-23 11:45:37 -0700653 synchronized (TaskPersister.this) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700654 probablyDone = mWriteQueue.isEmpty();
655 }
656 if (probablyDone) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700657 if (DEBUG) Slog.d(TAG, "Looking for obsolete files.");
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700658 persistentTaskIds.clear();
659 synchronized (mService) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700660 if (DEBUG) Slog.d(TAG, "mRecents=" + mRecentTasks);
Wale Ogunwalec82f2f52014-12-09 09:32:50 -0800661 for (int taskNdx = mRecentTasks.size() - 1; taskNdx >= 0; --taskNdx) {
662 final TaskRecord task = mRecentTasks.get(taskNdx);
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700663 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: task=" + task +
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700664 " persistable=" + task.isPersistable);
Andrii Kulian02b7a832016-10-06 23:11:56 -0700665 final ActivityStack stack = task.getStack();
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700666 if ((task.isPersistable || task.inRecents)
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800667 && (stack == null || !stack.isHomeOrRecentsStack())) {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700668 if (DEBUG) Slog.d(TAG, "adding to persistentTaskIds task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700669 persistentTaskIds.add(task.taskId);
670 } else {
Stefan Kuhnee88d1e52015-05-18 10:33:45 -0700671 if (DEBUG) Slog.d(TAG,
Wale Ogunwalebe23ff42014-10-21 16:29:51 -0700672 "omitting from persistentTaskIds task=" + task);
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700673 }
674 }
Jorim Jaggif9084ec2017-01-16 13:16:59 +0100675 mService.mWindowManager.removeObsoleteTaskFiles(persistentTaskIds,
676 mRecentTasks.usersWithRecentsLoadedLocked());
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700677 }
678 removeObsoleteFiles(persistentTaskIds);
679 }
Suprabh Shukla4bccb462016-02-10 18:45:12 -0800680 writeTaskIdsFiles();
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700681
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700682 processNextItem();
683 }
684 }
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700685
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700686 private void processNextItem() {
687 // This part is extracted into a method so that the GC can clearly see the end of the
688 // scope of the variable 'item'. If this part was in the loop above, the last item
689 // it processed would always "leak".
690 // See https://b.corp.google.com/issues/64438652#comment7
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700691
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700692 // If mNextWriteTime, then don't delay between each call to saveToXml().
693 final WriteQueueItem item;
694 synchronized (TaskPersister.this) {
695 if (mNextWriteTime != FLUSH_QUEUE) {
696 // The next write we don't have to wait so long.
697 mNextWriteTime = SystemClock.uptimeMillis() + INTER_WRITE_DELAY_MS;
698 if (DEBUG) Slog.d(TAG, "Next write time may be in " +
699 INTER_WRITE_DELAY_MS + " msec. (" + mNextWriteTime + ")");
Craig Mautner21d24a22014-04-23 11:45:37 -0700700 }
701
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700702 while (mWriteQueue.isEmpty()) {
703 if (mNextWriteTime != 0) {
704 mNextWriteTime = 0; // idle.
705 TaskPersister.this.notifyAll(); // wake up flush() if needed.
Suprabh Shukladc4b80d2016-04-20 15:24:31 -0700706 }
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700707 try {
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700708 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: waiting indefinitely.");
709 TaskPersister.this.wait();
710 } catch (InterruptedException e) {
Craig Mautnerc0ffce52014-07-01 12:38:52 -0700711 }
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700712 // Invariant: mNextWriteTime is either FLUSH_QUEUE or PRE_WRITE_DELAY_MS
713 // from now.
714 }
715 item = mWriteQueue.remove(0);
716
717 long now = SystemClock.uptimeMillis();
718 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: now=" + now + " mNextWriteTime=" +
719 mNextWriteTime + " mWriteQueue.size=" + mWriteQueue.size());
720 while (now < mNextWriteTime) {
721 try {
722 if (DEBUG) Slog.d(TAG, "LazyTaskWriter: waiting " +
723 (mNextWriteTime - now));
724 TaskPersister.this.wait(mNextWriteTime - now);
725 } catch (InterruptedException e) {
Dianne Hackborn852975d2014-08-22 17:42:43 -0700726 }
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700727 now = SystemClock.uptimeMillis();
728 }
729
730 // Got something to do.
731 }
732
733 if (item instanceof ImageWriteQueueItem) {
734 ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
735 final String filePath = imageWriteQueueItem.mFilePath;
736 if (!createParentDirectory(filePath)) {
737 Slog.e(TAG, "Error while creating images directory for file: " + filePath);
738 return;
739 }
740 final Bitmap bitmap = imageWriteQueueItem.mImage;
741 if (DEBUG) Slog.d(TAG, "writing bitmap: filename=" + filePath);
742 FileOutputStream imageFile = null;
743 try {
744 imageFile = new FileOutputStream(new File(filePath));
745 bitmap.compress(Bitmap.CompressFormat.PNG, 100, imageFile);
746 } catch (Exception e) {
747 Slog.e(TAG, "saveImage: unable to save " + filePath, e);
748 } finally {
749 IoUtils.closeQuietly(imageFile);
750 }
751 } else if (item instanceof TaskWriteQueueItem) {
752 // Write out one task.
753 StringWriter stringWriter = null;
754 TaskRecord task = ((TaskWriteQueueItem) item).mTask;
755 if (DEBUG) Slog.d(TAG, "Writing task=" + task);
756 synchronized (mService) {
757 if (task.inRecents) {
758 // Still there.
Dianne Hackborn852975d2014-08-22 17:42:43 -0700759 try {
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700760 if (DEBUG) Slog.d(TAG, "Saving task=" + task);
761 stringWriter = saveToXml(task);
Dianne Hackborn852975d2014-08-22 17:42:43 -0700762 } catch (IOException e) {
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700763 } catch (XmlPullParserException e) {
Craig Mautnerf4f8bb72014-07-29 10:41:40 -0700764 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700765 }
766 }
Makoto Onuki62a93aa2017-08-08 10:24:59 -0700767 if (stringWriter != null) {
768 // Write out xml file while not holding mService lock.
769 FileOutputStream file = null;
770 AtomicFile atomicFile = null;
771 try {
772 atomicFile = new AtomicFile(new File(
773 getUserTasksDir(task.userId),
774 String.valueOf(task.taskId) + TASK_FILENAME_SUFFIX));
775 file = atomicFile.startWrite();
776 file.write(stringWriter.toString().getBytes());
777 file.write('\n');
778 atomicFile.finishWrite(file);
779 } catch (IOException e) {
780 if (file != null) {
781 atomicFile.failWrite(file);
782 }
783 Slog.e(TAG,
784 "Unable to open " + atomicFile + " for persisting. " + e);
785 }
786 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700787 }
788 }
789 }
Craig Mautner21d24a22014-04-23 11:45:37 -0700790}