mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 15:19:53 +01:00
add KeyAgreementEventListener interface
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package org.briarproject.briar.android.keyagreement;
|
package org.briarproject.briar.android.keyagreement;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.UiThread;
|
||||||
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.contact.ContactExchangeListener;
|
import org.briarproject.bramble.api.contact.ContactExchangeListener;
|
||||||
@@ -12,6 +14,7 @@ import org.briarproject.bramble.api.identity.LocalAuthor;
|
|||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.R.string;
|
import org.briarproject.briar.R.string;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
|
|
||||||
@@ -22,6 +25,7 @@ import javax.inject.Inject;
|
|||||||
|
|
||||||
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;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
@@ -48,11 +52,6 @@ public class ContactExchangeActivity extends KeyAgreementActivity implements
|
|||||||
getSupportActionBar().setTitle(string.add_contact_title);
|
getSupportActionBar().setTitle(string.add_contact_title);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
void keyAgreementFinished(KeyAgreementResult result) {
|
|
||||||
runOnUiThreadUnlessDestroyed(() -> startContactExchange(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startContactExchange(KeyAgreementResult result) {
|
private void startContactExchange(KeyAgreementResult result) {
|
||||||
runOnDbThread(() -> {
|
runOnDbThread(() -> {
|
||||||
LocalAuthor localAuthor;
|
LocalAuthor localAuthor;
|
||||||
@@ -60,7 +59,7 @@ public class ContactExchangeActivity extends KeyAgreementActivity implements
|
|||||||
try {
|
try {
|
||||||
localAuthor = identityManager.getLocalAuthor();
|
localAuthor = identityManager.getLocalAuthor();
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
logException(LOG, WARNING, e);
|
||||||
contactExchangeFailed();
|
contactExchangeFailed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -105,4 +104,42 @@ public class ContactExchangeActivity extends KeyAgreementActivity implements
|
|||||||
finish();
|
finish();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public void keyAgreementFailed() {
|
||||||
|
// TODO show failure somewhere persistent?
|
||||||
|
Toast.makeText(this, R.string.connection_failed,
|
||||||
|
LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public void keyAgreementWaiting(TextView status) {
|
||||||
|
status.setText(R.string.waiting_for_contact_to_scan);
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public void keyAgreementStarted(TextView status) {
|
||||||
|
status.setText(R.string.authenticating_with_device);
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public void keyAgreementAborted(boolean remoteAborted) {
|
||||||
|
// TODO show abort somewhere persistent?
|
||||||
|
Toast.makeText(this,
|
||||||
|
remoteAborted ? R.string.connection_aborted_remote :
|
||||||
|
R.string.connection_aborted_local, LENGTH_LONG)
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
@Override
|
||||||
|
public void keyAgreementFinished(TextView status,
|
||||||
|
KeyAgreementResult result) {
|
||||||
|
status.setText(R.string.exchanging_contact_details);
|
||||||
|
startContactExchange(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,11 +16,7 @@ import android.support.v7.widget.Toolbar;
|
|||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
import org.briarproject.bramble.api.event.EventListener;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementFinishedEvent;
|
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.event.BluetoothEnabledEvent;
|
import org.briarproject.bramble.api.plugin.event.BluetoothEnabledEvent;
|
||||||
@@ -47,15 +43,14 @@ import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
|||||||
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
import static android.widget.Toast.LENGTH_LONG;
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
|
||||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_ENABLE_BLUETOOTH;
|
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_ENABLE_BLUETOOTH;
|
||||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PERMISSION_CAMERA;
|
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PERMISSION_CAMERA;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
public abstract class KeyAgreementActivity extends BriarActivity implements
|
public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||||
BaseFragmentListener, IntroScreenSeenListener, EventListener {
|
BaseFragmentListener, IntroScreenSeenListener,
|
||||||
|
KeyAgreementFragment.KeyAgreementEventListener {
|
||||||
|
|
||||||
private enum BluetoothState {
|
private enum BluetoothState {
|
||||||
UNKNOWN, NO_ADAPTER, WAITING, REFUSED, ENABLED
|
UNKNOWN, NO_ADAPTER, WAITING, REFUSED, ENABLED
|
||||||
@@ -99,18 +94,6 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
if (bluetoothReceiver != null) unregisterReceiver(bluetoothReceiver);
|
if (bluetoothReceiver != null) unregisterReceiver(bluetoothReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStart() {
|
|
||||||
super.onStart();
|
|
||||||
eventBus.addListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onStop() {
|
|
||||||
super.onStop();
|
|
||||||
eventBus.removeListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
@@ -194,7 +177,7 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
// FIXME #824
|
// FIXME #824
|
||||||
FragmentManager fm = getSupportFragmentManager();
|
FragmentManager fm = getSupportFragmentManager();
|
||||||
if (fm.findFragmentByTag(KeyAgreementFragment.TAG) == null) {
|
if (fm.findFragmentByTag(KeyAgreementFragment.TAG) == null) {
|
||||||
BaseFragment f = KeyAgreementFragment.newInstance();
|
BaseFragment f = KeyAgreementFragment.newInstance(this);
|
||||||
fm.beginTransaction()
|
fm.beginTransaction()
|
||||||
.replace(R.id.fragmentContainer, f, f.getUniqueTag())
|
.replace(R.id.fragmentContainer, f, f.getUniqueTag())
|
||||||
.addToBackStack(f.getUniqueTag())
|
.addToBackStack(f.getUniqueTag())
|
||||||
@@ -264,79 +247,6 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void eventOccurred(Event e) {
|
|
||||||
if (e instanceof KeyAgreementFinishedEvent) {
|
|
||||||
KeyAgreementFinishedEvent event = (KeyAgreementFinishedEvent) e;
|
|
||||||
keyAgreementFinished(event.getResult());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
<<<<<<<HEAD
|
|
||||||
|
|
||||||
private void keyAgreementFinished(KeyAgreementResult result) {
|
|
||||||
runOnUiThreadUnlessDestroyed(() -> startContactExchange(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startContactExchange(KeyAgreementResult result) {
|
|
||||||
runOnDbThread(() -> {
|
|
||||||
LocalAuthor localAuthor;
|
|
||||||
// Load the local pseudonym
|
|
||||||
try {
|
|
||||||
localAuthor = identityManager.getLocalAuthor();
|
|
||||||
} catch (DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
contactExchangeFailed();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exchange contact details
|
|
||||||
contactExchangeTask.startExchange(KeyAgreementActivity.this,
|
|
||||||
localAuthor, result.getMasterKey(),
|
|
||||||
result.getConnection(), result.getTransportId(),
|
|
||||||
result.wasAlice());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void contactExchangeSucceeded(Author remoteAuthor) {
|
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
|
||||||
String contactName = remoteAuthor.getName();
|
|
||||||
String format = getString(string.contact_added_toast);
|
|
||||||
String text = String.format(format, contactName);
|
|
||||||
Toast.makeText(KeyAgreementActivity.this, text, LENGTH_LONG).show();
|
|
||||||
supportFinishAfterTransition();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void duplicateContact(Author remoteAuthor) {
|
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
|
||||||
String contactName = remoteAuthor.getName();
|
|
||||||
String format = getString(string.contact_already_exists);
|
|
||||||
String text = String.format(format, contactName);
|
|
||||||
Toast.makeText(KeyAgreementActivity.this, text, LENGTH_LONG).show();
|
|
||||||
finish();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void contactExchangeFailed() {
|
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
|
||||||
Toast.makeText(KeyAgreementActivity.this,
|
|
||||||
string.contact_exchange_failed, LENGTH_LONG).show();
|
|
||||||
finish();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
=======
|
|
||||||
|
|
||||||
abstract void keyAgreementFinished(KeyAgreementResult result);
|
|
||||||
>>>>>>>927ab9571...
|
|
||||||
Add some
|
|
||||||
abstraction to
|
|
||||||
the keyagreement
|
|
||||||
ui
|
|
||||||
|
|
||||||
private class BluetoothStateReceiver extends BroadcastReceiver {
|
private class BluetoothStateReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import com.google.zxing.Result;
|
|||||||
import org.briarproject.bramble.api.UnsupportedVersionException;
|
import org.briarproject.bramble.api.UnsupportedVersionException;
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
import org.briarproject.bramble.api.event.EventBus;
|
||||||
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementResult;
|
||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementTask;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementTask;
|
||||||
import org.briarproject.bramble.api.keyagreement.Payload;
|
import org.briarproject.bramble.api.keyagreement.Payload;
|
||||||
import org.briarproject.bramble.api.keyagreement.PayloadEncoder;
|
import org.briarproject.bramble.api.keyagreement.PayloadEncoder;
|
||||||
@@ -87,10 +88,12 @@ public class KeyAgreementFragment extends BaseEventFragment
|
|||||||
private boolean gotRemotePayload;
|
private boolean gotRemotePayload;
|
||||||
private volatile boolean gotLocalPayload;
|
private volatile boolean gotLocalPayload;
|
||||||
private KeyAgreementTask task;
|
private KeyAgreementTask task;
|
||||||
|
private KeyAgreementEventListener listener;
|
||||||
|
|
||||||
public static KeyAgreementFragment newInstance() {
|
public static KeyAgreementFragment newInstance(KeyAgreementEventListener listener) {
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
KeyAgreementFragment fragment = new KeyAgreementFragment();
|
KeyAgreementFragment fragment = new KeyAgreementFragment();
|
||||||
|
fragment.listener = listener;
|
||||||
fragment.setArguments(args);
|
fragment.setArguments(args);
|
||||||
return fragment;
|
return fragment;
|
||||||
}
|
}
|
||||||
@@ -265,29 +268,27 @@ public class KeyAgreementFragment extends BaseEventFragment
|
|||||||
KeyAgreementAbortedEvent event = (KeyAgreementAbortedEvent) e;
|
KeyAgreementAbortedEvent event = (KeyAgreementAbortedEvent) e;
|
||||||
keyAgreementAborted(event.didRemoteAbort());
|
keyAgreementAborted(event.didRemoteAbort());
|
||||||
} else if (e instanceof KeyAgreementFinishedEvent) {
|
} else if (e instanceof KeyAgreementFinishedEvent) {
|
||||||
keyAgreementFinished();
|
keyAgreementFinished(((KeyAgreementFinishedEvent) e).getResult());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void keyAgreementFailed() {
|
private void keyAgreementFailed() {
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
runOnUiThreadUnlessDestroyed(() -> {
|
||||||
reset();
|
reset();
|
||||||
// TODO show failure somewhere persistent?
|
listener.keyAgreementFailed();
|
||||||
Toast.makeText(getActivity(), R.string.connection_failed,
|
|
||||||
LENGTH_LONG).show();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void keyAgreementWaiting() {
|
private void keyAgreementWaiting() {
|
||||||
runOnUiThreadUnlessDestroyed(
|
runOnUiThreadUnlessDestroyed(
|
||||||
() -> status.setText(R.string.waiting_for_contact_to_scan));
|
() -> listener.keyAgreementWaiting(status));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void keyAgreementStarted() {
|
private void keyAgreementStarted() {
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
runOnUiThreadUnlessDestroyed(() -> {
|
||||||
qrCodeView.setVisibility(INVISIBLE);
|
qrCodeView.setVisibility(INVISIBLE);
|
||||||
statusView.setVisibility(VISIBLE);
|
statusView.setVisibility(VISIBLE);
|
||||||
status.setText(R.string.authenticating_with_device);
|
listener.keyAgreementStarted(status);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,18 +298,14 @@ public class KeyAgreementFragment extends BaseEventFragment
|
|||||||
qrCodeView.setVisibility(VISIBLE);
|
qrCodeView.setVisibility(VISIBLE);
|
||||||
statusView.setVisibility(INVISIBLE);
|
statusView.setVisibility(INVISIBLE);
|
||||||
status.setText("");
|
status.setText("");
|
||||||
// TODO show abort somewhere persistent?
|
listener.keyAgreementAborted(remoteAborted);
|
||||||
Toast.makeText(getActivity(),
|
|
||||||
remoteAborted ? R.string.connection_aborted_remote :
|
|
||||||
R.string.connection_aborted_local, LENGTH_LONG)
|
|
||||||
.show();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void keyAgreementFinished() {
|
private void keyAgreementFinished(KeyAgreementResult result) {
|
||||||
runOnUiThreadUnlessDestroyed(() -> {
|
runOnUiThreadUnlessDestroyed(() -> {
|
||||||
statusView.setVisibility(VISIBLE);
|
statusView.setVisibility(VISIBLE);
|
||||||
status.setText(R.string.exchanging_contact_details);
|
listener.keyAgreementFinished(status, result);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,4 +340,22 @@ public class KeyAgreementFragment extends BaseEventFragment
|
|||||||
protected void finish() {
|
protected void finish() {
|
||||||
getActivity().getSupportFragmentManager().popBackStack();
|
getActivity().getSupportFragmentManager().popBackStack();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface KeyAgreementEventListener {
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
void keyAgreementFailed();
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
void keyAgreementWaiting(TextView status);
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
void keyAgreementStarted(TextView status);
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
void keyAgreementAborted(boolean remoteAborted);
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
void keyAgreementFinished(TextView status, KeyAgreementResult result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user