From 548b8e07a6f8ae10246914952712b2565fdde6c7 Mon Sep 17 00:00:00 2001 From: Joe Steele Date: Fri, 1 Mar 2013 11:26:52 -0500 Subject: [PATCH] Refactor code for MessageWebView.setText() The MIME type for the supplied text was always text/html, so there is no need to pass that as a parameter. Furthermore, we are relying on it being text/html because we are wrapping it with HTML code. Likewise, change/simplify/rename AccessibleWebView.loadDataWithBaseURL(). --- src/com/fsck/k9/activity/MessageCompose.java | 6 +++--- src/com/fsck/k9/helper/HtmlConverter.java | 8 ++++++-- src/com/fsck/k9/view/AccessibleWebView.java | 6 ++---- src/com/fsck/k9/view/MessageWebView.java | 16 ++++++++++++++-- src/com/fsck/k9/view/SingleMessageView.java | 14 +++++++------- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index b2fb4e3f9..f592ff03a 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -1126,7 +1126,7 @@ public class MessageCompose extends K9Activity implements OnClickListener { mQuotedHtmlContent = (InsertableHtmlContent) savedInstanceState.getSerializable(STATE_KEY_HTML_QUOTE); if (mQuotedHtmlContent != null && mQuotedHtmlContent.getQuotedContent() != null) { - mQuotedHTML.setText(mQuotedHtmlContent.getQuotedContent(), "text/html"); + mQuotedHTML.setText(mQuotedHtmlContent.getQuotedContent()); } mDraftId = savedInstanceState.getLong(STATE_KEY_DRAFT_ID); @@ -2815,7 +2815,7 @@ public class MessageCompose extends K9Activity implements OnClickListener { } else { mQuotedHtmlContent.setFooterInsertionPoint(bodyOffset); } - mQuotedHTML.setText(mQuotedHtmlContent.getQuotedContent(), "text/html"); + mQuotedHTML.setText(mQuotedHtmlContent.getQuotedContent()); } } if (bodyPlainOffset != null && bodyPlainLength != null) { @@ -2999,7 +2999,7 @@ public class MessageCompose extends K9Activity implements OnClickListener { mQuotedHtmlContent = quoteOriginalHtmlMessage(mSourceMessage, content, mQuoteStyle); // Load the message with the reply header. - mQuotedHTML.setText(mQuotedHtmlContent.getQuotedContent(), "text/html"); + mQuotedHTML.setText(mQuotedHtmlContent.getQuotedContent()); // TODO: Also strip the signature from the text/plain part mQuotedText.setText(quoteOriginalTextMessage(mSourceMessage, diff --git a/src/com/fsck/k9/helper/HtmlConverter.java b/src/com/fsck/k9/helper/HtmlConverter.java index 5478f2e25..e5ab099bd 100644 --- a/src/com/fsck/k9/helper/HtmlConverter.java +++ b/src/com/fsck/k9/helper/HtmlConverter.java @@ -133,7 +133,9 @@ public class HtmlConverter { * This method avoids using regular expressions on the entire message body to save memory. *

*

- * No HTML headers or footers are added to the result. + * No HTML headers or footers are added to the result. Headers and footers + * are added at display time in + * {@link com.fsck.k9.view#MessageWebView.setText(String) MessageWebView.setText()} *

* * @param text @@ -187,7 +189,9 @@ public class HtmlConverter { * Attempts to do smart replacement for large documents to prevent OOM * errors. *

- * No HTML headers or footers are added to the result. + * No HTML headers or footers are added to the result. Headers and footers + * are added at display time in + * {@link com.fsck.k9.view#MessageWebView.setText(String) MessageWebView.setText()} *

*

* To convert to a fragment, use {@link #textToHtmlFragment(String)} . diff --git a/src/com/fsck/k9/view/AccessibleWebView.java b/src/com/fsck/k9/view/AccessibleWebView.java index 5dd8bb9a8..323833d26 100644 --- a/src/com/fsck/k9/view/AccessibleWebView.java +++ b/src/com/fsck/k9/view/AccessibleWebView.java @@ -68,10 +68,8 @@ public class AccessibleWebView extends TextView { return mDummyWebView.getSettings(); } - public void loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, - String historyUrl) { - mHtmlSource = data; - this.setText(Html.fromHtml(mHtmlSource, null, null)); + public void setText(String text) { + this.setText(Html.fromHtml(text, null, null)); // Let everyone know that loading has finished. if (mListeners != null) { diff --git a/src/com/fsck/k9/view/MessageWebView.java b/src/com/fsck/k9/view/MessageWebView.java index 394c0aa9e..e673f4134 100644 --- a/src/com/fsck/k9/view/MessageWebView.java +++ b/src/com/fsck/k9/view/MessageWebView.java @@ -151,7 +151,19 @@ public class MessageWebView extends TitleBarWebView { } } - public void setText(String text, String contentType) { + /** + * Load a message body into a {@code MessageWebView} + * + *

+ * Before loading, the text is wrapped in an HTML header and footer + * so that it displays properly. + *

+ * + * @param text + * The message body to display. Assumed to be MIME type text/html. + */ + public void setText(String text) { + // Include a meta tag so the WebView will not use a fixed viewport width of 980 px String content = ""; if (K9.getK9MessageViewTheme() == K9.Theme.DARK) { content += " "; } content += "" + text + ""; - loadDataWithBaseURL("http://", content, contentType, "utf-8", null); + loadDataWithBaseURL("http://", content, "text/html", "utf-8", null); } /* diff --git a/src/com/fsck/k9/view/SingleMessageView.java b/src/com/fsck/k9/view/SingleMessageView.java index 40b6ba68c..b9b982422 100644 --- a/src/com/fsck/k9/view/SingleMessageView.java +++ b/src/com/fsck/k9/view/SingleMessageView.java @@ -400,7 +400,7 @@ public class SingleMessageView extends LinearLayout implements OnClickListener, // Allow network access first... setLoadPictures(true); // ...then re-populate the WebView with the message text - loadBodyFromText(mText, "text/html"); + loadBodyFromText(mText); break; } } @@ -611,7 +611,7 @@ public class SingleMessageView extends LinearLayout implements OnClickListener, } if (text != null) { - loadBodyFromText(text, "text/html"); + loadBodyFromText(text); updateCryptoLayout(account.getCryptoProvider(), pgpData, message); } else { showStatusMessage(getContext().getString(R.string.webview_empty_message)); @@ -622,15 +622,15 @@ public class SingleMessageView extends LinearLayout implements OnClickListener, String text = "
" + status + "
"; - loadBodyFromText(text, "text/html"); + loadBodyFromText(text); mCryptoView.hide(); } - private void loadBodyFromText(String emailText, String contentType) { + private void loadBodyFromText(String emailText) { if (mScreenReaderEnabled) { - mAccessibleMessageContentView.loadDataWithBaseURL("http://", emailText, contentType, "utf-8", null); + mAccessibleMessageContentView.setText(emailText); } else { - mMessageContentView.setText(emailText, contentType); + mMessageContentView.setText(emailText); } } @@ -734,7 +734,7 @@ public class SingleMessageView extends LinearLayout implements OnClickListener, * its size because the button to download the complete message was previously shown and * is now hidden. */ - loadBodyFromText("", "text/plain"); + loadBodyFromText(""); } public void resetHeaderView() {