Conversation.java revision ae7e6a0786a2d890b77c783d7ac39a90523b8154
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.Context;
20import android.database.Cursor;
21import android.net.Uri;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.provider.BaseColumns;
25import android.text.SpannableStringBuilder;
26import android.text.TextUtils;
27
28import com.android.mail.R;
29import com.android.mail.providers.UIProvider.ConversationColumns;
30import com.google.common.collect.ImmutableList;
31
32import java.util.ArrayList;
33import java.util.Collection;
34import java.util.Collections;
35
36public class Conversation implements Parcelable {
37    public static final int NO_POSITION = -1;
38
39    private static final String EMPTY_STRING = "";
40
41    /**
42     * @see BaseColumns#_ID
43     */
44    public long id;
45    /**
46     * @see UIProvider.ConversationColumns#URI
47     */
48    public Uri uri;
49    /**
50     * @see UIProvider.ConversationColumns#SUBJECT
51     */
52    public String subject;
53    /**
54     * @see UIProvider.ConversationColumns#DATE_RECEIVED_MS
55     */
56    public long dateMs;
57    /**
58     * @see UIProvider.ConversationColumns#SNIPPET
59     */
60    @Deprecated
61    public String snippet;
62    /**
63     * @see UIProvider.ConversationColumns#HAS_ATTACHMENTS
64     */
65    public boolean hasAttachments;
66    /**
67     * @see UIProvider.ConversationColumns#MESSAGE_LIST_URI
68     */
69    public Uri messageListUri;
70    /**
71     * @see UIProvider.ConversationColumns#SENDER_INFO
72     */
73    @Deprecated
74    public String senders;
75    /**
76     * @see UIProvider.ConversationColumns#NUM_MESSAGES
77     */
78    private int numMessages;
79    /**
80     * @see UIProvider.ConversationColumns#NUM_DRAFTS
81     */
82    private int numDrafts;
83    /**
84     * @see UIProvider.ConversationColumns#SENDING_STATE
85     */
86    public int sendingState;
87    /**
88     * @see UIProvider.ConversationColumns#PRIORITY
89     */
90    public int priority;
91    /**
92     * @see UIProvider.ConversationColumns#READ
93     */
94    public boolean read;
95    /**
96     * @see UIProvider.ConversationColumns#STARRED
97     */
98    public boolean starred;
99    /**
100     * @see UIProvider.ConversationColumns#RAW_FOLDERS
101     */
102    private String rawFolders;
103    /**
104     * @see UIProvider.ConversationColumns#FLAGS
105     */
106    public int convFlags;
107    /**
108     * @see UIProvider.ConversationColumns#PERSONAL_LEVEL
109     */
110    public int personalLevel;
111    /**
112     * @see UIProvider.ConversationColumns#SPAM
113     */
114    public boolean spam;
115    /**
116     * @see UIProvider.ConversationColumns#MUTED
117     */
118    public boolean muted;
119    /**
120     * @see UIProvider.ConversationColumns#PHISHING
121     */
122    public boolean phishing;
123    /**
124     * @see UIProvider.ConversationColumns#COLOR
125     */
126    public int color;
127    /**
128     * @see UIProvider.ConversationColumns#ACCOUNT_URI
129     */
130    public Uri accountUri;
131    /**
132     * @see UIProvider.ConversationColumns#CONVERSATION_INFO
133     */
134    public ConversationInfo conversationInfo;
135    /**
136     * @see UIProvider.ConversationColumns#CONVERSATION_BASE_URI
137     */
138    public Uri conversationBaseUri;
139    /**
140     * @see UIProvider.ConversationColumns#REMOTE
141     */
142    public boolean isRemote;
143
144    // Used within the UI to indicate the adapter position of this conversation
145    public transient int position;
146    // Used within the UI to indicate that a Conversation should be removed from
147    // the ConversationCursor when executing an update, e.g. the the
148    // Conversation is no longer in the ConversationList for the current folder,
149    // that is it's now in some other folder(s)
150    public transient boolean localDeleteOnUpdate;
151
152    private transient boolean viewed;
153
154    private ArrayList<Folder> cachedRawFolders;
155    private ArrayList<Folder> cachedDisplayableFolders;
156
157    private static String sSendersDelimeter;
158
159    private static String sSubjectAndSnippet;
160
161    // Constituents of convFlags below
162    // Flag indicating that the item has been deleted, but will continue being
163    // shown in the list Delete/Archive of a mostly-dead item will NOT propagate
164    // the delete/archive, but WILL remove the item from the cursor
165    public static final int FLAG_MOSTLY_DEAD = 1 << 0;
166
167    /** An immutable, empty conversation list */
168    public static final Collection<Conversation> EMPTY = Collections.emptyList();
169
170    @Override
171    public int describeContents() {
172        return 0;
173    }
174
175    @Override
176    public void writeToParcel(Parcel dest, int flags) {
177        dest.writeLong(id);
178        dest.writeParcelable(uri, flags);
179        dest.writeString(subject);
180        dest.writeLong(dateMs);
181        dest.writeString(snippet);
182        dest.writeInt(hasAttachments ? 1 : 0);
183        dest.writeParcelable(messageListUri, 0);
184        dest.writeString(senders);
185        dest.writeInt(numMessages);
186        dest.writeInt(numDrafts);
187        dest.writeInt(sendingState);
188        dest.writeInt(priority);
189        dest.writeInt(read ? 1 : 0);
190        dest.writeInt(starred ? 1 : 0);
191        dest.writeString(rawFolders);
192        dest.writeInt(convFlags);
193        dest.writeInt(personalLevel);
194        dest.writeInt(spam ? 1 : 0);
195        dest.writeInt(phishing ? 1 : 0);
196        dest.writeInt(muted ? 1 : 0);
197        dest.writeInt(color);
198        dest.writeParcelable(accountUri, 0);
199        dest.writeString(ConversationInfo.toString(conversationInfo));
200        dest.writeParcelable(conversationBaseUri, 0);
201        dest.writeInt(isRemote ? 1 : 0);
202    }
203
204    private Conversation(Parcel in) {
205        id = in.readLong();
206        uri = in.readParcelable(null);
207        subject = in.readString();
208        dateMs = in.readLong();
209        snippet = in.readString();
210        hasAttachments = (in.readInt() != 0);
211        messageListUri = in.readParcelable(null);
212        senders = emptyIfNull(in.readString());
213        numMessages = in.readInt();
214        numDrafts = in.readInt();
215        sendingState = in.readInt();
216        priority = in.readInt();
217        read = (in.readInt() != 0);
218        starred = (in.readInt() != 0);
219        rawFolders = in.readString();
220        convFlags = in.readInt();
221        personalLevel = in.readInt();
222        spam = in.readInt() != 0;
223        phishing = in.readInt() != 0;
224        muted = in.readInt() != 0;
225        color = in.readInt();
226        accountUri = in.readParcelable(null);
227        position = NO_POSITION;
228        localDeleteOnUpdate = false;
229        conversationInfo = ConversationInfo.fromString(in.readString());
230        conversationBaseUri = in.readParcelable(null);
231        isRemote = in.readInt() != 0;
232    }
233
234    @Override
235    public String toString() {
236        return "[conversation id=" + id + ", subject =" + subject + "]";
237    }
238
239    public static final Creator<Conversation> CREATOR = new Creator<Conversation>() {
240
241        @Override
242        public Conversation createFromParcel(Parcel source) {
243            return new Conversation(source);
244        }
245
246        @Override
247        public Conversation[] newArray(int size) {
248            return new Conversation[size];
249        }
250
251    };
252
253    public static final Uri MOVE_CONVERSATIONS_URI = Uri.parse("content://moveconversations");
254
255    /**
256     * The column that needs to be updated to change the folders for a conversation.
257     */
258    public static final String UPDATE_FOLDER_COLUMN = ConversationColumns.RAW_FOLDERS;
259
260    public Conversation(Cursor cursor) {
261        if (cursor != null) {
262            id = cursor.getLong(UIProvider.CONVERSATION_ID_COLUMN);
263            uri = Uri.parse(cursor.getString(UIProvider.CONVERSATION_URI_COLUMN));
264            dateMs = cursor.getLong(UIProvider.CONVERSATION_DATE_RECEIVED_MS_COLUMN);
265            subject = cursor.getString(UIProvider.CONVERSATION_SUBJECT_COLUMN);
266            // Don't allow null subject
267            if (subject == null) {
268                subject = "";
269            }
270            hasAttachments = cursor.getInt(UIProvider.CONVERSATION_HAS_ATTACHMENTS_COLUMN) != 0;
271            String messageList = cursor.getString(UIProvider.CONVERSATION_MESSAGE_LIST_URI_COLUMN);
272            messageListUri = !TextUtils.isEmpty(messageList) ? Uri.parse(messageList) : null;
273            sendingState = cursor.getInt(UIProvider.CONVERSATION_SENDING_STATE_COLUMN);
274            priority = cursor.getInt(UIProvider.CONVERSATION_PRIORITY_COLUMN);
275            read = cursor.getInt(UIProvider.CONVERSATION_READ_COLUMN) != 0;
276            starred = cursor.getInt(UIProvider.CONVERSATION_STARRED_COLUMN) != 0;
277            rawFolders = cursor.getString(UIProvider.CONVERSATION_RAW_FOLDERS_COLUMN);
278            convFlags = cursor.getInt(UIProvider.CONVERSATION_FLAGS_COLUMN);
279            personalLevel = cursor.getInt(UIProvider.CONVERSATION_PERSONAL_LEVEL_COLUMN);
280            spam = cursor.getInt(UIProvider.CONVERSATION_IS_SPAM_COLUMN) != 0;
281            phishing = cursor.getInt(UIProvider.CONVERSATION_IS_PHISHING_COLUMN) != 0;
282            muted = cursor.getInt(UIProvider.CONVERSATION_MUTED_COLUMN) != 0;
283            color = cursor.getInt(UIProvider.CONVERSATION_COLOR_COLUMN);
284            String account = cursor.getString(UIProvider.CONVERSATION_ACCOUNT_URI_COLUMN);
285            accountUri = !TextUtils.isEmpty(account) ? Uri.parse(account) : null;
286            position = NO_POSITION;
287            localDeleteOnUpdate = false;
288            conversationInfo = ConversationInfo.fromString(cursor
289                    .getString(UIProvider.CONVERSATION_INFO_COLUMN));
290            final String conversationBase =
291                    cursor.getString(UIProvider.CONVERSATION_BASE_URI_COLUMN);
292            conversationBaseUri = !TextUtils.isEmpty(conversationBase) ?
293                    Uri.parse(conversationBase) : null;
294            if (conversationInfo == null) {
295                snippet = cursor.getString(UIProvider.CONVERSATION_SNIPPET_COLUMN);
296                senders = emptyIfNull(cursor.getString(UIProvider.CONVERSATION_SENDER_INFO_COLUMN));
297                numMessages = cursor.getInt(UIProvider.CONVERSATION_NUM_MESSAGES_COLUMN);
298                numDrafts = cursor.getInt(UIProvider.CONVERSATION_NUM_DRAFTS_COLUMN);
299            }
300            isRemote = cursor.getInt(UIProvider.CONVERSATION_REMOTE_COLUMN) != 0;
301        }
302    }
303
304    public Conversation() {
305    }
306
307    public static Conversation create(long id, Uri uri, String subject, long dateMs,
308            String snippet, boolean hasAttachment, Uri messageListUri, String senders,
309            int numMessages, int numDrafts, int sendingState, int priority, boolean read,
310            boolean starred, String rawFolders, int convFlags, int personalLevel, boolean spam,
311            boolean phishing, boolean muted, Uri accountUri, ConversationInfo conversationInfo,
312            Uri conversationBase, boolean isRemote) {
313
314        final Conversation conversation = new Conversation();
315
316        conversation.id = id;
317        conversation.uri = uri;
318        conversation.subject = subject;
319        conversation.dateMs = dateMs;
320        conversation.snippet = snippet;
321        conversation.hasAttachments = hasAttachment;
322        conversation.messageListUri = messageListUri;
323        conversation.senders = emptyIfNull(senders);
324        conversation.numMessages = numMessages;
325        conversation.numDrafts = numDrafts;
326        conversation.sendingState = sendingState;
327        conversation.priority = priority;
328        conversation.read = read;
329        conversation.starred = starred;
330        conversation.rawFolders = rawFolders;
331        conversation.convFlags = convFlags;
332        conversation.personalLevel = personalLevel;
333        conversation.spam = spam;
334        conversation.phishing = phishing;
335        conversation.muted = muted;
336        conversation.color = 0;
337        conversation.accountUri = accountUri;
338        conversation.conversationInfo = conversationInfo;
339        conversation.conversationBaseUri = conversationBase;
340        conversation.isRemote = isRemote;
341        return conversation;
342    }
343
344    public ArrayList<Folder> getRawFolders() {
345        if (cachedRawFolders == null) {
346            // Create cached folders.
347            if (!TextUtils.isEmpty(rawFolders)) {
348                cachedRawFolders = Folder.getFoldersArray(rawFolders);
349            } else {
350                return new ArrayList<Folder>();
351            }
352        }
353        return cachedRawFolders;
354    }
355
356    public void setRawFolders(String raw) {
357        clearCachedFolders();
358        rawFolders = raw;
359    }
360
361    public String getRawFoldersString() {
362        return rawFolders;
363    }
364
365    private void clearCachedFolders() {
366        cachedRawFolders = null;
367        cachedDisplayableFolders = null;
368    }
369
370    public ArrayList<Folder> getRawFoldersForDisplay(Folder ignoreFolder) {
371        ArrayList<Folder> folders = getRawFolders();
372        if (cachedDisplayableFolders == null) {
373            cachedDisplayableFolders = new ArrayList<Folder>();
374            for (Folder folder : folders) {
375                // skip the ignoreFolder
376                if (ignoreFolder != null && ignoreFolder.equals(folder)) {
377                    continue;
378                }
379                cachedDisplayableFolders.add(folder);
380            }
381        }
382        return cachedDisplayableFolders;
383    }
384
385    @Override
386    public boolean equals(Object o) {
387        if (o instanceof Conversation) {
388            Conversation conv = (Conversation) o;
389            return conv.uri.equals(uri);
390        }
391        return false;
392    }
393
394    @Override
395    public int hashCode() {
396        return uri.hashCode();
397    }
398
399    /**
400     * Get if this conversation is marked as high priority.
401     */
402    public boolean isImportant() {
403        return priority == UIProvider.ConversationPriority.IMPORTANT;
404    }
405
406    /**
407     * Get if this conversation is mostly dead
408     */
409    public boolean isMostlyDead() {
410        return (convFlags & FLAG_MOSTLY_DEAD) != 0;
411    }
412
413    /**
414     * Returns true if the URI of the conversation specified as the needle was
415     * found in the collection of conversations specified as the haystack. False
416     * otherwise. This method is safe to call with null arguments.
417     *
418     * @param haystack
419     * @param needle
420     * @return true if the needle was found in the haystack, false otherwise.
421     */
422    public final static boolean contains(Collection<Conversation> haystack, Conversation needle) {
423        // If the haystack is empty, it cannot contain anything.
424        if (haystack == null || haystack.size() <= 0) {
425            return false;
426        }
427        // The null folder exists everywhere.
428        if (needle == null) {
429            return true;
430        }
431        final long toFind = needle.id;
432        for (final Conversation c : haystack) {
433            if (toFind == c.id) {
434                return true;
435            }
436        }
437        return false;
438    }
439
440    /**
441     * Returns a collection of a single conversation. This method always returns
442     * a valid collection even if the input conversation is null.
443     *
444     * @param in a conversation, possibly null.
445     * @return a collection of the conversation.
446     */
447    public static Collection<Conversation> listOf(Conversation in) {
448        final Collection<Conversation> target = (in == null) ? EMPTY : ImmutableList.of(in);
449        return target;
450    }
451
452    /**
453     * Get the snippet for this conversation. Masks that it may come from
454     * conversation info or the original deprecated snippet string.
455     */
456    public String getSnippet() {
457        return conversationInfo != null && !TextUtils.isEmpty(conversationInfo.firstSnippet) ?
458                conversationInfo.firstSnippet : snippet;
459    }
460
461    public String getSenders(Context context) {
462        if (conversationInfo != null) {
463            ArrayList<String> senders = new ArrayList<String>();
464            for (MessageInfo m : this.conversationInfo.messageInfos) {
465                senders.add(m.sender);
466            }
467            return TextUtils.join(getSendersDelimeter(context), senders);
468        } else {
469            return senders;
470        }
471    }
472
473    private String getSendersDelimeter(Context context) {
474        if (sSendersDelimeter == null) {
475            sSendersDelimeter = context.getResources().getString(R.string.senders_split_token);
476        }
477        return sSendersDelimeter;
478    }
479
480    /**
481     * Get the number of messages for this conversation.
482     */
483    public int getNumMessages() {
484        return conversationInfo != null ? conversationInfo.messageCount : numMessages;
485    }
486
487    /**
488     * Get the number of drafts for this conversation.
489     */
490    public int numDrafts() {
491        return conversationInfo != null ? conversationInfo.draftCount : numDrafts;
492    }
493
494    public boolean isViewed() {
495        return viewed;
496    }
497
498    public void markViewed() {
499        viewed = true;
500    }
501
502    /**
503     * Create a human-readable string of all the conversations
504     * @param collection Any collection of conversations
505     * @return string with a human readable representation of the conversations.
506     */
507    public static String toString(Collection<Conversation> collection) {
508        final StringBuilder out = new StringBuilder(collection.size() + " conversations:");
509        int count = 0;
510        for (final Conversation c : collection) {
511            count++;
512            // Indent the conversations to make them easy to read in debug
513            // output.
514            out.append("      " + count + ": " + c.toString() + "\n");
515        }
516        return out.toString();
517    }
518
519    /**
520     * Returns an empty string if the specified string is null
521     */
522    private static String emptyIfNull(String in) {
523        return in != null ? in : EMPTY_STRING;
524    }
525
526    /**
527     * Get the properly formatted subject and snippet string for display a conversation.
528     */
529    public static SpannableStringBuilder getSubjectAndSnippetForDisplay(Context context,
530            String filteredSubject, String snippet) {
531        if (sSubjectAndSnippet == null) {
532            sSubjectAndSnippet = context.getString(R.string.subject_and_snippet);
533        }
534        return new SpannableStringBuilder((!TextUtils.isEmpty(snippet)) ?
535                String.format(sSubjectAndSnippet, filteredSubject, snippet)
536                : filteredSubject);
537    }
538}
539