Search in sources :

Example 16 with PduPart

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

the class UriImage method getResizedImageAsPart.

/**
 * Get a version of this image resized to fit the given dimension and byte-size limits. Note
 * that the content type of the resulting PduPart may not be the same as the content type of
 * this UriImage; always call @link PduPart#getContentType() to get the new content type.
 *
 * @param widthLimit The width limit, in pixels
 * @param heightLimit The height limit, in pixels
 * @param byteLimit The binary size limit, in bytes
 * @return A new PduPart containing the resized image data
 */
public PduPart getResizedImageAsPart(int widthLimit, int heightLimit, int byteLimit) {
    PduPart part = new PduPart();
    byte[] data = getResizedImageData(mWidth, mHeight, widthLimit, heightLimit, byteLimit, mUri, mContext);
    if (data == null) {
        if (LOCAL_LOGV) {
            Log.v(TAG, "Resize image failed.");
        }
        return null;
    }
    part.setData(data);
    // getResizedImageData ALWAYS compresses to JPEG, regardless of the original content type
    part.setContentType(ContentType.IMAGE_JPEG.getBytes());
    return part;
}
Also used : PduPart(com.google.android.mms.pdu_alt.PduPart)

Example 17 with PduPart

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

the class MmsSendJob method constructSendPdu.

private SendReq constructSendPdu(OutgoingMediaMessage message) throws UndeliverableMessageException {
    SendReq req = new SendReq();
    String lineNumber = getMyNumber(context);
    Address destination = message.getRecipient().getAddress();
    MediaConstraints mediaConstraints = MediaConstraints.getMmsMediaConstraints(message.getSubscriptionId());
    List<Attachment> scaledAttachments = scaleAttachments(mediaConstraints, message.getAttachments());
    if (!TextUtils.isEmpty(lineNumber)) {
        req.setFrom(new EncodedStringValue(lineNumber));
    } else {
        req.setFrom(new EncodedStringValue(TextSecurePreferences.getLocalNumber(context)));
    }
    if (destination.isMmsGroup()) {
        List<Recipient> members = DatabaseFactory.getGroupDatabase(context).getGroupMembers(destination.toGroupString(), false);
        for (Recipient member : members) {
            if (message.getDistributionType() == ThreadDatabase.DistributionTypes.BROADCAST) {
                req.addBcc(new EncodedStringValue(member.getAddress().serialize()));
            } else {
                req.addTo(new EncodedStringValue(member.getAddress().serialize()));
            }
        }
    } else {
        req.addTo(new EncodedStringValue(destination.serialize()));
    }
    req.setDate(System.currentTimeMillis() / 1000);
    PduBody body = new PduBody();
    int size = 0;
    if (!TextUtils.isEmpty(message.getBody())) {
        PduPart part = new PduPart();
        String name = String.valueOf(System.currentTimeMillis());
        part.setData(Util.toUtf8Bytes(message.getBody()));
        part.setCharset(CharacterSets.UTF_8);
        part.setContentType(ContentType.TEXT_PLAIN.getBytes());
        part.setContentId(name.getBytes());
        part.setContentLocation((name + ".txt").getBytes());
        part.setName((name + ".txt").getBytes());
        body.addPart(part);
        size += getPartSize(part);
    }
    for (Attachment attachment : scaledAttachments) {
        try {
            if (attachment.getDataUri() == null)
                throw new IOException("Assertion failed, attachment for outgoing MMS has no data!");
            String fileName = attachment.getFileName();
            PduPart part = new PduPart();
            if (fileName == null) {
                fileName = String.valueOf(Math.abs(Util.getSecureRandom().nextLong()));
                String fileExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType(attachment.getContentType());
                if (fileExtension != null)
                    fileName = fileName + "." + fileExtension;
            }
            if (attachment.getContentType().startsWith("text")) {
                part.setCharset(CharacterSets.UTF_8);
            }
            part.setContentType(attachment.getContentType().getBytes());
            part.setContentLocation(fileName.getBytes());
            part.setName(fileName.getBytes());
            int index = fileName.lastIndexOf(".");
            String contentId = (index == -1) ? fileName : fileName.substring(0, index);
            part.setContentId(contentId.getBytes());
            part.setData(Util.readFully(PartAuthority.getAttachmentStream(context, attachment.getDataUri())));
            body.addPart(part);
            size += getPartSize(part);
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SmilXmlSerializer.serialize(SmilHelper.createSmilDocument(body), out);
    PduPart smilPart = new PduPart();
    smilPart.setContentId("smil".getBytes());
    smilPart.setContentLocation("smil.xml".getBytes());
    smilPart.setContentType(ContentType.APP_SMIL.getBytes());
    smilPart.setData(out.toByteArray());
    body.addPart(0, smilPart);
    req.setBody(body);
    req.setMessageSize(size);
    req.setMessageClass(PduHeaders.MESSAGE_CLASS_PERSONAL_STR.getBytes());
    req.setExpiry(7 * 24 * 60 * 60);
    try {
        req.setPriority(PduHeaders.PRIORITY_NORMAL);
        req.setDeliveryReport(PduHeaders.VALUE_NO);
        req.setReadReport(PduHeaders.VALUE_NO);
    } catch (InvalidHeaderValueException e) {
    }
    return req;
}
Also used : EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) Address(org.thoughtcrime.securesms.database.Address) PduBody(com.google.android.mms.pdu_alt.PduBody) Attachment(org.thoughtcrime.securesms.attachments.Attachment) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SendReq(com.google.android.mms.pdu_alt.SendReq) MediaConstraints(org.thoughtcrime.securesms.mms.MediaConstraints) InvalidHeaderValueException(com.google.android.mms.InvalidHeaderValueException) PduPart(com.google.android.mms.pdu_alt.PduPart)

Example 18 with PduPart

use of com.google.android.mms.pdu_alt.PduPart in project kdeconnect-android by KDE.

the class SmsMmsUtils method buildPdu.

/**
 * Copy of the same-name method from https://github.com/klinker41/android-smsmms
 */
private static SendReq buildPdu(Context context, String fromAddress, String[] recipients, String subject, List<MMSPart> parts, Settings settings) {
    final SendReq req = new SendReq();
    // From, per spec
    req.prepareFromAddress(context, fromAddress, settings.getSubscriptionId());
    // To
    for (String recipient : recipients) {
        req.addTo(new EncodedStringValue(recipient));
    }
    // Subject
    if (!TextUtils.isEmpty(subject)) {
        req.setSubject(new EncodedStringValue(subject));
    }
    // Date
    req.setDate(System.currentTimeMillis() / 1000);
    // Body
    PduBody body = new PduBody();
    // Add text part. Always add a smil part for compatibility, without it there
    // may be issues on some carriers/client apps
    int size = 0;
    for (int i = 0; i < parts.size(); i++) {
        MMSPart part = parts.get(i);
        size += addTextPart(body, part, i);
    }
    // add a SMIL document for compatibility
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SmilXmlSerializer.serialize(SmilHelper.createSmilDocument(body), out);
    PduPart smilPart = new PduPart();
    smilPart.setContentId("smil".getBytes());
    smilPart.setContentLocation("smil.xml".getBytes());
    smilPart.setContentType(ContentType.APP_SMIL.getBytes());
    smilPart.setData(out.toByteArray());
    body.addPart(0, smilPart);
    req.setBody(body);
    // Message size
    req.setMessageSize(size);
    // Message class
    req.setMessageClass(PduHeaders.MESSAGE_CLASS_PERSONAL_STR.getBytes());
    // Expiry
    req.setExpiry(DEFAULT_EXPIRY_TIME);
    try {
        // Priority
        req.setPriority(DEFAULT_PRIORITY);
        // Delivery report
        req.setDeliveryReport(PduHeaders.VALUE_NO);
        // Read report
        req.setReadReport(PduHeaders.VALUE_NO);
    } catch (InvalidHeaderValueException e) {
    }
    return req;
}
Also used : EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) PduBody(com.google.android.mms.pdu_alt.PduBody) InvalidHeaderValueException(com.google.android.mms.InvalidHeaderValueException) PduPart(com.google.android.mms.pdu_alt.PduPart) MMSPart(com.google.android.mms.MMSPart) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SendReq(com.google.android.mms.pdu_alt.SendReq)

