From 7cc4e93ad45a79b47d5fce3f2b6a8e328ecb5f15 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Wed, 2 Aug 2023 07:52:20 +0200 Subject: [PATCH] Faster Post parsing This commit dramatically increases post parsing and loading times. However, posts on the home screen with images are bugged. --- .../PostDetailRecyclerViewAdapter.java | 144 +++++++- .../adapters/PostRecyclerViewAdapter.java | 312 ++++++++++++++---- .../infinityforlemmy/post/ParsePost.java | 8 +- .../eu/toldi/infinityforlemmy/post/Post.java | 17 +- 4 files changed, 403 insertions(+), 78 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostDetailRecyclerViewAdapter.java index 40af0f8d..b6984a45 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostDetailRecyclerViewAdapter.java @@ -42,7 +42,9 @@ import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; +import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.target.Target; +import com.bumptech.glide.request.transition.Transition; import com.google.android.exoplayer2.Tracks; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.ui.DefaultTimeBar; @@ -634,8 +636,20 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + ((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) width / height); + ((PostDetailVideoAutoplayViewHolder) holder).previewImageView.setImageDrawable(resource); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); } else { ((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1); } @@ -843,9 +857,45 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + if (preview.getPreviewHeight() == 0 || preview.getPreviewWidth() == 0) { + ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setRatio((float) height / width); + ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } + ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setImageDrawable(resource); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setImageDrawable(null); + } + }); } else { - imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView); + imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + if (preview.getPreviewHeight() == 0 || preview.getPreviewWidth() == 0) { + ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setRatio((float) height / width); + ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } + ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setImageDrawable(resource); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostDetailImageAndGifAutoplayViewHolder) holder).mImageView.setImageDrawable(null); + } + }); } } else if (holder instanceof PostDetailVideoAndGifPreviewHolder) { RequestBuilder imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) @@ -871,9 +921,45 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + if (preview.getPreviewHeight() == 0 || preview.getPreviewWidth() == 0) { + ((PostDetailVideoAndGifPreviewHolder) holder).mImageView.setRatio((float) height / width); + ((PostDetailVideoAndGifPreviewHolder) holder).mImageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } + ((PostDetailVideoAndGifPreviewHolder) holder).mImageView.setImageDrawable(resource); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostDetailVideoAndGifPreviewHolder) holder).mImageView.setImageDrawable(null); + } + }); } else { - imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); + imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + if (preview.getPreviewHeight() == 0 || preview.getPreviewWidth() == 0) { + ((PostDetailVideoAndGifPreviewHolder) holder).mImageView.setRatio((float) height / width); + ((PostDetailVideoAndGifPreviewHolder) holder).mImageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } + ((PostDetailVideoAndGifPreviewHolder) holder).mImageView.setImageDrawable(resource); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostDetailVideoAndGifPreviewHolder) holder).mImageView.setImageDrawable(null); + } + }); } } else if (holder instanceof PostDetailLinkViewHolder) { RequestBuilder imageRequestBuilder = mGlide.load(preview.getPreviewUrl()) @@ -899,9 +985,51 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + if (preview.getPreviewHeight() == 0 || preview.getPreviewWidth() == 0) { + ((PostDetailLinkViewHolder) holder).mImageView.setRatio((float) height / width); + ((PostDetailLinkViewHolder) holder).mImageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } else { + ((PostDetailLinkViewHolder) holder).mImageView.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); + ((PostDetailLinkViewHolder) holder).mImageView.getLayoutParams().height = height; + } + ((PostDetailLinkViewHolder) holder).mImageView.setImageDrawable(resource); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostDetailLinkViewHolder) holder).mImageView.setImageDrawable(null); + } + }); } else { - imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(((PostDetailLinkViewHolder) holder).mImageView); + imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + ((PostDetailLinkViewHolder) holder).mImageView.setImageDrawable(resource); + if (preview.getPreviewHeight() == 0 || preview.getPreviewWidth() == 0) { + ((PostDetailLinkViewHolder) holder).mImageView.setRatio((float) height / width); + ((PostDetailLinkViewHolder) holder).mImageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } else { + ((PostDetailLinkViewHolder) holder).mImageView.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); + } + + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostDetailLinkViewHolder) holder).mImageView.setImageDrawable(null); + } + }); } } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostRecyclerViewAdapter.java index a15ce23d..aff64f57 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/PostRecyclerViewAdapter.java @@ -16,6 +16,7 @@ import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; +import android.view.ViewTreeObserver; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; @@ -41,10 +42,13 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.RequestManager; import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; +import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.target.Target; +import com.bumptech.glide.request.transition.Transition; import com.google.android.exoplayer2.Tracks; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; import com.google.android.exoplayer2.ui.DefaultTimeBar; @@ -439,7 +443,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + ((PostVideoAutoplayViewHolder) holder).previewImageView.setImageDrawable(resource); + if (preview.getPreviewHeight() <= 0 || preview.getPreviewWidth() <= 0) { + ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) height / width); + ((PostVideoAutoplayViewHolder) holder).previewImageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } else { + ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); + ((PostVideoAutoplayViewHolder) holder).previewImageView.getLayoutParams().height = preview.getPreviewHeight(); + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); } else { ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1); } @@ -752,7 +776,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + ((PostVideoAutoplayViewHolder) holder).previewImageView.setImageDrawable(resource); + if (preview.getPreviewHeight() <= 0 || preview.getPreviewWidth() <= 0) { + ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) height / width); + ((PostVideoAutoplayViewHolder) holder).previewImageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } else { + ((PostVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); + ((PostVideoAutoplayViewHolder) holder).previewImageView.getLayoutParams().height = height; + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); } else { ((PostCard2VideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1); } @@ -935,7 +974,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter imageRequestBuilder = mGlide.load(url).listener(((PostWithPreviewTypeViewHolder) holder).glideRequestListener); if (blurImage) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(((PostWithPreviewTypeViewHolder) holder).imageView); + .into(new CustomTarget() { + @Override + public void onLoadStarted(@Nullable Drawable placeholder) { + mGlide.clear(((PostWithPreviewTypeViewHolder) holder).imageView); + ((PostWithPreviewTypeViewHolder) holder).imageView.setVisibility(View.GONE); + ((PostWithPreviewTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + } + + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + ((PostWithPreviewTypeViewHolder) holder).imageView.setImageDrawable(resource); + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + if (!mFixedHeightPreviewInCard) { + if (preview.getPreviewHeight() <= 0 || preview.getPreviewWidth() <= 0) { + ((PostWithPreviewTypeViewHolder) holder).imageView.setRatio((float) height / width); + ((PostWithPreviewTypeViewHolder) holder).imageView.getLayoutParams().height = height; + ((PostWithPreviewTypeViewHolder) holder).imageWrapperRelativeLayout.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } else { + ((PostWithPreviewTypeViewHolder) holder).imageView.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); + ((PostWithPreviewTypeViewHolder) holder).imageView.getLayoutParams().height = preview.getPreviewHeight(); + ((PostWithPreviewTypeViewHolder) holder).imageWrapperRelativeLayout.getLayoutParams().height = preview.getPreviewHeight(); + } + } else { + ((PostWithPreviewTypeViewHolder) holder).imageView.setRatio(-1); + ((PostWithPreviewTypeViewHolder) holder).imageView.getLayoutParams().height = height; + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostWithPreviewTypeViewHolder) holder).imageView.setImageDrawable(null); + } + }); } else { - imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(((PostWithPreviewTypeViewHolder) holder).imageView); + mGlide.load(post.getPreviews().get(0).getPreviewUrl()).diskCacheStrategy(DiskCacheStrategy.NONE).into(new CustomTarget() { + @Override + public void onLoadStarted(@Nullable Drawable placeholder) { + ((PostWithPreviewTypeViewHolder) holder).imageView.setVisibility(View.GONE); + ((PostWithPreviewTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + } + + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + ((PostWithPreviewTypeViewHolder) holder).imageView.setImageDrawable(resource); + ((PostWithPreviewTypeViewHolder) holder).imageView.setAdjustViewBounds(true); + + ((PostWithPreviewTypeViewHolder) holder).imageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { + @Override + public boolean onPreDraw() { + ((PostWithPreviewTypeViewHolder) holder).imageView.getViewTreeObserver().removeOnPreDrawListener(this); + + // Calculate the aspect ratio and set the height + float aspectRatio = (float) height / width; + int imageViewWidth = ((PostWithPreviewTypeViewHolder) holder).imageView.getWidth(); + int imageViewHeight = Math.round(imageViewWidth * aspectRatio); + ((PostWithPreviewTypeViewHolder) holder).imageView.getLayoutParams().height = ((PostWithPreviewTypeViewHolder) holder).imageView.getHeight(); + ((PostWithPreviewTypeViewHolder) holder).imageView.requestLayout(); + ((PostWithPreviewTypeViewHolder) holder).imageView.setVisibility(View.VISIBLE); + ((PostWithPreviewTypeViewHolder) holder).progressBar.setVisibility(View.GONE); + return true; + } + }); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + } + }); } } } else if (holder instanceof PostCompactBaseViewHolder) { @@ -1645,9 +1737,53 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + ((PostGalleryViewHolder) holder).imageView.setImageDrawable(resource); + if (preview.getPreviewHeight() <= 0 || preview.getPreviewWidth() <= 0) { + ((PostGalleryViewHolder) holder).imageView.setRatio((float) height / width); + ((PostGalleryViewHolder) holder).imageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } else { + ((PostGalleryViewHolder) holder).imageView.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostGalleryViewHolder) holder).imageView.setImageDrawable(null); + } + }); } else { - imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(((PostGalleryViewHolder) holder).imageView); + imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + ((PostGalleryViewHolder) holder).imageView.setImageDrawable(resource); + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + if (!mFixedHeightPreviewInCard) { + if (preview.getPreviewHeight() <= 0 || preview.getPreviewWidth() <= 0) { + ((PostGalleryViewHolder) holder).imageView.setRatio((float) height / width); + ((PostGalleryViewHolder) holder).imageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } else { + ((PostGalleryViewHolder) holder).imageView.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); + } + } else { + ((PostGalleryViewHolder) holder).imageView.setRatio(-1); + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostGalleryViewHolder) holder).imageView.setImageDrawable(null); + } + }); } } } else if (holder instanceof PostCard2WithPreviewViewHolder) { @@ -1665,9 +1801,57 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + ((PostCard2WithPreviewViewHolder) holder).imageView.setImageDrawable(resource); + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + if (!mFixedHeightPreviewInCard) { + if (preview.getPreviewHeight() <= 0 || preview.getPreviewWidth() <= 0) { + ((PostCard2WithPreviewViewHolder) holder).imageView.setRatio((float) height / width); + ((PostCard2WithPreviewViewHolder) holder).imageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } else { + ((PostCard2WithPreviewViewHolder) holder).imageView.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); + } + } else { + ((PostCard2WithPreviewViewHolder) holder).imageView.setRatio(-1); + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostCard2WithPreviewViewHolder) holder).imageView.setImageDrawable(null); + } + }); } else { - imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(((PostCard2WithPreviewViewHolder) holder).imageView); + imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + ((PostCard2WithPreviewViewHolder) holder).imageView.setImageDrawable(resource); + int width = resource.getIntrinsicWidth(); + int height = resource.getIntrinsicHeight(); + if (!mFixedHeightPreviewInCard) { + if (preview.getPreviewHeight() <= 0 || preview.getPreviewWidth() <= 0) { + ((PostCard2WithPreviewViewHolder) holder).imageView.setRatio((float) height / width); + ((PostCard2WithPreviewViewHolder) holder).imageView.getLayoutParams().height = height; + preview.setPreviewHeight(height); + preview.setPreviewWidth(width); + } else { + ((PostCard2WithPreviewViewHolder) holder).imageView.setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); + } + } else { + ((PostCard2WithPreviewViewHolder) holder).imageView.setRatio(-1); + } + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + ((PostCard2WithPreviewViewHolder) holder).imageView.setImageDrawable(null); + } + }); } } } @@ -3307,7 +3491,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter minTouchSlop || Math.abs(e.getRawY() - downY) > minTouchSlop) { + if (Math.abs(e.getRawX() - downX) > minTouchSlop || Math.abs(e.getRawY() - downY) > minTouchSlop) { dragged = true; } break; @@ -3510,21 +3694,21 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter minTouchSlop || Math.abs(e.getRawY() - downY) > minTouchSlop) { + if (Math.abs(e.getRawX() - downX) > minTouchSlop || Math.abs(e.getRawY() - downY) > minTouchSlop) { dragged = true; } if (!dragged) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java index 7088d095..e754b7ca 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/ParsePost.java @@ -5,7 +5,6 @@ import static java.lang.Integer.max; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Handler; -import android.os.StrictMode; import android.text.Html; import android.text.TextUtils; import android.util.Log; @@ -191,8 +190,8 @@ public class ParsePost { ArrayList previews = new ArrayList<>(); if(!post.isNull("thumbnail_url")) { String thumbnail = post.getString("thumbnail_url"); - int[] wh_array = getImageDimension(thumbnail); - previews.add(new Post.Preview(thumbnail, wh_array[0], wh_array[1], "", "")); + //int[] wh_array = getImageDimension(thumbnail); + previews.add(new Post.Preview(thumbnail, 0, 0, "", "")); } @@ -684,8 +683,7 @@ public class ParsePost { } public static int[] getImageDimension(String imageUrl) { - StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); - StrictMode.setThreadPolicy(policy); //Permit all for simplicity. You may want to revise this for your actual app. + HttpURLConnection urlConnection = null; try { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java index 185cc6b6..7c80def5 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/Post.java @@ -1,5 +1,6 @@ package eu.toldi.infinityforlemmy.post; +import android.graphics.Bitmap; import android.os.Parcel; import android.os.Parcelable; @@ -624,6 +625,8 @@ public class Post implements Parcelable { private String previewCaption; private String previewCaptionUrl; + private Bitmap previewBitmap; + public Preview(String previewUrl, int previewWidth, int previewHeight, String previewCaption, String previewCaptionUrl) { this.previewUrl = previewUrl; this.previewWidth = previewWidth; @@ -638,6 +641,7 @@ public class Post implements Parcelable { previewHeight = in.readInt(); previewCaption = in.readString(); previewCaptionUrl = in.readString(); + previewBitmap = in.readParcelable(Bitmap.class.getClassLoader()); } public static final Creator CREATOR = new Creator() { @@ -686,13 +690,23 @@ public class Post implements Parcelable { return previewCaptionUrl; } - public void setPreviewCaptionUrl(String previewCaptionUrl) { this.previewCaptionUrl = previewCaptionUrl; } + public void setPreviewCaptionUrl(String previewCaptionUrl) { + this.previewCaptionUrl = previewCaptionUrl; + } @Override public int describeContents() { return 0; } + public Bitmap getPreviewBitmap() { + return previewBitmap; + } + + public void setPreviewBitmap(Bitmap previewBitmap) { + this.previewBitmap = previewBitmap; + } + @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeString(previewUrl); @@ -700,6 +714,7 @@ public class Post implements Parcelable { parcel.writeInt(previewHeight); parcel.writeString(previewCaption); parcel.writeString(previewCaptionUrl); + parcel.writeParcelable(previewBitmap, i); } } }