Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2f979d26 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Fixing task cache was incorrectly set when partial list was requested

Change-Id: Ic3ef49930e38b6a66a2871baada122f81865644f
parent 161a214e
Loading
Loading
Loading
Loading
+26 −19
Original line number Diff line number Diff line
@@ -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<Task> 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<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();
            });
        });

+3 −3
Original line number Diff line number Diff line
@@ -98,7 +98,7 @@ public class RecentsModel extends TaskStackChangeListener {
     * @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 @@ public class RecentsModel extends TaskStackChangeListener {
     *                 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 @@ 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