From 9f67b0fe54ed4045f8db2d377b90d15473a19912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 23 Sep 2014 23:04:18 +0200 Subject: [PATCH 1/9] DecryptTextActivity rework --- OpenKeychain/src/main/AndroidManifest.xml | 15 +- .../remote/ui/AccountSettingsActivity.java | 3 +- .../service/results/OperationResult.java | 4 +- .../service/results/SingletonResult.java | 2 +- .../keychain/ui/CertifyKeyFragment.java | 4 +- .../keychain/ui/DecryptActivity.java | 14 +- .../keychain/ui/DecryptFilesFragment.java | 2 - .../keychain/ui/DecryptTextActivity.java | 133 +++++++++++------- .../keychain/ui/EditKeyFragment.java | 2 +- OpenKeychain/src/main/res/values/strings.xml | 3 +- 10 files changed, 116 insertions(+), 66 deletions(-) diff --git a/OpenKeychain/src/main/AndroidManifest.xml b/OpenKeychain/src/main/AndroidManifest.xml index 9116a91b0..c78e80c19 100644 --- a/OpenKeychain/src/main/AndroidManifest.xml +++ b/OpenKeychain/src/main/AndroidManifest.xml @@ -96,7 +96,8 @@ + android:label="@string/title_create_key" + android:parentActivityName=".ui.KeyListActivity"> @@ -198,7 +199,11 @@ android:name=".ui.DecryptTextActivity" android:configChanges="orientation|screenSize|keyboardHidden|keyboard" android:label="@string/title_decrypt" - android:windowSoftInputMode="stateHidden"> + android:windowSoftInputMode="stateHidden" + android:parentActivityName=".ui.DecryptActivity"> + @@ -221,7 +226,11 @@ android:name=".ui.DecryptFilesActivity" android:configChanges="orientation|screenSize|keyboardHidden|keyboard" android:label="@string/title_decrypt" - android:windowSoftInputMode="stateHidden"> + android:windowSoftInputMode="stateHidden" + android:parentActivityName=".ui.DecryptActivity"> + diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java index 26063fc66..b43dec2f1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsActivity.java @@ -107,9 +107,8 @@ public class AccountSettingsActivity extends ActionBarActivity { private void save() { new ProviderHelper(this).updateApiAccount(mAccountUri, mAccountSettingsFragment.getAccSettings()); - // TODO: show "account saved" instead of "operation succeeded" SingletonResult result = new SingletonResult( - SingletonResult.RESULT_OK, LogLevel.OK, LogType.MSG_ACC_SAVED); + SingletonResult.RESULT_OK, LogType.MSG_ACC_SAVED); Intent intent = new Intent(); intent.putExtra(SingletonResult.EXTRA_RESULT, result); setResult(RESULT_OK, intent); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/OperationResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/OperationResult.java index beaa6f2ba..b0a255162 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/OperationResult.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/OperationResult.java @@ -519,7 +519,9 @@ public abstract class OperationResult implements Parcelable { MSG_CRT_UPLOAD_SUCCESS (LogLevel.OK, R.string.msg_crt_upload_success), MSG_CRT_SUCCESS (LogLevel.OK, R.string.msg_crt_success), - MSG_ACC_SAVED (LogLevel.INFO, R.string.api_settings_save) + MSG_ACC_SAVED (LogLevel.INFO, R.string.api_settings_save_msg), + + MSG_NO_VALID_ENC (LogLevel.ERROR, R.string.error_invalid_data) ; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/SingletonResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/SingletonResult.java index 642e81246..430c739d9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/SingletonResult.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/SingletonResult.java @@ -34,7 +34,7 @@ public class SingletonResult extends OperationResult { super(source); } - public SingletonResult(int result, LogLevel level, LogType reason) { + public SingletonResult(int result, LogType reason) { super(result, new OperationLog()); // Prepare the log mLog.add(reason, 0); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java index 438b56eb0..5eec8454a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyFragment.java @@ -330,7 +330,7 @@ public class CertifyKeyFragment extends LoaderFragment if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { SingletonResult result = new SingletonResult( - SingletonResult.RESULT_OK, LogLevel.OK, LogType.MSG_CRT_SUCCESS); + SingletonResult.RESULT_OK, LogType.MSG_CRT_SUCCESS); Intent intent = new Intent(); intent.putExtra(SingletonResult.EXTRA_RESULT, result); mActivity.setResult(CertifyKeyActivity.RESULT_OK, intent); @@ -384,7 +384,7 @@ public class CertifyKeyFragment extends LoaderFragment if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { SingletonResult result = new SingletonResult(SingletonResult.RESULT_OK, - LogLevel.OK, LogType.MSG_CRT_UPLOAD_SUCCESS); + LogType.MSG_CRT_UPLOAD_SUCCESS); Intent intent = new Intent(); intent.putExtra(SingletonResult.EXTRA_RESULT, result); mActivity.setResult(CertifyKeyActivity.RESULT_OK, intent); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java index 37382051b..2f3978dad 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java @@ -22,6 +22,7 @@ import android.os.Bundle; import android.view.View; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.service.results.OperationResult; public class DecryptActivity extends DrawerActivity { @@ -50,8 +51,19 @@ public class DecryptActivity extends DrawerActivity { public void onClick(View v) { Intent clipboardDecrypt = new Intent(DecryptActivity.this, DecryptTextActivity.class); clipboardDecrypt.setAction(DecryptTextActivity.ACTION_DECRYPT_FROM_CLIPBOARD); - startActivity(clipboardDecrypt); + startActivityForResult(clipboardDecrypt, 0); } }); } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // if a result has been returned, display a notify + if (data != null && data.hasExtra(OperationResult.EXTRA_RESULT)) { + OperationResult result = data.getParcelableExtra(OperationResult.EXTRA_RESULT); + result.createNotify(this).show(); + } else { + super.onActivityResult(requestCode, resultCode, data); + } + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesFragment.java index deb994808..875a12f4e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesFragment.java @@ -48,7 +48,6 @@ import java.io.File; public class DecryptFilesFragment extends DecryptFragment { public static final String ARG_URI = "uri"; - // public static final String ARG_FROM_VIEW_INTENT = "view_intent"; public static final String ARG_OPEN_DIRECTLY = "open_directly"; private static final int REQUEST_CODE_INPUT = 0x00007003; @@ -71,7 +70,6 @@ public class DecryptFilesFragment extends DecryptFragment { Bundle args = new Bundle(); args.putParcelable(ARG_URI, uri); -// args.putBoolean(ARG_FROM_VIEW_INTENT, fromViewIntent); args.putBoolean(ARG_OPEN_DIRECTLY, openDirectly); frag.setArguments(args); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptTextActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptTextActivity.java index 1bbe58f07..582c988df 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptTextActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptTextActivity.java @@ -27,6 +27,8 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.api.OpenKeychainIntents; import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; import org.sufficientlysecure.keychain.pgp.PgpHelper; +import org.sufficientlysecure.keychain.service.results.OperationResult; +import org.sufficientlysecure.keychain.service.results.SingletonResult; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.ui.util.Notify; @@ -53,6 +55,48 @@ public class DecryptTextActivity extends ActionBarActivity { handleActions(savedInstanceState, getIntent()); } + /** + * Fix the message a bit, trailing spaces and newlines break stuff, + * because GMail sends as HTML and such things break ASCII Armor + * TODO: things like "<" and ">" also make problems + *

+ * NOTE: Do not use on cleartext signatures, only on ASCII-armored ciphertext, + * it would change the signed message + */ + private String fixAsciiArmoredCiphertext(String message) { + message = message.replaceAll(" +\n", "\n"); + message = message.replaceAll("\n\n+", "\n\n"); + message = message.replaceFirst("^\n+", ""); + // make sure there'll be exactly one newline at the end + message = message.replaceFirst("\n*$", "\n"); + // replace non breakable spaces + message = message.replaceAll("\\xa0", " "); + + return message; + } + + private String getPgpContent(String input) { + // only decrypt if clipboard content is available and a pgp message or cleartext signature + if (input != null) { + Matcher matcher = PgpHelper.PGP_MESSAGE.matcher(input); + if (matcher.matches()) { + String message = matcher.group(1); + message = fixAsciiArmoredCiphertext(message); + return message; + } else { + matcher = PgpHelper.PGP_CLEARTEXT_SIGNATURE.matcher(input); + if (matcher.matches()) { + // return cleartext signature + return matcher.group(1); + } else { + return null; + } + } + } else { + return null; + } + } + /** * Handles all actions with this intent * @@ -67,73 +111,58 @@ public class DecryptTextActivity extends ActionBarActivity { extras = new Bundle(); } - String textData = null; - - /* - * Android's Action - */ if (Intent.ACTION_SEND.equals(action) && type != null) { + // Android action + // When sending to Keychain Decrypt via share menu if ("text/plain".equals(type)) { - // Plain text String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); + sharedText = getPgpContent(sharedText); + if (sharedText != null) { - // handle like normal text decryption, override action and extras to later - // executeServiceMethod ACTION_DECRYPT_TEXT in main actions - textData = sharedText; + loadFragment(savedInstanceState, sharedText); + } else { + Notify.showNotify(this, R.string.error_invalid_data, Notify.Style.ERROR); } - } - } - - /** - * Main Actions - */ - textData = extras.getString(EXTRA_TEXT); - if (ACTION_DECRYPT_TEXT.equals(action) && textData != null) { - Log.d(Constants.TAG, "textData not null, matching text ..."); - Matcher matcher = PgpHelper.PGP_MESSAGE.matcher(textData); - if (matcher.matches()) { - Log.d(Constants.TAG, "PGP_MESSAGE matched"); - textData = matcher.group(1); - // replace non breakable spaces - textData = textData.replaceAll("\\xa0", " "); } else { - matcher = PgpHelper.PGP_CLEARTEXT_SIGNATURE.matcher(textData); - if (matcher.matches()) { - Log.d(Constants.TAG, "PGP_CLEARTEXT_SIGNATURE matched"); - textData = matcher.group(1); - // replace non breakable spaces - textData = textData.replaceAll("\\xa0", " "); - } else { - Notify.showNotify(this, R.string.error_invalid_data, Notify.Style.ERROR); - Log.d(Constants.TAG, "Nothing matched!"); - } + Log.e(Constants.TAG, "ACTION_SEND received non-plaintext, this should not happen in this activity!"); } - } else if (ACTION_DECRYPT_FROM_CLIPBOARD.equals(action)) { - CharSequence clipboardText = ClipboardReflection.getClipboardText(this); + } else if (ACTION_DECRYPT_TEXT.equals(action)) { + Log.d(Constants.TAG, "ACTION_DECRYPT_TEXT textData not null, matching text..."); - // only decrypt if clipboard content is available and a pgp message or cleartext signature - if (clipboardText != null) { - Matcher matcher = PgpHelper.PGP_MESSAGE.matcher(clipboardText); - if (!matcher.matches()) { - matcher = PgpHelper.PGP_CLEARTEXT_SIGNATURE.matcher(clipboardText); - } - if (matcher.matches()) { - textData = matcher.group(1); - } else { - Notify.showNotify(this, R.string.error_invalid_data, Notify.Style.ERROR); - } + String extraText = extras.getString(EXTRA_TEXT); + extraText = getPgpContent(extraText); + + if (extraText != null) { + loadFragment(savedInstanceState, extraText); } else { Notify.showNotify(this, R.string.error_invalid_data, Notify.Style.ERROR); } - } else if (ACTION_DECRYPT_TEXT.equals(action)) { - Log.e(Constants.TAG, - "Include the extra 'text' in your Intent!"); - } + } else if (ACTION_DECRYPT_FROM_CLIPBOARD.equals(action)) { + Log.d(Constants.TAG, "ACTION_DECRYPT_FROM_CLIPBOARD"); - loadFragment(savedInstanceState, textData); + String clipboardText = ClipboardReflection.getClipboardText(this).toString(); + clipboardText = getPgpContent(clipboardText); + + if (clipboardText != null) { + loadFragment(savedInstanceState, clipboardText); + } else { + returnInvalidResult(); + } + } else if (ACTION_DECRYPT_TEXT.equals(action)) { + Log.e(Constants.TAG, "Include the extra 'text' in your Intent!"); + finish(); + } } + private void returnInvalidResult() { + SingletonResult result = new SingletonResult( + SingletonResult.RESULT_ERROR, OperationResult.LogType.MSG_NO_VALID_ENC); + Intent intent = new Intent(); + intent.putExtra(SingletonResult.EXTRA_RESULT, result); + setResult(RESULT_OK, intent); + finish(); + } private void loadFragment(Bundle savedInstanceState, String ciphertext) { // However, if we're being restored from a previous state, diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index a7db73a0a..57c2cac5c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -654,7 +654,7 @@ public class EditKeyFragment extends LoaderFragment implements // Prepare an intent with an EXTRA_RESULT Intent intent = new Intent(); intent.putExtra(OperationResult.EXTRA_RESULT, - new SingletonResult(SingletonResult.RESULT_ERROR, LogLevel.ERROR, reason)); + new SingletonResult(SingletonResult.RESULT_ERROR, reason)); // Finish with result getActivity().setResult(EditKeyActivity.RESULT_OK, intent); diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 3478acbbd..c2750df7f 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -241,7 +241,7 @@ "need at least one identity" "no passphrase given" "no signature key given" - "not valid encryption data" + "No valid encrypted or signed OpenPGP content!" "integrity check failed! Data has been modified!" "wrong passphrase" "could not extract private key" @@ -424,6 +424,7 @@ "Select key" "Create new key for this account" "Save" + "Account has been saved" "Cancel" "Revoke access" "Start application" From 0143b54e9ec2453c47baaf9e9ad4a0a14ffc08c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 23 Sep 2014 23:38:30 +0200 Subject: [PATCH 2/9] Implement buttons in DecryptTextFragment --- .../keychain/ui/DecryptFilesFragment.java | 8 +- .../keychain/ui/DecryptFragment.java | 87 ++++++++----- .../keychain/ui/DecryptTextFragment.java | 115 ++++++++++++++---- .../keychain/ui/EncryptActivity.java | 7 -- .../ui/EncryptAsymmetricFragment.java | 2 +- .../main/res/layout/decrypt_text_fragment.xml | 6 +- OpenKeychain/src/main/res/values/strings.xml | 2 + 7 files changed, 157 insertions(+), 70 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesFragment.java index 875a12f4e..255e1f2aa 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFilesFragment.java @@ -208,10 +208,10 @@ public class DecryptFilesFragment extends DecryptFragment { if (pgpResult.isPending()) { if ((pgpResult.getResult() & DecryptVerifyResult.RESULT_PENDING_ASYM_PASSPHRASE) == DecryptVerifyResult.RESULT_PENDING_ASYM_PASSPHRASE) { - showPassphraseDialog(pgpResult.getKeyIdPassphraseNeeded()); + startPassphraseDialog(pgpResult.getKeyIdPassphraseNeeded()); } else if ((pgpResult.getResult() & DecryptVerifyResult.RESULT_PENDING_SYM_PASSPHRASE) == DecryptVerifyResult.RESULT_PENDING_SYM_PASSPHRASE) { - showPassphraseDialog(Constants.key.symmetric); + startPassphraseDialog(Constants.key.symmetric); } else if ((pgpResult.getResult() & DecryptVerifyResult.RESULT_PENDING_NFC) == DecryptVerifyResult.RESULT_PENDING_NFC) { // TODO @@ -296,10 +296,10 @@ public class DecryptFilesFragment extends DecryptFragment { if (pgpResult.isPending()) { if ((pgpResult.getResult() & DecryptVerifyResult.RESULT_PENDING_ASYM_PASSPHRASE) == DecryptVerifyResult.RESULT_PENDING_ASYM_PASSPHRASE) { - showPassphraseDialog(pgpResult.getKeyIdPassphraseNeeded()); + startPassphraseDialog(pgpResult.getKeyIdPassphraseNeeded()); } else if ((pgpResult.getResult() & DecryptVerifyResult.RESULT_PENDING_SYM_PASSPHRASE) == DecryptVerifyResult.RESULT_PENDING_SYM_PASSPHRASE) { - showPassphraseDialog(Constants.key.symmetric); + startPassphraseDialog(Constants.key.symmetric); } else if ((pgpResult.getResult() & DecryptVerifyResult.RESULT_PENDING_NFC) == DecryptVerifyResult.RESULT_PENDING_NFC) { // TODO diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java index 7298ff6d7..e19110c1f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java @@ -33,6 +33,7 @@ import android.widget.TextView; import org.openintents.openpgp.OpenPgpSignatureResult; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.nfc.NfcActivity; import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.service.results.DecryptVerifyResult; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; @@ -81,25 +82,51 @@ public abstract class DecryptFragment extends Fragment { startActivityForResult(intent, RESULT_CODE_LOOKUP_KEY); } - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - switch (requestCode) { + public static final int REQUEST_CODE_PASSPHRASE = 0x00008001; + public static final int REQUEST_CODE_NFC = 0x00008002; - case RESULT_CODE_LOOKUP_KEY: { - if (resultCode == Activity.RESULT_OK) { - // TODO: generate new OpenPgpSignatureResult and display it - } - return; - } - - default: { - super.onActivityResult(requestCode, resultCode, data); - - break; - } - } + protected void startPassphraseDialog(long subkeyId) { + Intent intent = new Intent(getActivity(), PassphraseDialogActivity.class); + intent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID, subkeyId); + startActivityForResult(intent, REQUEST_CODE_PASSPHRASE); } + protected void startNfcSign(String pin, byte[] hashToSign, int hashAlgo) { + Intent data = new Intent(); + + // build PendingIntent for Yubikey NFC operations + Intent intent = new Intent(getActivity(), NfcActivity.class); + intent.setAction(NfcActivity.ACTION_SIGN_HASH); + // pass params through to activity that it can be returned again later to repeat pgp operation + intent.putExtra(NfcActivity.EXTRA_DATA, data); + intent.putExtra(NfcActivity.EXTRA_PIN, pin); + + intent.putExtra(NfcActivity.EXTRA_NFC_HASH_TO_SIGN, hashToSign); + intent.putExtra(NfcActivity.EXTRA_NFC_HASH_ALGO, hashAlgo); + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); + + startActivityForResult(intent, REQUEST_CODE_NFC); + } + +// @Override +// public void onActivityResult(int requestCode, int resultCode, Intent data) { +// switch (requestCode) { +// +// case RESULT_CODE_LOOKUP_KEY: { +// if (resultCode == Activity.RESULT_OK) { +// // TODO: generate new OpenPgpSignatureResult and display it +// } +// return; +// } +// +// default: { +// super.onActivityResult(requestCode, resultCode, data); +// +// break; +// } +// } +// } + protected void onResult(DecryptVerifyResult decryptVerifyResult) { OpenPgpSignatureResult signatureResult = decryptVerifyResult.getSignatureResult(); @@ -197,20 +224,20 @@ public abstract class DecryptFragment extends Fragment { } } - protected void showPassphraseDialog(long keyId) { - PassphraseDialogFragment.show(getActivity(), keyId, - new Handler() { - @Override - public void handleMessage(Message message) { - if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { - String passphrase = - message.getData().getString(PassphraseDialogFragment.MESSAGE_DATA_PASSPHRASE); - decryptStart(passphrase); - } - } - } - ); - } +// protected void showPassphraseDialog(long keyId) { +// PassphraseDialogFragment.show(getActivity(), keyId, +// new Handler() { +// @Override +// public void handleMessage(Message message) { +// if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { +// String passphrase = +// message.getData().getString(PassphraseDialogFragment.MESSAGE_DATA_PASSPHRASE); +// decryptStart(passphrase); +// } +// } +// } +// ); +// } /** * Should be overridden by MessageFragment and FileFragment to start actual decryption diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptTextFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptTextFragment.java index a7bd0825a..c51b8b766 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptTextFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptTextFragment.java @@ -17,6 +17,7 @@ package org.sufficientlysecure.keychain.ui; +import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; @@ -29,20 +30,23 @@ import android.widget.TextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.results.DecryptVerifyResult; +import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.ShareHelper; public class DecryptTextFragment extends DecryptFragment { public static final String ARG_CIPHERTEXT = "ciphertext"; -// // view - private TextView mMessage; -// private View mDecryptButton; -// private View mDecryptFromCLipboardButton; -// -// // model + // view + private TextView mText; + private View mShareButton; + private View mCopyButton; + + // model private String mCiphertext; /** @@ -66,25 +70,53 @@ public class DecryptTextFragment extends DecryptFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.decrypt_text_fragment, container, false); - mMessage = (TextView) view.findViewById(R.id.decrypt_text_plaintext); -// mDecryptButton = view.findViewById(R.id.action_decrypt); -// mDecryptFromCLipboardButton = view.findViewById(R.id.action_decrypt_from_clipboard); -// mDecryptButton.setOnClickListener(new OnClickListener() { -// @Override -// public void onClick(View v) { -// decryptClicked(); -// } -// }); -// mDecryptFromCLipboardButton.setOnClickListener(new OnClickListener() { -// @Override -// public void onClick(View v) { -// decryptFromClipboardClicked(); -// } -// }); + mText = (TextView) view.findViewById(R.id.decrypt_text_plaintext); + mShareButton = view.findViewById(R.id.action_decrypt_share_plaintext); + mCopyButton = view.findViewById(R.id.action_decrypt_copy_plaintext); + mShareButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(sendWithChooserExcludingEncrypt(mText.getText().toString())); + } + }); + mCopyButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + copyToClipboard(mText.getText().toString()); + } + }); return view; } + /** + * Create Intent Chooser but exclude OK's EncryptActivity. + */ + private Intent sendWithChooserExcludingEncrypt(String text) { + Intent prototype = createSendIntent(text); + String title = getString(R.string.title_share_file); + + // we don't want to encrypt the encrypted, no inception ;) + String[] blacklist = new String[]{ + Constants.PACKAGE_NAME + ".ui.DecryptTextActivity", + "org.thialfihar.android.apg.ui.DecryptActivity" + }; + + return new ShareHelper(getActivity()).createChooserExcluding(prototype, title, blacklist); + } + + private Intent createSendIntent(String text) { + Intent sendIntent = new Intent(Intent.ACTION_SEND); + sendIntent.putExtra(Intent.EXTRA_TEXT, text); + sendIntent.setType("text/plain"); + return sendIntent; + } + + private void copyToClipboard(String text) { + ClipboardReflection.copyToClipboard(getActivity(), text); + Notify.showNotify(getActivity(), R.string.text_copied_to_clipboard, Notify.Style.INFO); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -132,10 +164,10 @@ public class DecryptTextFragment extends DecryptFragment { if (pgpResult.isPending()) { if ((pgpResult.getResult() & DecryptVerifyResult.RESULT_PENDING_ASYM_PASSPHRASE) == DecryptVerifyResult.RESULT_PENDING_ASYM_PASSPHRASE) { - showPassphraseDialog(pgpResult.getKeyIdPassphraseNeeded()); + startPassphraseDialog(pgpResult.getKeyIdPassphraseNeeded()); } else if ((pgpResult.getResult() & DecryptVerifyResult.RESULT_PENDING_SYM_PASSPHRASE) == DecryptVerifyResult.RESULT_PENDING_SYM_PASSPHRASE) { - showPassphraseDialog(Constants.key.symmetric); + startPassphraseDialog(Constants.key.symmetric); } else if ((pgpResult.getResult() & DecryptVerifyResult.RESULT_PENDING_NFC) == DecryptVerifyResult.RESULT_PENDING_NFC) { // TODO @@ -146,8 +178,8 @@ public class DecryptTextFragment extends DecryptFragment { byte[] decryptedMessage = returnData .getByteArray(KeychainIntentService.RESULT_DECRYPTED_BYTES); - mMessage.setText(new String(decryptedMessage)); - mMessage.setHorizontallyScrolling(false); + mText.setText(new String(decryptedMessage)); + mText.setHorizontallyScrolling(false); pgpResult.createNotify(getActivity()).show(); @@ -171,4 +203,37 @@ public class DecryptTextFragment extends DecryptFragment { getActivity().startService(intent); } + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + switch (requestCode) { + + case REQUEST_CODE_PASSPHRASE: { + if (resultCode == Activity.RESULT_OK && data != null) { + String passphrase = data.getStringExtra(PassphraseDialogActivity.MESSAGE_DATA_PASSPHRASE); + decryptStart(passphrase); + return; + } else { + getActivity().finish(); + } + break; + } + + case REQUEST_CODE_NFC: { + if (resultCode == Activity.RESULT_OK && data != null) { + // TODO + return; + } else { + getActivity().finish(); + } + break; + } + + default: { + super.onActivityResult(requestCode, resultCode, data); + + break; + } + } + } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java index 836e7e268..0691e7a30 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptActivity.java @@ -10,15 +10,8 @@ public class EncryptActivity extends DrawerActivity { public static final int REQUEST_CODE_NFC = 0x00008002; protected void startPassphraseDialog(long subkeyId) { - Intent data = new Intent(); - - // build PendingIntent for Yubikey NFC operations Intent intent = new Intent(this, PassphraseDialogActivity.class); - // pass params through to activity that it can be returned again later to repeat pgp operation intent.putExtra(PassphraseDialogActivity.EXTRA_SUBKEY_ID, subkeyId); - -// intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); - startActivityForResult(intent, REQUEST_CODE_PASSPHRASE); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java index a508472d6..54877f676 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java @@ -135,7 +135,7 @@ public class EncryptAsymmetricFragment extends Fragment implements EncryptActivi try { CachedPublicKeyRing keyring = mProviderHelper.getCachedPublicKeyRing( KeyRings.buildUnifiedKeyRingUri(signatureKey)); - if(keyring.hasAnySecret()) { + if (keyring.hasAnySecret()) { setSignatureKeyId(keyring.getMasterKeyId()); mSign.setSelectedKeyId(mEncryptInterface.getSignatureKey()); } diff --git a/OpenKeychain/src/main/res/layout/decrypt_text_fragment.xml b/OpenKeychain/src/main/res/layout/decrypt_text_fragment.xml index f6cc892d7..1c6097495 100644 --- a/OpenKeychain/src/main/res/layout/decrypt_text_fragment.xml +++ b/OpenKeychain/src/main/res/layout/decrypt_text_fragment.xml @@ -43,7 +43,7 @@ "View certification key" "Create key" "Add file(s)" + "Share decrypted text" "Settings" @@ -225,6 +226,7 @@ "Fingerprint has been copied to the clipboard!" "Please select a key to be used for certification!" "Key is too big to be shared this way!" + "Text has been copied to the clipboard!" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + android:orientation="horizontal"> + + + + + + + + + + + + + \ No newline at end of file diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 36364853d..e77106705 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -51,6 +51,8 @@ "Keyserver" "Fingerprint" "Key to be certified" + "Files" + "Text" "Decrypt, verify, and save file" @@ -71,6 +73,9 @@ "Create key" "Add file(s)" "Share decrypted text" + "Decrypt from clipboard" + "and verify signatures" + "Decrypt files" "Settings" From 993046d12e14d478208281c0ce09ca7d2d21f139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 24 Sep 2014 01:38:18 +0200 Subject: [PATCH 8/9] cleanup --- .../src/main/res/layout/decrypt_content.xml | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/OpenKeychain/src/main/res/layout/decrypt_content.xml b/OpenKeychain/src/main/res/layout/decrypt_content.xml index 5dacf14a8..bcd040291 100644 --- a/OpenKeychain/src/main/res/layout/decrypt_content.xml +++ b/OpenKeychain/src/main/res/layout/decrypt_content.xml @@ -52,34 +52,6 @@ android:layout_marginTop="32dp" android:text="@string/section_decrypt_text" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - Date: Wed, 24 Sep 2014 01:41:38 +0200 Subject: [PATCH 9/9] Fix new decrypt button --- OpenKeychain/src/main/res/layout/decrypt_content.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/OpenKeychain/src/main/res/layout/decrypt_content.xml b/OpenKeychain/src/main/res/layout/decrypt_content.xml index bcd040291..7cd066b5b 100644 --- a/OpenKeychain/src/main/res/layout/decrypt_content.xml +++ b/OpenKeychain/src/main/res/layout/decrypt_content.xml @@ -65,10 +65,8 @@ android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" - android:clickable="true" android:paddingRight="4dp" android:gravity="center_vertical" - style="@style/SelectableItem" android:orientation="vertical">