From 8ce78408c2b66bb09ae41c8a8cfdc220db64ddbc Mon Sep 17 00:00:00 2001
From: cketti
+ * This method avoids using regular expressions on the entire message body to save memory. + *
+ * + * @param text + * Plain text string. + * @param useHtmlTag + * If {@code true} this method adds headers and footers to create a proper HTML + * document. + * * @return HTML string. */ - private static String simpleTextToHtml(String text) { + private static String simpleTextToHtml(String text, boolean useHtmlTag) { // Encode HTML entities to make sure we don't display something evil. text = TextUtils.htmlEncode(text); StringReader reader = new StringReader(text); StringBuilder buff = new StringBuilder(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH); - buff.append(""); + + if (useHtmlTag) { + buff.append(getHtmlHeader()); + } + + buff.append(htmlifyMessageHeader()); int c; try { @@ -159,25 +181,39 @@ public class HtmlConverter { Log.e(K9.LOG_TAG, "Could not read string to convert text to HTML:", e); } - buff.append(""); + buff.append(htmlifyMessageFooter()); + + if (useHtmlTag) { + buff.append(getHtmlFooter()); + } return buff.toString(); } /** - * Convert a text string into an HTML document. Attempts to do smart replacement for large - * documents to prevent OOM errors. This method adds headers and footers to create a proper HTML - * document. To convert to a fragment, use {@link #textToHtmlFragment(String)}. - * @param text Plain text string. + * Convert a text string into an HTML document. + * + *+ * Attempts to do smart replacement for large documents to prevent OOM errors. This method + * optionally adds headers and footers to create a proper HTML document. To convert to a + * fragment, use {@link #textToHtmlFragment(String)}. + *
+ * + * @param text + * Plain text string. + * @param useHtmlTag + * If {@code true} this method adds headers and footers to create a proper HTML + * document. + * * @return HTML string. */ - public static String textToHtml(String text) { + public static String textToHtml(String text, boolean useHtmlTag) { // Our HTMLification code is somewhat memory intensive // and was causing lots of OOM errors on the market // if the message is big and plain text, just do // a trivial htmlification if (text.length() > MAX_SMART_HTMLIFY_MESSAGE_LENGTH) { - return simpleTextToHtml(text); + return simpleTextToHtml(text, useHtmlTag); } StringReader reader = new StringReader(text); StringBuilder buff = new StringBuilder(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH); @@ -221,11 +257,19 @@ public class HtmlConverter { text = text.replaceAll("(?m)(\r\n|\n|\r){4,}", "\n\n"); StringBuffer sb = new StringBuffer(text.length() + TEXT_TO_HTML_EXTRA_BUFFER_LENGTH); - sb.append(""); + + if (useHtmlTag) { + sb.append(getHtmlHeader()); + } + sb.append(htmlifyMessageHeader()); linkifyText(text, sb); sb.append(htmlifyMessageFooter()); - sb.append(""); + + if (useHtmlTag) { + sb.append(getHtmlFooter()); + } + text = sb.toString(); return text; diff --git a/src/com/fsck/k9/mail/internet/MimeUtility.java b/src/com/fsck/k9/mail/internet/MimeUtility.java index 7d2755be9..74e3ce974 100644 --- a/src/com/fsck/k9/mail/internet/MimeUtility.java +++ b/src/com/fsck/k9/mail/internet/MimeUtility.java @@ -1112,7 +1112,7 @@ public class MimeUtility { return tempBody; } - + /** * Empty base class for the class hierarchy used by * {@link MimeUtility#extractTextAndAttachments(Context, Message)}. @@ -1320,6 +1320,8 @@ public class MimeUtility { StringBuilder text = new StringBuilder(); StringBuilder html = new StringBuilder(); + html.append(HtmlConverter.getHtmlHeader()); + for (Viewable viewable : viewables) { if (viewable instanceof Textual) { // This is either a text/plain or text/html part. Fill the variables 'text' and @@ -1370,6 +1372,8 @@ public class MimeUtility { } } + html.append(HtmlConverter.getHtmlFooter()); + return new ViewableContainer(text.toString(), html.toString(), attachments); } catch (Exception e) { throw new MessagingException("Couldn't extract viewable parts", e); @@ -1863,8 +1867,8 @@ public class MimeUtility { * Use the contents of a {@link Viewable} to create the HTML to be displayed. * *- * This will use {@link HtmlConverter#textToHtml(String)} to convert plain text parts to HTML - * if necessary. + * This will use {@link HtmlConverter#textToHtml(String, boolean)} to convert plain text parts + * to HTML if necessary. *
* * @param viewable @@ -1886,7 +1890,7 @@ public class MimeUtility { if (t == null) { t = ""; } else if (viewable instanceof Text) { - t = HtmlConverter.textToHtml(t); + t = HtmlConverter.textToHtml(t, false); } html.append(t); } else if (viewable instanceof Alternative) { diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index 8f2fbb021..e59eeeef4 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -2105,7 +2105,7 @@ public class LocalStore extends Store implements Serializable { List