Search in sources :

Example 1 with DrmConvertSession

use of com.google.android.mms.util_alt.DrmConvertSession 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

ContentValues (android.content.ContentValues)1 SQLiteException (android.database.sqlite.SQLiteException)1 Uri (android.net.Uri)1 InvalidHeaderValueException (com.google.android.mms.InvalidHeaderValueException)1 MmsException (com.google.android.mms.MmsException)1 DrmConvertSession (com.google.android.mms.util_alt.DrmConvertSession)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1