From 3cd686bfd8db39525ac45b45c0254b23979e3ab9 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sun, 19 Nov 2023 23:25:42 -0500 Subject: Click to open reddit preview images in posts. Fix IllegalArgumentException in writeResponseBodyToDisk in DownloadMediaService. --- .../infinityforreddit/activities/CommentActivity.java | 9 ++++++++- .../activities/FullMarkdownActivity.java | 10 +++++++++- .../infinityforreddit/activities/WikiActivity.java | 10 +++++++++- .../adapters/PostDetailRecyclerViewAdapter.java | 16 +++++++++++++++- .../adapters/RulesRecyclerViewAdapter.java | 10 +++++++++- .../infinityforreddit/fragments/SidebarFragment.java | 10 +++++++++- .../infinityforreddit/markdown/ImageAndGifEntry.java | 17 ++++++++++++++++- .../infinityforreddit/markdown/MarkdownUtils.java | 9 +++------ .../ml/docilealligator/infinityforreddit/post/Post.java | 4 ++++ .../services/DownloadMediaService.java | 2 +- .../docilealligator/infinityforreddit/utils/Utils.java | 5 ++--- 11 files changed, 85 insertions(+), 17 deletions(-) (limited to 'app/src/main') 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 e62f8773..3ad2c699 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java @@ -64,8 +64,10 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed; import ml.docilealligator.infinityforreddit.databinding.ActivityCommentBinding; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifEntry; import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; +import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; import okhttp3.ConnectionPool; @@ -211,7 +213,12 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA ImageAndGifPlugin imageAndGifPlugin = new ImageAndGifPlugin(); Markwon postBodyMarkwon = MarkdownUtils.createFullRedditMarkwon(this, miscPlugin, imageAndGifPlugin, parentTextColor, parentSpoilerBackgroundColor, null); - MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(this, mGlide); + MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(new ImageAndGifEntry(this, mGlide, new ImageAndGifEntry.OnItemClickListener() { + @Override + public void onItemClick(Post.MediaMetadata mediaMetadata) { + + } + })); binding.commentContentMarkdownView.setLayoutManager(new LinearLayoutManagerBugFixed(this)); binding.commentContentMarkdownView.setAdapter(markwonAdapter); markwonAdapter.setMarkdown(postBodyMarkwon, parentBodyMarkdown); 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 d92ecda7..73092f1d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FullMarkdownActivity.java @@ -44,8 +44,10 @@ import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifEntry; import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; +import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class FullMarkdownActivity extends BaseActivity { @@ -144,7 +146,13 @@ public class FullMarkdownActivity extends BaseActivity { Markwon markwon = MarkdownUtils.createFullRedditMarkwon(this, miscPlugin, imageAndGifPlugin, markdownColor, spoilerBackgroundColor, null); - MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(this, Glide.with(this)); + MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter(new ImageAndGifEntry(this, Glide.with(this), + new ImageAndGifEntry.OnItemClickListener() { + @Override + public void onItemClick(Post.MediaMetadata mediaMetadata) { + + } + })); LinearLayoutManagerBugFixed linearLayoutManager = new SwipeLockLinearLayoutManager(this, new SwipeLockInterface() { @Override public void lockSwipe() { 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 56bef8fb..01acf928 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java @@ -52,8 +52,10 @@ import ml.docilealligator.infinityforreddit.customviews.SwipeLockInterface; import ml.docilealligator.infinityforreddit.customviews.SwipeLockLinearLayoutManager; import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; +import ml.docilealligator.infinityforreddit.markdown.ImageAndGifEntry; import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; +import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -181,7 +183,13 @@ public class WikiActivity extends BaseActivity { markwon = MarkdownUtils.createFullRedditMarkwon(this, miscPlugin, imageAndGifPlugin, markdownColor, spoilerBackgroundColor, onLinkLongClickListener); - markwonAdapter = MarkdownUtils.createTablesAdapter(this, mGlide); + markwonAdapter = MarkdownUtils.createTablesAdapter(new ImageAndGifEntry(this, mGlide, + new ImageAndGifEntry.OnItemClickListener() { + @Override + public void onItemClick(Post.MediaMetadata mediaMetadata) { + + } + })); LinearLayoutManagerBugFixed linearLayoutManager = new SwipeLockLinearLayoutManager(this, new SwipeLockInterface() { @Override public void lockSwipe() { 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 30a3068f..63206e06 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -103,6 +103,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.ImageAndGifEntry; import ml.docilealligator.infinityforreddit.markdown.ImageAndGifPlugin; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; import ml.docilealligator.infinityforreddit.post.Post; @@ -292,7 +293,20 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { private BaseActivity activity; @@ -141,7 +143,13 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter { private BaseActivity baseActivity; private RequestManager glide; + private OnItemClickListener onItemClickListener; - public ImageAndGifEntry(BaseActivity baseActivity, RequestManager glide) { + public ImageAndGifEntry(BaseActivity baseActivity, RequestManager glide, OnItemClickListener onItemClickListener) { this.baseActivity = baseActivity; this.glide = glide; + this.onItemClickListener = onItemClickListener; } @NonNull @@ -39,6 +42,8 @@ public class ImageAndGifEntry extends MarkwonAdapter.Entry imageRequestBuilder = glide.load(node.mediaMetadata.original.url).listener(holder.requestListener); @@ -62,6 +67,7 @@ public class ImageAndGifEntry extends MarkwonAdapter.Entry requestListener; + ImageAndGifBlock imageAndGifBlock; public Holder(@NonNull MarkdownImageAndGifBlockBinding binding) { super(binding.getRoot()); @@ -81,6 +87,11 @@ public class ImageAndGifEntry extends MarkwonAdapter.Entry { + if (imageAndGifBlock != null) { + onItemClickListener.onItemClick(imageAndGifBlock.mediaMetadata); + } + }); } /*private final RequestListener requestListener = new RequestListener() { @@ -195,4 +206,8 @@ public class ImageAndGifEntry extends MarkwonAdapter.Entry builder .tableLayout(R.layout.adapter_table_block, R.id.table_layout) .textLayoutIsRoot(R.layout.view_table_entry_cell))) - .include(ImageAndGifBlock.class, new ImageAndGifEntry(baseActivity, glide)) + .include(ImageAndGifBlock.class, imageAndGifEntry) .build(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java index e91062b2..a7cb137b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit.post; +import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; @@ -771,12 +772,15 @@ public class Post implements Parcelable { public String id; //E.g. Image public String e; + public String fileName; public MediaItem original; public MediaItem downscaled; public MediaMetadata(String id, String e, MediaItem original, MediaItem downscaled) { this.id = id; this.e = e; + String path = Uri.parse(original.url).getPath(); + this.fileName = path == null ? "Image.jpg" : path.substring(1); this.original = original; this.downscaled = downscaled; } 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 d247ad4c..c2152229 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,7 @@ 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/*" : "image/*"); + contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mediaType == EXTRA_MEDIA_TYPE_VIDEO ? "video/mpeg" : "image/jpeg"); 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 ed88d6f9..f5f3b163 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java @@ -150,8 +150,7 @@ public final class Utils { } public static String parseInlineRedditImages(String markdown) { - return markdown; - /*StringBuilder markdownStringBuilder = new StringBuilder(markdown); + StringBuilder markdownStringBuilder = new StringBuilder(markdown); Pattern inlineRedditImagePattern = REGEX_PATTERNS[6]; Matcher matcher = inlineRedditImagePattern.matcher(markdownStringBuilder); int start = 0; @@ -162,7 +161,7 @@ public final class Utils { matcher = inlineRedditImagePattern.matcher(markdownStringBuilder); } - return markdownStringBuilder.toString();*/ + return markdownStringBuilder.toString(); } public static String trimTrailingWhitespace(String source) { -- cgit v1.2.3