Conversation.java revision e8a3eecf0a481665c5e886ebfb4a6184b5125f78
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.database.Cursor; 20import android.net.Uri; 21import android.os.Parcel; 22import android.os.Parcelable; 23import android.provider.BaseColumns; 24import android.text.TextUtils; 25 26import com.android.mail.providers.UIProvider.ConversationColumns; 27import com.google.common.collect.ImmutableList; 28 29import java.util.ArrayList; 30import java.util.Collection; 31import java.util.Collections; 32 33public class Conversation implements Parcelable { 34 public static final int NO_POSITION = -1; 35 36 /** 37 * @see BaseColumns#_ID 38 */ 39 public long id; 40 /** 41 * @see UIProvider.ConversationColumns#URI 42 */ 43 public Uri uri; 44 /** 45 * @see UIProvider.ConversationColumns#SUBJECT 46 */ 47 public String subject; 48 /** 49 * @see UIProvider.ConversationColumns#DATE_RECEIVED_MS 50 */ 51 public long dateMs; 52 /** 53 * @see UIProvider.ConversationColumns#SNIPPET 54 */ 55 @Deprecated 56 public String snippet; 57 /** 58 * @see UIProvider.ConversationColumns#HAS_ATTACHMENTS 59 */ 60 public boolean hasAttachments; 61 /** 62 * @see UIProvider.ConversationColumns#MESSAGE_LIST_URI 63 */ 64 public Uri messageListUri; 65 /** 66 * @see UIProvider.ConversationColumns#SENDER_INFO 67 */ 68 @Deprecated 69 public String senders; 70 /** 71 * @see UIProvider.ConversationColumns#NUM_MESSAGES 72 */ 73 public int numMessages; 74 /** 75 * @see UIProvider.ConversationColumns#NUM_DRAFTS 76 */ 77 public int numDrafts; 78 /** 79 * @see UIProvider.ConversationColumns#SENDING_STATE 80 */ 81 public int sendingState; 82 /** 83 * @see UIProvider.ConversationColumns#PRIORITY 84 */ 85 public int priority; 86 /** 87 * @see UIProvider.ConversationColumns#READ 88 */ 89 public boolean read; 90 /** 91 * @see UIProvider.ConversationColumns#STARRED 92 */ 93 public boolean starred; 94 /** 95 * @see UIProvider.ConversationColumns#RAW_FOLDERS 96 */ 97 private String rawFolders; 98 /** 99 * @see UIProvider.ConversationColumns#FLAGS 100 */ 101 public int convFlags; 102 /** 103 * @see UIProvider.ConversationColumns#PERSONAL_LEVEL 104 */ 105 public int personalLevel; 106 /** 107 * @see UIProvider.ConversationColumns#SPAM 108 */ 109 public boolean spam; 110 /** 111 * @see UIProvider.ConversationColumns#MUTED 112 */ 113 public boolean muted; 114 /** 115 * @see UIProvider.ConversationColumns#PHISHING 116 */ 117 public boolean phishing; 118 /** 119 * @see UIProvider.ConversationColumns#COLOR 120 */ 121 public int color; 122 /** 123 * @see UIProvider.ConversationColumns#ACCOUNT_URI 124 */ 125 public Uri accountUri; 126 /** 127 * @see UIProvider.ConversationColumns#CONVERSATION_INFO 128 */ 129 public ConversationInfo conversationInfo; 130 131 // Used within the UI to indicate the adapter position of this conversation 132 public transient int position; 133 // Used within the UI to indicate that a Conversation should be removed from 134 // the ConversationCursor when executing an update, e.g. the the 135 // Conversation is no longer in the ConversationList for the current folder, 136 // that is it's now in some other folder(s) 137 public transient boolean localDeleteOnUpdate; 138 139 private ArrayList<Folder> cachedRawFolders; 140 private ArrayList<Folder> cachedDisplayableFolders; 141 142 // Constituents of convFlags below 143 // Flag indicating that the item has been deleted, but will continue being 144 // shown in the list Delete/Archive of a mostly-dead item will NOT propagate 145 // the delete/archive, but WILL remove the item from the cursor 146 public static final int FLAG_MOSTLY_DEAD = 1 << 0; 147 148 /** An immutable, empty conversation list */ 149 public static final Collection<Conversation> EMPTY = Collections.emptyList(); 150 151 @Override 152 public int describeContents() { 153 return 0; 154 } 155 156 @Override 157 public void writeToParcel(Parcel dest, int flags) { 158 dest.writeLong(id); 159 dest.writeParcelable(uri, flags); 160 dest.writeString(subject); 161 dest.writeLong(dateMs); 162 dest.writeString(snippet); 163 dest.writeByte(hasAttachments ? (byte) 1 : 0); 164 dest.writeParcelable(messageListUri, 0); 165 dest.writeString(senders); 166 dest.writeInt(numMessages); 167 dest.writeInt(numDrafts); 168 dest.writeInt(sendingState); 169 dest.writeInt(priority); 170 dest.writeByte(read ? (byte) 1 : 0); 171 dest.writeByte(starred ? (byte) 1 : 0); 172 dest.writeString(rawFolders); 173 dest.writeInt(convFlags); 174 dest.writeInt(personalLevel); 175 dest.writeInt(spam ? 1 : 0); 176 dest.writeInt(phishing ? 1 : 0); 177 dest.writeInt(muted ? 1 : 0); 178 dest.writeInt(color); 179 dest.writeParcelable(accountUri, 0); 180 dest.writeString(ConversationInfo.toString(conversationInfo)); 181 } 182 183 private Conversation(Parcel in) { 184 id = in.readLong(); 185 uri = in.readParcelable(null); 186 subject = in.readString(); 187 dateMs = in.readLong(); 188 snippet = in.readString(); 189 hasAttachments = (in.readByte() != 0); 190 messageListUri = in.readParcelable(null); 191 senders = in.readString(); 192 numMessages = in.readInt(); 193 numDrafts = in.readInt(); 194 sendingState = in.readInt(); 195 priority = in.readInt(); 196 read = (in.readByte() != 0); 197 starred = (in.readByte() != 0); 198 rawFolders = in.readString(); 199 convFlags = in.readInt(); 200 personalLevel = in.readInt(); 201 spam = in.readInt() != 0; 202 phishing = in.readInt() != 0; 203 muted = in.readInt() != 0; 204 color = in.readInt(); 205 accountUri = in.readParcelable(null); 206 position = NO_POSITION; 207 localDeleteOnUpdate = false; 208 conversationInfo = ConversationInfo.fromString(in.readString()); 209 } 210 211 @Override 212 public String toString() { 213 return "[conversation id=" + id + ", subject =" + subject + "]"; 214 } 215 216 public static final Creator<Conversation> CREATOR = new Creator<Conversation>() { 217 218 @Override 219 public Conversation createFromParcel(Parcel source) { 220 return new Conversation(source); 221 } 222 223 @Override 224 public Conversation[] newArray(int size) { 225 return new Conversation[size]; 226 } 227 228 }; 229 230 public static final Uri MOVE_CONVERSATIONS_URI = Uri.parse("content://moveconversations"); 231 232 /** 233 * The column that needs to be updated to change the read state of a 234 * conversation. 235 */ 236 public static final String UPDATE_FOLDER_COLUMN = ConversationColumns.RAW_FOLDERS; 237 238 public Conversation(Cursor cursor) { 239 if (cursor != null) { 240 id = cursor.getLong(UIProvider.CONVERSATION_ID_COLUMN); 241 uri = Uri.parse(cursor.getString(UIProvider.CONVERSATION_URI_COLUMN)); 242 dateMs = cursor.getLong(UIProvider.CONVERSATION_DATE_RECEIVED_MS_COLUMN); 243 subject = cursor.getString(UIProvider.CONVERSATION_SUBJECT_COLUMN); 244 // Don't allow null subject 245 if (subject == null) { 246 subject = ""; 247 } 248 hasAttachments = cursor.getInt(UIProvider.CONVERSATION_HAS_ATTACHMENTS_COLUMN) != 0; 249 String messageList = cursor.getString(UIProvider.CONVERSATION_MESSAGE_LIST_URI_COLUMN); 250 messageListUri = !TextUtils.isEmpty(messageList) ? Uri.parse(messageList) : null; 251 numMessages = cursor.getInt(UIProvider.CONVERSATION_NUM_MESSAGES_COLUMN); 252 numDrafts = cursor.getInt(UIProvider.CONVERSATION_NUM_DRAFTS_COLUMN); 253 sendingState = cursor.getInt(UIProvider.CONVERSATION_SENDING_STATE_COLUMN); 254 priority = cursor.getInt(UIProvider.CONVERSATION_PRIORITY_COLUMN); 255 read = cursor.getInt(UIProvider.CONVERSATION_READ_COLUMN) != 0; 256 starred = cursor.getInt(UIProvider.CONVERSATION_STARRED_COLUMN) != 0; 257 rawFolders = cursor.getString(UIProvider.CONVERSATION_RAW_FOLDERS_COLUMN); 258 convFlags = cursor.getInt(UIProvider.CONVERSATION_FLAGS_COLUMN); 259 personalLevel = cursor.getInt(UIProvider.CONVERSATION_PERSONAL_LEVEL_COLUMN); 260 spam = cursor.getInt(UIProvider.CONVERSATION_IS_SPAM_COLUMN) != 0; 261 phishing = cursor.getInt(UIProvider.CONVERSATION_IS_PHISHING_COLUMN) != 0; 262 muted = cursor.getInt(UIProvider.CONVERSATION_MUTED_COLUMN) != 0; 263 color = cursor.getInt(UIProvider.CONVERSATION_COLOR_COLUMN); 264 String account = cursor.getString(UIProvider.CONVERSATION_ACCOUNT_URI_COLUMN); 265 accountUri = !TextUtils.isEmpty(account) ? Uri.parse(account) : null; 266 position = NO_POSITION; 267 localDeleteOnUpdate = false; 268 conversationInfo = ConversationInfo.fromString(cursor 269 .getString(UIProvider.CONVERSATION_INFO_COLUMN)); 270 if (conversationInfo == null) { 271 snippet = cursor.getString(UIProvider.CONVERSATION_SNIPPET_COLUMN); 272 senders = cursor.getString(UIProvider.CONVERSATION_SENDER_INFO_COLUMN); 273 } 274 } 275 } 276 277 public Conversation() { 278 } 279 280 public static Conversation create(long id, Uri uri, String subject, long dateMs, 281 String snippet, boolean hasAttachment, Uri messageListUri, String senders, 282 int numMessages, int numDrafts, int sendingState, int priority, boolean read, 283 boolean starred, String rawFolders, int convFlags, int personalLevel, boolean spam, 284 boolean phishing, boolean muted, Uri accountUri, ConversationInfo conversationInfo) { 285 286 final Conversation conversation = new Conversation(); 287 288 conversation.id = id; 289 conversation.uri = uri; 290 conversation.subject = subject; 291 conversation.dateMs = dateMs; 292 conversation.snippet = snippet; 293 conversation.hasAttachments = hasAttachment; 294 conversation.messageListUri = messageListUri; 295 conversation.senders = senders; 296 conversation.numMessages = numMessages; 297 conversation.numDrafts = numDrafts; 298 conversation.sendingState = sendingState; 299 conversation.priority = priority; 300 conversation.read = read; 301 conversation.starred = starred; 302 conversation.rawFolders = rawFolders; 303 conversation.convFlags = convFlags; 304 conversation.personalLevel = personalLevel; 305 conversation.spam = spam; 306 conversation.phishing = phishing; 307 conversation.muted = muted; 308 conversation.color = 0; 309 conversation.accountUri = accountUri; 310 conversation.conversationInfo = conversationInfo; 311 return conversation; 312 } 313 314 public ArrayList<Folder> getRawFolders() { 315 if (cachedRawFolders == null) { 316 // Create cached folders. 317 if (!TextUtils.isEmpty(rawFolders)) { 318 cachedRawFolders = Folder.getFoldersArray(rawFolders); 319 } else { 320 return new ArrayList<Folder>(); 321 } 322 } 323 return cachedRawFolders; 324 } 325 326 public void setRawFolders(String raw) { 327 clearCachedFolders(); 328 rawFolders = raw; 329 } 330 331 public String getRawFoldersString() { 332 return rawFolders; 333 } 334 335 private void clearCachedFolders() { 336 cachedRawFolders = null; 337 cachedDisplayableFolders = null; 338 } 339 340 public ArrayList<Folder> getRawFoldersForDisplay(Folder ignoreFolder) { 341 ArrayList<Folder> folders = getRawFolders(); 342 if (cachedDisplayableFolders == null) { 343 cachedDisplayableFolders = new ArrayList<Folder>(); 344 for (Folder folder : folders) { 345 // skip the ignoreFolder 346 if (ignoreFolder != null && ignoreFolder.equals(folder)) { 347 continue; 348 } 349 cachedDisplayableFolders.add(folder); 350 } 351 } 352 return cachedDisplayableFolders; 353 } 354 355 @Override 356 public boolean equals(Object o) { 357 if (o instanceof Conversation) { 358 Conversation conv = (Conversation) o; 359 return conv.uri.equals(uri); 360 } 361 return false; 362 } 363 364 @Override 365 public int hashCode() { 366 return uri.hashCode(); 367 } 368 369 /** 370 * Get if this conversation is marked as high priority. 371 */ 372 public boolean isImportant() { 373 return priority == UIProvider.ConversationPriority.IMPORTANT; 374 } 375 376 /** 377 * Get if this conversation is mostly dead 378 */ 379 public boolean isMostlyDead() { 380 return (convFlags & FLAG_MOSTLY_DEAD) != 0; 381 } 382 383 /** 384 * Returns true if the URI of the conversation specified as the needle was 385 * found in the collection of conversations specified as the haystack. False 386 * otherwise. This method is safe to call with null arguments. 387 * 388 * @param haystack 389 * @param needle 390 * @return true if the needle was found in the haystack, false otherwise. 391 */ 392 public final static boolean contains(Collection<Conversation> haystack, Conversation needle) { 393 // If the haystack is empty, it cannot contain anything. 394 if (haystack == null || haystack.size() <= 0) { 395 return false; 396 } 397 // The null folder exists everywhere. 398 if (needle == null) { 399 return true; 400 } 401 final long toFind = needle.id; 402 for (final Conversation c : haystack) { 403 if (toFind == c.id) { 404 return true; 405 } 406 } 407 return false; 408 } 409 410 /** 411 * Returns a collection of a single conversation. This method always returns 412 * a valid collection even if the input conversation is null. 413 * 414 * @param in a conversation, possibly null. 415 * @return a collection of the conversation. 416 */ 417 public static Collection<Conversation> listOf(Conversation in) { 418 final Collection<Conversation> target = (in == null) ? EMPTY : ImmutableList.of(in); 419 return target; 420 } 421 422 /** 423 * Get the snippet for this conversation. Masks that it may come from 424 * conversation info or the original deprecated snippet string. 425 */ 426 public String getSnippet() { 427 return conversationInfo != null && !TextUtils.isEmpty(conversationInfo.firstSnippet) ? 428 conversationInfo.firstSnippet : snippet; 429 } 430 431 /** 432 * Create a human-readable string of all the conversations 433 * @param collection Any collection of conversations 434 * @return string with a human readable representation of the conversations. 435 */ 436 public static String toString(Collection<Conversation> collection) { 437 final StringBuilder out = new StringBuilder(collection.size() + " conversations:"); 438 int count = 0; 439 for (final Conversation c : collection) { 440 count++; 441 // Indent the conversations to make them easy to read in debug 442 // output. 443 out.append(" " + count + ": " + c.toString() + "\n"); 444 } 445 return out.toString(); 446 } 447} 448