From e7dcf62693d651bd57cd56d7b008008f1b6e44d3 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 26 May 2023 11:17:49 -0700 Subject: [PATCH] Add new API enqueue to get result with Future --- .../java/com/topjohnwu/superuser/Shell.java | 20 +++++++++++++------ .../topjohnwu/superuser/internal/JobImpl.java | 10 ++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/topjohnwu/superuser/Shell.java b/core/src/main/java/com/topjohnwu/superuser/Shell.java index f8e91d0..a3be306 100644 --- a/core/src/main/java/com/topjohnwu/superuser/Shell.java +++ b/core/src/main/java/com/topjohnwu/superuser/Shell.java @@ -39,6 +39,7 @@ import java.util.Objects; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** @@ -235,8 +236,8 @@ public abstract class Shell implements Closeable { * {@link Job#to(List)} or {@link Job#to(List, List)}. *

* The main shell will NOT be requested until the developer invokes either - * {@link Job#exec()} or {@code Job.submit(...)}. This makes it possible to - * construct {@link Job}s before the program has created any root shell. + * {@link Job#exec()}, {@link Job#enqueue()}, or {@code Job.submit(...)}. This makes it + * possible to construct {@link Job}s before the program has created any root shell. * @return a job that the developer can execute or submit later. * @see Job#add(String...) */ @@ -255,8 +256,8 @@ public abstract class Shell implements Closeable { * {@link Job#to(List)} or {@link Job#to(List, List)}. *

* The main shell will NOT be requested until the developer invokes either - * {@link Job#exec()} or {@code Job.submit(...)}. This makes it possible to - * construct {@link Job}s before the program has created any root shell. + * {@link Job#exec()}, {@link Job#enqueue()}, or {@code Job.submit(...)}. This makes it + * possible to construct {@link Job}s before the program has created any root shell. * @see Job#add(InputStream) */ @NonNull @@ -585,7 +586,7 @@ public abstract class Shell implements Closeable { * Submit the job to an internal queue to run in the background. * The result will be omitted. */ - public void submit() { + public final void submit() { submit(null); } @@ -594,7 +595,7 @@ public abstract class Shell implements Closeable { * The result will be returned with a callback running on the main thread. * @param cb the callback to receive the result of the job. */ - public void submit(@Nullable ResultCallback cb) { + public final void submit(@Nullable ResultCallback cb) { submit(UiThreadHandler.executor, cb); } @@ -606,6 +607,13 @@ public abstract class Shell implements Closeable { * @param cb the callback to receive the result of the job. */ public abstract void submit(@Nullable Executor executor, @Nullable ResultCallback cb); + + /** + * Submit the job to an internal queue to run in the background. + * @return a {@link Future} to get the result of the job later. + */ + @NonNull + public abstract Future enqueue(); } /** diff --git a/core/src/main/java/com/topjohnwu/superuser/internal/JobImpl.java b/core/src/main/java/com/topjohnwu/superuser/internal/JobImpl.java index d1a3cea..5379806 100644 --- a/core/src/main/java/com/topjohnwu/superuser/internal/JobImpl.java +++ b/core/src/main/java/com/topjohnwu/superuser/internal/JobImpl.java @@ -28,6 +28,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.Executor; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; class JobImpl extends Shell.Job implements Closeable { @@ -81,6 +83,14 @@ class JobImpl extends Shell.Job implements Closeable { return exec0(); } + @NonNull + @Override + public Future enqueue() { + FutureTask future = new FutureTask<>(this::exec0); + shell.executor.execute(future); + return future; + } + @Override public void submit(@Nullable Executor executor, @Nullable Shell.ResultCallback cb) { shell.executor.execute(() -> exec0().callback(executor, cb));