Simplified AndroidExecutor API.

This commit is contained in:
akwizgran
2012-11-06 13:48:26 +00:00
parent 42383001f1
commit 91f4967124
3 changed files with 15 additions and 26 deletions

View File

@@ -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<Void> submit(Runnable r) {
startIfNecessary();
Future<Void> f = new FutureTask<Void>(r, null);
Message m = Message.obtain(handler, RUNNABLE, f);
handler.sendMessage(m);
return f;
}
public <V> Future<V> submit(Callable<V> c) {
public <V> V run(Callable<V> c) throws InterruptedException,
ExecutionException {
startIfNecessary();
Future<V> f = new FutureTask<V>(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:

View File

@@ -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<Void> submit(Runnable r);
<V> Future<V> submit(Callable<V> c);
/** Runs the given task on a thread with a message queue. */
<V> V run(Callable<V> c) throws InterruptedException, ExecutionException;
void shutdown();
}

View File

@@ -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<BluetoothAdapter> c = new Callable<BluetoothAdapter>() {
public BluetoothAdapter call() throws Exception {
return BluetoothAdapter.getDefaultAdapter();
}
};
Future<BluetoothAdapter> f = androidExecutor.submit(c);
try {
adapter = f.get();
adapter = androidExecutor.run(new Callable<BluetoothAdapter>() {
public BluetoothAdapter call() throws Exception {
return BluetoothAdapter.getDefaultAdapter();
}
});
} catch(InterruptedException e) {
throw new IOException(e.toString());
} catch(ExecutionException e) {