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

Commit bd1bc406 authored by Dan Zivkovic's avatar Dan Zivkovic
Browse files

ExecutorUtils lets use schedule a runnable chain.

Bug 19625976.

Change-Id: Iebbef7fed57a381498301dcf26fefa27d06802f7
parent f9e2d271
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -56,8 +56,8 @@ public class ContactsContentObserver implements Runnable {
        mContentObserver = new ContentObserver(null /* handler */) {
            @Override
            public void onChange(boolean self) {
                ExecutorUtils.getBackgroundExecutor()
                        .execute(ContactsContentObserver.this);
                // TODO(zivkovic): Schedule a separate task to reset the decoder.
                ExecutorUtils.getBackgroundExecutor().execute(ContactsContentObserver.this);
            }
        };
        final ContentResolver contentResolver = mContext.getContentResolver();
+35 −25
Original line number Diff line number Diff line
@@ -23,7 +23,9 @@ import com.android.inputmethod.annotations.UsedForTesting;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * Utilities to manage executors.
@@ -35,6 +37,20 @@ public class ExecutorUtils {
    private static final ScheduledExecutorService sExecutorService =
            Executors.newSingleThreadScheduledExecutor(new ExecutorFactory());

    private static class ExecutorFactory implements ThreadFactory {
        @Override
        public Thread newThread(final Runnable runnable) {
            Thread thread = new Thread(runnable, TAG);
            thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread thread, Throwable ex) {
                    Log.w(TAG + "-" + runnable.getClass().getSimpleName(), ex);
                }
            });
            return thread;
        }
    }

    @UsedForTesting
    private static ScheduledExecutorService sExecutorServiceForTests;

@@ -44,6 +60,10 @@ public class ExecutorUtils {
        sExecutorServiceForTests = executorServiceForTests;
    }

    //
    // Public methods used to schedule a runnable for execution.
    //

    /**
     * @return scheduled executor service used to run background tasks
     */
@@ -54,38 +74,28 @@ public class ExecutorUtils {
        return sExecutorService;
    }

    /**
     * Shutdowns all executors and removes all executors from the executor map for testing.
     */
    @UsedForTesting
    public static void shutdownAllExecutors() {
        sExecutorService.execute(new ExecutorShutdown(sExecutorService));
    public static Runnable chain(final Runnable... runnables) {
        return new RunnableChain(runnables);
    }

    private static class ExecutorFactory implements ThreadFactory {
        @Override
        public Thread newThread(final Runnable runnable) {
            Thread thread = new Thread(runnable, TAG);
            thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread thread, Throwable ex) {
                    Log.w(TAG + "-" + runnable.getClass().getSimpleName(), ex);
                }
            });
            return thread;
        }
    }
    private static class RunnableChain implements Runnable {
        private final Runnable[] mRunnables;

    private static class ExecutorShutdown implements Runnable {
        private final ScheduledExecutorService mExecutor;

        public ExecutorShutdown(final ScheduledExecutorService executor) {
            mExecutor = executor;
        private RunnableChain(final Runnable... runnables) {
            if (runnables == null || runnables.length == 0) {
                throw new IllegalArgumentException("Attempting to construct an empty chain");
            }
            mRunnables = runnables;
        }

        @Override
        public void run() {
            mExecutor.shutdown();
            for (Runnable runnable : mRunnables) {
                if (Thread.interrupted()) {
                    return;
                }
                runnable.run();
            }
        }
    }
}