Message.java revision a71f1f3368f26b6627512298f64484611fd6b0e8
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.text.TextUtils; 26 27import com.android.mail.browse.MessageCursor; 28import com.android.mail.providers.UIProvider.MessageColumns; 29import com.android.mail.ui.AbstractActivityController; 30import com.android.mail.ui.ConversationListCallbacks; 31import com.android.mail.utils.Utils; 32 33import java.util.Collections; 34import java.util.List; 35 36 37public class Message implements Parcelable { 38 public long id; 39 public long serverId; 40 public Uri uri; 41 public String conversationUri; 42 public String subject; 43 public String snippet; 44 public String from; 45 public String to; 46 public String cc; 47 public String bcc; 48 public String replyTo; 49 public long dateReceivedMs; 50 public String bodyHtml; 51 public String bodyText; 52 public boolean embedsExternalResources; 53 public String refMessageId; 54 public int draftType; 55 public boolean appendRefMessageContent; 56 public boolean hasAttachments; 57 public Uri attachmentListUri; 58 public long messageFlags; 59 public String saveUri; 60 public String sendUri; 61 public boolean alwaysShowImages; 62 public boolean read; 63 public boolean starred; 64 public int quotedTextOffset; 65 public String attachmentsJson; 66 public Uri accountUri; 67 68 private transient String[] mToAddresses = null; 69 private transient String[] mCcAddresses = null; 70 private transient String[] mBccAddresses = null; 71 private transient String[] mReplyToAddresses = null; 72 73 private transient List<Attachment> mAttachments = null; 74 75 // While viewing a list of messages, points to the cursor that contains it 76 private transient MessageCursor mOwningCursor = null; 77 private transient ConversationListCallbacks mListController = null; 78 79 @Override 80 public int describeContents() { 81 return 0; 82 } 83 84 @Override 85 public boolean equals(Object o) { 86 if (o == null || !(o instanceof Message)) { 87 return false; 88 } 89 final Uri otherUri = ((Message) o).uri; 90 if (uri == null) { 91 return (otherUri == null); 92 } 93 return uri.equals(otherUri); 94 } 95 96 @Override 97 public int hashCode() { 98 return uri == null ? 0 : uri.hashCode(); 99 } 100 101 @Override 102 public void writeToParcel(Parcel dest, int flags) { 103 dest.writeLong(id); 104 dest.writeLong(serverId); 105 dest.writeParcelable(uri, 0); 106 dest.writeString(conversationUri); 107 dest.writeString(subject); 108 dest.writeString(snippet); 109 dest.writeString(from); 110 dest.writeString(to); 111 dest.writeString(cc); 112 dest.writeString(bcc); 113 dest.writeString(replyTo); 114 dest.writeLong(dateReceivedMs); 115 dest.writeString(bodyHtml); 116 dest.writeString(bodyText); 117 dest.writeInt(embedsExternalResources ? 1 : 0); 118 dest.writeString(refMessageId); 119 dest.writeInt(draftType); 120 dest.writeInt(appendRefMessageContent ? 1 : 0); 121 dest.writeInt(hasAttachments ? 1 : 0); 122 dest.writeParcelable(attachmentListUri, 0); 123 dest.writeLong(messageFlags); 124 dest.writeString(saveUri); 125 dest.writeString(sendUri); 126 dest.writeInt(alwaysShowImages ? 1 : 0); 127 dest.writeInt(quotedTextOffset); 128 dest.writeString(attachmentsJson); 129 dest.writeParcelable(accountUri, 0); 130 } 131 132 private Message(Parcel in) { 133 id = in.readLong(); 134 serverId = in.readLong(); 135 uri = in.readParcelable(null); 136 conversationUri = in.readString(); 137 subject = in.readString(); 138 snippet = in.readString(); 139 from = in.readString(); 140 to = in.readString(); 141 cc = in.readString(); 142 bcc = in.readString(); 143 replyTo = in.readString(); 144 dateReceivedMs = in.readLong(); 145 bodyHtml = in.readString(); 146 bodyText = in.readString(); 147 embedsExternalResources = in.readInt() != 0; 148 refMessageId = in.readString(); 149 draftType = in.readInt(); 150 appendRefMessageContent = in.readInt() != 0; 151 hasAttachments = in.readInt() != 0; 152 attachmentListUri = in.readParcelable(null); 153 messageFlags = in.readLong(); 154 saveUri = in.readString(); 155 sendUri = in.readString(); 156 alwaysShowImages = in.readInt() != 0; 157 quotedTextOffset = in.readInt(); 158 attachmentsJson = in.readString(); 159 accountUri = in.readParcelable(null); 160 } 161 162 public Message() { 163 164 } 165 166 @Override 167 public String toString() { 168 return "[message id=" + id + "]"; 169 } 170 171 public static final Creator<Message> CREATOR = new Creator<Message>() { 172 173 @Override 174 public Message createFromParcel(Parcel source) { 175 return new Message(source); 176 } 177 178 @Override 179 public Message[] newArray(int size) { 180 return new Message[size]; 181 } 182 183 }; 184 185 public Message(MessageCursor cursor, ConversationListCallbacks listController) { 186 this(cursor); 187 // Only set message cursor if appropriate 188 mOwningCursor = cursor; 189 mListController = listController; 190 } 191 192 public Message(Cursor cursor) { 193 if (cursor != null) { 194 id = cursor.getLong(UIProvider.MESSAGE_ID_COLUMN); 195 serverId = cursor.getLong(UIProvider.MESSAGE_SERVER_ID_COLUMN); 196 String message = cursor.getString(UIProvider.MESSAGE_URI_COLUMN); 197 uri = !TextUtils.isEmpty(message) ? Uri.parse(message) : null; 198 conversationUri = cursor.getString(UIProvider.MESSAGE_CONVERSATION_URI_COLUMN); 199 subject = cursor.getString(UIProvider.MESSAGE_SUBJECT_COLUMN); 200 snippet = cursor.getString(UIProvider.MESSAGE_SNIPPET_COLUMN); 201 from = cursor.getString(UIProvider.MESSAGE_FROM_COLUMN); 202 to = cursor.getString(UIProvider.MESSAGE_TO_COLUMN); 203 cc = cursor.getString(UIProvider.MESSAGE_CC_COLUMN); 204 bcc = cursor.getString(UIProvider.MESSAGE_BCC_COLUMN); 205 replyTo = cursor.getString(UIProvider.MESSAGE_REPLY_TO_COLUMN); 206 dateReceivedMs = cursor.getLong(UIProvider.MESSAGE_DATE_RECEIVED_MS_COLUMN); 207 bodyHtml = cursor.getString(UIProvider.MESSAGE_BODY_HTML_COLUMN); 208 bodyText = cursor.getString(UIProvider.MESSAGE_BODY_TEXT_COLUMN); 209 embedsExternalResources = cursor 210 .getInt(UIProvider.MESSAGE_EMBEDS_EXTERNAL_RESOURCES_COLUMN) != 0; 211 refMessageId = cursor.getString(UIProvider.MESSAGE_REF_MESSAGE_ID_COLUMN); 212 draftType = cursor.getInt(UIProvider.MESSAGE_DRAFT_TYPE_COLUMN); 213 appendRefMessageContent = cursor 214 .getInt(UIProvider.MESSAGE_APPEND_REF_MESSAGE_CONTENT_COLUMN) != 0; 215 hasAttachments = cursor.getInt(UIProvider.MESSAGE_HAS_ATTACHMENTS_COLUMN) != 0; 216 final String attachmentsUri = cursor 217 .getString(UIProvider.MESSAGE_ATTACHMENT_LIST_URI_COLUMN); 218 attachmentListUri = hasAttachments && !TextUtils.isEmpty(attachmentsUri) ? Uri 219 .parse(attachmentsUri) : null; 220 messageFlags = cursor.getLong(UIProvider.MESSAGE_FLAGS_COLUMN); 221 saveUri = cursor 222 .getString(UIProvider.MESSAGE_SAVE_URI_COLUMN); 223 sendUri = cursor 224 .getString(UIProvider.MESSAGE_SEND_URI_COLUMN); 225 alwaysShowImages = cursor.getInt(UIProvider.MESSAGE_ALWAYS_SHOW_IMAGES_COLUMN) != 0; 226 read = cursor.getInt(UIProvider.MESSAGE_READ_COLUMN) != 0; 227 starred = cursor.getInt(UIProvider.MESSAGE_STARRED_COLUMN) != 0; 228 quotedTextOffset = cursor.getInt(UIProvider.QUOTED_TEXT_OFFSET_COLUMN); 229 attachmentsJson = cursor.getString(UIProvider.MESSAGE_ATTACHMENTS_COLUMN); 230 String accountUriString = cursor.getString(UIProvider.MESSAGE_ACCOUNT_URI_COLUMN); 231 accountUri = !TextUtils.isEmpty(accountUriString) ? Uri.parse(accountUriString) : null; 232 } 233 } 234 235 public boolean isFlaggedReplied() { 236 return (messageFlags & UIProvider.MessageFlags.REPLIED) == 237 UIProvider.MessageFlags.REPLIED; 238 } 239 240 public boolean isFlaggedForwarded() { 241 return (messageFlags & UIProvider.MessageFlags.FORWARDED) == 242 UIProvider.MessageFlags.FORWARDED; 243 } 244 245 public boolean isFlaggedCalendarInvite() { 246 return (messageFlags & UIProvider.MessageFlags.CALENDAR_INVITE) == 247 UIProvider.MessageFlags.CALENDAR_INVITE; 248 } 249 250 public synchronized String[] getToAddresses() { 251 if (mToAddresses == null) { 252 mToAddresses = Utils.splitCommaSeparatedString(to); 253 } 254 return mToAddresses; 255 } 256 257 public synchronized String[] getCcAddresses() { 258 if (mCcAddresses == null) { 259 mCcAddresses = Utils.splitCommaSeparatedString(cc); 260 } 261 return mCcAddresses; 262 } 263 264 public synchronized String[] getBccAddresses() { 265 if (mBccAddresses == null) { 266 mBccAddresses = Utils.splitCommaSeparatedString(bcc); 267 } 268 return mBccAddresses; 269 } 270 271 public synchronized String[] getReplyToAddresses() { 272 if (mReplyToAddresses == null) { 273 mReplyToAddresses = Utils.splitCommaSeparatedString(replyTo); 274 } 275 return mReplyToAddresses; 276 } 277 278 public synchronized List<Attachment> getAttachments() { 279 if (mAttachments == null) { 280 if (attachmentsJson != null) { 281 mAttachments = Attachment.fromJSONArray(attachmentsJson); 282 } else { 283 mAttachments = Collections.emptyList(); 284 } 285 } 286 return mAttachments; 287 } 288 289 /** 290 * Returns whether a "Show Pictures" button should initially appear for this message. If the 291 * button is shown, the message must also block all non-local images in the body. Inversely, if 292 * the button is not shown, the message must show all images within (or else the user would be 293 * stuck with no images and no way to reveal them). 294 * 295 * @return true if a "Show Pictures" button should appear. 296 */ 297 public boolean shouldShowImagePrompt() { 298 return embedsExternalResources && !alwaysShowImages; 299 } 300 301 /** 302 * Helper method to command a provider to mark all messages from this sender with the 303 * {@link MessageColumns#ALWAYS_SHOW_IMAGES} flag set. 304 * 305 * @param handler a caller-provided handler to run the query on 306 * @param token (optional) token to identify the command to the handler 307 * @param cookie (optional) cookie to pass to the handler 308 */ 309 public void markAlwaysShowImages(AsyncQueryHandler handler, int token, Object cookie) { 310 final ContentValues values = new ContentValues(1); 311 values.put(UIProvider.MessageColumns.ALWAYS_SHOW_IMAGES, 1); 312 313 handler.startUpdate(token, cookie, uri, values, null, null); 314 } 315 316 /** 317 * Helper method to command a provider to star/unstar this message. 318 * 319 * @param starred whether to star or unstar the message 320 * @param handler a caller-provided handler to run the query on 321 * @param token (optional) token to identify the command to the handler 322 * @param cookie (optional) cookie to pass to the handler 323 */ 324 public void star(boolean starred, AsyncQueryHandler handler, int token, Object cookie) { 325 this.starred = starred; 326 // If we're unstarring, we need to find out if the conversation is still starred 327 if (mListController != null) { 328 boolean conversationStarred = starred; 329 if (!starred) { 330 conversationStarred = mOwningCursor.isConversationStarred(); 331 } 332 // Update the conversation cursor so that changes are reflected simultaneously 333 mListController.sendConversationUriStarred( 334 AbstractActivityController.TAG_CONVERSATION_LIST, conversationUri, 335 conversationStarred, true /*local*/); 336 } 337 final ContentValues values = new ContentValues(1); 338 values.put(UIProvider.MessageColumns.STARRED, starred ? 1 : 0); 339 340 handler.startUpdate(token, cookie, uri, values, null, null); 341 } 342 343} 344