From 7347b161aac0aaf2b775de8581d365cb852526ea Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 22 Feb 2020 21:20:51 +0100 Subject: [PATCH] Support for multiple body parts --- .../java/eu/faircode/email/MessageHelper.java | 123 ++++++++++-------- 1 file changed, 66 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index a9ad995902..5d5a6494f4 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -1221,78 +1221,90 @@ public class MessageHelper { } class MessageParts { - private Part plain = null; - private Part html = null; + private List plain = new ArrayList<>(); + private List html = new ArrayList<>(); private List attachments = new ArrayList<>(); private ArrayList warnings = new ArrayList<>(); Boolean isPlainOnly() { - if (plain == null && html == null) + if (plain.size() + html.size() == 0) return null; - return (html == null); + return (html.size() == 0); } Long getBodySize() throws MessagingException { - Part part = (html == null ? plain : html); - if (part == null) - return null; - int size = part.getSize(); - if (size < 0) - return null; - else - return (long) size; + Long size = null; + + List all = new ArrayList<>(); + all.addAll(plain); + all.addAll(html); + for (Part p : all) { + int s = p.getSize(); + if (s >= 0) + if (size == null) + size = (long) s; + else + size += (long) s; + } + return size; } String getHtml(Context context) throws MessagingException, IOException { - if (plain == null && html == null) { + if (plain.size() + html.size() == 0) { Log.i("No body part"); return null; } - String result; - Part part = (html == null ? plain : html); - if (part.getSize() > MAX_MESSAGE_SIZE) { - warnings.add(context.getString(R.string.title_insufficient_memory)); - return null; - } + StringBuilder sb = new StringBuilder(); - try { - Object content = part.getContent(); - Log.i("Content class=" + (content == null ? null : content.getClass().getName())); + for (Part part : html.size() > 0 ? html : plain) { + if (part.getSize() > MAX_MESSAGE_SIZE) { + warnings.add(context.getString(R.string.title_insufficient_memory)); + return null; + } + + String result; + + try { + Object content = part.getContent(); + Log.i("Content class=" + (content == null ? null : content.getClass().getName())); + + if (content == null) { + warnings.add(context.getString(R.string.title_no_body)); + return null; + } - if (content == null) { - warnings.add(context.getString(R.string.title_no_body)); + if (content instanceof String) + result = (String) content; + else if (content instanceof InputStream) + // Typically com.sun.mail.util.QPDecoderStream + result = Helper.readStream((InputStream) content, StandardCharsets.UTF_8.name()); + else + result = content.toString(); + } catch (IOException | FolderClosedException | MessageRemovedException ex) { + throw ex; + } catch (Throwable ex) { + Log.w(ex); + warnings.add(Log.formatThrowable(ex, false)); return null; } - if (content instanceof String) - result = (String) content; - else if (content instanceof InputStream) - // Typically com.sun.mail.util.QPDecoderStream - result = Helper.readStream((InputStream) content, StandardCharsets.UTF_8.name()); - else - result = content.toString(); - } catch (IOException | FolderClosedException | MessageRemovedException ex) { - throw ex; - } catch (Throwable ex) { - Log.w(ex); - warnings.add(Log.formatThrowable(ex, false)); - return null; - } + try { + ContentType ct = new ContentType(part.getContentType()); + String charset = ct.getParameter("charset"); + if (UnknownCharsetProvider.charsetForMime(charset) == null) + warnings.add(context.getString(R.string.title_no_charset, charset)); + } catch (ParseException ex) { + Log.e(ex); + } - try { - ContentType ct = new ContentType(part.getContentType()); - String charset = ct.getParameter("charset"); - if (UnknownCharsetProvider.charsetForMime(charset) == null) - warnings.add(context.getString(R.string.title_no_charset, charset)); - } catch (ParseException ex) { - Log.e(ex); - } + if (part.isMimeType("text/plain")) + result = "
" + HtmlHelper.formatPre(result) + "
"; - if (part == plain) - result = "
" + HtmlHelper.formatPre(result) + "
"; + sb.append(result); + } - return result; + return sb.toString(); } List getAttachmentParts() { @@ -1637,14 +1649,11 @@ public class MessageHelper { contentType = new ContentType(Helper.guessMimeType(filename)); } - if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) && - TextUtils.isEmpty(filename) && - ((parts.plain == null && "text/plain".equalsIgnoreCase(contentType.getBaseType())) || - (parts.html == null && "text/html".equalsIgnoreCase(contentType.getBaseType())))) { - if ("text/html".equalsIgnoreCase(contentType.getBaseType())) - parts.html = part; - else - parts.plain = part; + if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) && TextUtils.isEmpty(filename)) { + if ("text/plain".equalsIgnoreCase(contentType.getBaseType())) + parts.plain.add(part); + else if ("text/html".equalsIgnoreCase(contentType.getBaseType())) + parts.html.add(part); } else { AttachmentPart apart = new AttachmentPart(); apart.disposition = disposition;