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

Commit cc1dad0c authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Merging multiple implementations of CancellableTask

Bug: 318539160
Flag: None
Test: Manual
Change-Id: Ie855427c2676e0ce76655e4dd67472c0b5e435d8
parent 772e0127
Loading
Loading
Loading
Loading
+13 −16
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ package com.android.quickstep;

import static com.android.launcher3.Flags.enableOverviewIconMenu;
import static com.android.launcher3.util.DisplayController.CHANGE_DENSITY;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;

import android.annotation.Nullable;
import android.app.ActivityManager;
@@ -41,12 +42,12 @@ import com.android.launcher3.icons.BaseIconFactory.IconOptions;
import com.android.launcher3.icons.BitmapInfo;
import com.android.launcher3.icons.IconProvider;
import com.android.launcher3.pm.UserCache;
import com.android.launcher3.util.CancellableTask;
import com.android.launcher3.util.DisplayController;
import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
import com.android.launcher3.util.DisplayController.Info;
import com.android.launcher3.util.FlagOp;
import com.android.launcher3.util.Preconditions;
import com.android.quickstep.util.CancellableTask;
import com.android.quickstep.util.TaskKeyLruCache;
import com.android.quickstep.util.TaskVisualsChangeListener;
import com.android.systemui.shared.recents.model.Task;
@@ -109,21 +110,17 @@ public class TaskIconCache implements DisplayInfoChangeListener {
            callback.accept(task);
            return null;
        }
        CancellableTask<TaskCacheEntry> request = new CancellableTask<TaskCacheEntry>() {
            @Override
            public TaskCacheEntry getResultOnBg() {
                return getCacheEntry(task);
            }

            @Override
            public void handleResult(TaskCacheEntry result) {
        CancellableTask<TaskCacheEntry> request = new CancellableTask<>(
                () -> getCacheEntry(task),
                MAIN_EXECUTOR,
                result -> {
                    task.icon = result.icon;
                    task.titleDescription = result.contentDescription;
                    task.title = result.title;
                    callback.accept(task);
                    dispatchIconUpdate(task.key.id);
                }
        };
        );
        mBgExecutor.execute(request);
        return request;
    }
+23 −26
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package com.android.quickstep;

import static com.android.launcher3.Flags.enableGridOnlyOverview;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;

import android.content.Context;
import android.content.res.Resources;
@@ -23,8 +24,8 @@ import android.content.res.Resources;
import androidx.annotation.VisibleForTesting;

import com.android.launcher3.R;
import com.android.launcher3.util.CancellableTask;
import com.android.launcher3.util.Preconditions;
import com.android.quickstep.util.CancellableTask;
import com.android.quickstep.util.TaskKeyByLastActiveTimeCache;
import com.android.quickstep.util.TaskKeyCache;
import com.android.quickstep.util.TaskKeyLruCache;
@@ -195,33 +196,29 @@ public class TaskThumbnailCache {
            return null;
        }

        CancellableTask<ThumbnailData> request = new CancellableTask<>() {
            @Override
            public ThumbnailData getResultOnBg() {
                ThumbnailData thumbnailData = ActivityManagerWrapper.getInstance().getTaskThumbnail(
                        key.id, lowResolution);
                if (thumbnailData.thumbnail != null) {
                    return thumbnailData;
                }
                return ActivityManagerWrapper.getInstance().takeTaskThumbnail(key.id);
            }

            @Override
            public void handleResult(ThumbnailData result) {
                // Avoid an async timing issue that a low res entry replaces an existing high res
                // entry in high res enabled state, so we check before putting it to cache
        CancellableTask<ThumbnailData> request = new CancellableTask<>(
                () -> {
                    ThumbnailData thumbnailData = ActivityManagerWrapper.getInstance()
                            .getTaskThumbnail(key.id, lowResolution);
                    return thumbnailData.thumbnail != null ? thumbnailData
                            : ActivityManagerWrapper.getInstance().takeTaskThumbnail(key.id);
                },
                MAIN_EXECUTOR,
                result -> {
                    // Avoid an async timing issue that a low res entry replaces an existing high
                    // res entry in high res enabled state, so we check before putting it to cache
                    if (enableGridOnlyOverview() && result.reducedResolution
                            && getHighResLoadingState().isEnabled()) {
                    ThumbnailData cachedThumbnail = mCache.getAndInvalidateIfModified(key);
                    if (cachedThumbnail != null && cachedThumbnail.thumbnail != null
                            && !cachedThumbnail.reducedResolution) {
                        ThumbnailData newCachedThumbnail = mCache.getAndInvalidateIfModified(key);
                        if (newCachedThumbnail != null && newCachedThumbnail.thumbnail != null
                                && !newCachedThumbnail.reducedResolution) {
                            return;
                        }
                    }
                    mCache.put(key, result);
                    callback.accept(result);
                }
        };
        );
        mBgExecutor.execute(request);
        return request;
    }
+0 −68
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.quickstep.util;

import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;

import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;

/**
 * Utility class to executore a task on background and post the result on UI thread
 */
public abstract class CancellableTask<T> implements Runnable {

    private boolean mCancelled = false;

    @Override
    public final void run() {
        if (mCancelled) {
            return;
        }
        T result = getResultOnBg();
        if (mCancelled) {
            return;
        }
        MAIN_EXECUTOR.execute(() -> {
            if (mCancelled) {
                return;
            }
            handleResult(result);
        });
    }

    /**
     * Called on the worker thread to process the request. The return object is passed to
     * {@link #handleResult(Object)}
     */
    @WorkerThread
    public abstract T getResultOnBg();

    /**
     * Called on the UI thread to handle the final result.
     * @param result
     */
    @UiThread
    public abstract void handleResult(T result);

    /**
     * Cancels the request. If it is called before {@link #handleResult(Object)}, that method
     * will not be called
     */
    public void cancel() {
        mCancelled = true;
    }
}
+1 −2
Original line number Diff line number Diff line
@@ -44,10 +44,10 @@ import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.desktop.DesktopRecentsTransitionController;
import com.android.launcher3.icons.IconProvider;
import com.android.launcher3.util.CancellableTask;
import com.android.launcher3.util.RunnableList;
import com.android.quickstep.RecentsModel;
import com.android.quickstep.TaskThumbnailCache;
import com.android.quickstep.util.CancellableTask;
import com.android.quickstep.util.RecentsOrientedState;
import com.android.systemui.shared.recents.model.Task;
import com.android.systemui.shared.recents.model.ThumbnailData;
@@ -63,7 +63,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;


/**
 * TaskView that contains all tasks that are part of the desktop.
 */
+1 −2
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import com.android.internal.jank.Cuj;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.util.CancellableTask;
import com.android.launcher3.util.RunnableList;
import com.android.launcher3.util.SplitConfigurationOptions;
import com.android.launcher3.util.SplitConfigurationOptions.SplitBounds;
@@ -31,7 +32,6 @@ import com.android.launcher3.util.TransformingTouchDelegate;
import com.android.quickstep.RecentsModel;
import com.android.quickstep.TaskIconCache;
import com.android.quickstep.TaskThumbnailCache;
import com.android.quickstep.util.CancellableTask;
import com.android.quickstep.util.RecentsOrientedState;
import com.android.quickstep.util.SplitSelectStateController;
import com.android.systemui.shared.recents.model.Task;
@@ -45,7 +45,6 @@ import kotlin.Unit;
import java.util.HashMap;
import java.util.function.Consumer;


/**
 * TaskView that contains and shows thumbnails for not one, BUT TWO(!!) tasks
 *
Loading