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

Commit c9230bce authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Refactoring handler runnable to be more generic" into sc-dev

parents 7c86dcca 4c222611
Loading
Loading
Loading
Loading
+34 −22
Original line number Diff line number Diff line
@@ -17,19 +17,36 @@ package com.android.launcher3.icons.cache;

import android.os.Handler;

import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * A runnable that can be posted to a {@link Handler} which can be canceled.
 */
public abstract class HandlerRunnable implements Runnable {
public class HandlerRunnable<T> implements Runnable {

    private final Handler mWorkerHandler;
    private final Supplier<T> mTask;

    private final Handler mHandler;
    private final Executor mCallbackExecutor;
    private final Consumer<T> mCallback;
    private final Runnable mEndRunnable;

    private boolean mEnded = false;
    private boolean mCanceled = false;

    public HandlerRunnable(Handler handler, Runnable endRunnable) {
        mHandler = handler;
    public HandlerRunnable(Handler workerHandler, Supplier<T> task, Executor callbackExecutor,
            Consumer<T> callback) {
        this(workerHandler, task, callbackExecutor, callback, () -> { });
    }

    public HandlerRunnable(Handler workerHandler, Supplier<T> task, Executor callbackExecutor,
            Consumer<T> callback, Runnable endRunnable) {
        mWorkerHandler = workerHandler;
        mTask = task;
        mCallbackExecutor = callbackExecutor;
        mCallback = callback;
        mEndRunnable = endRunnable;
    }

@@ -37,31 +54,26 @@ public abstract class HandlerRunnable implements Runnable {
     * Cancels this runnable from being run, only if it has not already run.
     */
    public void cancel() {
        mHandler.removeCallbacks(this);
        // TODO: This can actually cause onEnd to be called twice if the handler is already running
        //       this runnable
        // NOTE: This is currently run on whichever thread the caller is run on.
        mWorkerHandler.removeCallbacks(this);
        mCanceled = true;
        onEnd();
        mCallbackExecutor.execute(this::onEnd);
    }

    /**
     * @return whether this runnable was canceled.
     */
    protected boolean isCanceled() {
        return mCanceled;
    @Override
    public void run() {
        T value = mTask.get();
        mCallbackExecutor.execute(() -> {
            if (!mCanceled) {
                mCallback.accept(value);
            }
            onEnd();
        });
    }

    /**
     * To be called by the implemention of this runnable. The end callback is done on whichever
     * thread the caller is calling from.
     */
    public void onEnd() {
    private void onEnd() {
        if (!mEnded) {
            mEnded = true;
            if (mEndRunnable != null) {
            mEndRunnable.run();
        }
    }
}
}

searchuilib/.gitignore

0 → 100644
+13 −0
Original line number Diff line number Diff line
*.iml
.project
.classpath
.project.properties
gen/
bin/
.idea/
.gradle/
local.properties
gradle/
build/
gradlew*
.DS_Store