Search in sources :

Example 11 with PduPart

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

the class Transaction method getBytes.

public static MessageInfo getBytes(Context context, boolean saveMessage, String[] recipients, MMSPart[] parts, String subject) throws MmsException {
    final SendReq sendRequest = new SendReq();
    // create send request addresses
    for (int i = 0; i < recipients.length; i++) {
        final EncodedStringValue[] phoneNumbers = EncodedStringValue.extract(recipients[i]);
        if (phoneNumbers != null && phoneNumbers.length > 0) {
            sendRequest.addTo(phoneNumbers[0]);
        }
    }
    if (subject != null) {
        sendRequest.setSubject(new EncodedStringValue(subject));
    }
    sendRequest.setDate(Calendar.getInstance().getTimeInMillis() / 1000L);
    try {
        sendRequest.setFrom(new EncodedStringValue(Utils.getMyPhoneNumber(context)));
    } catch (Exception e) {
    // my number is nothing
    }
    final PduBody pduBody = new PduBody();
    // assign parts to the pdu body which contains sending data
    if (parts != null) {
        for (int i = 0; i < parts.length; i++) {
            MMSPart part = parts[i];
            if (part != null) {
                try {
                    PduPart partPdu = new PduPart();
                    partPdu.setName(part.Name.getBytes());
                    partPdu.setContentType(part.MimeType.getBytes());
                    if (part.MimeType.startsWith("text")) {
                        partPdu.setCharset(CharacterSets.UTF_8);
                    }
                    partPdu.setData(part.Data);
                    pduBody.addPart(partPdu);
                } catch (Exception e) {
                }
            }
        }
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SmilXmlSerializer.serialize(SmilHelper.createSmilDocument(pduBody), out);
    PduPart smilPart = new PduPart();
    smilPart.setContentId("smil".getBytes());
    smilPart.setContentLocation("smil.xml".getBytes());
    smilPart.setContentType(ContentType.APP_SMIL.getBytes());
    smilPart.setData(out.toByteArray());
    pduBody.addPart(0, smilPart);
    sendRequest.setBody(pduBody);
    // create byte array which will actually be sent
    final PduComposer composer = new PduComposer(context, sendRequest);
    final byte[] bytesToSend;
    try {
        bytesToSend = composer.make();
    } catch (OutOfMemoryError e) {
        throw new MmsException("Out of memory!");
    }
    MessageInfo info = new MessageInfo();
    info.bytes = bytesToSend;
    if (saveMessage) {
        try {
            PduPersister persister = PduPersister.getPduPersister(context);
            info.location = persister.persist(sendRequest, Uri.parse("content://mms/outbox"), true, settings.getGroup(), null);
        } catch (Exception e) {
            if (LOCAL_LOGV)
                Log.v(TAG, "error saving mms message");
            Log.e(TAG, "exception thrown", e);
            // use the old way if something goes wrong with the persister
            insert(context, recipients, parts, subject);
        }
    }
    try {
        Cursor query = context.getContentResolver().query(info.location, new String[] { "thread_id" }, null, null, null);
        if (query != null && query.moveToFirst()) {
            info.token = query.getLong(query.getColumnIndex("thread_id"));
        } else {
            // just default sending token for what I had before
            info.token = 4444L;
        }
    } catch (Exception e) {
        Log.e(TAG, "exception thrown", e);
        info.token = 4444L;
    }
    return info;
}
Also used : EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) PduBody(com.google.android.mms.pdu_alt.PduBody) PduPersister(com.google.android.mms.pdu_alt.PduPersister) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cursor(android.database.Cursor) SendReq(com.google.android.mms.pdu_alt.SendReq) MmsException(com.google.android.mms.MmsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PduComposer(com.google.android.mms.pdu_alt.PduComposer) MmsException(com.google.android.mms.MmsException) PduPart(com.google.android.mms.pdu_alt.PduPart) MMSPart(com.google.android.mms.MMSPart)

Example 12 with PduPart

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

the class ImageModel method resizeMedia.

@Override
protected void resizeMedia(int byteLimit, long messageId) throws MmsException {
    UriImage image = new UriImage(mContext, getUri());
    int widthLimit = MmsConfig.getMaxImageWidth();
    int heightLimit = MmsConfig.getMaxImageHeight();
    int size = getMediaSize();
    // possible.
    if (image.getHeight() > image.getWidth()) {
        int temp = widthLimit;
        widthLimit = heightLimit;
        heightLimit = temp;
    }
    if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
        Log.v(TAG, "resizeMedia size: " + size + " image.getWidth(): " + image.getWidth() + " widthLimit: " + widthLimit + " image.getHeight(): " + image.getHeight() + " heightLimit: " + heightLimit + " image.getContentType(): " + image.getContentType());
    }
    // set the size.
    if (size != 0 && size <= byteLimit && image.getWidth() <= widthLimit && image.getHeight() <= heightLimit && SUPPORTED_MMS_IMAGE_CONTENT_TYPES.contains(image.getContentType())) {
        if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
            Log.v(TAG, "resizeMedia - already sized");
        }
        return;
    }
    PduPart part = image.getResizedImageAsPart(widthLimit, heightLimit, byteLimit);
    if (part == null) {
        throw new ExceedMessageSizeException("Not enough memory to turn image into part: " + getUri());
    }
    // Update the content type because it may have changed due to resizing/recompressing
    mContentType = new String(part.getContentType());
    String src = getSrc();
    byte[] srcBytes = src.getBytes();
    part.setContentLocation(srcBytes);
    int period = src.lastIndexOf(".");
    byte[] contentId = period != -1 ? src.substring(0, period).getBytes() : srcBytes;
    part.setContentId(contentId);
    PduPersister persister = PduPersister.getPduPersister(mContext);
    this.mSize = part.getData().length;
    if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
        Log.v(TAG, "resizeMedia mSize: " + mSize);
    }
    Uri newUri = persister.persistPart(part, messageId, null);
    setUri(newUri);
}
Also used : UriImage(com.moez.QKSMS.common.google.UriImage) PduPersister(com.google.android.mms.pdu_alt.PduPersister) PduPart(com.google.android.mms.pdu_alt.PduPart) ExceedMessageSizeException(com.moez.QKSMS.ExceedMessageSizeException) Uri(android.net.Uri)

