DecryptTextActivity rework

This commit is contained in:
Dominik Schürmann
2014-09-23 23:04:18 +02:00
parent 5e090e6fb6
commit 9f67b0fe54
10 changed files with 116 additions and 66 deletions

View File

@@ -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);

View File

@@ -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);
}
}
}

View File

@@ -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);

View File

@@ -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
* <p/>
* 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,

View File

@@ -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);