FileDocCategorySizeDatePackage
DraftCache.javaAPI DocAndroid 1.5 API5393Wed May 06 22:42:46 BST 2009com.android.mms.util

DraftCache

public class DraftCache extends Object
Cache for information about draft messages on conversations.

Fields Summary
private static final String
TAG
private static DraftCache
sInstance
private final android.content.Context
mContext
private HashSet
mDraftSet
private final HashSet
mChangeListeners
static final String[]
DRAFT_PROJECTION
static final int
COLUMN_DRAFT_THREAD_ID
Constructors Summary
private DraftCache(android.content.Context context)

    
       
            
    
    
       
        mContext = context;
        refresh();
    
Methods Summary
public synchronized voidaddOnDraftChangedListener(com.android.mms.util.DraftCache$OnDraftChangedListener l)

        mChangeListeners.add(l);
    
public static com.android.mms.util.DraftCachegetInstance()
Get the global instance.

        return sInstance;
    
public synchronized booleanhasDraft(long threadId)
Returns true if the given thread ID has a draft associated with it, false if not.

        return mDraftSet.contains(threadId);
    
public static voidinit(android.content.Context context)
Initialize the global instance. Should call only once.

        sInstance = new DraftCache(context);
    
private voidlog(java.lang.String format, java.lang.Object args)

        String s = String.format(format, args);
        Log.d(TAG, "[" + Thread.currentThread().getId() + "] " + s);
    
private synchronized voidrebuildCache()
Does the actual work of rebuilding the draft cache.

        HashSet<Long> oldDraftSet = mDraftSet;
        HashSet<Long> newDraftSet = new HashSet<Long>(oldDraftSet.size());
        
        Cursor cursor = SqliteWrapper.query(
                mContext,
                mContext.getContentResolver(),
                MmsSms.CONTENT_DRAFT_URI,
                DRAFT_PROJECTION, null, null, null);

        try {
            if (cursor.moveToFirst()) {
                for (; !cursor.isAfterLast(); cursor.moveToNext()) {
                    long threadId = cursor.getLong(COLUMN_DRAFT_THREAD_ID);
                    newDraftSet.add(threadId);
                }
            }
        } finally {
            cursor.close();
        }
        
        mDraftSet = newDraftSet;
        
        // If nobody's interested in finding out about changes,
        // just bail out early.
        if (mChangeListeners.size() < 1) {
            return;
        }
        
        // Find out which drafts were removed and added and notify
        // listeners.
        Set<Long> added = new HashSet<Long>(newDraftSet);
        added.removeAll(oldDraftSet);
        Set<Long> removed = new HashSet<Long>(oldDraftSet);
        removed.removeAll(newDraftSet);

        for (OnDraftChangedListener l : mChangeListeners) {
            for (long threadId : added) {
                l.onDraftChanged(threadId, true);
            }
            for (long threadId : removed) {
                l.onDraftChanged(threadId, false);
            }
        }
    
public voidrefresh()
To be called whenever the draft state might have changed. Dispatches work to a thread and returns immediately.


                            
       
        new Thread(new Runnable() {
            public void run() {
                rebuildCache();
            }
        }).start();
    
public synchronized voidremoveOnDraftChangedListener(com.android.mms.util.DraftCache$OnDraftChangedListener l)

        mChangeListeners.remove(l);
    
public synchronized voidsetDraftState(long threadId, boolean hasDraft)
Updates the has-draft status of a particular thread on a piecemeal basis, to be called when a draft has appeared or disappeared.

        if (threadId <= 0) {
            return;
        }
        
        boolean changed;
        if (hasDraft) {
            changed = mDraftSet.add(threadId);
        } else {
            changed = mDraftSet.remove(threadId);
        }

        // Notify listeners if there was a change.
        if (changed) {
            for (OnDraftChangedListener l : mChangeListeners) {
                l.onDraftChanged(threadId, hasDraft);
            }
        }