mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
Implement BQP Android UI using QR codes
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
package org.briarproject.android.keyagreement;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.AdapterView.OnItemSelectedListener;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.AndroidComponent;
|
||||
import org.briarproject.android.fragment.BaseFragment;
|
||||
import org.briarproject.android.identity.CreateIdentityActivity;
|
||||
import org.briarproject.android.identity.LocalAuthorItem;
|
||||
import org.briarproject.android.identity.LocalAuthorItemComparator;
|
||||
import org.briarproject.android.identity.LocalAuthorSpinnerAdapter;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.app.Activity.RESULT_OK;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.android.identity.LocalAuthorItem.NEW;
|
||||
|
||||
public class ChooseIdentityFragment extends BaseFragment
|
||||
implements OnItemSelectedListener {
|
||||
|
||||
interface IdentitySelectedListener {
|
||||
void identitySelected(AuthorId localAuthorId);
|
||||
}
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ChooseIdentityFragment.class.getName());
|
||||
|
||||
public static final String TAG = "ChooseIdentityFragment";
|
||||
|
||||
private static final int REQUEST_CREATE_IDENTITY = 1;
|
||||
|
||||
private IdentitySelectedListener lsnr;
|
||||
private LocalAuthorSpinnerAdapter adapter;
|
||||
private Spinner spinner;
|
||||
private View button;
|
||||
|
||||
private AuthorId localAuthorId;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject
|
||||
protected volatile IdentityManager identityManager;
|
||||
|
||||
public static ChooseIdentityFragment newInstance() {
|
||||
Bundle args = new Bundle();
|
||||
ChooseIdentityFragment fragment = new ChooseIdentityFragment();
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
try {
|
||||
lsnr = (IdentitySelectedListener) context;
|
||||
} catch (ClassCastException e) {
|
||||
throw new ClassCastException(
|
||||
"Using class must implement IdentitySelectedListener");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectActivity(AndroidComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.invitation_bluetooth_start, container,
|
||||
false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
adapter = new LocalAuthorSpinnerAdapter(getActivity(), false);
|
||||
spinner = (Spinner) view.findViewById(R.id.spinner);
|
||||
spinner.setAdapter(adapter);
|
||||
spinner.setOnItemSelectedListener(this);
|
||||
|
||||
button = view.findViewById(R.id.continueButton);
|
||||
button.setEnabled(false);
|
||||
button.setOnClickListener(
|
||||
new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
lsnr.identitySelected(localAuthorId);
|
||||
}
|
||||
});
|
||||
|
||||
loadLocalAuthors();
|
||||
}
|
||||
|
||||
private void loadLocalAuthors() {
|
||||
listener.runOnDbThread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
Collection<LocalAuthor> authors =
|
||||
identityManager.getLocalAuthors();
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading authors took " + duration + " ms");
|
||||
displayLocalAuthors(authors);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void displayLocalAuthors(final Collection<LocalAuthor> authors) {
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
adapter.clear();
|
||||
for (LocalAuthor a : authors)
|
||||
adapter.add(new LocalAuthorItem(a));
|
||||
adapter.sort(LocalAuthorItemComparator.INSTANCE);
|
||||
// If a local author has been selected, select it again
|
||||
if (localAuthorId == null) return;
|
||||
int count = adapter.getCount();
|
||||
for (int i = 0; i < count; i++) {
|
||||
LocalAuthorItem item = adapter.getItem(i);
|
||||
if (item == NEW) continue;
|
||||
if (item.getLocalAuthor().getId().equals(localAuthorId)) {
|
||||
spinner.setSelection(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setLocalAuthorId(AuthorId authorId) {
|
||||
localAuthorId = authorId;
|
||||
button.setEnabled(localAuthorId != null);
|
||||
}
|
||||
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position,
|
||||
long id) {
|
||||
LocalAuthorItem item = adapter.getItem(position);
|
||||
if (item == NEW) {
|
||||
setLocalAuthorId(null);
|
||||
Intent i = new Intent(getActivity(), CreateIdentityActivity.class);
|
||||
startActivityForResult(i, REQUEST_CREATE_IDENTITY);
|
||||
} else {
|
||||
setLocalAuthorId(item.getLocalAuthor().getId());
|
||||
}
|
||||
}
|
||||
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
setLocalAuthorId(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int request, int result, Intent data) {
|
||||
if (request == REQUEST_CREATE_IDENTITY && result == RESULT_OK) {
|
||||
byte[] b = data.getByteArrayExtra("briar.LOCAL_AUTHOR_ID");
|
||||
if (b == null) throw new IllegalStateException();
|
||||
setLocalAuthorId(new AuthorId(b));
|
||||
loadLocalAuthors();
|
||||
} else
|
||||
super.onActivityResult(request, result, data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
package org.briarproject.android.keyagreement;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.AndroidComponent;
|
||||
import org.briarproject.android.BriarFragmentActivity;
|
||||
import org.briarproject.android.fragment.BaseFragment;
|
||||
import org.briarproject.android.util.CustomAnimations;
|
||||
import org.briarproject.api.contact.ContactExchangeListener;
|
||||
import org.briarproject.api.contact.ContactExchangeTask;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.event.Event;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.EventListener;
|
||||
import org.briarproject.api.event.KeyAgreementFinishedEvent;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.keyagreement.KeyAgreementResult;
|
||||
import org.briarproject.api.settings.SettingsManager;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
public class KeyAgreementActivity extends BriarFragmentActivity implements
|
||||
BaseFragment.BaseFragmentListener,
|
||||
ChooseIdentityFragment.IdentitySelectedListener, EventListener,
|
||||
ContactExchangeListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(KeyAgreementActivity.class.getName());
|
||||
|
||||
private static final String LOCAL_AUTHOR_ID = "briar.LOCAL_AUTHOR_ID";
|
||||
|
||||
private static final int STEP_ID = 1;
|
||||
private static final int STEP_QR = 2;
|
||||
private static final int STEPS = 2;
|
||||
|
||||
@Inject
|
||||
protected EventBus eventBus;
|
||||
@Inject
|
||||
protected SettingsManager settingsManager;
|
||||
|
||||
private Toolbar toolbar;
|
||||
private View progressContainer;
|
||||
private TextView progressTitle;
|
||||
|
||||
private AuthorId localAuthorId;
|
||||
|
||||
@Inject
|
||||
protected volatile ContactExchangeTask contactExchangeTask;
|
||||
@Inject
|
||||
protected volatile IdentityManager identityManager;
|
||||
|
||||
@Override
|
||||
public void injectActivity(AndroidComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(state);
|
||||
setContentView(R.layout.activity_with_loading);
|
||||
|
||||
toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
progressContainer = findViewById(R.id.container_progress);
|
||||
progressTitle = (TextView) findViewById(R.id.title_progress_bar);
|
||||
|
||||
setSupportActionBar(toolbar);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
|
||||
if (state != null) {
|
||||
byte[] b = state.getByteArray(LOCAL_AUTHOR_ID);
|
||||
if (b != null)
|
||||
localAuthorId = new AuthorId(b);
|
||||
}
|
||||
|
||||
showStep(localAuthorId == null ? STEP_ID : STEP_QR);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
private void showStep(int step) {
|
||||
getSupportActionBar().setTitle(
|
||||
String.format(getString(R.string.add_contact_title_step), step,
|
||||
STEPS));
|
||||
switch (step) {
|
||||
case STEP_QR:
|
||||
startFragment(ShowQrCodeFragment.newInstance());
|
||||
break;
|
||||
case STEP_ID:
|
||||
default:
|
||||
startFragment(ChooseIdentityFragment.newInstance());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
eventBus.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle state) {
|
||||
super.onSaveInstanceState(state);
|
||||
if (localAuthorId != null) {
|
||||
byte[] b = localAuthorId.getBytes();
|
||||
state.putByteArray(LOCAL_AUTHOR_ID, b);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showLoadingScreen(boolean isBlocking, int stringId) {
|
||||
if (isBlocking) {
|
||||
CustomAnimations.animateHeight(toolbar, false, 250);
|
||||
}
|
||||
progressTitle.setText(stringId);
|
||||
progressContainer.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hideLoadingScreen() {
|
||||
CustomAnimations.animateHeight(toolbar, true, 250);
|
||||
progressContainer.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void identitySelected(AuthorId localAuthorId) {
|
||||
this.localAuthorId = localAuthorId;
|
||||
showStep(STEP_QR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof KeyAgreementFinishedEvent) {
|
||||
KeyAgreementFinishedEvent event = (KeyAgreementFinishedEvent) e;
|
||||
keyAgreementFinished(event.getResult());
|
||||
}
|
||||
}
|
||||
|
||||
private void keyAgreementFinished(final KeyAgreementResult result) {
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
showLoadingScreen(false, R.string.exchanging_contact_details);
|
||||
startContactExchange(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void startContactExchange(final KeyAgreementResult result) {
|
||||
runOnDbThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LocalAuthor localAuthor;
|
||||
// Load the local pseudonym
|
||||
try {
|
||||
localAuthor = identityManager.getLocalAuthor(localAuthorId);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
contactExchangeFailed();
|
||||
return;
|
||||
}
|
||||
|
||||
// Exchange contact details
|
||||
contactExchangeTask.startExchange(KeyAgreementActivity.this,
|
||||
localAuthor, result.getMasterKey(),
|
||||
result.getConnection(), result.getTransportId(),
|
||||
result.wasAlice(), true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contactExchangeSucceeded(final Author remoteAuthor) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
String contactName = remoteAuthor.getName();
|
||||
String format = getString(R.string.contact_added_toast);
|
||||
String text = String.format(format, contactName);
|
||||
Toast.makeText(KeyAgreementActivity.this, text, LENGTH_LONG)
|
||||
.show();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void duplicateContact(final Author remoteAuthor) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
String contactName = remoteAuthor.getName();
|
||||
String format = getString(R.string.contact_already_exists);
|
||||
String text = String.format(format, contactName);
|
||||
Toast.makeText(KeyAgreementActivity.this, text, LENGTH_LONG)
|
||||
.show();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contactExchangeFailed() {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
Toast.makeText(KeyAgreementActivity.this,
|
||||
R.string.contact_exchange_failed, LENGTH_LONG).show();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,384 @@
|
||||
package org.briarproject.android.keyagreement;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.hardware.Camera;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Base64;
|
||||
import android.view.Display;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.animation.AlphaAnimation;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.zxing.Result;
|
||||
|
||||
import org.briarproject.R;
|
||||
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.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;
|
||||
import org.briarproject.api.keyagreement.KeyAgreementTask;
|
||||
import org.briarproject.api.keyagreement.KeyAgreementTaskFactory;
|
||||
import org.briarproject.api.keyagreement.Payload;
|
||||
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;
|
||||
|
||||
import static android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED;
|
||||
import static android.bluetooth.BluetoothAdapter.EXTRA_STATE;
|
||||
import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
||||
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
|
||||
import static android.widget.LinearLayout.HORIZONTAL;
|
||||
import static android.widget.LinearLayout.VERTICAL;
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
public class ShowQrCodeFragment extends BaseEventFragment
|
||||
implements QrCodeDecoder.ResultCallback {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ShowQrCodeFragment.class.getName());
|
||||
|
||||
public static final String TAG = "ShowQrCodeFragment";
|
||||
|
||||
@Inject
|
||||
protected KeyAgreementTaskFactory keyAgreementTaskFactory;
|
||||
@Inject
|
||||
protected PayloadEncoder payloadEncoder;
|
||||
@Inject
|
||||
protected PayloadParser payloadParser;
|
||||
|
||||
private LinearLayout qrLayout;
|
||||
private CameraView cameraView;
|
||||
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;
|
||||
|
||||
public static ShowQrCodeFragment newInstance() {
|
||||
Bundle args = new Bundle();
|
||||
ShowQrCodeFragment fragment = new ShowQrCodeFragment();
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectActivity(AndroidComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_keyagreement_qr, container,
|
||||
false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
qrLayout = (LinearLayout) view.findViewById(R.id.qr_layout);
|
||||
cameraView = (CameraView) view.findViewById(R.id.camera_view);
|
||||
status = (TextView) view.findViewById(R.id.connect_status);
|
||||
qrCode = (ImageView) view.findViewById(R.id.qr_code);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
getActivity().setRequestedOrientation(SCREEN_ORIENTATION_NOSENSOR);
|
||||
|
||||
decoder = new QrCodeDecoder(this);
|
||||
|
||||
Display display = getActivity().getWindowManager().getDefaultDisplay();
|
||||
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
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
// Listen for changes to the Bluetooth state
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(ACTION_STATE_CHANGED);
|
||||
receiver = new BluetoothStateReceiver();
|
||||
getActivity().registerReceiver(receiver, filter);
|
||||
|
||||
if (adapter != null && toggleBluetooth) {
|
||||
waitingForBluetooth.set(true);
|
||||
toggleBluetooth(true);
|
||||
} else
|
||||
startListening();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (!gotRemotePayload) openCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
if (!gotRemotePayload) releaseCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
stopListening();
|
||||
if (receiver != null) getActivity().unregisterReceiver(receiver);
|
||||
}
|
||||
|
||||
private void startListening() {
|
||||
task = keyAgreementTaskFactory.getTask();
|
||||
gotRemotePayload = false;
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
task.listen();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void stopListening() {
|
||||
new Thread(new Runnable() {
|
||||
@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>() {
|
||||
@Override
|
||||
protected Camera doInBackground(Void... unused) {
|
||||
LOG.info("Opening camera");
|
||||
try {
|
||||
return Camera.open();
|
||||
} catch (RuntimeException e) {
|
||||
LOG.log(WARNING,
|
||||
"Error opening camera, trying again", e);
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e2) {
|
||||
LOG.info("Interrupted before second attempt");
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Camera.open();
|
||||
} catch (RuntimeException e2) {
|
||||
LOG.log(WARNING, "Error opening camera", e2);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Camera camera) {
|
||||
if (camera == null) {
|
||||
// TODO better solution?
|
||||
getActivity().finish();
|
||||
} else {
|
||||
cameraView.start(camera, decoder, 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
openTask.execute();
|
||||
}
|
||||
|
||||
private void releaseCamera() {
|
||||
LOG.info("Releasing camera");
|
||||
try {
|
||||
cameraView.stop();
|
||||
} catch (RuntimeException e) {
|
||||
LOG.log(WARNING, "Error releasing camera", e);
|
||||
// TODO better solution
|
||||
getActivity().finish();
|
||||
}
|
||||
}
|
||||
|
||||
private void reset() {
|
||||
cameraView.setVisibility(View.VISIBLE);
|
||||
startListening();
|
||||
openCamera();
|
||||
}
|
||||
|
||||
private void qrCodeScanned(String content) {
|
||||
try {
|
||||
// TODO use Base32
|
||||
Payload remotePayload = payloadParser.parse(
|
||||
Base64.decode(content, 0));
|
||||
cameraView.setVisibility(View.GONE);
|
||||
status.setText(R.string.connecting_to_device);
|
||||
task.connectAndRunProtocol(remotePayload);
|
||||
} catch (IOException e) {
|
||||
// TODO show failure
|
||||
Toast.makeText(getActivity(), R.string.qr_code_invalid,
|
||||
LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof KeyAgreementListeningEvent) {
|
||||
KeyAgreementListeningEvent event = (KeyAgreementListeningEvent) e;
|
||||
setQrCode(event.getLocalPayload());
|
||||
} else if (e instanceof KeyAgreementFailedEvent) {
|
||||
keyAgreementFailed();
|
||||
} else if (e instanceof KeyAgreementWaitingEvent) {
|
||||
keyAgreementWaiting();
|
||||
} else if (e instanceof KeyAgreementStartedEvent) {
|
||||
keyAgreementStarted();
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
private void setQrCode(final Payload localPayload) {
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO use Base32
|
||||
String input = Base64.encodeToString(
|
||||
payloadEncoder.encode(localPayload), 0);
|
||||
qrCode.setImageBitmap(
|
||||
QrCodeUtils.createQrCode(getActivity(), input));
|
||||
// Simple fade-in animation
|
||||
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
|
||||
anim.setDuration(200);
|
||||
qrCode.startAnimation(anim);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void keyAgreementFailed() {
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
reset();
|
||||
// TODO show failure somewhere persistent?
|
||||
Toast.makeText(getActivity(), R.string.connection_failed,
|
||||
LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void keyAgreementWaiting() {
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
status.setText(R.string.waiting_for_contact);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void keyAgreementStarted() {
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listener.showLoadingScreen(false,
|
||||
R.string.authenticating_with_device);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void keyAgreementAborted(final boolean remoteAborted) {
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
reset();
|
||||
listener.hideLoadingScreen();
|
||||
// TODO show abort somewhere persistent?
|
||||
Toast.makeText(getActivity(),
|
||||
remoteAborted ? R.string.connection_aborted_remote :
|
||||
R.string.connection_aborted_local, LENGTH_LONG)
|
||||
.show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleResult(final Result result) {
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
LOG.info("Got result from decoder");
|
||||
if (!gotRemotePayload) {
|
||||
gotRemotePayload = true;
|
||||
releaseCamera();
|
||||
qrCodeScanned(result.getText());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class BluetoothStateReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context ctx, Intent intent) {
|
||||
int state = intent.getIntExtra(EXTRA_STATE, 0);
|
||||
if (state == STATE_ON && waitingForBluetooth.get()) {
|
||||
LOG.info("Bluetooth enabled");
|
||||
waitingForBluetooth.set(false);
|
||||
startListening();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user