warnings = new ArrayList<>();
String getHtml(Context context) throws MessagingException {
if (plain == null && html == null) {
warnings.add(context.getString(R.string.title_no_body));
return null;
}
String result;
boolean text = false;
Part part = (html == null ? plain : html);
try {
Object content = part.getContent();
if (content instanceof String)
result = (String) content;
else if (content instanceof InputStream)
// Typically com.sun.mail.util.QPDecoderStream
result = readStream((InputStream) content, "UTF-8");
else
result = content.toString();
} catch (Throwable ex) {
Log.w(ex);
text = true;
result = ex + "\n" + android.util.Log.getStackTraceString(ex);
}
ContentType ct = new ContentType(part.getContentType());
String charset = ct.getParameter("charset");
if (TextUtils.isEmpty(charset)) {
if (BuildConfig.DEBUG)
warnings.add(context.getString(R.string.title_no_charset, ct.toString()));
if (part.isMimeType("text/plain")) {
// The first 127 characters are the same as in US-ASCII
result = new String(result.getBytes(StandardCharsets.ISO_8859_1));
}
} else {
if ("US-ASCII".equals(Charset.forName(charset).name()) &&
!"US-ASCII".equals(charset.toUpperCase()))
warnings.add(context.getString(R.string.title_no_charset, charset));
}
if (part.isMimeType("text/plain") || text)
result = "" + result.replaceAll("\\r?\\n", "
") + "
";
return result;
}
List getAttachmentParts() {
return attachments;
}
List getAttachments() throws MessagingException {
List result = new ArrayList<>();
for (AttachmentPart apart : attachments) {
ContentType ct = new ContentType(apart.part.getContentType());
String[] cid = apart.part.getHeader("Content-ID");
EntityAttachment attachment = new EntityAttachment();
attachment.name = apart.filename;
attachment.type = ct.getBaseType().toLowerCase();
attachment.disposition = apart.disposition;
attachment.size = (long) apart.part.getSize();
attachment.cid = (cid == null || cid.length == 0 ? null : cid[0]);
attachment.encryption = (apart.pgp ? EntityAttachment.PGP_MESSAGE : null);
if ("text/calendar".equalsIgnoreCase(attachment.type) && TextUtils.isEmpty(attachment.name))
attachment.name = "invite.ics";
// Try to guess a better content type
// Sometimes PDF files are sent using the wrong type
if ("application/octet-stream".equalsIgnoreCase(attachment.type)) {
String extension = Helper.getExtension(attachment.name);
if (extension != null) {
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
if (type != null) {
Log.w("Guessing file=" + attachment.name + " type=" + type);
attachment.type = type;
}
}
}
if (attachment.size < 0)
attachment.size = null;
result.add(attachment);
}
// Fix duplicate CIDs
for (int i = 0; i < result.size(); i++) {
String cid = result.get(i).cid;
if (cid != null)
for (int j = i + 1; j < result.size(); j++) {
EntityAttachment a = result.get(j);
if (cid.equals(a.cid))
a.cid = null;
}
}
return result;
}
boolean downloadAttachment(Context context, DB db, long id, int sequence) throws IOException {
// Attachments of drafts might not have been uploaded yet
if (sequence > attachments.size()) {
Log.w("Attachment unavailable sequence=" + sequence + " size=" + attachments.size());
return false;
}
// Get data
AttachmentPart apart = attachments.get(sequence - 1);
File file = EntityAttachment.getFile(context, id);
// Download attachment
db.attachment().setProgress(id, null);
try (InputStream is = apart.part.getInputStream()) {
long size = 0;
long total = apart.part.getSize();
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE];
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {
size += len;
os.write(buffer, 0, len);
// Update progress
if (total > 0)
db.attachment().setProgress(id, (int) (size * 100 / total));
}
}
// Store attachment data
db.attachment().setDownloaded(id, size);
Log.i("Downloaded attachment size=" + size);
return true;
} catch (Throwable ex) {
Log.w(ex);
// Reset progress on failure
db.attachment().setError(id, Helper.formatThrowable(ex));
return false;
}
}
String getWarnings(String existing) {
if (existing != null)
warnings.add(0, existing);
if (warnings.size() == 0)
return null;
else
return TextUtils.join(", ", warnings);
}
}
class AttachmentPart {
String disposition;
String filename;
boolean pgp;
Part part;
}
MessageParts getMessageParts() throws IOException, FolderClosedException {
MessageParts parts = new MessageParts();
getMessageParts(imessage, parts, false); // Can throw ParseException
return parts;
}
private void getMessageParts(Part part, MessageParts parts, boolean pgp) throws IOException, FolderClosedException {
try {
if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
for (int i = 0; i < multipart.getCount(); i++)
try {
Part cpart = multipart.getBodyPart(i);
getMessageParts(cpart, parts, pgp);
ContentType ct = new ContentType(cpart.getContentType());
if ("application/pgp-encrypted".equals(ct.getBaseType().toLowerCase()))
pgp = true;
} catch (ParseException ex) {
// Nested body: try to continue
// ParseException: In parameter list boundary="...">, expected parameter name, got ";"
Log.w(ex);
}
} else {
// https://www.iana.org/assignments/cont-disp/cont-disp.xhtml
String disposition;
try {
disposition = part.getDisposition();
} catch (MessagingException ex) {
Log.w(ex);
disposition = null;
}
String filename;
try {
filename = part.getFileName();
} catch (MessagingException ex) {
Log.w(ex);
filename = null;
}
//Log.i("Part" +
// " disposition=" + disposition +
// " filename=" + filename +
// " content type=" + part.getContentType());
if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) &&
((parts.plain == null && part.isMimeType("text/plain")) ||
(parts.html == null && part.isMimeType("text/html")))) {
if (part.isMimeType("text/plain"))
parts.plain = part;
else
parts.html = part;
} else {
AttachmentPart apart = new AttachmentPart();
apart.disposition = disposition;
apart.filename = filename;
apart.pgp = pgp;
apart.part = part;
parts.attachments.add(apart);
}
}
} catch (FolderClosedException ex) {
throw ex;
} catch (MessagingException ex) {
Log.w(ex);
parts.warnings.add(Helper.formatThrowable(ex));
}
}
private static String readStream(InputStream is, String charset) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
for (int len = is.read(buffer); len != -1; len = is.read(buffer))
os.write(buffer, 0, len);
return new String(os.toByteArray(), charset);
}
static boolean equal(Address[] a1, Address[] a2) {
if (a1 == null && a2 == null)
return true;
if (a1 == null || a2 == null)
return false;
if (a1.length != a2.length)
return false;
for (int i = 0; i < a1.length; i++)
if (!a1[i].toString().equals(a2[i].toString()))
return false;
return true;
}
}