From 95159d8121314dfcc2716e9efde5def534be621d Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 8 Dec 2009 04:58:10 +0000 Subject: [PATCH] Defer creation of a MessageId until we actually need it. This was ~75% of our message load-from-db time --- .../email/mail/internet/MimeMessage.java | 37 +++++++++++-------- .../android/email/mail/store/LocalStore.java | 28 ++++++++------ 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/com/android/email/mail/internet/MimeMessage.java b/src/com/android/email/mail/internet/MimeMessage.java index 5052597e1..dcc4241bd 100644 --- a/src/com/android/email/mail/internet/MimeMessage.java +++ b/src/com/android/email/mail/internet/MimeMessage.java @@ -54,23 +54,8 @@ public class MimeMessage extends Message public MimeMessage() { - addHeader("Message-ID", generateMessageId()); } - private String generateMessageId() - { - StringBuffer sb = new StringBuffer(); - sb.append("<"); - for (int i = 0; i < 24; i++) - { - sb.append(Integer.toString((int)(Math.random() * 35), 36)); - } - sb.append("."); - sb.append(Long.toString(System.currentTimeMillis())); - sb.append("@email.android.com>"); - - return sb.toString(); - } /** * Parse the given InputStream using Apache Mime4J to build a MimeMessage. @@ -349,10 +334,29 @@ public class MimeMessage extends Message { mMessageId = getFirstHeader("Message-ID"); } + if (mMessageId == null) // even after checking the header + { + setMessageId(generateMessageId()); + } return mMessageId; } - public void setMessageId(String messageId) throws MessagingException + private String generateMessageId() + { + StringBuffer sb = new StringBuffer(); + sb.append("<"); + for (int i = 0; i < 24; i++) + { + sb.append(Integer.toString((int)(Math.random() * 35), 36)); + } + sb.append("."); + sb.append(Long.toString(System.currentTimeMillis())); + sb.append("@email.android.com>"); + + return sb.toString(); + } + + public void setMessageId(String messageId) { setHeader("Message-ID", messageId); mMessageId = messageId; @@ -437,6 +441,7 @@ public class MimeMessage extends Message public void writeTo(OutputStream out) throws IOException, MessagingException { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024); mHeader.writeTo(out); writer.write("\r\n"); diff --git a/src/com/android/email/mail/store/LocalStore.java b/src/com/android/email/mail/store/LocalStore.java index ccd6acf74..a33095c51 100644 --- a/src/com/android/email/mail/store/LocalStore.java +++ b/src/com/android/email/mail/store/LocalStore.java @@ -1457,11 +1457,12 @@ public class LocalStore extends Store implements Serializable String uid = message.getUid(); if (uid == null) { - message.setUid(Email.LOCAL_UID_PREFIX + UUID.randomUUID().toString()); + uid = Email.LOCAL_UID_PREFIX + UUID.randomUUID().toString(); + message.setUid(uid); } else { - Message oldMessage = getMessage(message.getUid()); + Message oldMessage = getMessage(uid); if (oldMessage != null && oldMessage.isSet(Flag.SEEN) == false) { setUnreadMessageCount(getUnreadMessageCount() - 1); @@ -1510,7 +1511,7 @@ public class LocalStore extends Store implements Serializable try { ContentValues cv = new ContentValues(); - cv.put("uid", message.getUid()); + cv.put("uid", uid); cv.put("subject", message.getSubject()); cv.put("sender_list", Address.pack(message.getFrom())); cv.put("date", message.getSentDate() == null @@ -1527,17 +1528,19 @@ public class LocalStore extends Store implements Serializable cv.put("attachment_count", attachments.size()); cv.put("internal_date", message.getInternalDate() == null ? System.currentTimeMillis() : message.getInternalDate().getTime()); - String[] mHeaders = message.getHeader("Message-ID"); - if (mHeaders != null && mHeaders.length > 0) + String messageId = message.getMessageId(); + Log.e(Email.LOG_TAG, "saving a messag"); + if (messageId != null ) { - cv.put("message_id", mHeaders[0]); + Log.e(Email.LOG_TAG, "saving a message messasgeid is "+messageId); + cv.put("message_id", messageId); } - long messageId = mDb.insert("messages", "uid", cv); + long messageUid = mDb.insert("messages", "uid", cv); for (Part attachment : attachments) { - saveAttachment(messageId, attachment, copy); + saveAttachment(messageUid, attachment, copy); } - saveHeaders(messageId, (MimeMessage)message); + saveHeaders(messageUid, (MimeMessage)message); if (message.isSet(Flag.SEEN) == false) { setUnreadMessageCount(getUnreadMessageCount() + 1); @@ -1565,6 +1568,9 @@ public class LocalStore extends Store implements Serializable open(OpenMode.READ_WRITE); ArrayList viewables = new ArrayList(); ArrayList attachments = new ArrayList(); + + message.buildMimeRepresentation(); + MimeUtility.collectParts(message, viewables, attachments); StringBuffer sbHtml = new StringBuffer(); @@ -2124,7 +2130,7 @@ public class LocalStore extends Store implements Serializable super.setRecipients(RecipientType.TO, mTo); super.setRecipients(RecipientType.CC, mCc); super.setRecipients(RecipientType.BCC, mBcc); - + if (mMessageId != null) super.setMessageId(mMessageId); mMessageDirty = false; return; @@ -2146,7 +2152,7 @@ public class LocalStore extends Store implements Serializable } - public void setMessageId(String messageId) throws MessagingException + public void setMessageId(String messageId) { mMessageId = messageId; mMessageDirty = true;