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

Closes #3824: Adding the site tracking protection icon on the toolbar #3906

Merged
merged 2 commits into from
Aug 7, 2019
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 @@ -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 iconOnNoTrackersBlocked icon for when the site is on the state
* [Toolbar.SiteTrackingProtection.ON_NO_TRACKERS_BLOCKED]
* @param iconOnTrackersBlocked icon for when the site is on the state
* [Toolbar.SiteTrackingProtection.ON_TRACKERS_BLOCKED]
* @param iconDisabledForSite icon for when the site is on the state
* [Toolbar.SiteTrackingProtection.OFF_FOR_A_SITE]
*/
fun setTrackingProtectionIcons(
iconOnNoTrackersBlocked: Drawable = requireNotNull(
context.getDrawable(
TrackingProtectionIconView.DEFAULT_ICON_ON_NO_TRACKERS_BLOCKED
)
),
iconOnTrackersBlocked: Drawable = requireNotNull(
context.getDrawable(
TrackingProtectionIconView.DEFAULT_ICON_ON_TRACKERS_BLOCKED
)
),
iconDisabledForSite: Drawable = requireNotNull(
context.getDrawable(
TrackingProtectionIconView.DEFAULT_ICON_OFF_FOR_A_SITE
)
)
) {
displayToolbar.setTrackingProtectionIcons(
iconOnNoTrackersBlocked,
iconOnTrackersBlocked,
iconDisabledForSite
)
}

/**
* Sets the colour of the vertical separator between the tracking protection icon and the
* security indicator icon.
*/
@get:ColorInt
var separatorColor: Int
get() = displayToolbar.separatorColor
set(@ColorInt value) {
displayToolbar.separatorColor = value
}
Amejia481 marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is setting null used for this API to remove the listener? I'm not sure I follow how this would be used.

toolbar.setOnTrackingProtectionClickedListener { /* do magic */ }

// I no longer want to listen for TP clicked changes..
toolbar.setOnTrackingProtectionClickedListener(null)

Seems strange to want to allow being able to explicitly do that, and use null to allow that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can can open an issue for checking that!

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
)

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

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