Example 19 with PduPart

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

the class SmilHelper method createSmilDocument.

public static SMILDocument createSmilDocument(PduBody pb) {
    SMILDocument document = new SmilDocumentImpl();
    // Create root element.
    SMILElement smil = (SMILElement) document.createElement("smil");
    smil.setAttribute("xmlns", "http://www.w3.org/2001/SMIL20/Language");
    document.appendChild(smil);
    // Create <head> and <layout> element.
    SMILElement head = (SMILElement) document.createElement("head");
    smil.appendChild(head);
    SMILLayoutElement layout = (SMILLayoutElement) document.createElement("layout");
    head.appendChild(layout);
    // Create <body> element and add a empty <par>.
    SMILElement body = (SMILElement) document.createElement("body");
    smil.appendChild(body);
    SMILParElement par = addPar(document);
    // Create media objects for the parts in PDU.
    int partsNum = pb.getPartsNum();
    if (partsNum == 0) {
        return document;
    }
    boolean hasText = false;
    boolean hasMedia = false;
    for (int i = 0; i < partsNum; i++) {
        // Create new <par> element.
        if ((par == null) || (hasMedia && hasText)) {
            par = addPar(document);
            hasText = false;
            hasMedia = false;
        }
        PduPart part = pb.getPart(i);
        String contentType = new String(part.getContentType());
        if (contentType.equals(ContentType.TEXT_PLAIN) || contentType.equalsIgnoreCase(ContentType.APP_WAP_XHTML) || contentType.equals(ContentType.TEXT_HTML)) {
            SMILMediaElement textElement = createMediaElement(ELEMENT_TAG_TEXT, document, part.generateLocation());
            par.appendChild(textElement);
            hasText = true;
        } else if (ContentType.isImageType(contentType)) {
            SMILMediaElement imageElement = createMediaElement(ELEMENT_TAG_IMAGE, document, part.generateLocation());
            par.appendChild(imageElement);
            hasMedia = true;
        } else if (ContentType.isVideoType(contentType)) {
            SMILMediaElement videoElement = createMediaElement(ELEMENT_TAG_VIDEO, document, part.generateLocation());
            par.appendChild(videoElement);
            hasMedia = true;
        } else if (ContentType.isAudioType(contentType)) {
            SMILMediaElement audioElement = createMediaElement(ELEMENT_TAG_AUDIO, document, part.generateLocation());
            par.appendChild(audioElement);
            hasMedia = true;
        } else if (contentType.equals(ContentType.TEXT_VCARD)) {
            SMILMediaElement textElement = createMediaElement(ELEMENT_TAG_VCARD, document, part.generateLocation());
            par.appendChild(textElement);
            hasMedia = true;
        } else {
            Timber.e("creating_smil_document", "unknown mimetype");
        }
    }
    return document;
}
Also used : SMILMediaElement(org.w3c.dom.smil.SMILMediaElement) SMILDocument(org.w3c.dom.smil.SMILDocument) SmilDocumentImpl(com.android.mms.dom.smil.SmilDocumentImpl) SMILLayoutElement(org.w3c.dom.smil.SMILLayoutElement) SMILParElement(org.w3c.dom.smil.SMILParElement) PduPart(com.google.android.mms.pdu_alt.PduPart) SMILElement(org.w3c.dom.smil.SMILElement)

