Skip to content

Commit

Permalink
Proper initialization of BraveShieldsHandler.mContext (uplift to 1.49…
Browse files Browse the repository at this point in the history
….x) (#17623)

Uplift of #17604 (squashed) to release
  • Loading branch information
AlexeyBarabash authored Mar 16, 2023
1 parent 7ad5213 commit 0fa26ac
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
23 changes: 15 additions & 8 deletions android/java/org/chromium/chrome/browser/app/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.chrome.browser.crypto_wallet.util.WalletUtils;
import org.chromium.chrome.browser.custom_layout.popup_window_tooltip.PopupWindowTooltip;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.dependency_injection.ChromeActivityComponent;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
Expand Down Expand Up @@ -1653,23 +1654,29 @@ public void showDormantUsersEngagementDialog(String notificationType) {
}
}

static public ChromeTabbedActivity getChromeTabbedActivity() {
private static Activity getActivityOfType(Class<?> classOfActivity) {
for (Activity ref : ApplicationStatus.getRunningActivities()) {
if (!(ref instanceof ChromeTabbedActivity)) continue;
if (!classOfActivity.isInstance(ref)) continue;

return (ChromeTabbedActivity)ref;
return ref;
}

return null;
}

static public BraveActivity getBraveActivity() {
for (Activity ref : ApplicationStatus.getRunningActivities()) {
if (!(ref instanceof BraveActivity)) continue;
static public ChromeTabbedActivity getChromeTabbedActivity() {
return (ChromeTabbedActivity) getActivityOfType(ChromeTabbedActivity.class);
}

return (BraveActivity)ref;
}
static public CustomTabActivity getCustomTabActivity() {
return (CustomTabActivity) getActivityOfType(CustomTabActivity.class);
}

static public BraveActivity getBraveActivity() {
BraveActivity activity = (BraveActivity) getActivityOfType(BraveActivity.class);
if (activity != null) {
return activity;
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public BlockersInfo() {
public ArrayList<String> mBlockerNames;
}

private final Context mContext;
private Context mContext;
private PopupWindow mPopupWindow;
private AnimatorSet mMenuItemEnterAnimator;
private BraveShieldsMenuObserver mMenuObserver;
Expand Down Expand Up @@ -282,8 +282,22 @@ public void addObserver(BraveShieldsMenuObserver menuObserver) {
mMenuObserver = menuObserver;
}

private void ensureInitializedForCustomTabs() {
if (mHardwareButtonMenuAnchor == null && mContext == null) {
mContext = BraveActivity.getCustomTabActivity();
mHardwareButtonMenuAnchor = ((Activity) mContext).findViewById(R.id.menu_anchor_stub);
}
}

public void show(View anchorView, Tab tab) {
if (mHardwareButtonMenuAnchor == null) return;
// Current class can be initialized by WarmupManager.inflateViewHierarchy
// prior to creation of proper activity. In such case activity is available later.
// Try to find it, and give up if somehow we won't be able.
ensureInitializedForCustomTabs();

if (mHardwareButtonMenuAnchor == null || mContext == null) {
return;
}

mHost = tab.getUrl().getSpec();
mTitle = tab.getUrl().getHost();
Expand All @@ -297,8 +311,9 @@ public void show(View anchorView, Tab tab) {
updateValues(mTabId);
}

public PopupWindow showPopupMenu(View anchorView) {
if (mContext == null) return null;
private PopupWindow showPopupMenu(View anchorView) {
assert (mContext != null);
assert (mHardwareButtonMenuAnchor != null);

int rotation = ((Activity)mContext).getWindowManager().getDefaultDisplay().getRotation();
// This fixes the bug where the bottom of the menu starts at the top of
Expand Down

0 comments on commit 0fa26ac

Please sign in to comment.