New NFC dialog design with indeterminate progress, do NFC stuff in background

This commit is contained in:
Dominik Schürmann
2015-07-03 04:58:30 +02:00
parent d6b4d4b4f2
commit d03595f64e
11 changed files with 257 additions and 99 deletions

View File

@@ -63,6 +63,11 @@ public class CreateKeyActivity extends BaseNfcActivity {
Fragment mCurrentFragment;
byte[] mScannedFingerprints;
byte[] mNfcAid;
String mNfcUserId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -72,8 +77,6 @@ public class CreateKeyActivity extends BaseNfcActivity {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
try {
handleTagDiscoveredIntent(getIntent());
} catch (CardException e) {
handleNfcError(e);
} catch (IOException e) {
handleNfcError(e);
}
@@ -142,40 +145,47 @@ public class CreateKeyActivity extends BaseNfcActivity {
}
@Override
protected void onNfcPerform() throws IOException {
protected void doNfcInBackground() throws IOException {
if (mCurrentFragment instanceof NfcListenerFragment) {
((NfcListenerFragment) mCurrentFragment).onNfcPerform();
((NfcListenerFragment) mCurrentFragment).doNfcInBackground();
return;
}
byte[] scannedFingerprints = nfcGetFingerprints();
byte[] nfcAid = nfcGetAid();
String userId = nfcGetUserId();
mScannedFingerprints = nfcGetFingerprints();
mNfcAid = nfcGetAid();
mNfcUserId = nfcGetUserId();
}
if (containsKeys(scannedFingerprints)) {
@Override
protected void onNfcPostExecute() throws IOException {
if (mCurrentFragment instanceof NfcListenerFragment) {
((NfcListenerFragment) mCurrentFragment).onNfcPostExecute();
return;
}
if (containsKeys(mScannedFingerprints)) {
try {
long masterKeyId = KeyFormattingUtils.getKeyIdFromFingerprint(scannedFingerprints);
long masterKeyId = KeyFormattingUtils.getKeyIdFromFingerprint(mScannedFingerprints);
CachedPublicKeyRing ring = new ProviderHelper(this).getCachedPublicKeyRing(masterKeyId);
ring.getMasterKeyId();
Intent intent = new Intent(this, ViewKeyActivity.class);
intent.setData(KeyRings.buildGenericKeyRingUri(masterKeyId));
intent.putExtra(ViewKeyActivity.EXTRA_NFC_AID, nfcAid);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_USER_ID, userId);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_FINGERPRINTS, scannedFingerprints);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_AID, mNfcAid);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_USER_ID, mNfcUserId);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_FINGERPRINTS, mScannedFingerprints);
startActivity(intent);
finish();
} catch (PgpKeyNotFoundException e) {
Fragment frag = CreateYubiKeyImportFragment.newInstance(
scannedFingerprints, nfcAid, userId);
mScannedFingerprints, mNfcAid, mNfcUserId);
loadFragment(frag, FragAction.TO_RIGHT);
}
} else {
Fragment frag = CreateYubiKeyBlankFragment.newInstance();
loadFragment(frag, FragAction.TO_RIGHT);
}
}
private boolean containsKeys(byte[] scannedFingerprints) {
@@ -246,7 +256,8 @@ public class CreateKeyActivity extends BaseNfcActivity {
}
interface NfcListenerFragment {
public void onNfcPerform() throws IOException;
public void doNfcInBackground() throws IOException;
public void onNfcPostExecute() throws IOException;
}
}

View File

