From 323c2942503cf993071bd047f9750bb420282c90 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Sun, 3 May 2009 20:52:32 +0000 Subject: [PATCH] Move our linkification and htmlification of mesages from on-display to on-sync. There's no need to defer this work until the user is waiting for it --- .../android/email/activity/MessageView.java | 40 +------------ .../android/email/mail/store/LocalStore.java | 58 ++++++++++++++++++- 2 files changed, 60 insertions(+), 38 deletions(-) diff --git a/src/com/android/email/activity/MessageView.java b/src/com/android/email/activity/MessageView.java index 8ab6a34a3..dd1dd3755 100644 --- a/src/com/android/email/activity/MessageView.java +++ b/src/com/android/email/activity/MessageView.java @@ -31,7 +31,6 @@ import android.text.Spannable; import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.util.Regex; -import android.text.util.Linkify; import android.util.Config; import android.util.Log; import android.view.KeyEvent; @@ -1065,41 +1064,6 @@ public class MessageView extends Activity } if (part != null) { String text = MimeUtility.getTextFromPart(part); - if (part.getMimeType().equalsIgnoreCase("text/html")) { - markup = new SpannableString(text.replaceAll("cid:", "http://cid/")); - Linkify.addLinks(markup, Linkify.ALL); - text = markup.toString(); - } else { - if(text.length() != 0) { - /* - * Convert plain text to HTML by replacing - * \r?\n with
and adding a html/body wrapper. - */ - text = text.replaceAll("&", "&"); - text = text.replaceAll("<", "<"); - text = text.replaceAll(">", ">"); - text = text.replaceAll("\r?\n", "
"); - Matcher m = Regex.WEB_URL_PATTERN.matcher(text); - StringBuffer sb = new StringBuffer(text.length() + 512); - sb.append(""); - while (m.find()) { - int start = m.start(); - if (start == 0 || (start != 0 && text.charAt(start - 1) != '@')) { - m.appendReplacement(sb, "$0"); - } - else { - m.appendReplacement(sb, "$0"); - } - } - m.appendTail(sb); - sb.append(""); - markup = new SpannableStringBuilder(sb, 0, sb.length()); - Linkify.addLinks(markup, Linkify.ALL); - text = markup.toString(); - } - else - text = ""; - } /* * TODO this should be smarter, change to regex for img, but consider how to * get background images and a million other things that HTML allows. @@ -1117,7 +1081,9 @@ public class MessageView extends Activity } } } - + + + @Override public void loadMessageForViewFailed(Account account, String folder, String uid, final String message) { diff --git a/src/com/android/email/mail/store/LocalStore.java b/src/com/android/email/mail/store/LocalStore.java index 4cccfde04..4585393eb 100644 --- a/src/com/android/email/mail/store/LocalStore.java +++ b/src/com/android/email/mail/store/LocalStore.java @@ -15,6 +15,7 @@ import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; import java.util.UUID; +import java.util.regex.Matcher; import android.content.SharedPreferences; import org.apache.commons.io.IOUtils; @@ -26,6 +27,11 @@ import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.util.Config; import android.util.Log; +import android.text.util.Regex; +import android.text.util.Linkify; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.SpannableStringBuilder; import com.android.email.Email; import com.android.email.Preferences; @@ -57,7 +63,7 @@ import com.android.email.provider.AttachmentProvider; * */ public class LocalStore extends Store implements Serializable { - private static final int DB_VERSION = 23; + private static final int DB_VERSION = 24; private static final Flag[] PERMANENT_FLAGS = { Flag.DELETED, Flag.X_DESTROYED, Flag.SEEN }; private String mPath; @@ -1080,6 +1086,13 @@ public class LocalStore extends Store implements Serializable { } } + + + sbHtml = markupContent(sbText,sbHtml); + sbText = new StringBuffer(); + + + try { ContentValues cv = new ContentValues(); cv.put("uid", message.getUid()); @@ -1150,6 +1163,10 @@ public class LocalStore extends Store implements Serializable { } } + sbHtml = markupContent(sbText,sbHtml); + sbText = new StringBuffer(); + + try { mDb.execSQL("UPDATE messages SET " + "uid = ?, subject = ?, sender_list = ?, date = ?, flags = ?, " @@ -1423,6 +1440,45 @@ public class LocalStore extends Store implements Serializable { } } } + + + public StringBuffer markupContent(StringBuffer sbText, StringBuffer sbHtml) { + if (sbText.length() > 0 && sbHtml.length() == 0) { + sbHtml.append(htmlifyString(sbText.toString())); + } + + Spannable markup = new SpannableString(sbHtml.toString().replaceAll("cid:", "http://cid/")); + Linkify.addLinks(markup, Linkify.ALL); + StringBuffer sb = new StringBuffer(markup.length()); + sb.append(markup.toString()); + return sb; + + } + + public String htmlifyString(String text) { + text = text.replaceAll("&", "&"); + text = text.replaceAll("<", "<"); + text = text.replaceAll(">", ">"); + text = text.replaceAll("\r?\n", "
"); + Matcher m = Regex.WEB_URL_PATTERN.matcher(text); + StringBuffer sb = new StringBuffer(text.length() + 512); + sb.append(""); + while (m.find()) { + int start = m.start(); + if (start == 0 || (start != 0 && text.charAt(start - 1) != '@')) { + m.appendReplacement(sb, "$0"); + } + else { + m.appendReplacement(sb, "$0"); + } + } + m.appendTail(sb); + sb.append(""); + text = sb.toString(); + + return text; + } + } public class LocalMessage extends MimeMessage {