mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-12-25 00:18:51 -05:00
some changes to qr scan logic
This commit is contained in:
parent
2ae4d6ce05
commit
0557e0680e
@ -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_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;
|
||||
|
||||
@ -78,10 +81,15 @@ public class ImportKeysProxyActivity extends FragmentActivity {
|
||||
// Scanning a fingerprint directly with Barcode Scanner, thus we already have scanned
|
||||
|
||||
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)) {
|
||||
// scan using xzing's Barcode Scanner and return result parcel in OpenKeychain
|
||||
|
||||
returnResult = true;
|
||||
IntentIntegrator integrator = new IntentIntegrator(this);
|
||||
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)
|
||||
@ -113,16 +121,16 @@ public class ImportKeysProxyActivity extends FragmentActivity {
|
||||
if (requestCode == IntentIntegratorSupportV4.REQUEST_CODE) {
|
||||
IntentResult scanResult = IntentIntegratorSupportV4.parseActivityResult(requestCode,
|
||||
resultCode, data);
|
||||
if (scanResult != null && scanResult.getFormatName() != null) {
|
||||
String scannedContent = scanResult.getContents();
|
||||
Log.d(Constants.TAG, "scannedContent: " + scannedContent);
|
||||
|
||||
startCertify(Uri.parse(scanResult.getContents()));
|
||||
} else {
|
||||
if (scanResult == null || scanResult.getFormatName() == null) {
|
||||
Log.e(Constants.TAG, "scanResult or formatName null! Should not happen!");
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
String scannedContent = scanResult.getContents();
|
||||
processScannedContent(scannedContent);
|
||||
|
||||
return;
|
||||
}
|
||||
// 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) {
|
||||
if (returnResult) {
|
||||
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) {
|
||||
|
||||
ParcelableKeyRing keyEntry = new ParcelableKeyRing(keyringData);
|
||||
|
@ -527,7 +527,7 @@ public class KeyListFragment extends LoaderFragment
|
||||
|
||||
private void scanQrCode() {
|
||||
Intent scanQrCode = new Intent(getActivity(), ImportKeysProxyActivity.class);
|
||||
scanQrCode.setAction(ImportKeysProxyActivity.ACTION_SCAN_WITH_RESULT);
|
||||
scanQrCode.setAction(ImportKeysProxyActivity.ACTION_SCAN_IMPORT);
|
||||
startActivityForResult(scanQrCode, 0);
|
||||
}
|
||||
|
||||
|
@ -88,6 +88,8 @@ import java.util.HashMap;
|
||||
public class ViewKeyActivity extends BaseActivity implements
|
||||
LoaderManager.LoaderCallbacks<Cursor> {
|
||||
|
||||
static final int REQUEST_QR_FINGERPRINT = 1;
|
||||
|
||||
ExportHelper mExportHelper;
|
||||
ProviderHelper mProviderHelper;
|
||||
|
||||
@ -122,6 +124,8 @@ public class ViewKeyActivity extends BaseActivity implements
|
||||
private boolean mIsRefreshing;
|
||||
private Animation mRotate, mRotateSpin;
|
||||
private View mRefresh;
|
||||
private String mFingerprint;
|
||||
private long mMasterKeyId;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -380,13 +384,24 @@ public class ViewKeyActivity extends BaseActivity implements
|
||||
private void scanQrCode() {
|
||||
Intent scanQrCode = new Intent(this, ImportKeysProxyActivity.class);
|
||||
scanQrCode.setAction(ImportKeysProxyActivity.ACTION_SCAN_WITH_RESULT);
|
||||
startActivityForResult(scanQrCode, 0);
|
||||
startActivityForResult(scanQrCode, REQUEST_QR_FINGERPRINT);
|
||||
}
|
||||
|
||||
private void certifyFingeprint(Uri dataUri) {
|
||||
Intent intent = new Intent(this, CertifyFingerprintActivity.class);
|
||||
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
|
||||
KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this) {
|
||||
public void handleMessage(Message message) {
|
||||
@ -456,7 +471,31 @@ public class ViewKeyActivity extends BaseActivity implements
|
||||
|
||||
@Override
|
||||
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)) {
|
||||
OperationResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT);
|
||||
result.createNotify(this).show();
|
||||
@ -764,7 +803,8 @@ public class ViewKeyActivity extends BaseActivity implements
|
||||
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;
|
||||
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);
|
||||
mStatusImage.setVisibility(View.GONE);
|
||||
color = getResources().getColor(R.color.primary);
|
||||
photoTask.execute(fingerprint);
|
||||
loadQrCode(fingerprint);
|
||||
photoTask.execute(mFingerprint);
|
||||
loadQrCode(mFingerprint);
|
||||
mQrCodeLayout.setVisibility(View.VISIBLE);
|
||||
|
||||
// and place leftOf qr code
|
||||
@ -873,7 +913,7 @@ public class ViewKeyActivity extends BaseActivity implements
|
||||
KeyFormattingUtils.setStatusImage(this, mStatusImage, mStatusText,
|
||||
KeyFormattingUtils.STATE_VERIFIED, R.color.icons, true);
|
||||
color = getResources().getColor(R.color.primary);
|
||||
photoTask.execute(fingerprint);
|
||||
photoTask.execute(mFingerprint);
|
||||
|
||||
mFab.setVisibility(View.GONE);
|
||||
} else {
|
||||
|
@ -100,18 +100,25 @@ public class Notify {
|
||||
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,
|
||||
final ActionListener listener, int resIdAction) {
|
||||
|
||||
final Snackbar bar = getSnackbar(activity)
|
||||
.text(msg)
|
||||
.actionLabel(resIdAction)
|
||||
.actionListener(new ActionClickListener() {
|
||||
@Override
|
||||
public void onActionClicked(Snackbar snackbar) {
|
||||
listener.onAction();
|
||||
}
|
||||
});
|
||||
.text(msg);
|
||||
|
||||
if (listener != null) {
|
||||
bar.actionLabel(resIdAction);
|
||||
bar.actionListener(new ActionClickListener() {
|
||||
@Override
|
||||
public void onActionClicked(Snackbar snackbar) {
|
||||
listener.onAction();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (duration == LENGTH_INDEFINITE) {
|
||||
bar.duration(SnackbarDuration.LENGTH_INDEFINITE);
|
||||
|
Loading…
Reference in New Issue
Block a user