Handle invalid uri when opening in Custom Tabs.

This commit is contained in:
Alex Ning 2019-08-15 16:25:20 +08:00
parent 478b85e67b
commit 2c6ca51e5c
5 changed files with 70 additions and 7 deletions

View File

@ -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();

View File

@ -374,7 +374,15 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
builder.addDefaultShareMenuItem();
builder.setToolbarColor(mActivity.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(mActivity, Uri.parse(mPost.getUrl()));
Uri uri = Uri.parse(mPost.getUrl());
if(uri.getHost() != null && uri.getHost().contains("reddit.com")) {
customTabsIntent.intent.setPackage(mActivity.getPackageName());
}
String uriString = mPost.getUrl();
if(!uriString.startsWith("http://") && !uriString.startsWith("https://")) {
uriString = "http://" + uriString;
}
customTabsIntent.launchUrl(mActivity, Uri.parse(uriString));
});
break;
case Post.GIF_VIDEO_TYPE:
@ -431,7 +439,15 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
builder.addDefaultShareMenuItem();
builder.setToolbarColor(mActivity.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(mActivity, Uri.parse(mPost.getUrl()));
Uri uri = Uri.parse(mPost.getUrl());
if(uri.getHost() != null && uri.getHost().contains("reddit.com")) {
customTabsIntent.intent.setPackage(mActivity.getPackageName());
}
String uriString = mPost.getUrl();
if(!uriString.startsWith("http://") && !uriString.startsWith("https://")) {
uriString = "http://" + uriString;
}
customTabsIntent.launchUrl(mActivity, Uri.parse(uriString));
});
break;
case Post.TEXT_TYPE:
@ -451,6 +467,11 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
comment = mVisibleComments.get(holder.getAdapterPosition() - 1);
}
if(mIsSingleCommentThreadMode && comment.getId().equals(mSingleCommentId)) {
((CommentViewHolder) holder).itemView.setBackgroundColor(
mActivity.getResources().getColor(R.color.singleCommentThreadBackgroundColor));
}
String authorPrefixed = "u/" + comment.getAuthor();
((CommentViewHolder) holder).authorTextView.setText(authorPrefixed);
@ -722,6 +743,8 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
((CommentViewHolder) holder).expandButton.setVisibility(View.GONE);
((CommentViewHolder) holder).upVoteButton.clearColorFilter();
((CommentViewHolder) holder).downVoteButton.clearColorFilter();
((CommentViewHolder) holder).itemView.setBackgroundColor(
mActivity.getResources().getColor(R.color.cardViewBackgroundColor));
}
}
@ -1297,6 +1320,8 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
itemView.setOnClickListener(view -> {
if(mActivity != null && mActivity instanceof ViewPostDetailActivity) {
mIsSingleCommentThreadMode = false;
mSingleCommentId = null;
((ViewPostDetailActivity) mActivity).changeToSingleThreadMode();
}
});

View File

@ -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<String> 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);

View File

@ -390,7 +390,15 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
builder.addDefaultShareMenuItem();
builder.setToolbarColor(mContext.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(mContext, Uri.parse(post.getUrl()));
Uri uri = Uri.parse(post.getUrl());
if(uri.getHost() != null && uri.getHost().contains("reddit.com")) {
customTabsIntent.intent.setPackage(mContext.getPackageName());
}
String uriString = post.getUrl();
if(!uriString.startsWith("http://") && !uriString.startsWith("https://")) {
uriString = "http://" + uriString;
}
customTabsIntent.launchUrl(mContext, Uri.parse(uriString));
});
break;
case Post.GIF_VIDEO_TYPE:
@ -443,7 +451,15 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
builder.addDefaultShareMenuItem();
builder.setToolbarColor(mContext.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(mContext, Uri.parse(noPreviewLinkUrl));
Uri uri = Uri.parse(post.getUrl());
if(uri.getHost() != null && uri.getHost().contains("reddit.com")) {
customTabsIntent.intent.setPackage(mContext.getPackageName());
}
String uriString = noPreviewLinkUrl;
if(!uriString.startsWith("http://") && !uriString.startsWith("https://")) {
uriString = "http://" + uriString;
}
customTabsIntent.launchUrl(mContext, Uri.parse(uriString));
});
break;
case Post.TEXT_TYPE:

View File

@ -45,4 +45,6 @@
<color name="navBarColor">#FFFFFF</color>
<color name="cardViewBackgroundColor">#FFFFFF</color>
<color name="singleCommentThreadBackgroundColor">#B3E5F9</color>
</resources>