From 8ba7bd7b7e0ce4bf1cdbc4276f3ee9f4118a3d62 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 9 Sep 2019 16:30:26 +0800 Subject: Handle no browser installed when opening links and no apps are eligible to share when sharing. --- .../CommentAndPostRecyclerViewAdapter.java | 68 +++--- .../CommentsListingRecyclerViewAdapter.java | 245 +++++++++++---------- .../infinityforreddit/PostRecyclerViewAdapter.java | 52 ++--- .../infinityforreddit/SubmitPost.java | 1 - 4 files changed, 180 insertions(+), 186 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index fc6619a3..d0893b14 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit; import android.app.Activity; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.graphics.ColorFilter; import android.graphics.PorterDuff; @@ -22,7 +23,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; -import androidx.browser.customtabs.CustomTabsIntent; import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.RecyclerView; @@ -411,20 +411,14 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); - // add share action to menu list - builder.addDefaultShareMenuItem(); - builder.setToolbarColor(mActivity.getResources().getColor(R.color.colorPrimary)); - CustomTabsIntent customTabsIntent = builder.build(); + Intent intent = new Intent(mActivity, LinkResolverActivity.class); 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; + if(uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(mPost.getUrl())); + } else { + intent.setData(uri); } - customTabsIntent.launchUrl(mActivity, Uri.parse(uriString)); + mActivity.startActivity(intent); }); break; case Post.GIF_VIDEO_TYPE: @@ -477,20 +471,14 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); - // add share action to menu list - builder.addDefaultShareMenuItem(); - builder.setToolbarColor(mActivity.getResources().getColor(R.color.colorPrimary)); - CustomTabsIntent customTabsIntent = builder.build(); + Intent intent = new Intent(mActivity, LinkResolverActivity.class); 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; + if(uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(mPost.getUrl())); + } else { + intent.setData(uri); } - customTabsIntent.launchUrl(mActivity, Uri.parse(uriString)); + mActivity.startActivity(intent); }); break; case Post.TEXT_TYPE: @@ -1078,11 +1066,15 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - String extraText = mPost.getTitle() + "\n" + mPost.getPermalink(); - intent.putExtra(Intent.EXTRA_TEXT, extraText); - mActivity.startActivity(Intent.createChooser(intent, "Share")); + try { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + String extraText = mPost.getTitle() + "\n" + mPost.getPermalink(); + intent.putExtra(Intent.EXTRA_TEXT, extraText); + mActivity.startActivity(Intent.createChooser(intent, mActivity.getString(R.string.share))); + } catch (ActivityNotFoundException e) { + Toast.makeText(mActivity, R.string.no_activity_found_for_share, Toast.LENGTH_SHORT).show(); + } }); mUpvoteButton.setOnClickListener(view -> { @@ -1257,12 +1249,16 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - String extraText = mIsSingleCommentThreadMode ? mVisibleComments.get(getAdapterPosition() - 2).getPermalink() - : mVisibleComments.get(getAdapterPosition() - 1).getPermalink(); - intent.putExtra(Intent.EXTRA_TEXT, extraText); - mActivity.startActivity(Intent.createChooser(intent, "Share")); + try { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + String extraText = mIsSingleCommentThreadMode ? mVisibleComments.get(getAdapterPosition() - 2).getPermalink() + : mVisibleComments.get(getAdapterPosition() - 1).getPermalink(); + intent.putExtra(Intent.EXTRA_TEXT, extraText); + mActivity.startActivity(Intent.createChooser(intent, mActivity.getString(R.string.share))); + } catch (ActivityNotFoundException e) { + Toast.makeText(mActivity, R.string.no_activity_found_for_share, Toast.LENGTH_SHORT).show(); + } }); expandButton.setOnClickListener(view -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java index 3b39ace5..668a66b0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit; +import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.net.Uri; @@ -161,6 +162,131 @@ class CommentsListingRecyclerViewAdapter extends PagedListAdapter { + Intent intent = new Intent(mContext, ViewPostDetailActivity.class); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, comment.getLinkId()); + intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, comment.getId()); + mContext.startActivity(intent); + }); + + ((DataViewHolder) holder).verticalBlock.setVisibility(View.GONE); + + ((DataViewHolder) holder).commentMarkdownView.setOnClickListener(view -> + ((DataViewHolder) holder).linearLayout.callOnClick()); + + ((DataViewHolder) holder).shareButton.setOnClickListener(view -> { + try { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + String extraText = comment.getPermalink(); + intent.putExtra(Intent.EXTRA_TEXT, extraText); + mContext.startActivity(Intent.createChooser(intent, mContext.getString(R.string.share))); + } catch (ActivityNotFoundException e) { + Toast.makeText(mContext, R.string.no_activity_found_for_share, Toast.LENGTH_SHORT).show(); + } + }); + + ((DataViewHolder) holder).replyButton.setVisibility(View.GONE); + + ((DataViewHolder) holder).upvoteButton.setOnClickListener(view -> { + if(mAccessToken == null) { + Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + return; + } + + int previousVoteType = comment.getVoteType(); + String newVoteType; + + ((DataViewHolder) holder).downvoteButton.clearColorFilter(); + + if(previousVoteType != CommentData.VOTE_TYPE_UPVOTE) { + //Not upvoted before + comment.setVoteType(CommentData.VOTE_TYPE_UPVOTE); + newVoteType = RedditUtils.DIR_UPVOTE; + ((DataViewHolder) holder).upvoteButton + .setColorFilter(ContextCompat.getColor(mContext, R.color.upvoted), android.graphics.PorterDuff.Mode.SRC_IN); + ((DataViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.upvoted)); + } else { + //Upvoted before + comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE); + newVoteType = RedditUtils.DIR_UNVOTE; + ((DataViewHolder) holder).upvoteButton.clearColorFilter(); + ((DataViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.defaultTextColor)); + } + + ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType())); + + VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { + @Override + public void onVoteThingSuccess(int position) { + if(newVoteType.equals(RedditUtils.DIR_UPVOTE)) { + comment.setVoteType(CommentData.VOTE_TYPE_UPVOTE); + ((DataViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.upvoted), android.graphics.PorterDuff.Mode.SRC_IN); + ((DataViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.upvoted)); + } else { + comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE); + ((DataViewHolder) holder).upvoteButton.clearColorFilter(); + ((DataViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.defaultTextColor)); + } + + ((DataViewHolder) holder).downvoteButton.clearColorFilter(); + ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType())); + } + + @Override + public void onVoteThingFail(int position) { } + }, comment.getFullName(), newVoteType, holder.getAdapterPosition()); + }); + + ((DataViewHolder) holder).downvoteButton.setOnClickListener(view -> { + if(mAccessToken == null) { + Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + return; + } + + int previousVoteType = comment.getVoteType(); + String newVoteType; + + ((DataViewHolder) holder).upvoteButton.clearColorFilter(); + + if(previousVoteType != CommentData.VOTE_TYPE_DOWNVOTE) { + //Not downvoted before + comment.setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); + newVoteType = RedditUtils.DIR_DOWNVOTE; + ((DataViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.downvoted), android.graphics.PorterDuff.Mode.SRC_IN); + ((DataViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.downvoted)); + } else { + //Downvoted before + comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE); + newVoteType = RedditUtils.DIR_UNVOTE; + ((DataViewHolder) holder).downvoteButton.clearColorFilter(); + ((DataViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.defaultTextColor)); + } + + ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType())); + + VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { + @Override + public void onVoteThingSuccess(int position1) { + if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { + comment.setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); + ((DataViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.downvoted), android.graphics.PorterDuff.Mode.SRC_IN); + ((DataViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.downvoted)); + } else { + comment.setVoteType(CommentData.VOTE_TYPE_NO_VOTE); + ((DataViewHolder) holder).downvoteButton.clearColorFilter(); + ((DataViewHolder) holder).scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.defaultTextColor)); + } + + ((DataViewHolder) holder).upvoteButton.clearColorFilter(); + ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType())); + } + + @Override + public void onVoteThingFail(int position1) { } + }, comment.getFullName(), newVoteType, holder.getAdapterPosition()); + }); } } } @@ -232,125 +358,6 @@ class CommentsListingRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(mContext, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, getItem(getAdapterPosition()).getLinkId()); - intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, getItem(getAdapterPosition()).getId()); - mContext.startActivity(intent); - }); - - verticalBlock.setVisibility(View.GONE); - - commentMarkdownView.setOnClickListener(view -> linearLayout.callOnClick()); - - shareButton.setOnClickListener(view -> { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - String extraText = getItem(getAdapterPosition()).getPermalink(); - intent.putExtra(Intent.EXTRA_TEXT, extraText); - mContext.startActivity(Intent.createChooser(intent, "Share")); - }); - - replyButton.setVisibility(View.GONE); - - upvoteButton.setOnClickListener(view -> { - if(mAccessToken == null) { - Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); - return; - } - - int previousVoteType = getItem(getAdapterPosition()).getVoteType(); - String newVoteType; - - downvoteButton.clearColorFilter(); - - if(previousVoteType != CommentData.VOTE_TYPE_UPVOTE) { - //Not upvoted before - getItem(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_UPVOTE); - newVoteType = RedditUtils.DIR_UPVOTE; - upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.upvoted), android.graphics.PorterDuff.Mode.SRC_IN); - scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.upvoted)); - } else { - //Upvoted before - getItem(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_NO_VOTE); - newVoteType = RedditUtils.DIR_UNVOTE; - upvoteButton.clearColorFilter(); - scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.defaultTextColor)); - } - - scoreTextView.setText(Integer.toString(getItem(getAdapterPosition()).getScore() + getItem(getAdapterPosition()).getVoteType())); - - VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position) { - if(newVoteType.equals(RedditUtils.DIR_UPVOTE)) { - getItem(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_UPVOTE); - upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.upvoted), android.graphics.PorterDuff.Mode.SRC_IN); - scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.upvoted)); - } else { - getItem(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_NO_VOTE); - upvoteButton.clearColorFilter(); - scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.defaultTextColor)); - } - - downvoteButton.clearColorFilter(); - scoreTextView.setText(Integer.toString(getItem(getAdapterPosition()).getScore() + getItem(getAdapterPosition()).getVoteType())); - } - - @Override - public void onVoteThingFail(int position) { } - }, getItem(getAdapterPosition()).getFullName(), newVoteType, getAdapterPosition()); - }); - - downvoteButton.setOnClickListener(view -> { - if(mAccessToken == null) { - Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); - return; - } - - int previousVoteType = getItem(getAdapterPosition()).getVoteType(); - String newVoteType; - - upvoteButton.clearColorFilter(); - - if(previousVoteType != CommentData.VOTE_TYPE_DOWNVOTE) { - //Not downvoted before - getItem(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); - newVoteType = RedditUtils.DIR_DOWNVOTE; - downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.downvoted), android.graphics.PorterDuff.Mode.SRC_IN); - scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.downvoted)); - } else { - //Downvoted before - getItem(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_NO_VOTE); - newVoteType = RedditUtils.DIR_UNVOTE; - downvoteButton.clearColorFilter(); - scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.defaultTextColor)); - } - - scoreTextView.setText(Integer.toString(getItem(getAdapterPosition()).getScore() + getItem(getAdapterPosition()).getVoteType())); - - VoteThing.voteThing(mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position1) { - if(newVoteType.equals(RedditUtils.DIR_DOWNVOTE)) { - getItem(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_DOWNVOTE); - downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.downvoted), android.graphics.PorterDuff.Mode.SRC_IN); - scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.downvoted)); - } else { - getItem(getAdapterPosition()).setVoteType(CommentData.VOTE_TYPE_NO_VOTE); - downvoteButton.clearColorFilter(); - scoreTextView.setTextColor(ContextCompat.getColor(mContext, R.color.defaultTextColor)); - } - - upvoteButton.clearColorFilter(); - scoreTextView.setText(Integer.toString(getItem(getAdapterPosition()).getScore() + getItem(getAdapterPosition()).getVoteType())); - } - - @Override - public void onVoteThingFail(int position1) { } - }, getItem(getAdapterPosition()).getFullName(), newVoteType, getAdapterPosition()); - }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 164b21a1..fa2c097f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit; +import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.graphics.ColorFilter; @@ -18,7 +19,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.browser.customtabs.CustomTabsIntent; import androidx.cardview.widget.CardView; import androidx.core.content.ContextCompat; import androidx.paging.PagedListAdapter; @@ -401,20 +401,14 @@ class PostRecyclerViewAdapter extends PagedListAdapter { - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); - // add share action to menu list - builder.addDefaultShareMenuItem(); - builder.setToolbarColor(mContext.getResources().getColor(R.color.colorPrimary)); - CustomTabsIntent customTabsIntent = builder.build(); + Intent intent = new Intent(mContext, LinkResolverActivity.class); 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; + if(uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); + } else { + intent.setData(uri); } - customTabsIntent.launchUrl(mContext, Uri.parse(uriString)); + mContext.startActivity(intent); }); break; case Post.GIF_VIDEO_TYPE: @@ -462,20 +456,14 @@ class PostRecyclerViewAdapter extends PagedListAdapter { - CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); - // add share action to menu list - builder.addDefaultShareMenuItem(); - builder.setToolbarColor(mContext.getResources().getColor(R.color.colorPrimary)); - CustomTabsIntent customTabsIntent = builder.build(); + Intent intent = new Intent(mContext, LinkResolverActivity.class); 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; + if(uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl())); + } else { + intent.setData(uri); } - customTabsIntent.launchUrl(mContext, Uri.parse(uriString)); + mContext.startActivity(intent); }); break; case Post.TEXT_TYPE: @@ -626,11 +614,15 @@ class PostRecyclerViewAdapter extends PagedListAdapter { - Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - String extraText = title + "\n" + permalink; - intent.putExtra(Intent.EXTRA_TEXT, extraText); - mContext.startActivity(Intent.createChooser(intent, "Share")); + try { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + String extraText = title + "\n" + permalink; + intent.putExtra(Intent.EXTRA_TEXT, extraText); + mContext.startActivity(Intent.createChooser(intent, mContext.getString(R.string.share))); + } catch (ActivityNotFoundException e) { + Toast.makeText(mContext, R.string.no_activity_found_for_share, Toast.LENGTH_SHORT).show(); + } }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java index 98374317..5c6b9945 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubmitPost.java @@ -212,7 +212,6 @@ class SubmitPost { submitPostListener.submitFailed(null); } } else { - Log.i("call_failed", response.message()); submitPostListener.submitFailed(response.message()); } } -- cgit v1.2.3