From a5b82c9804a9e6756725fdbb232a4cd95718c5db Mon Sep 17 00:00:00 2001 From: Tariq Abusheikh Date: Mon, 14 Dec 2015 17:23:40 -0600 Subject: [PATCH 1/4] Fixed potential error in MediaModelFactory::getContentId(src) method; caused by '<' and '>' in CID --- .../moez/QKSMS/model/MediaModelFactory.java | 148 ++++++++++-------- 1 file changed, 86 insertions(+), 62 deletions(-) diff --git a/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java b/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java index df22932c..0431d938 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java @@ -20,12 +20,14 @@ package com.moez.QKSMS.model; import android.content.Context; import android.preference.PreferenceManager; import android.util.Log; + import com.google.android.mms.ContentType; import com.google.android.mms.MmsException; import com.google.android.mms.pdu_alt.PduBody; import com.google.android.mms.pdu_alt.PduPart; import com.moez.QKSMS.LogTag; import com.moez.QKSMS.MmsConfig; + import org.json.JSONArray; import org.json.JSONObject; import org.w3c.dom.smil.SMILMediaElement; @@ -42,16 +44,17 @@ import java.util.Collections; public class MediaModelFactory { private static final String TAG = "Mms:media"; + private static final boolean LOCAL_LOGV = true; /** * Returns the media model for the given SMILMediaElement in the PduBody. * - * @param context - * @param sme The SMILMediaElement to find - * @param srcs - * @param layouts - * @param pb - * @return + * @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 @@ -60,40 +63,40 @@ public class MediaModelFactory { ArrayList srcs, LayoutModel layouts, PduBody pb) - throws IOException, IllegalArgumentException, MmsException { + throws IOException, IllegalArgumentException, MmsException { String tag = sme.getTagName(); String src = sme.getSrc(); - + Log.v(TAG, "SRC: " + src); PduPart part = findPart(context, pb, src, srcs); if (sme instanceof SMILRegionMediaElement) { return getRegionMediaModel( - context, tag, src, (SMILRegionMediaElement) sme, layouts, part); + context, tag, src, (SMILRegionMediaElement) sme, layouts, part); } else { return getGenericMediaModel( - context, tag, src, sme, part, null); + context, tag, src, sme, part, null); } } /** * 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. + * slideshows, videos, etc. * 2. One or more parts that correspond to one of the elements that was mentioned in the - * formatting above. - * + * 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, @@ -134,6 +137,7 @@ public class MediaModelFactory { // Now, build a list of long IDs, sort them, and remove the duplicates. ArrayList cids = new ArrayList<>(); for (String _src : srcs) { + Log.v(TAG, "Current CID: " + _src); cids.add(getContentId(_src)); } Collections.sort(cids); @@ -318,7 +322,22 @@ public class MediaModelFactory { if (src == null) { return -1; } 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) + if (src.startsWith("<")) { + src = src.replace("<", ""); + } + if (src.endsWith(">")) { + src = src.replace(">", ""); + } + if (LOCAL_LOGV) { + Log.v(TAG, "Final contentId: " + src); + } + return Long.parseLong(src); } } @@ -347,16 +366,16 @@ public class MediaModelFactory { } private static String unescapeXML(String str) { - return str.replaceAll("<","<") + return str.replaceAll("<", "<") .replaceAll(">", ">") - .replaceAll(""","\"") - .replaceAll("'","'") + .replaceAll(""", "\"") + .replaceAll("'", "'") .replaceAll("&", "&"); } private static MediaModel getRegionMediaModel(Context context, - String tag, String src, SMILRegionMediaElement srme, - LayoutModel layouts, PduPart part) throws IOException, MmsException { + String tag, String src, SMILRegionMediaElement srme, + LayoutModel layouts, PduPart part) throws IOException, MmsException { SMILRegionElement sre = srme.getRegion(); if (sre != null) { RegionModel region = layouts.findRegionById(sre.getId()); @@ -364,7 +383,7 @@ public class MediaModelFactory { return getGenericMediaModel(context, tag, src, srme, part, region); } } else { - String rId = null; + String rId; if (tag.equals(SmilHelper.ELEMENT_TAG_TEXT)) { rId = LayoutModel.TEXT_REGION_ID; @@ -383,54 +402,61 @@ public class MediaModelFactory { // When we encounter a content type we can't handle, such as "application/vnd.smaf", instead // of throwing an exception and crashing, insert an empty TextModel in its place. - private static MediaModel createEmptyTextModel(Context context, RegionModel regionModel) - throws IOException { + private static MediaModel createEmptyTextModel(Context context, RegionModel regionModel) + throws IOException { return new TextModel(context, ContentType.TEXT_PLAIN, null, regionModel); } private static MediaModel getGenericMediaModel(Context context, - String tag, String src, SMILMediaElement sme, PduPart part, - RegionModel regionModel) throws IOException, MmsException { + String tag, String src, SMILMediaElement sme, PduPart part, + RegionModel regionModel) throws IOException, MmsException { byte[] bytes = part.getContentType(); if (bytes == null) { throw new IllegalArgumentException( - "Content-Type of the part may not be null."); + "Content-Type of the part may not be null."); } String contentType = new String(bytes); - MediaModel media = null; - if (tag.equals(SmilHelper.ELEMENT_TAG_TEXT)) { - media = new TextModel(context, contentType, src, - part.getCharset(), part.getData(), regionModel); - } else if (tag.equals(SmilHelper.ELEMENT_TAG_IMAGE)) { - media = new ImageModel(context, contentType, src, - part.getDataUri(), regionModel); - } else if (tag.equals(SmilHelper.ELEMENT_TAG_VIDEO)) { - media = new VideoModel(context, contentType, src, - part.getDataUri(), regionModel); - } else if (tag.equals(SmilHelper.ELEMENT_TAG_AUDIO)) { - media = new AudioModel(context, contentType, src, - part.getDataUri()); - } else if (tag.equals(SmilHelper.ELEMENT_TAG_REF)) { - if (ContentType.isTextType(contentType)) { + MediaModel media; + + switch (tag) { + case SmilHelper.ELEMENT_TAG_TEXT: media = new TextModel(context, contentType, src, - part.getCharset(), part.getData(), regionModel); - } else if (ContentType.isImageType(contentType)) { + part.getCharset(), part.getData(), regionModel); + break; + case SmilHelper.ELEMENT_TAG_IMAGE: media = new ImageModel(context, contentType, src, - part.getDataUri(), regionModel); - } else if (ContentType.isVideoType(contentType)) { + part.getDataUri(), regionModel); + break; + case SmilHelper.ELEMENT_TAG_VIDEO: media = new VideoModel(context, contentType, src, - part.getDataUri(), regionModel); - } else if (ContentType.isAudioType(contentType)) { + part.getDataUri(), regionModel); + break; + case SmilHelper.ELEMENT_TAG_AUDIO: media = new AudioModel(context, contentType, src, + part.getDataUri()); + break; + case SmilHelper.ELEMENT_TAG_REF: + if (ContentType.isTextType(contentType)) { + media = new TextModel(context, contentType, src, + part.getCharset(), part.getData(), regionModel); + } else if (ContentType.isImageType(contentType)) { + media = new ImageModel(context, contentType, src, + part.getDataUri(), regionModel); + } else if (ContentType.isVideoType(contentType)) { + media = new VideoModel(context, contentType, src, + part.getDataUri(), regionModel); + } else if (ContentType.isAudioType(contentType)) { + media = new AudioModel(context, contentType, src, part.getDataUri()); - } else { - Log.d(TAG, "[MediaModelFactory] getGenericMediaModel Unsupported Content-Type: " + } else { + Log.d(TAG, "[MediaModelFactory] getGenericMediaModel Unsupported Content-Type: " + contentType); - media = createEmptyTextModel(context, regionModel); - } - } else { - throw new IllegalArgumentException("Unsupported TAG: " + tag); + media = createEmptyTextModel(context, regionModel); + } + break; + default: + throw new IllegalArgumentException("Unsupported TAG: " + tag); } // Set 'begin' property. @@ -453,12 +479,10 @@ public class MediaModelFactory { if (t.getTimeType() != Time.SMIL_TIME_INDEFINITE) { duration = (int) (t.getResolvedOffset() * 1000) - begin; - if (duration == 0 && - (media instanceof AudioModel || media instanceof VideoModel)) { + if (duration == 0 && (media instanceof AudioModel || media instanceof VideoModel)) { duration = MmsConfig.getMinimumSlideElementDuration(); - if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { - Log.d(TAG, "[MediaModelFactory] compute new duration for " + tag + - ", duration=" + duration); + if (Log.isLoggable(TAG, Log.VERBOSE)) { + Log.v(TAG, "[MediaModelFactory] compute new duration for " + tag + ", duration=" + duration); } } } @@ -474,7 +498,7 @@ public class MediaModelFactory { * If not, the media will disappear while rotating the screen * in the slide show play view. */ - media.setFill(sme.FILL_FREEZE); + media.setFill(SMILMediaElement.FILL_FREEZE); } else { // Set 'fill' property. media.setFill(sme.getFill()); From 297f5ab488adb7b8ba9fb25a2a5c8172a39cbc7a Mon Sep 17 00:00:00 2001 From: Tariq Abusheikh Date: Mon, 14 Dec 2015 17:28:31 -0600 Subject: [PATCH 2/4] Removed some temp. debug logs, set LOCAL_LOGV to false --- .../main/java/com/moez/QKSMS/model/MediaModelFactory.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java b/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java index 0431d938..3a94e638 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java @@ -44,7 +44,7 @@ import java.util.Collections; public class MediaModelFactory { private static final String TAG = "Mms:media"; - private static final boolean LOCAL_LOGV = true; + private static final boolean LOCAL_LOGV = false; /** * Returns the media model for the given SMILMediaElement in the PduBody. @@ -67,7 +67,7 @@ public class MediaModelFactory { String tag = sme.getTagName(); String src = sme.getSrc(); - Log.v(TAG, "SRC: " + src); + PduPart part = findPart(context, pb, src, srcs); if (sme instanceof SMILRegionMediaElement) { @@ -137,7 +137,6 @@ public class MediaModelFactory { // Now, build a list of long IDs, sort them, and remove the duplicates. ArrayList cids = new ArrayList<>(); for (String _src : srcs) { - Log.v(TAG, "Current CID: " + _src); cids.add(getContentId(_src)); } Collections.sort(cids); From cd6d63b1c11440f867557463cb28c0f38c0c2fe8 Mon Sep 17 00:00:00 2001 From: Tariq Abusheikh Date: Mon, 14 Dec 2015 17:45:48 -0600 Subject: [PATCH 3/4] * Replaced if checks with one regular expression (will only match leading < or trailing >) * Removed some

tags that Android Studio added to DocBlock --- .../com/moez/QKSMS/model/MediaModelFactory.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java b/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java index 3a94e638..cce8c100 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/model/MediaModelFactory.java @@ -82,21 +82,20 @@ public class MediaModelFactory { /** * 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, @@ -327,12 +326,7 @@ public class MediaModelFactory { src = src.substring("cid:".length()); src = unescapeXML(src); // Strip any leading < or trailing > ... they are present sometimes and causing error(s) - if (src.startsWith("<")) { - src = src.replace("<", ""); - } - if (src.endsWith(">")) { - src = src.replace(">", ""); - } + src = src.replaceAll("(^\\<)|(\\>$)", ""); if (LOCAL_LOGV) { Log.v(TAG, "Final contentId: " + src); } From 0a271e17bae964e935f38643c80853ed34152d1f Mon Sep 17 00:00:00 2001 From: allsimon Date: Wed, 23 Dec 2015 14:31:14 +0100 Subject: [PATCH 4/4] Add Linkify --- .../moez/QKSMS/common/utils/LinkifyUtils.java | 111 ++++++++++++++++++ .../ui/messagelist/MessageListAdapter.java | 12 +- 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 QKSMS/src/main/java/com/moez/QKSMS/common/utils/LinkifyUtils.java diff --git a/QKSMS/src/main/java/com/moez/QKSMS/common/utils/LinkifyUtils.java b/QKSMS/src/main/java/com/moez/QKSMS/common/utils/LinkifyUtils.java new file mode 100644 index 00000000..e6419989 --- /dev/null +++ b/QKSMS/src/main/java/com/moez/QKSMS/common/utils/LinkifyUtils.java @@ -0,0 +1,111 @@ +package com.moez.QKSMS.common.utils; + +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.provider.Browser; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.style.CharacterStyle; +import android.text.style.ClickableSpan; +import android.text.style.URLSpan; +import android.text.util.Linkify; +import android.text.util.Linkify.TransformFilter; +import android.util.Log; +import android.view.View; +import android.widget.TextView; + +import java.util.regex.Pattern; + +/** + * Shamelessly stolen from + * ChatSecure's implementation + */ +public class LinkifyUtils { + + private static Pattern bitcoin = Pattern.compile("bitcoin:[1-9a-km-zA-HJ-NP-Z]{27,34}(\\?[a-zA-Z0-9$\\-_.+!*'(),%:;@&=]*)?"); + private static Pattern geo = Pattern.compile("geo:[-0-9.]+,[-0-9.]+[^ \t\n\"\':]*"); + private static Pattern market = Pattern.compile("market://[^ \t\n\"\':,<>]+"); + private static Pattern openpgp4fpr = Pattern.compile("openpgp4fpr:[A-Za-z0-9]{8,40}"); + private static Pattern xmpp = Pattern.compile("xmpp:[^ \t\n\"\':,<>]+"); + private static Pattern twitterHandle = Pattern.compile("@([A-Za-z0-9_-]+)"); + private static Pattern hashtag = Pattern.compile("#([A-Za-z0-9_-]+)"); + + static TransformFilter returnMatchFilter = (match, url) -> match.group(1); + + /* Right now, if there is no app to handle */ + public static void addLinks(TextView text) { + Linkify.addLinks(text, Linkify.ALL); + Linkify.addLinks(text, geo, null); + Linkify.addLinks(text, market, null); + Linkify.addLinks(text, openpgp4fpr, null); + Linkify.addLinks(text, bitcoin, null); + Linkify.addLinks(text, xmpp, null); + Linkify.addLinks(text, twitterHandle, "https://twitter.com/", null, returnMatchFilter); + Linkify.addLinks(text, hashtag, "https://twitter.com/hashtag/", null, returnMatchFilter); + text.setText(replaceAll(text.getText(), URLSpan.class, new URLSpanConverter())); + } + + /** + * Do not create this static utility class. + */ + private LinkifyUtils() { + } + + // thanks to @commonsware https://stackoverflow.com/a/11417498 + public static Spannable replaceAll( + CharSequence original, Class sourceType, SpanConverter converter) { + SpannableString result = new SpannableString(original); + A[] spans = result.getSpans(0, result.length(), sourceType); + + for (A span : spans) { + int start = result.getSpanStart(span); + int end = result.getSpanEnd(span); + int flags = result.getSpanFlags(span); + + result.removeSpan(span); + result.setSpan(converter.convert(span), start, end, flags); + } + + return (result); + } + + private interface SpanConverter { + B convert(A span); + } + + /** + * This trickery is needed in order to have clickable links that open things + * in a new {@code Task} rather than in ChatSecure's {@code Task.} Thanks to @commonsware + * https://stackoverflow.com/a/11417498 + */ + private static class NewTaskUrlSpan extends ClickableSpan { + private String urlString; + + NewTaskUrlSpan(String urlString) { + this.urlString = urlString; + } + + @Override + public void onClick(View widget) { + try { + Uri uri = Uri.parse(urlString); + Context context = widget.getContext(); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + } catch (ActivityNotFoundException ignored) { + Log.e("LinkifyUtils", "", ignored); // We should (or shouldn't) explain that no app can handle this intent ? + } + } + } + + private static class URLSpanConverter implements LinkifyUtils.SpanConverter { + @Override + public NewTaskUrlSpan convert(URLSpan span) { + return (new NewTaskUrlSpan(span.getURL())); + } + } +} \ No newline at end of file diff --git a/QKSMS/src/main/java/com/moez/QKSMS/ui/messagelist/MessageListAdapter.java b/QKSMS/src/main/java/com/moez/QKSMS/ui/messagelist/MessageListAdapter.java index 45196a64..0b7d9ae9 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/ui/messagelist/MessageListAdapter.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/ui/messagelist/MessageListAdapter.java @@ -14,12 +14,16 @@ import android.telephony.TelephonyManager; import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.TextUtils; +import android.text.style.ClickableSpan; import android.text.style.StyleSpan; +import android.text.style.URLSpan; import android.text.style.UnderlineSpan; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.RelativeLayout; + import com.android.mms.transaction.Transaction; import com.android.mms.transaction.TransactionBundle; import com.android.mms.transaction.TransactionService; @@ -30,6 +34,7 @@ import com.moez.QKSMS.QKSMSApp; import com.moez.QKSMS.R; import com.moez.QKSMS.common.emoji.EmojiRegistry; import com.moez.QKSMS.common.utils.CursorUtils; +import com.moez.QKSMS.common.utils.LinkifyUtils; import com.moez.QKSMS.common.utils.MessageUtils; import com.moez.QKSMS.data.Contact; import com.moez.QKSMS.transaction.SmsHelper; @@ -39,6 +44,7 @@ import com.moez.QKSMS.ui.base.RecyclerCursorAdapter; import com.moez.QKSMS.ui.mms.MmsThumbnailPresenter; import com.moez.QKSMS.ui.settings.SettingsFragment; import com.moez.QKSMS.ui.view.AvatarView; + import ezvcard.Ezvcard; import ezvcard.VCard; @@ -320,6 +326,7 @@ public class MessageListAdapter extends RecyclerCursorAdapter