From b57d381e94cbcc0ba6a9d233213b6a8e718efeb6 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 15 Jun 2020 22:05:33 +0800 Subject: Maybe fix java.lang.RuntimeException: Canvas: trying to draw too large bitmap. Fix many bugs. --- .../Activity/ViewImageActivity.java | 5 ++- .../Activity/ViewImgurMediaActivity.java | 22 ++++++++----- .../Activity/ViewSubredditDetailActivity.java | 36 ++++++++++++---------- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 18 +++++------ .../Adapter/PostRecyclerViewAdapter.java | 16 +++++----- .../infinityforreddit/CommentData.java | 2 +- .../Fragment/ViewImgurImageFragment.java | 2 +- .../Service/DownloadRedditVideoService.java | 9 +++--- .../infinityforreddit/Utils/GlideImageGetter.java | 6 ++-- 9 files changed, 65 insertions(+), 51 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java index 76acf253..62fa12a5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImageActivity.java @@ -89,7 +89,6 @@ public class ViewImageActivity extends AppCompatActivity implements SetAsWallpap private String mImageUrl; private String mImageFileName; private RequestManager glide; - private String postTitle; @Override protected void onCreate(Bundle savedInstanceState) { @@ -131,7 +130,7 @@ public class ViewImageActivity extends AppCompatActivity implements SetAsWallpap Intent intent = getIntent(); mImageUrl = intent.getStringExtra(IMAGE_URL_KEY); mImageFileName = intent.getStringExtra(FILE_NAME_KEY); - postTitle = intent.getStringExtra(POST_TITLE_KEY); + String postTitle = intent.getStringExtra(POST_TITLE_KEY); if (postTitle != null) { setTitle(Html.fromHtml(String.format("%s", postTitle))); @@ -185,7 +184,7 @@ public class ViewImageActivity extends AppCompatActivity implements SetAsWallpap mProgressBar.setVisibility(View.GONE); return false; } - }).into(mImageView); + }).override(Target.SIZE_ORIGINAL).into(mImageView); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java index c4b0e38d..f35d220f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java @@ -287,10 +287,12 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa } private void setToolbarTitle(int position) { - if (images.get(position).getType() == ImgurMedia.TYPE_VIDEO) { - setTitle(getString(R.string.view_imgur_media_activity_video_label, position + 1, images.size())); - } else { - setTitle(getString(R.string.view_imgur_media_activity_image_label, position + 1, images.size())); + if (images != null && position >= 0 && position < images.size()) { + if (images.get(position).getType() == ImgurMedia.TYPE_VIDEO) { + setTitle(getString(R.string.view_imgur_media_activity_video_label, position + 1, images.size())); + } else { + setTitle(getString(R.string.view_imgur_media_activity_image_label, position + 1, images.size())); + } } } @@ -382,17 +384,23 @@ public class ViewImgurMediaActivity extends AppCompatActivity implements SetAsWa @Override public void setToHomeScreen(int viewPagerPosition) { - setAsWallpaper(images.get(viewPagerPosition).getLink(), 0); + if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) { + setAsWallpaper(images.get(viewPagerPosition).getLink(), 0); + } } @Override public void setToLockScreen(int viewPagerPosition) { - setAsWallpaper(images.get(viewPagerPosition).getLink(), 1); + if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) { + setAsWallpaper(images.get(viewPagerPosition).getLink(), 1); + } } @Override public void setToBoth(int viewPagerPosition) { - setAsWallpaper(images.get(viewPagerPosition).getLink(), 2); + if (images != null && viewPagerPosition >= 0 && viewPagerPosition < images.size()) { + setAsWallpaper(images.get(viewPagerPosition).getLink(), 2); + } } public int getCurrentPagePosition() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 0ec10e46..17c80234 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -701,23 +701,25 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp fetchSubredditData(); return true; case R.id.action_lazy_mode_view_subreddit_detail_activity: - MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity); - if (isInLazyMode) { - isInLazyMode = false; - sectionsPagerAdapter.stopLazyMode(); - lazyModeItem.setTitle(R.string.action_start_lazy_mode); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | - AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED); - collapsingToolbarLayout.setLayoutParams(params); - } else { - isInLazyMode = true; - if (sectionsPagerAdapter.startLazyMode()) { - lazyModeItem.setTitle(R.string.action_stop_lazy_mode); - appBarLayout.setExpanded(false); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); + if (sectionsPagerAdapter != null) { + MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity); + if (isInLazyMode) { + isInLazyMode = false; + sectionsPagerAdapter.stopLazyMode(); + lazyModeItem.setTitle(R.string.action_start_lazy_mode); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | + AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED); collapsingToolbarLayout.setLayoutParams(params); } else { - isInLazyMode = false; + isInLazyMode = true; + if (sectionsPagerAdapter.startLazyMode()) { + lazyModeItem.setTitle(R.string.action_stop_lazy_mode); + appBarLayout.setExpanded(false); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); + collapsingToolbarLayout.setLayoutParams(params); + } else { + isInLazyMode = false; + } } } return true; @@ -777,7 +779,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp @Override public void sortTypeSelected(SortType sortType) { - sectionsPagerAdapter.changeSortType(sortType); + if (sectionsPagerAdapter != null) { + sectionsPagerAdapter.changeSortType(sortType); + } } @Override 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 c4d65766..c7984bfb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -1117,7 +1117,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter imageRequestBuilder = mGlide.load(url) .listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -1140,12 +1140,12 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) .listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -1168,12 +1168,12 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) .listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -1196,9 +1196,9 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { @@ -1173,9 +1173,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { @@ -1201,9 +1201,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 1 && children.get(children.size() - 1).isPlaceHolder) { children.addAll(children.size() - 2, moreChildren); } else { children.addAll(moreChildren); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java index 0319c904..99ebd248 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ViewImgurImageFragment.java @@ -100,7 +100,7 @@ public class ViewImgurImageFragment extends Fragment { progressBar.setVisibility(View.GONE); return false; } - }).into(imageView); + }).override(Target.SIZE_ORIGINAL).into(imageView); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java index 3da24433..b958eba2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Service/DownloadRedditVideoService.java @@ -397,7 +397,7 @@ public class DownloadRedditVideoService extends Service { int audioTrack = muxer.addTrack(audioFormat); boolean sawEOS = false; int offset = 100; - int sampleSize = 256 * 1024; + int sampleSize = 2048 * 1024; ByteBuffer videoBuf = ByteBuffer.allocate(sampleSize); ByteBuffer audioBuf = ByteBuffer.allocate(sampleSize); MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo(); @@ -412,7 +412,6 @@ public class DownloadRedditVideoService extends Service { videoBufferInfo.offset = offset; videoBufferInfo.size = videoExtractor.readSampleData(videoBuf, offset); - if (videoBufferInfo.size < 0 || audioBufferInfo.size < 0) { sawEOS = true; videoBufferInfo.size = 0; @@ -441,8 +440,10 @@ public class DownloadRedditVideoService extends Service { } } - muxer.stop(); - muxer.release(); + try { + muxer.stop(); + muxer.release(); + } catch (IllegalStateException ignore) {} } catch (IOException e) { e.printStackTrace(); return false; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/GlideImageGetter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/GlideImageGetter.java index 04c16284..fc8cfcf9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/GlideImageGetter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/GlideImageGetter.java @@ -56,7 +56,7 @@ public class GlideImageGetter implements Html.ImageGetter { BitmapDrawablePlaceholder drawable = new BitmapDrawablePlaceholder(textSize); Context context = container.get().getContext(); - if (!(context instanceof Activity && ((Activity) context).isFinishing())) { + if (!(context instanceof Activity && ((Activity) context).isDestroyed())) { container.get().post(() -> Glide.with(context) .asBitmap() .load(source) @@ -118,7 +118,9 @@ public class GlideImageGetter implements Html.ImageGetter { @Override public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition transition) { - setDrawable(new BitmapDrawable(container.get().getResources(), bitmap)); + if (container != null) { + setDrawable(new BitmapDrawable(container.get().getResources(), bitmap)); + } } @Override -- cgit v1.2.3