diff --git a/quickstep/src/com/android/quickstep/RecentTasksList.java b/quickstep/src/com/android/quickstep/RecentTasksList.java index fec38bf144..e1b44740eb 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 @@ import java.util.function.Consumer; /** * 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 @@ public class RecentTasksList extends TaskStackChangeListener { 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 mTasks = new ArrayList<>(); @@ -57,41 +62,43 @@ public class RecentTasksList extends TaskStackChangeListener { } /** - * Asynchronously fetches the list of recent tasks. + * Fetches the task keys skipping any local cache. + */ + public void getTaskKeys(int numTasks, Consumer> callback) { + // Kick off task loading in the background + mBgThreadExecutor.submit(() -> { + ArrayList 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> callback) { + public synchronized int getTasks(boolean loadKeysOnly, Consumer> 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 tasks = loadTasksInBackground(numLoadTasks, - loadKeysOnly); + ArrayList 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 a62e6adfae..75ccba6183 100644 --- a/quickstep/src/com/android/quickstep/RecentsModel.java +++ b/quickstep/src/com/android/quickstep/RecentsModel.java @@ -98,7 +98,7 @@ public class RecentsModel extends TaskStackChangeListener { * @return the request id associated with this call. */ public int getTasks(Consumer> callback) { - return mTaskList.getTasks(-1, false /* loadKeysOnly */, callback); + return mTaskList.getTasks(false /* loadKeysOnly */, callback); } /** @@ -124,7 +124,7 @@ public class RecentsModel extends TaskStackChangeListener { * called on the UI thread. */ public void findTaskWithId(int taskId, Consumer 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 @@ public class RecentsModel extends TaskStackChangeListener { // 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