Don't touch system_ce until the first user unlock.

This CL does the following things:
1) Only start PersistQueue when the first user unlocked.
2) Only create recent task folder when we need to write the first file
to it.

Bug: 124697664
Test: Manual test -- checked recent_tasks, recent_images and
launch_params folders are still created correctly. No encryption policy
log seen.
Change-Id: I5b8bf712a3c45092b463622731d58f90d76fec58
diff --git a/services/core/java/com/android/server/wm/ActivityStackSupervisor.java b/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
index a8e8c7c5..6e7cba8 100644
--- a/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
@@ -450,10 +450,17 @@
     }
 
     void onSystemReady() {
-        mPersisterQueue.startPersisting();
         mLaunchParamsPersister.onSystemReady();
     }
 
+    void onUserUnlocked(int userId) {
+        // Only start persisting when the first user is unlocked. The method call is
+        // idempotent so there is no side effect to call it again when the second user is
+        // unlocked.
+        mPersisterQueue.startPersisting();
+        mLaunchParamsPersister.onUnlockUser(userId);
+    }
+
     public ActivityMetricsLogger getActivityMetricsLogger() {
         return mActivityMetricsLogger;
     }
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
index 75f299c..bda52b9 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@@ -948,7 +948,7 @@
         @Override
         public void onUnlockUser(int userId) {
             synchronized (mService.getGlobalLock()) {
-                mService.mStackSupervisor.mLaunchParamsPersister.onUnlockUser(userId);
+                mService.mStackSupervisor.onUserUnlocked(userId);
             }
         }
 
diff --git a/services/core/java/com/android/server/wm/PersisterQueue.java b/services/core/java/com/android/server/wm/PersisterQueue.java
index a17ee65..9dc3d6a 100644
--- a/services/core/java/com/android/server/wm/PersisterQueue.java
+++ b/services/core/java/com/android/server/wm/PersisterQueue.java
@@ -131,10 +131,8 @@
     }
 
     /**
-     *
-     * @param item
-     * @param flush
-     * @param <T>
+     * Updates the last item found in the queue that matches the given item, or adds it to the end
+     * of the queue if no such item is found.
      */
     synchronized <T extends WriteQueueItem> void updateLastOrAddItem(T item, boolean flush) {
         final T itemToUpdate = findLastItem(item::matches, (Class<T>) item.getClass());
@@ -149,10 +147,6 @@
 
     /**
      * Removes all items with which given predicate returns {@code true}.
-     *
-     * @param predicate the predicate
-     * @param clazz
-     * @param <T>
      */
     synchronized <T extends WriteQueueItem> void removeItems(Predicate<T> predicate,
             Class<T> clazz) {
diff --git a/services/core/java/com/android/server/wm/TaskPersister.java b/services/core/java/com/android/server/wm/TaskPersister.java
index d50af38..06bdcc0 100644
--- a/services/core/java/com/android/server/wm/TaskPersister.java
+++ b/services/core/java/com/android/server/wm/TaskPersister.java
@@ -492,16 +492,8 @@
         return new File(userTaskIdsDir, PERSISTED_TASK_IDS_FILENAME);
     }
 
-    static File getUserTasksDir(int userId) {
-        File userTasksDir = new File(Environment.getDataSystemCeDirectory(userId), TASKS_DIRNAME);
-
-        if (!userTasksDir.exists()) {
-            if (!userTasksDir.mkdir()) {
-                Slog.e(TAG, "Failure creating tasks directory for user " + userId + ": "
-                        + userTasksDir);
-            }
-        }
-        return userTasksDir;
+    private static File getUserTasksDir(int userId) {
+        return new File(Environment.getDataSystemCeDirectory(userId), TASKS_DIRNAME);
     }
 
     static File getUserImagesDir(int userId) {
@@ -568,8 +560,13 @@
                 FileOutputStream file = null;
                 AtomicFile atomicFile = null;
                 try {
-                    atomicFile = new AtomicFile(new File(
-                            getUserTasksDir(task.userId),
+                    File userTasksDir = getUserTasksDir(task.userId);
+                    if (!userTasksDir.isDirectory() && !userTasksDir.mkdirs()) {
+                        Slog.e(TAG, "Failure creating tasks directory for user " + task.userId
+                                + ": " + userTasksDir + " Dropping persistence for task " + task);
+                        return;
+                    }
+                    atomicFile = new AtomicFile(new File(userTasksDir,
                             String.valueOf(task.taskId) + TASK_FILENAME_SUFFIX));
                     file = atomicFile.startWrite();
                     file.write(stringWriter.toString().getBytes());