Fix issue #17179314: Make recents limits consistent

The max limit is now 100 (or 50 on svelte devices), and that is
what everyone used.

Re-arranged things so we have a big expensive "fix the world!"
function for recents that we go in to at only select points:
when first initializing the system, when external storage comes
and goes, and if we detect something wrong with the recents
structure.

With that, now getRecentTasks() and addRecentTaskLocked() are
generally much simpler, doing very little work in most cases.
This will help a lot with scaling up to many more recents
entries.

Change-Id: I7b5ae89edc06568f68c8af54a4420aff7635581c
diff --git a/services/core/java/com/android/server/am/TaskPersister.java b/services/core/java/com/android/server/am/TaskPersister.java
index 0180db3..3cc406b 100644
--- a/services/core/java/com/android/server/am/TaskPersister.java
+++ b/services/core/java/com/android/server/am/TaskPersister.java
@@ -273,11 +273,14 @@
                         if (TAG_TASK.equals(name)) {
                             final TaskRecord task =
                                     TaskRecord.restoreFromXml(in, mStackSupervisor);
-                            if (true || DEBUG) Slog.d(TAG, "restoreTasksLocked: restored task=" +
+                            if (DEBUG) Slog.d(TAG, "restoreTasksLocked: restored task=" +
                                     task);
                             if (task != null) {
                                 task.isPersistable = true;
-                                mWriteQueue.add(new TaskWriteQueueItem(task));
+                                // XXX Don't add to write queue... there is no reason to write
+                                // out the stuff we just read, if we don't write it we will
+                                // read the same thing again.
+                                //mWriteQueue.add(new TaskWriteQueueItem(task));
                                 tasks.add(task);
                                 final int taskId = task.taskId;
                                 recoveredTaskIds.add(taskId);
@@ -486,24 +489,24 @@
                             } catch (XmlPullParserException e) {
                             }
                         }
-                        if (stringWriter != null) {
-                            // Write out xml file while not holding mService lock.
-                            FileOutputStream file = null;
-                            AtomicFile atomicFile = null;
-                            try {
-                                atomicFile = new AtomicFile(new File(sTasksDir, String.valueOf(
-                                        task.taskId) + RECENTS_FILENAME + TASK_EXTENSION));
-                                file = atomicFile.startWrite();
-                                file.write(stringWriter.toString().getBytes());
-                                file.write('\n');
-                                atomicFile.finishWrite(file);
-                            } catch (IOException e) {
-                                if (file != null) {
-                                    atomicFile.failWrite(file);
-                                }
-                                Slog.e(TAG, "Unable to open " + atomicFile + " for persisting. " +
-                                        e);
+                    }
+                    if (stringWriter != null) {
+                        // Write out xml file while not holding mService lock.
+                        FileOutputStream file = null;
+                        AtomicFile atomicFile = null;
+                        try {
+                            atomicFile = new AtomicFile(new File(sTasksDir, String.valueOf(
+                                    task.taskId) + RECENTS_FILENAME + TASK_EXTENSION));
+                            file = atomicFile.startWrite();
+                            file.write(stringWriter.toString().getBytes());
+                            file.write('\n');
+                            atomicFile.finishWrite(file);
+                        } catch (IOException e) {
+                            if (file != null) {
+                                atomicFile.failWrite(file);
                             }
+                            Slog.e(TAG, "Unable to open " + atomicFile + " for persisting. " +
+                                    e);
                         }
                     }
                 }