Implement restoring & correct caching of snapshots

Introduce a retrieval cache that holds the last accessed
snapshots, in addition to the cache of the activities
that are the top most activtiy of a task that have a
running process.

Change everything to use an integer id instead of a Task object
to work around the issue that some tasks SystemUI might access
might not exist in WM yet (not yet restored from recents).

Don't put anything in the cache on the SystemUI side, but still
retrieve the thumbnails after a task changed event to make sure
the cache on the system_server side is fresh.

Test: runtest frameworks-services -c
com.android.server.wm.TaskSnapshotCacheTest

Bug: 31339431
Change-Id: I8e56365459a677735320adaa169da8fb033ceab0
diff --git a/services/core/java/com/android/server/wm/TaskSnapshotController.java b/services/core/java/com/android/server/wm/TaskSnapshotController.java
index 10ecf3b..15878f6 100644
--- a/services/core/java/com/android/server/wm/TaskSnapshotController.java
+++ b/services/core/java/com/android/server/wm/TaskSnapshotController.java
@@ -28,7 +28,6 @@
 
 import com.android.internal.annotations.VisibleForTesting;
 
-import java.io.File;
 import java.io.PrintWriter;
 
 /**
@@ -48,7 +47,7 @@
 
     private final WindowManagerService mService;
 
-    private final TaskSnapshotCache mCache = new TaskSnapshotCache();
+    private final TaskSnapshotCache mCache;
     private final TaskSnapshotPersister mPersister = new TaskSnapshotPersister(
             Environment::getDataSystemCeDirectory);
     private final TaskSnapshotLoader mLoader = new TaskSnapshotLoader(mPersister);
@@ -56,6 +55,7 @@
 
     TaskSnapshotController(WindowManagerService service) {
         mService = service;
+        mCache = new TaskSnapshotCache(mService, mLoader);
     }
 
     void systemReady() {
@@ -86,8 +86,12 @@
         }
     }
 
-    @Nullable TaskSnapshot getSnapshot(Task task) {
-        return mCache.getSnapshot(task);
+    /**
+     * Retrieves a snapshot. If {@param restoreFromDisk} equals {@code true}, DO HOLD THE WINDOW
+     * MANAGER LOCK WHEN CALLING THIS METHOD!
+     */
+    @Nullable TaskSnapshot getSnapshot(int taskId, int userId, boolean restoreFromDisk) {
+        return mCache.getSnapshot(taskId, userId, restoreFromDisk);
     }
 
     /**
@@ -138,20 +142,18 @@
      * Called when an {@link AppWindowToken} has been removed.
      */
     void onAppRemoved(AppWindowToken wtoken) {
-        // TODO: Clean from both recents and running cache.
-        mCache.cleanCache(wtoken);
+        mCache.onAppRemoved(wtoken);
     }
 
     /**
      * Called when the process of an {@link AppWindowToken} has died.
      */
     void onAppDied(AppWindowToken wtoken) {
-
-        // TODO: Only clean from running cache.
-        mCache.cleanCache(wtoken);
+        mCache.onAppDied(wtoken);
     }
 
     void notifyTaskRemovedFromRecents(int taskId, int userId) {
+        mCache.onTaskRemoved(taskId);
         mPersister.onTaskRemovedFromRecents(taskId, userId);
     }