From 53acf695b64cbaa8c220df376ceee81d82152adb Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Tue, 28 Nov 2023 12:36:02 -0500 Subject: Emotes are now clickable. Fix IndexOutOfBoundsException in Utils.parseRedditImagesBlock when parsing i.redd.it media. --- .../infinityforreddit/MediaMetadata.java | 2 +- .../activities/CommentActivity.java | 15 ++++++-- .../activities/FullMarkdownActivity.java | 15 ++++++-- .../infinityforreddit/activities/WikiActivity.java | 15 ++++++-- .../CommentsListingRecyclerViewAdapter.java | 45 +++++++++++++--------- .../adapters/CommentsRecyclerViewAdapter.java | 20 ++++++++-- .../adapters/PostDetailRecyclerViewAdapter.java | 22 +++++++++-- .../adapters/RulesRecyclerViewAdapter.java | 15 ++++++-- .../fragments/SidebarFragment.java | 15 ++++++-- .../infinityforreddit/markdown/EmotePlugin.java | 32 ++++++++++++--- .../services/DownloadMediaService.java | 11 +++++- .../infinityforreddit/utils/Utils.java | 2 +- 12 files changed, 160 insertions(+), 49 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaMetadata.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaMetadata.java index fd7d2a6a..db41399c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MediaMetadata.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MediaMetadata.java @@ -22,7 +22,7 @@ public class MediaMetadata implements Parcelable { this.e = e; isGIF = !e.equalsIgnoreCase("image"); String path = Uri.parse(original.url).getPath(); - this.fileName = path == null ? (isGIF ? "Animated.gif" : "Image.jpg") : path.substring(1); + this.fileName = path == null ? (isGIF ? "Animated.gif" : "Image.jpg") : path.substring(path.lastIndexOf('/') + 1); this.original = original; this.downscaled = downscaled; } 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 df2d6faa..316fe0c4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -218,14 +218,23 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA } }; EmoteCloseBracketInlineProcessor emoteCloseBracketInlineProcessor = new EmoteCloseBracketInlineProcessor(); - emotePlugin = EmotePlugin.create(this); + emotePlugin = EmotePlugin.create(this, mediaMetadata -> { + Intent imageIntent = new Intent(this, ViewImageOrGifActivity.class); + if (mediaMetadata.isGIF) { + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + } + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, intent.getStringExtra(EXTRA_SUBREDDIT_NAME_KEY)); + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + }); ImageAndGifPlugin imageAndGifPlugin = new ImageAndGifPlugin(); imageAndGifEntry = new ImageAndGifEntry(this, mGlide, mediaMetadata -> { Intent imageIntent = new Intent(this, ViewImageOrGifActivity.class); if (mediaMetadata.isGIF) { - imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); - } else { imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); } imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, intent.getStringExtra(EXTRA_SUBREDDIT_NAME_KEY)); imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); 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 e3e5a263..270771cb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java @@ -148,15 +148,24 @@ public class FullMarkdownActivity extends BaseActivity { } }; EmoteCloseBracketInlineProcessor emoteCloseBracketInlineProcessor = new EmoteCloseBracketInlineProcessor(); - emotePlugin = EmotePlugin.create(this); + emotePlugin = EmotePlugin.create(this, mediaMetadata -> { + Intent intent = new Intent(this, ViewImageOrGifActivity.class); + if (mediaMetadata.isGIF) { + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + } + intent.putExtra(ViewImageOrGifActivity.EXTRA_IS_NSFW, isNsfw); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + }); ImageAndGifPlugin imageAndGifPlugin = new ImageAndGifPlugin(); imageAndGifEntry = new ImageAndGifEntry(this, Glide.with(this), mediaMetadata -> { Intent intent = new Intent(this, ViewImageOrGifActivity.class); if (mediaMetadata.isGIF) { - intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); - } else { intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); } intent.putExtra(ViewImageOrGifActivity.EXTRA_IS_NSFW, isNsfw); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java index a65a49fc..57ec0ff1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java @@ -188,15 +188,24 @@ public class WikiActivity extends BaseActivity { return true; }; emoteCloseBracketInlineProcessor = new EmoteCloseBracketInlineProcessor(); - emotePlugin = EmotePlugin.create(this); + emotePlugin = EmotePlugin.create(this, mediaMetadata -> { + Intent intent = new Intent(this, ViewImageOrGifActivity.class); + if (mediaMetadata.isGIF) { + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + } + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mSubredditName); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + }); imageAndGifPlugin = new ImageAndGifPlugin(); imageAndGifEntry = new ImageAndGifEntry(this, mGlide, mediaMetadata -> { Intent intent = new Intent(this, ViewImageOrGifActivity.class); if (mediaMetadata.isGIF) { - intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); - } else { intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); } intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mSubredditName); intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java index df672f19..1964f7f7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsListingRecyclerViewAdapter.java @@ -37,7 +37,6 @@ import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.MarkwonPlugin; import io.noties.markwon.core.MarkwonTheme; import me.saket.bettermovementmethod.BetterLinkMovementMethod; -import ml.docilealligator.infinityforreddit.MediaMetadata; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.SaveThing; @@ -52,12 +51,12 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSh import ml.docilealligator.infinityforreddit.comment.Comment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.CommentIndentationView; -import ml.docilealligator.infinityforreddit.markdown.CustomMarkwonAdapter; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; import ml.docilealligator.infinityforreddit.customviews.SpoilerOnClickTextView; import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; import ml.docilealligator.infinityforreddit.databinding.ItemCommentBinding; +import ml.docilealligator.infinityforreddit.markdown.CustomMarkwonAdapter; import ml.docilealligator.infinityforreddit.markdown.EmoteCloseBracketInlineProcessor; import ml.docilealligator.infinityforreddit.markdown.EmotePlugin; import ml.docilealligator.infinityforreddit.markdown.ImageAndGifEntry; @@ -182,26 +181,36 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter { + Intent intent = new Intent(activity, ViewImageOrGifActivity.class); + if (mediaMetadata.isGIF) { + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + } + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, username); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + if (canStartActivity) { + canStartActivity = false; + activity.startActivity(intent); + } + }); mImageAndGifPlugin = new ImageAndGifPlugin(); mMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity, miscPlugin, mEmoteCloseBracketInlineProcessor, mEmotePlugin, mImageAndGifPlugin, mCommentColor, commentSpoilerBackgroundColor, onLinkLongClickListener); - mImageAndGifEntry = new ImageAndGifEntry(activity, Glide.with(activity), new ImageAndGifEntry.OnItemClickListener() { - @Override - public void onItemClick(MediaMetadata mediaMetadata) { - Intent intent = new Intent(activity, ViewImageOrGifActivity.class); - if (mediaMetadata.isGIF) { - intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); - } else { - intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); - } - intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, username); - intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); - if (canStartActivity) { - canStartActivity = false; - activity.startActivity(intent); - } + mImageAndGifEntry = new ImageAndGifEntry(activity, Glide.with(activity), mediaMetadata -> { + Intent intent = new Intent(activity, ViewImageOrGifActivity.class); + if (mediaMetadata.isGIF) { + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + } + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, username); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + if (canStartActivity) { + canStartActivity = false; + activity.startActivity(intent); } }); recycledViewPool = new RecyclerView.RecycledViewPool(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index 672a2be0..8aa76ba7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -215,7 +215,21 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter { + Intent intent = new Intent(activity, ViewImageOrGifActivity.class); + if (mediaMetadata.isGIF) { + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + } + intent.putExtra(ViewImageOrGifActivity.EXTRA_IS_NSFW, post.isNSFW()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + if (canStartActivity) { + canStartActivity = false; + activity.startActivity(intent); + } + }); mImageAndGifPlugin = new ImageAndGifPlugin(); mCommentMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity, miscPlugin, mEmoteCloseBracketInlineProcessor, mEmotePlugin, mImageAndGifPlugin, mCommentTextColor, @@ -229,9 +243,9 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(activity, ViewImageOrGifActivity.class); if (mediaMetadata.isGIF) { - intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); - } else { intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); } intent.putExtra(ViewImageOrGifActivity.EXTRA_IS_NSFW, post.isNSFW()); intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index 0f433688..a7c36f73 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -90,7 +90,6 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.ShareLinkBottom import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView; -import ml.docilealligator.infinityforreddit.markdown.CustomMarkwonAdapter; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; @@ -103,6 +102,7 @@ import ml.docilealligator.infinityforreddit.databinding.ItemPostDetailVideoAndGi import ml.docilealligator.infinityforreddit.databinding.ItemPostDetailVideoAutoplayBinding; import ml.docilealligator.infinityforreddit.databinding.ItemPostDetailVideoAutoplayLegacyControllerBinding; import ml.docilealligator.infinityforreddit.fragments.ViewPostDetailFragment; +import ml.docilealligator.infinityforreddit.markdown.CustomMarkwonAdapter; import ml.docilealligator.infinityforreddit.markdown.EmoteCloseBracketInlineProcessor; import ml.docilealligator.infinityforreddit.markdown.EmotePlugin; import ml.docilealligator.infinityforreddit.markdown.ImageAndGifEntry; @@ -388,7 +388,21 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { + Intent intent = new Intent(activity, ViewImageOrGifActivity.class); + if (mediaMetadata.isGIF) { + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + } + intent.putExtra(ViewImageOrGifActivity.EXTRA_IS_NSFW, post.isNSFW()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + if (canStartActivity) { + canStartActivity = false; + activity.startActivity(intent); + } + }); mImageAndGifPlugin = new ImageAndGifPlugin(); mPostDetailMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity, miscPlugin, mEmoteCloseBracketInlineProcessor, mEmotePlugin, mImageAndGifPlugin, markdownColor, @@ -399,9 +413,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { Intent intent = new Intent(activity, ViewImageOrGifActivity.class); if (mediaMetadata.isGIF) { - intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); - } else { intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); } intent.putExtra(ViewImageOrGifActivity.EXTRA_IS_NSFW, post.isNSFW()); intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, post.getSubredditName()); 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 8d7ce6dc..554e3b06 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RulesRecyclerViewAdapter.java @@ -95,16 +95,25 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter { + Intent imageIntent = new Intent(activity, ViewImageOrGifActivity.class); + if (mediaMetadata.isGIF) { + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + } + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName); + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + }); imageAndGifPlugin = new ImageAndGifPlugin(); imageAndGifEntry = new ImageAndGifEntry(activity, Glide.with(activity), mediaMetadata -> { Intent imageIntent = new Intent(activity, ViewImageOrGifActivity.class); if (mediaMetadata.isGIF) { - imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); - } else { imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); } imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName); imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); 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 d84617cf..a30fe3c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java @@ -159,16 +159,25 @@ public class SidebarFragment extends Fragment { return true; }; EmoteCloseBracketInlineProcessor emoteCloseBracketInlineProcessor = new EmoteCloseBracketInlineProcessor(); - emotePlugin = EmotePlugin.create(activity); + emotePlugin = EmotePlugin.create(activity, mediaMetadata -> { + Intent imageIntent = new Intent(activity, ViewImageOrGifActivity.class); + if (mediaMetadata.isGIF) { + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); + } + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName); + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); + }); ImageAndGifPlugin imageAndGifPlugin = new ImageAndGifPlugin(); imageAndGifEntry = new ImageAndGifEntry(activity, Glide.with(this), mediaMetadata -> { Intent imageIntent = new Intent(activity, ViewImageOrGifActivity.class); if (mediaMetadata.isGIF) { - imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); - } else { imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mediaMetadata.original.url); + } else { + imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mediaMetadata.original.url); } imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, subredditName); imageIntent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mediaMetadata.fileName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/EmotePlugin.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/EmotePlugin.java index 2cdae1a4..be4e1883 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/EmotePlugin.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/EmotePlugin.java @@ -3,6 +3,8 @@ package ml.docilealligator.infinityforreddit.markdown; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; import android.text.Spanned; +import android.text.style.ClickableSpan; +import android.view.View; import android.widget.TextView; import androidx.annotation.NonNull; @@ -33,6 +35,7 @@ import io.noties.markwon.image.AsyncDrawableLoader; import io.noties.markwon.image.AsyncDrawableScheduler; import io.noties.markwon.image.DrawableUtils; import io.noties.markwon.image.ImageProps; +import ml.docilealligator.infinityforreddit.MediaMetadata; import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -41,6 +44,7 @@ public class EmotePlugin extends AbstractMarkwonPlugin { private final GlideAsyncDrawableLoader glideAsyncDrawableLoader; private boolean dataSavingMode; private boolean disableImagePreview; + private OnEmoteClickListener onEmoteClickListener; public interface GlideStore { @@ -50,8 +54,12 @@ public class EmotePlugin extends AbstractMarkwonPlugin { void cancel(@NonNull Target target); } + public interface OnEmoteClickListener { + void onEmoteClick(MediaMetadata mediaMetadata); + } + @NonNull - public static EmotePlugin create(@NonNull final BaseActivity baseActivity) { + public static EmotePlugin create(@NonNull final BaseActivity baseActivity, @NonNull final OnEmoteClickListener onEmoteClickListener) { // @since 4.5.0 cache RequestManager // sometimes `cancel` would be called after activity is destroyed, // so `Glide.with(baseActivity)` will throw an exception @@ -67,11 +75,13 @@ public class EmotePlugin extends AbstractMarkwonPlugin { public void cancel(@NonNull Target target) { requestManager.clear(target); } - }); + }, onEmoteClickListener); } @NonNull - public static EmotePlugin create(@NonNull final BaseActivity baseActivity, boolean dataSavingMode, boolean disableImagePreview) { + public static EmotePlugin create(@NonNull final BaseActivity baseActivity, boolean dataSavingMode, + boolean disableImagePreview, + @NonNull final OnEmoteClickListener onEmoteClickListener) { // @since 4.5.0 cache RequestManager // sometimes `cancel` would be called after activity is destroyed, // so `Glide.with(baseActivity)` will throw an exception @@ -87,11 +97,12 @@ public class EmotePlugin extends AbstractMarkwonPlugin { public void cancel(@NonNull Target target) { requestManager.clear(target); } - }, dataSavingMode, disableImagePreview); + }, dataSavingMode, disableImagePreview, onEmoteClickListener); } @SuppressWarnings("WeakerAccess") - EmotePlugin(@NonNull final BaseActivity baseActivity, @NonNull GlideStore glideStore) { + EmotePlugin(@NonNull final BaseActivity baseActivity, @NonNull GlideStore glideStore, + @NonNull final OnEmoteClickListener onEmoteClickListener) { this.glideAsyncDrawableLoader = new GlideAsyncDrawableLoader(glideStore); String dataSavingModeString = baseActivity.getDefaultSharedPreferences().getString(SharedPreferencesUtils.DATA_SAVING_MODE, SharedPreferencesUtils.DATA_SAVING_MODE_OFF); if (dataSavingModeString.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ALWAYS)) { @@ -100,13 +111,16 @@ public class EmotePlugin extends AbstractMarkwonPlugin { dataSavingMode = Utils.getConnectedNetwork(baseActivity) == Utils.NETWORK_TYPE_CELLULAR; } disableImagePreview = baseActivity.getDefaultSharedPreferences().getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false); + this.onEmoteClickListener = onEmoteClickListener; } @SuppressWarnings("WeakerAccess") - EmotePlugin(@NonNull final BaseActivity baseActivity, @NonNull GlideStore glideStore, boolean dataSavingMode, boolean disableImagePreview) { + EmotePlugin(@NonNull final BaseActivity baseActivity, @NonNull GlideStore glideStore, boolean dataSavingMode, + boolean disableImagePreview, @NonNull final OnEmoteClickListener onEmoteClickListener) { this.glideAsyncDrawableLoader = new GlideAsyncDrawableLoader(glideStore); this.dataSavingMode = dataSavingMode; this.disableImagePreview = disableImagePreview; + this.onEmoteClickListener = onEmoteClickListener; } @Override @@ -169,6 +183,12 @@ public class EmotePlugin extends AbstractMarkwonPlugin { ImageProps.IMAGE_SIZE.set(props, null); visitor.setSpans(length, spanFactory.getSpans(configuration, props)); + visitor.setSpans(length, new ClickableSpan() { + @Override + public void onClick(@NonNull View widget) { + onEmoteClickListener.onEmoteClick(emote.getMediaMetadata()); + } + }); }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java index c2152229..cc7f7910 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java @@ -267,7 +267,16 @@ public class DownloadMediaService extends Service { } else { ContentValues contentValues = new ContentValues(); contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, destinationFileName); - contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/mpeg" : "image/jpeg"); + String mimeType; + switch (mediaType) { + case EXTRA_MEDIA_TYPE_VIDEO: + mimeType = "video/mpeg"; + case EXTRA_MEDIA_TYPE_GIF: + mimeType = "image/gif"; + default: + mimeType = "image/jpeg"; + } + contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType); contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, destinationFileUriString); contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java index e945fa71..ea8adeb2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java @@ -148,7 +148,7 @@ public final class Utils { //Minus "](".length() caption = markdownStringBuilder.substring(matcher.start() + 1, urlStartIndex - 2); } else { - id = markdownStringBuilder.substring(matcher.start() + iReddItLength, matcher.start() + markdownStringBuilder.indexOf(".", iReddItLength)); + id = markdownStringBuilder.substring(matcher.start() + iReddItLength, markdownStringBuilder.indexOf(".", matcher.start() + iReddItLength)); } MediaMetadata mediaMetadata = mediaMetadataMap.get(id); -- cgit v1.2.3