Search in sources :

Example 1 with PduCacheEntry

use of com.google.android.mms.util_alt.PduCacheEntry in project qksms by moezbhatti.

the class PduPersister method load.

/**
     * Load a PDU from storage by given Uri.
     *
     * @param uri The Uri of the PDU to be loaded.
     * @return A generic PDU object, it may be cast to dedicated PDU.
     * @throws MmsException Failed to load some fields of a PDU.
     */
public GenericPdu load(Uri uri) throws MmsException {
    GenericPdu pdu = null;
    PduCacheEntry cacheEntry = null;
    int msgBox = 0;
    long threadId = DUMMY_THREAD_ID;
    try {
        synchronized (PDU_CACHE_INSTANCE) {
            if (PDU_CACHE_INSTANCE.isUpdating(uri)) {
                if (LOCAL_LOGV)
                    Log.v(TAG, "load: " + uri + " blocked by isUpdating()");
                try {
                    PDU_CACHE_INSTANCE.wait();
                } catch (InterruptedException e) {
                    Log.e(TAG, "load: ", e);
                }
                cacheEntry = PDU_CACHE_INSTANCE.get(uri);
                if (cacheEntry != null) {
                    return cacheEntry.getPdu();
                }
            }
            // Tell the cache to indicate to other callers that this item
            // is currently being updated.
            PDU_CACHE_INSTANCE.setUpdating(uri, true);
        }
        Cursor c = SqliteWrapper.query(mContext, mContentResolver, uri, PDU_PROJECTION, null, null, null);
        PduHeaders headers = new PduHeaders();
        Set<Entry<Integer, Integer>> set;
        long msgId = ContentUris.parseId(uri);
        try {
            if ((c == null) || (c.getCount() != 1) || !c.moveToFirst()) {
                throw new MmsException("Bad uri: " + uri);
            }
            msgBox = c.getInt(PDU_COLUMN_MESSAGE_BOX);
            threadId = c.getLong(PDU_COLUMN_THREAD_ID);
            set = ENCODED_STRING_COLUMN_INDEX_MAP.entrySet();
            for (Entry<Integer, Integer> e : set) {
                setEncodedStringValueToHeaders(c, e.getValue(), headers, e.getKey());
            }
            set = TEXT_STRING_COLUMN_INDEX_MAP.entrySet();
            for (Entry<Integer, Integer> e : set) {
                setTextStringToHeaders(c, e.getValue(), headers, e.getKey());
            }
            set = OCTET_COLUMN_INDEX_MAP.entrySet();
            for (Entry<Integer, Integer> e : set) {
                setOctetToHeaders(c, e.getValue(), headers, e.getKey());
            }
            set = LONG_COLUMN_INDEX_MAP.entrySet();
            for (Entry<Integer, Integer> e : set) {
                setLongToHeaders(c, e.getValue(), headers, e.getKey());
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
        // Check whether 'msgId' has been assigned a valid value.
        if (msgId == -1L) {
            throw new MmsException("Error! ID of the message: -1.");
        }
        // Load address information of the MM.
        loadAddress(msgId, headers);
        int msgType = headers.getOctet(PduHeaders.MESSAGE_TYPE);
        PduBody body = new PduBody();
        // load multiparts and put them into the body of the PDU.
        if ((msgType == PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF) || (msgType == PduHeaders.MESSAGE_TYPE_SEND_REQ)) {
            PduPart[] parts = loadParts(msgId);
            if (parts != null) {
                int partsNum = parts.length;
                for (int i = 0; i < partsNum; i++) {
                    body.addPart(parts[i]);
                }
            }
        }
        switch(msgType) {
            case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND:
                pdu = new NotificationInd(headers);
                break;
            case PduHeaders.MESSAGE_TYPE_DELIVERY_IND:
                pdu = new DeliveryInd(headers);
                break;
            case PduHeaders.MESSAGE_TYPE_READ_ORIG_IND:
                pdu = new ReadOrigInd(headers);
                break;
            case PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF:
                pdu = new RetrieveConf(headers, body);
                break;
            case PduHeaders.MESSAGE_TYPE_SEND_REQ:
                pdu = new SendReq(headers, body);
                break;
            case PduHeaders.MESSAGE_TYPE_ACKNOWLEDGE_IND:
                pdu = new AcknowledgeInd(headers);
                break;
            case PduHeaders.MESSAGE_TYPE_NOTIFYRESP_IND:
                pdu = new NotifyRespInd(headers);
                break;
            case PduHeaders.MESSAGE_TYPE_READ_REC_IND:
                pdu = new ReadRecInd(headers);
                break;
            case PduHeaders.MESSAGE_TYPE_SEND_CONF:
            case PduHeaders.MESSAGE_TYPE_FORWARD_REQ:
            case PduHeaders.MESSAGE_TYPE_FORWARD_CONF:
            case PduHeaders.MESSAGE_TYPE_MBOX_STORE_REQ:
            case PduHeaders.MESSAGE_TYPE_MBOX_STORE_CONF:
            case PduHeaders.MESSAGE_TYPE_MBOX_VIEW_REQ:
            case PduHeaders.MESSAGE_TYPE_MBOX_VIEW_CONF:
            case PduHeaders.MESSAGE_TYPE_MBOX_UPLOAD_REQ:
            case PduHeaders.MESSAGE_TYPE_MBOX_UPLOAD_CONF:
            case PduHeaders.MESSAGE_TYPE_MBOX_DELETE_REQ:
            case PduHeaders.MESSAGE_TYPE_MBOX_DELETE_CONF:
            case PduHeaders.MESSAGE_TYPE_MBOX_DESCR:
            case PduHeaders.MESSAGE_TYPE_DELETE_REQ:
            case PduHeaders.MESSAGE_TYPE_DELETE_CONF:
            case PduHeaders.MESSAGE_TYPE_CANCEL_REQ:
            case PduHeaders.MESSAGE_TYPE_CANCEL_CONF:
                throw new MmsException("Unsupported PDU type: " + Integer.toHexString(msgType));
            default:
                throw new MmsException("Unrecognized PDU type: " + Integer.toHexString(msgType));
        }
    } finally {
        synchronized (PDU_CACHE_INSTANCE) {
            if (pdu != null) {
                assert (PDU_CACHE_INSTANCE.get(uri) == null);
                // Update the cache entry with the real info
                cacheEntry = new PduCacheEntry(pdu, msgBox, threadId);
                PDU_CACHE_INSTANCE.put(uri, cacheEntry);
            }
            PDU_CACHE_INSTANCE.setUpdating(uri, false);
            // tell anybody waiting on this entry to go ahead
            PDU_CACHE_INSTANCE.notifyAll();
        }
    }
    return pdu;
}
Also used : PduCacheEntry(com.google.android.mms.util_alt.PduCacheEntry) Cursor(android.database.Cursor) PduCacheEntry(com.google.android.mms.util_alt.PduCacheEntry) Entry(java.util.Map.Entry) MmsException(com.google.android.mms.MmsException)

Example 2 with PduCacheEntry

use of com.google.android.mms.util_alt.PduCacheEntry in project qksms by moezbhatti.

the class PduPersister method updateParts.

/**
     * Update all parts of a PDU.
     *
     * @param uri The PDU which need to be updated.
     * @param body New message body of the PDU.
     * @param preOpenedFiles if not null, a map of preopened InputStreams for the parts.
     * @throws MmsException Bad URI or updating failed.
     */
public void updateParts(Uri uri, PduBody body, HashMap<Uri, InputStream> preOpenedFiles) throws MmsException {
    try {
        PduCacheEntry cacheEntry;
        synchronized (PDU_CACHE_INSTANCE) {
            if (PDU_CACHE_INSTANCE.isUpdating(uri)) {
                if (LOCAL_LOGV)
                    Log.v(TAG, "updateParts: " + uri + " blocked by isUpdating()");
                try {
                    PDU_CACHE_INSTANCE.wait();
                } catch (InterruptedException e) {
                    Log.e(TAG, "updateParts: ", e);
                }
                cacheEntry = PDU_CACHE_INSTANCE.get(uri);
                if (cacheEntry != null) {
                    ((MultimediaMessagePdu) cacheEntry.getPdu()).setBody(body);
                }
            }
            // Tell the cache to indicate to other callers that this item
            // is currently being updated.
            PDU_CACHE_INSTANCE.setUpdating(uri, true);
        }
        ArrayList<PduPart> toBeCreated = new ArrayList<PduPart>();
        HashMap<Uri, PduPart> toBeUpdated = new HashMap<Uri, PduPart>();
        int partsNum = body.getPartsNum();
        StringBuilder filter = new StringBuilder().append('(');
        for (int i = 0; i < partsNum; i++) {
            PduPart part = body.getPart(i);
            Uri partUri = part.getDataUri();
            if ((partUri == null) || !partUri.getAuthority().startsWith("mms")) {
                toBeCreated.add(part);
            } else {
                toBeUpdated.put(partUri, part);
                // 'AND' since 'i = 0' may be skipped in another branch.
                if (filter.length() > 1) {
                    filter.append(" AND ");
                }
                filter.append("_id");
                filter.append("!=");
                DatabaseUtils.appendEscapedSQLString(filter, partUri.getLastPathSegment());
            }
        }
        filter.append(')');
        long msgId = ContentUris.parseId(uri);
        // Remove the parts which doesn't exist anymore.
        SqliteWrapper.delete(mContext, mContentResolver, Uri.parse(Uri.parse("content://mms") + "/" + msgId + "/part"), filter.length() > 2 ? filter.toString() : null, null);
        // Create new parts which didn't exist before.
        for (PduPart part : toBeCreated) {
            persistPart(part, msgId, preOpenedFiles);
        }
        // Update the modified parts.
        for (Entry<Uri, PduPart> e : toBeUpdated.entrySet()) {
            updatePart(e.getKey(), e.getValue(), preOpenedFiles);
        }
    } finally {
        synchronized (PDU_CACHE_INSTANCE) {
            PDU_CACHE_INSTANCE.setUpdating(uri, false);
            PDU_CACHE_INSTANCE.notifyAll();
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PduCacheEntry(com.google.android.mms.util_alt.PduCacheEntry) Uri(android.net.Uri)

Example 3 with PduCacheEntry

use of com.google.android.mms.util_alt.PduCacheEntry in project qksms by moezbhatti.

the class PduLoaderManager method getPdu.

public ItemLoadedFuture getPdu(Uri uri, boolean requestSlideshow, final ItemLoadedCallback<PduLoaded> callback) {
    if (uri == null) {
        throw new NullPointerException();
    }
    PduCacheEntry cacheEntry = null;
    synchronized (mPduCache) {
        if (!mPduCache.isUpdating(uri)) {
            cacheEntry = mPduCache.get(uri);
        }
    }
    final SlideshowModel slideshow = (requestSlideshow && !DEBUG_DISABLE_CACHE) ? mSlideshowCache.get(uri) : null;
    final boolean slideshowExists = !requestSlideshow || slideshow != null;
    final boolean pduExists = cacheEntry != null && cacheEntry.getPdu() != null;
    final boolean taskExists = mPendingTaskUris.contains(uri);
    final boolean newTaskRequired = (!pduExists || !slideshowExists) && !taskExists;
    final boolean callbackRequired = callback != null;
    if (pduExists && slideshowExists) {
        if (callbackRequired) {
            PduLoaded pduLoaded = new PduLoaded(cacheEntry.getPdu(), slideshow);
            callback.onItemLoaded(pduLoaded, null);
        }
        return new NullItemLoadedFuture();
    }
    if (callbackRequired) {
        addCallback(uri, callback);
    }
    if (newTaskRequired) {
        mPendingTaskUris.add(uri);
        Runnable task = new PduTask(uri, requestSlideshow);
        mExecutor.execute(task);
    }
    return new ItemLoadedFuture() {

        private boolean mIsDone;

        public void cancel(Uri uri) {
            cancelCallback(callback);
            // the pdu and/or slideshow might be half loaded. Make sure
            removePdu(uri);
        // we load fresh the next time this uri is requested.
        }

        public void setIsDone(boolean done) {
            mIsDone = done;
        }

        public boolean isDone() {
            return mIsDone;
        }
    };
}
Also used : SlideshowModel(com.moez.QKSMS.model.SlideshowModel) PduCacheEntry(com.google.android.mms.util_alt.PduCacheEntry) Uri(android.net.Uri)

Aggregations

PduCacheEntry (com.google.android.mms.util_alt.PduCacheEntry)3 Uri (android.net.Uri)2 Cursor (android.database.Cursor)1 MmsException (com.google.android.mms.MmsException)1 SlideshowModel (com.moez.QKSMS.model.SlideshowModel)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1