Conversation.java revision 374863eeb0ee37f1ca7d57840e6f02aaa052e284
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            // Don't allow null subject
116            if (subject == null) {
117                subject = "";
118            }
119            snippet = cursor.getString(UIProvider.CONVERSATION_SNIPPET_COLUMN);
120            hasAttachments = cursor.getInt(UIProvider.CONVERSATION_HAS_ATTACHMENTS_COLUMN) == 1;
121            messageListUri = Uri.parse(cursor
122                    .getString(UIProvider.CONVERSATION_MESSAGE_LIST_URI_COLUMN));
123            senders = cursor.getString(UIProvider.CONVERSATION_SENDER_INFO_COLUMN);
124            numMessages = cursor.getInt(UIProvider.CONVERSATION_NUM_MESSAGES_COLUMN);
125            numDrafts = cursor.getInt(UIProvider.CONVERSATION_NUM_DRAFTS_COLUMN);
126            sendingState = cursor.getInt(UIProvider.CONVERSATION_SENDING_STATE_COLUMN);
127            priority = cursor.getInt(UIProvider.CONVERSATION_PRIORITY_COLUMN);
128            read = cursor.getInt(UIProvider.CONVERSATION_READ_COLUMN) == 1;
129            starred = cursor.getInt(UIProvider.CONVERSATION_STARRED_COLUMN) == 1;
130        }
131    }
132
133    // Below are methods that update Conversation data (update/delete)
134
135    /**
136     * Update a boolean column for a single conversation
137     * @param context the caller's context
138     * @param columnName the column to update
139     * @param value the new value
140     */
141    public void updateBoolean(Context context, String columnName, boolean value) {
142        ContentValues cv = new ContentValues();
143        cv.put(columnName, value);
144        context.getContentResolver().update(messageListUri, cv, null, null);
145    }
146
147    /**
148     * Update a boolean column for a group of conversations, immediately in the UI and in a single
149     * transaction in the underlying provider
150     * @param conversations a collection of conversations
151     * @param context the caller's context
152     * @param columnName the column to update
153     * @param value the new value
154     */
155    public static void updateBoolean(Context context, Collection<Conversation> conversations,
156            String columnName, boolean value) {
157        ContentValues cv = new ContentValues();
158        cv.put(columnName, value);
159        ArrayList<ConversationOperation> ops = new ArrayList<ConversationOperation>();
160        for (Conversation conv: conversations) {
161            ConversationOperation op =
162                    new ConversationOperation(ConversationOperation.UPDATE, conv.messageListUri,
163                            cv);
164            ops.add(op);
165        }
166        apply(context, ops);
167    }
168
169    /**
170     * Delete a single conversation
171     * @param context the caller's context
172     */
173    public void delete(Context context) {
174        context.getContentResolver().delete(messageListUri, null, null);
175    }
176
177    /**
178     * Delete a group of conversations immediately in the UI and in a single transaction in the
179     * underlying provider
180     * @param context the caller's context
181     * @param conversations a collection of conversations
182     */
183    public static void delete(Context context, Collection<Conversation> conversations) {
184        ArrayList<ConversationOperation> ops = new ArrayList<ConversationOperation>();
185        for (Conversation conv: conversations) {
186            ConversationOperation op =
187                    new ConversationOperation(ConversationOperation.DELETE, conv.messageListUri);
188            ops.add(op);
189        }
190        apply(context, ops);
191    }
192
193    // Convenience methods
194    private static void apply(Context context, ArrayList<ConversationOperation> operations) {
195        ContentProviderClient client =
196                context.getContentResolver().acquireContentProviderClient(
197                        ConversationProvider.AUTHORITY);
198        try {
199            ConversationProvider cp = (ConversationProvider)client.getLocalContentProvider();
200            cp.apply(operations);
201        } finally {
202            client.release();
203        }
204    }
205}
206