Handle no browser installed when opening links and no apps are eligible to share when sharing.

This commit is contained in:
Alex Ning 2019-09-09 16:30:26 +08:00
parent 3bb5e83d2e
commit 8ba7bd7b7e
5 changed files with 183 additions and 186 deletions

View File

@ -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<RecyclerVie
((PostDetailViewHolder) holder).linkTextView.setText(domain);
((PostDetailViewHolder) holder).mImageView.setOnClickListener(view -> {
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());
if(uri.getScheme() == null && uri.getHost() == null) {
intent.setData(LinkResolverActivity.getRedditUriByPath(mPost.getUrl()));
} else {
intent.setData(uri);
}
String uriString = mPost.getUrl();
if(!uriString.startsWith("http://") && !uriString.startsWith("https://")) {
uriString = "http://" + uriString;
}
customTabsIntent.launchUrl(mActivity, Uri.parse(uriString));
mActivity.startActivity(intent);
});
break;
case Post.GIF_VIDEO_TYPE:
@ -477,20 +471,14 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
((PostDetailViewHolder) holder).mNoPreviewLinkImageView.setVisibility(View.VISIBLE);
((PostDetailViewHolder) holder).mNoPreviewLinkImageView.setOnClickListener(view -> {
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());
if(uri.getScheme() == null && uri.getHost() == null) {
intent.setData(LinkResolverActivity.getRedditUriByPath(mPost.getUrl()));
} else {
intent.setData(uri);
}
String uriString = mPost.getUrl();
if(!uriString.startsWith("http://") && !uriString.startsWith("https://")) {
uriString = "http://" + uriString;
}
customTabsIntent.launchUrl(mActivity, Uri.parse(uriString));
mActivity.startActivity(intent);
});
break;
case Post.TEXT_TYPE:
@ -1078,11 +1066,15 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
});
mShareButton.setOnClickListener(view -> {
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<RecyclerVie
});
shareButton.setOnClickListener(view -> {
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 -> {

View File

@ -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<CommentData, R
modifyCommentBottomSheetFragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), modifyCommentBottomSheetFragment.getTag());
});
}
((DataViewHolder) holder).linearLayout.setOnClickListener(view -> {
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<CommentData, R
DataViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
linearLayout.setOnClickListener(view -> {
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());
});
}
}

View File

@ -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<Post, RecyclerView.ViewHo
((DataViewHolder) holder).linkTextView.setText(domain);
((DataViewHolder) holder).imageView.setOnClickListener(view -> {
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());
if(uri.getScheme() == null && uri.getHost() == null) {
intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl()));
} else {
intent.setData(uri);
}
String uriString = post.getUrl();
if(!uriString.startsWith("http://") && !uriString.startsWith("https://")) {
uriString = "http://" + uriString;
}
customTabsIntent.launchUrl(mContext, Uri.parse(uriString));
mContext.startActivity(intent);
});
break;
case Post.GIF_VIDEO_TYPE:
@ -462,20 +456,14 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
((DataViewHolder) holder).linkTextView.setText(noPreviewLinkDomain);
((DataViewHolder) holder).noPreviewLinkImageView.setVisibility(View.VISIBLE);
((DataViewHolder) holder).noPreviewLinkImageView.setOnClickListener(view -> {
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());
if(uri.getScheme() == null && uri.getHost() == null) {
intent.setData(LinkResolverActivity.getRedditUriByPath(post.getUrl()));
} else {
intent.setData(uri);
}
String uriString = noPreviewLinkUrl;
if(!uriString.startsWith("http://") && !uriString.startsWith("https://")) {
uriString = "http://" + uriString;
}
customTabsIntent.launchUrl(mContext, Uri.parse(uriString));
mContext.startActivity(intent);
});
break;
case Post.TEXT_TYPE:
@ -626,11 +614,15 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
});
((DataViewHolder) holder).shareButton.setOnClickListener(view -> {
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();
}
});
}
}

View File

@ -212,7 +212,6 @@ class SubmitPost {
submitPostListener.submitFailed(null);
}
} else {
Log.i("call_failed", response.message());
submitPostListener.submitFailed(response.message());
}
}

View File

@ -188,6 +188,7 @@
<string name="open_link_with">Open link with</string>
<string name="no_browser_found">No browser found</string>
<string name="no_activity_found_for_share">There is no app that can handle the share action</string>
<string name="archived_post_vote_unavailable">Archived post. Vote unavailable.</string>
<string name="archived_post_comment_unavailable">Archived post. Comment unavailable.</string>
@ -301,4 +302,6 @@
<string name="no_image_path_received">No image path received</string>
<string name="no_video_path_received">No video path received</string>
<string name="cannot_handle_intent">Could not handle the share request</string>
<string name="share">Share</string>
</resources>