Conversation.java revision 4f2224c70d4d07df85b325fa3faf78218f92aae6
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            snippet = cursor.getString(UIProvider.CONVERSATION_SNIPPET_COLUMN);
249            hasAttachments = cursor.getInt(UIProvider.CONVERSATION_HAS_ATTACHMENTS_COLUMN) != 0;
250            String messageList = cursor.getString(UIProvider.CONVERSATION_MESSAGE_LIST_URI_COLUMN);
251            messageListUri = !TextUtils.isEmpty(messageList) ? Uri.parse(messageList) : null;
252            numMessages = cursor.getInt(UIProvider.CONVERSATION_NUM_MESSAGES_COLUMN);
253            numDrafts = cursor.getInt(UIProvider.CONVERSATION_NUM_DRAFTS_COLUMN);
254            sendingState = cursor.getInt(UIProvider.CONVERSATION_SENDING_STATE_COLUMN);
255            priority = cursor.getInt(UIProvider.CONVERSATION_PRIORITY_COLUMN);
256            read = cursor.getInt(UIProvider.CONVERSATION_READ_COLUMN) != 0;
257            starred = cursor.getInt(UIProvider.CONVERSATION_STARRED_COLUMN) != 0;
258            rawFolders = cursor.getString(UIProvider.CONVERSATION_RAW_FOLDERS_COLUMN);
259            convFlags = cursor.getInt(UIProvider.CONVERSATION_FLAGS_COLUMN);
260            personalLevel = cursor.getInt(UIProvider.CONVERSATION_PERSONAL_LEVEL_COLUMN);
261            spam = cursor.getInt(UIProvider.CONVERSATION_IS_SPAM_COLUMN) != 0;
262            phishing = cursor.getInt(UIProvider.CONVERSATION_IS_PHISHING_COLUMN) != 0;
263            muted = cursor.getInt(UIProvider.CONVERSATION_MUTED_COLUMN) != 0;
264            color = cursor.getInt(UIProvider.CONVERSATION_COLOR_COLUMN);
265            String account = cursor.getString(UIProvider.CONVERSATION_ACCOUNT_URI_COLUMN);
266            accountUri = !TextUtils.isEmpty(account) ? Uri.parse(account) : null;
267            position = NO_POSITION;
268            localDeleteOnUpdate = false;
269            senders = cursor.getString(UIProvider.CONVERSATION_SENDER_INFO_COLUMN);
270            conversationInfo = ConversationInfo.fromString(cursor
271                    .getString(UIProvider.CONVERSATION_INFO_COLUMN));
272        }
273    }
274
275    public Conversation() {
276    }
277
278    public static Conversation create(long id, Uri uri, String subject, long dateMs,
279            String snippet, boolean hasAttachment, Uri messageListUri, String senders,
280            int numMessages, int numDrafts, int sendingState, int priority, boolean read,
281            boolean starred, String rawFolders, int convFlags, int personalLevel, boolean spam,
282            boolean phishing, boolean muted, Uri accountUri, ConversationInfo conversationInfo) {
283
284        final Conversation conversation = new Conversation();
285
286        conversation.id = id;
287        conversation.uri = uri;
288        conversation.subject = subject;
289        conversation.dateMs = dateMs;
290        conversation.snippet = snippet;
291        conversation.hasAttachments = hasAttachment;
292        conversation.messageListUri = messageListUri;
293        conversation.senders = senders;
294        conversation.numMessages = numMessages;
295        conversation.numDrafts = numDrafts;
296        conversation.sendingState = sendingState;
297        conversation.priority = priority;
298        conversation.read = read;
299        conversation.starred = starred;
300        conversation.rawFolders = rawFolders;
301        conversation.convFlags = convFlags;
302        conversation.personalLevel = personalLevel;
303        conversation.spam = spam;
304        conversation.phishing = phishing;
305        conversation.muted = muted;
306        conversation.color = 0;
307        conversation.accountUri = accountUri;
308        conversation.conversationInfo = conversationInfo;
309        return conversation;
310    }
311
312    public ArrayList<Folder> getRawFolders() {
313        if (cachedRawFolders == null) {
314            // Create cached folders.
315            if (!TextUtils.isEmpty(rawFolders)) {
316                cachedRawFolders = Folder.getFoldersArray(rawFolders);
317            } else {
318                return new ArrayList<Folder>();
319            }
320        }
321        return cachedRawFolders;
322    }
323
324    public void setRawFolders(String raw) {
325        clearCachedFolders();
326        rawFolders = raw;
327    }
328
329    public String getRawFoldersString() {
330        return rawFolders;
331    }
332
333    private void clearCachedFolders() {
334        cachedRawFolders = null;
335        cachedDisplayableFolders = null;
336    }
337
338    public ArrayList<Folder> getRawFoldersForDisplay(Folder ignoreFolder) {
339        ArrayList<Folder> folders = getRawFolders();
340        if (cachedDisplayableFolders == null) {
341            cachedDisplayableFolders = new ArrayList<Folder>();
342            for (Folder folder : folders) {
343                // skip the ignoreFolder
344                if (ignoreFolder != null && ignoreFolder.equals(folder)) {
345                    continue;
346                }
347                cachedDisplayableFolders.add(folder);
348            }
349        }
350        return cachedDisplayableFolders;
351    }
352
353    @Override
354    public boolean equals(Object o) {
355        if (o instanceof Conversation) {
356            Conversation conv = (Conversation) o;
357            return conv.uri.equals(uri);
358        }
359        return false;
360    }
361
362    @Override
363    public int hashCode() {
364        return uri.hashCode();
365    }
366
367    /**
368     * Get if this conversation is marked as high priority.
369     */
370    public boolean isImportant() {
371        return priority == UIProvider.ConversationPriority.IMPORTANT;
372    }
373
374    /**
375     * Get if this conversation is mostly dead
376     */
377    public boolean isMostlyDead() {
378        return (convFlags & FLAG_MOSTLY_DEAD) != 0;
379    }
380
381    /**
382     * Returns true if the URI of the conversation specified as the needle was
383     * found in the collection of conversations specified as the haystack. False
384     * otherwise. This method is safe to call with null arguments.
385     *
386     * @param haystack
387     * @param needle
388     * @return true if the needle was found in the haystack, false otherwise.
389     */
390    public final static boolean contains(Collection<Conversation> haystack, Conversation needle) {
391        // If the haystack is empty, it cannot contain anything.
392        if (haystack == null || haystack.size() <= 0) {
393            return false;
394        }
395        // The null folder exists everywhere.
396        if (needle == null) {
397            return true;
398        }
399        final long toFind = needle.id;
400        for (final Conversation c : haystack) {
401            if (toFind == c.id) {
402                return true;
403            }
404        }
405        return false;
406    }
407
408    /**
409     * Returns a collection of a single conversation. This method always returns
410     * a valid collection even if the input conversation is null.
411     *
412     * @param in a conversation, possibly null.
413     * @return a collection of the conversation.
414     */
415    public static Collection<Conversation> listOf(Conversation in) {
416        final Collection<Conversation> target = (in == null) ? EMPTY : ImmutableList.of(in);
417        return target;
418    }
419
420    /**
421     * Get the snippet for this conversation. Masks that it may come from
422     * conversation info or the original deprecated snippet string.
423     */
424    public String getSnippet() {
425        return conversationInfo != null && !TextUtils.isEmpty(conversationInfo.firstSnippet) ?
426                conversationInfo.firstSnippet : snippet;
427    }
428
429    public String getSenders() {
430        return conversationInfo != null ? conversationInfo.firstSnippet : senders;
431    }
432
433    /**
434     * Create a human-readable string of all the conversations
435     * @param collection Any collection of conversations
436     * @return string with a human readable representation of the conversations.
437     */
438    public static String toString(Collection<Conversation> collection) {
439        final StringBuilder out = new StringBuilder(collection.size() + " conversations:");
440        int count = 0;
441        for (final Conversation c : collection) {
442            count++;
443            // Indent the conversations to make them easy to read in debug
444            // output.
445            out.append("      " + count + ": " + c.toString() + "\n");
446        }
447        return out.toString();
448    }
449}
450