@@ -195,7 +195,7 @@ public class CreateYubiKeyImportFragment
}
@Override
public void onNfcPerform() throws IOException {
public void doNfcInBackground() throws IOException {
mNfcFingerprints = mCreateKeyActivity.nfcGetFingerprints();
mNfcAid = mCreateKeyActivity.nfcGetAid();
@@ -204,10 +204,13 @@ public class CreateYubiKeyImportFragment
byte[] fp = new byte[20];
ByteBuffer.wrap(fp).put(mNfcFingerprints, 0, 20);
mNfcFingerprint = KeyFormattingUtils.convertFingerprintToHex(fp);
}
@Override
public void onNfcPostExecute() throws IOException {
setData();
refreshSearch();
}
@Override

View File

@@ -448,10 +448,8 @@ public class ImportKeysActivity extends BaseNfcActivity
}
@Override
protected void onNfcPerform() throws IOException {
// this displays the key or moves to the yubikey import dialogue.
super.onNfcPerform();
// either way, finish afterwards
protected void onNfcPostExecute() throws IOException {
// either way, finish after NFC AsyncTask
finish();
}
@@ -463,7 +461,7 @@ public class ImportKeysActivity extends BaseNfcActivity
}
}
public void handleResult (ImportKeyResult result) {
public void handleResult(ImportKeyResult result) {
if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(getIntent().getAction())
|| ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN.equals(getIntent().getAction())) {
Intent intent = new Intent();

View File

@@ -7,8 +7,10 @@
package org.sufficientlysecure.keychain.ui;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ViewAnimator;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
@@ -44,11 +46,15 @@ public class NfcOperationActivity extends BaseNfcActivity {
public static final String RESULT_DATA = "result_data";
public ViewAnimator vAnimator;
private RequiredInputParcel mRequiredInput;
private Intent mServiceIntent;
private static final byte[] BLANK_FINGERPRINT = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
private CryptoInputParcel mInputParcel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -56,6 +62,11 @@ public class NfcOperationActivity extends BaseNfcActivity {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setTitle(R.string.nfc_text);
vAnimator = (ViewAnimator) findViewById(R.id.view_animator);
vAnimator.setDisplayedChild(0);
Intent intent = getIntent();
Bundle data = intent.getExtras();
@@ -74,16 +85,22 @@ public class NfcOperationActivity extends BaseNfcActivity {
}
@Override
protected void onNfcPerform() throws IOException {
public void onNfcPreExecute() {
// start with indeterminate progress
vAnimator.setDisplayedChild(1);
}
CryptoInputParcel inputParcel = new CryptoInputParcel(mRequiredInput.mSignatureTime);
@Override
protected void doNfcInBackground() throws IOException {
mInputParcel = new CryptoInputParcel(mRequiredInput.mSignatureTime);
switch (mRequiredInput.mType) {
case NFC_DECRYPT: {
for (int i = 0; i < mRequiredInput.mInputData.length; i++) {
byte[] encryptedSessionKey = mRequiredInput.mInputData[i];
byte[] decryptedSessionKey = nfcDecryptSessionKey(encryptedSessionKey);
inputParcel.addCryptoData(encryptedSessionKey, decryptedSessionKey);
mInputParcel.addCryptoData(encryptedSessionKey, decryptedSessionKey);
}
break;
}
@@ -92,7 +109,7 @@ public class NfcOperationActivity extends BaseNfcActivity {
byte[] hash = mRequiredInput.mInputData[i];
int algo = mRequiredInput.mSignAlgos[i];
byte[] signedHash = nfcCalculateSignature(hash, algo);
inputParcel.addCryptoData(hash, signedHash);
mInputParcel.addCryptoData(hash, signedHash);
}
break;
}
@@ -163,7 +180,7 @@ public class NfcOperationActivity extends BaseNfcActivity {
}
// TODO: Is this really needed?
inputParcel.addCryptoData(subkeyBytes, cardSerialNumber);
mInputParcel.addCryptoData(subkeyBytes, cardSerialNumber);
}
// change PINs afterwards
@@ -177,16 +194,39 @@ public class NfcOperationActivity extends BaseNfcActivity {
}
}
}
@Override
protected void onNfcPostExecute() throws IOException {
if (mServiceIntent != null) {
CryptoInputParcelCacheService.addCryptoInputParcel(this, mServiceIntent, inputParcel);
CryptoInputParcelCacheService.addCryptoInputParcel(this, mServiceIntent, mInputParcel);
setResult(RESULT_OK, mServiceIntent);
} else {
Intent result = new Intent();
result.putExtra(NfcOperationActivity.RESULT_DATA, inputParcel);
result.putExtra(NfcOperationActivity.RESULT_DATA, mInputParcel);
setResult(RESULT_OK, result);
}
finish();
// show finish
vAnimator.setDisplayedChild(2);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// wait some 1000ms here, give the user time to appreciate the displayed finish
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// never mind
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
finish();
}
}.execute();
}
private boolean shouldPutKey(byte[] fingerprint, int idx) throws IOException {

View File

@@ -148,6 +148,10 @@ public class ViewKeyActivity extends BaseNfcActivity implements
private String mFingerprint;
private long mMasterKeyId;
private byte[] mNfcFingerprints;
private String mNfcUserId;
private byte[] mNfcAid;
@SuppressLint("InflateParams")
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -540,13 +544,17 @@ public class ViewKeyActivity extends BaseNfcActivity implements
}
@Override
protected void onNfcPerform() throws IOException {
protected void doNfcInBackground() throws IOException {
final byte[] nfcFingerprints = nfcGetFingerprints();
final String nfcUserId = nfcGetUserId();
final byte[] nfcAid = nfcGetAid();
mNfcFingerprints = nfcGetFingerprints();
mNfcUserId = nfcGetUserId();
mNfcAid = nfcGetAid();
}
long yubiKeyId = KeyFormattingUtils.getKeyIdFromFingerprint(nfcFingerprints);
@Override
protected void onNfcPostExecute() throws IOException {
long yubiKeyId = KeyFormattingUtils.getKeyIdFromFingerprint(mNfcFingerprints);
try {
@@ -557,7 +565,7 @@ public class ViewKeyActivity extends BaseNfcActivity implements
// if the master key of that key matches this one, just show the yubikey dialog
if (KeyFormattingUtils.convertFingerprintToHex(candidateFp).equals(mFingerprint)) {
showYubiKeyFragment(nfcFingerprints, nfcUserId, nfcAid);
showYubiKeyFragment(mNfcFingerprints, mNfcUserId, mNfcAid);
return;
}
@@ -570,9 +578,9 @@ public class ViewKeyActivity extends BaseNfcActivity implements
Intent intent = new Intent(
ViewKeyActivity.this, ViewKeyActivity.class);
intent.setData(KeyRings.buildGenericKeyRingUri(masterKeyId));
intent.putExtra(ViewKeyActivity.EXTRA_NFC_AID, nfcAid);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_USER_ID, nfcUserId);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_FINGERPRINTS, nfcFingerprints);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_AID, mNfcAid);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_USER_ID, mNfcUserId);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_FINGERPRINTS, mNfcFingerprints);
startActivity(intent);
finish();
}
@@ -586,15 +594,14 @@ public class ViewKeyActivity extends BaseNfcActivity implements
public void onAction() {
Intent intent = new Intent(
ViewKeyActivity.this, CreateKeyActivity.class);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_AID, nfcAid);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_USER_ID, nfcUserId);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_FINGERPRINTS, nfcFingerprints);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_AID, mNfcAid);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_USER_ID, mNfcUserId);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_FINGERPRINTS, mNfcFingerprints);
startActivity(intent);
finish();
}
}, R.string.snack_yubikey_import).show();
}
}
public void showYubiKeyFragment(

View File

@@ -30,6 +30,7 @@ import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.IsoDep;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
@@ -74,6 +75,10 @@ public abstract class BaseNfcActivity extends BaseActivity {
private static final int TIMEOUT = 100000;
private byte[] mNfcFingerprints;
private String mNfcUserId;
private byte[] mNfcAid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -91,28 +96,59 @@ public abstract class BaseNfcActivity extends BaseActivity {
* All new NFC Intents which are delivered to this activity are handled here
*/
@Override
public void onNewIntent(Intent intent) {
public void onNewIntent(final Intent intent) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
try {
handleTagDiscoveredIntent(intent);
} catch (CardException e) {
handleNfcError(e);
} catch (IOException e) {
handleNfcError(e);
}
// Actual NFC operations are executed in doInBackground to not block the UI thread
new AsyncTask<Void, Void, Exception>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
onNfcPreExecute();
}
@Override
protected Exception doInBackground(Void... params) {
try {
handleTagDiscoveredIntent(intent);
} catch (CardException e) {
return e;
} catch (IOException e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception exception) {
super.onPostExecute(exception);
if (exception != null) {
handleNfcError(exception);
return;
}
try {
onNfcPostExecute();
} catch (IOException e) {
handleNfcError(e);
}
}
}.execute();
}
}
public void handleNfcError(IOException e) {
public void handleNfcError(Exception e) {
Log.e(Constants.TAG, "nfc error", e);
Notify.create(this, getString(R.string.error_nfc, e.getMessage()), Style.WARN).show();
}
public void handleNfcError(CardException e) {
Log.e(Constants.TAG, "card error", e);
short status = e.getResponseCode();
short status;
if (e instanceof CardException) {
status = ((CardException) e).getResponseCode();
} else {
status = -1;
}
// When entering a PIN, a status of 63CX indicates X attempts remaining.
if ((status & (short)0xFFF0) == 0x63C0) {
Notify.create(this, getString(R.string.error_pin, status & 0x000F), Style.WARN).show();
@@ -313,20 +349,25 @@ public abstract class BaseNfcActivity extends BaseActivity {
mPw1ValidatedForDecrypt = false;
mPw3Validated = false;
onNfcPerform();
doNfcInBackground();
mIsoDep.close();
mIsoDep = null;
}
protected void onNfcPerform() throws IOException {
protected void onNfcPreExecute() {
}
final byte[] nfcFingerprints = nfcGetFingerprints();
final String nfcUserId = nfcGetUserId();
final byte[] nfcAid = nfcGetAid();
protected void doNfcInBackground() throws IOException {
mNfcFingerprints = nfcGetFingerprints();
mNfcUserId = nfcGetUserId();
mNfcAid = nfcGetAid();
}
final long subKeyId = KeyFormattingUtils.getKeyIdFromFingerprint(nfcFingerprints);
protected void onNfcPostExecute() throws IOException {
final long subKeyId = KeyFormattingUtils.getKeyIdFromFingerprint(mNfcFingerprints);
try {
CachedPublicKeyRing ring = new ProviderHelper(this).getCachedPublicKeyRing(
@@ -335,18 +376,17 @@ public abstract class BaseNfcActivity extends BaseActivity {
Intent intent = new Intent(this, ViewKeyActivity.class);
intent.setData(KeyRings.buildGenericKeyRingUri(masterKeyId));
intent.putExtra(ViewKeyActivity.EXTRA_NFC_AID, nfcAid);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_USER_ID, nfcUserId);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_FINGERPRINTS, nfcFingerprints);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_AID, mNfcAid);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_USER_ID, mNfcUserId);
intent.putExtra(ViewKeyActivity.EXTRA_NFC_FINGERPRINTS, mNfcFingerprints);
startActivity(intent);
} catch (PgpKeyNotFoundException e) {
Intent intent = new Intent(this, CreateKeyActivity.class);
intent.putExtra(CreateKeyActivity.EXTRA_NFC_AID, nfcAid);
intent.putExtra(CreateKeyActivity.EXTRA_NFC_USER_ID, nfcUserId);
intent.putExtra(CreateKeyActivity.EXTRA_NFC_FINGERPRINTS, nfcFingerprints);
intent.putExtra(CreateKeyActivity.EXTRA_NFC_AID, mNfcAid);
intent.putExtra(CreateKeyActivity.EXTRA_NFC_USER_ID, mNfcUserId);
intent.putExtra(CreateKeyActivity.EXTRA_NFC_FINGERPRINTS, mNfcFingerprints);
startActivity(intent);
}
}
/** Return the key id from application specific data stored on tag, or null
@@ -608,7 +648,7 @@ public abstract class BaseNfcActivity extends BaseActivity {
* conformance to the card's requirements for key length.
*
* @param pw For PW1, this is 0x81. For PW3 (Admin PIN), mode is 0x83.
* @param newPinString The new PW1 or PW3.
* @param newPin The new PW1 or PW3.
*/
public void nfcModifyPIN(int pw, byte[] newPin) throws IOException {
final int MAX_PW1_LENGTH_INDEX = 1;