Skip to content
Snippets Groups Projects
Unverified Commit 9a3ad118 authored by akwizgran's avatar akwizgran
Browse files

Retry opening camera, finish activity on any error.

parent 1f9b87ce
No related branches found
No related tags found
No related merge requests found
......@@ -57,10 +57,9 @@
<string name="contact_exchange_failed">Contact exchange failed</string>
<string name="scan_qr_code">Scan QR code</string>
<string name="qr_code_invalid">The QR code is invalid</string>
<string name="could_not_open_camera">Could not open camera</string>
<string name="connecting_to_device">Connecting to device\u2026</string>
<string name="authenticating_with_device">Authenticating with device\u2026</string>
<string name="connection_aborted_local">Connection aborted by us! This could mean that someone is trying to interfere with your connection</string>
<string name="connection_aborted_remote">Connection aborted by your contact! This could mean that someone is trying to interfere with your connection</string>
<string name="no_private_messages">No messages</string>
<string name="private_message_hint">Type message</string>
<string name="message_sent_toast">Message sent</string>
......
......@@ -59,6 +59,8 @@ import static java.util.logging.Level.WARNING;
public class ShowQrCodeFragment extends BaseEventFragment
implements QrCodeDecoder.ResultCallback {
private static final int CAMERA_MAX_RETRIES = 3;
private static final int CAMERA_RETRY_DELAY = 500; // Milliseconds
private static final Logger LOG =
Logger.getLogger(ShowQrCodeFragment.class.getName());
......@@ -194,33 +196,34 @@ public class ShowQrCodeFragment extends BaseEventFragment
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);
// Work around transient failures
for (int i = 0; i < CAMERA_MAX_RETRIES; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e2) {
LOG.info("Interrupted before second attempt");
return null;
return Camera.open();
} catch (RuntimeException e) {
LOG.warning("Error opening camera: " + e);
}
try {
return Camera.open();
} catch (RuntimeException e2) {
LOG.log(WARNING, "Error opening camera", e2);
Thread.sleep(CAMERA_RETRY_DELAY);
} catch (InterruptedException e) {
LOG.info("Interrupted while opening camera");
return null;
}
}
LOG.info("Failed to open camera");
return null;
}
@Override
protected void onPostExecute(Camera camera) {
if (camera == null) {
// TODO better solution?
Toast.makeText(getActivity(),
R.string.could_not_open_camera,
LENGTH_LONG).show();
getActivity().finish();
} else {
cameraView.start(camera, decoder, 0);
......@@ -236,17 +239,9 @@ public class ShowQrCodeFragment extends BaseEventFragment
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
......@@ -256,9 +251,9 @@ public class ShowQrCodeFragment extends BaseEventFragment
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();
getActivity().finish();
}
}
......@@ -274,8 +269,7 @@ public class ShowQrCodeFragment extends BaseEventFragment
} else if (e instanceof KeyAgreementStartedEvent) {
keyAgreementStarted();
} else if (e instanceof KeyAgreementAbortedEvent) {
KeyAgreementAbortedEvent event = (KeyAgreementAbortedEvent) e;
keyAgreementAborted(event.didRemoteAbort());
keyAgreementFailed();
}
}
......@@ -300,10 +294,9 @@ public class ShowQrCodeFragment extends BaseEventFragment
listener.runOnUiThread(new Runnable() {
@Override
public void run() {
reset();
// TODO show failure somewhere persistent?
Toast.makeText(getActivity(), R.string.connection_failed,
LENGTH_LONG).show();
getActivity().finish();
}
});
}
......@@ -327,21 +320,6 @@ public class ShowQrCodeFragment extends BaseEventFragment
});
}
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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment