Search in sources :

Example 1 with PduBody

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

the class SlideshowModel method makePduBody.

private PduBody makePduBody(SMILDocument document) {
    PduBody pb = new PduBody();
    boolean hasForwardLock = false;
    for (SlideModel slide : mSlides) {
        for (MediaModel media : slide) {
            PduPart part = new PduPart();
            if (media.isText()) {
                TextModel text = (TextModel) media;
                // Don't create empty text part.
                if (TextUtils.isEmpty(text.getText())) {
                    continue;
                }
                // Set Charset if it's a text media.
                part.setCharset(text.getCharset());
            }
            // Set Content-Type.
            part.setContentType(media.getContentType().getBytes());
            String src = media.getSrc();
            String location;
            boolean startWithContentId = src.startsWith("cid:");
            if (startWithContentId) {
                location = src.substring("cid:".length());
            } else {
                location = src;
            }
            // Set Content-Location.
            part.setContentLocation(location.getBytes());
            // Set Content-Id.
            if (startWithContentId) {
                // Keep the original Content-Id.
                part.setContentId(location.getBytes());
            } else {
                int index = location.lastIndexOf(".");
                String contentId = (index == -1) ? location : location.substring(0, index);
                part.setContentId(contentId.getBytes());
            }
            if (media.isText()) {
                part.setData(((TextModel) media).getText().getBytes());
            } else if (media.isImage() || media.isVideo() || media.isAudio()) {
                part.setDataUri(media.getUri());
            } else {
                Log.w(TAG, "Unsupport media: " + media);
            }
            pb.addPart(part);
        }
    }
    // Create and insert SMIL part(as the first part) into the PduBody.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SmilXmlSerializer.serialize(document, out);
    PduPart smilPart = new PduPart();
    smilPart.setContentId("smil".getBytes());
    smilPart.setContentLocation("smil.xml".getBytes());
    smilPart.setContentType(ContentType.APP_SMIL.getBytes());
    smilPart.setData(out.toByteArray());
    pb.addPart(0, smilPart);
    return pb;
}
Also used : PduBody(com.google.android.mms.pdu_alt.PduBody) PduPart(com.google.android.mms.pdu_alt.PduPart) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with PduBody

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

the class MessageUtils method isDrmRingtoneWithRights.

/**
 * Returns true if any part is drm'd audio with ringtone rights.
 *
 * @param context
 * @param msgId
 * @return true if one of the parts is drm'd audio with rights to save as a ringtone.
 */
public static boolean isDrmRingtoneWithRights(Context context, long msgId) {
    PduBody body = null;
    try {
        body = SlideshowModel.getPduBody(context, ContentUris.withAppendedId(Mms.CONTENT_URI, msgId));
    } catch (MmsException e) {
        Log.e(TAG, "isDrmRingtoneWithRights 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)) {
            String mimeType = QKSMSApp.getApplication().getDrmManagerClient().getOriginalMimeType(part.getDataUri());
            if (ContentType.isAudioType(mimeType) && DrmUtils.haveRightsForAction(part.getDataUri(), DrmStore.Action.RINGTONE)) {
                return true;
            }
        }
    }
    return false;
}
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 3 with PduBody

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

the class MessageUtils method haveSomethingToCopyToSDCard.

/**
 * Looks to see if there are any valid parts of the attachment that can be copied to a SD card.
 *
 * @param context
 * @param msgId
 */
public static boolean haveSomethingToCopyToSDCard(Context context, long msgId) {
    PduBody body = null;
    try {
        body = SlideshowModel.getPduBody(context, ContentUris.withAppendedId(Mms.CONTENT_URI, msgId));
    } catch (MmsException e) {
        Log.e(TAG, "haveSomethingToCopyToSDCard can't load pdu body: " + msgId);
    }
    if (body == null) {
        return false;
    }
    boolean result = false;
    int partNum = body.getPartsNum();
    for (int i = 0; i < partNum; i++) {
        PduPart part = body.getPart(i);
        String type = new String(part.getContentType());
        if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
            Log.v(TAG, "[CMA] haveSomethingToCopyToSDCard: part[" + i + "] contentType=" + type);
        }
        if (ContentType.isImageType(type) || ContentType.isVideoType(type) || ContentType.isAudioType(type) || DrmUtils.isDrmType(type)) {
            result = true;
            break;
        }
    }
    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 4 with PduBody

use of com.google.android.mms.pdu_alt.PduBody in project Signal-Android by signalapp.

