2013-04-26 18:23:43 +00:00
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.ContentType;
|
|
|
|
import ws.com.google.android.mms.pdu.CharacterSets;
|
|
|
|
import ws.com.google.android.mms.pdu.PduBody;
|
|
|
|
|
|
|
|
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 {
|
2013-05-25 20:17:18 +00:00
|
|
|
String characterSet = CharacterSets.getMimeName(body.getPart(i).getCharset());
|
|
|
|
|
|
|
|
if (characterSet.equals(CharacterSets.MIMENAME_ANY_CHARSET))
|
|
|
|
characterSet = CharacterSets.MIMENAME_ISO_8859_1;
|
|
|
|
|
2015-01-13 20:14:47 +00:00
|
|
|
if (body.getPart(i).getData() != null) {
|
|
|
|
partText = new String(body.getPart(i).getData(), characterSet);
|
|
|
|
} else {
|
|
|
|
partText = "";
|
|
|
|
}
|
2013-04-26 18:23:43 +00:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
Log.w("PartParser", e);
|
|
|
|
partText = "Unsupported Encoding!";
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyText = (bodyText == null) ? partText : bodyText + " " + partText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bodyText;
|
|
|
|
}
|
|
|
|
|
2014-12-30 09:36:51 +00:00
|
|
|
public static PduBody getSupportedMediaParts(PduBody body) {
|
2013-04-26 18:23:43 +00:00
|
|
|
PduBody stripped = new PduBody();
|
|
|
|
|
|
|
|
for (int i=0;i<body.getPartsNum();i++) {
|
2014-12-30 09:36:51 +00:00
|
|
|
if (isDisplayableMedia(Util.toIsoString(body.getPart(i).getContentType()))) {
|
2013-04-26 18:23:43 +00:00
|
|
|
stripped.addPart(body.getPart(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stripped;
|
|
|
|
}
|
2013-05-05 19:51:36 +00:00
|
|
|
|
2014-12-30 09:36:51 +00:00
|
|
|
public static int getSupportedMediaPartCount(PduBody body) {
|
2013-05-05 19:51:36 +00:00
|
|
|
int partCount = 0;
|
|
|
|
|
|
|
|
for (int i=0;i<body.getPartsNum();i++) {
|
|
|
|
String contentType = Util.toIsoString(body.getPart(i).getContentType());
|
|
|
|
|
2014-12-30 09:36:51 +00:00
|
|
|
if (isDisplayableMedia(contentType)) {
|
2013-05-05 19:51:36 +00:00
|
|
|
partCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return partCount;
|
|
|
|
}
|
2014-12-30 09:36:51 +00:00
|
|
|
|
|
|
|
private static boolean isDisplayableMedia(String contentType) {
|
|
|
|
return ContentType.isImageType(contentType) ||
|
|
|
|
ContentType.isAudioType(contentType) ||
|
|
|
|
ContentType.isVideoType(contentType);
|
|
|
|
}
|
2013-04-26 18:23:43 +00:00
|
|
|
}
|