Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the code snippet which caused ANR to background thread #20833

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand All @@ -55,6 +60,9 @@ 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;
Expand Down Expand Up @@ -103,41 +111,42 @@ 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;
}

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) {
Expand Down
Loading