From 79399f419b6f7c2d965e24f94499323f0f6876b0 Mon Sep 17 00:00:00 2001 From: Jarvis Lin Date: Thu, 16 May 2024 11:17:42 +0300 Subject: [PATCH 1/2] Move the code snippet which caused ANR to background thread --- .../ui/reader/views/ReaderSiteHeaderView.java | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java index 3fd01fae0109..6943e15a488a 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java @@ -3,6 +3,8 @@ import android.content.Context; import android.icu.text.CompactDecimalFormat; import android.icu.text.NumberFormat; +import android.os.Handler; +import android.os.Looper; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; @@ -31,6 +33,9 @@ import org.wordpress.android.util.image.BlavatarShape; import org.wordpress.android.util.image.ImageManager; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + import javax.inject.Inject; /** @@ -55,6 +60,10 @@ public interface OnBlogInfoLoadedListener { private OnBlogInfoLoadedListener mBlogInfoListener; private OnFollowListener mFollowListener; + private final ExecutorService mExecutorService = Executors.newSingleThreadExecutor(); + private final Handler mMainHandler = new Handler(Looper.getMainLooper()); + + @Inject AccountStore mAccountStore; @Inject ImageManager mImageManager; @Inject ReaderTracker mReaderTracker; @@ -103,7 +112,6 @@ public void loadBlogInfo( mBlogId = blogId; mFeedId = feedId; - final ReaderBlog localBlogInfo; if (blogId == 0 && feedId == 0) { ToastUtils.showToast(getContext(), R.string.reader_toast_err_show_blog); return; @@ -111,33 +119,35 @@ public void loadBlogInfo( mIsFeed = ReaderUtils.isExternalFeed(mBlogId, mFeedId); - if (mIsFeed) { - localBlogInfo = ReaderBlogTable.getFeedInfo(mFeedId); - } else { - localBlogInfo = ReaderBlogTable.getBlogInfo(mBlogId); - } - - if (localBlogInfo != null) { - showBlogInfo(localBlogInfo, source); - } - - // then get from server if doesn't exist locally or is time to update it - if (localBlogInfo == null || ReaderBlogTable.isTimeToUpdateBlogInfo(localBlogInfo)) { - ReaderActions.UpdateBlogInfoListener listener = new ReaderActions.UpdateBlogInfoListener() { - @Override - public void onResult(ReaderBlog serverBlogInfo) { - if (isAttachedToWindow()) { - showBlogInfo(serverBlogInfo, source); - } - } - }; - + // run in background to avoid ANR + mExecutorService.execute(() -> { + final ReaderBlog localBlogInfo; if (mIsFeed) { - ReaderBlogActions.updateFeedInfo(mFeedId, null, listener); + localBlogInfo = ReaderBlogTable.getFeedInfo(mFeedId); } else { - ReaderBlogActions.updateBlogInfo(mBlogId, null, listener); + localBlogInfo = ReaderBlogTable.getBlogInfo(mBlogId); } - } + + mMainHandler.post(() -> { + if (localBlogInfo != null) { + showBlogInfo(localBlogInfo, source); + } + // then get from server if doesn't exist locally or is time to update it + if (localBlogInfo == null || ReaderBlogTable.isTimeToUpdateBlogInfo(localBlogInfo)) { + ReaderActions.UpdateBlogInfoListener listener = serverBlogInfo -> { + if (isAttachedToWindow()) { + showBlogInfo(serverBlogInfo, source); + } + }; + + if (mIsFeed) { + ReaderBlogActions.updateFeedInfo(mFeedId, null, listener); + } else { + ReaderBlogActions.updateBlogInfo(mBlogId, null, listener); + } + } + }); + }); } private void showBlogInfo(ReaderBlog blogInfo, String source) { From 03a7cbdbc7eff9127ddf7e0081440ac5df84b085 Mon Sep 17 00:00:00 2001 From: Jarvis Lin Date: Thu, 16 May 2024 11:56:40 +0300 Subject: [PATCH 2/2] Update the release note --- RELEASE-NOTES.txt | 1 + .../android/ui/reader/views/ReaderSiteHeaderView.java | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 7454a862db0e..4b04f4bb87e6 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -4,6 +4,7 @@ ----- * [*] Fixed a rare crash on Posts List screen [https://github.com/wordpress-mobile/WordPress-Android/pull/20813] * [*] Fixed a rare crash on the Login screen [https://github.com/wordpress-mobile/WordPress-Android/pull/20821] +* [*] Fixed an ANR issue on the Post List screen [https://github.com/wordpress-mobile/WordPress-Android/pull/20833] 24.9 ----- diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java index 6943e15a488a..7516e164beae 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java @@ -62,8 +62,7 @@ public interface OnBlogInfoLoadedListener { private final ExecutorService mExecutorService = Executors.newSingleThreadExecutor(); private final Handler mMainHandler = new Handler(Looper.getMainLooper()); - - + @Inject AccountStore mAccountStore; @Inject ImageManager mImageManager; @Inject ReaderTracker mReaderTracker;