Refactoring

pull/168/head
M66B 5 years ago
parent 2d3e5efcc4
commit 1d77fe950a

@ -1511,15 +1511,15 @@ public class FragmentCompose extends FragmentBase {
throw new IllegalArgumentException(getString(R.string.title_from_missing)); throw new IllegalArgumentException(getString(R.string.title_from_missing));
// Create files // Create files
File input = new File(context.getCacheDir(), "input." + id); File input = new File(context.getCacheDir(), "pgp_input." + id);
File output = new File(context.getCacheDir(), "output." + id); File output = new File(context.getCacheDir(), "pgp_output." + id);
// Serializing messages is NOT reproducible // Serializing messages is NOT reproducible
if ((EntityMessage.PGP_SIGNONLY.equals(draft.encrypt) && if ((EntityMessage.PGP_SIGNONLY.equals(draft.encrypt) &&
OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) || OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction())) ||
(EntityMessage.PGP_SIGNENCRYPT.equals(draft.encrypt) && (EntityMessage.PGP_SIGNENCRYPT.equals(draft.encrypt) &&
OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction()))) { OpenPgpApi.ACTION_GET_KEY_IDS.equals(data.getAction()))) {
// Get attachments // Get/clean attachments
List<EntityAttachment> attachments = db.attachment().getAttachments(id); List<EntityAttachment> attachments = db.attachment().getAttachments(id);
for (EntityAttachment attachment : new ArrayList<>(attachments)) for (EntityAttachment attachment : new ArrayList<>(attachments))
if (attachment.encryption != null) { if (attachment.encryption != null) {
@ -1558,6 +1558,7 @@ public class FragmentCompose extends FragmentBase {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
bpContent.writeTo(bos); bpContent.writeTo(bos);
// Just to be sure
String raw = new String(bos.toByteArray()); String raw = new String(bos.toByteArray());
raw.replaceAll(" +$", "") // trim trailing spaces raw.replaceAll(" +$", "") // trim trailing spaces
.replace("\\r?\\n", "\\r\\n"); // normalize new lines .replace("\\r?\\n", "\\r\\n"); // normalize new lines
@ -1573,13 +1574,14 @@ public class FragmentCompose extends FragmentBase {
} }
} }
// Call OpenPGP
Intent result; Intent result;
if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction()) && identity.sign_key != null) { if (OpenPgpApi.ACTION_GET_SIGN_KEY_ID.equals(data.getAction()) && identity.sign_key != null) {
// Short circuit
result = data; result = data;
result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);
result.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, identity.sign_key); result.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, identity.sign_key);
} else { } else {
// Call OpenPGP
Log.i("Executing " + data.getAction()); Log.i("Executing " + data.getAction());
Log.logExtras(data); Log.logExtras(data);
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService()); OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
@ -1593,10 +1595,10 @@ public class FragmentCompose extends FragmentBase {
Log.logExtras(data); Log.logExtras(data);
switch (resultCode) { switch (resultCode) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
// Attach encrypted data / signature // Attach key, signed/encrypted data
if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction()) || if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction()) ||
OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()) || OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction()) ||
OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction())) OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction()))
try { try {
db.beginTransaction(); db.beginTransaction();
@ -1606,14 +1608,14 @@ public class FragmentCompose extends FragmentBase {
if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction())) { if (OpenPgpApi.ACTION_GET_KEY.equals(data.getAction())) {
name = "keydata.asc"; name = "keydata.asc";
encryption = EntityAttachment.PGP_KEY; encryption = EntityAttachment.PGP_KEY;
} else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) {
name = "encrypted.asc";
encryption = EntityAttachment.PGP_MESSAGE;
} else if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction())) { } else if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction())) {
name = "signature.asc"; name = "signature.asc";
encryption = EntityAttachment.PGP_SIGNATURE; encryption = EntityAttachment.PGP_SIGNATURE;
type = "application/pgp-signature; micalg=\"" + type = "application/pgp-signature; micalg=\"" +
result.getStringExtra(OpenPgpApi.RESULT_SIGNATURE_MICALG) + "\""; result.getStringExtra(OpenPgpApi.RESULT_SIGNATURE_MICALG) + "\"";
} else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) {
name = "encrypted.asc";
encryption = EntityAttachment.PGP_MESSAGE;
} else } else
throw new IllegalStateException(data.getAction()); throw new IllegalStateException(data.getAction());
@ -1709,13 +1711,7 @@ public class FragmentCompose extends FragmentBase {
return intent; return intent;
} else } else
throw new IllegalArgumentException("Invalid encrypt=" + draft.encrypt); throw new IllegalArgumentException("Invalid encrypt=" + draft.encrypt);
} else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) {
input.delete();
// send message
return null;
} else if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction())) { } else if (OpenPgpApi.ACTION_DETACHED_SIGN.equals(data.getAction())) {
EntityAttachment attachment = new EntityAttachment(); EntityAttachment attachment = new EntityAttachment();
attachment.message = id; attachment.message = id;
attachment.sequence = db.attachment().getAttachmentSequence(id) + 1; attachment.sequence = db.attachment().getAttachmentSequence(id) + 1;
@ -1730,6 +1726,11 @@ public class FragmentCompose extends FragmentBase {
db.attachment().setDownloaded(attachment.id, file.length()); db.attachment().setDownloaded(attachment.id, file.length());
// send message
return null;
} else if (OpenPgpApi.ACTION_SIGN_AND_ENCRYPT.equals(data.getAction())) {
input.delete();
// send message // send message
return null; return null;
} else } else

Loading…
Cancel
Save