Search in sources :

Example 51 with MmsException

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

the class PduPersister method updatePart.

private void updatePart(Uri uri, PduPart part, HashMap<Uri, InputStream> preOpenedFiles) 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, preOpenedFiles);
    }
}
Also used : ContentValues(android.content.ContentValues) MmsException(com.google.android.mms.MmsException)

Example 52 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();
            if (data == null) {
                data = new String("").getBytes(CharacterSets.DEFAULT_CHARSET_NAME);
            }
            String dataText = new EncodedStringValue(data).getString();
            cv.put(Part.TEXT, dataText);
            if (mContentResolver.update(uri, cv, null, null) != 1) {
                if (data.length > MAX_TEXT_BODY_SIZE) {
                    ContentValues cv2 = new ContentValues();
                    cv2.put(Part.TEXT, cutString(dataText, MAX_TEXT_BODY_SIZE));
                    if (mContentResolver.update(uri, cv2, null, null) != 1) {
                        throw new MmsException("unable to update " + uri.toString());
                    }
                } else {
                    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) {
                            Timber.v("drm uri: " + uri + " path: " + path);
                        }
                        File f = new File(path);
                        long len = f.length();
                        if (LOCAL_LOGV) {
                            Timber.v("drm path: " + path + " len: " + len);
                        }
                        if (len > 0) {
                            // converted drm file
                            return;
                        }
                    } catch (Exception e) {
                        Timber.e(e, "Can't get file info for: " + part.getDataUri());
                    }
                }
                // 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)) {
                    Timber.w("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) {
                    Timber.v("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) {
                    Timber.v("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) {
        Timber.e(e, "Failed to open Input/Output stream.");
        throw new MmsException(e);
    } catch (IOException e) {
        Timber.e(e, "Failed to read/write data.");
        throw new MmsException(e);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                Timber.e(e, "IOException while closing: " + os);
            }
        // Ignore
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                Timber.e(e, "IOException while closing: " + is);
            }
        // 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)

Example 53 with MmsException

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

the class PushReceiver method getContentLocation.

public static String getContentLocation(Context context, Uri uri) throws MmsException {
    Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), uri, PROJECTION, null, null, null);
    if (cursor != null) {
        try {
            if ((cursor.getCount() == 1) && cursor.moveToFirst()) {
                String location = cursor.getString(COLUMN_CONTENT_LOCATION);
                cursor.close();
                return location;
            }
        } finally {
            cursor.close();
        }
    }
    throw new MmsException("Cannot get X-Mms-Content-Location from: " + uri);
}
Also used : MmsException(com.google.android.mms.MmsException) Cursor(android.database.Cursor)

Example 54 with MmsException

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

the class DownloadRequest method getContentLocation.

private String getContentLocation(Context context, Uri uri) throws MmsException {
    Cursor cursor = android.database.sqlite.SqliteWrapper.query(context, context.getContentResolver(), uri, PROJECTION, null, null, null);
    if (cursor != null) {
        try {
            if ((cursor.getCount() == 1) && cursor.moveToFirst()) {
                String location = cursor.getString(COLUMN_CONTENT_LOCATION);
                cursor.close();
                return location;
            }
        } finally {
            cursor.close();
        }
    }
    throw new MmsException("Cannot get X-Mms-Content-Location from: " + uri);
}
Also used : MmsException(com.google.android.mms.MmsException) Cursor(android.database.Cursor)

Example 55 with MmsException

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

the class DownloadRequest method persist.

