From 91f4967124733eb5f5605b7a313ab8f10fb38439 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Tue, 6 Nov 2012 13:48:26 +0000 Subject: [PATCH] Simplified AndroidExecutor API. --- .../sf/briar/android/AndroidExecutorImpl.java | 21 +++++++------------ .../sf/briar/api/android/AndroidExecutor.java | 7 +++---- .../plugins/droidtooth/DroidtoothPlugin.java | 13 +++++------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/net/sf/briar/android/AndroidExecutorImpl.java b/src/net/sf/briar/android/AndroidExecutorImpl.java index fd3ac27c2..7928e059d 100644 --- a/src/net/sf/briar/android/AndroidExecutorImpl.java +++ b/src/net/sf/briar/android/AndroidExecutorImpl.java @@ -2,6 +2,7 @@ package net.sf.briar.android; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.atomic.AtomicBoolean; @@ -15,7 +16,7 @@ import com.google.inject.Inject; class AndroidExecutorImpl implements AndroidExecutor { - private static final int SHUTDOWN = 0, RUNNABLE = 1, CALLABLE = 2; + private static final int SHUTDOWN = 0, RUN = 1; private final Runnable loop; private final AtomicBoolean started = new AtomicBoolean(false); @@ -45,20 +46,13 @@ class AndroidExecutorImpl implements AndroidExecutor { } } - public Future submit(Runnable r) { - startIfNecessary(); - Future f = new FutureTask(r, null); - Message m = Message.obtain(handler, RUNNABLE, f); - handler.sendMessage(m); - return f; - } - - public Future submit(Callable c) { + public V run(Callable c) throws InterruptedException, + ExecutionException { startIfNecessary(); Future f = new FutureTask(c); - Message m = Message.obtain(handler, RUNNABLE, f); + Message m = Message.obtain(handler, RUN, f); handler.sendMessage(m); - return f; + return f.get(); } public void shutdown() { @@ -76,8 +70,7 @@ class AndroidExecutorImpl implements AndroidExecutor { case SHUTDOWN: Looper.myLooper().quit(); break; - case RUNNABLE: - case CALLABLE: + case RUN: ((FutureTask) m.obj).run(); break; default: diff --git a/src/net/sf/briar/api/android/AndroidExecutor.java b/src/net/sf/briar/api/android/AndroidExecutor.java index 3066a7b24..b950806aa 100644 --- a/src/net/sf/briar/api/android/AndroidExecutor.java +++ b/src/net/sf/briar/api/android/AndroidExecutor.java @@ -1,7 +1,7 @@ package net.sf.briar.api.android; import java.util.concurrent.Callable; -import java.util.concurrent.Future; +import java.util.concurrent.ExecutionException; /** * Enables background threads to make Android API calls that must be made from @@ -9,9 +9,8 @@ import java.util.concurrent.Future; */ public interface AndroidExecutor { - Future submit(Runnable r); - - Future submit(Callable c); + /** Runs the given task on a thread with a message queue. */ + V run(Callable c) throws InterruptedException, ExecutionException; void shutdown(); } diff --git a/src/net/sf/briar/plugins/droidtooth/DroidtoothPlugin.java b/src/net/sf/briar/plugins/droidtooth/DroidtoothPlugin.java index f93708e21..3d53d5595 100644 --- a/src/net/sf/briar/plugins/droidtooth/DroidtoothPlugin.java +++ b/src/net/sf/briar/plugins/droidtooth/DroidtoothPlugin.java @@ -23,7 +23,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; -import java.util.concurrent.Future; import java.util.logging.Logger; import net.sf.briar.api.ContactId; @@ -89,14 +88,12 @@ class DroidtoothPlugin implements DuplexPlugin { public void start() throws IOException { // BluetoothAdapter.getDefaultAdapter() must be called on a thread // with a message queue, so submit it to the AndroidExecutor - Callable c = new Callable() { - public BluetoothAdapter call() throws Exception { - return BluetoothAdapter.getDefaultAdapter(); - } - }; - Future f = androidExecutor.submit(c); try { - adapter = f.get(); + adapter = androidExecutor.run(new Callable() { + public BluetoothAdapter call() throws Exception { + return BluetoothAdapter.getDefaultAdapter(); + } + }); } catch(InterruptedException e) { throw new IOException(e.toString()); } catch(ExecutionException e) {