Fixing task cache was incorrectly set when partial list was requested

Change-Id: Ic3ef49930e38b6a66a2871baada122f81865644f
diff --git a/quickstep/src/com/android/quickstep/RecentTasksList.java b/quickstep/src/com/android/quickstep/RecentTasksList.java
index fec38bf..e1b4474 100644
--- a/quickstep/src/com/android/quickstep/RecentTasksList.java
+++ b/quickstep/src/com/android/quickstep/RecentTasksList.java
@@ -16,8 +16,10 @@
 
 package com.android.quickstep;
 
+import android.annotation.TargetApi;
 import android.app.ActivityManager;
 import android.content.Context;
+import android.os.Build;
 import android.os.Process;
 import android.util.SparseBooleanArray;
 import com.android.launcher3.MainThreadExecutor;
@@ -36,6 +38,7 @@
 /**
  * Manages the recent task list from the system, caching it as necessary.
  */
+@TargetApi(Build.VERSION_CODES.P)
 public class RecentTasksList extends TaskStackChangeListener {
 
     private final KeyguardManagerCompat mKeyguardManager;
@@ -46,6 +49,8 @@
     private int mChangeId;
     // The last change id when the list was last loaded completely, must be <= the list change id
     private int mLastLoadedId;
+    // The last change id was loaded with keysOnly  = true
+    private boolean mLastLoadHadKeysOnly;
 
     ArrayList<Task> mTasks = new ArrayList<>();
 
@@ -57,41 +62,43 @@
     }
 
     /**
-     * Asynchronously fetches the list of recent tasks.
+     * Fetches the task keys skipping any local cache.
+     */
+    public void getTaskKeys(int numTasks, Consumer<ArrayList<Task>> callback) {
+        // Kick off task loading in the background
+        mBgThreadExecutor.submit(() -> {
+            ArrayList<Task> tasks = loadTasksInBackground(numTasks, true /* loadKeysOnly */);
+            mMainThreadExecutor.execute(() -> callback.accept(tasks));
+        });
+    }
+
+    /**
+     * Asynchronously fetches the list of recent tasks, reusing cached list if available.
      *
-     * @param numTasks The maximum number of tasks to fetch
      * @param loadKeysOnly Whether to load other associated task data, or just the key
      * @param callback The callback to receive the list of recent tasks
      * @return The change id of the current task list
      */
-    public synchronized int getTasks(int numTasks, boolean loadKeysOnly,
-            Consumer<ArrayList<Task>> callback) {
+    public synchronized int getTasks(boolean loadKeysOnly, Consumer<ArrayList<Task>> callback) {
         final int requestLoadId = mChangeId;
-        final int numLoadTasks = numTasks > 0
-                ? numTasks
-                : Integer.MAX_VALUE;
+        Runnable resultCallback = callback == null
+                ? () -> { }
+                : () -> callback.accept(mTasks);
 
-        if (mLastLoadedId == mChangeId) {
+        if (mLastLoadedId == mChangeId && (!mLastLoadHadKeysOnly || loadKeysOnly)) {
             // The list is up to date, callback with the same list
-            mMainThreadExecutor.execute(() -> {
-                if (callback != null) {
-                    callback.accept(mTasks);
-                }
-            });
+            mMainThreadExecutor.execute(resultCallback);
         }
 
         // Kick off task loading in the background
         mBgThreadExecutor.submit(() -> {
-            ArrayList<Task> tasks = loadTasksInBackground(numLoadTasks,
-                    loadKeysOnly);
+            ArrayList<Task> tasks = loadTasksInBackground(Integer.MAX_VALUE, loadKeysOnly);
 
             mMainThreadExecutor.execute(() -> {
                 mTasks = tasks;
                 mLastLoadedId = requestLoadId;
-
-                if (callback != null) {
-                    callback.accept(tasks);
-                }
+                mLastLoadHadKeysOnly = loadKeysOnly;
+                resultCallback.run();
             });
         });
 
diff --git a/quickstep/src/com/android/quickstep/RecentsModel.java b/quickstep/src/com/android/quickstep/RecentsModel.java
index a62e6ad..75ccba6 100644
--- a/quickstep/src/com/android/quickstep/RecentsModel.java
+++ b/quickstep/src/com/android/quickstep/RecentsModel.java
@@ -98,7 +98,7 @@
      * @return the request id associated with this call.
      */
     public int getTasks(Consumer<ArrayList<Task>> callback) {
-        return mTaskList.getTasks(-1, false /* loadKeysOnly */, callback);
+        return mTaskList.getTasks(false /* loadKeysOnly */, callback);
     }
 
     /**
@@ -124,7 +124,7 @@
      *                 called on the UI thread.
      */
     public void findTaskWithId(int taskId, Consumer<Task.TaskKey> callback) {
-        mTaskList.getTasks(-1, true /* loadKeysOnly */, (tasks) -> {
+        mTaskList.getTasks(true /* loadKeysOnly */, (tasks) -> {
             for (Task task : tasks) {
                 if (task.key.id == taskId) {
                     callback.accept(task.key);
@@ -150,7 +150,7 @@
 
         // Keep the cache up to date with the latest thumbnails
         int runningTaskId = RecentsModel.getRunningTaskId();
-        mTaskList.getTasks(mThumbnailCache.getCacheSize(), true /* keysOnly */, (tasks) -> {
+        mTaskList.getTaskKeys(mThumbnailCache.getCacheSize(), tasks -> {
             for (Task task : tasks) {
                 if (task.key.id == runningTaskId) {
                     // Skip the running task, it's not going to have an up-to-date snapshot by the