Example 13 with PduPart

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

the class MessageUtils method saveRingtone.

/**
 * Copies media from an Mms to the DrmProvider
 *
 * @param context
 * @param msgId
 */
public static boolean saveRingtone(Context context, long msgId) {
    boolean result = true;
    PduBody body = null;
    try {
        body = SlideshowModel.getPduBody(context, ContentUris.withAppendedId(Mms.CONTENT_URI, msgId));
    } catch (MmsException e) {
        Log.e(TAG, "copyToDrmProvider can't load pdu body: " + msgId);
    }
    if (body == null) {
        return false;
    }
    int partNum = body.getPartsNum();
    for (int i = 0; i < partNum; i++) {
        PduPart part = body.getPart(i);
        String type = new String(part.getContentType());
        if (DrmUtils.isDrmType(type)) {
            // All parts (but there's probably only a single one) have to be successful
            // for a valid result.
            result &= copyPart(context, part, Long.toHexString(msgId));
        }
    }
    return result;
}
Also used : MmsException(com.google.android.mms.MmsException) PduBody(com.google.android.mms.pdu_alt.PduBody) PduPart(com.google.android.mms.pdu_alt.PduPart)

Example 14 with PduPart

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

the class MessageUtils method copyMedia.

/**
 * Copies media from an Mms to the "download" directory on the SD card. If any of the parts
 * are audio types, drm'd or not, they're copied to the "Ringtones" directory.
 *
 * @param context
 * @param msgId
 */
public static boolean copyMedia(Context context, long msgId) {
    boolean result = true;
    PduBody body = null;
    try {
        body = SlideshowModel.getPduBody(context, ContentUris.withAppendedId(Mms.CONTENT_URI, msgId));
    } catch (MmsException e) {
        Log.e(TAG, "copyMedia can't load pdu body: " + msgId);
    }
    if (body == null) {
        return false;
    }
    int partNum = body.getPartsNum();
    for (int i = 0; i < partNum; i++) {
        PduPart part = body.getPart(i);
        // all parts have to be successful for a valid result.
        result &= copyPart(context, part, Long.toHexString(msgId));
    }
    return result;
}
Also used : MmsException(com.google.android.mms.MmsException) PduBody(com.google.android.mms.pdu_alt.PduBody) PduPart(com.google.android.mms.pdu_alt.PduPart)

Example 15 with PduPart

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

the class MessageUtils method isForwardable.

/**
 * Returns true if all drm'd parts are forwardable.
 *
 * @param context
 * @param msgId
 * @return true if all drm'd parts are forwardable.
 */
public static boolean isForwardable(Context context, long msgId) {
    PduBody body = null;
    try {
        body = SlideshowModel.getPduBody(context, ContentUris.withAppendedId(Mms.CONTENT_URI, msgId));
    } catch (MmsException e) {
        Log.e(TAG, "getDrmMimeType can't load pdu body: " + msgId);
    }
    if (body == null) {
        return false;
    }
    int partNum = body.getPartsNum();
    for (int i = 0; i < partNum; i++) {
        PduPart part = body.getPart(i);
        String type = new String(part.getContentType());
        if (DrmUtils.isDrmType(type) && !DrmUtils.haveRightsForAction(part.getDataUri(), DrmStore.Action.TRANSFER)) {
            return false;
        }
    }
    return true;
}
Also used : MmsException(com.google.android.mms.MmsException) PduBody(com.google.android.mms.pdu_alt.PduBody) PduPart(com.google.android.mms.pdu_alt.PduPart)

Aggregations

PduPart (com.google.android.mms.pdu_alt.PduPart)21 PduBody (com.google.android.mms.pdu_alt.PduBody)12 MmsException (com.google.android.mms.MmsException)7 EncodedStringValue (com.google.android.mms.pdu_alt.EncodedStringValue)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SendReq (com.google.android.mms.pdu_alt.SendReq)4 IOException (java.io.IOException)4 Attachment (org.thoughtcrime.securesms.attachments.Attachment)4 Uri (android.net.Uri)3 InvalidHeaderValueException (com.google.android.mms.InvalidHeaderValueException)3 SMILDocument (org.w3c.dom.smil.SMILDocument)3 SmilDocumentImpl (com.android.mms.dom.smil.SmilDocumentImpl)2 MMSPart (com.google.android.mms.MMSPart)2 PduPersister (com.google.android.mms.pdu_alt.PduPersister)2 UriImage (com.moez.QKSMS.common.google.UriImage)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 UriAttachment (org.thoughtcrime.securesms.attachments.UriAttachment)2 Address (org.thoughtcrime.securesms.database.Address)2