Don't disable Bluetooth, always reuse the connection.

This commit is contained in:
akwizgran
2016-03-30 10:38:38 +01:00
parent ed6c3fb1e3
commit a8fa6339fb
5 changed files with 24 additions and 48 deletions

View File

@@ -184,7 +184,7 @@ public class KeyAgreementActivity extends BriarFragmentActivity implements
contactExchangeTask.startExchange(KeyAgreementActivity.this, contactExchangeTask.startExchange(KeyAgreementActivity.this,
localAuthor, result.getMasterKey(), localAuthor, result.getMasterKey(),
result.getConnection(), result.getTransportId(), result.getConnection(), result.getTransportId(),
result.wasAlice(), true); result.wasAlice());
} }
}); });
} }

View File

@@ -27,12 +27,11 @@ import org.briarproject.android.AndroidComponent;
import org.briarproject.android.fragment.BaseEventFragment; import org.briarproject.android.fragment.BaseEventFragment;
import org.briarproject.android.util.AndroidUtils; import org.briarproject.android.util.AndroidUtils;
import org.briarproject.android.util.CameraView; import org.briarproject.android.util.CameraView;
import org.briarproject.android.util.QrCodeUtils;
import org.briarproject.android.util.QrCodeDecoder; import org.briarproject.android.util.QrCodeDecoder;
import org.briarproject.android.util.QrCodeUtils;
import org.briarproject.api.event.Event; import org.briarproject.api.event.Event;
import org.briarproject.api.event.KeyAgreementAbortedEvent; import org.briarproject.api.event.KeyAgreementAbortedEvent;
import org.briarproject.api.event.KeyAgreementFailedEvent; import org.briarproject.api.event.KeyAgreementFailedEvent;
import org.briarproject.api.event.KeyAgreementFinishedEvent;
import org.briarproject.api.event.KeyAgreementListeningEvent; import org.briarproject.api.event.KeyAgreementListeningEvent;
import org.briarproject.api.event.KeyAgreementStartedEvent; import org.briarproject.api.event.KeyAgreementStartedEvent;
import org.briarproject.api.event.KeyAgreementWaitingEvent; import org.briarproject.api.event.KeyAgreementWaitingEvent;
@@ -43,7 +42,6 @@ import org.briarproject.api.keyagreement.PayloadEncoder;
import org.briarproject.api.keyagreement.PayloadParser; import org.briarproject.api.keyagreement.PayloadParser;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.inject.Inject; import javax.inject.Inject;
@@ -57,6 +55,7 @@ import static android.widget.LinearLayout.VERTICAL;
import static android.widget.Toast.LENGTH_LONG; import static android.widget.Toast.LENGTH_LONG;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
@SuppressWarnings("deprecation")
public class ShowQrCodeFragment extends BaseEventFragment public class ShowQrCodeFragment extends BaseEventFragment
implements QrCodeDecoder.ResultCallback { implements QrCodeDecoder.ResultCallback {
@@ -77,14 +76,14 @@ public class ShowQrCodeFragment extends BaseEventFragment
private TextView status; private TextView status;
private ImageView qrCode; private ImageView qrCode;
private volatile KeyAgreementTask task;
private volatile boolean toggleBluetooth;
private volatile BluetoothAdapter adapter;
private BluetoothStateReceiver receiver; private BluetoothStateReceiver receiver;
private AtomicBoolean waitingForBluetooth = new AtomicBoolean();
private QrCodeDecoder decoder; private QrCodeDecoder decoder;
private boolean gotRemotePayload; private boolean gotRemotePayload;
private volatile KeyAgreementTask task;
private volatile BluetoothAdapter adapter;
private volatile boolean waitingForBluetooth;
public static ShowQrCodeFragment newInstance() { public static ShowQrCodeFragment newInstance() {
Bundle args = new Bundle(); Bundle args = new Bundle();
ShowQrCodeFragment fragment = new ShowQrCodeFragment(); ShowQrCodeFragment fragment = new ShowQrCodeFragment();
@@ -132,10 +131,7 @@ public class ShowQrCodeFragment extends BaseEventFragment
boolean portrait = display.getWidth() < display.getHeight(); boolean portrait = display.getWidth() < display.getHeight();
qrLayout.setOrientation(portrait ? VERTICAL : HORIZONTAL); qrLayout.setOrientation(portrait ? VERTICAL : HORIZONTAL);
// Only enable BT adapter if it is not already on.
adapter = BluetoothAdapter.getDefaultAdapter(); adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null)
toggleBluetooth = !adapter.isEnabled();
} }
@Override @Override
@@ -148,9 +144,10 @@ public class ShowQrCodeFragment extends BaseEventFragment
receiver = new BluetoothStateReceiver(); receiver = new BluetoothStateReceiver();
getActivity().registerReceiver(receiver, filter); getActivity().registerReceiver(receiver, filter);
if (adapter != null && toggleBluetooth) { // Enable BT adapter if it is not already on.
waitingForBluetooth.set(true); if (adapter != null && !adapter.isEnabled()) {
toggleBluetooth(true); waitingForBluetooth = true;
AndroidUtils.enableBluetooth(adapter, true);
} else } else
startListening(); startListening();
} }
@@ -190,17 +187,10 @@ public class ShowQrCodeFragment extends BaseEventFragment
@Override @Override
public void run() { public void run() {
task.stopListening(); task.stopListening();
if (toggleBluetooth) toggleBluetooth(false);
} }
}).start(); }).start();
} }
private void toggleBluetooth(boolean enable) {
if (adapter != null) {
AndroidUtils.enableBluetooth(adapter, enable);
}
}
private void openCamera() { private void openCamera() {
AsyncTask<Void, Void, Camera> openTask = AsyncTask<Void, Void, Camera> openTask =
new AsyncTask<Void, Void, Camera>() { new AsyncTask<Void, Void, Camera>() {
@@ -286,9 +276,6 @@ public class ShowQrCodeFragment extends BaseEventFragment
} else if (e instanceof KeyAgreementAbortedEvent) { } else if (e instanceof KeyAgreementAbortedEvent) {
KeyAgreementAbortedEvent event = (KeyAgreementAbortedEvent) e; KeyAgreementAbortedEvent event = (KeyAgreementAbortedEvent) e;
keyAgreementAborted(event.didRemoteAbort()); keyAgreementAborted(event.didRemoteAbort());
} else if (e instanceof KeyAgreementFinishedEvent) {
// We want to reuse the connection, so don't disable Bluetooth
toggleBluetooth = false;
} }
} }
@@ -374,9 +361,9 @@ public class ShowQrCodeFragment extends BaseEventFragment
@Override @Override
public void onReceive(Context ctx, Intent intent) { public void onReceive(Context ctx, Intent intent) {
int state = intent.getIntExtra(EXTRA_STATE, 0); int state = intent.getIntExtra(EXTRA_STATE, 0);
if (state == STATE_ON && waitingForBluetooth.get()) { if (state == STATE_ON && waitingForBluetooth) {
LOG.info("Bluetooth enabled"); LOG.info("Bluetooth enabled");
waitingForBluetooth.set(false); waitingForBluetooth = false;
startListening(); startListening();
} }
} }

