Conversation.java revision 25b939e5a7ecb1e0879b684dc5bc55183cf468b4
1package com.android.mms.data;
2
3import java.util.HashSet;
4import java.util.Iterator;
5import java.util.Set;
6
7import android.content.AsyncQueryHandler;
8import android.content.ContentUris;
9import android.content.ContentValues;
10import android.content.Context;
11import android.database.Cursor;
12import android.net.Uri;
13import android.provider.Telephony.MmsSms;
14import android.provider.Telephony.Threads;
15import android.provider.Telephony.Sms.Conversations;
16import android.text.TextUtils;
17import android.util.Log;
18
19import com.android.mms.R;
20import com.android.mms.LogTag;
21import com.android.mms.transaction.MessagingNotification;
22import com.android.mms.ui.MessageUtils;
23import com.android.mms.util.DraftCache;
24
25/**
26 * An interface for finding information about conversations and/or creating new ones.
27 */
28public class Conversation {
29    private static final String TAG = "Mms/conv";
30    private static final boolean DEBUG = false;
31
32    private static final Uri sAllThreadsUri =
33        Threads.CONTENT_URI.buildUpon().appendQueryParameter("simple", "true").build();
34
35    private static final String[] ALL_THREADS_PROJECTION = {
36        Threads._ID, Threads.DATE, Threads.MESSAGE_COUNT, Threads.RECIPIENT_IDS,
37        Threads.SNIPPET, Threads.SNIPPET_CHARSET, Threads.READ, Threads.ERROR,
38        Threads.HAS_ATTACHMENT
39    };
40    private static final int ID             = 0;
41    private static final int DATE           = 1;
42    private static final int MESSAGE_COUNT  = 2;
43    private static final int RECIPIENT_IDS  = 3;
44    private static final int SNIPPET        = 4;
45    private static final int SNIPPET_CS     = 5;
46    private static final int READ           = 6;
47    private static final int ERROR          = 7;
48    private static final int HAS_ATTACHMENT = 8;
49
50
51    private final Context mContext;
52
53    // The thread ID of this conversation.  Can be zero in the case of a
54    // new conversation where the recipient set is changing as the user
55    // types and we have not hit the database yet to create a thread.
56    private long mThreadId;
57
58    private ContactList mRecipients;    // The current set of recipients.
59    private long mDate;                 // The last update time.
60    private int mMessageCount;          // Number of messages.
61    private String mSnippet;            // Text of the most recent message.
62    private boolean mHasUnreadMessages; // True if there are unread messages.
63    private boolean mHasAttachment;     // True if any message has an attachment.
64    private boolean mHasError;          // True if any message is in an error state.
65
66    private static ContentValues mReadContentValues;
67
68
69    private Conversation(Context context) {
70        mContext = context;
71        mRecipients = new ContactList();
72        mThreadId = 0;
73    }
74
75    private Conversation(Context context, long threadId) {
76        mContext = context;
77        if (!loadFromThreadId(threadId)) {
78            mRecipients = new ContactList();
79            mThreadId = 0;
80        }
81    }
82
83    private Conversation(Context context, Cursor cursor, boolean allowQuery) {
84        mContext = context;
85        fillFromCursor(context, this, cursor, allowQuery);
86    }
87
88    /**
89     * Create a new conversation with no recipients.  {@link setRecipients} can
90     * be called as many times as you like; the conversation will not be
91     * created in the database until {@link ensureThreadId} is called.
92     */
93    public static Conversation createNew(Context context) {
94        return new Conversation(context);
95    }
96
97    /**
98     * Find the conversation matching the provided thread ID.
99     */
100    public static Conversation get(Context context, long threadId) {
101        synchronized (Cache.getInstance()) {
102            Conversation conv = Cache.get(threadId);
103            if (conv != null)
104                return conv;
105
106            conv = new Conversation(context, threadId);
107            try {
108                Cache.put(conv);
109            } catch (IllegalStateException e) {
110                LogTag.error("Tried to add duplicate Conversation to Cache");
111            }
112            return conv;
113        }
114    }
115
116    /**
117     * Find the conversation matching the provided recipient set.
118     * When called with an empty recipient list, equivalent to {@link createEmpty}.
119     */
120    public static Conversation get(Context context, ContactList recipients) {
121        // If there are no recipients in the list, make a new conversation.
122        if (recipients.size() < 1) {
123            return createNew(context);
124        }
125
126        synchronized (Cache.getInstance()) {
127            Conversation conv = Cache.get(recipients);
128            if (conv != null)
129                return conv;
130
131            long threadId = getOrCreateThreadId(context, recipients);
132            conv = new Conversation(context, threadId);
133
134            Cache.put(conv);
135            return conv;
136        }
137    }
138
139    /**
140     * Find the conversation matching in the specified Uri.  Example
141     * forms: {@value content://mms-sms/conversations/3} or
142     * {@value sms:+12124797990}.
143     * When called with a null Uri, equivalent to {@link createEmpty}.
144     */
145    public static Conversation get(Context context, Uri uri) {
146        if (uri == null) {
147            return createNew(context);
148        }
149
150        if (DEBUG) {
151            Log.v(TAG, "Conversation get URI: " + uri);
152        }
153        // Handle a conversation URI
154        if (uri.getPathSegments().size() >= 2) {
155            try {
156                long threadId = Long.parseLong(uri.getPathSegments().get(1));
157                if (DEBUG) {
158                    Log.v(TAG, "Conversation get threadId: " + threadId);
159                }
160                return get(context, threadId);
161            } catch (NumberFormatException exception) {
162                LogTag.error("Invalid URI: " + uri);
163            }
164        }
165
166        String recipient = uri.getSchemeSpecificPart();
167        return get(context, ContactList.getByNumbers(recipient, false));
168    }
169
170    /**
171     * Returns true if the recipient in the uri matches the recipient list in this
172     * conversation.
173     */
174    public boolean sameRecipient(Uri uri) {
175        int size = mRecipients.size();
176        if (size > 1) {
177            return false;
178        }
179        if (uri == null) {
180            return size == 0;
181        }
182        if (uri.getPathSegments().size() >= 2) {
183            return false;       // it's a thread id for a conversation
184        }
185        String recipient = uri.getSchemeSpecificPart();
186        ContactList incomingRecipient = ContactList.getByNumbers(recipient, false);
187        return mRecipients.equals(incomingRecipient);
188    }
189
190    /**
191     * Returns a temporary Conversation (not representing one on disk) wrapping
192     * the contents of the provided cursor.  The cursor should be the one
193     * returned to your AsyncQueryHandler passed in to {@link startQueryForAll}.
194     * The recipient list of this conversation can be empty if the results
195     * were not in cache.
196     */
197    // TODO: check why can't load a cached Conversation object here.
198    public static Conversation from(Context context, Cursor cursor) {
199        return new Conversation(context, cursor, false);
200    }
201
202    private void buildReadContentValues() {
203        if (mReadContentValues == null) {
204            mReadContentValues = new ContentValues(1);
205            mReadContentValues.put("read", 1);
206        }
207    }
208
209    /**
210     * Marks all messages in this conversation as read and updates
211     * relevant notifications.  This method returns immediately;
212     * work is dispatched to a background thread.
213     */
214    public synchronized void markAsRead() {
215        // If we have no Uri to mark (as in the case of a conversation that
216        // has not yet made its way to disk), there's nothing to do.
217        final Uri threadUri = getUri();
218
219        new Thread(new Runnable() {
220            public void run() {
221                if (threadUri != null) {
222                    buildReadContentValues();
223                    mContext.getContentResolver().update(threadUri, mReadContentValues,
224                            "read=0", null);
225                    mHasUnreadMessages = false;
226                }
227                // Always update notifications regardless of the read state.
228                MessagingNotification.updateAllNotifications(mContext);
229            }
230        }).start();
231    }
232
233    /**
234     * Returns a content:// URI referring to this conversation,
235     * or null if it does not exist on disk yet.
236     */
237    public synchronized Uri getUri() {
238        if (mThreadId <= 0)
239            return null;
240
241        return ContentUris.withAppendedId(Threads.CONTENT_URI, mThreadId);
242    }
243
244    /**
245     * Return the Uri for all messages in the given thread ID.
246     * @deprecated
247     */
248    public static Uri getUri(long threadId) {
249        // TODO: Callers using this should really just have a Conversation
250        // and call getUri() on it, but this guarantees no blocking.
251        return ContentUris.withAppendedId(Threads.CONTENT_URI, threadId);
252    }
253
254    /**
255     * Returns the thread ID of this conversation.  Can be zero if
256     * {@link ensureThreadId} has not been called yet.
257     */
258    public synchronized long getThreadId() {
259        return mThreadId;
260    }
261
262    /**
263     * Guarantees that the conversation has been created in the database.
264     * This will make a blocking database call if it hasn't.
265     *
266     * @return The thread ID of this conversation in the database
267     */
268    public synchronized long ensureThreadId() {
269        if (DEBUG) {
270            LogTag.debug("ensureThreadId before: " + mThreadId);
271        }
272        if (mThreadId <= 0) {
273            mThreadId = getOrCreateThreadId(mContext, mRecipients);
274        }
275        if (DEBUG) {
276            LogTag.debug("ensureThreadId after: " + mThreadId);
277        }
278
279        return mThreadId;
280    }
281
282    public synchronized void clearThreadId() {
283        // remove ourself from the cache
284        if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
285            LogTag.debug("clearThreadId old threadId was: " + mThreadId + " now zero");
286        }
287        Cache.remove(mThreadId);
288
289        mThreadId = 0;
290    }
291
292    /**
293     * Sets the list of recipients associated with this conversation.
294     * If called, {@link ensureThreadId} must be called before the next
295     * operation that depends on this conversation existing in the
296     * database (e.g. storing a draft message to it).
297     */
298    public synchronized void setRecipients(ContactList list) {
299        mRecipients = list;
300
301        // Invalidate thread ID because the recipient set has changed.
302        mThreadId = 0;
303    }
304
305    /**
306     * Returns the recipient set of this conversation.
307     */
308    public synchronized ContactList getRecipients() {
309        return mRecipients;
310    }
311
312    /**
313     * Returns true if a draft message exists in this conversation.
314     */
315    public synchronized boolean hasDraft() {
316        if (mThreadId <= 0)
317            return false;
318
319        return DraftCache.getInstance().hasDraft(mThreadId);
320    }
321
322    /**
323     * Sets whether or not this conversation has a draft message.
324     */
325    public synchronized void setDraftState(boolean hasDraft) {
326        if (mThreadId <= 0)
327            return;
328
329        DraftCache.getInstance().setDraftState(mThreadId, hasDraft);
330    }
331
332    /**
333     * Returns the time of the last update to this conversation in milliseconds,
334     * on the {@link System.currentTimeMillis} timebase.
335     */
336    public synchronized long getDate() {
337        return mDate;
338    }
339
340    /**
341     * Returns the number of messages in this conversation, excluding the draft
342     * (if it exists).
343     */
344    public synchronized int getMessageCount() {
345        return mMessageCount;
346    }
347
348    /**
349     * Returns a snippet of text from the most recent message in the conversation.
350     */
351    public synchronized String getSnippet() {
352        return mSnippet;
353    }
354
355    /**
356     * Returns true if there are any unread messages in the conversation.
357     */
358    public synchronized boolean hasUnreadMessages() {
359        return mHasUnreadMessages;
360    }
361
362    /**
363     * Returns true if any messages in the conversation have attachments.
364     */
365    public synchronized boolean hasAttachment() {
366        return mHasAttachment;
367    }
368
369    /**
370     * Returns true if any messages in the conversation are in an error state.
371     */
372    public synchronized boolean hasError() {
373        return mHasError;
374    }
375
376    private static long getOrCreateThreadId(Context context, ContactList list) {
377        HashSet<String> recipients = new HashSet<String>();
378        Contact cacheContact = null;
379        for (Contact c : list) {
380            cacheContact = Contact.get(c.getNumber(),true);
381            if (cacheContact != null) {
382                recipients.add(cacheContact.getNumber());
383            } else {
384                recipients.add(c.getNumber());
385            }
386        }
387        if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
388            LogTag.debug("getOrCreateThreadId %s", recipients);
389        }
390        return Threads.getOrCreateThreadId(context, recipients);
391    }
392
393    /*
394     * The primary key of a conversation is its recipient set; override
395     * equals() and hashCode() to just pass through to the internal
396     * recipient sets.
397     */
398    @Override
399    public synchronized boolean equals(Object obj) {
400        try {
401            Conversation other = (Conversation)obj;
402            return (mRecipients.equals(other.mRecipients));
403        } catch (ClassCastException e) {
404            return false;
405        }
406    }
407
408    @Override
409    public synchronized int hashCode() {
410        return mRecipients.hashCode();
411    }
412
413    @Override
414    public synchronized String toString() {
415        return String.format("[%s] (tid %d)", mRecipients.serialize(), mThreadId);
416    }
417
418    /**
419     * Remove any obsolete conversations sitting around on disk.
420     * @deprecated
421     */
422    public static void cleanup(Context context) {
423        // TODO: Get rid of this awful hack.
424        context.getContentResolver().delete(Threads.OBSOLETE_THREADS_URI, null, null);
425    }
426
427    /**
428     * Start a query for all conversations in the database on the specified
429     * AsyncQueryHandler.
430     *
431     * @param handler An AsyncQueryHandler that will receive onQueryComplete
432     *                upon completion of the query
433     * @param token   The token that will be passed to onQueryComplete
434     */
435    public static void startQueryForAll(AsyncQueryHandler handler, int token) {
436        handler.cancelOperation(token);
437        handler.startQuery(token, null, sAllThreadsUri,
438                ALL_THREADS_PROJECTION, null, null, Conversations.DEFAULT_SORT_ORDER);
439    }
440
441    /**
442     * Start a delete of the conversation with the specified thread ID.
443     *
444     * @param handler An AsyncQueryHandler that will receive onDeleteComplete
445     *                upon completion of the conversation being deleted
446     * @param token   The token that will be passed to onDeleteComplete
447     * @param deleteAll Delete the whole thread including locked messages
448     * @param threadId Thread ID of the conversation to be deleted
449     */
450    public static void startDelete(AsyncQueryHandler handler, int token, boolean deleteAll,
451            long threadId) {
452        Uri uri = ContentUris.withAppendedId(Threads.CONTENT_URI, threadId);
453        String selection = deleteAll ? null : "locked=0";
454        handler.startDelete(token, null, uri, selection, null);
455    }
456
457    /**
458     * Start deleting all conversations in the database.
459     * @param handler An AsyncQueryHandler that will receive onDeleteComplete
460     *                upon completion of all conversations being deleted
461     * @param token   The token that will be passed to onDeleteComplete
462     * @param deleteAll Delete the whole thread including locked messages
463     */
464    public static void startDeleteAll(AsyncQueryHandler handler, int token, boolean deleteAll) {
465        String selection = deleteAll ? null : "locked=0";
466        handler.startDelete(token, null, Threads.CONTENT_URI, selection, null);
467    }
468
469    /**
470     * Check for locked messages in all threads or a specified thread.
471     * @param handler An AsyncQueryHandler that will receive onQueryComplete
472     *                upon completion of looking for locked messages
473     * @param threadId   The threadId of the thread to search. -1 means all threads
474     * @param token   The token that will be passed to onQueryComplete
475     */
476    public static void startQueryHaveLockedMessages(AsyncQueryHandler handler, long threadId,
477            int token) {
478        handler.cancelOperation(token);
479        Uri uri = MmsSms.CONTENT_LOCKED_URI;
480        if (threadId != -1) {
481            uri = ContentUris.withAppendedId(uri, threadId);
482        }
483        handler.startQuery(token, new Long(threadId), uri,
484                ALL_THREADS_PROJECTION, null, null, Conversations.DEFAULT_SORT_ORDER);
485    }
486
487    /**
488     * Fill the specified conversation with the values from the specified
489     * cursor, possibly setting recipients to empty if {@value allowQuery}
490     * is false and the recipient IDs are not in cache.  The cursor should
491     * be one made via {@link startQueryForAll}.
492     */
493    private static void fillFromCursor(Context context, Conversation conv,
494                                       Cursor c, boolean allowQuery) {
495        synchronized (conv) {
496            conv.mThreadId = c.getLong(ID);
497            conv.mDate = c.getLong(DATE);
498            conv.mMessageCount = c.getInt(MESSAGE_COUNT);
499
500            // Replace the snippet with a default value if it's empty.
501            String snippet = MessageUtils.extractEncStrFromCursor(c, SNIPPET, SNIPPET_CS);
502            if (TextUtils.isEmpty(snippet)) {
503                snippet = context.getString(R.string.no_subject_view);
504            }
505            conv.mSnippet = snippet;
506
507            conv.mHasUnreadMessages = (c.getInt(READ) == 0);
508            conv.mHasError = (c.getInt(ERROR) != 0);
509            conv.mHasAttachment = (c.getInt(HAS_ATTACHMENT) != 0);
510
511            String recipientIds = c.getString(RECIPIENT_IDS);
512            conv.mRecipients = ContactList.getByIds(recipientIds, allowQuery);
513        }
514    }
515
516    /**
517     * Private cache for the use of the various forms of Conversation.get.
518     */
519    private static class Cache {
520        private static Cache sInstance = new Cache();
521        static Cache getInstance() { return sInstance; }
522        private final HashSet<Conversation> mCache;
523        private Cache() {
524            mCache = new HashSet<Conversation>(10);
525        }
526
527        /**
528         * Return the conversation with the specified thread ID, or
529         * null if it's not in cache.
530         */
531        static Conversation get(long threadId) {
532            synchronized (sInstance) {
533                if (DEBUG) {
534                    LogTag.debug("Conversation get with threadId: " + threadId);
535                }
536                dumpCache();
537                for (Conversation c : sInstance.mCache) {
538                    if (DEBUG) {
539                        LogTag.debug("Conversation get() threadId: " + threadId +
540                                " c.getThreadId(): " + c.getThreadId());
541                    }
542                    if (c.getThreadId() == threadId) {
543                        return c;
544                    }
545                }
546            }
547            return null;
548        }
549
550        /**
551         * Return the conversation with the specified recipient
552         * list, or null if it's not in cache.
553         */
554        static Conversation get(ContactList list) {
555            synchronized (sInstance) {
556                if (DEBUG) {
557                    LogTag.debug("Conversation get with ContactList: " + list);
558                    dumpCache();
559                }
560                for (Conversation c : sInstance.mCache) {
561                    if (c.getRecipients().equals(list)) {
562                        return c;
563                    }
564                }
565            }
566            return null;
567        }
568
569        /**
570         * Put the specified conversation in the cache.  The caller
571         * should not place an already-existing conversation in the
572         * cache, but rather update it in place.
573         */
574        static void put(Conversation c) {
575            synchronized (sInstance) {
576                // We update cache entries in place so people with long-
577                // held references get updated.
578                if (DEBUG) {
579                    LogTag.debug("Conversation c: " + c + " put with threadid: " + c.getThreadId() +
580                            " c.hash: " + c.hashCode());
581                    dumpCache();
582                }
583
584                if (sInstance.mCache.contains(c)) {
585                    throw new IllegalStateException("cache already contains " + c +
586                            " threadId: " + c.mThreadId);
587                }
588                sInstance.mCache.add(c);
589            }
590        }
591
592        static void remove(long threadId) {
593            if (DEBUG) {
594                LogTag.debug("remove threadid: " + threadId);
595                dumpCache();
596            }
597            for (Conversation c : sInstance.mCache) {
598                if (c.getThreadId() == threadId) {
599                    sInstance.mCache.remove(c);
600                    return;
601                }
602            }
603        }
604
605        static void dumpCache() {
606            if (DEBUG) {
607                synchronized (sInstance) {
608                    LogTag.debug("Conversation dumpCache: ");
609                    for (Conversation c : sInstance.mCache) {
610                        LogTag.debug("   c: " + c + " c.getThreadId(): " + c.getThreadId() +
611                                " hash: " + c.hashCode());
612                    }
613                }
614            }
615        }
616
617        /**
618         * Remove all conversations from the cache that are not in
619         * the provided set of thread IDs.
620         */
621        static void keepOnly(Set<Long> threads) {
622            synchronized (sInstance) {
623                Iterator<Conversation> iter = sInstance.mCache.iterator();
624                while (iter.hasNext()) {
625                    Conversation c = iter.next();
626                    if (!threads.contains(c.getThreadId())) {
627                        iter.remove();
628                    }
629                }
630            }
631        }
632    }
633
634    /**
635     * Set up the conversation cache.  To be called once at application
636     * startup time.
637     */
638    public static void init(final Context context) {
639        new Thread(new Runnable() {
640            public void run() {
641                cacheAllThreads(context);
642            }
643        }).start();
644    }
645
646    private static void cacheAllThreads(Context context) {
647        synchronized (Cache.getInstance()) {
648            if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
649                LogTag.debug("[Conversation] cacheAllThreads");
650            }
651            // Keep track of what threads are now on disk so we
652            // can discard anything removed from the cache.
653            HashSet<Long> threadsOnDisk = new HashSet<Long>();
654
655            // Query for all conversations.
656            Cursor c = context.getContentResolver().query(sAllThreadsUri,
657                    ALL_THREADS_PROJECTION, null, null, null);
658            try {
659                while (c.moveToNext()) {
660                    long threadId = c.getLong(ID);
661                    threadsOnDisk.add(threadId);
662
663                    // Try to find this thread ID in the cache.
664                    Conversation conv = Cache.get(threadId);
665
666                    if (conv == null) {
667                        // Make a new Conversation and put it in
668                        // the cache if necessary.
669                        conv = new Conversation(context, c, true);
670                        try {
671                            Cache.put(conv);
672                        } catch (IllegalStateException e) {
673                            LogTag.error("Tried to add duplicate Conversation to Cache");
674                        }
675                    } else {
676                        // Or update in place so people with references
677                        // to conversations get updated too.
678                        fillFromCursor(context, conv, c, true);
679                    }
680                }
681            } finally {
682                c.close();
683            }
684
685            // Purge the cache of threads that no longer exist on disk.
686            Cache.keepOnly(threadsOnDisk);
687        }
688    }
689
690    private boolean loadFromThreadId(long threadId) {
691        Cursor c = mContext.getContentResolver().query(sAllThreadsUri, ALL_THREADS_PROJECTION,
692                "_id=" + Long.toString(threadId), null, null);
693        try {
694            if (c.moveToFirst()) {
695                fillFromCursor(mContext, this, c, true);
696            } else {
697                LogTag.error("loadFromThreadId: Can't find thread ID " + threadId);
698                return false;
699            }
700        } finally {
701            c.close();
702        }
703        return true;
704    }
705}
706