Support for multiple body parts

pull/174/head
M66B 5 years ago
parent d5cef39de0
commit 7347b161aa

@ -1221,78 +1221,90 @@ public class MessageHelper {
}
class MessageParts {
private Part plain = null;
private Part html = null;
private List<Part> plain = new ArrayList<>();
private List<Part> html = new ArrayList<>();
private List<AttachmentPart> attachments = new ArrayList<>();
private ArrayList<String> 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<Part> 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 = "<div>" + HtmlHelper.formatPre(result) + "</div>";
if (part == plain)
result = "<div>" + HtmlHelper.formatPre(result) + "</div>";
sb.append(result);
}
return result;
return sb.toString();
}
List<AttachmentPart> 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;

Loading…
Cancel
Save