public static Uri persist(Context context, byte[] response, MmsConfig.Overridden mmsConfig, String locationUrl, int subId, String creator) {
    // Let any mms apps running as secondary user know that a new mms has been downloaded.
    notifyOfDownload(context);
    Timber.d("DownloadRequest.persistIfRequired");
    if (response == null || response.length < 1) {
        Timber.e("DownloadRequest.persistIfRequired: empty response");
        // Update the retrieve status of the NotificationInd
        final ContentValues values = new ContentValues(1);
        values.put(Telephony.Mms.RETRIEVE_STATUS, PduHeaders.RETRIEVE_STATUS_ERROR_END);
        SqliteWrapper.update(context, context.getContentResolver(), Telephony.Mms.CONTENT_URI, values, LOCATION_SELECTION, new String[] { Integer.toString(PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND), locationUrl });
        return null;
    }
    final long identity = Binder.clearCallingIdentity();
    try {
        final GenericPdu pdu = (new PduParser(response, mmsConfig.getSupportMmsContentDisposition())).parse();
        if (pdu == null || !(pdu instanceof RetrieveConf)) {
            Timber.e("DownloadRequest.persistIfRequired: invalid parsed PDU");
            // Update the error type of the NotificationInd
            setErrorType(context, locationUrl, Telephony.MmsSms.ERR_TYPE_MMS_PROTO_PERMANENT);
            return null;
        }
        final RetrieveConf retrieveConf = (RetrieveConf) pdu;
        final int status = retrieveConf.getRetrieveStatus();
        // if (status != PduHeaders.RETRIEVE_STATUS_OK) {
        // Timber.e("DownloadRequest.persistIfRequired: retrieve failed "
        // + status);
        // // Update the retrieve status of the NotificationInd
        // final ContentValues values = new ContentValues(1);
        // values.put(Telephony.Mms.RETRIEVE_STATUS, status);
        // SqliteWrapper.update(
        // context,
        // context.getContentResolver(),
        // Telephony.Mms.CONTENT_URI,
        // values,
        // LOCATION_SELECTION,
        // new String[]{
        // Integer.toString(PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND),
        // mLocationUrl
        // });
        // return null;
        // }
        // Store the downloaded message
        final PduPersister persister = PduPersister.getPduPersister(context);
        final Uri messageUri = persister.persist(pdu, Telephony.Mms.Inbox.CONTENT_URI, PduPersister.DUMMY_THREAD_ID, true, true, null);
        if (messageUri == null) {
            Timber.e("DownloadRequest.persistIfRequired: can not persist message");
            return null;
        }
        // Update some of the properties of the message
        final ContentValues values = new ContentValues();
        values.put(Telephony.Mms.DATE, System.currentTimeMillis() / 1000L);
        try {
            values.put(Telephony.Mms.DATE_SENT, retrieveConf.getDate());
        } catch (Exception ignored) {
        }
        values.put(Telephony.Mms.READ, 0);
        values.put(Telephony.Mms.SEEN, 0);
        if (!TextUtils.isEmpty(creator)) {
            values.put(Telephony.Mms.CREATOR, creator);
        }
        if (SubscriptionIdChecker.getInstance(context).canUseSubscriptionId()) {
            values.put(Telephony.Mms.SUBSCRIPTION_ID, subId);
        }
        try {
            context.getContentResolver().update(messageUri, values, null, null);
        } catch (SQLiteException e) {
            // in advance that it will fail, and we have to just try again
            if (values.containsKey(Telephony.Mms.SUBSCRIPTION_ID)) {
                values.remove(Telephony.Mms.SUBSCRIPTION_ID);
                context.getContentResolver().update(messageUri, values, null, null);
            } else {
                throw e;
            }
        }
        // Delete the corresponding NotificationInd
        SqliteWrapper.delete(context, context.getContentResolver(), Telephony.Mms.CONTENT_URI, LOCATION_SELECTION, new String[] { Integer.toString(PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND), locationUrl });
        return messageUri;
    } catch (MmsException e) {
        Timber.e(e, "DownloadRequest.persistIfRequired: can not persist message");
    } catch (SQLiteException e) {
        Timber.e(e, "DownloadRequest.persistIfRequired: can not update message");
    } catch (RuntimeException e) {
        Timber.e(e, "DownloadRequest.persistIfRequired: can not parse response");
    } finally {
        Binder.restoreCallingIdentity(identity);
    }
    return null;
}
Also used : ContentValues(android.content.ContentValues) PduParser(com.google.android.mms.pdu_alt.PduParser) MmsException(com.google.android.mms.MmsException) PduPersister(com.google.android.mms.pdu_alt.PduPersister) GenericPdu(com.google.android.mms.pdu_alt.GenericPdu) SQLiteException(android.database.sqlite.SQLiteException) Uri(android.net.Uri) RetrieveConf(com.google.android.mms.pdu_alt.RetrieveConf) MmsException(com.google.android.mms.MmsException) MmsHttpException(com.android.mms.service_alt.exception.MmsHttpException) SQLiteException(android.database.sqlite.SQLiteException)

Aggregations

MmsException (com.google.android.mms.MmsException)88 Uri (android.net.Uri)32 ContentValues (android.content.ContentValues)27 Cursor (android.database.Cursor)21 IOException (java.io.IOException)18 Intent (android.content.Intent)12 EncodedStringValue (com.google.android.mms.pdu.EncodedStringValue)9 PduBody (com.google.android.mms.pdu.PduBody)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ContentResolver (android.content.ContentResolver)8 InputStream (java.io.InputStream)8 PduPersister (com.google.android.mms.pdu.PduPersister)7 PduPersister (com.google.android.mms.pdu_alt.PduPersister)7 Entry (java.util.Map.Entry)7 SpannableString (android.text.SpannableString)6 PduBody (com.google.android.mms.pdu_alt.PduBody)6 PduPart (com.google.android.mms.pdu_alt.PduPart)6 FileNotFoundException (java.io.FileNotFoundException)6 InvalidHeaderValueException (com.google.android.mms.InvalidHeaderValueException)5 PduPart (com.google.android.mms.pdu.PduPart)5