From 2c6ca51e5c772f30490a35bf73305b43eef91b56 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 15 Aug 2019 16:25:20 +0800 Subject: Handle invalid uri when opening in Custom Tabs. --- .../main/java/CustomView/CustomMarkwonView.java | 6 ++++- .../CommentAndPostRecyclerViewAdapter.java | 29 ++++++++++++++++++++-- .../infinityforreddit/LinkResolverActivity.java | 20 +++++++++++++-- .../infinityforreddit/PostRecyclerViewAdapter.java | 20 +++++++++++++-- 4 files changed, 68 insertions(+), 7 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/CustomView/CustomMarkwonView.java b/app/src/main/java/CustomView/CustomMarkwonView.java index da6b2f55..d5828f13 100644 --- a/app/src/main/java/CustomView/CustomMarkwonView.java +++ b/app/src/main/java/CustomView/CustomMarkwonView.java @@ -44,7 +44,11 @@ public class CustomMarkwonView extends MarkwonView { if(uri.getHost() != null && uri.getHost().contains("reddit.com")) { customTabsIntent.intent.setPackage(context.getPackageName()); } - customTabsIntent.launchUrl(context, uri); + String uriString = uri.toString(); + if(!uriString.startsWith("http://") && (!uriString.startsWith("https://"))) { + uriString = "http://" + uriString; + } + customTabsIntent.launchUrl(context, Uri.parse(uriString)); } }).build(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index 5ecc81df..9816946c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -374,7 +374,15 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { if(mActivity != null && mActivity instanceof ViewPostDetailActivity) { + mIsSingleCommentThreadMode = false; + mSingleCommentId = null; ((ViewPostDetailActivity) mActivity).changeToSingleThreadMode(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java index 8df02968..a091cef0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LinkResolverActivity.java @@ -17,7 +17,8 @@ import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_C public class LinkResolverActivity extends AppCompatActivity { - private static final String POST_PATTERN = "/r/\\w+/comments/\\w+/*[\\w+]*/*"; + private static final String POST_PATTERN = "/r/\\w+/comments/\\w+/{0,1}\\w+/{0,1}"; + private static final String COMMENT_PATTERN = "/r/\\w+/comments/\\w+/{0,1}\\w+/\\w+/{0,1}"; private static final String SUBREDDIT_PATTERN = "/r/\\w+/*"; private static final String USER_PATTERN = "/user/\\w+/*"; @@ -41,6 +42,17 @@ public class LinkResolverActivity extends AppCompatActivity { } else { deepLinkError(uri); } + } else if(path.matches(COMMENT_PATTERN)) { + List segments = uri.getPathSegments(); + int commentsIndex = segments.lastIndexOf("comments"); + if(commentsIndex >=0 && commentsIndex < segments.size() - 1) { + Intent intent = new Intent(this, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, segments.get(commentsIndex + 1)); + intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, segments.get(segments.size() - 1)); + startActivity(intent); + } else { + deepLinkError(uri); + } } else if(path.matches(SUBREDDIT_PATTERN)) { String subredditName = path.substring(3); if(subredditName.equals("popular") || subredditName.equals("all")) { @@ -73,7 +85,11 @@ public class LinkResolverActivity extends AppCompatActivity { builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); CustomTabsIntent customTabsIntent = builder.build(); customTabsIntent.intent.setPackage(resolveInfos.get(0).activityInfo.packageName); - customTabsIntent.launchUrl(this, uri); + String uriString = uri.toString(); + if(!uriString.startsWith("http://") || (!uriString.startsWith("https://"))) { + uriString = "http://" + uriString; + } + customTabsIntent.launchUrl(this, Uri.parse(uriString)); } else { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(uri); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 07b620ca..239bf8c0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -390,7 +390,15 @@ class PostRecyclerViewAdapter extends PagedListAdapter