Search in sources :

Example 31 with MmsException

use of com.google.android.mms.MmsException in project XobotOS by xamarin.

the class PduPersister method updatePart.

private void updatePart(Uri uri, PduPart part) throws MmsException {
    ContentValues values = new ContentValues(7);
    int charset = part.getCharset();
    if (charset != 0) {
        values.put(Part.CHARSET, charset);
    }
    String contentType = null;
    if (part.getContentType() != null) {
        contentType = toIsoString(part.getContentType());
        values.put(Part.CONTENT_TYPE, contentType);
    } else {
        throw new MmsException("MIME type of the part must be set.");
    }
    if (part.getFilename() != null) {
        String fileName = new String(part.getFilename());
        values.put(Part.FILENAME, fileName);
    }
    if (part.getName() != null) {
        String name = new String(part.getName());
        values.put(Part.NAME, name);
    }
    Object value = null;
    if (part.getContentDisposition() != null) {
        value = toIsoString(part.getContentDisposition());
        values.put(Part.CONTENT_DISPOSITION, (String) value);
    }
    if (part.getContentId() != null) {
        value = toIsoString(part.getContentId());
        values.put(Part.CONTENT_ID, (String) value);
    }
    if (part.getContentLocation() != null) {
        value = toIsoString(part.getContentLocation());
        values.put(Part.CONTENT_LOCATION, (String) value);
    }
    SqliteWrapper.update(mContext, mContentResolver, uri, values, null, null);
    // 2. The Uri of the part is different from the current one.
    if ((part.getData() != null) || (uri != part.getDataUri())) {
        persistData(part, uri, contentType);
    }
}
Also used : ContentValues(android.content.ContentValues) MmsException(com.google.android.mms.MmsException)

Example 32 with MmsException

use of com.google.android.mms.MmsException in project XobotOS by xamarin.

the class PduPersister method move.

/**
     * Move a PDU object from one location to another.
     *
     * @param from Specify the PDU object to be moved.
     * @param to The destination location, should be one of the following:
     *        "content://mms/inbox", "content://mms/sent",
     *        "content://mms/drafts", "content://mms/outbox",
     *        "content://mms/trash".
     * @return New Uri of the moved PDU.
     * @throws MmsException Error occurred while moving the message.
     */
public Uri move(Uri from, Uri to) throws MmsException {
    // Check whether the 'msgId' has been assigned a valid value.
    long msgId = ContentUris.parseId(from);
    if (msgId == -1L) {
        throw new MmsException("Error! ID of the message: -1.");
    }
    // Get corresponding int value of destination box.
    Integer msgBox = MESSAGE_BOX_MAP.get(to);
    if (msgBox == null) {
        throw new MmsException("Bad destination, must be one of " + "content://mms/inbox, content://mms/sent, " + "content://mms/drafts, content://mms/outbox, " + "content://mms/temp.");
    }
    ContentValues values = new ContentValues(1);
    values.put(Mms.MESSAGE_BOX, msgBox);
    SqliteWrapper.update(mContext, mContentResolver, from, values, null, null);
    return ContentUris.withAppendedId(to, msgId);
}
Also used : ContentValues(android.content.ContentValues) MmsException(com.google.android.mms.MmsException)

Example 33 with MmsException

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

the class SmsHelper method getUnreadUnseenConversations.

/**
 * List of messages grouped by thread id, used for showing notifications
 */
