summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/providers/Conversation.java
blob: 1b0cb0ef801b7b456caa27669d908f33e60e7406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/**
 * Copyright (c) 2012, Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mail.providers;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;

import com.android.mail.R;
import com.android.mail.providers.UIProvider.ConversationColumns;
import com.google.common.collect.ImmutableList;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Conversation implements Parcelable {
    public static final int NO_POSITION = -1;

    private static final String EMPTY_STRING = "";

    /**
     * @see BaseColumns#_ID
     */
    public long id;
    /**
     * @see UIProvider.ConversationColumns#URI
     */
    public Uri uri;
    /**
     * @see UIProvider.ConversationColumns#SUBJECT
     */
    public String subject;
    /**
     * @see UIProvider.ConversationColumns#DATE_RECEIVED_MS
     */
    public long dateMs;
    /**
     * @see UIProvider.ConversationColumns#SNIPPET
     */
    @Deprecated
    public String snippet;
    /**
     * @see UIProvider.ConversationColumns#HAS_ATTACHMENTS
     */
    public boolean hasAttachments;
    /**
     * @see UIProvider.ConversationColumns#MESSAGE_LIST_URI
     */
    public Uri messageListUri;
    /**
     * @see UIProvider.ConversationColumns#SENDER_INFO
     */
    @Deprecated
    public String senders;
    /**
     * @see UIProvider.ConversationColumns#NUM_MESSAGES
     */
    private int numMessages;
    /**
     * @see UIProvider.ConversationColumns#NUM_DRAFTS
     */
    private int numDrafts;
    /**
     * @see UIProvider.ConversationColumns#SENDING_STATE
     */
    public int sendingState;
    /**
     * @see UIProvider.ConversationColumns#PRIORITY
     */
    public int priority;
    /**
     * @see UIProvider.ConversationColumns#READ
     */
    public boolean read;
    /**
     * @see UIProvider.ConversationColumns#STARRED
     */
    public boolean starred;
    /**
     * @see UIProvider.ConversationColumns#RAW_FOLDERS
     */
    private FolderList rawFolders;
    /**
     * @see UIProvider.ConversationColumns#FLAGS
     */
    public int convFlags;
    /**
     * @see UIProvider.ConversationColumns#PERSONAL_LEVEL
     */
    public int personalLevel;
    /**
     * @see UIProvider.ConversationColumns#SPAM
     */
    public boolean spam;
    /**
     * @see UIProvider.ConversationColumns#MUTED
     */
    public boolean muted;
    /**
     * @see UIProvider.ConversationColumns#PHISHING
     */
    public boolean phishing;
    /**
     * @see UIProvider.ConversationColumns#COLOR
     */
    public int color;
    /**
     * @see UIProvider.ConversationColumns#ACCOUNT_URI
     */
    public Uri accountUri;
    /**
     * @see UIProvider.ConversationColumns#CONVERSATION_INFO
     */
    public ConversationInfo conversationInfo;
    /**
     * @see UIProvider.ConversationColumns#CONVERSATION_BASE_URI
     */
    public Uri conversationBaseUri;
    /**
     * @see UIProvider.ConversationColumns#REMOTE
     */
    public boolean isRemote;

    // Used within the UI to indicate the adapter position of this conversation
    public transient int position;
    // Used within the UI to indicate that a Conversation should be removed from
    // the ConversationCursor when executing an update, e.g. the the
    // Conversation is no longer in the ConversationList for the current folder,
    // that is it's now in some other folder(s)
    public transient boolean localDeleteOnUpdate;

    private transient boolean viewed;

    private ArrayList<Folder> cachedDisplayableFolders;

    private static String sSendersDelimeter;

    private static String sSubjectAndSnippet;

    // Constituents of convFlags below
    // Flag indicating that the item has been deleted, but will continue being
    // shown in the list Delete/Archive of a mostly-dead item will NOT propagate
    // the delete/archive, but WILL remove the item from the cursor
    public static final int FLAG_MOSTLY_DEAD = 1 << 0;

    /** An immutable, empty conversation list */
    public static final Collection<Conversation> EMPTY = Collections.emptyList();

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeParcelable(uri, flags);
        dest.writeString(subject);
        dest.writeLong(dateMs);
        dest.writeString(snippet);
        dest.writeInt(hasAttachments ? 1 : 0);
        dest.writeParcelable(messageListUri, 0);
        dest.writeString(senders);
        dest.writeInt(numMessages);
        dest.writeInt(numDrafts);
        dest.writeInt(sendingState);
        dest.writeInt(priority);
        dest.writeInt(read ? 1 : 0);
        dest.writeInt(starred ? 1 : 0);
        dest.writeParcelable(rawFolders, 0);
        dest.writeInt(convFlags);
        dest.writeInt(personalLevel);
        dest.writeInt(spam ? 1 : 0);
        dest.writeInt(phishing ? 1 : 0);
        dest.writeInt(muted ? 1 : 0);
        dest.writeInt(color);
        dest.writeParcelable(accountUri, 0);
        dest.writeParcelable(conversationInfo, 0);
        dest.writeParcelable(conversationBaseUri, 0);
        dest.writeInt(isRemote ? 1 : 0);
    }

    private Conversation(Parcel in, ClassLoader loader) {
        id = in.readLong();
        uri = in.readParcelable(null);
        subject = in.readString();
        dateMs = in.readLong();
        snippet = in.readString();
        hasAttachments = (in.readInt() != 0);
        messageListUri = in.readParcelable(null);
        senders = emptyIfNull(in.readString());
        numMessages = in.readInt();
        numDrafts = in.readInt();
        sendingState = in.readInt();
        priority = in.readInt();
        read = (in.readInt() != 0);
        starred = (in.readInt() != 0);
        rawFolders = in.readParcelable(loader);
        convFlags = in.readInt();
        personalLevel = in.readInt();
        spam = in.readInt() != 0;
        phishing = in.readInt() != 0;
        muted = in.readInt() != 0;
        color = in.readInt();
        accountUri = in.readParcelable(null);
        position = NO_POSITION;
        localDeleteOnUpdate = false;
        conversationInfo = in.readParcelable(loader);
        conversationBaseUri = in.readParcelable(null);
        isRemote = in.readInt() != 0;
    }

    @Override
    public String toString() {
        return "[conversation id=" + id + ", subject =" + subject + "]";
    }

    public static final ClassLoaderCreator<Conversation> CREATOR =
            new ClassLoaderCreator<Conversation>() {

        @Override
        public Conversation createFromParcel(Parcel source) {
            return new Conversation(source, null);
        }

        @Override
        public Conversation createFromParcel(Parcel source, ClassLoader loader) {
            return new Conversation(source, loader);
        }

        @Override
        public Conversation[] newArray(int size) {
            return new Conversation[size];
        }

    };

    public static final Uri MOVE_CONVERSATIONS_URI = Uri.parse("content://moveconversations");

    /**
     * The column that needs to be updated to change the folders for a conversation.
     */
    public static final String UPDATE_FOLDER_COLUMN = ConversationColumns.RAW_FOLDERS;

    public Conversation(Cursor cursor) {
        if (cursor != null) {
            id = cursor.getLong(UIProvider.CONVERSATION_ID_COLUMN);
            uri = Uri.parse(cursor.getString(UIProvider.CONVERSATION_URI_COLUMN));
            dateMs = cursor.getLong(UIProvider.CONVERSATION_DATE_RECEIVED_MS_COLUMN);
            subject = cursor.getString(UIProvider.CONVERSATION_SUBJECT_COLUMN);
            // Don't allow null subject
            if (subject == null) {
                subject = "";
            }
            hasAttachments = cursor.getInt(UIProvider.CONVERSATION_HAS_ATTACHMENTS_COLUMN) != 0;
            String messageList = cursor.getString(UIProvider.CONVERSATION_MESSAGE_LIST_URI_COLUMN);
            messageListUri = !TextUtils.isEmpty(messageList) ? Uri.parse(messageList) : null;
            sendingState = cursor.getInt(UIProvider.CONVERSATION_SENDING_STATE_COLUMN);
            priority = cursor.getInt(UIProvider.CONVERSATION_PRIORITY_COLUMN);
            read = cursor.getInt(UIProvider.CONVERSATION_READ_COLUMN) != 0;
            starred = cursor.getInt(UIProvider.CONVERSATION_STARRED_COLUMN) != 0;
            rawFolders = FolderList.fromBlob(
                    cursor.getBlob(UIProvider.CONVERSATION_RAW_FOLDERS_COLUMN));
            convFlags = cursor.getInt(UIProvider.CONVERSATION_FLAGS_COLUMN);
            personalLevel = cursor.getInt(UIProvider.CONVERSATION_PERSONAL_LEVEL_COLUMN);
            spam = cursor.getInt(UIProvider.CONVERSATION_IS_SPAM_COLUMN) != 0;
            phishing = cursor.getInt(UIProvider.CONVERSATION_IS_PHISHING_COLUMN) != 0;
            muted = cursor.getInt(UIProvider.CONVERSATION_MUTED_COLUMN) != 0;
            color = cursor.getInt(UIProvider.CONVERSATION_COLOR_COLUMN);
            String account = cursor.getString(UIProvider.CONVERSATION_ACCOUNT_URI_COLUMN);
            accountUri = !TextUtils.isEmpty(account) ? Uri.parse(account) : null;
            position = NO_POSITION;
            localDeleteOnUpdate = false;
            conversationInfo = ConversationInfo.fromBlob(
                    cursor.getBlob(UIProvider.CONVERSATION_INFO_COLUMN));
            final String conversationBase =
                    cursor.getString(UIProvider.CONVERSATION_BASE_URI_COLUMN);
            conversationBaseUri = !TextUtils.isEmpty(conversationBase) ?
                    Uri.parse(conversationBase) : null;
            if (conversationInfo == null) {
                snippet = cursor.getString(UIProvider.CONVERSATION_SNIPPET_COLUMN);
                senders = emptyIfNull(cursor.getString(UIProvider.CONVERSATION_SENDER_INFO_COLUMN));
                numMessages = cursor.getInt(UIProvider.CONVERSATION_NUM_MESSAGES_COLUMN);
                numDrafts = cursor.getInt(UIProvider.CONVERSATION_NUM_DRAFTS_COLUMN);
            }
            isRemote = cursor.getInt(UIProvider.CONVERSATION_REMOTE_COLUMN) != 0;
        }
    }

    public Conversation() {
    }

    public static Conversation create(long id, Uri uri, String subject, long dateMs,
            String snippet, boolean hasAttachment, Uri messageListUri, String senders,
            int numMessages, int numDrafts, int sendingState, int priority, boolean read,
            boolean starred, FolderList rawFolders, int convFlags, int personalLevel, boolean spam,
            boolean phishing, boolean muted, Uri accountUri, ConversationInfo conversationInfo,
            Uri conversationBase, boolean isRemote) {

        final Conversation conversation = new Conversation();

        conversation.id = id;
        conversation.uri = uri;
        conversation.subject = subject;
        conversation.dateMs = dateMs;
        conversation.snippet = snippet;
        conversation.hasAttachments = hasAttachment;
        conversation.messageListUri = messageListUri;
        conversation.senders = emptyIfNull(senders);
        conversation.numMessages = numMessages;
        conversation.numDrafts = numDrafts;
        conversation.sendingState = sendingState;
        conversation.priority = priority;
        conversation.read = read;
        conversation.starred = starred;
        conversation.rawFolders = rawFolders;
        conversation.convFlags = convFlags;
        conversation.personalLevel = personalLevel;
        conversation.spam = spam;
        conversation.phishing = phishing;
        conversation.muted = muted;
        conversation.color = 0;
        conversation.accountUri = accountUri;
        conversation.conversationInfo = conversationInfo;
        conversation.conversationBaseUri = conversationBase;
        conversation.isRemote = isRemote;
        return conversation;
    }

    /**
     * Get the <strong>immutable</strong> list of {@link Folder}s for this conversation. To modify
     * this list, make a new {@link FolderList} and use {@link #setRawFolders(FolderList)}.
     *
     * @return <strong>Immutable</strong> list of {@link Folder}s.
     */
    public List<Folder> getRawFolders() {
        return rawFolders.folders;
    }

    public void setRawFolders(FolderList folders) {
        clearCachedFolders();
        rawFolders = folders;
    }

    private void clearCachedFolders() {
        cachedDisplayableFolders = null;
    }

    public ArrayList<Folder> getRawFoldersForDisplay(Folder ignoreFolder) {
        if (cachedDisplayableFolders == null) {
            cachedDisplayableFolders = new ArrayList<Folder>();
            for (Folder folder : rawFolders.folders) {
                // skip the ignoreFolder
                if (ignoreFolder != null && ignoreFolder.equals(folder)) {
                    continue;
                }
                cachedDisplayableFolders.add(folder);
            }
        }
        return cachedDisplayableFolders;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Conversation) {
            Conversation conv = (Conversation) o;
            return conv.uri.equals(uri);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return uri.hashCode();
    }

    /**
     * Get if this conversation is marked as high priority.
     */
    public boolean isImportant() {
        return priority == UIProvider.ConversationPriority.IMPORTANT;
    }

    /**
     * Get if this conversation is mostly dead
     */
    public boolean isMostlyDead() {
        return (convFlags & FLAG_MOSTLY_DEAD) != 0;
    }

    /**
     * Returns true if the URI of the conversation specified as the needle was
     * found in the collection of conversations specified as the haystack. False
     * otherwise. This method is safe to call with null arguments.
     *
     * @param haystack
     * @param needle
     * @return true if the needle was found in the haystack, false otherwise.
     */
    public final static boolean contains(Collection<Conversation> haystack, Conversation needle) {
        // If the haystack is empty, it cannot contain anything.
        if (haystack == null || haystack.size() <= 0) {
            return false;
        }
        // The null folder exists everywhere.
        if (needle == null) {
            return true;
        }
        final long toFind = needle.id;
        for (final Conversation c : haystack) {
            if (toFind == c.id) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns a collection of a single conversation. This method always returns
     * a valid collection even if the input conversation is null.
     *
     * @param in a conversation, possibly null.
     * @return a collection of the conversation.
     */
    public static Collection<Conversation> listOf(Conversation in) {
        final Collection<Conversation> target = (in == null) ? EMPTY : ImmutableList.of(in);
        return target;
    }

    /**
     * Get the snippet for this conversation. Masks that it may come from
     * conversation info or the original deprecated snippet string.
     */
    public String getSnippet() {
        return conversationInfo != null && !TextUtils.isEmpty(conversationInfo.firstSnippet) ?
                conversationInfo.firstSnippet : snippet;
    }

    public String getSenders(Context context) {
        if (conversationInfo != null) {
            ArrayList<String> senders = new ArrayList<String>();
            for (MessageInfo m : this.conversationInfo.messageInfos) {
                senders.add(m.sender);
            }
            return TextUtils.join(getSendersDelimeter(context), senders);
        } else {
            return senders;
        }
    }

    private String getSendersDelimeter(Context context) {
        if (sSendersDelimeter == null) {
            sSendersDelimeter = context.getResources().getString(R.string.senders_split_token);
        }
        return sSendersDelimeter;
    }

    /**
     * Get the number of messages for this conversation.
     */
    public int getNumMessages() {
        return conversationInfo != null ? conversationInfo.messageCount : numMessages;
    }

    /**
     * Get the number of drafts for this conversation.
     */
    public int numDrafts() {
        return conversationInfo != null ? conversationInfo.draftCount : numDrafts;
    }

    public boolean isViewed() {
        return viewed;
    }

    public void markViewed() {
        viewed = true;
    }

    /**
     * Create a human-readable string of all the conversations
     * @param collection Any collection of conversations
     * @return string with a human readable representation of the conversations.
     */
    public static String toString(Collection<Conversation> collection) {
        final StringBuilder out = new StringBuilder(collection.size() + " conversations:");
        int count = 0;
        for (final Conversation c : collection) {
            count++;
            // Indent the conversations to make them easy to read in debug
            // output.
            out.append("      " + count + ": " + c.toString() + "\n");
        }
        return out.toString();
    }

    /**
     * Returns an empty string if the specified string is null
     */
    private static String emptyIfNull(String in) {
        return in != null ? in : EMPTY_STRING;
    }

    /**
     * Get the properly formatted subject and snippet string for display a
     * conversation.
     *
     * @param context
     * @param filteredSubject
     * @param snippet
     * @param maxChars Supply max characters the returned string can have, or -1
     *            if there is no limit
     */
    public static SpannableStringBuilder getSubjectAndSnippetForDisplay(Context context,
            String filteredSubject, String snippet, int maxChars) {
        if (sSubjectAndSnippet == null) {
            sSubjectAndSnippet = context.getString(R.string.subject_and_snippet);
        }
        String subjectText = (!TextUtils.isEmpty(snippet)) ?
                String.format(sSubjectAndSnippet, filteredSubject, snippet)
                : filteredSubject;
        SpannableStringBuilder builder = new SpannableStringBuilder();
        if (maxChars != -1 && maxChars < subjectText.length()) {
            builder.append(subjectText, 0, maxChars);
        } else {
            builder.append(subjectText);
        }
        return builder;
    }
}