Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Closes #3824: Adding the site tracking protection icon on the toolbar.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejia481 committed Aug 5, 2019
1 parent a3e5bc2 commit 61188a6
Show file tree
Hide file tree
Showing 18 changed files with 804 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.view.View
import android.view.View.OnFocusChangeListener
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.VisibleForTesting
import androidx.core.view.forEach
Expand All @@ -30,6 +31,7 @@ import kotlinx.coroutines.launch
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.toolbar.display.DisplayToolbar
import mozilla.components.browser.toolbar.display.DisplayToolbar.Companion.BOTTOM_PROGRESS_BAR
import mozilla.components.browser.toolbar.display.TrackingProtectionIconView
import mozilla.components.browser.toolbar.edit.EditToolbar
import mozilla.components.concept.toolbar.AutocompleteDelegate
import mozilla.components.concept.toolbar.AutocompleteResult
Expand Down Expand Up @@ -210,6 +212,51 @@ class BrowserToolbar @JvmOverloads constructor(
editToolbar.urlView.setTextColor(value)
}

/**
* Sets the different icons that the tracking protection icon could has depending of its
* [Toolbar.siteTrackingProtection]
* @param iconTrackingProtectionOnNoTrackersBlocked icon for when the site is on the state
* [Toolbar.SiteTrackingProtection.ON_NO_TRACKERS_BLOCKED]
* @param iconTrackingProtectionOnTrackersBlocked icon for when the site is on the state
* [Toolbar.SiteTrackingProtection.ON_TRACKERS_BLOCKED]
* @param iconTrackingProtectionOffForASite icon for when the site is on the state
* [Toolbar.SiteTrackingProtection.OFF_FOR_A_SITE]
*/
fun setTrackingProtectionIcons(
iconTrackingProtectionOnNoTrackersBlocked: Drawable = requireNotNull(
context.getDrawable(
TrackingProtectionIconView.DEFAULT_ICON_ON_NO_TRACKERS_BLOCKED
)
),
iconTrackingProtectionOnTrackersBlocked: Drawable = requireNotNull(
context.getDrawable(
TrackingProtectionIconView.DEFAULT_ICON_ON_TRACKERS_BLOCKED
)
),
iconTrackingProtectionOffForASite: Drawable = requireNotNull(
context.getDrawable(
TrackingProtectionIconView.DEFAULT_ICON_OFF_FOR_A_SITE
)
)
) {
displayToolbar.setTrackingProtectionIcons(
iconTrackingProtectionOnNoTrackersBlocked,
iconTrackingProtectionOnTrackersBlocked,
iconTrackingProtectionOffForASite
)
}

/**
* Sets the colour of the vertical separator between the tracking protection icon and the
* security indicator icon.
*/
@get:ColorInt
var trackingProtectionAndSecurityIndicatorSeparatorColor: Int
get() = displayToolbar.trackingProtectionAndSecurityIndicatorSeparatorColor
set(@ColorInt value) {
displayToolbar.trackingProtectionAndSecurityIndicatorSeparatorColor = value
}

/**
* Sets the size of the text for the title displayed in the toolbar.
*/
Expand Down Expand Up @@ -285,6 +332,29 @@ class BrowserToolbar @JvmOverloads constructor(
}
}

/**
* Sets a listener to be invoked when the site tracking protection indicator icon is clicked.
*/
fun setOnTrackingProtectionClickedListener(listener: (() -> Unit)?) {
if (listener == null) {
displayToolbar.trackingProtectionIconView.setOnClickListener(null)
displayToolbar.trackingProtectionIconView.background = null
} else {
displayToolbar.trackingProtectionIconView.setOnClickListener {
listener.invoke()
}

val outValue = TypedValue()

context.theme.resolveAttribute(
android.R.attr.selectableItemBackgroundBorderless,
outValue,
true)

displayToolbar.trackingProtectionIconView.setBackgroundResource(outValue.resourceId)
}
}

override fun setOnEditListener(listener: Toolbar.OnEditListener) {
editToolbar.editListener = listener
}
Expand Down Expand Up @@ -336,6 +406,25 @@ class BrowserToolbar @JvmOverloads constructor(
field = value
}

override var siteTrackingProtection: Toolbar.SiteTrackingProtection =
Toolbar.SiteTrackingProtection.OFF_GLOBALLY
set(value) {
if (field != value) {
displayToolbar.setTrackingProtectionState(value)
field = value
}
}

/**
* Set/Get whether a tracking protection icon (usually a shield icon) should be visible.
*/
var displayTrackingProtectionIcon: Boolean = displayToolbar.displayTrackingProtectionIcon
get() = displayToolbar.displayTrackingProtectionIcon
set(value) {
displayToolbar.displayTrackingProtectionIcon = value
field = value
}

init {
context.obtainStyledAttributes(attrs, R.styleable.BrowserToolbar, defStyleAttr, 0).run {
attrs?.let {
Expand Down Expand Up @@ -363,6 +452,12 @@ class BrowserToolbar @JvmOverloads constructor(
R.styleable.BrowserToolbar_browserToolbarClearColor,
editToolbar.clearViewColor
)

trackingProtectionAndSecurityIndicatorSeparatorColor = getColor(
R.styleable.BrowserToolbar_browserToolbarTrackingProtectionAndSecurityIndicatorSeparatorColor,
displayToolbar.trackingProtectionAndSecurityIndicatorSeparatorColor
)

if (peekValue(R.styleable.BrowserToolbar_browserToolbarSuggestionForegroundColor) != null) {
suggestionForegroundColor = getColor(
R.styleable.BrowserToolbar_browserToolbarSuggestionForegroundColor,
Expand Down
Loading

0 comments on commit 61188a6

Please sign in to comment.