FileDocCategorySizeDatePackage
PduCache.javaAPI DocAndroid 1.5 API8298Wed May 06 22:41:56 BST 2009com.google.android.mms.util

PduCache

public final class PduCache extends AbstractCache

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final boolean
LOCAL_LOGV
private static final int
MMS_ALL
private static final int
MMS_ALL_ID
private static final int
MMS_INBOX
private static final int
MMS_INBOX_ID
private static final int
MMS_SENT
private static final int
MMS_SENT_ID
private static final int
MMS_DRAFTS
private static final int
MMS_DRAFTS_ID
private static final int
MMS_OUTBOX
private static final int
MMS_OUTBOX_ID
private static final int
MMS_CONVERSATION
private static final int
MMS_CONVERSATION_ID
private static final android.content.UriMatcher
URI_MATCHER
private static final HashMap
MATCH_TO_MSGBOX_ID_MAP
private static PduCache
sInstance
private final HashMap
mMessageBoxes
private final HashMap
mThreads
Constructors Summary
private PduCache()

        mMessageBoxes = new HashMap<Integer, HashSet<Uri>>();
        mThreads = new HashMap<Long, HashSet<Uri>>();
    
Methods Summary
public static final synchronized com.google.android.mms.util.PduCachegetInstance()

        if (sInstance == null) {
            if (LOCAL_LOGV) {
                Log.v(TAG, "Constructing new PduCache instance.");
            }
            sInstance = new PduCache();
        }
        return sInstance;
    
private android.net.UrinormalizeKey(android.net.Uri uri)

param
uri The Uri to be normalized.
return
Uri The normalized key of cached entry.

        int match = URI_MATCHER.match(uri);
        Uri normalizedKey = null;

        switch (match) {
            case MMS_ALL_ID:
                normalizedKey = uri;
                break;
            case MMS_INBOX_ID:
            case MMS_SENT_ID:
            case MMS_DRAFTS_ID:
            case MMS_OUTBOX_ID:
                String msgId = uri.getLastPathSegment();
                normalizedKey = Uri.withAppendedPath(Mms.CONTENT_URI, msgId);
                break;
            default:
                return null;
        }

        if (LOCAL_LOGV) {
            Log.v(TAG, uri + " -> " + normalizedKey);
        }
        return normalizedKey;
    
public synchronized PduCacheEntrypurge(android.net.Uri uri)

        int match = URI_MATCHER.match(uri);
        switch (match) {
            case MMS_ALL_ID:
                return purgeSingleEntry(uri);
            case MMS_INBOX_ID:
            case MMS_SENT_ID:
            case MMS_DRAFTS_ID:
            case MMS_OUTBOX_ID:
                String msgId = uri.getLastPathSegment();
                return purgeSingleEntry(Uri.withAppendedPath(Mms.CONTENT_URI, msgId));
            // Implicit batch of purge, return null.
            case MMS_ALL:
            case MMS_CONVERSATION:
                purgeAll();
                return null;
            case MMS_INBOX:
            case MMS_SENT:
            case MMS_DRAFTS:
            case MMS_OUTBOX:
                purgeByMessageBox(MATCH_TO_MSGBOX_ID_MAP.get(match));
                return null;
            case MMS_CONVERSATION_ID:
                purgeByThreadId(ContentUris.parseId(uri));
                return null;
            default:
                return null;
        }
    
public synchronized voidpurgeAll()

        super.purgeAll();

        mMessageBoxes.clear();
        mThreads.clear();
    
private voidpurgeByMessageBox(java.lang.Integer msgBoxId)

        if (LOCAL_LOGV) {
            Log.v(TAG, "Purge cache in message box: " + msgBoxId);
        }

        if (msgBoxId != null) {
            HashSet<Uri> msgBox = mMessageBoxes.remove(msgBoxId);
            if (msgBox != null) {
                for (Uri key : msgBox) {
                    PduCacheEntry entry = super.purge(key);
                    if (entry != null) {
                        removeFromThreads(key, entry);
                    }
                }
            }
        }
    
private voidpurgeByThreadId(long threadId)

        if (LOCAL_LOGV) {
            Log.v(TAG, "Purge cache in thread: " + threadId);
        }

        HashSet<Uri> thread = mThreads.remove(threadId);
        if (thread != null) {
            for (Uri key : thread) {
                PduCacheEntry entry = super.purge(key);
                if (entry != null) {
                    removeFromMessageBoxes(key, entry);
                }
            }
        }
    
private PduCacheEntrypurgeSingleEntry(android.net.Uri key)

        PduCacheEntry entry = super.purge(key);
        if (entry != null) {
            removeFromThreads(key, entry);
            removeFromMessageBoxes(key, entry);
            return entry;
        }
        return null;
    
public synchronized booleanput(android.net.Uri uri, PduCacheEntry entry)

        int msgBoxId = entry.getMessageBox();
        HashSet<Uri> msgBox = mMessageBoxes.get(msgBoxId);
        if (msgBox == null) {
            msgBox = new HashSet<Uri>();
            mMessageBoxes.put(msgBoxId, msgBox);
        }

        long threadId = entry.getThreadId();
        HashSet<Uri> thread = mThreads.get(threadId);
        if (thread == null) {
            thread = new HashSet<Uri>();
            mThreads.put(threadId, thread);
        }

        Uri finalKey = normalizeKey(uri);
        boolean result = super.put(finalKey, entry);
        if (result) {
            msgBox.add(finalKey);
            thread.add(finalKey);
        }
        return result;
    
private voidremoveFromMessageBoxes(android.net.Uri key, PduCacheEntry entry)

        HashSet<Uri> msgBox = mThreads.get(entry.getMessageBox());
        if (msgBox != null) {
            msgBox.remove(key);
        }
    
private voidremoveFromThreads(android.net.Uri key, PduCacheEntry entry)

        HashSet<Uri> thread = mThreads.get(entry.getThreadId());
        if (thread != null) {
            thread.remove(key);
        }