From cd108fbe554b81dc4fb1279ef5e0660ca9836d8e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 13 Jul 2020 22:57:30 +0800 Subject: Probably fix app crashes when loading large images in PostRecyclerViewAdapter. --- .../Activity/ViewImageOrGifActivity.java | 6 - .../Adapter/CommentAndPostRecyclerViewAdapter.java | 3 +- .../Adapter/PostRecyclerViewAdapter.java | 303 ++++++------------- .../infinityforreddit/Fragment/PostFragment.java | 16 +- .../main/res/layout/activity_view_image_or_gif.xml | 1 - app/src/main/res/layout/item_post_detail_image.xml | 2 - app/src/main/res/layout/item_post_gif_autoplay.xml | 325 -------------------- app/src/main/res/layout/item_post_image.xml | 329 --------------------- .../layout/item_post_image_and_gif_autoplay.xml | 326 ++++++++++++++++++++ 9 files changed, 427 insertions(+), 884 deletions(-) delete mode 100644 app/src/main/res/layout/item_post_gif_autoplay.xml delete mode 100644 app/src/main/res/layout/item_post_image.xml create mode 100644 app/src/main/res/layout/item_post_image_and_gif_autoplay.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java index 8c7da456..29c123bc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageOrGifActivity.java @@ -81,12 +81,6 @@ public class ViewImageOrGifActivity extends AppCompatActivity implements SetAsWa ProgressBar mProgressBar; @BindView(R.id.image_view_view_image_or_gif_activity) BigImageView mImageView; - /*@BindView(R.id.image_view_view_image_or_gif_activity) - SubsamplingScaleImageView mImageView; - @BindView(R.id.gesture_layout_view_image_or_gif_activity) - GestureFrameLayout gestureLayout; - @BindView(R.id.gif_view_view_image_or_gif_activity) - GifImageView mGifView;*/ @BindView(R.id.load_image_error_linear_layout_view_image_or_gif_activity) LinearLayout mLoadErrorLinearLayout; @Inject diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index ce1c32fa..5fc3a511 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -1268,7 +1268,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter customTarget = new CustomTarget() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - ((PostDetailImageViewHolder) holder).mImageView.setImage(ImageSource.bitmap(resource)); + ((PostDetailImageViewHolder) holder).mImageView.setImage(ImageSource.cachedBitmap(resource)); } @Override @@ -1907,6 +1907,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter implements CacheManager { private static final int VIEW_TYPE_POST_CARD_VIDEO_AUTOPLAY_TYPE = 1; private static final int VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE = 2; - private static final int VIEW_TYPE_POST_CARD_GIF_AUTOPLAY_TYPE = 3; - private static final int VIEW_TYPE_POST_CARD_IMAGE_TYPE = 4; + private static final int VIEW_TYPE_POST_CARD_IMAGE_AND_GIF_AUTOPLAY_TYPE = 4; private static final int VIEW_TYPE_POST_CARD_LINK_TYPE = 5; private static final int VIEW_TYPE_POST_CARD_NO_PREVIEW_LINK_TYPE = 6; private static final int VIEW_TYPE_POST_CARD_TEXT_TYPE = 7; @@ -128,6 +121,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter() { + if (holder instanceof PostImageAndGifAutoplayViewHolder) { + String url = post.getPostType() == Post.IMAGE_TYPE ? post.getPreviewUrl() : post.getUrl(); + RequestBuilder imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostGifAutoplayViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); loadImage(holder, post); }); return false; @@ -868,49 +861,21 @@ public class PostRecyclerViewAdapter extends PagedListAdapter target, DataSource dataSource, boolean isFirstResource) { - ((PostGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }).into(((PostGifAutoplayViewHolder) holder).imageView); - } else if (holder instanceof PostImageViewHolder) { - RequestBuilder imageRequestBuilder = mGlide.asBitmap().load(post.getPreviewUrl()).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostImageViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostImageViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostImageViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostImageViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostImageViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - loadImage(holder, post); - }); - return false; - } - - @Override - public boolean onResourceReady(Bitmap resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((PostImageViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostImageViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); return false; } }); - CustomTarget customTarget = new CustomTarget() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) { - ((PostImageViewHolder) holder).imageView.setImage(ImageSource.cachedBitmap(resource)); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }; - if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { - imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(customTarget); + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) + .into(((PostImageAndGifAutoplayViewHolder) holder).imageView); } else { - imageRequestBuilder.apply(RequestOptions.noTransformation()).into(customTarget); + if (mImageViewWidth > post.getPreviewWidth()) { + imageRequestBuilder.override(Target.SIZE_ORIGINAL).into(((PostImageAndGifAutoplayViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((PostImageAndGifAutoplayViewHolder) holder).imageView); + } } } else if (holder instanceof PostVideoAndGifPreviewViewHolder) { RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { @@ -1119,15 +1084,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - Post post = getItem(getAdapterPosition()); - if (post != null) { - Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() - + "-" + post.getId() + ".gif"); - intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, post.getVideoUrl()); - intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); - mActivity.startActivity(intent); - } - }); - } - } - - class PostImageViewHolder extends PostBaseViewHolder { - @BindView(R.id.card_view_item_post_image) - MaterialCardView cardView; - @BindView(R.id.icon_gif_image_view_item_post_image) - AspectRatioGifImageView iconGifImageView; - @BindView(R.id.subreddit_name_text_view_item_post_image) - TextView subredditTextView; - @BindView(R.id.user_text_view_item_post_image) - TextView userTextView; - @BindView(R.id.stickied_post_image_view_item_post_image) - ImageView stickiedPostImageView; - @BindView(R.id.post_time_text_view_item_post_image) - TextView postTimeTextView; - @BindView(R.id.title_text_view_item_post_image) - TextView titleTextView; - @BindView(R.id.type_text_view_item_post_image) - CustomTextView typeTextView; - @BindView(R.id.archived_image_view_item_post_image) - ImageView archivedImageView; - @BindView(R.id.locked_image_view_item_post_image) - ImageView lockedImageView; - @BindView(R.id.crosspost_image_view_item_post_image) - ImageView crosspostImageView; - @BindView(R.id.nsfw_text_view_item_post_image) - CustomTextView nsfwTextView; - @BindView(R.id.spoiler_custom_text_view_item_post_image) - CustomTextView spoilerTextView; - @BindView(R.id.flair_custom_text_view_item_post_image) - CustomTextView flairTextView; - @BindView(R.id.awards_text_view_item_post_image) - CustomTextView awardsTextView; - @BindView(R.id.progress_bar_item_post_image) - ProgressBar progressBar; - @BindView(R.id.image_view_item_post_image) - AspectRatioSubsamplingScaleImageView imageView; - @BindView(R.id.load_image_error_relative_layout_item_post_image) + @BindView(R.id.load_image_error_relative_layout_item_post_image_and_gif_autoplay) RelativeLayout errorRelativeLayout; - @BindView(R.id.load_image_error_text_view_item_post_image) + @BindView(R.id.load_image_error_text_view_item_post_image_and_gif_autoplay) TextView errorTextView; - @BindView(R.id.bottom_constraint_layout_item_post_image) + @BindView(R.id.bottom_constraint_layout_item_post_image_and_gif_autoplay) ConstraintLayout bottomConstraintLayout; - @BindView(R.id.plus_button_item_post_image) + @BindView(R.id.plus_button_item_post_image_and_gif_autoplay) ImageView upvoteButton; - @BindView(R.id.score_text_view_item_post_image) + @BindView(R.id.score_text_view_item_post_image_and_gif_autoplay) TextView scoreTextView; - @BindView(R.id.minus_button_item_post_image) + @BindView(R.id.minus_button_item_post_image_and_gif_autoplay) ImageView downvoteButton; - @BindView(R.id.comments_count_item_post_image) + @BindView(R.id.comments_count_item_post_image_and_gif_autoplay) TextView commentsCountTextView; - @BindView(R.id.save_button_item_post_image) + @BindView(R.id.save_button_item_post_image_and_gif_autoplay) ImageView saveButton; - @BindView(R.id.share_button_item_post_image) + @BindView(R.id.share_button_item_post_image_and_gif_autoplay) ImageView shareButton; - PostImageViewHolder(View itemView) { + PostImageAndGifAutoplayViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); setBaseView(cardView, @@ -2138,12 +2002,21 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { Post post = getItem(getAdapterPosition()); if (post != null) { - Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, post.getUrl()); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() - + "-" + post.getId() + ".jpg"); - intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); - mActivity.startActivity(intent); + if (post.getPostType() == Post.IMAGE_TYPE) { + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, post.getUrl()); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() + + "-" + post.getId() + ".jpg"); + intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); + mActivity.startActivity(intent); + } else { + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() + + "-" + post.getId() + ".gif"); + intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, post.getVideoUrl()); + intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); + mActivity.startActivity(intent); + } } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 443a6cf0..67a5e8bd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -11,6 +11,7 @@ import android.os.Build; import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; +import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; @@ -242,6 +243,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + DisplayMetrics displayMetrics = new DisplayMetrics(); + activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); + int windowWidth = displayMetrics.widthPixels; + if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT && !resources.getBoolean(R.bool.isTablet)) { mLinearLayoutManager = new LinearLayoutManager(activity); mPostRecyclerView.setLayoutManager(mLinearLayoutManager); @@ -251,6 +256,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { StaggeredGridLayoutManagerItemOffsetDecoration itemDecoration = new StaggeredGridLayoutManagerItemOffsetDecoration(activity, R.dimen.staggeredLayoutManagerItemOffset); mPostRecyclerView.addItemDecoration(itemDecoration); + windowWidth /= 2; } mGlide = Glide.with(activity); @@ -361,7 +367,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, accessToken, postType, postLayout, true, + customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -423,7 +429,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, accessToken, postType, postLayout, displaySubredditName, + customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, displaySubredditName, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -470,7 +476,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, accessToken, postType, postLayout, true, + customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -515,7 +521,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, accessToken, postType, postLayout, true, + customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -553,7 +559,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, accessToken, postType, postLayout, true, + customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { diff --git a/app/src/main/res/layout/activity_view_image_or_gif.xml b/app/src/main/res/layout/activity_view_image_or_gif.xml index 0156de76..35cdb0b6 100644 --- a/app/src/main/res/layout/activity_view_image_or_gif.xml +++ b/app/src/main/res/layout/activity_view_image_or_gif.xml @@ -27,7 +27,6 @@ android:id="@+id/image_view_view_image_or_gif_activity" android:layout_width="match_parent" android:layout_height="match_parent" - app:initScaleType="fitCenter" app:optimizeDisplay="true" app:tapToRetry="false" /> diff --git a/app/src/main/res/layout/item_post_detail_image.xml b/app/src/main/res/layout/item_post_detail_image.xml index 0c0ed5f5..be6dabfa 100644 --- a/app/src/main/res/layout/item_post_detail_image.xml +++ b/app/src/main/res/layout/item_post_detail_image.xml @@ -204,8 +204,6 @@ android:id="@+id/image_view_item_post_detail_image" android:layout_width="match_parent" android:layout_height="wrap_content" - android:adjustViewBounds="true" - android:scaleType="fitStart" app:quickScaleEnabled="false" app:zoomEnabled="false" app:panEnabled="false" /> diff --git a/app/src/main/res/layout/item_post_gif_autoplay.xml b/app/src/main/res/layout/item_post_gif_autoplay.xml deleted file mode 100644 index 504cb903..00000000 --- a/app/src/main/res/layout/item_post_gif_autoplay.xml +++ /dev/null @@ -1,325 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/item_post_image.xml b/app/src/main/res/layout/item_post_image.xml deleted file mode 100644 index 2a1f8513..00000000 --- a/app/src/main/res/layout/item_post_image.xml +++ /dev/null @@ -1,329 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/item_post_image_and_gif_autoplay.xml b/app/src/main/res/layout/item_post_image_and_gif_autoplay.xml new file mode 100644 index 00000000..561200b1 --- /dev/null +++ b/app/src/main/res/layout/item_post_image_and_gif_autoplay.xml @@ -0,0 +1,326 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3