Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2012, Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.mail.providers; |
| 18 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 19 | import android.content.AsyncQueryHandler; |
| 20 | import android.content.ContentValues; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 21 | import android.database.Cursor; |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 22 | import android.net.Uri; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 25 | import android.provider.BaseColumns; |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 26 | import android.text.Html; |
| 27 | import android.text.SpannedString; |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 28 | import android.text.TextUtils; |
mindyp | 5ed6311 | 2012-09-17 17:31:45 -0700 | [diff] [blame] | 29 | import android.text.util.Rfc822Token; |
| 30 | import android.text.util.Rfc822Tokenizer; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 31 | |
Marc Blank | 26846d8 | 2012-03-22 18:12:54 -0700 | [diff] [blame] | 32 | import com.android.mail.providers.UIProvider.MessageColumns; |
| 33 | import com.android.mail.utils.Utils; |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 34 | import com.google.common.base.Objects; |
Marc Blank | 26846d8 | 2012-03-22 18:12:54 -0700 | [diff] [blame] | 35 | |
Andy Huang | 9ab47b1 | 2012-03-27 19:39:43 -0700 | [diff] [blame] | 36 | import java.util.Collections; |
Andy Huang | 5a92907 | 2012-03-23 20:17:10 -0700 | [diff] [blame] | 37 | import java.util.List; |
Paul Westbrook | 41dca18 | 2012-08-07 10:43:42 -0700 | [diff] [blame] | 38 | import java.util.regex.Pattern; |
Andy Huang | 5a92907 | 2012-03-23 20:17:10 -0700 | [diff] [blame] | 39 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 40 | |
| 41 | public class Message implements Parcelable { |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 42 | /** |
Paul Westbrook | 41dca18 | 2012-08-07 10:43:42 -0700 | [diff] [blame] | 43 | * Regex pattern used to look for any inline images in message bodies, including Gmail-hosted |
| 44 | * relative-URL images, Gmail emoticons, and any external inline images (although we usually |
| 45 | * count on the server to detect external images). |
| 46 | */ |
| 47 | private static Pattern INLINE_IMAGE_PATTERN = Pattern.compile("<img\\s+[^>]*src=", |
| 48 | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); |
| 49 | |
| 50 | /** |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 51 | * @see BaseColumns#_ID |
| 52 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 53 | public long id; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 54 | /** |
| 55 | * @see UIProvider.MessageColumns#SERVER_ID |
| 56 | */ |
Andy Huang | d47877e | 2012-08-09 19:31:24 -0700 | [diff] [blame] | 57 | public String serverId; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 58 | /** |
| 59 | * @see UIProvider.MessageColumns#URI |
| 60 | */ |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 61 | public Uri uri; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 62 | /** |
| 63 | * @see UIProvider.MessageColumns#CONVERSATION_ID |
| 64 | */ |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 65 | public Uri conversationUri; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 66 | /** |
| 67 | * @see UIProvider.MessageColumns#SUBJECT |
| 68 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 69 | public String subject; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 70 | /** |
| 71 | * @see UIProvider.MessageColumns#SNIPPET |
| 72 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 73 | public String snippet; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 74 | /** |
| 75 | * @see UIProvider.MessageColumns#FROM |
| 76 | */ |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 77 | private String mFrom; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 78 | /** |
| 79 | * @see UIProvider.MessageColumns#TO |
| 80 | */ |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 81 | private String mTo; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @see UIProvider.MessageColumns#CC |
| 84 | */ |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 85 | private String mCc; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 86 | /** |
| 87 | * @see UIProvider.MessageColumns#BCC |
| 88 | */ |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 89 | private String mBcc; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 90 | /** |
| 91 | * @see UIProvider.MessageColumns#REPLY_TO |
| 92 | */ |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 93 | private String mReplyTo; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 94 | /** |
| 95 | * @see UIProvider.MessageColumns#DATE_RECEIVED_MS |
| 96 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 97 | public long dateReceivedMs; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 98 | /** |
| 99 | * @see UIProvider.MessageColumns#BODY_HTML |
| 100 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 101 | public String bodyHtml; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 102 | /** |
| 103 | * @see UIProvider.MessageColumns#BODY_TEXT |
| 104 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 105 | public String bodyText; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 106 | /** |
| 107 | * @see UIProvider.MessageColumns#EMBEDS_EXTERNAL_RESOURCES |
| 108 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 109 | public boolean embedsExternalResources; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 110 | /** |
| 111 | * @see UIProvider.MessageColumns#REF_MESSAGE_ID |
| 112 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 113 | public String refMessageId; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 114 | /** |
| 115 | * @see UIProvider.MessageColumns#DRAFT_TYPE |
| 116 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 117 | public int draftType; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 118 | /** |
| 119 | * @see UIProvider.MessageColumns#APPEND_REF_MESSAGE_CONTENT |
| 120 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 121 | public boolean appendRefMessageContent; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 122 | /** |
| 123 | * @see UIProvider.MessageColumns#HAS_ATTACHMENTS |
| 124 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 125 | public boolean hasAttachments; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 126 | /** |
| 127 | * @see UIProvider.MessageColumns#ATTACHMENT_LIST_URI |
| 128 | */ |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 129 | public Uri attachmentListUri; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 130 | /** |
| 131 | * @see UIProvider.MessageColumns#MESSAGE_FLAGS |
| 132 | */ |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 133 | public long messageFlags; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 134 | /** |
| 135 | * @see UIProvider.MessageColumns#SAVE_MESSAGE_URI |
| 136 | */ |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 137 | @Deprecated |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 138 | public String saveUri; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 139 | /** |
| 140 | * @see UIProvider.MessageColumns#SEND_MESSAGE_URI |
| 141 | */ |
Paul Westbrook | 72e2ea8 | 2012-10-22 16:25:22 -0700 | [diff] [blame] | 142 | @Deprecated |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 143 | public String sendUri; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 144 | /** |
| 145 | * @see UIProvider.MessageColumns#ALWAYS_SHOW_IMAGES |
| 146 | */ |
Paul Westbrook | 104f729 | 2012-02-28 16:07:07 -0800 | [diff] [blame] | 147 | public boolean alwaysShowImages; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 148 | /** |
| 149 | * @see UIProvider.MessageColumns#READ |
| 150 | */ |
Andy Huang | d8e249e | 2012-03-21 17:01:37 -0700 | [diff] [blame] | 151 | public boolean read; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 152 | /** |
| 153 | * @see UIProvider.MessageColumns#STARRED |
| 154 | */ |
Andy Huang | d8e249e | 2012-03-21 17:01:37 -0700 | [diff] [blame] | 155 | public boolean starred; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 156 | /** |
| 157 | * @see UIProvider.MessageColumns#QUOTE_START_POS |
| 158 | */ |
Mindy Pereira | 4dbbf9f | 2012-03-20 15:48:26 -0700 | [diff] [blame] | 159 | public int quotedTextOffset; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 160 | /** |
| 161 | * @see UIProvider.MessageColumns#ATTACHMENTS |
Andy Huang | 6766b6e | 2012-09-28 12:43:52 -0700 | [diff] [blame] | 162 | *<p> |
| 163 | * N.B. this value is NOT immutable and may change during conversation view render. |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 164 | */ |
Andy Huang | 5a92907 | 2012-03-23 20:17:10 -0700 | [diff] [blame] | 165 | public String attachmentsJson; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 166 | /** |
| 167 | * @see UIProvider.MessageColumns#MESSAGE_ACCOUNT_URI |
| 168 | */ |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 169 | public Uri accountUri; |
Andy Huang | e623a0f | 2012-07-12 15:01:23 -0700 | [diff] [blame] | 170 | /** |
| 171 | * @see UIProvider.MessageColumns#EVENT_INTENT_URI |
| 172 | */ |
Marc Blank | 3842af9 | 2012-04-27 09:06:09 -0700 | [diff] [blame] | 173 | public Uri eventIntentUri; |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 174 | /** |
| 175 | * @see UIProvider.MessageColumns#SPAM_WARNING_STRING |
| 176 | */ |
Andrew Sapperstein | 3b08c51 | 2012-07-11 16:51:07 -0700 | [diff] [blame] | 177 | public String spamWarningString; |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 178 | /** |
| 179 | * @see UIProvider.MessageColumns#SPAM_WARNING_LEVEL |
| 180 | */ |
Andrew Sapperstein | 3b08c51 | 2012-07-11 16:51:07 -0700 | [diff] [blame] | 181 | public int spamWarningLevel; |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 182 | /** |
| 183 | * @see UIProvider.MessageColumns#SPAM_WARNING_LINK_TYPE |
| 184 | */ |
Andrew Sapperstein | 3b08c51 | 2012-07-11 16:51:07 -0700 | [diff] [blame] | 185 | public int spamLinkType; |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 186 | /** |
| 187 | * @see UIProvider.MessageColumns#VIA_DOMAIN |
| 188 | */ |
| 189 | public String viaDomain; |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 190 | /** |
| 191 | * @see UIProvider.MessageColumns#IS_SENDING |
| 192 | */ |
| 193 | public boolean isSending; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 194 | |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 195 | private transient String[] mFromAddresses = null; |
Andy Huang | 5a92907 | 2012-03-23 20:17:10 -0700 | [diff] [blame] | 196 | private transient String[] mToAddresses = null; |
| 197 | private transient String[] mCcAddresses = null; |
| 198 | private transient String[] mBccAddresses = null; |
| 199 | private transient String[] mReplyToAddresses = null; |
| 200 | |
| 201 | private transient List<Attachment> mAttachments = null; |
mindyp | 845f87e | 2012-09-24 15:14:49 -0700 | [diff] [blame] | 202 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 203 | @Override |
| 204 | public int describeContents() { |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | @Override |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 209 | public boolean equals(Object o) { |
Andy Huang | cd5c5ee | 2012-08-12 19:03:51 -0700 | [diff] [blame] | 210 | return this == o || (o != null && o instanceof Message |
| 211 | && Objects.equal(uri, ((Message) o).uri)); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public int hashCode() { |
| 216 | return uri == null ? 0 : uri.hashCode(); |
| 217 | } |
| 218 | |
| 219 | @Override |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 220 | public void writeToParcel(Parcel dest, int flags) { |
| 221 | dest.writeLong(id); |
Andy Huang | d47877e | 2012-08-09 19:31:24 -0700 | [diff] [blame] | 222 | dest.writeString(serverId); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 223 | dest.writeParcelable(uri, 0); |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 224 | dest.writeParcelable(conversationUri, 0); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 225 | dest.writeString(subject); |
| 226 | dest.writeString(snippet); |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 227 | dest.writeString(mFrom); |
| 228 | dest.writeString(mTo); |
| 229 | dest.writeString(mCc); |
| 230 | dest.writeString(mBcc); |
| 231 | dest.writeString(mReplyTo); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 232 | dest.writeLong(dateReceivedMs); |
| 233 | dest.writeString(bodyHtml); |
| 234 | dest.writeString(bodyText); |
| 235 | dest.writeInt(embedsExternalResources ? 1 : 0); |
| 236 | dest.writeString(refMessageId); |
| 237 | dest.writeInt(draftType); |
| 238 | dest.writeInt(appendRefMessageContent ? 1 : 0); |
| 239 | dest.writeInt(hasAttachments ? 1 : 0); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 240 | dest.writeParcelable(attachmentListUri, 0); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 241 | dest.writeLong(messageFlags); |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 242 | dest.writeString(saveUri); |
| 243 | dest.writeString(sendUri); |
Paul Westbrook | 104f729 | 2012-02-28 16:07:07 -0800 | [diff] [blame] | 244 | dest.writeInt(alwaysShowImages ? 1 : 0); |
Mindy Pereira | 4dbbf9f | 2012-03-20 15:48:26 -0700 | [diff] [blame] | 245 | dest.writeInt(quotedTextOffset); |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 246 | dest.writeString(attachmentsJson); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 247 | dest.writeParcelable(accountUri, 0); |
Marc Blank | 3842af9 | 2012-04-27 09:06:09 -0700 | [diff] [blame] | 248 | dest.writeParcelable(eventIntentUri, 0); |
Andrew Sapperstein | 3b08c51 | 2012-07-11 16:51:07 -0700 | [diff] [blame] | 249 | dest.writeString(spamWarningString); |
| 250 | dest.writeInt(spamWarningLevel); |
| 251 | dest.writeInt(spamLinkType); |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 252 | dest.writeString(viaDomain); |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 253 | dest.writeInt(isSending ? 1 : 0); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | private Message(Parcel in) { |
| 257 | id = in.readLong(); |
Andy Huang | d47877e | 2012-08-09 19:31:24 -0700 | [diff] [blame] | 258 | serverId = in.readString(); |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 259 | uri = in.readParcelable(null); |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 260 | conversationUri = in.readParcelable(null); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 261 | subject = in.readString(); |
| 262 | snippet = in.readString(); |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 263 | mFrom = in.readString(); |
| 264 | mTo = in.readString(); |
| 265 | mCc = in.readString(); |
| 266 | mBcc = in.readString(); |
| 267 | mReplyTo = in.readString(); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 268 | dateReceivedMs = in.readLong(); |
| 269 | bodyHtml = in.readString(); |
| 270 | bodyText = in.readString(); |
| 271 | embedsExternalResources = in.readInt() != 0; |
| 272 | refMessageId = in.readString(); |
| 273 | draftType = in.readInt(); |
| 274 | appendRefMessageContent = in.readInt() != 0; |
| 275 | hasAttachments = in.readInt() != 0; |
Mindy Pereira | cfb7f33 | 2012-02-28 10:23:43 -0800 | [diff] [blame] | 276 | attachmentListUri = in.readParcelable(null); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 277 | messageFlags = in.readLong(); |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 278 | saveUri = in.readString(); |
| 279 | sendUri = in.readString(); |
Paul Westbrook | 104f729 | 2012-02-28 16:07:07 -0800 | [diff] [blame] | 280 | alwaysShowImages = in.readInt() != 0; |
Mindy Pereira | 4dbbf9f | 2012-03-20 15:48:26 -0700 | [diff] [blame] | 281 | quotedTextOffset = in.readInt(); |
Mindy Pereira | 2421dc8 | 2012-03-27 13:32:31 -0700 | [diff] [blame] | 282 | attachmentsJson = in.readString(); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 283 | accountUri = in.readParcelable(null); |
Marc Blank | 3842af9 | 2012-04-27 09:06:09 -0700 | [diff] [blame] | 284 | eventIntentUri = in.readParcelable(null); |
Andrew Sapperstein | 3b08c51 | 2012-07-11 16:51:07 -0700 | [diff] [blame] | 285 | spamWarningString = in.readString(); |
| 286 | spamWarningLevel = in.readInt(); |
| 287 | spamLinkType = in.readInt(); |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 288 | viaDomain = in.readString(); |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 289 | isSending = in.readInt() != 0; |
Paul Westbrook | 104f729 | 2012-02-28 16:07:07 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | public Message() { |
| 293 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public String toString() { |
| 298 | return "[message id=" + id + "]"; |
| 299 | } |
| 300 | |
| 301 | public static final Creator<Message> CREATOR = new Creator<Message>() { |
| 302 | |
| 303 | @Override |
| 304 | public Message createFromParcel(Parcel source) { |
| 305 | return new Message(source); |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public Message[] newArray(int size) { |
| 310 | return new Message[size]; |
| 311 | } |
| 312 | |
| 313 | }; |
| 314 | |
Mindy Pereira | a831b2f | 2012-02-23 19:24:40 -0800 | [diff] [blame] | 315 | public Message(Cursor cursor) { |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 316 | if (cursor != null) { |
| 317 | id = cursor.getLong(UIProvider.MESSAGE_ID_COLUMN); |
Andy Huang | d47877e | 2012-08-09 19:31:24 -0700 | [diff] [blame] | 318 | serverId = cursor.getString(UIProvider.MESSAGE_SERVER_ID_COLUMN); |
Andy Huang | daa06ab | 2012-07-24 10:46:44 -0700 | [diff] [blame] | 319 | final String messageUriStr = cursor.getString(UIProvider.MESSAGE_URI_COLUMN); |
| 320 | uri = !TextUtils.isEmpty(messageUriStr) ? Uri.parse(messageUriStr) : null; |
| 321 | final String convUriStr = cursor.getString(UIProvider.MESSAGE_CONVERSATION_URI_COLUMN); |
| 322 | conversationUri = !TextUtils.isEmpty(convUriStr) ? Uri.parse(convUriStr) : null; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 323 | subject = cursor.getString(UIProvider.MESSAGE_SUBJECT_COLUMN); |
| 324 | snippet = cursor.getString(UIProvider.MESSAGE_SNIPPET_COLUMN); |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 325 | mFrom = cursor.getString(UIProvider.MESSAGE_FROM_COLUMN); |
| 326 | mTo = cursor.getString(UIProvider.MESSAGE_TO_COLUMN); |
| 327 | mCc = cursor.getString(UIProvider.MESSAGE_CC_COLUMN); |
| 328 | mBcc = cursor.getString(UIProvider.MESSAGE_BCC_COLUMN); |
| 329 | mReplyTo = cursor.getString(UIProvider.MESSAGE_REPLY_TO_COLUMN); |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 330 | dateReceivedMs = cursor.getLong(UIProvider.MESSAGE_DATE_RECEIVED_MS_COLUMN); |
| 331 | bodyHtml = cursor.getString(UIProvider.MESSAGE_BODY_HTML_COLUMN); |
| 332 | bodyText = cursor.getString(UIProvider.MESSAGE_BODY_TEXT_COLUMN); |
| 333 | embedsExternalResources = cursor |
| 334 | .getInt(UIProvider.MESSAGE_EMBEDS_EXTERNAL_RESOURCES_COLUMN) != 0; |
| 335 | refMessageId = cursor.getString(UIProvider.MESSAGE_REF_MESSAGE_ID_COLUMN); |
| 336 | draftType = cursor.getInt(UIProvider.MESSAGE_DRAFT_TYPE_COLUMN); |
| 337 | appendRefMessageContent = cursor |
| 338 | .getInt(UIProvider.MESSAGE_APPEND_REF_MESSAGE_CONTENT_COLUMN) != 0; |
| 339 | hasAttachments = cursor.getInt(UIProvider.MESSAGE_HAS_ATTACHMENTS_COLUMN) != 0; |
Mindy Pereira | 7bb2725 | 2012-03-07 10:17:09 -0800 | [diff] [blame] | 340 | final String attachmentsUri = cursor |
| 341 | .getString(UIProvider.MESSAGE_ATTACHMENT_LIST_URI_COLUMN); |
| 342 | attachmentListUri = hasAttachments && !TextUtils.isEmpty(attachmentsUri) ? Uri |
| 343 | .parse(attachmentsUri) : null; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 344 | messageFlags = cursor.getLong(UIProvider.MESSAGE_FLAGS_COLUMN); |
Mindy Pereira | 7ed1c11 | 2012-01-18 10:59:25 -0800 | [diff] [blame] | 345 | saveUri = cursor |
| 346 | .getString(UIProvider.MESSAGE_SAVE_URI_COLUMN); |
| 347 | sendUri = cursor |
| 348 | .getString(UIProvider.MESSAGE_SEND_URI_COLUMN); |
Andy Huang | d8e249e | 2012-03-21 17:01:37 -0700 | [diff] [blame] | 349 | alwaysShowImages = cursor.getInt(UIProvider.MESSAGE_ALWAYS_SHOW_IMAGES_COLUMN) != 0; |
| 350 | read = cursor.getInt(UIProvider.MESSAGE_READ_COLUMN) != 0; |
| 351 | starred = cursor.getInt(UIProvider.MESSAGE_STARRED_COLUMN) != 0; |
Mindy Pereira | 4dbbf9f | 2012-03-20 15:48:26 -0700 | [diff] [blame] | 352 | quotedTextOffset = cursor.getInt(UIProvider.QUOTED_TEXT_OFFSET_COLUMN); |
Andy Huang | 5a92907 | 2012-03-23 20:17:10 -0700 | [diff] [blame] | 353 | attachmentsJson = cursor.getString(UIProvider.MESSAGE_ATTACHMENTS_COLUMN); |
Mindy Pereira | 62de1b1 | 2012-04-06 12:17:56 -0700 | [diff] [blame] | 354 | String accountUriString = cursor.getString(UIProvider.MESSAGE_ACCOUNT_URI_COLUMN); |
| 355 | accountUri = !TextUtils.isEmpty(accountUriString) ? Uri.parse(accountUriString) : null; |
Marc Blank | 3842af9 | 2012-04-27 09:06:09 -0700 | [diff] [blame] | 356 | eventIntentUri = |
| 357 | Utils.getValidUri(cursor.getString(UIProvider.MESSAGE_EVENT_INTENT_COLUMN)); |
Andrew Sapperstein | 3b08c51 | 2012-07-11 16:51:07 -0700 | [diff] [blame] | 358 | spamWarningString = |
| 359 | cursor.getString(UIProvider.MESSAGE_SPAM_WARNING_STRING_ID_COLUMN); |
| 360 | spamWarningLevel = cursor.getInt(UIProvider.MESSAGE_SPAM_WARNING_LEVEL_COLUMN); |
| 361 | spamLinkType = cursor.getInt(UIProvider.MESSAGE_SPAM_WARNING_LINK_TYPE_COLUMN); |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 362 | viaDomain = cursor.getString(UIProvider.MESSAGE_VIA_DOMAIN_COLUMN); |
Andy Huang | 47aa9c9 | 2012-07-31 15:37:21 -0700 | [diff] [blame] | 363 | isSending = cursor.getInt(UIProvider.MESSAGE_IS_SENDING_COLUMN) != 0; |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 364 | } |
| 365 | } |
Andy Huang | 0a22047 | 2012-03-14 17:36:55 -0700 | [diff] [blame] | 366 | |
Andy Huang | a71f1f3 | 2012-04-24 20:17:22 -0700 | [diff] [blame] | 367 | public boolean isFlaggedReplied() { |
| 368 | return (messageFlags & UIProvider.MessageFlags.REPLIED) == |
| 369 | UIProvider.MessageFlags.REPLIED; |
| 370 | } |
| 371 | |
| 372 | public boolean isFlaggedForwarded() { |
| 373 | return (messageFlags & UIProvider.MessageFlags.FORWARDED) == |
| 374 | UIProvider.MessageFlags.FORWARDED; |
| 375 | } |
| 376 | |
| 377 | public boolean isFlaggedCalendarInvite() { |
| 378 | return (messageFlags & UIProvider.MessageFlags.CALENDAR_INVITE) == |
| 379 | UIProvider.MessageFlags.CALENDAR_INVITE; |
| 380 | } |
| 381 | |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 382 | public String getFrom() { |
| 383 | return mFrom; |
| 384 | } |
| 385 | |
| 386 | public synchronized void setFrom(final String from) { |
| 387 | mFrom = from; |
| 388 | mFromAddresses = null; |
| 389 | } |
| 390 | |
| 391 | public String getTo() { |
| 392 | return mTo; |
| 393 | } |
| 394 | |
| 395 | public synchronized void setTo(final String to) { |
| 396 | mTo = to; |
| 397 | mToAddresses = null; |
| 398 | } |
| 399 | |
| 400 | public String getCc() { |
| 401 | return mCc; |
| 402 | } |
| 403 | |
| 404 | public synchronized void setCc(final String cc) { |
| 405 | mCc = cc; |
| 406 | mCcAddresses = null; |
| 407 | } |
| 408 | |
| 409 | public String getBcc() { |
| 410 | return mBcc; |
| 411 | } |
| 412 | |
| 413 | public synchronized void setBcc(final String bcc) { |
| 414 | mBcc = bcc; |
| 415 | mBccAddresses = null; |
| 416 | } |
| 417 | |
| 418 | public String getReplyTo() { |
| 419 | return mReplyTo; |
| 420 | } |
| 421 | |
| 422 | public synchronized void setReplyTo(final String replyTo) { |
| 423 | mReplyTo = replyTo; |
| 424 | mReplyToAddresses = null; |
| 425 | } |
| 426 | |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 427 | public synchronized String[] getFromAddresses() { |
| 428 | if (mFromAddresses == null) { |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 429 | mFromAddresses = tokenizeAddresses(mFrom); |
Andrew Sapperstein | d5b369b | 2012-07-13 12:38:46 -0700 | [diff] [blame] | 430 | } |
| 431 | return mFromAddresses; |
| 432 | } |
| 433 | |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 434 | public synchronized String[] getToAddresses() { |
| 435 | if (mToAddresses == null) { |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 436 | mToAddresses = tokenizeAddresses(mTo); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 437 | } |
| 438 | return mToAddresses; |
| 439 | } |
| 440 | |
| 441 | public synchronized String[] getCcAddresses() { |
| 442 | if (mCcAddresses == null) { |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 443 | mCcAddresses = tokenizeAddresses(mCc); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 444 | } |
| 445 | return mCcAddresses; |
| 446 | } |
| 447 | |
| 448 | public synchronized String[] getBccAddresses() { |
| 449 | if (mBccAddresses == null) { |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 450 | mBccAddresses = tokenizeAddresses(mBcc); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 451 | } |
| 452 | return mBccAddresses; |
| 453 | } |
| 454 | |
| 455 | public synchronized String[] getReplyToAddresses() { |
| 456 | if (mReplyToAddresses == null) { |
Scott Kennedy | 8960f0a | 2012-11-07 15:35:50 -0800 | [diff] [blame^] | 457 | mReplyToAddresses = tokenizeAddresses(mReplyTo); |
Paul Westbrook | bb87b7f | 2012-03-20 16:20:07 -0700 | [diff] [blame] | 458 | } |
| 459 | return mReplyToAddresses; |
| 460 | } |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 461 | |
Paul Westbrook | 233f562 | 2012-09-27 16:18:11 -0700 | [diff] [blame] | 462 | public static String[] tokenizeAddresses(String addresses) { |
mindyp | 5ed6311 | 2012-09-17 17:31:45 -0700 | [diff] [blame] | 463 | if (TextUtils.isEmpty(addresses)) { |
| 464 | return new String[0]; |
| 465 | } |
mindyp | 845f87e | 2012-09-24 15:14:49 -0700 | [diff] [blame] | 466 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addresses); |
mindyp | 5ed6311 | 2012-09-17 17:31:45 -0700 | [diff] [blame] | 467 | String[] strings = new String[tokens.length]; |
| 468 | for (int i = 0; i < tokens.length;i++) { |
| 469 | strings[i] = tokens[i].toString(); |
| 470 | } |
| 471 | return strings; |
| 472 | } |
Andy Huang | 6766b6e | 2012-09-28 12:43:52 -0700 | [diff] [blame] | 473 | |
| 474 | public List<Attachment> getAttachments() { |
Andy Huang | 9ab47b1 | 2012-03-27 19:39:43 -0700 | [diff] [blame] | 475 | if (mAttachments == null) { |
| 476 | if (attachmentsJson != null) { |
| 477 | mAttachments = Attachment.fromJSONArray(attachmentsJson); |
| 478 | } else { |
| 479 | mAttachments = Collections.emptyList(); |
| 480 | } |
Andy Huang | 5a92907 | 2012-03-23 20:17:10 -0700 | [diff] [blame] | 481 | } |
| 482 | return mAttachments; |
| 483 | } |
| 484 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 485 | /** |
| 486 | * Returns whether a "Show Pictures" button should initially appear for this message. If the |
| 487 | * button is shown, the message must also block all non-local images in the body. Inversely, if |
| 488 | * the button is not shown, the message must show all images within (or else the user would be |
| 489 | * stuck with no images and no way to reveal them). |
| 490 | * |
| 491 | * @return true if a "Show Pictures" button should appear. |
| 492 | */ |
| 493 | public boolean shouldShowImagePrompt() { |
Paul Westbrook | 41dca18 | 2012-08-07 10:43:42 -0700 | [diff] [blame] | 494 | return !alwaysShowImages && embedsExternalResources(); |
| 495 | } |
| 496 | |
| 497 | private boolean embedsExternalResources() { |
| 498 | return embedsExternalResources || |
| 499 | (!TextUtils.isEmpty(bodyHtml) && INLINE_IMAGE_PATTERN.matcher(bodyHtml).find()); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Helper method to command a provider to mark all messages from this sender with the |
| 504 | * {@link MessageColumns#ALWAYS_SHOW_IMAGES} flag set. |
| 505 | * |
| 506 | * @param handler a caller-provided handler to run the query on |
| 507 | * @param token (optional) token to identify the command to the handler |
| 508 | * @param cookie (optional) cookie to pass to the handler |
| 509 | */ |
| 510 | public void markAlwaysShowImages(AsyncQueryHandler handler, int token, Object cookie) { |
Andy Huang | 839ada2 | 2012-07-20 15:48:40 -0700 | [diff] [blame] | 511 | alwaysShowImages = true; |
| 512 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 513 | final ContentValues values = new ContentValues(1); |
| 514 | values.put(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES, 1); |
| 515 | |
| 516 | handler.startUpdate(token, cookie, uri, values, null, null); |
| 517 | } |
| 518 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 519 | public String getBodyAsHtml() { |
| 520 | String body = ""; |
| 521 | if (!TextUtils.isEmpty(bodyHtml)) { |
| 522 | body = bodyHtml; |
| 523 | } else if (!TextUtils.isEmpty(bodyText)) { |
| 524 | body = Html.toHtml(new SpannedString(bodyText)); |
| 525 | } |
| 526 | return body; |
| 527 | } |
| 528 | |
Mindy Pereira | 3ce64e7 | 2012-01-13 14:29:45 -0800 | [diff] [blame] | 529 | } |