Conversation.java revision 8d69d4e10a9a36ff790babb2f3a098a12d0dc732
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.ContentProviderClient; 20import android.content.ContentValues; 21import android.content.Context; 22import android.database.Cursor; 23import android.net.Uri; 24import android.os.Parcel; 25import android.os.Parcelable; 26 27import com.android.mail.browse.ConversationCursor.ConversationOperation; 28import com.android.mail.browse.ConversationCursor.ConversationProvider; 29 30import java.util.ArrayList; 31import java.util.Collection; 32 33public class Conversation implements Parcelable { 34 35 public long id; 36 public String subject; 37 public long dateMs; 38 public String snippet; 39 public boolean hasAttachments; 40 public Uri messageListUri; 41 public String senders; 42 public int numMessages; 43 public int numDrafts; 44 public int sendingState; 45 public int priority; 46 public boolean read; 47 public boolean starred; 48 49 @Override 50 public int describeContents() { 51 return 0; 52 } 53 54 @Override 55 public void writeToParcel(Parcel dest, int flags) { 56 dest.writeLong(id); 57 dest.writeString(subject); 58 dest.writeLong(dateMs); 59 dest.writeString(snippet); 60 dest.writeByte(hasAttachments ? (byte) 1 : 0); 61 dest.writeParcelable(messageListUri, flags); 62 dest.writeString(senders); 63 dest.writeInt(numMessages); 64 dest.writeInt(numDrafts); 65 dest.writeInt(sendingState); 66 dest.writeInt(priority); 67 dest.writeByte(read ? (byte) 1 : 0); 68 dest.writeByte(starred ? (byte) 1 : 0); 69 } 70 71 private Conversation(Parcel in) { 72 id = in.readLong(); 73 subject = in.readString(); 74 dateMs = in.readLong(); 75 snippet = in.readString(); 76 hasAttachments = (in.readByte() != 0); 77 messageListUri = in.readParcelable(null); 78 senders = in.readString(); 79 numMessages = in.readInt(); 80 numDrafts = in.readInt(); 81 sendingState = in.readInt(); 82 priority = in.readInt(); 83 read = (in.readByte() != 0); 84 starred = (in.readByte() != 0); 85 } 86 87 @Override 88 public String toString() { 89 return "[conversation id=" + id + "]"; 90 } 91 92 public static final Creator<Conversation> CREATOR = new Creator<Conversation>() { 93 94 @Override 95 public Conversation createFromParcel(Parcel source) { 96 return new Conversation(source); 97 } 98 99 @Override 100 public Conversation[] newArray(int size) { 101 return new Conversation[size]; 102 } 103 104 }; 105 106 public static Conversation from(Cursor cursor) { 107 return new Conversation(cursor); 108 } 109 110 private Conversation(Cursor cursor) { 111 if (cursor != null) { 112 id = cursor.getLong(UIProvider.CONVERSATION_ID_COLUMN); 113 dateMs = cursor.getLong(UIProvider.CONVERSATION_DATE_RECEIVED_MS_COLUMN); 114 subject = cursor.getString(UIProvider.CONVERSATION_SUBJECT_COLUMN); 115 snippet = cursor.getString(UIProvider.CONVERSATION_SNIPPET_COLUMN); 116 hasAttachments = cursor.getInt(UIProvider.CONVERSATION_HAS_ATTACHMENTS_COLUMN) == 1; 117 messageListUri = Uri.parse(cursor 118 .getString(UIProvider.CONVERSATION_MESSAGE_LIST_URI_COLUMN)); 119 senders = cursor.getString(UIProvider.CONVERSATION_SENDER_INFO_COLUMN); 120 numMessages = cursor.getInt(UIProvider.CONVERSATION_NUM_MESSAGES_COLUMN); 121 numDrafts = cursor.getInt(UIProvider.CONVERSATION_NUM_DRAFTS_COLUMN); 122 sendingState = cursor.getInt(UIProvider.CONVERSATION_SENDING_STATE_COLUMN); 123 priority = cursor.getInt(UIProvider.CONVERSATION_PRIORITY_COLUMN); 124 read = cursor.getInt(UIProvider.CONVERSATION_READ_COLUMN) == 1; 125 starred = cursor.getInt(UIProvider.CONVERSATION_STARRED_COLUMN) == 1; 126 } 127 } 128 129 // Below are methods that update Conversation data (update/delete) 130 131 /** 132 * Update a boolean column for a single conversation 133 * @param context the caller's context 134 * @param columnName the column to update 135 * @param value the new value 136 */ 137 public void updateBoolean(Context context, String columnName, boolean value) { 138 ContentValues cv = new ContentValues(); 139 cv.put(columnName, value); 140 context.getContentResolver().update(messageListUri, cv, null, null); 141 } 142 143 /** 144 * Update a boolean column for a group of conversations, immediately in the UI and in a single 145 * transaction in the underlying provider 146 * @param conversations a collection of conversations 147 * @param context the caller's context 148 * @param columnName the column to update 149 * @param value the new value 150 */ 151 public static void updateBoolean(Context context, Collection<Conversation> conversations, 152 String columnName, boolean value) { 153 ContentValues cv = new ContentValues(); 154 cv.put(columnName, value); 155 ArrayList<ConversationOperation> ops = new ArrayList<ConversationOperation>(); 156 for (Conversation conv: conversations) { 157 ConversationOperation op = 158 new ConversationOperation(ConversationOperation.UPDATE, conv.messageListUri, 159 cv); 160 ops.add(op); 161 } 162 apply(context, ops); 163 } 164 165 /** 166 * Delete a single conversation 167 * @param context the caller's context 168 */ 169 public void delete(Context context) { 170 context.getContentResolver().delete(messageListUri, null, null); 171 } 172 173 /** 174 * Delete a group of conversations immediately in the UI and in a single transaction in the 175 * underlying provider 176 * @param context the caller's context 177 * @param conversations a collection of conversations 178 */ 179 public static void delete(Context context, Collection<Conversation> conversations) { 180 ArrayList<ConversationOperation> ops = new ArrayList<ConversationOperation>(); 181 for (Conversation conv: conversations) { 182 ConversationOperation op = 183 new ConversationOperation(ConversationOperation.DELETE, conv.messageListUri); 184 ops.add(op); 185 } 186 apply(context, ops); 187 } 188 189 // Convenience methods 190 private static void apply(Context context, ArrayList<ConversationOperation> operations) { 191 ContentProviderClient client = 192 context.getContentResolver().acquireContentProviderClient( 193 ConversationProvider.AUTHORITY); 194 try { 195 ConversationProvider cp = (ConversationProvider)client.getLocalContentProvider(); 196 cp.apply(operations); 197 } finally { 198 client.release(); 199 } 200 } 201} 202