Code cleanup for DroidtoothPlugin.

This commit is contained in:
akwizgran
2014-03-22 16:35:47 +00:00
parent ab2849e2f3
commit c028728c19

View File

@@ -58,7 +58,8 @@ class DroidtoothPlugin implements DuplexPlugin {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(DroidtoothPlugin.class.getName()); Logger.getLogger(DroidtoothPlugin.class.getName());
private static final int UUID_BYTES = 16; private static final int UUID_BYTES = 16;
private static final String FOUND = "android.bluetooth.device.action.FOUND"; private static final String FOUND =
"android.bluetooth.device.action.FOUND";
private static final String DISCOVERY_FINISHED = private static final String DISCOVERY_FINISHED =
"android.bluetooth.adapter.action.DISCOVERY_FINISHED"; "android.bluetooth.adapter.action.DISCOVERY_FINISHED";
@@ -136,12 +137,9 @@ class DroidtoothPlugin implements DuplexPlugin {
if(adapter.isEnabled()) { if(adapter.isEnabled()) {
bind(); bind();
} else if(callback.getConfig().getBoolean("enable", true)) { } else if(callback.getConfig().getBoolean("enable", true)) {
if(adapter.enable()) { wasDisabled = true;
LOG.info("Enabling Bluetooth"); if(adapter.enable()) LOG.info("Enabling Bluetooth");
wasDisabled = true; else LOG.info("Could not enable Bluetooth");
} else {
LOG.info("Could not enable Bluetooth");
}
} else { } else {
LOG.info("Not enabling Bluetooth"); LOG.info("Not enabling Bluetooth");
} }
@@ -151,8 +149,7 @@ class DroidtoothPlugin implements DuplexPlugin {
private void bind() { private void bind() {
pluginExecutor.execute(new Runnable() { pluginExecutor.execute(new Runnable() {
public void run() { public void run() {
if(!running) return; if(!isRunning()) return;
if(!adapter.isEnabled()) return;
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info("Local address " + adapter.getAddress()); LOG.info("Local address " + adapter.getAddress());
// Advertise the Bluetooth address to contacts // Advertise the Bluetooth address to contacts
@@ -169,7 +166,7 @@ class DroidtoothPlugin implements DuplexPlugin {
tryToClose(ss); tryToClose(ss);
return; return;
} }
if(!running) { if(!isRunning()) {
tryToClose(ss); tryToClose(ss);
return; return;
} }
@@ -202,7 +199,7 @@ class DroidtoothPlugin implements DuplexPlugin {
} }
private void acceptContactConnections() { private void acceptContactConnections() {
while(running) { while(isRunning()) {
BluetoothSocket s; BluetoothSocket s;
try { try {
s = socket.accept(); s = socket.accept();
@@ -244,8 +241,7 @@ class DroidtoothPlugin implements DuplexPlugin {
} }
public void poll(Collection<ContactId> connected) { public void poll(Collection<ContactId> connected) {
if(!running) return; if(!isRunning()) return;
if(!adapter.isEnabled()) return;
// Try to connect to known devices in parallel // Try to connect to known devices in parallel
Map<ContactId, TransportProperties> remote = Map<ContactId, TransportProperties> remote =
callback.getRemoteProperties(); callback.getRemoteProperties();
@@ -308,7 +304,7 @@ class DroidtoothPlugin implements DuplexPlugin {
} }
public DuplexTransportConnection createConnection(ContactId c) { public DuplexTransportConnection createConnection(ContactId c) {
if(!running) return null; if(!isRunning()) return null;
TransportProperties p = callback.getRemoteProperties().get(c); TransportProperties p = callback.getRemoteProperties().get(c);
if(p == null) return null; if(p == null) return null;
String address = p.get("address"); String address = p.get("address");
@@ -326,8 +322,7 @@ class DroidtoothPlugin implements DuplexPlugin {
public DuplexTransportConnection createInvitationConnection(PseudoRandom r, public DuplexTransportConnection createInvitationConnection(PseudoRandom r,
long timeout) { long timeout) {
if(!running) return null; if(!isRunning()) return null;
if(!adapter.isEnabled()) return null;
// Use the invitation codes to generate the UUID // Use the invitation codes to generate the UUID
byte[] b = r.nextBytes(UUID_BYTES); byte[] b = r.nextBytes(UUID_BYTES);
UUID uuid = UUID.nameUUIDFromBytes(b); UUID uuid = UUID.nameUUIDFromBytes(b);
@@ -397,13 +392,13 @@ class DroidtoothPlugin implements DuplexPlugin {
@Override @Override
public void run() { public void run() {
long now = clock.currentTimeMillis(); long end = clock.currentTimeMillis() + timeout;
long end = now + timeout; while(!finished(end)) {
while(now < end && running && !socketLatch.isSet()) {
// Discover nearby devices // Discover nearby devices
LOG.info("Discovering nearby devices"); LOG.info("Discovering nearby devices");
List<String> addresses; List<String> addresses;
try { try {
long now = clock.currentTimeMillis();
addresses = discoverDevices(end - now); addresses = discoverDevices(end - now);
} catch(InterruptedException e) { } catch(InterruptedException e) {
LOG.warning("Interrupted while discovering devices"); LOG.warning("Interrupted while discovering devices");
@@ -412,15 +407,13 @@ class DroidtoothPlugin implements DuplexPlugin {
} }
if(addresses.isEmpty()) { if(addresses.isEmpty()) {
LOG.info("No devices discovered"); LOG.info("No devices discovered");
now = clock.currentTimeMillis();
continue; continue;
} }
// Connect to any device with the right UUID // Connect to any device with the right UUID
for(String address : addresses) { for(String address : addresses) {
now = clock.currentTimeMillis(); if(finished(end)) return;
if(now < end && running && !socketLatch.isSet()) { BluetoothSocket s = connect(address, uuid);
BluetoothSocket s = connect(address, uuid); if(s != null) {
if(s == null) continue;
LOG.info("Outgoing connection"); LOG.info("Outgoing connection");
if(!socketLatch.set(s)) { if(!socketLatch.set(s)) {
LOG.info("Closing redundant connection"); LOG.info("Closing redundant connection");
@@ -432,6 +425,11 @@ class DroidtoothPlugin implements DuplexPlugin {
} }
} }
private boolean finished(long end) {
long now = clock.currentTimeMillis();
return now >= end || !isRunning() || socketLatch.isSet();
}
private List<String> discoverDevices(long timeout) private List<String> discoverDevices(long timeout)
throws InterruptedException { throws InterruptedException {
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();