From c52ac7d3449a2419f79d9572f92e97c654e675f2 Mon Sep 17 00:00:00 2001 From: samartnik Date: Wed, 8 Jul 2020 16:25:31 -0400 Subject: [PATCH] [Android] Fix for shields on custom tabs --- .../BraveRewardsDonationSentActivity.java | 4 +-- .../chrome/browser/BraveRewardsHelper.java | 30 +++++++++++-------- .../browser/BraveRewardsNativeWorker.java | 2 +- .../browser/BraveRewardsPanelPopup.java | 6 ++-- .../BraveRewardsSiteBannerActivity.java | 4 +-- .../browser/shields/BraveShieldsHandler.java | 16 +++++----- .../toolbar/top/BraveToolbarLayout.java | 3 +- 7 files changed, 35 insertions(+), 30 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/BraveRewardsDonationSentActivity.java b/android/java/org/chromium/chrome/browser/BraveRewardsDonationSentActivity.java index 7d4814a6f5bf..489584105d4f 100644 --- a/android/java/org/chromium/chrome/browser/BraveRewardsDonationSentActivity.java +++ b/android/java/org/chromium/chrome/browser/BraveRewardsDonationSentActivity.java @@ -50,10 +50,10 @@ protected void onCreate(Bundle savedInstanceState) { mBraveRewardsNativeWorker.AddObserver(this); String publisherFavIconURL = mBraveRewardsNativeWorker.GetPublisherFavIconURL(currentTabId_); - Tab currentActiveTab = BraveRewardsHelper.currentActiveTab(); + Tab currentActiveTab = BraveRewardsHelper.currentActiveChromeTabbedActivityTab(); String url = currentActiveTab.getUrlString(); String favicon_url = (publisherFavIconURL.isEmpty()) ? url : publisherFavIconURL; - mIconFetcher = new org.chromium.chrome.browser.BraveRewardsHelper(); + mIconFetcher = new org.chromium.chrome.browser.BraveRewardsHelper(currentActiveTab); mIconFetcher.retrieveLargeIcon(favicon_url, this); SetData(); SetAnimation(); diff --git a/android/java/org/chromium/chrome/browser/BraveRewardsHelper.java b/android/java/org/chromium/chrome/browser/BraveRewardsHelper.java index fb20ee373d1e..b11fe7026d06 100644 --- a/android/java/org/chromium/chrome/browser/BraveRewardsHelper.java +++ b/android/java/org/chromium/chrome/browser/BraveRewardsHelper.java @@ -55,15 +55,18 @@ public class BraveRewardsHelper implements LargeIconBridge.LargeIconCallback{ public static final int THANKYOU_FADE_IN_DURATION = 1500; //ms public static final int THANKYOU_STAY_DURATION = 2000; //ms private static final float DP_PER_INCH_MDPI = 160f; + private Tab mTab; public interface LargeIconReadyCallback { void onLargeIconReady(Bitmap icon); } - public BraveRewardsHelper () { - if (mLargeIconBridge == null) { - mLargeIconBridge = new LargeIconBridge(Profile.fromWebContents(((TabImpl)currentActiveTab()).getWebContents())); + public BraveRewardsHelper(Tab tab) { + mTab = tab; + assert mTab != null; + if (mLargeIconBridge == null && mTab != null) { + mLargeIconBridge = new LargeIconBridge(Profile.fromWebContents(mTab.getWebContents())); } } @@ -94,9 +97,8 @@ private void retrieveLargeIconInternal() { //favIconURL (or content URL) is still not available, try to read it again if (mFaviconUrl == null || mFaviconUrl.isEmpty() || mFaviconUrl.equals("clear")) { - Tab tab = currentActiveTab(); - if (tab != null) { - mFaviconUrl = tab.getUrlString(); + if (mTab != null) { + mFaviconUrl = mTab.getUrlString(); } mHandler.postDelayed(new Runnable() { @@ -115,6 +117,10 @@ public void run() { } } + public Tab getTab() { + return mTab; + } + @Override @CalledByNative("LargeIconCallback") public void onLargeIconAvailable(@Nullable Bitmap icon, int fallbackColor, @@ -241,12 +247,12 @@ static public String getCurrentYear(Resources resources) { return Integer.toString(currentTime.get(Calendar.YEAR)); } - public static Tab currentActiveTab() { - ChromeTabbedActivity activity = BraveRewardsHelper.getChromeTabbedActivity(); - if (activity == null || activity.getTabModelSelector() == null) { - return null; - } - return activity.getActivityTab(); + public static Tab currentActiveChromeTabbedActivityTab() { + ChromeTabbedActivity activity = BraveRewardsHelper.getChromeTabbedActivity(); + if (activity == null || activity.getTabModelSelector() == null) { + return null; + } + return activity.getActivityTab(); } /** diff --git a/android/java/org/chromium/chrome/browser/BraveRewardsNativeWorker.java b/android/java/org/chromium/chrome/browser/BraveRewardsNativeWorker.java index cf96b7e7cbdb..e0623c8fba44 100644 --- a/android/java/org/chromium/chrome/browser/BraveRewardsNativeWorker.java +++ b/android/java/org/chromium/chrome/browser/BraveRewardsNativeWorker.java @@ -152,7 +152,7 @@ private void TriggerOnNotifyFrontTabUrlChanged() { mHandler.post(new Runnable() { @Override public void run() { - Tab tab = BraveRewardsHelper.currentActiveTab(); + Tab tab = BraveRewardsHelper.currentActiveChromeTabbedActivityTab(); if (tab != null && !tab.isIncognito()) { OnNotifyFrontTabUrlChanged(tab.getId(), tab.getUrlString()); } diff --git a/android/java/org/chromium/chrome/browser/BraveRewardsPanelPopup.java b/android/java/org/chromium/chrome/browser/BraveRewardsPanelPopup.java index df9eba722572..c0074c9e6452 100644 --- a/android/java/org/chromium/chrome/browser/BraveRewardsPanelPopup.java +++ b/android/java/org/chromium/chrome/browser/BraveRewardsPanelPopup.java @@ -192,7 +192,7 @@ public BraveRewardsPanelPopup(View anchor) { this.window.setElevation(20); } thisObject = this; - mIconFetcher = new BraveRewardsHelper(); + mIconFetcher = new BraveRewardsHelper(BraveRewardsHelper.currentActiveChromeTabbedActivityTab()); this.window.setTouchInterceptor(new View.OnTouchListener() { @Override @@ -656,7 +656,7 @@ public void ShowWebSiteView(boolean returning_to_rewards) { ((LinearLayout)this.root.findViewById(R.id.website_summary)).setVisibility(View.VISIBLE); ((LinearLayout)this.root.findViewById(R.id.rewards_welcome_back)).setVisibility(View.GONE); ShowRewardsSummary(); - Tab currentActiveTab = BraveRewardsHelper.currentActiveTab(); + Tab currentActiveTab = BraveRewardsHelper.currentActiveChromeTabbedActivityTab(); if (currentActiveTab != null && !currentActiveTab.isIncognito()) { String url = currentActiveTab.getUrlString(); if (URLUtil.isValidUrl(url)) { @@ -1207,7 +1207,7 @@ public void OnPublisherInfo(int tabId) { } String publisherFavIconURL = mBraveRewardsNativeWorker.GetPublisherFavIconURL(currentTabId); - Tab currentActiveTab = BraveRewardsHelper.currentActiveTab(); + Tab currentActiveTab = BraveRewardsHelper.currentActiveChromeTabbedActivityTab(); String url = currentActiveTab.getUrlString(); final String favicon_url = (publisherFavIconURL.isEmpty()) ? url : publisherFavIconURL; diff --git a/android/java/org/chromium/chrome/browser/BraveRewardsSiteBannerActivity.java b/android/java/org/chromium/chrome/browser/BraveRewardsSiteBannerActivity.java index 80d69722c7ff..435eb9f84a96 100644 --- a/android/java/org/chromium/chrome/browser/BraveRewardsSiteBannerActivity.java +++ b/android/java/org/chromium/chrome/browser/BraveRewardsSiteBannerActivity.java @@ -128,10 +128,10 @@ public void onClick(View view) { publisher.setText(publisherName); String publisherFavIconURL = mBraveRewardsNativeWorker.GetPublisherFavIconURL(currentTabId_); - Tab currentActiveTab = BraveRewardsHelper.currentActiveTab(); + Tab currentActiveTab = BraveRewardsHelper.currentActiveChromeTabbedActivityTab(); String url = currentActiveTab.getUrlString(); String favicon_url = (publisherFavIconURL.isEmpty()) ? url : publisherFavIconURL; - mIconFetcher = new BraveRewardsHelper(); + mIconFetcher = new BraveRewardsHelper(currentActiveTab); mIconFetcher.retrieveLargeIcon(favicon_url, this); double balance = .0; diff --git a/android/java/org/chromium/chrome/browser/shields/BraveShieldsHandler.java b/android/java/org/chromium/chrome/browser/shields/BraveShieldsHandler.java index 4d7b175f388b..4fb86543b17b 100644 --- a/android/java/org/chromium/chrome/browser/shields/BraveShieldsHandler.java +++ b/android/java/org/chromium/chrome/browser/shields/BraveShieldsHandler.java @@ -183,16 +183,16 @@ public void addObserver(BraveShieldsMenuObserver menuObserver) { mMenuObserver = menuObserver; } - public void show(View anchorView, String host, String title, int tabId, - Profile profile) { + public void show(View anchorView, Tab tab) { if (mHardwareButtonMenuAnchor == null) return; - mHost = host; - mTitle = title; - mTabId = tabId; - mProfile = profile; + + mHost = tab.getUrlString(); + mTitle = tab.getUrl().getHost(); + mTabId = tab.getId(); + mProfile = Profile.fromWebContents(tab.getWebContents()); mBraveRewardsNativeWorker = BraveRewardsNativeWorker.getInstance(); - mIconFetcher = new BraveRewardsHelper(); + mIconFetcher = new BraveRewardsHelper(tab); int rotation = ((Activity)mContext).getWindowManager().getDefaultDisplay().getRotation(); // This fixes the bug where the bottom of the menu starts at the top of @@ -349,7 +349,7 @@ private void initViews() { private void setUpMainLayout() { String favIconURL = mBraveRewardsNativeWorker.GetPublisherFavIconURL(mTabId); - Tab currentActiveTab = BraveRewardsHelper.currentActiveTab(); + Tab currentActiveTab = mIconFetcher.getTab(); String url = currentActiveTab.getUrlString(); final String favicon_url = (favIconURL.isEmpty()) ? url : favIconURL; mIconFetcher.retrieveLargeIcon(favicon_url, this); diff --git a/android/java/org/chromium/chrome/browser/toolbar/top/BraveToolbarLayout.java b/android/java/org/chromium/chrome/browser/toolbar/top/BraveToolbarLayout.java index b92769281963..3ebd37e08e38 100644 --- a/android/java/org/chromium/chrome/browser/toolbar/top/BraveToolbarLayout.java +++ b/android/java/org/chromium/chrome/browser/toolbar/top/BraveToolbarLayout.java @@ -283,8 +283,7 @@ public void onClick(View v) { if (!isValidProtocolForShields(url.getProtocol())) { return; } - mBraveShieldsHandler.show(mBraveShieldsButton, currentTab.getUrlString(), - url.getHost(), currentTab.getId(), Profile.fromWebContents(((TabImpl)currentTab).getWebContents())); + mBraveShieldsHandler.show(mBraveShieldsButton, currentTab); } catch (Exception e) { // Do nothing if url is invalid. // Just return w/o showing shields popup.