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

View File

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