Search in sources :

Example 1 with PduPart

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

the class SmilHelper method getDocument.

public static SMILDocument getDocument(PduBody pb) {
    // Find SMIL part in the message.
    PduPart smilPart = findSmilPart(pb);
    SMILDocument document = null;
    // Try to load SMIL document from existing part.
    if (smilPart != null) {
        document = getSmilDocument(smilPart);
    }
    if (document == null) {
        // Create a new SMIL document.
        document = createSmilDocument(pb);
    }
    return document;
}
Also used : SMILDocument(org.w3c.dom.smil.SMILDocument) PduPart(com.google.android.mms.pdu_alt.PduPart)

Example 2 with PduPart

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

the class MediaModelFactory method findPart.

/**
 * This method is meant to identify the part in the given PduBody that corresponds to the given
 * src string.
 *
 * Essentially, a SMIL MMS is formatted as follows:
 *
 * 1. A smil/application part, which contains XML-like formatting for images, text, audio,
 * slideshows, videos, etc.
 * 2. One or more parts that correspond to one of the elements that was mentioned in the
 * formatting above.
 *
 * In the smil/application part, elements are identified by a "src" attribute in an XML-like
 * element. The challenge of this method lies in the fact that sometimes, the src string isn't
 * located at all in the part that it is meant to identify.
 *
 * We employ several methods of pairing src strings up to parts, using certain patterns we've
 * seen in failed MMS messages. These are described in this method.
 * TODO TODO TODO: Create a testing suite for this!
 */
private static PduPart findPart(final Context context, PduBody pb, String src, ArrayList<String> srcs) {
    PduPart result = null;
    if (src != null) {
        src = unescapeXML(src);
        // Sometimes, src takes the form of "cid:[NUMBER]".
        if (contentIdSrc(src)) {
            // Extract the content ID, and try finding the part using that.
            result = pb.getPartByContentId("<" + src.substring("cid:".length()) + ">");
            if (result == null) {
                // Another thing that can happen is that there is a slideshow of images, each with
                // "cid:[NUMBER]" src, but the parts aren't labelled with the content ID. If
                // this is the case, then we just return the ith image part, where i is the position
                // of src if all the srcs are sorted in ascending order as content IDs. srcs may
                // include duplicates; we remove those and then identify i when the list is free from
                // duplicates.
                // 
                // i.e., for srcs = [ "cid:755", "cid:755", "cid:756", "cid:757", "cid:758" ],
                // the i of "cid:755" is 0; for "cid:756" is 1, etc.
                // First check that all the src strings are content IDs.
                boolean allContentIDs = true;
                for (String _src : srcs) {
                    if (!contentIdSrc(_src)) {
                        allContentIDs = false;
                        break;
                    }
                }
                if (allContentIDs) {
                    // Now, build a list of long IDs, sort them, and remove the duplicates.
                    ArrayList<Long> cids = new ArrayList<>();
                    for (String _src : srcs) {
                        cids.add(getContentId(_src));
                    }
                    Collections.sort(cids);
                    int removed = 0;
                    long previous = -1;
                    for (int i = 0; i < cids.size() - removed; i++) {
                        long cid = cids.get(i);
                        if (cid == previous) {
                            cids.remove(i);
                            removed++;
                        } else {
                            previous = cid;
                        }
                    }
                    // Find the i such that getContentId(src) == cids[i]
                    long cid = getContentId(src);
                    int i = cids.indexOf(cid);
                    // 1 + i.
                    if (1 + i < pb.getPartsNum()) {
                        result = pb.getPart(i + 1);
                    }
                }
            }
        } else if (textSrc(src)) {
            // content type.
            for (int i = 0; i < pb.getPartsNum(); i++) {
                PduPart part = pb.getPart(i);
                String contentType = byteArrayToString(part.getContentType());
                if ("text/plain".equals(contentType)) {
                    result = part;
                    break;
                }
            }
        }
        // Search by name
        if (result == null) {
            result = pb.getPartByName(src);
        }
        // Search by filename
        if (result == null) {
            result = pb.getPartByFileName(src);
        }
        // Search by content location
        if (result == null) {
            result = pb.getPartByContentLocation(src);
        }
        // Try treating the src string as a content ID, and searching by content ID.
        if (result == null) {
            result = pb.getPartByContentId("<" + src + ">");
        }
    // TODO:
    // four remaining cases currently in Firebase:
    // 1. src: "image:[NUMBER]" (broken formatting)
    // 2. src: "[NUMBER]" (broken formatting)
    // 3. src: "[name].vcf" (we don't support x-vcard)
    // 3. src: "Current Location.loc.vcf" (we don't support x-vcard)
    }
    if (result != null) {
        return result;
    }
    if (pb.getPartsNum() > 0) {
        final JSONArray array = new JSONArray();
        for (int i = 0; i < pb.getPartsNum(); i++) {
            JSONObject object = new JSONObject();
            try {
                object.put("part_number", i);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("location", i);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("charset", pb.getPart(i).getCharset());
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("content_disposition", byteArrayToString(pb.getPart(i).getContentDisposition()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("content_id", byteArrayToString(pb.getPart(i).getContentId()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("content_location", byteArrayToString(pb.getPart(i).getContentLocation()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("content_transfer_encoding", byteArrayToString(pb.getPart(i).getContentTransferEncoding()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("content_type", byteArrayToString(pb.getPart(i).getContentType()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("data", byteArrayToString(pb.getPart(i).getData()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("data_uri", pb.getPart(i).getDataUri());
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("file_name", byteArrayToString(pb.getPart(i).getFilename()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                object.put("name", byteArrayToString(pb.getPart(i).getName()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (pb.getPart(i).generateLocation() != null) {
                Log.d(TAG, "Location: " + pb.getPart(i).generateLocation());
                if (pb.getPart(i).generateLocation().contains(src)) {
                    return pb.getPart(i);
                }
            }
            array.put(object);
        }
    }
    throw new IllegalArgumentException("No part found for the model.");
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) PduPart(com.google.android.mms.pdu_alt.PduPart) MmsException(com.google.android.mms.MmsException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with PduPart

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

the class MediaModelFactory method getMediaModel.

/**
 * Returns the media model for the given SMILMediaElement in the PduBody.
 *
 * @param context Context
 * @param sme     The SMILMediaElement to find
 * @param srcs    String array of sources
 * @param layouts LayoutModel
 * @param pb      PduBuddy
 * @return MediaModel
 * @throws IOException
 * @throws IllegalArgumentException
 * @throws MmsException
 */
public static MediaModel getMediaModel(Context context, SMILMediaElement sme, ArrayList<String> srcs, LayoutModel layouts, PduBody pb) throws IOException, IllegalArgumentException, MmsException {
    String tag = sme.getTagName();
    String src = sme.getSrc();
    PduPart part = findPart(context, pb, src, srcs);
    if (sme instanceof SMILRegionMediaElement) {
        return getRegionMediaModel(context, tag, src, (SMILRegionMediaElement) sme, layouts, part);
    } else {
        return getGenericMediaModel(context, tag, src, sme, part, null);
    }
}
Also used : PduPart(com.google.android.mms.pdu_alt.PduPart) SMILRegionMediaElement(org.w3c.dom.smil.SMILRegionMediaElement)

Example 4 with PduPart

use of com.google.android.mms.pdu_alt.PduPart 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 5 with PduPart

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

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