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: DecryptTextActivity rework --- .../keychain/ui/CertifyKeyFragment.java | 4 +- .../keychain/ui/DecryptActivity.java | 14 ++- .../keychain/ui/DecryptFilesFragment.java | 2 - .../keychain/ui/DecryptTextActivity.java | 125 +++++++++++++-------- .../keychain/ui/EditKeyFragment.java | 2 +- 5 files changed, 93 insertions(+), 54 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui') 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); } + } else { + Log.e(Constants.TAG, "ACTION_SEND received non-plaintext, this should not happen in this activity!"); } - } + } else if (ACTION_DECRYPT_TEXT.equals(action)) { + Log.d(Constants.TAG, "ACTION_DECRYPT_TEXT textData not null, matching text..."); - /** - * 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", " "); + String extraText = extras.getString(EXTRA_TEXT); + extraText = getPgpContent(extraText); + + if (extraText != null) { + loadFragment(savedInstanceState, extraText); } 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!"); - } + Notify.showNotify(this, R.string.error_invalid_data, Notify.Style.ERROR); } } else if (ACTION_DECRYPT_FROM_CLIPBOARD.equals(action)) { - CharSequence clipboardText = ClipboardReflection.getClipboardText(this); + Log.d(Constants.TAG, "ACTION_DECRYPT_FROM_CLIPBOARD"); + + String clipboardText = ClipboardReflection.getClipboardText(this).toString(); + clipboardText = getPgpContent(clipboardText); - // 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); - } + loadFragment(savedInstanceState, clipboardText); } else { - Notify.showNotify(this, R.string.error_invalid_data, Notify.Style.ERROR); + returnInvalidResult(); } } else if (ACTION_DECRYPT_TEXT.equals(action)) { - Log.e(Constants.TAG, - "Include the extra 'text' in your Intent!"); + Log.e(Constants.TAG, "Include the extra 'text' in your Intent!"); + finish(); } - - loadFragment(savedInstanceState, textData); } + 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); -- cgit v1.2.3