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

Fixes asserts on Brave News via accessing current Tab from multiple threads (uplift to 1.62.x) #21691

Merged
merged 1 commit into from
Jan 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,16 @@
import java.util.concurrent.Executors;

public class CardBuilderFeedCard {
private final int CARD_LAYOUT = 7;
private final int BUTTON_LAYOUT = 8;
private final int ROUNDED_TOP_LAYOUT = 9;

private final int TITLE = 10;
private final int DESC = 11;
private final int URL = 12;
private final int PUBLISHER = 13;
private final int TIME = 14;
private final int CATEGORY = 15;
private final int DEALS = 16;
private static final int CARD_LAYOUT = 7;
private static final int BUTTON_LAYOUT = 8;
private static final int ROUNDED_TOP_LAYOUT = 9;

private static final int TITLE = 10;
private static final int DESC = 11;
private static final int PUBLISHER = 13;
private static final int TIME = 14;
private static final int CATEGORY = 15;
private static final int DEALS = 16;

public static final int CARDTYPE_BRAVE_RATING = 100;

Expand All @@ -103,7 +102,6 @@ public class CardBuilderFeedCard {
private BraveNewsController mBraveNewsController;
private int mType;
private int mPosition;
private View view;
private int mHorizontalMargin;
private int mDeviceWidth;
private boolean mIsPromo;
Expand All @@ -113,8 +111,8 @@ public class CardBuilderFeedCard {
private DisplayAdsTable mPosTabAd;

private static final String TAG = "BN";
private final int MARGIN_VERTICAL = 10;
private final String BRAVE_OFFERS_URL = "offers.brave.com";
private static final int MARGIN_VERTICAL = 10;
private static final String BRAVE_OFFERS_URL = "offers.brave.com";

public CardBuilderFeedCard(BraveNewsController braveNewsController, RequestManager glide,
LinearLayout layout, Activity activity, int position, FeedItemsCard newsItem,
Expand Down Expand Up @@ -397,31 +395,35 @@ public LinearLayout createCard(int type, int position) {
break;
case CardType.DISPLAY_AD:
try {
Tab tab = BraveActivity.getBraveActivity().getActivityTab();
final int tabId = tab != null ? tab.getId() : -1;
ExecutorService executor = Executors.newSingleThreadExecutor();
Handler handler = new Handler(Looper.getMainLooper());
executor.execute(() -> {
try {
Tab tab = BraveActivity.getBraveActivity().getActivityTab();
if (tab != null) {
int tabId = tab.getId();
DatabaseHelper dbHelper = DatabaseHelper.getInstance();
DisplayAdsTable posTabAd =
dbHelper.getDisplayAd(position, tabId);
handler.post(() -> {
if (posTabAd != null) {
createAdFromTable(posTabAd);
} else {
mBraveNewsController.getDisplayAd(adData -> {
BraveNewsUtils.putToDisplayAdsMap(position, adData);
createdDisplayAdCard(adData);
});
}
});
}
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "createCard DISPLAY_AD " + e);
}
});
executor.execute(
() -> {
if (tabId != -1) {
DatabaseHelper dbHelper = DatabaseHelper.getInstance();
DisplayAdsTable posTabAd =
dbHelper.getDisplayAd(position, tabId);
handler.post(
() -> {
if (posTabAd != null) {
createAdFromTable(posTabAd);
} else {
mBraveNewsController.getDisplayAd(
adData -> {
BraveNewsUtils
.putToDisplayAdsMap(
position,
adData);
createdDisplayAdCard(adData);
});
}
});
}
});
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "createCard DISPLAY_AD " + e);
} catch (Exception e) {
Log.e(TAG, "displayad Exception" + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,42 +445,38 @@ public void onScrollStateChanged(
linearLayoutManager.findFirstVisibleItemPosition();

int newsFeedPosition = firstNewsFeedPosition();

int tabId = -1;
try {
Tab tab = BraveActivity.getBraveActivity().getActivityTab();
tabId = tab != null ? tab.getId() : -1;
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "onScrollStateChanged " + e);
}
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
try {
if (BraveActivity.getBraveActivity().getActivityTab() != null
&& mRecyclerView.getChildCount() > 0) {
View firstChild = mRecyclerView.getChildAt(0);
if (firstChild != null) {
int firstVisiblePosition =
mRecyclerView.getChildAdapterPosition(firstChild);
int verticalOffset = firstChild.getTop();

ContextUtils.getAppSharedPreferences()
.edit()
.putInt(
BravePreferenceKeys
.BRAVE_RECYCLERVIEW_OFFSET_POSITION
+ BraveActivity.getBraveActivity()
.getActivityTab()
.getId(),
verticalOffset)
.apply();

ContextUtils.getAppSharedPreferences()
.edit()
.putInt(
BravePreferenceKeys
.BRAVE_RECYCLERVIEW_POSITION
+ BraveActivity.getBraveActivity()
.getActivityTab()
.getId(),
firstVisiblePosition)
.apply();
}
if (tabId != -1 && mRecyclerView.getChildCount() > 0) {
View firstChild = mRecyclerView.getChildAt(0);
if (firstChild != null) {
int firstVisiblePosition =
mRecyclerView.getChildAdapterPosition(firstChild);
int verticalOffset = firstChild.getTop();

ContextUtils.getAppSharedPreferences()
.edit()
.putInt(
BravePreferenceKeys
.BRAVE_RECYCLERVIEW_OFFSET_POSITION
+ tabId,
verticalOffset)
.apply();

ContextUtils.getAppSharedPreferences()
.edit()
.putInt(
BravePreferenceKeys.BRAVE_RECYCLERVIEW_POSITION
+ tabId,
firstVisiblePosition)
.apply();
}
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "onScrollStateChanged " + e);
}
}
if (mIsDisplayNewsFeed
Expand Down Expand Up @@ -661,6 +657,7 @@ public void onScrollStateChanged(
// if viewed for more than 100 ms and is more
// than 50%
// visible send the event
final int tabIdForLambda = tabId;
Timer timer = new Timer();
timer.schedule(
new TimerTask() {
Expand All @@ -684,7 +681,8 @@ public void run() {
mUuid,
mCreativeInstanceId);

insertAd();
insertAd(
tabIdForLambda);
}
}
}.start();
Expand All @@ -701,16 +699,11 @@ public void run() {
}
}

private void insertAd() {
private void insertAd(int tabId) {
DisplayAd currentDisplayAd =
BraveNewsUtils.getFromDisplayAdsMap(mItemPosition);
try {
mDatabaseHelper.insertAd(
currentDisplayAd,
mItemPosition,
BraveActivity.getBraveActivity().getActivityTab().getId());
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "insertAd " + e);
if (tabId != -1) {
mDatabaseHelper.insertAd(currentDisplayAd, mItemPosition, tabId);
}
}

Expand Down