Skip to content

Commit

Permalink
Merge pull request #3365 from brave/improve_shields_button_logic
Browse files Browse the repository at this point in the history
Improve shields button state update logic
  • Loading branch information
simonhong authored Sep 5, 2019
2 parents 76ed0c5 + 7cdb471 commit da51459
Showing 1 changed file with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
import org.chromium.chrome.browser.tabmodel.TabModelSelectorTabObserver;
import org.chromium.chrome.browser.tabmodel.TabSelectionType;
import org.chromium.chrome.browser.toolbar.top.ToolbarLayout;
import org.chromium.chrome.browser.util.AccessibilityUtil;
import org.chromium.chrome.browser.util.MathUtils;
Expand Down Expand Up @@ -63,11 +64,12 @@ void destroy() {

@Override
protected void onFinishInflate() {
super.onFinishInflate();
super.onFinishInflate();
mShieldsLayout = (FrameLayout) findViewById(R.id.brave_shields_button_layout);
mRewardsLayout = (FrameLayout) findViewById(R.id.brave_rewards_button_layout);
mBraveShieldsButton = (ImageView) findViewById(R.id.brave_shields_button);
if (mBraveShieldsButton != null) {
mBraveShieldsButton.setEnabled(false);
mBraveShieldsButton.setClickable(true);
mBraveShieldsButton.setOnClickListener(this);
mBraveShieldsButton.setOnLongClickListener(this);
Expand All @@ -86,9 +88,9 @@ public void onMenuTopShieldsChanged(boolean isOn, boolean isTopShield) {
}
if (isTopShield) {
if (isOn) {
setBraveShieldsColored();
enableBraveShieldsButton();
} else {
setBraveShieldsBlackAndWhite();
disableBraveShieldsButton();
}
}
if (currentTab.isLoading()) {
Expand All @@ -112,6 +114,10 @@ public void blockEvent(int tabId, String block_type, String subresource) {
mBraveShieldsMenuHandler.updateValues(tabId);
}
};
// Initially disabled. Shields button state will be updated when tab is
// shown and loading state is changed.
// Otherwise, button can be clicked when displayed tab is not ready.
disableBraveShieldsButton();
}

@Override
Expand All @@ -123,14 +129,25 @@ void onNativeLibraryReady() {
@Override
public void setTabModelSelector(TabModelSelector selector) {
mTabModelSelectorTabObserver = new TabModelSelectorTabObserver(selector) {
@Override
public void onShown(Tab tab, @TabSelectionType int type) {
try {
// Update shields button state when visible tab is changed.
URL url = new URL(tab.getUrl());
updateBraveShieldsButtonState(tab.getProfile(), url.toString());
} catch (Exception e) {
disableBraveShieldsButton();
}
}

@Override
public void onPageLoadStarted(Tab tab, String url) {
if (mMainActivity.getActivityTab() == tab) {
try {
URL urlCheck = new URL(url);
setBraveShieldsColor(tab.getProfile(), urlCheck.toString());
updateBraveShieldsButtonState(tab.getProfile(), urlCheck.toString());
} catch (Exception e) {
setBraveShieldsBlackAndWhite();
disableBraveShieldsButton();
}
}
mBraveShieldsMenuHandler.clearBraveShieldsCount(tab.getId());
Expand All @@ -142,9 +159,9 @@ public void onPageLoadFinished(final Tab tab, String url) {
try {
URL urlCheck = new URL(url);
mBraveShieldsMenuHandler.updateHost(urlCheck.toString());
setBraveShieldsColor(tab.getProfile(), urlCheck.toString());
updateBraveShieldsButtonState(tab.getProfile(), urlCheck.toString());
} catch (Exception e) {
setBraveShieldsBlackAndWhite();
disableBraveShieldsButton();
}
}
}
Expand Down Expand Up @@ -174,23 +191,25 @@ public void onClick(View v) {
}
try {
URL url = new URL(currentTab.getUrl());
setBraveShieldsColor(currentTab.getProfile(), url.toString());
updateBraveShieldsButtonState(currentTab.getProfile(), url.toString());
mBraveShieldsMenuHandler.show(mBraveShieldsButton, url.toString(),
url.getHost(), currentTab.getId(), currentTab.getProfile());
} catch (Exception e) {
setBraveShieldsBlackAndWhite();
disableBraveShieldsButton();
}
}
}

private void setBraveShieldsBlackAndWhite() {
private void disableBraveShieldsButton() {
if (null != mBraveShieldsButton) {
mBraveShieldsButton.setEnabled(false);
mBraveShieldsButton.setImageResource(R.drawable.btn_brave_off);
}
}

protected void setBraveShieldsColored() {
protected void enableBraveShieldsButton() {
if (null != mBraveShieldsButton) {
mBraveShieldsButton.setEnabled(true);
mBraveShieldsButton.setImageResource(R.drawable.btn_brave);
}
}
Expand All @@ -210,8 +229,8 @@ public boolean onLongClick(View v) {
return AccessibilityUtil.showAccessibilityToast(context, v, description);
}

public void populateUrlAnimatorSet(boolean hasFocus, int urlFocusToolbarButtonsDuration,
int urlClearFocusTabStackDelayMs, int urlFocusToolbarButtonsTranslationXDP,
public void populateUrlAnimatorSet(boolean hasFocus, int urlFocusToolbarButtonsDuration,
int urlClearFocusTabStackDelayMs, int urlFocusToolbarButtonsTranslationXDP,
List<Animator> animators) {
if (mBraveShieldsButton != null) {
Animator animator;
Expand Down Expand Up @@ -266,12 +285,12 @@ protected int getBoundsAfterAccountingForRightButtons(
params.getMarginEnd();
}

private void setBraveShieldsColor(Profile profile, String url) {
private void updateBraveShieldsButtonState(Profile profile, String url) {
if (BraveShieldsContentSettings.getShields(profile, url, BraveShieldsContentSettings.RESOURCE_IDENTIFIER_BRAVE_SHIELDS)) {
// Set Brave Shields button in color if we have a valid URL
setBraveShieldsColored();
enableBraveShieldsButton();
} else {
setBraveShieldsBlackAndWhite();
disableBraveShieldsButton();
}
}
}

0 comments on commit da51459

Please sign in to comment.