From 8ce78408c2b66bb09ae41c8a8cfdc220db64ddbc Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 17 Feb 2012 19:40:58 +0100 Subject: [PATCH] Fixed HTML generation in MimeUtility.extractTextAndAttachments() --- src/com/fsck/k9/activity/MessageCompose.java | 2 +- src/com/fsck/k9/helper/HtmlConverter.java | 72 +++++++++++++++---- .../fsck/k9/mail/internet/MimeUtility.java | 12 ++-- src/com/fsck/k9/mail/store/LocalStore.java | 13 +--- 4 files changed, 68 insertions(+), 31 deletions(-) diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index 8b85cfd95..927078cf2 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -2709,7 +2709,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc if (part != null) { if (K9.DEBUG) Log.d(K9.LOG_TAG, "getBodyTextFromMessage: HTML requested, text found."); - return HtmlConverter.textToHtml(MimeUtility.getTextFromPart(part)); + return HtmlConverter.textToHtml(MimeUtility.getTextFromPart(part), true); } } else if (format == MessageFormat.TEXT) { // Text takes precedence, then html. diff --git a/src/com/fsck/k9/helper/HtmlConverter.java b/src/com/fsck/k9/helper/HtmlConverter.java index 9cf3c38df..e0bf6b720 100644 --- a/src/com/fsck/k9/helper/HtmlConverter.java +++ b/src/com/fsck/k9/helper/HtmlConverter.java @@ -125,19 +125,41 @@ public class HtmlConverter { private static final int MAX_SMART_HTMLIFY_MESSAGE_LENGTH = 1024 * 256 ; + public static final String getHtmlHeader() { + return ""; + } + + public static final String getHtmlFooter() { + return ""; + } + /** - * Naively convert a text string into an HTML document. This method avoids using regular expressions on the entire - * message body to save memory. - * @param text Plain text string. + * Naively convert a text string into an HTML document. + * + *

+ * 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 attachments = container.attachments; String text = container.text; - String html = container.html; + String html = HtmlConverter.convertEmoji2Img(container.html); String preview = calculateContentPreview(text); @@ -2754,17 +2754,6 @@ public class LocalStore extends Store implements Serializable { } - public String markupContent(String text, String html) { - if (text.length() > 0 && html.length() == 0) { - html = HtmlConverter.textToHtml(text); - } - - html = HtmlConverter.convertEmoji2Img(html); - - return html; - } - - @Override public boolean isInTopGroup() { return mInTopGroup;