public static HashMap<Long, ArrayList<MessageItem>> getUnreadUnseenConversations(Context context) {
    HashMap<Long, ArrayList<MessageItem>> result = new HashMap<>();
    String selection = SmsHelper.UNSEEN_SELECTION + " AND " + SmsHelper.UNREAD_SELECTION;
    // Create a cursor for the conversation list
    Cursor conversationCursor = context.getContentResolver().query(SmsHelper.CONVERSATIONS_CONTENT_PROVIDER, Conversation.ALL_THREADS_PROJECTION, SmsHelper.UNREAD_SELECTION, null, SmsHelper.sortDateAsc);
    if (conversationCursor != null && conversationCursor.moveToFirst()) {
        do {
            ArrayList<MessageItem> messages = new ArrayList<>();
            long threadId = conversationCursor.getLong(Conversation.ID);
            Uri threadUri = Uri.withAppendedPath(Message.MMS_SMS_CONTENT_PROVIDER, Long.toString(threadId));
            Cursor messageCursor = context.getContentResolver().query(threadUri, MessageColumns.PROJECTION, selection, null, SmsHelper.sortDateAsc);
            if (messageCursor != null && messageCursor.moveToFirst()) {
                do {
                    MessageColumns.ColumnsMap columnsMap = new MessageColumns.ColumnsMap(messageCursor);
                    MessageItem message = null;
                    try {
                        message = new MessageItem(context, messageCursor.getString(columnsMap.mColumnMsgType), messageCursor, columnsMap, null, true);
                    } catch (MmsException e) {
                        e.printStackTrace();
                    }
                    messages.add(message);
                } while (messageCursor.moveToNext());
                messageCursor.close();
                result.put(threadId, messages);
            }
        } while (conversationCursor.moveToNext());
        conversationCursor.close();
    }
    return result;
}
Also used : MessageItem(com.moez.QKSMS.ui.messagelist.MessageItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri) MessageColumns(com.moez.QKSMS.ui.messagelist.MessageColumns) MmsException(com.google.android.mms.MmsException)

Example 34 with MmsException

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

the class PduPersister method persist.

/**
 * Persist a PDU object to specific location in the storage.
 *
 * @param pdu The PDU object to be stored.
 * @param uri Where to store the given PDU object.
 * @param createThreadId if true, this function may create a thread id for the recipients
 * @param groupMmsEnabled if true, all of the recipients addressed in the PDU will be used
 *  to create the associated thread. When false, only the sender will be used in finding or
 *  creating the appropriate thread or conversation.
 * @param preOpenedFiles if not null, a map of preopened InputStreams for the parts.
 * @return A Uri which can be used to access the stored PDU.
 */