View File

@@ -16,5 +16,5 @@ public interface ContactExchangeTask {
void startExchange(ContactExchangeListener listener, void startExchange(ContactExchangeListener listener,
LocalAuthor localAuthor, SecretKey masterSecret, LocalAuthor localAuthor, SecretKey masterSecret,
DuplexTransportConnection conn, TransportId transportId, DuplexTransportConnection conn, TransportId transportId,
boolean alice, boolean reuseConnection); boolean alice);
} }

View File

@@ -22,7 +22,6 @@ import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.plugins.ConnectionManager; import org.briarproject.api.plugins.ConnectionManager;
import org.briarproject.api.plugins.duplex.DuplexTransportConnection; import org.briarproject.api.plugins.duplex.DuplexTransportConnection;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.api.transport.KeyManager;
import org.briarproject.api.transport.StreamReaderFactory; import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory; import org.briarproject.api.transport.StreamWriterFactory;
@@ -51,7 +50,6 @@ public class ContactExchangeTaskImpl extends Thread
private final ConnectionManager connectionManager; private final ConnectionManager connectionManager;
private final ContactManager contactManager; private final ContactManager contactManager;
private final CryptoComponent crypto; private final CryptoComponent crypto;
private final KeyManager keyManager;
private final StreamReaderFactory streamReaderFactory; private final StreamReaderFactory streamReaderFactory;
private final StreamWriterFactory streamWriterFactory; private final StreamWriterFactory streamWriterFactory;
@@ -61,14 +59,12 @@ public class ContactExchangeTaskImpl extends Thread
private TransportId transportId; private TransportId transportId;
private SecretKey masterSecret; private SecretKey masterSecret;
private boolean alice; private boolean alice;
private boolean reuseConnection;
public ContactExchangeTaskImpl(AuthorFactory authorFactory, public ContactExchangeTaskImpl(AuthorFactory authorFactory,
BdfReaderFactory bdfReaderFactory, BdfReaderFactory bdfReaderFactory,
BdfWriterFactory bdfWriterFactory, Clock clock, BdfWriterFactory bdfWriterFactory, Clock clock,
ConnectionManager connectionManager, ContactManager contactManager, ConnectionManager connectionManager, ContactManager contactManager,
CryptoComponent crypto, KeyManager keyManager, CryptoComponent crypto, StreamReaderFactory streamReaderFactory,
StreamReaderFactory streamReaderFactory,
StreamWriterFactory streamWriterFactory) { StreamWriterFactory streamWriterFactory) {
this.authorFactory = authorFactory; this.authorFactory = authorFactory;
this.bdfReaderFactory = bdfReaderFactory; this.bdfReaderFactory = bdfReaderFactory;
@@ -77,7 +73,6 @@ public class ContactExchangeTaskImpl extends Thread
this.connectionManager = connectionManager; this.connectionManager = connectionManager;
this.contactManager = contactManager; this.contactManager = contactManager;
this.crypto = crypto; this.crypto = crypto;
this.keyManager = keyManager;
this.streamReaderFactory = streamReaderFactory; this.streamReaderFactory = streamReaderFactory;
this.streamWriterFactory = streamWriterFactory; this.streamWriterFactory = streamWriterFactory;
} }
@@ -86,14 +81,13 @@ public class ContactExchangeTaskImpl extends Thread
public void startExchange(ContactExchangeListener listener, public void startExchange(ContactExchangeListener listener,
LocalAuthor localAuthor, SecretKey masterSecret, LocalAuthor localAuthor, SecretKey masterSecret,
DuplexTransportConnection conn, TransportId transportId, DuplexTransportConnection conn, TransportId transportId,
boolean alice, boolean reuseConnection) { boolean alice) {
this.listener = listener; this.listener = listener;
this.localAuthor = localAuthor; this.localAuthor = localAuthor;
this.conn = conn; this.conn = conn;
this.transportId = transportId; this.transportId = transportId;
this.masterSecret = masterSecret; this.masterSecret = masterSecret;
this.alice = alice; this.alice = alice;
this.reuseConnection = reuseConnection;
start(); start();
} }
@@ -164,9 +158,8 @@ public class ContactExchangeTaskImpl extends Thread
ContactId contactId = ContactId contactId =
addContact(remoteAuthor, masterSecret, timestamp, true); addContact(remoteAuthor, masterSecret, timestamp, true);
// Reuse the connection as a transport connection // Reuse the connection as a transport connection
if (reuseConnection) connectionManager.manageOutgoingConnection(contactId, transportId,
connectionManager conn);
.manageOutgoingConnection(contactId, transportId, conn);
// Pseudonym exchange succeeded // Pseudonym exchange succeeded
LOG.info("Pseudonym exchange succeeded"); LOG.info("Pseudonym exchange succeeded");
listener.contactExchangeSucceeded(remoteAuthor); listener.contactExchangeSucceeded(remoteAuthor);
@@ -235,9 +228,8 @@ public class ContactExchangeTaskImpl extends Thread
private ContactId addContact(Author remoteAuthor, SecretKey master, private ContactId addContact(Author remoteAuthor, SecretKey master,
long timestamp, boolean alice) throws DbException { long timestamp, boolean alice) throws DbException {
// Add the contact to the database // Add the contact to the database
ContactId contactId = contactManager.addContact(remoteAuthor, return contactManager.addContact(remoteAuthor, localAuthor.getId(),
localAuthor.getId(), master, timestamp, alice, true); master, timestamp, alice, true);
return contactId;
} }
private void tryToClose(DuplexTransportConnection conn, private void tryToClose(DuplexTransportConnection conn,

View File

@@ -10,7 +10,6 @@ import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.lifecycle.LifecycleManager; import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.plugins.ConnectionManager; import org.briarproject.api.plugins.ConnectionManager;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.api.transport.KeyManager;
import org.briarproject.api.transport.StreamReaderFactory; import org.briarproject.api.transport.StreamReaderFactory;
import org.briarproject.api.transport.StreamWriterFactory; import org.briarproject.api.transport.StreamWriterFactory;
@@ -41,12 +40,10 @@ public class ContactModule {
AuthorFactory authorFactory, BdfReaderFactory bdfReaderFactory, AuthorFactory authorFactory, BdfReaderFactory bdfReaderFactory,
BdfWriterFactory bdfWriterFactory, Clock clock, BdfWriterFactory bdfWriterFactory, Clock clock,
ConnectionManager connectionManager, ContactManager contactManager, ConnectionManager connectionManager, ContactManager contactManager,
CryptoComponent crypto, KeyManager keyManager, CryptoComponent crypto, StreamReaderFactory streamReaderFactory,
StreamReaderFactory streamReaderFactory,
StreamWriterFactory streamWriterFactory) { StreamWriterFactory streamWriterFactory) {
return new ContactExchangeTaskImpl(authorFactory, return new ContactExchangeTaskImpl(authorFactory, bdfReaderFactory,
bdfReaderFactory, bdfWriterFactory, clock, connectionManager, bdfWriterFactory, clock, connectionManager, contactManager,
contactManager, crypto, keyManager, streamReaderFactory, crypto, streamReaderFactory, streamWriterFactory);
streamWriterFactory);
} }
} }