the class MmsDownloadJob method storeRetrievedMms.

private void storeRetrievedMms(String contentLocation, long messageId, long threadId, RetrieveConf retrieved, int subscriptionId, @Nullable Address notificationFrom) throws MmsException, NoSessionException, DuplicateMessageException, InvalidMessageException, LegacyMessageException {
    MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
    SingleUseBlobProvider provider = SingleUseBlobProvider.getInstance();
    Optional<Address> group = Optional.absent();
    Set<Address> members = new HashSet<>();
    String body = null;
    List<Attachment> attachments = new LinkedList<>();
    Address from;
    if (retrieved.getFrom() != null) {
        from = Address.fromExternal(context, Util.toIsoString(retrieved.getFrom().getTextString()));
    } else if (notificationFrom != null) {
        from = notificationFrom;
    } else {
        from = Address.UNKNOWN;
    }
    if (retrieved.getTo() != null) {
        for (EncodedStringValue toValue : retrieved.getTo()) {
            members.add(Address.fromExternal(context, Util.toIsoString(toValue.getTextString())));
        }
    }
    if (retrieved.getCc() != null) {
        for (EncodedStringValue ccValue : retrieved.getCc()) {
            members.add(Address.fromExternal(context, Util.toIsoString(ccValue.getTextString())));
        }
    }
    members.add(from);
    members.add(Address.fromExternal(context, TextSecurePreferences.getLocalNumber(context)));
    if (retrieved.getBody() != null) {
        body = PartParser.getMessageText(retrieved.getBody());
        PduBody media = PartParser.getSupportedMediaParts(retrieved.getBody());
        for (int i = 0; i < media.getPartsNum(); i++) {
            PduPart part = media.getPart(i);
            if (part.getData() != null) {
                Uri uri = provider.createUri(part.getData());
                String name = null;
                if (part.getName() != null)
                    name = Util.toIsoString(part.getName());
                attachments.add(new UriAttachment(uri, Util.toIsoString(part.getContentType()), AttachmentDatabase.TRANSFER_PROGRESS_DONE, part.getData().length, name, false));
            }
        }
    }
    if (members.size() > 2) {
        group = Optional.of(Address.fromSerialized(DatabaseFactory.getGroupDatabase(context).getOrCreateGroupForMembers(new LinkedList<>(members), true)));
    }
    IncomingMediaMessage message = new IncomingMediaMessage(from, group, body, retrieved.getDate() * 1000L, attachments, subscriptionId, 0, false);
    Optional<InsertResult> insertResult = database.insertMessageInbox(message, contentLocation, threadId);
    if (insertResult.isPresent()) {
        database.delete(messageId);
        MessageNotifier.updateNotification(context, insertResult.get().getThreadId());
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult) EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) Address(org.thoughtcrime.securesms.database.Address) PduBody(com.google.android.mms.pdu_alt.PduBody) IncomingMediaMessage(org.thoughtcrime.securesms.mms.IncomingMediaMessage) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) Uri(android.net.Uri) LinkedList(java.util.LinkedList) SingleUseBlobProvider(org.thoughtcrime.securesms.providers.SingleUseBlobProvider) PduPart(com.google.android.mms.pdu_alt.PduPart) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) MmsDatabase(org.thoughtcrime.securesms.database.MmsDatabase) HashSet(java.util.HashSet)

Example 5 with PduBody

use of com.google.android.mms.pdu_alt.PduBody 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)

Aggregations

PduBody (com.google.android.mms.pdu_alt.PduBody)13 PduPart (com.google.android.mms.pdu_alt.PduPart)12 MmsException (com.google.android.mms.MmsException)6 EncodedStringValue (com.google.android.mms.pdu_alt.EncodedStringValue)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SendReq (com.google.android.mms.pdu_alt.SendReq)4 Attachment (org.thoughtcrime.securesms.attachments.Attachment)4 InvalidHeaderValueException (com.google.android.mms.InvalidHeaderValueException)3 IOException (java.io.IOException)3 Uri (android.net.Uri)2 MMSPart (com.google.android.mms.MMSPart)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 IncomingMediaMessage (org.thoughtcrime.securesms.mms.IncomingMediaMessage)2 MediaConstraints (org.thoughtcrime.securesms.mms.MediaConstraints)2 Recipient (org.thoughtcrime.securesms.recipients.Recipient)2 Cursor (android.database.Cursor)1 PduComposer (com.google.android.mms.pdu_alt.PduComposer)1