mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
If Bluetooth is activated to add a contact, deactivate it afterwards.
Bug #47.
This commit is contained in:
@@ -13,6 +13,8 @@ import org.briarproject.R;
|
||||
import org.briarproject.android.BriarActivity;
|
||||
import org.briarproject.api.AuthorId;
|
||||
import org.briarproject.api.LocalAuthor;
|
||||
import org.briarproject.api.TransportConfig;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.android.ReferenceManager;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
@@ -22,6 +24,7 @@ import org.briarproject.api.invitation.InvitationState;
|
||||
import org.briarproject.api.invitation.InvitationTask;
|
||||
import org.briarproject.api.invitation.InvitationTaskFactory;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Toast;
|
||||
@@ -48,9 +51,11 @@ implements InvitationListener {
|
||||
private boolean localCompared = false, remoteCompared = false;
|
||||
private boolean localMatched = false, remoteMatched = false;
|
||||
private String contactName = null;
|
||||
private boolean bluetoothWasEnabled = false;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject private volatile DatabaseComponent db;
|
||||
private volatile boolean enableBluetooth = true;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
@@ -129,6 +134,8 @@ implements InvitationListener {
|
||||
}
|
||||
}
|
||||
}
|
||||
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
if(adapter != null) bluetoothWasEnabled = adapter.isEnabled();
|
||||
}
|
||||
|
||||
private void showToastAndFinish() {
|
||||
@@ -142,6 +149,25 @@ implements InvitationListener {
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
view.populate();
|
||||
loadBluetoothSetting();
|
||||
}
|
||||
|
||||
private void loadBluetoothSetting() {
|
||||
runOnDbThread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
TransportConfig c = db.getConfig(new TransportId("bt"));
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if(LOG.isLoggable(INFO))
|
||||
LOG.info("Loading setting took " + duration + " ms");
|
||||
enableBluetooth = c.getBoolean("enable", true);
|
||||
} catch(DbException e) {
|
||||
if(LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -162,6 +188,10 @@ implements InvitationListener {
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
if(task != null) task.removeListener(this);
|
||||
if(!bluetoothWasEnabled && !enableBluetooth) {
|
||||
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
if(adapter != null) adapter.disable();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -248,7 +278,7 @@ implements InvitationListener {
|
||||
remoteInvitationCode = code;
|
||||
setView(new ConnectionView(this));
|
||||
task = invitationTaskFactory.createTask(localAuthorId,
|
||||
localInvitationCode, code);
|
||||
localInvitationCode, code, enableBluetooth);
|
||||
taskHandle = referenceManager.putReference(task, InvitationTask.class);
|
||||
task.addListener(AddContactActivity.this);
|
||||
// Add a second listener so we can remove the first in onDestroy(),
|
||||
|
||||
@@ -7,5 +7,5 @@ public interface InvitationTaskFactory {
|
||||
|
||||
/** Creates a task using the given pseudonym and invitation codes. */
|
||||
InvitationTask createTask(AuthorId localAuthorId, int localCode,
|
||||
int remoteCode);
|
||||
int remoteCode, boolean reuseConnection);
|
||||
}
|
||||
|
||||
@@ -46,14 +46,14 @@ class AliceConnector extends Connector {
|
||||
ConnectionWriterFactory connectionWriterFactory,
|
||||
AuthorFactory authorFactory, GroupFactory groupFactory,
|
||||
KeyManager keyManager, ConnectionDispatcher connectionDispatcher,
|
||||
Clock clock, ConnectorGroup group, DuplexPlugin plugin,
|
||||
LocalAuthor localAuthor,
|
||||
Clock clock, boolean reuseConnection, ConnectorGroup group,
|
||||
DuplexPlugin plugin, LocalAuthor localAuthor,
|
||||
Map<TransportId, TransportProperties> localProps,
|
||||
PseudoRandom random) {
|
||||
super(crypto, db, readerFactory, writerFactory, connectionReaderFactory,
|
||||
connectionWriterFactory, authorFactory, groupFactory,
|
||||
keyManager, connectionDispatcher, clock, group, plugin,
|
||||
localAuthor, localProps, random);
|
||||
keyManager, connectionDispatcher, clock, reuseConnection, group,
|
||||
plugin, localAuthor, localProps, random);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -181,6 +181,7 @@ class AliceConnector extends Connector {
|
||||
LOG.info(pluginName + " pseudonym exchange succeeded");
|
||||
group.pseudonymExchangeSucceeded(remoteAuthor);
|
||||
// Reuse the connection as an outgoing BTP connection
|
||||
reuseConnection(conn, true);
|
||||
if(reuseConnection) reuseConnection(conn, true);
|
||||
else tryToClose(conn, false);
|
||||
}
|
||||
}
|
||||
@@ -46,14 +46,14 @@ class BobConnector extends Connector {
|
||||
ConnectionWriterFactory connectionWriterFactory,
|
||||
AuthorFactory authorFactory, GroupFactory groupFactory,
|
||||
KeyManager keyManager, ConnectionDispatcher connectionDispatcher,
|
||||
Clock clock, ConnectorGroup group, DuplexPlugin plugin,
|
||||
LocalAuthor localAuthor,
|
||||
Clock clock, boolean reuseConnection, ConnectorGroup group,
|
||||
DuplexPlugin plugin, LocalAuthor localAuthor,
|
||||
Map<TransportId, TransportProperties> localProps,
|
||||
PseudoRandom random) {
|
||||
super(crypto, db, readerFactory, writerFactory, connectionReaderFactory,
|
||||
connectionWriterFactory, authorFactory, groupFactory,
|
||||
keyManager, connectionDispatcher, clock, group, plugin,
|
||||
localAuthor, localProps, random);
|
||||
keyManager, connectionDispatcher, clock, reuseConnection, group,
|
||||
plugin, localAuthor, localProps, random);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -181,6 +181,7 @@ class BobConnector extends Connector {
|
||||
LOG.info(pluginName + " pseudonym exchange succeeded");
|
||||
group.pseudonymExchangeSucceeded(remoteAuthor);
|
||||
// Reuse the connection as an incoming BTP connection
|
||||
reuseConnection(conn, false);
|
||||
if(reuseConnection) reuseConnection(conn, false);
|
||||
else tryToClose(conn, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ abstract class Connector extends Thread {
|
||||
protected final KeyManager keyManager;
|
||||
protected final ConnectionDispatcher connectionDispatcher;
|
||||
protected final Clock clock;
|
||||
protected final boolean reuseConnection;
|
||||
protected final ConnectorGroup group;
|
||||
protected final DuplexPlugin plugin;
|
||||
protected final LocalAuthor localAuthor;
|
||||
@@ -90,8 +91,8 @@ abstract class Connector extends Thread {
|
||||
ConnectionWriterFactory connectionWriterFactory,
|
||||
AuthorFactory authorFactory, GroupFactory groupFactory,
|
||||
KeyManager keyManager, ConnectionDispatcher connectionDispatcher,
|
||||
Clock clock, ConnectorGroup group, DuplexPlugin plugin,
|
||||
LocalAuthor localAuthor,
|
||||
Clock clock, boolean reuseConnection, ConnectorGroup group,
|
||||
DuplexPlugin plugin, LocalAuthor localAuthor,
|
||||
Map<TransportId, TransportProperties> localProps,
|
||||
PseudoRandom random) {
|
||||
super("Connector");
|
||||
@@ -106,6 +107,7 @@ abstract class Connector extends Thread {
|
||||
this.keyManager = keyManager;
|
||||
this.connectionDispatcher = connectionDispatcher;
|
||||
this.clock = clock;
|
||||
this.reuseConnection = reuseConnection;
|
||||
this.group = group;
|
||||
this.plugin = plugin;
|
||||
this.localAuthor = localAuthor;
|
||||
|
||||
@@ -56,6 +56,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
private final PluginManager pluginManager;
|
||||
private final AuthorId localAuthorId;
|
||||
private final int localInvitationCode, remoteInvitationCode;
|
||||
private final boolean reuseConnection;
|
||||
private final Collection<InvitationListener> listeners;
|
||||
private final AtomicBoolean connected;
|
||||
private final CountDownLatch localConfirmationLatch;
|
||||
@@ -79,7 +80,8 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
AuthorFactory authorFactory, GroupFactory groupFactory,
|
||||
KeyManager keyManager, ConnectionDispatcher connectionDispatcher,
|
||||
Clock clock, PluginManager pluginManager, AuthorId localAuthorId,
|
||||
int localInvitationCode, int remoteInvitationCode) {
|
||||
int localInvitationCode, int remoteInvitationCode,
|
||||
boolean reuseConnection) {
|
||||
super("ConnectorGroup");
|
||||
this.crypto = crypto;
|
||||
this.db = db;
|
||||
@@ -96,6 +98,7 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
this.localAuthorId = localAuthorId;
|
||||
this.localInvitationCode = localInvitationCode;
|
||||
this.remoteInvitationCode = remoteInvitationCode;
|
||||
this.reuseConnection = reuseConnection;
|
||||
listeners = new CopyOnWriteArrayList<InvitationListener>();
|
||||
connected = new AtomicBoolean(false);
|
||||
localConfirmationLatch = new CountDownLatch(1);
|
||||
@@ -174,8 +177,8 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
remoteInvitationCode);
|
||||
return new AliceConnector(crypto, db, readerFactory, writerFactory,
|
||||
connectionReaderFactory, connectionWriterFactory, authorFactory,
|
||||
groupFactory, keyManager, connectionDispatcher, clock, this,
|
||||
plugin, localAuthor, localProps, random);
|
||||
groupFactory, keyManager, connectionDispatcher, clock,
|
||||
reuseConnection, this, plugin, localAuthor, localProps, random);
|
||||
}
|
||||
|
||||
private Connector createBobConnector(DuplexPlugin plugin,
|
||||
@@ -185,8 +188,8 @@ class ConnectorGroup extends Thread implements InvitationTask {
|
||||
localInvitationCode);
|
||||
return new BobConnector(crypto, db, readerFactory, writerFactory,
|
||||
connectionReaderFactory, connectionWriterFactory, authorFactory,
|
||||
groupFactory, keyManager, connectionDispatcher, clock, this,
|
||||
plugin, localAuthor, localProps, random);
|
||||
groupFactory, keyManager, connectionDispatcher, clock,
|
||||
reuseConnection, this, plugin, localAuthor, localProps, random);
|
||||
}
|
||||
|
||||
public void localConfirmationSucceeded() {
|
||||
|
||||
@@ -56,10 +56,11 @@ class InvitationTaskFactoryImpl implements InvitationTaskFactory {
|
||||
}
|
||||
|
||||
public InvitationTask createTask(AuthorId localAuthorId, int localCode,
|
||||
int remoteCode) {
|
||||
int remoteCode, boolean reuseConnection) {
|
||||
return new ConnectorGroup(crypto, db, readerFactory, writerFactory,
|
||||
connectionReaderFactory, connectionWriterFactory,
|
||||
authorFactory, groupFactory, keyManager, connectionDispatcher,
|
||||
clock, pluginManager, localAuthorId, localCode, remoteCode);
|
||||
clock, pluginManager, localAuthorId, localCode, remoteCode,
|
||||
reuseConnection);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user