mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 07:09:56 +01:00
Simplified AndroidExecutor API.
This commit is contained in:
@@ -2,6 +2,7 @@ package net.sf.briar.android;
|
|||||||
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.FutureTask;
|
import java.util.concurrent.FutureTask;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
@@ -15,7 +16,7 @@ import com.google.inject.Inject;
|
|||||||
|
|
||||||
class AndroidExecutorImpl implements AndroidExecutor {
|
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 Runnable loop;
|
||||||
private final AtomicBoolean started = new AtomicBoolean(false);
|
private final AtomicBoolean started = new AtomicBoolean(false);
|
||||||
@@ -45,20 +46,13 @@ class AndroidExecutorImpl implements AndroidExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Future<Void> submit(Runnable r) {
|
public <V> V run(Callable<V> c) throws InterruptedException,
|
||||||
startIfNecessary();
|
ExecutionException {
|
||||||
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) {
|
|
||||||
startIfNecessary();
|
startIfNecessary();
|
||||||
Future<V> f = new FutureTask<V>(c);
|
Future<V> f = new FutureTask<V>(c);
|
||||||
Message m = Message.obtain(handler, RUNNABLE, f);
|
Message m = Message.obtain(handler, RUN, f);
|
||||||
handler.sendMessage(m);
|
handler.sendMessage(m);
|
||||||
return f;
|
return f.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
@@ -76,8 +70,7 @@ class AndroidExecutorImpl implements AndroidExecutor {
|
|||||||
case SHUTDOWN:
|
case SHUTDOWN:
|
||||||
Looper.myLooper().quit();
|
Looper.myLooper().quit();
|
||||||
break;
|
break;
|
||||||
case RUNNABLE:
|
case RUN:
|
||||||
case CALLABLE:
|
|
||||||
((FutureTask<?>) m.obj).run();
|
((FutureTask<?>) m.obj).run();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package net.sf.briar.api.android;
|
package net.sf.briar.api.android;
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
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
|
* 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 {
|
public interface AndroidExecutor {
|
||||||
|
|
||||||
Future<Void> submit(Runnable r);
|
/** Runs the given task on a thread with a message queue. */
|
||||||
|
<V> V run(Callable<V> c) throws InterruptedException, ExecutionException;
|
||||||
<V> Future<V> submit(Callable<V> c);
|
|
||||||
|
|
||||||
void shutdown();
|
void shutdown();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import java.util.concurrent.Callable;
|
|||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import net.sf.briar.api.ContactId;
|
import net.sf.briar.api.ContactId;
|
||||||
@@ -89,14 +88,12 @@ class DroidtoothPlugin implements DuplexPlugin {
|
|||||||
public void start() throws IOException {
|
public void start() throws IOException {
|
||||||
// BluetoothAdapter.getDefaultAdapter() must be called on a thread
|
// BluetoothAdapter.getDefaultAdapter() must be called on a thread
|
||||||
// with a message queue, so submit it to the AndroidExecutor
|
// 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 {
|
try {
|
||||||
adapter = f.get();
|
adapter = androidExecutor.run(new Callable<BluetoothAdapter>() {
|
||||||
|
public BluetoothAdapter call() throws Exception {
|
||||||
|
return BluetoothAdapter.getDefaultAdapter();
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch(InterruptedException e) {
|
} catch(InterruptedException e) {
|
||||||
throw new IOException(e.toString());
|
throw new IOException(e.toString());
|
||||||
} catch(ExecutionException e) {
|
} catch(ExecutionException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user