Merge pull request #308 from triq6/master

Fixed Error When Parsing Content ID
This commit is contained in:
Moez Bhatti
2015-12-25 14:51:41 -05:00
@@ -20,12 +20,14 @@ package com.moez.QKSMS.model;
import android.content.Context; import android.content.Context;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import com.google.android.mms.ContentType; import com.google.android.mms.ContentType;
import com.google.android.mms.MmsException; import com.google.android.mms.MmsException;
import com.google.android.mms.pdu_alt.PduBody; import com.google.android.mms.pdu_alt.PduBody;
import com.google.android.mms.pdu_alt.PduPart; import com.google.android.mms.pdu_alt.PduPart;
import com.moez.QKSMS.LogTag; import com.moez.QKSMS.LogTag;
import com.moez.QKSMS.MmsConfig; import com.moez.QKSMS.MmsConfig;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.w3c.dom.smil.SMILMediaElement; import org.w3c.dom.smil.SMILMediaElement;
@@ -42,16 +44,17 @@ import java.util.Collections;
public class MediaModelFactory { public class MediaModelFactory {
private static final String TAG = "Mms:media"; private static final String TAG = "Mms:media";
private static final boolean LOCAL_LOGV = false;
/** /**
* Returns the media model for the given SMILMediaElement in the PduBody. * Returns the media model for the given SMILMediaElement in the PduBody.
* *
* @param context * @param context Context
* @param sme The SMILMediaElement to find * @param sme The SMILMediaElement to find
* @param srcs * @param srcs String array of sources
* @param layouts * @param layouts LayoutModel
* @param pb * @param pb PduBuddy
* @return * @return MediaModel
* @throws IOException * @throws IOException
* @throws IllegalArgumentException * @throws IllegalArgumentException
* @throws MmsException * @throws MmsException
@@ -93,7 +96,6 @@ public class MediaModelFactory {
* *
* We employ several methods of pairing src strings up to parts, using certain patterns we've * 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. * seen in failed MMS messages. These are described in this method.
*
* TODO TODO TODO: Create a testing suite for this! * TODO TODO TODO: Create a testing suite for this!
*/ */
private static PduPart findPart(final Context context, PduBody pb, String src, private static PduPart findPart(final Context context, PduBody pb, String src,
@@ -318,7 +320,17 @@ public class MediaModelFactory {
if (src == null) { if (src == null) {
return -1; return -1;
} else { } else {
return Long.parseLong(src.substring("cid:".length())); if (LOCAL_LOGV) {
Log.v(TAG, "Initial contentId: " + src);
}
src = src.substring("cid:".length());
src = unescapeXML(src);
// Strip any leading < or trailing > ... they are present sometimes and causing error(s)
src = src.replaceAll("(^\\<)|(\\>$)", "");
if (LOCAL_LOGV) {
Log.v(TAG, "Final contentId: " + src);
}
return Long.parseLong(src);
} }
} }
@@ -347,10 +359,10 @@ public class MediaModelFactory {
} }
private static String unescapeXML(String str) { private static String unescapeXML(String str) {
return str.replaceAll("&lt;","<") return str.replaceAll("&lt;", "<")
.replaceAll("&gt;", ">") .replaceAll("&gt;", ">")
.replaceAll("&quot;","\"") .replaceAll("&quot;", "\"")
.replaceAll("&apos;","'") .replaceAll("&apos;", "'")
.replaceAll("&amp;", "&"); .replaceAll("&amp;", "&");
} }
@@ -364,7 +376,7 @@ public class MediaModelFactory {
return getGenericMediaModel(context, tag, src, srme, part, region); return getGenericMediaModel(context, tag, src, srme, part, region);
} }
} else { } else {
String rId = null; String rId;
if (tag.equals(SmilHelper.ELEMENT_TAG_TEXT)) { if (tag.equals(SmilHelper.ELEMENT_TAG_TEXT)) {
rId = LayoutModel.TEXT_REGION_ID; rId = LayoutModel.TEXT_REGION_ID;
@@ -398,20 +410,26 @@ public class MediaModelFactory {
} }
String contentType = new String(bytes); String contentType = new String(bytes);
MediaModel media = null; MediaModel media;
if (tag.equals(SmilHelper.ELEMENT_TAG_TEXT)) {
switch (tag) {
case SmilHelper.ELEMENT_TAG_TEXT:
media = new TextModel(context, contentType, src, media = new TextModel(context, contentType, src,
part.getCharset(), part.getData(), regionModel); part.getCharset(), part.getData(), regionModel);
} else if (tag.equals(SmilHelper.ELEMENT_TAG_IMAGE)) { break;
case SmilHelper.ELEMENT_TAG_IMAGE:
media = new ImageModel(context, contentType, src, media = new ImageModel(context, contentType, src,
part.getDataUri(), regionModel); part.getDataUri(), regionModel);
} else if (tag.equals(SmilHelper.ELEMENT_TAG_VIDEO)) { break;
case SmilHelper.ELEMENT_TAG_VIDEO:
media = new VideoModel(context, contentType, src, media = new VideoModel(context, contentType, src,
part.getDataUri(), regionModel); part.getDataUri(), regionModel);
} else if (tag.equals(SmilHelper.ELEMENT_TAG_AUDIO)) { break;
case SmilHelper.ELEMENT_TAG_AUDIO:
media = new AudioModel(context, contentType, src, media = new AudioModel(context, contentType, src,
part.getDataUri()); part.getDataUri());
} else if (tag.equals(SmilHelper.ELEMENT_TAG_REF)) { break;
case SmilHelper.ELEMENT_TAG_REF:
if (ContentType.isTextType(contentType)) { if (ContentType.isTextType(contentType)) {
media = new TextModel(context, contentType, src, media = new TextModel(context, contentType, src,
part.getCharset(), part.getData(), regionModel); part.getCharset(), part.getData(), regionModel);
@@ -429,7 +447,8 @@ public class MediaModelFactory {
+ contentType); + contentType);
media = createEmptyTextModel(context, regionModel); media = createEmptyTextModel(context, regionModel);
} }
} else { break;
default:
throw new IllegalArgumentException("Unsupported TAG: " + tag); throw new IllegalArgumentException("Unsupported TAG: " + tag);
} }
@@ -453,12 +472,10 @@ public class MediaModelFactory {
if (t.getTimeType() != Time.SMIL_TIME_INDEFINITE) { if (t.getTimeType() != Time.SMIL_TIME_INDEFINITE) {
duration = (int) (t.getResolvedOffset() * 1000) - begin; duration = (int) (t.getResolvedOffset() * 1000) - begin;
if (duration == 0 && if (duration == 0 && (media instanceof AudioModel || media instanceof VideoModel)) {
(media instanceof AudioModel || media instanceof VideoModel)) {
duration = MmsConfig.getMinimumSlideElementDuration(); duration = MmsConfig.getMinimumSlideElementDuration();
if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.d(TAG, "[MediaModelFactory] compute new duration for " + tag + Log.v(TAG, "[MediaModelFactory] compute new duration for " + tag + ", duration=" + duration);
", duration=" + duration);
} }
} }
} }
@@ -474,7 +491,7 @@ public class MediaModelFactory {
* If not, the media will disappear while rotating the screen * If not, the media will disappear while rotating the screen
* in the slide show play view. * in the slide show play view.
*/ */
media.setFill(sme.FILL_FREEZE); media.setFill(SMILMediaElement.FILL_FREEZE);
} else { } else {
// Set 'fill' property. // Set 'fill' property.
media.setFill(sme.getFill()); media.setFill(sme.getFill());