Example 20 with PduPart

use of com.google.android.mms.pdu_alt.PduPart in project Signal-Android by WhisperSystems.

the class MmsDownloadJob method storeRetrievedMms.

private void storeRetrievedMms(String contentLocation, long messageId, long threadId, RetrieveConf retrieved, int subscriptionId, @Nullable RecipientId notificationFrom) throws MmsException {
    MessageDatabase database = SignalDatabase.mms();
    Optional<GroupId> group = Optional.absent();
    Set<RecipientId> members = new HashSet<>();
    String body = null;
    List<Attachment> attachments = new LinkedList<>();
    List<Contact> sharedContacts = new LinkedList<>();
    RecipientId from = null;
    if (retrieved.getFrom() != null) {
        from = Recipient.external(context, Util.toIsoString(retrieved.getFrom().getTextString())).getId();
    } else if (notificationFrom != null) {
        from = notificationFrom;
    }
    if (retrieved.getTo() != null) {
        for (EncodedStringValue toValue : retrieved.getTo()) {
            members.add(Recipient.external(context, Util.toIsoString(toValue.getTextString())).getId());
        }
    }
    if (retrieved.getCc() != null) {
        for (EncodedStringValue ccValue : retrieved.getCc()) {
            members.add(Recipient.external(context, Util.toIsoString(ccValue.getTextString())).getId());
        }
    }
    if (from != null) {
        members.add(from);
    }
    members.add(Recipient.self().getId());
    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) {
                if (Util.toIsoString(part.getContentType()).toLowerCase().equals(MediaUtil.VCARD)) {
                    sharedContacts.addAll(VCardUtil.parseContacts(new String(part.getData())));
                } else {
                    Uri uri = BlobProvider.getInstance().forData(part.getData()).createForSingleUseInMemory();
                    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, false, false, false, null, null, null, null, null));
                }
            }
        }
    }
    if (members.size() > 2) {
        List<RecipientId> recipients = new ArrayList<>(members);
        group = Optional.of(SignalDatabase.groups().getOrCreateMmsGroupForMembers(recipients));
    }
    IncomingMediaMessage message = new IncomingMediaMessage(from, group, body, TimeUnit.SECONDS.toMillis(retrieved.getDate()), -1, System.currentTimeMillis(), attachments, subscriptionId, 0, false, false, false, Optional.of(sharedContacts));
    Optional<InsertResult> insertResult = database.insertMessageInbox(message, contentLocation, threadId);
    if (insertResult.isPresent()) {
        database.deleteMessage(messageId);
        ApplicationDependencies.getMessageNotifier().updateNotification(context, insertResult.get().getThreadId());
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessageDatabase.InsertResult) MessageDatabase(org.thoughtcrime.securesms.database.MessageDatabase) EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) PduBody(com.google.android.mms.pdu_alt.PduBody) IncomingMediaMessage(org.thoughtcrime.securesms.mms.IncomingMediaMessage) ArrayList(java.util.ArrayList) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) Uri(android.net.Uri) LinkedList(java.util.LinkedList) GroupId(org.thoughtcrime.securesms.groups.GroupId) Contact(org.thoughtcrime.securesms.contactshare.Contact) PduPart(com.google.android.mms.pdu_alt.PduPart) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) HashSet(java.util.HashSet)

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