Signal-Android/src/org/thoughtcrime/securesms/mms/PartParser.java
Moxie Marlinspike 51d6144591 Significant MMS changes
1) Remove all our PDU code and switch to the PDU code from the
   klinker library

2) Switch to using the system Lollipop MMS library by default,
   and falling back to our own custom library if that fails.

3) Format SMIL differently, using code from klinker instead of
   what we've pieced together.

4) Pull per-carrier MMS media constraints from the XML config
   files in the klinker library, instead of hardcoding it at 280kb.

Hopefully this is an improvement, but given that MMS is involved,
it will probably make things worse instead.
2017-05-08 18:14:55 -07:00

89 lines
2.4 KiB
Java

package org.thoughtcrime.securesms.mms;
import android.util.Log;
import com.google.android.mms.ContentType;
import com.google.android.mms.pdu_alt.CharacterSets;
import com.google.android.mms.pdu_alt.PduBody;
import com.google.android.mms.pdu_alt.PduPart;
import org.thoughtcrime.securesms.util.Util;
import java.io.UnsupportedEncodingException;
public class PartParser {
public static String getMessageText(PduBody body) {
String bodyText = null;
for (int i=0;i<body.getPartsNum();i++) {
if (ContentType.isTextType(Util.toIsoString(body.getPart(i).getContentType()))) {
String partText;
try {
String characterSet = CharacterSets.getMimeName(body.getPart(i).getCharset());
if (characterSet.equals(CharacterSets.MIMENAME_ANY_CHARSET))
characterSet = CharacterSets.MIMENAME_ISO_8859_1;
if (body.getPart(i).getData() != null) {
partText = new String(body.getPart(i).getData(), characterSet);
} else {
partText = "";
}
} catch (UnsupportedEncodingException e) {
Log.w("PartParser", e);
partText = "Unsupported Encoding!";
}
bodyText = (bodyText == null) ? partText : bodyText + " " + partText;
}
}
return bodyText;
}
public static PduBody getSupportedMediaParts(PduBody body) {
PduBody stripped = new PduBody();
for (int i=0;i<body.getPartsNum();i++) {
if (isDisplayableMedia(body.getPart(i))) {
stripped.addPart(body.getPart(i));
}
}
return stripped;
}
public static int getSupportedMediaPartCount(PduBody body) {
int partCount = 0;
for (int i=0;i<body.getPartsNum();i++) {
if (isDisplayableMedia(body.getPart(i))) {
partCount++;
}
}
return partCount;
}
public static boolean isImage(PduPart part) {
return ContentType.isImageType(Util.toIsoString(part.getContentType()));
}
public static boolean isAudio(PduPart part) {
return ContentType.isAudioType(Util.toIsoString(part.getContentType()));
}
public static boolean isVideo(PduPart part) {
return ContentType.isVideoType(Util.toIsoString(part.getContentType()));
}
public static boolean isText(PduPart part) {
return ContentType.isTextType(Util.toIsoString(part.getContentType()));
}
public static boolean isDisplayableMedia(PduPart part) {
return isImage(part) || isAudio(part) || isVideo(part);
}
}