aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2015-03-24 15:29:02 +0100
committerVincent Breitmoser <valodim@mugenguild.com>2015-03-24 15:29:58 +0100
commitf9ef1160ca764d508dafcb45fbf65f18911fff9c (patch)
tree816564326b238848669d7a0713f48c84791cd13b /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java
parentfe32e7bff4e724d37903d07cd3b4f0287ec85879 (diff)
parent8b4388e1a2d5cb2b7f0524c18ad3383e3375ba7b (diff)
downloadopen-keychain-f9ef1160ca764d508dafcb45fbf65f18911fff9c.tar.gz
open-keychain-f9ef1160ca764d508dafcb45fbf65f18911fff9c.tar.bz2
open-keychain-f9ef1160ca764d508dafcb45fbf65f18911fff9c.zip
Merge branch 'development' into linked-identities
Conflicts: OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java265
1 files changed, 71 insertions, 194 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java
index 70c590728..435a455dc 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java
@@ -21,18 +21,12 @@ package org.sufficientlysecure.keychain.ui;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
-import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
-import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
-import android.nfc.NdefMessage;
-import android.nfc.NdefRecord;
-import android.nfc.NfcAdapter;
-import android.nfc.NfcEvent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
@@ -40,7 +34,6 @@ import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.provider.ContactsContract;
-import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.LoaderManager;
@@ -59,9 +52,7 @@ import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
-
import com.getbase.floatingactionbutton.FloatingActionButton;
-
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
@@ -76,6 +67,8 @@ import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler;
import org.sufficientlysecure.keychain.ui.linked.LinkedIdWizard;
import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler.MessageStatus;
+import org.sufficientlysecure.keychain.service.PassphraseCacheService;
+import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment;
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils.State;
@@ -84,6 +77,7 @@ import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
import org.sufficientlysecure.keychain.util.ContactHelper;
import org.sufficientlysecure.keychain.util.ExportHelper;
import org.sufficientlysecure.keychain.util.Log;
+import org.sufficientlysecure.keychain.util.NfcHelper;
import org.sufficientlysecure.keychain.util.Preferences;
import java.util.ArrayList;
@@ -93,8 +87,8 @@ public class ViewKeyActivity extends BaseActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
static final int REQUEST_QR_FINGERPRINT = 1;
- static final int REQUEST_DELETE= 2;
- static final int REQUEST_EXPORT= 3;
+ static final int REQUEST_DELETE = 2;
+ static final int REQUEST_EXPORT = 3;
ExportHelper mExportHelper;
ProviderHelper mProviderHelper;
@@ -115,11 +109,7 @@ public class ViewKeyActivity extends BaseActivity implements
private CardView mQrCodeLayout;
// NFC
- private NfcAdapter mNfcAdapter;
- private NfcAdapter.CreateNdefMessageCallback mNdefCallback;
- private NfcAdapter.OnNdefPushCompleteCallback mNdefCompleteCallback;
- private byte[] mNfcKeyringBytes;
- private static final int NFC_SENT = 1;
+ private NfcHelper mNfcHelper;
private static final int LOADER_ID_UNIFIED = 0;
@@ -256,7 +246,7 @@ public class ViewKeyActivity extends BaseActivity implements
mActionNfc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
- invokeNfcBeam();
+ mNfcHelper.invokeNfcBeam();
}
});
@@ -264,7 +254,8 @@ public class ViewKeyActivity extends BaseActivity implements
// or start new ones.
getSupportLoaderManager().initLoader(LOADER_ID_UNIFIED, null, this);
- initNfc(mDataUri);
+ mNfcHelper = new NfcHelper(this, mProviderHelper);
+ mNfcHelper.initNfc(mDataUri);
}
@@ -291,31 +282,31 @@ public class ViewKeyActivity extends BaseActivity implements
return true;
}
case R.id.menu_key_view_export_file: {
- Intent mIntent = new Intent(this,PassphraseDialogActivity.class);
- long keyId=0;
try {
- keyId = new ProviderHelper(this)
- .getCachedPublicKeyRing(mDataUri)
- .extractOrGetMasterKeyId();
- } catch (PgpKeyNotFoundException e) {
- e.printStackTrace();
+ if (PassphraseCacheService.getCachedPassphrase(this, mMasterKeyId, mMasterKeyId) != null) {
+ exportToFile(mDataUri, mExportHelper, mProviderHelper);
+ return true;
+ }
+
+ startPassphraseActivity(REQUEST_EXPORT);
+ } catch (PassphraseCacheService.KeyNotFoundException e) {
+ // This happens when the master key is stripped
+ exportToFile(mDataUri, mExportHelper, mProviderHelper);
}
- mIntent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID,keyId);
- startActivityForResult(mIntent,REQUEST_EXPORT);
return true;
}
case R.id.menu_key_view_delete: {
- Intent mIntent = new Intent(this,PassphraseDialogActivity.class);
- long keyId=0;
try {
- keyId = new ProviderHelper(this)
- .getCachedPublicKeyRing(mDataUri)
- .extractOrGetMasterKeyId();
- } catch (PgpKeyNotFoundException e) {
- e.printStackTrace();
+ if (PassphraseCacheService.getCachedPassphrase(this, mMasterKeyId, mMasterKeyId) != null) {
+ deleteKey();
+ return true;
+ }
+
+ startPassphraseActivity(REQUEST_DELETE);
+ } catch (PassphraseCacheService.KeyNotFoundException e) {
+ // This happens when the master key is stripped
+ deleteKey();
}
- mIntent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID,keyId);
- startActivityForResult(mIntent,REQUEST_DELETE);
return true;
}
case R.id.menu_key_view_advanced: {
@@ -328,7 +319,7 @@ public class ViewKeyActivity extends BaseActivity implements
try {
updateFromKeyserver(mDataUri, mProviderHelper);
} catch (ProviderHelper.NotFoundException e) {
- Notify.showNotify(this, R.string.error_key_not_found, Notify.Style.ERROR);
+ Notify.create(this, R.string.error_key_not_found, Notify.Style.ERROR).show();
}
return true;
}
@@ -364,41 +355,6 @@ public class ViewKeyActivity extends BaseActivity implements
return true;
}
- @TargetApi(Build.VERSION_CODES.LOLLIPOP)
- private void invokeNfcBeam() {
- // Check if device supports NFC
- if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
- Notify.createNotify(this, R.string.no_nfc_support, Notify.LENGTH_LONG, Notify.Style.ERROR).show();
- return;
- }
- // Check for available NFC Adapter
- mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
- if (mNfcAdapter == null || !mNfcAdapter.isEnabled()) {
- Notify.createNotify(this, R.string.error_nfc_needed, Notify.LENGTH_LONG, Notify.Style.ERROR, new Notify.ActionListener() {
- @Override
- public void onAction() {
- Intent intentSettings = new Intent(Settings.ACTION_NFC_SETTINGS);
- startActivity(intentSettings);
- }
- }, R.string.menu_nfc_preferences).show();
-
- return;
- }
-
- if (!mNfcAdapter.isNdefPushEnabled()) {
- Notify.createNotify(this, R.string.error_beam_needed, Notify.LENGTH_LONG, Notify.Style.ERROR, new Notify.ActionListener() {
- @Override
- public void onAction() {
- Intent intentSettings = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
- startActivity(intentSettings);
- }
- }, R.string.menu_beam_preferences).show();
-
- return;
- }
-
- mNfcAdapter.invokeBeam(this);
- }
private void scanQrCode() {
Intent scanQrCode = new Intent(this, ImportKeysProxyActivity.class);
@@ -415,7 +371,7 @@ public class ViewKeyActivity extends BaseActivity implements
private void certifyImmediate() {
Intent intent = new Intent(this, CertifyKeyActivity.class);
- intent.putExtra(CertifyKeyActivity.EXTRA_KEY_IDS, new long[]{mMasterKeyId});
+ intent.putExtra(CertifyKeyActivity.EXTRA_KEY_IDS, new long[] {mMasterKeyId});
startCertifyIntent(intent);
}
@@ -464,22 +420,32 @@ public class ViewKeyActivity extends BaseActivity implements
ActivityCompat.startActivity(this, qrCodeIntent, opts);
}
- private void exportToFile(Uri dataUri, ExportHelper exportHelper, ProviderHelper providerHelper)
- throws ProviderHelper.NotFoundException {
- Uri baseUri = KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri);
-
- HashMap<String, Object> data = providerHelper.getGenericData(
- baseUri,
- new String[]{KeychainContract.Keys.MASTER_KEY_ID, KeychainContract.KeyRings.HAS_SECRET},
- new int[]{ProviderHelper.FIELD_TYPE_INTEGER, ProviderHelper.FIELD_TYPE_INTEGER});
+ private void startPassphraseActivity(int requestCode) {
+ Intent intent = new Intent(this, PassphraseDialogActivity.class);
+ intent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID, mMasterKeyId);
+ startActivityForResult(intent, requestCode);
+ }
- exportHelper.showExportKeysDialog(
- new long[]{(Long) data.get(KeychainContract.KeyRings.MASTER_KEY_ID)},
- Constants.Path.APP_DIR_FILE, ((Long) data.get(KeychainContract.KeyRings.HAS_SECRET) != 0)
- );
+ private void exportToFile(Uri dataUri, ExportHelper exportHelper, ProviderHelper providerHelper) {
+ try {
+ Uri baseUri = KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri);
+
+ HashMap<String, Object> data = providerHelper.getGenericData(
+ baseUri,
+ new String[] {KeychainContract.Keys.MASTER_KEY_ID, KeychainContract.KeyRings.HAS_SECRET},
+ new int[] {ProviderHelper.FIELD_TYPE_INTEGER, ProviderHelper.FIELD_TYPE_INTEGER});
+
+ exportHelper.showExportKeysDialog(
+ new long[] {(Long) data.get(KeychainContract.KeyRings.MASTER_KEY_ID)},
+ Constants.Path.APP_DIR_FILE, ((Long) data.get(KeychainContract.KeyRings.HAS_SECRET) != 0)
+ );
+ } catch (ProviderHelper.NotFoundException e) {
+ Notify.create(this, R.string.error_key_not_found, Notify.Style.ERROR).show();
+ Log.e(Constants.TAG, "Key not found", e);
+ }
}
- private void deleteKey(Uri dataUri, ExportHelper exportHelper) {
+ private void deleteKey() {
// Message is received after key is deleted
Handler returnHandler = new Handler() {
@Override
@@ -491,7 +457,11 @@ public class ViewKeyActivity extends BaseActivity implements
}
};
- exportHelper.deleteKey(dataUri, returnHandler);
+ // Create a new Messenger for the communication back
+ Messenger messenger = new Messenger(returnHandler);
+ DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger,
+ new long[] {mMasterKeyId});
+ deleteKeyDialog.show(getSupportFragmentManager(), "deleteKeyDialog");
}
@Override
@@ -507,32 +477,27 @@ public class ViewKeyActivity extends BaseActivity implements
String fp = data.getStringExtra(ImportKeysProxyActivity.EXTRA_FINGERPRINT);
if (fp == null) {
- Notify.createNotify(this, "Error scanning fingerprint!",
+ Notify.create(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.create(this, "Fingerprints did not match!",
Notify.LENGTH_LONG, Notify.Style.ERROR).show();
}
return;
}
- if (requestCode == REQUEST_DELETE && resultCode == Activity.RESULT_OK){
- deleteKey(mDataUri, mExportHelper);
- }
- if (requestCode == REQUEST_EXPORT && resultCode == Activity.RESULT_OK){
- try {
- exportToFile(mDataUri, mExportHelper, mProviderHelper);
- } catch (ProviderHelper.NotFoundException e) {
- Notify.showNotify(this, R.string.error_key_not_found, Notify.Style.ERROR);
- Log.e(Constants.TAG, "Key not found", e);
- }
+ if (requestCode == REQUEST_DELETE && resultCode == Activity.RESULT_OK) {
+ deleteKey();
}
+ if (requestCode == REQUEST_EXPORT && resultCode == Activity.RESULT_OK) {
+ exportToFile(mDataUri, mExportHelper, mProviderHelper);
+ }
if (data != null && data.hasExtra(OperationResult.EXTRA_RESULT)) {
OperationResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT);
@@ -545,14 +510,14 @@ public class ViewKeyActivity extends BaseActivity implements
private void encrypt(Uri dataUri, boolean text) {
// If there is no encryption key, don't bother.
if (!mHasEncrypt) {
- Notify.showNotify(this, R.string.error_no_encrypt_subkey, Notify.Style.ERROR);
+ Notify.create(this, R.string.error_no_encrypt_subkey, Notify.Style.ERROR).show();
return;
}
try {
long keyId = new ProviderHelper(this)
.getCachedPublicKeyRing(dataUri)
.extractOrGetMasterKeyId();
- long[] encryptionKeyIds = new long[]{keyId};
+ long[] encryptionKeyIds = new long[] {keyId};
Intent intent;
if (text) {
intent = new Intent(this, EncryptTextActivity.class);
@@ -690,98 +655,9 @@ public class ViewKeyActivity extends BaseActivity implements
loadTask.execute();
}
- /**
- * NFC: Initialize NFC sharing if OS and device supports it
- */
- @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
- private void initNfc(final Uri dataUri) {
- // check if NFC Beam is supported (>= Android 4.1)
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
-
- // Implementation for the CreateNdefMessageCallback interface
- mNdefCallback = new NfcAdapter.CreateNdefMessageCallback() {
- @Override
- public NdefMessage createNdefMessage(NfcEvent event) {
- /*
- * When a device receives a push with an AAR in it, the application specified in the AAR is
- * guaranteed to run. The AAR overrides the tag dispatch system. You can add it back in to
- * guarantee that this activity starts when receiving a beamed message. For now, this code
- * uses the tag dispatch system.
- */
- return new NdefMessage(NdefRecord.createMime(Constants.NFC_MIME,
- mNfcKeyringBytes), NdefRecord.createApplicationRecord(Constants.PACKAGE_NAME));
- }
- };
-
- // Implementation for the OnNdefPushCompleteCallback interface
- mNdefCompleteCallback = new NfcAdapter.OnNdefPushCompleteCallback() {
- @Override
- public void onNdefPushComplete(NfcEvent event) {
- // A handler is needed to send messages to the activity when this
- // callback occurs, because it happens from a binder thread
- mNfcHandler.obtainMessage(NFC_SENT).sendToTarget();
- }
- };
-
- // Check for available NFC Adapter
- mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
- if (mNfcAdapter != null) {
- /*
- * Retrieve mNfcKeyringBytes here asynchronously (to not block the UI)
- * and init nfc adapter afterwards.
- * mNfcKeyringBytes can not be retrieved in createNdefMessage, because this process
- * has no permissions to query the Uri.
- */
- AsyncTask<Void, Void, Void> initTask =
- new AsyncTask<Void, Void, Void>() {
- protected Void doInBackground(Void... unused) {
- try {
- Uri blobUri =
- KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
- mNfcKeyringBytes = (byte[]) mProviderHelper.getGenericData(
- blobUri,
- KeychainContract.KeyRingData.KEY_RING_DATA,
- ProviderHelper.FIELD_TYPE_BLOB);
- } catch (ProviderHelper.NotFoundException e) {
- Log.e(Constants.TAG, "key not found!", e);
- }
-
- // no AsyncTask return (Void)
- return null;
- }
-
- protected void onPostExecute(Void unused) {
- // Register callback to set NDEF message
- mNfcAdapter.setNdefPushMessageCallback(mNdefCallback,
- ViewKeyActivity.this);
- // Register callback to listen for message-sent success
- mNfcAdapter.setOnNdefPushCompleteCallback(mNdefCompleteCallback,
- ViewKeyActivity.this);
- }
- };
-
- initTask.execute();
- }
- }
- }
-
- /**
- * NFC: This handler receives a message from onNdefPushComplete
- */
- private final Handler mNfcHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case NFC_SENT:
- Notify.showNotify(
- ViewKeyActivity.this, R.string.nfc_successful, Notify.Style.INFO);
- break;
- }
- }
- };
// These are the rows that we will retrieve.
- static final String[] PROJECTION = new String[]{
+ static final String[] PROJECTION = new String[] {
KeychainContract.KeyRings._ID,
KeychainContract.KeyRings.MASTER_KEY_ID,
KeychainContract.KeyRings.USER_ID,
@@ -849,9 +725,9 @@ public class ViewKeyActivity extends BaseActivity implements
startFragment(mIsSecret, fpData);
// get name, email, and comment from USER_ID
- String[] mainUserId = KeyRing.splitUserId(data.getString(INDEX_USER_ID));
- if (mainUserId[0] != null) {
- mName.setText(mainUserId[0]);
+ KeyRing.UserId mainUserId = KeyRing.splitUserId(data.getString(INDEX_USER_ID));
+ if (mainUserId.name != null) {
+ mName.setText(mainUserId.name);
} else {
mName.setText(R.string.user_id_no_name);
}
@@ -892,6 +768,7 @@ public class ViewKeyActivity extends BaseActivity implements
} else if (mIsExpired) {
if (mIsSecret) {
mStatusText.setText(R.string.view_key_expired_secret);
+ mName.setText(mainUserId.name);
} else {
mStatusText.setText(R.string.view_key_expired);
}