public Uri persist(GenericPdu pdu, Uri uri, boolean createThreadId, boolean groupMmsEnabled, HashMap<Uri, InputStream> preOpenedFiles) throws MmsException {
    if (uri == null) {
        throw new MmsException("Uri may not be null.");
    }
    long msgId = -1;
    try {
        msgId = ContentUris.parseId(uri);
    } catch (NumberFormatException e) {
    // the uri ends with "inbox" or something else like that
    }
    boolean existingUri = msgId != -1;
    if (!existingUri && MESSAGE_BOX_MAP.get(uri) == null) {
        throw new MmsException("Bad destination, must be one of " + "content://mms/inbox, content://mms/sent, " + "content://mms/drafts, content://mms/outbox, " + "content://mms/temp.");
    }
    synchronized (PDU_CACHE_INSTANCE) {
        // purging it.
        if (PDU_CACHE_INSTANCE.isUpdating(uri)) {
            if (LOCAL_LOGV)
                Log.v(TAG, "persist: " + uri + " blocked by isUpdating()");
            try {
                PDU_CACHE_INSTANCE.wait();
            } catch (InterruptedException e) {
                Log.e(TAG, "persist1: ", e);
            }
        }
    }
    PDU_CACHE_INSTANCE.purge(uri);
    PduHeaders header = pdu.getPduHeaders();
    PduBody body = null;
    ContentValues values = new ContentValues();
    Set<Entry<Integer, String>> set;
    set = ENCODED_STRING_COLUMN_NAME_MAP.entrySet();
    for (Entry<Integer, String> e : set) {
        int field = e.getKey();
        EncodedStringValue encodedString = header.getEncodedStringValue(field);
        if (encodedString != null) {
            String charsetColumn = CHARSET_COLUMN_NAME_MAP.get(field);
            values.put(e.getValue(), toIsoString(encodedString.getTextString()));
            values.put(charsetColumn, encodedString.getCharacterSet());
        }
    }
    set = TEXT_STRING_COLUMN_NAME_MAP.entrySet();
    for (Entry<Integer, String> e : set) {
        byte[] text = header.getTextString(e.getKey());
        if (text != null) {
            values.put(e.getValue(), toIsoString(text));
        }
    }
    set = OCTET_COLUMN_NAME_MAP.entrySet();
    for (Entry<Integer, String> e : set) {
        int b = header.getOctet(e.getKey());
        if (b != 0) {
            values.put(e.getValue(), b);
        }
    }
    set = LONG_COLUMN_NAME_MAP.entrySet();
    for (Entry<Integer, String> e : set) {
        long l = header.getLongInteger(e.getKey());
        if (l != -1L) {
            values.put(e.getValue(), l);
        }
    }
    HashMap<Integer, EncodedStringValue[]> addressMap = new HashMap<Integer, EncodedStringValue[]>(ADDRESS_FIELDS.length);
    // Save address information.
    for (int addrType : ADDRESS_FIELDS) {
        EncodedStringValue[] array = null;
        if (addrType == PduHeaders.FROM) {
            EncodedStringValue v = header.getEncodedStringValue(addrType);
            if (v != null) {
                array = new EncodedStringValue[1];
                array[0] = v;
            }
        } else {
            array = header.getEncodedStringValues(addrType);
        }
        addressMap.put(addrType, array);
    }
    HashSet<String> recipients = new HashSet<String>();
    int msgType = pdu.getMessageType();
    // this scope.
    if ((msgType == PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND) || (msgType == PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF) || (msgType == PduHeaders.MESSAGE_TYPE_SEND_REQ)) {
        switch(msgType) {
            case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND:
            case PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF:
                loadRecipients(PduHeaders.FROM, recipients, addressMap, false);
                // and we have to ignore it in loadRecipients.
                if (groupMmsEnabled) {
                    loadRecipients(PduHeaders.TO, recipients, addressMap, true);
                    loadRecipients(PduHeaders.CC, recipients, addressMap, true);
                }
                break;
            case PduHeaders.MESSAGE_TYPE_SEND_REQ:
                loadRecipients(PduHeaders.TO, recipients, addressMap, false);
                break;
        }
        long threadId = DUMMY_THREAD_ID;
        if (createThreadId && !recipients.isEmpty()) {
            // Given all the recipients associated with this message, find (or create) the
            // correct thread.
            threadId = Utils.getOrCreateThreadId(mContext, recipients);
        }
        values.put("thread_id", threadId);
    }
    // Save parts first to avoid inconsistent message is loaded
    // while saving the parts.
    // Dummy ID of the msg.
    long dummyId = System.currentTimeMillis();
    // Figure out if this PDU is a text-only message
    boolean textOnly = true;
    // Get body if the PDU is a RetrieveConf or SendReq.
    if (pdu instanceof MultimediaMessagePdu) {
        body = ((MultimediaMessagePdu) pdu).getBody();
        // Start saving parts if necessary.
        if (body != null) {
            int partsNum = body.getPartsNum();
            if (partsNum > 2) {
                // For a text-only message there will be two parts: 1-the SMIL, 2-the text.
                // Down a few lines below we're checking to make sure we've only got SMIL or
                // text. We also have to check then we don't have more than two parts.
                // Otherwise, a slideshow with two text slides would be marked as textOnly.
                textOnly = false;
            }
            for (int i = 0; i < partsNum; i++) {
                PduPart part = body.getPart(i);
                persistPart(part, dummyId, preOpenedFiles);
                // If we've got anything besides text/plain or SMIL part, then we've got
                // an mms message with some other type of attachment.
                String contentType = getPartContentType(part);
                if (contentType != null && !ContentType.APP_SMIL.equals(contentType) && !ContentType.TEXT_PLAIN.equals(contentType)) {
                    textOnly = false;
                }
            }
        }
    }
    // Record whether this mms message is a simple plain text or not. This is a hint for the
    // UI.
    // values.put(Mms.TEXT_ONLY, textOnly ? 1 : 0);
    Uri res = null;
    if (existingUri) {
        res = uri;
        SqliteWrapper.update(mContext, mContentResolver, res, values, null, null);
    } else {
        res = SqliteWrapper.insert(mContext, mContentResolver, uri, values);
        if (res == null) {
            throw new MmsException("persist() failed: return null.");
        }
        // Get the real ID of the PDU and update all parts which were
        // saved with the dummy ID.
        msgId = ContentUris.parseId(res);
    }
    values = new ContentValues(1);
    values.put("mid", msgId);
    SqliteWrapper.update(mContext, mContentResolver, Uri.parse("content://mms/" + dummyId + "/part"), values, null, null);
    // FIXME: Should the MmsProvider be responsible for this???
    if (!existingUri) {
        res = Uri.parse(uri + "/" + msgId);
    }
    // Save address information.
    for (int addrType : ADDRESS_FIELDS) {
        EncodedStringValue[] array = addressMap.get(addrType);
        if (array != null) {
            persistAddress(msgId, addrType, array);
        }
    }
    return res;
}
Also used : ContentValues(android.content.ContentValues) HashMap(java.util.HashMap) Uri(android.net.Uri) MmsException(com.google.android.mms.MmsException) PduCacheEntry(com.google.android.mms.util_alt.PduCacheEntry) Entry(java.util.Map.Entry) HashSet(java.util.HashSet)

