Message.java revision 013a23c75cf8a71c51ad1580aa84f2a8e06b6761
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 private String mFrom; 78 /** 79 * @see UIProvider.MessageColumns#TO 80 */ 81 private String mTo; 82 /** 83 * @see UIProvider.MessageColumns#CC 84 */ 85 private String mCc; 86 /** 87 * @see UIProvider.MessageColumns#BCC 88 */ 89 private String mBcc; 90 /** 91 * @see UIProvider.MessageColumns#REPLY_TO 92 */ 93 private String mReplyTo; 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#ALWAYS_SHOW_IMAGES 136 */ 137 public boolean alwaysShowImages; 138 /** 139 * @see UIProvider.MessageColumns#READ 140 */ 141 public boolean read; 142 /** 143 * @see UIProvider.MessageColumns#SEEN 144 */ 145 public boolean seen; 146 /** 147 * @see UIProvider.MessageColumns#STARRED 148 */ 149 public boolean starred; 150 /** 151 * @see UIProvider.MessageColumns#QUOTE_START_POS 152 */ 153 public int quotedTextOffset; 154 /** 155 * @see UIProvider.MessageColumns#ATTACHMENTS 156 *<p> 157 * N.B. this value is NOT immutable and may change during conversation view render. 158 */ 159 public String attachmentsJson; 160 /** 161 * @see UIProvider.MessageColumns#MESSAGE_ACCOUNT_URI 162 */ 163 public Uri accountUri; 164 /** 165 * @see UIProvider.MessageColumns#EVENT_INTENT_URI 166 */ 167 public Uri eventIntentUri; 168 /** 169 * @see UIProvider.MessageColumns#SPAM_WARNING_STRING 170 */ 171 public String spamWarningString; 172 /** 173 * @see UIProvider.MessageColumns#SPAM_WARNING_LEVEL 174 */ 175 public int spamWarningLevel; 176 /** 177 * @see UIProvider.MessageColumns#SPAM_WARNING_LINK_TYPE 178 */ 179 public int spamLinkType; 180 /** 181 * @see UIProvider.MessageColumns#VIA_DOMAIN 182 */ 183 public String viaDomain; 184 /** 185 * @see UIProvider.MessageColumns#IS_SENDING 186 */ 187 public boolean isSending; 188 189 private transient String[] mFromAddresses = null; 190 private transient String[] mToAddresses = null; 191 private transient String[] mCcAddresses = null; 192 private transient String[] mBccAddresses = null; 193 private transient String[] mReplyToAddresses = null; 194 195 private transient List<Attachment> mAttachments = null; 196 197 @Override 198 public int describeContents() { 199 return 0; 200 } 201 202 @Override 203 public boolean equals(Object o) { 204 return this == o || (o != null && o instanceof Message 205 && Objects.equal(uri, ((Message) o).uri)); 206 } 207 208 @Override 209 public int hashCode() { 210 return uri == null ? 0 : uri.hashCode(); 211 } 212 213 @Override 214 public void writeToParcel(Parcel dest, int flags) { 215 dest.writeLong(id); 216 dest.writeString(serverId); 217 dest.writeParcelable(uri, 0); 218 dest.writeParcelable(conversationUri, 0); 219 dest.writeString(subject); 220 dest.writeString(snippet); 221 dest.writeString(mFrom); 222 dest.writeString(mTo); 223 dest.writeString(mCc); 224 dest.writeString(mBcc); 225 dest.writeString(mReplyTo); 226 dest.writeLong(dateReceivedMs); 227 dest.writeString(bodyHtml); 228 dest.writeString(bodyText); 229 dest.writeInt(embedsExternalResources ? 1 : 0); 230 dest.writeString(refMessageId); 231 dest.writeInt(draftType); 232 dest.writeInt(appendRefMessageContent ? 1 : 0); 233 dest.writeInt(hasAttachments ? 1 : 0); 234 dest.writeParcelable(attachmentListUri, 0); 235 dest.writeLong(messageFlags); 236 dest.writeInt(alwaysShowImages ? 1 : 0); 237 dest.writeInt(quotedTextOffset); 238 dest.writeString(attachmentsJson); 239 dest.writeParcelable(accountUri, 0); 240 dest.writeParcelable(eventIntentUri, 0); 241 dest.writeString(spamWarningString); 242 dest.writeInt(spamWarningLevel); 243 dest.writeInt(spamLinkType); 244 dest.writeString(viaDomain); 245 dest.writeInt(isSending ? 1 : 0); 246 } 247 248 private Message(Parcel in) { 249 id = in.readLong(); 250 serverId = in.readString(); 251 uri = in.readParcelable(null); 252 conversationUri = in.readParcelable(null); 253 subject = in.readString(); 254 snippet = in.readString(); 255 mFrom = in.readString(); 256 mTo = in.readString(); 257 mCc = in.readString(); 258 mBcc = in.readString(); 259 mReplyTo = in.readString(); 260 dateReceivedMs = in.readLong(); 261 bodyHtml = in.readString(); 262 bodyText = in.readString(); 263 embedsExternalResources = in.readInt() != 0; 264 refMessageId = in.readString(); 265 draftType = in.readInt(); 266 appendRefMessageContent = in.readInt() != 0; 267 hasAttachments = in.readInt() != 0; 268 attachmentListUri = in.readParcelable(null); 269 messageFlags = in.readLong(); 270 alwaysShowImages = in.readInt() != 0; 271 quotedTextOffset = in.readInt(); 272 attachmentsJson = in.readString(); 273 accountUri = in.readParcelable(null); 274 eventIntentUri = in.readParcelable(null); 275 spamWarningString = in.readString(); 276 spamWarningLevel = in.readInt(); 277 spamLinkType = in.readInt(); 278 viaDomain = in.readString(); 279 isSending = in.readInt() != 0; 280 } 281 282 public Message() { 283 284 } 285 286 @Override 287 public String toString() { 288 return "[message id=" + id + "]"; 289 } 290 291 public static final Creator<Message> CREATOR = new Creator<Message>() { 292 293 @Override 294 public Message createFromParcel(Parcel source) { 295 return new Message(source); 296 } 297 298 @Override 299 public Message[] newArray(int size) { 300 return new Message[size]; 301 } 302 303 }; 304 305 public Message(Cursor cursor) { 306 if (cursor != null) { 307 id = cursor.getLong(UIProvider.MESSAGE_ID_COLUMN); 308 serverId = cursor.getString(UIProvider.MESSAGE_SERVER_ID_COLUMN); 309 final String messageUriStr = cursor.getString(UIProvider.MESSAGE_URI_COLUMN); 310 uri = !TextUtils.isEmpty(messageUriStr) ? Uri.parse(messageUriStr) : null; 311 final String convUriStr = cursor.getString(UIProvider.MESSAGE_CONVERSATION_URI_COLUMN); 312 conversationUri = !TextUtils.isEmpty(convUriStr) ? Uri.parse(convUriStr) : null; 313 subject = cursor.getString(UIProvider.MESSAGE_SUBJECT_COLUMN); 314 snippet = cursor.getString(UIProvider.MESSAGE_SNIPPET_COLUMN); 315 mFrom = cursor.getString(UIProvider.MESSAGE_FROM_COLUMN); 316 mTo = cursor.getString(UIProvider.MESSAGE_TO_COLUMN); 317 mCc = cursor.getString(UIProvider.MESSAGE_CC_COLUMN); 318 mBcc = cursor.getString(UIProvider.MESSAGE_BCC_COLUMN); 319 mReplyTo = cursor.getString(UIProvider.MESSAGE_REPLY_TO_COLUMN); 320 dateReceivedMs = cursor.getLong(UIProvider.MESSAGE_DATE_RECEIVED_MS_COLUMN); 321 bodyHtml = cursor.getString(UIProvider.MESSAGE_BODY_HTML_COLUMN); 322 bodyText = cursor.getString(UIProvider.MESSAGE_BODY_TEXT_COLUMN); 323 embedsExternalResources = cursor 324 .getInt(UIProvider.MESSAGE_EMBEDS_EXTERNAL_RESOURCES_COLUMN) != 0; 325 refMessageId = cursor.getString(UIProvider.MESSAGE_REF_MESSAGE_ID_COLUMN); 326 draftType = cursor.getInt(UIProvider.MESSAGE_DRAFT_TYPE_COLUMN); 327 appendRefMessageContent = cursor 328 .getInt(UIProvider.MESSAGE_APPEND_REF_MESSAGE_CONTENT_COLUMN) != 0; 329 hasAttachments = cursor.getInt(UIProvider.MESSAGE_HAS_ATTACHMENTS_COLUMN) != 0; 330 final String attachmentsUri = cursor 331 .getString(UIProvider.MESSAGE_ATTACHMENT_LIST_URI_COLUMN); 332 attachmentListUri = hasAttachments && !TextUtils.isEmpty(attachmentsUri) ? Uri 333 .parse(attachmentsUri) : null; 334 messageFlags = cursor.getLong(UIProvider.MESSAGE_FLAGS_COLUMN); 335 alwaysShowImages = cursor.getInt(UIProvider.MESSAGE_ALWAYS_SHOW_IMAGES_COLUMN) != 0; 336 read = cursor.getInt(UIProvider.MESSAGE_READ_COLUMN) != 0; 337 seen = cursor.getInt(UIProvider.MESSAGE_SEEN_COLUMN) != 0; 338 starred = cursor.getInt(UIProvider.MESSAGE_STARRED_COLUMN) != 0; 339 quotedTextOffset = cursor.getInt(UIProvider.QUOTED_TEXT_OFFSET_COLUMN); 340 attachmentsJson = cursor.getString(UIProvider.MESSAGE_ATTACHMENTS_COLUMN); 341 String accountUriString = cursor.getString(UIProvider.MESSAGE_ACCOUNT_URI_COLUMN); 342 accountUri = !TextUtils.isEmpty(accountUriString) ? Uri.parse(accountUriString) : null; 343 eventIntentUri = 344 Utils.getValidUri(cursor.getString(UIProvider.MESSAGE_EVENT_INTENT_COLUMN)); 345 spamWarningString = 346 cursor.getString(UIProvider.MESSAGE_SPAM_WARNING_STRING_ID_COLUMN); 347 spamWarningLevel = cursor.getInt(UIProvider.MESSAGE_SPAM_WARNING_LEVEL_COLUMN); 348 spamLinkType = cursor.getInt(UIProvider.MESSAGE_SPAM_WARNING_LINK_TYPE_COLUMN); 349 viaDomain = cursor.getString(UIProvider.MESSAGE_VIA_DOMAIN_COLUMN); 350 isSending = cursor.getInt(UIProvider.MESSAGE_IS_SENDING_COLUMN) != 0; 351 } 352 } 353 354 public boolean isFlaggedReplied() { 355 return (messageFlags & UIProvider.MessageFlags.REPLIED) == 356 UIProvider.MessageFlags.REPLIED; 357 } 358 359 public boolean isFlaggedForwarded() { 360 return (messageFlags & UIProvider.MessageFlags.FORWARDED) == 361 UIProvider.MessageFlags.FORWARDED; 362 } 363 364 public boolean isFlaggedCalendarInvite() { 365 return (messageFlags & UIProvider.MessageFlags.CALENDAR_INVITE) == 366 UIProvider.MessageFlags.CALENDAR_INVITE; 367 } 368 369 public String getFrom() { 370 return mFrom; 371 } 372 373 public synchronized void setFrom(final String from) { 374 mFrom = from; 375 mFromAddresses = null; 376 } 377 378 public String getTo() { 379 return mTo; 380 } 381 382 public synchronized void setTo(final String to) { 383 mTo = to; 384 mToAddresses = null; 385 } 386 387 public String getCc() { 388 return mCc; 389 } 390 391 public synchronized void setCc(final String cc) { 392 mCc = cc; 393 mCcAddresses = null; 394 } 395 396 public String getBcc() { 397 return mBcc; 398 } 399 400 public synchronized void setBcc(final String bcc) { 401 mBcc = bcc; 402 mBccAddresses = null; 403 } 404 405 public String getReplyTo() { 406 return mReplyTo; 407 } 408 409 public synchronized void setReplyTo(final String replyTo) { 410 mReplyTo = replyTo; 411 mReplyToAddresses = null; 412 } 413 414 public synchronized String[] getFromAddresses() { 415 if (mFromAddresses == null) { 416 mFromAddresses = tokenizeAddresses(mFrom); 417 } 418 return mFromAddresses; 419 } 420 421 public synchronized String[] getToAddresses() { 422 if (mToAddresses == null) { 423 mToAddresses = tokenizeAddresses(mTo); 424 } 425 return mToAddresses; 426 } 427 428 public synchronized String[] getCcAddresses() { 429 if (mCcAddresses == null) { 430 mCcAddresses = tokenizeAddresses(mCc); 431 } 432 return mCcAddresses; 433 } 434 435 public synchronized String[] getBccAddresses() { 436 if (mBccAddresses == null) { 437 mBccAddresses = tokenizeAddresses(mBcc); 438 } 439 return mBccAddresses; 440 } 441 442 public synchronized String[] getReplyToAddresses() { 443 if (mReplyToAddresses == null) { 444 mReplyToAddresses = tokenizeAddresses(mReplyTo); 445 } 446 return mReplyToAddresses; 447 } 448 449 public static String[] tokenizeAddresses(String addresses) { 450 if (TextUtils.isEmpty(addresses)) { 451 return new String[0]; 452 } 453 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addresses); 454 String[] strings = new String[tokens.length]; 455 for (int i = 0; i < tokens.length;i++) { 456 strings[i] = tokens[i].toString(); 457 } 458 return strings; 459 } 460 461 public List<Attachment> getAttachments() { 462 if (mAttachments == null) { 463 if (attachmentsJson != null) { 464 mAttachments = Attachment.fromJSONArray(attachmentsJson); 465 } else { 466 mAttachments = Collections.emptyList(); 467 } 468 } 469 return mAttachments; 470 } 471 472 /** 473 * Returns whether a "Show Pictures" button should initially appear for this message. If the 474 * button is shown, the message must also block all non-local images in the body. Inversely, if 475 * the button is not shown, the message must show all images within (or else the user would be 476 * stuck with no images and no way to reveal them). 477 * 478 * @return true if a "Show Pictures" button should appear. 479 */ 480 public boolean shouldShowImagePrompt() { 481 return !alwaysShowImages && embedsExternalResources(); 482 } 483 484 private boolean embedsExternalResources() { 485 return embedsExternalResources || 486 (!TextUtils.isEmpty(bodyHtml) && INLINE_IMAGE_PATTERN.matcher(bodyHtml).find()); 487 } 488 489 /** 490 * Helper method to command a provider to mark all messages from this sender with the 491 * {@link MessageColumns#ALWAYS_SHOW_IMAGES} flag set. 492 * 493 * @param handler a caller-provided handler to run the query on 494 * @param token (optional) token to identify the command to the handler 495 * @param cookie (optional) cookie to pass to the handler 496 */ 497 public void markAlwaysShowImages(AsyncQueryHandler handler, int token, Object cookie) { 498 alwaysShowImages = true; 499 500 final ContentValues values = new ContentValues(1); 501 values.put(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES, 1); 502 503 handler.startUpdate(token, cookie, uri, values, null, null); 504 } 505 506 public String getBodyAsHtml() { 507 String body = ""; 508 if (!TextUtils.isEmpty(bodyHtml)) { 509 body = bodyHtml; 510 } else if (!TextUtils.isEmpty(bodyText)) { 511 body = Html.toHtml(new SpannedString(bodyText)); 512 } 513 return body; 514 } 515 516} 517