diff options
author | Docile-Alligator <chineseperson5@gmail.com> | 2021-12-02 11:39:03 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-02 11:39:03 +0000 |
commit | 49e0df2980a561d6bb29223d6bcc82c483e5af02 (patch) | |
tree | f077c16f76153066bb835ceba01575e2672d6e52 /app | |
parent | f39f608cde18a434a907a55cf5985bc18e0f2950 (diff) | |
parent | 951024f08671ed0013ad469a5024ddd303bda07c (diff) | |
download | infinity-for-reddit-49e0df2980a561d6bb29223d6bcc82c483e5af02.tar infinity-for-reddit-49e0df2980a561d6bb29223d6bcc82c483e5af02.tar.gz infinity-for-reddit-49e0df2980a561d6bb29223d6bcc82c483e5af02.tar.bz2 infinity-for-reddit-49e0df2980a561d6bb29223d6bcc82c483e5af02.tar.lz infinity-for-reddit-49e0df2980a561d6bb29223d6bcc82c483e5af02.tar.xz infinity-for-reddit-49e0df2980a561d6bb29223d6bcc82c483e5af02.tar.zst infinity-for-reddit-49e0df2980a561d6bb29223d6bcc82c483e5af02.zip |
Merge pull request #561 from scria1000/master
Download unprocessed gallery picture
Diffstat (limited to 'app')
3 files changed, 54 insertions, 7 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryImageOrGifFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryImageOrGifFragment.java index 942a90e4..2d8526bc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryImageOrGifFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewRedditGalleryImageOrGifFragment.java @@ -110,6 +110,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { private boolean isDownloading = false; private boolean isActionBarHidden = false; private boolean isUseBottomCaption = false; + private boolean isFallback = false; public ViewRedditGalleryImageOrGifFragment() { // Required empty public constructor @@ -176,6 +177,21 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { view.setQuickScaleEnabled(true); view.resetScaleAndCenter(); } + + @Override + public void onImageLoadError(Exception e) { + e.printStackTrace(); + // For issue #558 + // Make sure it's not stuck in a loop if it comes to that + // Fallback url should be empty if it's not an album item + if (!isFallback && media.hasFallback()) { + imageView.cancel(); + isFallback = true; + loadImage(); + } else { + isFallback = false; + } + } }); } } @@ -314,7 +330,12 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { } private void loadImage() { - imageView.showImage(Uri.parse(media.url)); + if(isFallback) { + imageView.showImage(Uri.parse(media.fallbackUrl)); + } + else{ + imageView.showImage(Uri.parse(media.url)); + } } @Override @@ -371,16 +392,18 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { isDownloading = false; Intent intent = new Intent(activity, DownloadMediaService.class); - intent.putExtra(DownloadMediaService.EXTRA_URL, media.url); - intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, media.mediaType == Post.Gallery.TYPE_GIF ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF : DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE); + intent.putExtra(DownloadMediaService.EXTRA_URL, media.hasFallback() ? media.fallbackUrl : media.url); // Retrieve original instead of the one additionally compressed by reddit + intent.putExtra(DownloadMediaService.EXTRA_MEDIA_TYPE, media.mediaType == Post.Gallery.TYPE_GIF ? DownloadMediaService.EXTRA_MEDIA_TYPE_GIF: DownloadMediaService.EXTRA_MEDIA_TYPE_IMAGE); intent.putExtra(DownloadMediaService.EXTRA_FILE_NAME, media.fileName); intent.putExtra(DownloadMediaService.EXTRA_SUBREDDIT_NAME, subredditName); ContextCompat.startForegroundService(activity, intent); Toast.makeText(activity, R.string.download_started, Toast.LENGTH_SHORT).show(); } + //TODO: Find a way to share original image, Glide messes with the size and quality, + // compression should be up to the app being shared with (WhatsApp for example) private void shareImage() { - glide.asBitmap().load(media.url).into(new CustomTarget<Bitmap>() { + glide.asBitmap().load(media.hasFallback() ? media.fallbackUrl : media.url).into(new CustomTarget<Bitmap>() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { if (activity.getExternalCacheDir() != null) { @@ -496,7 +519,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { super.onResume(); SubsamplingScaleImageView ssiv = imageView.getSSIV(); if (ssiv == null || !ssiv.hasImage()) { - imageView.showImage(Uri.parse(media.url)); + loadImage(); } } @@ -504,6 +527,7 @@ public class ViewRedditGalleryImageOrGifFragment extends Fragment { public void onDestroyView() { super.onDestroyView(); imageView.cancel(); + isFallback = false; SubsamplingScaleImageView subsamplingScaleImageView = imageView.getSSIV(); if (subsamplingScaleImageView != null) { subsamplingScaleImageView.recycle(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index 860feaf2..cf070e4e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.post; import android.net.Uri; import android.os.Handler; import android.text.Html; +import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; @@ -575,8 +576,17 @@ public class ParsePost { previews.add(new Post.Preview(galleryItemUrl, singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.X_KEY), singleGalleryObject.getJSONObject(JSONUtils.S_KEY).getInt(JSONUtils.Y_KEY), galleryItemCaption, galleryItemCaptionUrl)); } + + Post.Gallery postGalleryItem = new Post.Gallery(mimeType, galleryItemUrl, "", subredditName + "-" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1), galleryItemCaption, galleryItemCaptionUrl); + + // For issue #558 + // Construct a fallback image url + if(!TextUtils.isEmpty(galleryItemUrl) && !TextUtils.isEmpty(mimeType) && (mimeType.contains("jpg") || mimeType.contains("png"))) { + postGalleryItem.setFallbackUrl("https://i.redd.it/" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1)); + postGalleryItem.setHasFallback(true); + } - gallery.add(new Post.Gallery(mimeType, galleryItemUrl, subredditName + "-" + galleryId + "." + mimeType.substring(mimeType.lastIndexOf("/") + 1), galleryItemCaption, galleryItemCaptionUrl)); + gallery.add(postGalleryItem); } if (!gallery.isEmpty()) { 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 e19696a3..ce096362 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java @@ -580,14 +580,17 @@ public class Post implements Parcelable { public String mimeType; public String url; + public String fallbackUrl; + private boolean hasFallback; public String fileName; public int mediaType; public String caption; public String captionUrl; - public Gallery(String mimeType, String url, String fileName, String caption, String captionUrl) { + public Gallery(String mimeType, String url, String fallbackUrl, String fileName, String caption, String captionUrl) { this.mimeType = mimeType; this.url = url; + this.fallbackUrl = fallbackUrl; this.fileName = fileName; if (mimeType.contains("gif")) { mediaType = TYPE_GIF; @@ -603,6 +606,8 @@ public class Post implements Parcelable { protected Gallery(Parcel in) { mimeType = in.readString(); url = in.readString(); + fallbackUrl = in.readString(); + hasFallback = in.readByte() != 0; fileName = in.readString(); mediaType = in.readInt(); caption = in.readString(); @@ -630,11 +635,19 @@ public class Post implements Parcelable { public void writeToParcel(Parcel parcel, int i) { parcel.writeString(mimeType); parcel.writeString(url); + parcel.writeString(fallbackUrl); + parcel.writeByte((byte) (hasFallback ? 1 : 0)); parcel.writeString(fileName); parcel.writeInt(mediaType); parcel.writeString(caption); parcel.writeString(captionUrl); } + + public void setFallbackUrl(String fallbackUrl) { this.fallbackUrl = fallbackUrl; } + + public void setHasFallback(boolean hasFallback) { this.hasFallback = hasFallback; } + + public boolean hasFallback() { return this.hasFallback; } } public static class Preview implements Parcelable { |