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