Conversation.java revision 92939fc7b40a56e17fb0d2fde987133ca1614e29
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.text.TextUtils; 24 25public class Conversation implements Parcelable { 26 public static final int NO_POSITION = -1; 27 28 public long id; 29 public Uri uri; 30 public String subject; 31 public long dateMs; 32 public String snippet; 33 public boolean hasAttachments; 34 public Uri messageListUri; 35 public String senders; 36 public int numMessages; 37 public int numDrafts; 38 public int sendingState; 39 public int priority; 40 public boolean read; 41 public boolean starred; 42 public String folderList; 43 public String rawFolders; 44 public int convFlags; 45 public int personalLevel; 46 public boolean spam; 47 public boolean muted; 48 public int color; 49 50 // Used within the UI to indicate the adapter position of this conversation 51 public transient int position; 52 // Used within the UI to indicate that a Conversation should be removed from the 53 // ConversationCursor when executing an update, e.g. the the Conversation is no longer 54 // in the ConversationList for the current folder, that is it's now in some other folder(s) 55 public transient boolean localDeleteOnUpdate; 56 57 // Constituents of convFlags below 58 // Flag indicating that the item has been deleted, but will continue being shown in the list 59 // Delete/Archive of a mostly-dead item will NOT propagate the delete/archive, but WILL remove 60 // the item from the cursor 61 public static final int FLAG_MOSTLY_DEAD = 1 << 0; 62 63 @Override 64 public int describeContents() { 65 return 0; 66 } 67 68 @Override 69 public void writeToParcel(Parcel dest, int flags) { 70 dest.writeLong(id); 71 dest.writeParcelable(uri, flags); 72 dest.writeString(subject); 73 dest.writeLong(dateMs); 74 dest.writeString(snippet); 75 dest.writeByte(hasAttachments ? (byte) 1 : 0); 76 dest.writeParcelable(messageListUri, 0); 77 dest.writeString(senders); 78 dest.writeInt(numMessages); 79 dest.writeInt(numDrafts); 80 dest.writeInt(sendingState); 81 dest.writeInt(priority); 82 dest.writeByte(read ? (byte) 1 : 0); 83 dest.writeByte(starred ? (byte) 1 : 0); 84 dest.writeString(folderList); 85 dest.writeString(rawFolders); 86 dest.writeInt(convFlags); 87 dest.writeInt(personalLevel); 88 dest.writeInt(spam ? 1 : 0); 89 dest.writeInt(muted ? 1 : 0); 90 dest.writeInt(color); 91 } 92 93 private Conversation(Parcel in) { 94 id = in.readLong(); 95 uri = in.readParcelable(null); 96 subject = in.readString(); 97 dateMs = in.readLong(); 98 snippet = in.readString(); 99 hasAttachments = (in.readByte() != 0); 100 messageListUri = in.readParcelable(null); 101 senders = in.readString(); 102 numMessages = in.readInt(); 103 numDrafts = in.readInt(); 104 sendingState = in.readInt(); 105 priority = in.readInt(); 106 read = (in.readByte() != 0); 107 starred = (in.readByte() != 0); 108 folderList = in.readString(); 109 rawFolders = in.readString(); 110 convFlags = in.readInt(); 111 personalLevel = in.readInt(); 112 spam = in.readInt() != 0; 113 muted = in.readInt() != 0; 114 color = in.readInt(); 115 position = NO_POSITION; 116 localDeleteOnUpdate = false; 117 } 118 119 @Override 120 public String toString() { 121 return "[conversation id=" + id + "]"; 122 } 123 124 public static final Creator<Conversation> CREATOR = new Creator<Conversation>() { 125 126 @Override 127 public Conversation createFromParcel(Parcel source) { 128 return new Conversation(source); 129 } 130 131 @Override 132 public Conversation[] newArray(int size) { 133 return new Conversation[size]; 134 } 135 136 }; 137 138 public static final Uri MOVE_CONVERSATIONS_URI = Uri.parse("content://moveconversations"); 139 140 public Conversation(Cursor cursor) { 141 if (cursor != null) { 142 id = cursor.getLong(UIProvider.CONVERSATION_ID_COLUMN); 143 uri = Uri.parse(cursor.getString(UIProvider.CONVERSATION_URI_COLUMN)); 144 dateMs = cursor.getLong(UIProvider.CONVERSATION_DATE_RECEIVED_MS_COLUMN); 145 subject = cursor.getString(UIProvider.CONVERSATION_SUBJECT_COLUMN); 146 // Don't allow null subject 147 if (subject == null) { 148 subject = ""; 149 } 150 snippet = cursor.getString(UIProvider.CONVERSATION_SNIPPET_COLUMN); 151 hasAttachments = cursor.getInt(UIProvider.CONVERSATION_HAS_ATTACHMENTS_COLUMN) != 0; 152 String messageList = cursor 153 .getString(UIProvider.CONVERSATION_MESSAGE_LIST_URI_COLUMN); 154 messageListUri = !TextUtils.isEmpty(messageList) ? Uri.parse(messageList) : null; 155 senders = cursor.getString(UIProvider.CONVERSATION_SENDER_INFO_COLUMN); 156 numMessages = cursor.getInt(UIProvider.CONVERSATION_NUM_MESSAGES_COLUMN); 157 numDrafts = cursor.getInt(UIProvider.CONVERSATION_NUM_DRAFTS_COLUMN); 158 sendingState = cursor.getInt(UIProvider.CONVERSATION_SENDING_STATE_COLUMN); 159 priority = cursor.getInt(UIProvider.CONVERSATION_PRIORITY_COLUMN); 160 read = cursor.getInt(UIProvider.CONVERSATION_READ_COLUMN) != 0; 161 starred = cursor.getInt(UIProvider.CONVERSATION_STARRED_COLUMN) != 0; 162 folderList = cursor.getString(UIProvider.CONVERSATION_FOLDER_LIST_COLUMN); 163 rawFolders = cursor.getString(UIProvider.CONVERSATION_RAW_FOLDERS_COLUMN); 164 convFlags = cursor.getInt(UIProvider.CONVERSATION_FLAGS_COLUMN); 165 personalLevel = cursor.getInt(UIProvider.CONVERSATION_PERSONAL_LEVEL_COLUMN); 166 spam = cursor.getInt(UIProvider.CONVERSATION_IS_SPAM_COLUMN) != 0; 167 muted = cursor.getInt(UIProvider.CONVERSATION_MUTED_COLUMN) != 0; 168 color = cursor.getInt(UIProvider.CONVERSATION_COLOR_COLUMN); 169 position = NO_POSITION; 170 localDeleteOnUpdate = false; 171 } 172 } 173 174 public Conversation() { 175 } 176 177 public static Conversation create(long id, Uri uri, String subject, long dateMs, 178 String snippet, boolean hasAttachment, Uri messageListUri, String senders, 179 int numMessages, int numDrafts, int sendingState, int priority, boolean read, 180 boolean starred, String folderList, String rawFolders, int convFlags, 181 int personalLevel, boolean spam, boolean muted) { 182 183 final Conversation conversation = new Conversation(); 184 185 conversation.id = id; 186 conversation.uri = uri; 187 conversation.subject = subject; 188 conversation.dateMs = dateMs; 189 conversation.snippet = snippet; 190 conversation.hasAttachments = hasAttachment; 191 conversation.messageListUri = messageListUri; 192 conversation.senders = senders; 193 conversation.numMessages = numMessages; 194 conversation.numDrafts = numDrafts; 195 conversation.sendingState = sendingState; 196 conversation.priority = priority; 197 conversation.read = read; 198 conversation.starred = starred; 199 conversation.folderList = folderList; 200 conversation.rawFolders = rawFolders; 201 conversation.convFlags = convFlags; 202 conversation.personalLevel = personalLevel; 203 conversation.spam = spam; 204 conversation.muted = muted; 205 conversation.color = 0; 206 return conversation; 207 } 208 209 @Override 210 public boolean equals(Object o) { 211 if (o instanceof Conversation) { 212 Conversation conv = (Conversation)o; 213 return conv.uri.equals(uri); 214 } 215 return false; 216 } 217 218 @Override 219 public int hashCode() { 220 return uri.hashCode(); 221 } 222 223 /** 224 * Get if this conversation is marked as high priority. 225 */ 226 public boolean isImportant() { 227 return priority == UIProvider.ConversationPriority.IMPORTANT; 228 } 229 230 /** 231 * Get if this conversation is mostly dead 232 */ 233 public boolean isMostlyDead() { 234 return (convFlags & FLAG_MOSTLY_DEAD) != 0; 235 } 236}