Skip to content

Commit

Permalink
Closes mozilla-mobile#9378: Add dot notification to the toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejia481 committed Jan 13, 2021
1 parent 1523c42 commit 5da2b8c
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import mozilla.components.browser.toolbar.edit.EditToolbar
import mozilla.components.concept.toolbar.AutocompleteDelegate
import mozilla.components.concept.toolbar.AutocompleteResult
import mozilla.components.concept.toolbar.Toolbar
import mozilla.components.concept.toolbar.Toolbar.PermissionHighlights
import mozilla.components.concept.toolbar.Toolbar.DotNotification
import mozilla.components.support.base.android.Padding
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.ui.autocomplete.AutocompleteView
Expand Down Expand Up @@ -112,10 +112,10 @@ class BrowserToolbar @JvmOverloads constructor(
get() = display.siteSecurity
set(value) { display.siteSecurity = value }

override var permissionHighlights: PermissionHighlights = PermissionHighlights.NONE
override var dotNotification: DotNotification = DotNotification.NONE
set(value) {
if (field != value) {
display.setPermissionIndicator(value)
display.setDotNotification(value)
field = value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DisplayToolbar internal constructor(
SECURITY,
TRACKING_PROTECTION,
EMPTY,
PERMISSION_HIGHLIGHTS
DOT_NOTIFICATION
}

/**
Expand All @@ -93,7 +93,7 @@ class DisplayToolbar internal constructor(
* @property text Text color of the URL.
* @property trackingProtection Color tint for the tracking protection icons.
* @property separator Color tint for the separator shown between indicators.
* @property permissionHighlights Color tint for the permission indicator.
* @property dotNotification Color tint for the dot notification.
*
* Set/Get the site security icon colours. It uses a pair of color integers which represent the
* insecure and secure colours respectively.
Expand All @@ -108,7 +108,7 @@ class DisplayToolbar internal constructor(
@ColorInt val text: Int,
@ColorInt val trackingProtection: Int?,
@ColorInt val separator: Int,
@ColorInt val permissionHighlights: Int?
@ColorInt val dotNotification: Int?
)

/**
Expand All @@ -129,15 +129,8 @@ class DisplayToolbar internal constructor(
val trackingProtectionTrackersBlocked: Drawable,
val trackingProtectionNothingBlocked: Drawable,
val trackingProtectionException: Drawable,
val permissionHighlights: PermissionHighlights
) {
/**
* Icons for site permission indicators.
*/
data class PermissionHighlights(
val autoPlayBlocked: Drawable
)
}
val dotNotification: Drawable
)

/**
* Gravity enum for positioning the progress bar.
Expand Down Expand Up @@ -176,7 +169,7 @@ class DisplayToolbar internal constructor(
}
}
},
permissionIndicator = rootView.findViewById(R.id.mozac_browser_toolbar_permission_indicator)
dotNotification = rootView.findViewById(R.id.mozac_browser_toolbar_permission_indicator)
)

/**
Expand All @@ -192,7 +185,7 @@ class DisplayToolbar internal constructor(
text = views.origin.textColor,
trackingProtection = null,
separator = ContextCompat.getColor(context, R.color.photonGrey80),
permissionHighlights = null
dotNotification = null
)
set(value) {
field = value
Expand All @@ -210,8 +203,8 @@ class DisplayToolbar internal constructor(
views.trackingProtectionIndicator.setColorFilter(value.trackingProtection)
}

if (value.permissionHighlights != null) {
views.permissionIndicator.setTint(value.permissionHighlights)
if (value.dotNotification != null) {
views.dotNotification.setTint(value.dotNotification)
}
}

Expand All @@ -229,9 +222,8 @@ class DisplayToolbar internal constructor(
trackingProtectionException = requireNotNull(
getDrawable(context, TrackingProtectionIconView.DEFAULT_ICON_OFF_FOR_A_SITE)
),
permissionHighlights = Icons.PermissionHighlights(
autoPlayBlocked =
requireNotNull(getDrawable(context, R.drawable.mozac_ic_autoplay_blocked))
dotNotification = requireNotNull(
getDrawable(context, R.drawable.mozac_dot_notification)
)
)
set(value) {
Expand All @@ -244,7 +236,7 @@ class DisplayToolbar internal constructor(
value.trackingProtectionTrackersBlocked,
value.trackingProtectionException
)
views.permissionIndicator.setIcons(value.permissionHighlights)
views.dotNotification.setIcon(value.dotNotification)
}

/**
Expand Down Expand Up @@ -298,29 +290,6 @@ class DisplayToolbar internal constructor(
}
}

/**
* Sets a listener to be invoked when the site permission indicator icon is clicked.
*/
fun setOnPermissionIndicatorClickedListener(listener: (() -> Unit)?) {
if (listener == null) {
views.permissionIndicator.setOnClickListener(null)
views.permissionIndicator.background = null
} else {
views.permissionIndicator.setOnClickListener {
listener.invoke()
}

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

views.permissionIndicator.setBackgroundResource(outValue.resourceId)
}
}

/**
* Sets a lambda to be invoked when the menu is dismissed
*/
Expand Down Expand Up @@ -470,8 +439,8 @@ class DisplayToolbar internal constructor(
View.GONE
}

views.permissionIndicator.visibility = if (!urlEmpty && indicators.contains(Indicators.PERMISSION_HIGHLIGHTS)) {
setPermissionIndicator(toolbar.permissionHighlights)
views.dotNotification.visibility = if (!urlEmpty && indicators.contains(Indicators.DOT_NOTIFICATION)) {
setDotNotification(toolbar.dotNotification)
} else {
View.GONE
}
Expand Down Expand Up @@ -549,14 +518,14 @@ class DisplayToolbar internal constructor(
updateSeparatorVisibility()
}

internal fun setPermissionIndicator(state: Toolbar.PermissionHighlights): Int {
if (!indicators.contains(Indicators.PERMISSION_HIGHLIGHTS)) {
return views.permissionIndicator.visibility
internal fun setDotNotification(state: Toolbar.DotNotification): Int {
if (!indicators.contains(Indicators.DOT_NOTIFICATION)) {
return views.dotNotification.visibility
}

views.permissionIndicator.permissionHighlights = state
views.dotNotification.state = state

return views.permissionIndicator.visibility
return views.dotNotification.visibility
}

internal fun onStop() {
Expand Down Expand Up @@ -685,5 +654,5 @@ internal class DisplayToolbarViews(
val trackingProtectionIndicator: TrackingProtectionIconView,
val origin: OriginView,
val progress: ProgressBar,
val permissionIndicator: PermissionHighlightsIconView
val dotNotification: DotNotificationView
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.view.isVisible
import mozilla.components.browser.toolbar.R
import mozilla.components.concept.toolbar.Toolbar.PermissionHighlights
import mozilla.components.concept.toolbar.Toolbar.PermissionHighlights.AUTOPLAY_BLOCKED
import mozilla.components.concept.toolbar.Toolbar.PermissionHighlights.NONE
import mozilla.components.concept.toolbar.Toolbar.DotNotification
import mozilla.components.concept.toolbar.Toolbar.DotNotification.AUTOPLAY_BLOCKED
import mozilla.components.concept.toolbar.Toolbar.DotNotification.NONE

/**
* Internal widget to display the different icons of site permission.
* Internal widget to display a dot notification.
*/
internal class PermissionHighlightsIconView @JvmOverloads constructor(
internal class DotNotificationView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
Expand All @@ -29,7 +29,7 @@ internal class PermissionHighlightsIconView @JvmOverloads constructor(
visibility = GONE
}

var permissionHighlights: PermissionHighlights = NONE
var state: DotNotification = NONE
set(value) {
if (value != field) {
field = value
Expand All @@ -38,26 +38,26 @@ internal class PermissionHighlightsIconView @JvmOverloads constructor(
}

@VisibleForTesting
internal var permissionTint: Int? = null
internal var dotTint: Int? = null

private var iconAutoplayBlocked: Drawable =
requireNotNull(AppCompatResources.getDrawable(context, DEFAULT_ICON_AUTOPLAY_BLOCKED))
private var notificationIcon: Drawable =
requireNotNull(AppCompatResources.getDrawable(context, DEFAULT_ICON))

fun setTint(tint: Int) {
permissionTint = tint
dotTint = tint
setColorFilter(tint)
}

fun setIcons(icons: DisplayToolbar.Icons.PermissionHighlights) {
this.iconAutoplayBlocked = icons.autoPlayBlocked
fun setIcon(icons: Drawable) {
this.notificationIcon = icons

updateIcon()
}

@Synchronized
@VisibleForTesting
internal fun updateIcon() {
val update = permissionHighlights.toUpdate()
val update = state.toUpdate()

isVisible = update.visible

Expand All @@ -67,18 +67,17 @@ internal class PermissionHighlightsIconView @JvmOverloads constructor(
null
}

permissionTint?.let { setColorFilter(it) }
dotTint?.let { setColorFilter(it) }
setImageDrawable(update.drawable)
}

companion object {
val DEFAULT_ICON_AUTOPLAY_BLOCKED =
R.drawable.mozac_ic_autoplay_blocked
val DEFAULT_ICON = R.drawable.mozac_dot_notification
}

private fun PermissionHighlights.toUpdate(): Update = when (this) {
private fun DotNotification.toUpdate(): Update = when (this) {
AUTOPLAY_BLOCKED -> Update(
iconAutoplayBlocked,
notificationIcon,
R.string.mozac_browser_toolbar_content_description_autoplay_blocked,
true)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="8dp"
android:height="8dp"
android:viewportWidth="10"
android:viewportHeight="10">
<path
android:pathData="M1,5a4,4 0 1,0 8,0a4,4 0 1,0 -8,0"
android:strokeWidth="1"
android:strokeAlpha=".2"
android:fillColor="#fff"
android:strokeColor="#000" />
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,17 @@
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/mozac_ic_site_security" />

<mozilla.components.browser.toolbar.display.PermissionHighlightsIconView
<mozilla.components.browser.toolbar.display.DotNotificationView
android:id="@+id/mozac_browser_toolbar_permission_indicator"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_width="10dp"
android:layout_height="10dp"
android:importantForAccessibility="no"
android:scaleType="center"
android:visibility="gone"
app:layout_constraintStart_toEndOf="@+id/mozac_browser_toolbar_security_indicator"
app:layout_constraintTop_toTopOf="parent"
android:paddingStart="0dp"
android:paddingEnd="8dp"
app:layout_goneMarginStart="8dp"
app:srcCompat="@drawable/mozac_ic_autoplay_blocked" />
app:layout_constraintBottom_toBottomOf="@+id/mozac_browser_toolbar_security_indicator"
app:layout_constraintEnd_toEndOf="@+id/mozac_browser_toolbar_security_indicator"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:tint="@color/photonBlue40"
app:srcCompat="@drawable/mozac_dot_notification" />

<!-- URL & Title -->

Expand All @@ -106,8 +103,9 @@
android:layout_height="40dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toStartOf="@+id/mozac_browser_toolbar_page_actions"
app:layout_constraintStart_toEndOf="@+id/mozac_browser_toolbar_permission_indicator"
app:layout_constraintStart_toEndOf="@+id/mozac_browser_toolbar_security_indicator"
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginStart="8dp"
app:layout_goneMarginTop="8dp" />

<!-- Page actions -->
Expand Down
Loading

0 comments on commit 5da2b8c

Please sign in to comment.