From 321f4da6230fd6197e5e0773db3a8d28b01e5215 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 31 Dec 2020 14:46:19 +0800 Subject: Long click the url to see more options. Make getRedditUriByPath in LinkResolverActivity private. --- app/build.gradle | 1 + .../infinityforreddit/PullNotificationWorker.java | 4 ++-- .../activities/CommentActivity.java | 12 ++--------- .../activities/FullMarkdownActivity.java | 6 +----- .../activities/LinkResolverActivity.java | 9 ++++++-- .../CommentAndPostRecyclerViewAdapter.java | 24 ++++------------------ .../CommentsListingRecyclerViewAdapter.java | 6 +----- .../adapters/MessageRecyclerViewAdapter.java | 8 ++------ .../adapters/PostRecyclerViewAdapter.java | 12 ++--------- .../PrivateMessagesDetailRecyclerViewAdapter.java | 6 +----- .../adapters/RulesRecyclerViewAdapter.java | 6 +----- .../UrlMenuBottomSheetFragment.java | 21 +++++++++++++++---- .../fragments/SidebarFragment.java | 23 +++++++++++++-------- app/src/main/res/layout/fragment_sidebar.xml | 5 ++++- .../res/layout/fragment_url_menu_bottom_sheet.xml | 18 +++++++++++++--- app/src/main/res/values/strings.xml | 1 + 16 files changed, 76 insertions(+), 86 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ae211bd8..e9de6ce6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -132,6 +132,7 @@ dependencies { implementation "io.noties.markwon:recycler-table:$markwonVersion" implementation "io.noties.markwon:simple-ext:$markwonVersion" implementation 'com.atlassian.commonmark:commonmark-ext-gfm-tables:0.14.0' + implementation 'me.saket:better-link-movement-method:2.2.0' // Animations implementation 'com.airbnb.android:lottie:3.4.4' diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 82260f00..538c4a2c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -139,7 +139,7 @@ public class PullNotificationWorker extends Worker { if (kind.equals(Message.TYPE_COMMENT)) { Intent intent = new Intent(context, LinkResolverActivity.class); - Uri uri = LinkResolverActivity.getRedditUriByPath(message.getContext()); + Uri uri = Uri.parse(message.getContext()); intent.setData(uri); intent.putExtra(LinkResolverActivity.EXTRA_NEW_ACCOUNT_NAME, accountName); intent.putExtra(LinkResolverActivity.EXTRA_MESSAGE_FULLNAME, message.getFullname()); @@ -152,7 +152,7 @@ public class PullNotificationWorker extends Worker { builder.setContentIntent(summaryPendingIntent); } else if (kind.equals(Message.TYPE_LINK)) { Intent intent = new Intent(context, LinkResolverActivity.class); - Uri uri = LinkResolverActivity.getRedditUriByPath(message.getContext()); + Uri uri = Uri.parse(message.getContext()); intent.setData(uri); intent.putExtra(LinkResolverActivity.EXTRA_NEW_ACCOUNT_NAME, accountName); intent.putExtra(LinkResolverActivity.EXTRA_MESSAGE_FULLNAME, message.getFullname()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java index c12403de..78bfe015 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -154,11 +154,7 @@ public class CommentActivity extends BaseActivity { builder.linkResolver((view, link) -> { Intent intent = new Intent(CommentActivity.this, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); startActivity(intent); }); } @@ -218,11 +214,7 @@ public class CommentActivity extends BaseActivity { builder.linkResolver((view, link) -> { Intent intent = new Intent(CommentActivity.this, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); startActivity(intent); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java index 5e0ce147..79e3d8a5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java @@ -191,11 +191,7 @@ public class FullMarkdownActivity extends BaseActivity { builder.linkResolver((view, link) -> { Intent intent = new Intent(FullMarkdownActivity.this, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, isNsfw); startActivity(intent); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java index 27fa8298..6a76497b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java @@ -51,7 +51,7 @@ public class LinkResolverActivity extends AppCompatActivity { @Inject CustomThemeWrapper mCustomThemeWrapper; - public static Uri getRedditUriByPath(String path) { + private Uri getRedditUriByPath(String path) { return Uri.parse("https://www.reddit.com" + path); } @@ -62,7 +62,12 @@ public class LinkResolverActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); Uri uri = getIntent().getData(); - handleUri(uri); + + if (uri.getScheme() == null && uri.getHost() == null) { + handleUri(getRedditUriByPath(uri.toString())); + } else { + handleUri(uri); + } } private void handleUri(Uri uri) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java index f86a84a2..7bb8c00c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentAndPostRecyclerViewAdapter.java @@ -332,11 +332,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, mPost.isNSFW()); mActivity.startActivity(intent); }); @@ -420,11 +416,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, mPost.isNSFW()); mActivity.startActivity(intent); }); @@ -2992,11 +2984,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mActivity, LinkResolverActivity.class); Uri uri = Uri.parse(mPost.getUrl()); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(mPost.getUrl())); - } else { - intent.setData(uri); - } + intent.setData(uri); intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, mPost.isNSFW()); mActivity.startActivity(intent); }); @@ -3120,11 +3108,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(mContext, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); mContext.startActivity(intent); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java index 04ec62f2..504b3a51 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java @@ -164,11 +164,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter { Intent intent = new Intent(mContext, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); mContext.startActivity(intent); }); } @@ -238,7 +234,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter { if (mMessageType == FetchMessage.MESSAGE_TYPE_INBOX && message.getContext() != null && !message.getContext().equals("")) { - Uri uri = LinkResolverActivity.getRedditUriByPath(message.getContext()); + Uri uri = Uri.parse(message.getContext()); Intent intent = new Intent(mContext, LinkResolverActivity.class); intent.setData(uri); mContext.startActivity(intent); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 4abf2e55..970426c3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -2359,11 +2359,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { Intent intent = new Intent(viewPrivateMessagesActivity, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); viewPrivateMessagesActivity.startActivity(intent); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java index ee728b96..337a15d6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java @@ -43,11 +43,7 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(context, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); context.startActivity(intent); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/UrlMenuBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/UrlMenuBottomSheetFragment.java index 6ce44743..c907f02b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/UrlMenuBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/UrlMenuBottomSheetFragment.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.bottomsheetfragments; import android.app.Activity; +import android.content.ActivityNotFoundException; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; @@ -25,6 +26,8 @@ import ml.docilealligator.infinityforreddit.R; public class UrlMenuBottomSheetFragment extends RoundedBottomSheetDialogFragment { public static final String EXTRA_URL = "EU"; + @BindView(R.id.link_text_view_url_menu_bottom_sheet_fragment) + TextView linkTextView; @BindView(R.id.open_link_text_view_url_menu_bottom_sheet_fragment) TextView openLinkTextView; @BindView(R.id.copy_link_text_view_url_menu_bottom_sheet_fragment) @@ -32,6 +35,7 @@ public class UrlMenuBottomSheetFragment extends RoundedBottomSheetDialogFragment @BindView(R.id.share_link_text_view_url_menu_bottom_sheet_fragment) TextView shareLinkTextView; private Activity activity; + private String url; public UrlMenuBottomSheetFragment() { // Required empty public constructor @@ -43,7 +47,14 @@ public class UrlMenuBottomSheetFragment extends RoundedBottomSheetDialogFragment View rootView = inflater.inflate(R.layout.fragment_url_menu_bottom_sheet, container, false); ButterKnife.bind(this, rootView); - String url = getArguments().getString(EXTRA_URL); + url = getArguments().getString(EXTRA_URL); + + Uri uri = Uri.parse(url); + if (uri.getScheme() == null && uri.getHost() == null) { + url = "https://www.reddit.com" + url; + } + + linkTextView.setText(url); openLinkTextView.setOnClickListener(view -> { Intent intent = new Intent(activity, LinkResolverActivity.class); @@ -68,9 +79,11 @@ public class UrlMenuBottomSheetFragment extends RoundedBottomSheetDialogFragment Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT, url); - if (intent.resolveActivity(activity.getPackageManager()) != null) { - startActivity(intent); - } else { + try { + Intent shareIntent = Intent.createChooser(intent, null); + startActivity(shareIntent); + } catch (ActivityNotFoundException e) { + e.printStackTrace(); Toast.makeText(activity, R.string.no_app, Toast.LENGTH_SHORT).show(); } dismiss(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java index 50a7a912..571350ce 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java @@ -34,17 +34,20 @@ import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; import io.noties.markwon.linkify.LinkifyPlugin; +import io.noties.markwon.movement.MovementMethodPlugin; import io.noties.markwon.recycler.MarkwonAdapter; import io.noties.markwon.recycler.table.TableEntry; import io.noties.markwon.recycler.table.TableEntryPlugin; import io.noties.markwon.simple.ext.SimpleExtPlugin; +import me.saket.bettermovementmethod.BetterLinkMovementMethod; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity; import ml.docilealligator.infinityforreddit.activities.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubredditDataAsyncTask; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.subreddit.FetchSubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditViewModel; @@ -111,16 +114,20 @@ public class SidebarFragment extends Fragment { builder.linkResolver((view, link) -> { Intent intent = new Intent(activity, LinkResolverActivity.class); Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } + intent.setData(uri); startActivity(intent); }); } }) .usePlugin(StrikethroughPlugin.create()) + .usePlugin(MovementMethodPlugin.create(BetterLinkMovementMethod.linkify(Linkify.WEB_URLS, recyclerView).setOnLinkLongClickListener((textView, url) -> { + UrlMenuBottomSheetFragment urlMenuBottomSheetFragment = new UrlMenuBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putString(UrlMenuBottomSheetFragment.EXTRA_URL, url); + urlMenuBottomSheetFragment.setArguments(bundle); + urlMenuBottomSheetFragment.show(getChildFragmentManager(), urlMenuBottomSheetFragment.getTag()); + return true; + }))) .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) .usePlugin(SimpleExtPlugin.create(plugin -> plugin.addExtension(1, '^', (configuration, props) -> { diff --git a/app/src/main/res/layout/fragment_sidebar.xml b/app/src/main/res/layout/fragment_sidebar.xml index a0773c9c..0b0e5ace 100644 --- a/app/src/main/res/layout/fragment_sidebar.xml +++ b/app/src/main/res/layout/fragment_sidebar.xml @@ -16,7 +16,10 @@ android:id="@+id/markdown_recycler_view_sidebar_fragment" android:layout_width="match_parent" android:layout_height="match_parent" - android:padding="8dp" + android:paddingTop="8dp" + android:paddingBottom="144dp" + android:paddingStart="8dp" + android:paddingEnd="8dp" android:clipToPadding="false" /> diff --git a/app/src/main/res/layout/fragment_url_menu_bottom_sheet.xml b/app/src/main/res/layout/fragment_url_menu_bottom_sheet.xml index 852c53ed..65dacae2 100644 --- a/app/src/main/res/layout/fragment_url_menu_bottom_sheet.xml +++ b/app/src/main/res/layout/fragment_url_menu_bottom_sheet.xml @@ -12,11 +12,23 @@ android:layout_height="wrap_content" android:orientation="vertical"> + + Copy Gif Link Copy Video Link Copy Link + Open Link Copied Cannot copy the link -- cgit v1.2.3