some changes to qr scan logic

This commit is contained in:
Vincent Breitmoser 2015-02-28 15:58:26 +01:00
parent 2ae4d6ce05
commit 0557e0680e
4 changed files with 113 additions and 37 deletions

View File

@ -57,6 +57,9 @@ public class ImportKeysProxyActivity extends FragmentActivity {
public static final String ACTION_QR_CODE_API = OpenKeychainIntents.IMPORT_KEY_FROM_QR_CODE; public static final String ACTION_QR_CODE_API = OpenKeychainIntents.IMPORT_KEY_FROM_QR_CODE;
public static final String ACTION_SCAN_WITH_RESULT = Constants.INTENT_PREFIX + "SCAN_QR_CODE_WITH_RESULT"; public static final String ACTION_SCAN_WITH_RESULT = Constants.INTENT_PREFIX + "SCAN_QR_CODE_WITH_RESULT";
public static final String ACTION_SCAN_IMPORT = Constants.INTENT_PREFIX + "SCAN_QR_CODE_IMPORT";
public static final String EXTRA_FINGERPRINT = "fingerprint";
boolean returnResult; boolean returnResult;
@ -78,10 +81,15 @@ public class ImportKeysProxyActivity extends FragmentActivity {
// Scanning a fingerprint directly with Barcode Scanner, thus we already have scanned // Scanning a fingerprint directly with Barcode Scanner, thus we already have scanned
returnResult = false; returnResult = false;
startCertify(dataUri); processScannedContent(dataUri);
} else if (ACTION_SCAN_IMPORT.equals(action)) {
returnResult = false;
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)
.setPrompt(getString(R.string.import_qr_code_text))
.setResultDisplayDuration(0)
.initiateScan();
} else if (ACTION_SCAN_WITH_RESULT.equals(action)) { } else if (ACTION_SCAN_WITH_RESULT.equals(action)) {
// scan using xzing's Barcode Scanner and return result parcel in OpenKeychain
returnResult = true; returnResult = true;
IntentIntegrator integrator = new IntentIntegrator(this); IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES) integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)
@ -113,16 +121,16 @@ public class ImportKeysProxyActivity extends FragmentActivity {
if (requestCode == IntentIntegratorSupportV4.REQUEST_CODE) { if (requestCode == IntentIntegratorSupportV4.REQUEST_CODE) {
IntentResult scanResult = IntentIntegratorSupportV4.parseActivityResult(requestCode, IntentResult scanResult = IntentIntegratorSupportV4.parseActivityResult(requestCode,
resultCode, data); resultCode, data);
if (scanResult != null && scanResult.getFormatName() != null) {
String scannedContent = scanResult.getContents();
Log.d(Constants.TAG, "scannedContent: " + scannedContent);
startCertify(Uri.parse(scanResult.getContents())); if (scanResult == null || scanResult.getFormatName() == null) {
} else {
Log.e(Constants.TAG, "scanResult or formatName null! Should not happen!"); Log.e(Constants.TAG, "scanResult or formatName null! Should not happen!");
finish(); finish();
return;
} }
String scannedContent = scanResult.getContents();
processScannedContent(scannedContent);
return; return;
} }
// if a result has been returned, return it down to other activity // if a result has been returned, return it down to other activity
@ -134,6 +142,41 @@ public class ImportKeysProxyActivity extends FragmentActivity {
} }
} }
private void processScannedContent(String content) {
Uri uri = Uri.parse(content);
processScannedContent(uri);
}
private void processScannedContent(Uri uri) {
Log.d(Constants.TAG, "scanned: " + uri.toString());
String fingerprint = null;
// example: openpgp4fpr:73EE2314F65FA92EC2390D3A718C070100012282
if (uri.getScheme().toLowerCase(Locale.ENGLISH).equals(Constants.FINGERPRINT_SCHEME)) {
fingerprint = uri.getEncodedSchemeSpecificPart().toLowerCase(Locale.ENGLISH);
}
if (fingerprint == null) {
SingletonResult result = new SingletonResult(
SingletonResult.RESULT_ERROR, OperationResult.LogType.MSG_WRONG_QR_CODE);
Intent intent = new Intent();
intent.putExtra(SingletonResult.EXTRA_RESULT, result);
returnResult(intent);
return;
}
if (returnResult) {
Intent result = new Intent();
result.putExtra(EXTRA_FINGERPRINT, fingerprint);
setResult(RESULT_OK, result);
finish();
} else {
importKeys(fingerprint);
}
}
public void returnResult(Intent data) { public void returnResult(Intent data) {
if (returnResult) { if (returnResult) {
setResult(RESULT_OK, data); setResult(RESULT_OK, data);
@ -147,20 +190,6 @@ public class ImportKeysProxyActivity extends FragmentActivity {
} }
} }
public void startCertify(Uri dataUri) {
// example: openpgp4fpr:73EE2314F65FA92EC2390D3A718C070100012282
if (dataUri.getScheme().equals(Constants.FINGERPRINT_SCHEME)) {
String fingerprint = dataUri.getEncodedSchemeSpecificPart().toLowerCase(Locale.ENGLISH);
importKeys(fingerprint);
} else {
SingletonResult result = new SingletonResult(
SingletonResult.RESULT_ERROR, OperationResult.LogType.MSG_WRONG_QR_CODE);
Intent intent = new Intent();
intent.putExtra(SingletonResult.EXTRA_RESULT, result);
returnResult(intent);
}
}
public void importKeys(byte[] keyringData) { public void importKeys(byte[] keyringData) {
ParcelableKeyRing keyEntry = new ParcelableKeyRing(keyringData); ParcelableKeyRing keyEntry = new ParcelableKeyRing(keyringData);

View File

@ -527,7 +527,7 @@ public class KeyListFragment extends LoaderFragment
private void scanQrCode() { private void scanQrCode() {
Intent scanQrCode = new Intent(getActivity(), ImportKeysProxyActivity.class); Intent scanQrCode = new Intent(getActivity(), ImportKeysProxyActivity.class);
scanQrCode.setAction(ImportKeysProxyActivity.ACTION_SCAN_WITH_RESULT); scanQrCode.setAction(ImportKeysProxyActivity.ACTION_SCAN_IMPORT);
startActivityForResult(scanQrCode, 0); startActivityForResult(scanQrCode, 0);
} }

View File

@ -88,6 +88,8 @@ import java.util.HashMap;
public class ViewKeyActivity extends BaseActivity implements public class ViewKeyActivity extends BaseActivity implements
LoaderManager.LoaderCallbacks<Cursor> { LoaderManager.LoaderCallbacks<Cursor> {
static final int REQUEST_QR_FINGERPRINT = 1;
ExportHelper mExportHelper; ExportHelper mExportHelper;
ProviderHelper mProviderHelper; ProviderHelper mProviderHelper;
@ -122,6 +124,8 @@ public class ViewKeyActivity extends BaseActivity implements
private boolean mIsRefreshing; private boolean mIsRefreshing;
private Animation mRotate, mRotateSpin; private Animation mRotate, mRotateSpin;
private View mRefresh; private View mRefresh;
private String mFingerprint;
private long mMasterKeyId;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -380,13 +384,24 @@ public class ViewKeyActivity extends BaseActivity implements
private void scanQrCode() { private void scanQrCode() {
Intent scanQrCode = new Intent(this, ImportKeysProxyActivity.class); Intent scanQrCode = new Intent(this, ImportKeysProxyActivity.class);
scanQrCode.setAction(ImportKeysProxyActivity.ACTION_SCAN_WITH_RESULT); scanQrCode.setAction(ImportKeysProxyActivity.ACTION_SCAN_WITH_RESULT);
startActivityForResult(scanQrCode, 0); startActivityForResult(scanQrCode, REQUEST_QR_FINGERPRINT);
} }
private void certifyFingeprint(Uri dataUri) { private void certifyFingeprint(Uri dataUri) {
Intent intent = new Intent(this, CertifyFingerprintActivity.class); Intent intent = new Intent(this, CertifyFingerprintActivity.class);
intent.setData(dataUri); intent.setData(dataUri);
startCertifyIntent(intent);
}
private void certifyImmediate() {
Intent intent = new Intent(this, CertifyKeyActivity.class);
intent.putExtra(CertifyKeyActivity.EXTRA_KEY_IDS, new long[]{ mMasterKeyId });
startCertifyIntent(intent);
}
private void startCertifyIntent (Intent intent) {
// Message is received after signing is done in KeychainIntentService // Message is received after signing is done in KeychainIntentService
KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this) { KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this) {
public void handleMessage(Message message) { public void handleMessage(Message message) {
@ -456,7 +471,31 @@ public class ViewKeyActivity extends BaseActivity implements
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if a result has been returned, display a notify if (requestCode == REQUEST_QR_FINGERPRINT && resultCode == Activity.RESULT_OK) {
// If there is an EXTRA_RESULT, that's an error. Just show it.
if (data.hasExtra(OperationResult.EXTRA_RESULT)) {
OperationResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT);
result.createNotify(this).show();
return;
}
String fp = data.getStringExtra(ImportKeysProxyActivity.EXTRA_FINGERPRINT);
if (fp == null) {
Notify.createNotify(this, "Error scanning fingerprint!",
Notify.LENGTH_LONG, Notify.Style.ERROR).show();
return;
}
if (mFingerprint.equalsIgnoreCase(fp)) {
certifyImmediate();
} else {
Notify.createNotify(this, "Fingerprints did not match!",
Notify.LENGTH_LONG, Notify.Style.ERROR).show();
}
return;
}
if (data != null && data.hasExtra(OperationResult.EXTRA_RESULT)) { if (data != null && data.hasExtra(OperationResult.EXTRA_RESULT)) {
OperationResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT); OperationResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT);
result.createNotify(this).show(); result.createNotify(this).show();
@ -764,7 +803,8 @@ public class ViewKeyActivity extends BaseActivity implements
mName.setText(R.string.user_id_no_name); mName.setText(R.string.user_id_no_name);
} }
String fingerprint = KeyFormattingUtils.convertFingerprintToHex(data.getBlob(INDEX_FINGERPRINT)); mMasterKeyId = data.getLong(INDEX_MASTER_KEY_ID);
mFingerprint = KeyFormattingUtils.convertFingerprintToHex(data.getBlob(INDEX_FINGERPRINT));
mIsSecret = data.getInt(INDEX_HAS_ANY_SECRET) != 0; mIsSecret = data.getInt(INDEX_HAS_ANY_SECRET) != 0;
mHasEncrypt = data.getInt(INDEX_HAS_ENCRYPT) != 0; mHasEncrypt = data.getInt(INDEX_HAS_ENCRYPT) != 0;
@ -826,8 +866,8 @@ public class ViewKeyActivity extends BaseActivity implements
mStatusText.setText(R.string.view_key_my_key); mStatusText.setText(R.string.view_key_my_key);
mStatusImage.setVisibility(View.GONE); mStatusImage.setVisibility(View.GONE);
color = getResources().getColor(R.color.primary); color = getResources().getColor(R.color.primary);
photoTask.execute(fingerprint); photoTask.execute(mFingerprint);
loadQrCode(fingerprint); loadQrCode(mFingerprint);
mQrCodeLayout.setVisibility(View.VISIBLE); mQrCodeLayout.setVisibility(View.VISIBLE);
// and place leftOf qr code // and place leftOf qr code
@ -873,7 +913,7 @@ public class ViewKeyActivity extends BaseActivity implements
KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText, KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText,
KeyFormattingUtils.STATE_VERIFIED, R.color.icons, true); KeyFormattingUtils.STATE_VERIFIED, R.color.icons, true);
color = getResources().getColor(R.color.primary); color = getResources().getColor(R.color.primary);
photoTask.execute(fingerprint); photoTask.execute(mFingerprint);
mFab.setVisibility(View.GONE); mFab.setVisibility(View.GONE);
} else { } else {

View File

@ -100,18 +100,25 @@ public class Notify {
return createNotify(activity, activity.getString(resId), duration, style, listener, resIdAction); return createNotify(activity, activity.getString(resId), duration, style, listener, resIdAction);
} }
public static Showable createNotify(Activity activity, String msg, int duration, Style style) {
return createNotify(activity, msg, duration, style, null, 0);
}
public static Showable createNotify(Activity activity, String msg, int duration, Style style, public static Showable createNotify(Activity activity, String msg, int duration, Style style,
final ActionListener listener, int resIdAction) { final ActionListener listener, int resIdAction) {
final Snackbar bar = getSnackbar(activity) final Snackbar bar = getSnackbar(activity)
.text(msg) .text(msg);
.actionLabel(resIdAction)
.actionListener(new ActionClickListener() { if (listener != null) {
bar.actionLabel(resIdAction);
bar.actionListener(new ActionClickListener() {
@Override @Override
public void onActionClicked(Snackbar snackbar) { public void onActionClicked(Snackbar snackbar) {
listener.onAction(); listener.onAction();
} }
}); });
}
if (duration == LENGTH_INDEFINITE) { if (duration == LENGTH_INDEFINITE) {
bar.duration(SnackbarDuration.LENGTH_INDEFINITE); bar.duration(SnackbarDuration.LENGTH_INDEFINITE);