From 88952aed2c977ad53c9fcc06b253b259d09f6f80 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 23 Dec 2019 18:08:34 +0100 Subject: [PATCH] Simplification --- app/src/main/java/eu/faircode/email/Core.java | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 5ae38b6289..8cd7edf42e 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -32,6 +32,7 @@ import android.os.Build; import android.os.Bundle; import android.os.SystemClock; import android.text.TextUtils; +import android.util.Base64; import android.util.Pair; import androidx.annotation.NonNull; @@ -2147,28 +2148,29 @@ class Core { int sequence = 1; String autocrypt = helper.getAutocrypt(); - if (autocrypt != null) { - EntityAttachment attachment = new EntityAttachment(); - attachment.message = message.id; - attachment.sequence = sequence++; - attachment.name = "pubkey.pem"; - attachment.type = "application/pgp-keys"; - attachment.disposition = Part.ATTACHMENT; - attachment.id = db.attachment().insertAttachment(attachment); + if (autocrypt != null) + try { + EntityAttachment attachment = new EntityAttachment(); + attachment.message = message.id; + attachment.sequence = sequence++; + attachment.name = "pubkey.pem"; + attachment.type = "application/pgp-keys"; + attachment.disposition = Part.ATTACHMENT; + attachment.id = db.attachment().insertAttachment(attachment); - File file = attachment.getFile(context); - try (FileWriter writer = new FileWriter(file)) { - writer.write("-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\n"); - while (autocrypt.length() > 0) { - int i = Math.min(64, autocrypt.length()); - writer.write(autocrypt.substring(0, i) + "\r\n"); - autocrypt = autocrypt.substring(i); + byte[] b = Base64.decode(autocrypt, Base64.DEFAULT); + + File file = attachment.getFile(context); + try (FileWriter writer = new FileWriter(file)) { + writer.write("-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n\r\n"); + writer.write(Base64.encodeToString(b, Base64.CRLF)); + writer.write("-----END PGP PUBLIC KEY BLOCK-----\r\n"); } - writer.write("-----END PGP PUBLIC KEY BLOCK-----\r\n"); - } - db.attachment().setDownloaded(attachment.id, file.length()); - } + db.attachment().setDownloaded(attachment.id, file.length()); + } catch (IllegalArgumentException ex) { + Log.w(ex); + } List attachments = parts.getAttachments(); for (EntityAttachment attachment : attachments) {