Message.java revision 014ea4c15d147794789b9c5bf4e243fa08781ad9
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 17package com.android.mail.providers; 18 19import android.content.AsyncQueryHandler; 20import android.content.ContentValues; 21import android.database.Cursor; 22import android.net.Uri; 23import android.os.Parcel; 24import android.os.Parcelable; 25import android.provider.BaseColumns; 26import android.text.Html; 27import android.text.SpannedString; 28import android.text.TextUtils; 29import android.text.util.Rfc822Token; 30import android.text.util.Rfc822Tokenizer; 31 32import com.android.mail.providers.UIProvider.MessageColumns; 33import com.android.mail.utils.Utils; 34import com.google.common.base.Objects; 35 36import java.util.Collections; 37import java.util.List; 38import java.util.regex.Pattern; 39 40 41public class Message implements Parcelable { 42 /** 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 /** 51 * @see BaseColumns#_ID 52 */ 53 public long id; 54 /** 55 * @see UIProvider.MessageColumns#SERVER_ID 56 */ 57 public String serverId; 58 /** 59 * @see UIProvider.MessageColumns#URI 60 */ 61 public Uri uri; 62 /** 63 * @see UIProvider.MessageColumns#CONVERSATION_ID 64 */ 65 public Uri conversationUri; 66 /** 67 * @see UIProvider.MessageColumns#SUBJECT 68 */ 69 public String subject; 70 /** 71 * @see UIProvider.MessageColumns#SNIPPET 72 */ 73 public String snippet; 74 /** 75 * @see UIProvider.MessageColumns#FROM 76 */ 77 public String from; 78 /** 79 * @see UIProvider.MessageColumns#TO 80 */ 81 public String to; 82 /** 83 * @see UIProvider.MessageColumns#CC 84 */ 85 public String cc; 86 /** 87 * @see UIProvider.MessageColumns#BCC 88 */ 89 public String bcc; 90 /** 91 * @see UIProvider.MessageColumns#REPLY_TO 92 */ 93 public String replyTo; 94 /** 95 * @see UIProvider.MessageColumns#DATE_RECEIVED_MS 96 */ 97 public long dateReceivedMs; 98 /** 99 * @see UIProvider.MessageColumns#BODY_HTML 100 */ 101 public String bodyHtml; 102 /** 103 * @see UIProvider.MessageColumns#BODY_TEXT 104 */ 105 public String bodyText; 106 /** 107 * @see UIProvider.MessageColumns#EMBEDS_EXTERNAL_RESOURCES 108 */ 109 public boolean embedsExternalResources; 110 /** 111 * @see UIProvider.MessageColumns#REF_MESSAGE_ID 112 */ 113 public String refMessageId; 114 /** 115 * @see UIProvider.MessageColumns#DRAFT_TYPE 116 */ 117 public int draftType; 118 /** 119 * @see UIProvider.MessageColumns#APPEND_REF_MESSAGE_CONTENT 120 */ 121 public boolean appendRefMessageContent; 122 /** 123 * @see UIProvider.MessageColumns#HAS_ATTACHMENTS 124 */ 125 public boolean hasAttachments; 126 /** 127 * @see UIProvider.MessageColumns#ATTACHMENT_LIST_URI 128 */ 129 public Uri attachmentListUri; 130 /** 131 * @see UIProvider.MessageColumns#MESSAGE_FLAGS 132 */ 133 public long messageFlags; 134 /** 135 * @see UIProvider.MessageColumns#SAVE_MESSAGE_URI 136 */ 137 public String saveUri; 138 /** 139 * @see UIProvider.MessageColumns#SEND_MESSAGE_URI 140 */ 141 public String sendUri; 142 /** 143 * @see UIProvider.MessageColumns#ALWAYS_SHOW_IMAGES 144 */ 145 public boolean alwaysShowImages; 146 /** 147 * @see UIProvider.MessageColumns#READ 148 */ 149 public boolean read; 150 /** 151 * @see UIProvider.MessageColumns#STARRED 152 */ 153 public boolean starred; 154 /** 155 * @see UIProvider.MessageColumns#QUOTE_START_POS 156 */ 157 public int quotedTextOffset; 158 /** 159 * @see UIProvider.MessageColumns#ATTACHMENTS 160 */ 161 public String attachmentsJson; 162 /** 163 * @see UIProvider.MessageColumns#MESSAGE_ACCOUNT_URI 164 */ 165 public Uri accountUri; 166 /** 167 * @see UIProvider.MessageColumns#EVENT_INTENT_URI 168 */ 169 public Uri eventIntentUri; 170 /** 171 * @see UIProvider.MessageColumns#SPAM_WARNING_STRING 172 */ 173 public String spamWarningString; 174 /** 175 * @see UIProvider.MessageColumns#SPAM_WARNING_LEVEL 176 */ 177 public int spamWarningLevel; 178 /** 179 * @see UIProvider.MessageColumns#SPAM_WARNING_LINK_TYPE 180 */ 181 public int spamLinkType; 182 /** 183 * @see UIProvider.MessageColumns#VIA_DOMAIN 184 */ 185 public String viaDomain; 186 /** 187 * @see UIProvider.MessageColumns#IS_SENDING 188 */ 189 public boolean isSending; 190 191 private transient String[] mFromAddresses = null; 192 private transient String[] mToAddresses = null; 193 private transient String[] mCcAddresses = null; 194 private transient String[] mBccAddresses = null; 195 private transient String[] mReplyToAddresses = null; 196 197 private transient List<Attachment> mAttachments = null; 198 199 @Override 200 public int describeContents() { 201 return 0; 202 } 203 204 @Override 205 public boolean equals(Object o) { 206 return this == o || (o != null && o instanceof Message 207 && Objects.equal(uri, ((Message) o).uri)); 208 } 209 210 @Override 211 public int hashCode() { 212 return uri == null ? 0 : uri.hashCode(); 213 } 214 215 @Override 216 public void writeToParcel(Parcel dest, int flags) { 217 dest.writeLong(id); 218 dest.writeString(serverId); 219 dest.writeParcelable(uri, 0); 220 dest.writeParcelable(conversationUri, 0); 221 dest.writeString(subject); 222 dest.writeString(snippet); 223 dest.writeString(from); 224 dest.writeString(to); 225 dest.writeString(cc); 226 dest.writeString(bcc); 227 dest.writeString(replyTo); 228 dest.writeLong(dateReceivedMs); 229 dest.writeString(bodyHtml); 230 dest.writeString(bodyText); 231 dest.writeInt(embedsExternalResources ? 1 : 0); 232 dest.writeString(refMessageId); 233 dest.writeInt(draftType); 234 dest.writeInt(appendRefMessageContent ? 1 : 0); 235 dest.writeInt(hasAttachments ? 1 : 0); 236 dest.writeParcelable(attachmentListUri, 0); 237 dest.writeLong(messageFlags); 238 dest.writeString(saveUri); 239 dest.writeString(sendUri); 240 dest.writeInt(alwaysShowImages ? 1 : 0); 241 dest.writeInt(quotedTextOffset); 242 dest.writeString(attachmentsJson); 243 dest.writeParcelable(accountUri, 0); 244 dest.writeParcelable(eventIntentUri, 0); 245 dest.writeString(spamWarningString); 246 dest.writeInt(spamWarningLevel); 247 dest.writeInt(spamLinkType); 248 dest.writeString(viaDomain); 249 dest.writeInt(isSending ? 1 : 0); 250 } 251 252 private Message(Parcel in) { 253 id = in.readLong(); 254 serverId = in.readString(); 255 uri = in.readParcelable(null); 256 conversationUri = in.readParcelable(null); 257 subject = in.readString(); 258 snippet = in.readString(); 259 from = in.readString(); 260 to = in.readString(); 261 cc = in.readString(); 262 bcc = in.readString(); 263 replyTo = in.readString(); 264 dateReceivedMs = in.readLong(); 265 bodyHtml = in.readString(); 266 bodyText = in.readString(); 267 embedsExternalResources = in.readInt() != 0; 268 refMessageId = in.readString(); 269 draftType = in.readInt(); 270 appendRefMessageContent = in.readInt() != 0; 271 hasAttachments = in.readInt() != 0; 272 attachmentListUri = in.readParcelable(null); 273 messageFlags = in.readLong(); 274 saveUri = in.readString(); 275 sendUri = in.readString(); 276 alwaysShowImages = in.readInt() != 0; 277 quotedTextOffset = in.readInt(); 278 attachmentsJson = in.readString(); 279 accountUri = in.readParcelable(null); 280 eventIntentUri = in.readParcelable(null); 281 spamWarningString = in.readString(); 282 spamWarningLevel = in.readInt(); 283 spamLinkType = in.readInt(); 284 viaDomain = in.readString(); 285 isSending = in.readInt() != 0; 286 } 287 288 public Message() { 289 290 } 291 292 @Override 293 public String toString() { 294 return "[message id=" + id + "]"; 295 } 296 297 public static final Creator<Message> CREATOR = new Creator<Message>() { 298 299 @Override 300 public Message createFromParcel(Parcel source) { 301 return new Message(source); 302 } 303 304 @Override 305 public Message[] newArray(int size) { 306 return new Message[size]; 307 } 308 309 }; 310 311 public Message(Cursor cursor) { 312 if (cursor != null) { 313 id = cursor.getLong(UIProvider.MESSAGE_ID_COLUMN); 314 serverId = cursor.getString(UIProvider.MESSAGE_SERVER_ID_COLUMN); 315 final String messageUriStr = cursor.getString(UIProvider.MESSAGE_URI_COLUMN); 316 uri = !TextUtils.isEmpty(messageUriStr) ? Uri.parse(messageUriStr) : null; 317 final String convUriStr = cursor.getString(UIProvider.MESSAGE_CONVERSATION_URI_COLUMN); 318 conversationUri = !TextUtils.isEmpty(convUriStr) ? Uri.parse(convUriStr) : null; 319 subject = cursor.getString(UIProvider.MESSAGE_SUBJECT_COLUMN); 320 snippet = cursor.getString(UIProvider.MESSAGE_SNIPPET_COLUMN); 321 from = cursor.getString(UIProvider.MESSAGE_FROM_COLUMN); 322 to = cursor.getString(UIProvider.MESSAGE_TO_COLUMN); 323 cc = cursor.getString(UIProvider.MESSAGE_CC_COLUMN); 324 bcc = cursor.getString(UIProvider.MESSAGE_BCC_COLUMN); 325 replyTo = cursor.getString(UIProvider.MESSAGE_REPLY_TO_COLUMN); 326 dateReceivedMs = cursor.getLong(UIProvider.MESSAGE_DATE_RECEIVED_MS_COLUMN); 327 bodyHtml = cursor.getString(UIProvider.MESSAGE_BODY_HTML_COLUMN); 328 bodyText = cursor.getString(UIProvider.MESSAGE_BODY_TEXT_COLUMN); 329 embedsExternalResources = cursor 330 .getInt(UIProvider.MESSAGE_EMBEDS_EXTERNAL_RESOURCES_COLUMN) != 0; 331 refMessageId = cursor.getString(UIProvider.MESSAGE_REF_MESSAGE_ID_COLUMN); 332 draftType = cursor.getInt(UIProvider.MESSAGE_DRAFT_TYPE_COLUMN); 333 appendRefMessageContent = cursor 334 .getInt(UIProvider.MESSAGE_APPEND_REF_MESSAGE_CONTENT_COLUMN) != 0; 335 hasAttachments = cursor.getInt(UIProvider.MESSAGE_HAS_ATTACHMENTS_COLUMN) != 0; 336 final String attachmentsUri = cursor 337 .getString(UIProvider.MESSAGE_ATTACHMENT_LIST_URI_COLUMN); 338 attachmentListUri = hasAttachments && !TextUtils.isEmpty(attachmentsUri) ? Uri 339 .parse(attachmentsUri) : null; 340 messageFlags = cursor.getLong(UIProvider.MESSAGE_FLAGS_COLUMN); 341 saveUri = cursor 342 .getString(UIProvider.MESSAGE_SAVE_URI_COLUMN); 343 sendUri = cursor 344 .getString(UIProvider.MESSAGE_SEND_URI_COLUMN); 345 alwaysShowImages = cursor.getInt(UIProvider.MESSAGE_ALWAYS_SHOW_IMAGES_COLUMN) != 0; 346 read = cursor.getInt(UIProvider.MESSAGE_READ_COLUMN) != 0; 347 starred = cursor.getInt(UIProvider.MESSAGE_STARRED_COLUMN) != 0; 348 quotedTextOffset = cursor.getInt(UIProvider.QUOTED_TEXT_OFFSET_COLUMN); 349 attachmentsJson = cursor.getString(UIProvider.MESSAGE_ATTACHMENTS_COLUMN); 350 String accountUriString = cursor.getString(UIProvider.MESSAGE_ACCOUNT_URI_COLUMN); 351 accountUri = !TextUtils.isEmpty(accountUriString) ? Uri.parse(accountUriString) : null; 352 eventIntentUri = 353 Utils.getValidUri(cursor.getString(UIProvider.MESSAGE_EVENT_INTENT_COLUMN)); 354 spamWarningString = 355 cursor.getString(UIProvider.MESSAGE_SPAM_WARNING_STRING_ID_COLUMN); 356 spamWarningLevel = cursor.getInt(UIProvider.MESSAGE_SPAM_WARNING_LEVEL_COLUMN); 357 spamLinkType = cursor.getInt(UIProvider.MESSAGE_SPAM_WARNING_LINK_TYPE_COLUMN); 358 viaDomain = cursor.getString(UIProvider.MESSAGE_VIA_DOMAIN_COLUMN); 359 isSending = cursor.getInt(UIProvider.MESSAGE_IS_SENDING_COLUMN) != 0; 360 } 361 } 362 363 public boolean isFlaggedReplied() { 364 return (messageFlags & UIProvider.MessageFlags.REPLIED) == 365 UIProvider.MessageFlags.REPLIED; 366 } 367 368 public boolean isFlaggedForwarded() { 369 return (messageFlags & UIProvider.MessageFlags.FORWARDED) == 370 UIProvider.MessageFlags.FORWARDED; 371 } 372 373 public boolean isFlaggedCalendarInvite() { 374 return (messageFlags & UIProvider.MessageFlags.CALENDAR_INVITE) == 375 UIProvider.MessageFlags.CALENDAR_INVITE; 376 } 377 378 public synchronized String[] getFromAddresses() { 379 if (mFromAddresses == null) { 380 mFromAddresses = tokenizeAddresses(from); 381 } 382 return mFromAddresses; 383 } 384 385 public synchronized String[] getToAddresses() { 386 if (mToAddresses == null) { 387 mToAddresses = tokenizeAddresses(to); 388 } 389 return mToAddresses; 390 } 391 392 public synchronized String[] getCcAddresses() { 393 if (mCcAddresses == null) { 394 mCcAddresses = tokenizeAddresses(cc); 395 } 396 return mCcAddresses; 397 } 398 399 public synchronized String[] getBccAddresses() { 400 if (mBccAddresses == null) { 401 mBccAddresses = tokenizeAddresses(bcc); 402 } 403 return mBccAddresses; 404 } 405 406 public synchronized String[] getReplyToAddresses() { 407 if (mReplyToAddresses == null) { 408 mReplyToAddresses = tokenizeAddresses(replyTo); 409 } 410 return mReplyToAddresses; 411 } 412 413 private String[] tokenizeAddresses(String addresses) { 414 if (TextUtils.isEmpty(addresses)) { 415 return new String[0]; 416 } 417 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addresses); 418 String[] strings = new String[tokens.length]; 419 for (int i = 0; i < tokens.length;i++) { 420 strings[i] = tokens[i].toString(); 421 } 422 return strings; 423 } 424 public synchronized List<Attachment> getAttachments() { 425 if (mAttachments == null) { 426 if (attachmentsJson != null) { 427 mAttachments = Attachment.fromJSONArray(attachmentsJson); 428 } else { 429 mAttachments = Collections.emptyList(); 430 } 431 } 432 return mAttachments; 433 } 434 435 /** 436 * Returns whether a "Show Pictures" button should initially appear for this message. If the 437 * button is shown, the message must also block all non-local images in the body. Inversely, if 438 * the button is not shown, the message must show all images within (or else the user would be 439 * stuck with no images and no way to reveal them). 440 * 441 * @return true if a "Show Pictures" button should appear. 442 */ 443 public boolean shouldShowImagePrompt() { 444 return !alwaysShowImages && embedsExternalResources(); 445 } 446 447 private boolean embedsExternalResources() { 448 return embedsExternalResources || 449 (!TextUtils.isEmpty(bodyHtml) && INLINE_IMAGE_PATTERN.matcher(bodyHtml).find()); 450 } 451 452 /** 453 * Helper method to command a provider to mark all messages from this sender with the 454 * {@link MessageColumns#ALWAYS_SHOW_IMAGES} flag set. 455 * 456 * @param handler a caller-provided handler to run the query on 457 * @param token (optional) token to identify the command to the handler 458 * @param cookie (optional) cookie to pass to the handler 459 */ 460 public void markAlwaysShowImages(AsyncQueryHandler handler, int token, Object cookie) { 461 alwaysShowImages = true; 462 463 final ContentValues values = new ContentValues(1); 464 values.put(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES, 1); 465 466 handler.startUpdate(token, cookie, uri, values, null, null); 467 } 468 469 public String getBodyAsHtml() { 470 String body = ""; 471 if (!TextUtils.isEmpty(bodyHtml)) { 472 body = bodyHtml; 473 } else if (!TextUtils.isEmpty(bodyText)) { 474 body = Html.toHtml(new SpannedString(bodyText)); 475 } 476 return body; 477 } 478 479} 480