Example 35 with MmsException

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

the class PduPersister method persistData.

/**
 * Save data of the part into storage. The source data may be given
 * by a byte[] or a Uri. If it's a byte[], directly save it
 * into storage, otherwise load source data from the dataUri and then
 * save it. If the data is an image, we may scale down it according
 * to user preference.
 *
 * @param part The PDU part which contains data to be saved.
 * @param uri The URI of the part.
 * @param contentType The MIME type of the part.
 * @param preOpenedFiles if not null, a map of preopened InputStreams for the parts.
 * @throws MmsException Cannot find source data or error occurred
 *         while saving the data.
 */
private void persistData(PduPart part, Uri uri, String contentType, HashMap<Uri, InputStream> preOpenedFiles) throws MmsException {
    OutputStream os = null;
    InputStream is = null;
    DrmConvertSession drmConvertSession = null;
    Uri dataUri = null;
    String path = null;
    try {
        byte[] data = part.getData();
        if (ContentType.TEXT_PLAIN.equals(contentType) || ContentType.APP_SMIL.equals(contentType) || ContentType.TEXT_HTML.equals(contentType)) {
            ContentValues cv = new ContentValues();
            cv.put(Telephony.Mms.Part.TEXT, new EncodedStringValue(data).getString());
            if (mContentResolver.update(uri, cv, null, null) != 1) {
                throw new MmsException("unable to update " + uri.toString());
            }
        } else {
            boolean isDrm = DownloadDrmHelper.isDrmConvertNeeded(contentType);
            if (isDrm) {
                if (uri != null) {
                    try {
                        path = convertUriToPath(mContext, uri);
                        if (LOCAL_LOGV)
                            Log.v(TAG, "drm uri: " + uri + " path: " + path);
                        File f = new File(path);
                        long len = f.length();
                        if (LOCAL_LOGV)
                            Log.v(TAG, "drm path: " + path + " len: " + len);
                        if (len > 0) {
                            // converted drm file
                            return;
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "Can't get file info for: " + part.getDataUri(), e);
                    }
                }
                // We haven't converted the file yet, start the conversion
                drmConvertSession = DrmConvertSession.open(mContext, contentType);
                if (drmConvertSession == null) {
                    throw new MmsException("Mimetype " + contentType + " can not be converted.");
                }
            }
            // uri can look like:
            // content://mms/part/98
            os = mContentResolver.openOutputStream(uri);
            if (data == null) {
                dataUri = part.getDataUri();
                if ((dataUri == null) || (dataUri == uri)) {
                    Log.w(TAG, "Can't find data for this part.");
                    return;
                }
                // content://com.google.android.gallery3d.provider/picasa/item/5720646660183715586
                if (preOpenedFiles != null && preOpenedFiles.containsKey(dataUri)) {
                    is = preOpenedFiles.get(dataUri);
                }
                if (is == null) {
                    is = mContentResolver.openInputStream(dataUri);
                }
                if (LOCAL_LOGV)
                    Log.v(TAG, "Saving data to: " + uri);
                byte[] buffer = new byte[8192];
                for (int len = 0; (len = is.read(buffer)) != -1; ) {
                    if (!isDrm) {
                        os.write(buffer, 0, len);
                    } else {
                        byte[] convertedData = drmConvertSession.convert(buffer, len);
                        if (convertedData != null) {
                            os.write(convertedData, 0, convertedData.length);
                        } else {
                            throw new MmsException("Error converting drm data.");
                        }
                    }
                }
            } else {
                if (LOCAL_LOGV)
                    Log.v(TAG, "Saving data to: " + uri);
                if (!isDrm) {
                    os.write(data);
                } else {
                    dataUri = uri;
                    byte[] convertedData = drmConvertSession.convert(data, data.length);
                    if (convertedData != null) {
                        os.write(convertedData, 0, convertedData.length);
                    } else {
                        throw new MmsException("Error converting drm data.");
                    }
                }
            }
        }
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Failed to open Input/Output stream.", e);
        throw new MmsException(e);
    } catch (IOException e) {
        Log.e(TAG, "Failed to read/write data.", e);
        throw new MmsException(e);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException while closing: " + os, e);
            }
        // Ignore
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException while closing: " + is, e);
            }
        // Ignore
        }
        if (drmConvertSession != null) {
            drmConvertSession.close(path);
            // Reset the permissions on the encrypted part file so everyone has only read
            // permission.
            File f = new File(path);
            ContentValues values = new ContentValues(0);
            SqliteWrapper.update(mContext, mContentResolver, Uri.parse("content://mms/resetFilePerm/" + f.getName()), values, null, null);
        }
    }
}
Also used : ContentValues(android.content.ContentValues) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Uri(android.net.Uri) MmsException(com.google.android.mms.MmsException) SQLiteException(android.database.sqlite.SQLiteException) InvalidHeaderValueException(com.google.android.mms.InvalidHeaderValueException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MmsException(com.google.android.mms.MmsException) DrmConvertSession(com.google.android.mms.util_alt.DrmConvertSession) File(java.io.File)

Aggregations

MmsException (com.google.android.mms.MmsException)39 Uri (android.net.Uri)15 ContentValues (android.content.ContentValues)14 Cursor (android.database.Cursor)10 IOException (java.io.IOException)10 PduBody (com.google.android.mms.pdu_alt.PduBody)6 PduPart (com.google.android.mms.pdu_alt.PduPart)6 PduPersister (com.google.android.mms.pdu_alt.PduPersister)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Intent (android.content.Intent)5 EncodedStringValue (com.google.android.mms.pdu_alt.EncodedStringValue)5 InputStream (java.io.InputStream)4 ContentResolver (android.content.ContentResolver)3 InvalidHeaderValueException (com.google.android.mms.InvalidHeaderValueException)3 EncodedStringValue (com.google.android.mms.pdu.EncodedStringValue)3 SendReq (com.google.android.mms.pdu_alt.SendReq)3 FileNotFoundException (java.io.FileNotFoundException)3 HashMap (java.util.HashMap)3 Entry (java.util.Map.Entry)3 Resources (android.content.res.Resources)2