Skip to content

Commit

Permalink
Issue mozilla-mobile#3647 - Custom HTTP(S) icons in toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Aug 2, 2019
1 parent ca3eff8 commit 44bf9a0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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.SiteSecurityIcons
import mozilla.components.browser.toolbar.edit.EditToolbar
import mozilla.components.concept.toolbar.AutocompleteDelegate
import mozilla.components.concept.toolbar.AutocompleteResult
Expand Down Expand Up @@ -111,12 +112,12 @@ class BrowserToolbar @JvmOverloads constructor(
}

/**
* Set/Get the site security icon colours (usually a lock or globe icon). It uses a pair of integers
* Set/Get the site security icons (usually a lock or globe icon). It uses a pair of drawables
* which represent the insecure and secure colours respectively.
*/
var siteSecurityColor: Pair<Int, Int>
get() = displayToolbar.securityIconColor
set(value) { displayToolbar.securityIconColor = value }
var siteSecurityIcons
get() = displayToolbar.securityIcons
set(value) { displayToolbar.securityIcons = value }

/**
* Gets/Sets a custom view that will be drawn as behind the URL and page actions in display mode.
Expand Down Expand Up @@ -373,15 +374,11 @@ class BrowserToolbar @JvmOverloads constructor(
R.styleable.BrowserToolbar_browserToolbarSuggestionBackgroundColor,
suggestionBackgroundColor
)
val inSecure = getColor(
R.styleable.BrowserToolbar_browserToolbarInsecureColor,
displayToolbar.securityIconColor.first
)
val secure = getColor(
R.styleable.BrowserToolbar_browserToolbarSecureColor,
displayToolbar.securityIconColor.second
)
siteSecurityColor = Pair(inSecure, secure)
val inSecure = getDrawable(R.styleable.BrowserToolbar_browserToolbarInsecureIcon)
?: displayToolbar.securityIcons.insecure
val secure = getDrawable(R.styleable.BrowserToolbar_browserToolbarSecureIcon)
?: displayToolbar.securityIcons.secure
siteSecurityIcons = SiteSecurityIcons(inSecure, secure)
val fadingEdgeLength = getDimensionPixelSize(
R.styleable.BrowserToolbar_browserToolbarFadingEdgeSize,
resources.getDimensionPixelSize(R.dimen.mozac_browser_toolbar_url_fading_edge_size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ package mozilla.components.browser.toolbar.display

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.BlendMode
import android.graphics.BlendModeColorFilter
import android.graphics.ColorFilter
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -162,7 +169,7 @@ internal class DisplayToolbar(

private var currentSiteSecurity = SiteSecurity.INSECURE

internal var securityIconColor = Pair(defaultColor, defaultColor)
internal var securityIcons = getDefaultSecurityIcons()
set(value) {
field = value
setSiteSecurity(currentSiteSecurity)
Expand Down Expand Up @@ -262,14 +269,11 @@ internal class DisplayToolbar(
* Sets the site's security icon as secure if true, else the regular globe.
*/
fun setSiteSecurity(secure: SiteSecurity) {
val (image, color) = when (secure) {
SiteSecurity.INSECURE -> Pair(mozac_ic_globe, securityIconColor.first)
SiteSecurity.SECURE -> Pair(mozac_ic_lock, securityIconColor.second)
}
siteSecurityIconView.apply {
setImageResource(image)
setColorFilter(color)
val drawable = when (secure) {
SiteSecurity.INSECURE -> securityIcons.insecure
SiteSecurity.SECURE -> securityIcons.secure
}
siteSecurityIconView.setImageDrawable(drawable)

currentSiteSecurity = secure
}
Expand Down Expand Up @@ -472,6 +476,23 @@ internal class DisplayToolbar(
}
}

private fun getDefaultSecurityIcons(): SiteSecurityIcons {
val defaultColorFilter: ColorFilter = if (SDK_INT >= Build.VERSION_CODES.Q) {
BlendModeColorFilter(defaultColor, BlendMode.SRC_IN)
} else {
PorterDuffColorFilter(defaultColor, PorterDuff.Mode.SRC_IN)
}

return SiteSecurityIcons(
insecure = context.getDrawable(mozac_ic_globe)?.apply {
colorFilter = defaultColorFilter
},
secure = context.getDrawable(mozac_ic_lock)?.apply {
colorFilter = defaultColorFilter
}
)
}

companion object {
internal const val MEASURED_HEIGHT_THIRD_DENOMINATOR = 3
internal const val MEASURED_HEIGHT_DENOMINATOR = 2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.browser.toolbar.display

import android.graphics.drawable.Drawable

/**
* Specifies icons to display in the toolbar representing the security of the current website.
*
* @property insecure Icon to display for HTTP sites.
* @property secure Icon to display for HTTPS sites.
*/
class SiteSecurityIcons(
val insecure: Drawable?,
val secure: Drawable?
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<attr name="browserToolbarHintColor" format="color"/>
<attr name="browserToolbarTextColor" format="color"/>
<attr name="browserToolbarTextSize" format="dimension"/>
<attr name="browserToolbarSecureColor" format="color"/>
<attr name="browserToolbarInsecureColor" format="color"/>
<attr name="browserToolbarSecureIcon" format="reference"/>
<attr name="browserToolbarInsecureIcon" format="reference"/>
<attr name="browserToolbarMenuColor" format="color"/>
<attr name="browserToolbarSuggestionBackgroundColor" format="color" />
<attr name="browserToolbarSuggestionForegroundColor" format="color" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
Expand Down Expand Up @@ -796,26 +797,27 @@ class DisplayToolbarTest {
}

@Test
fun `securityIconColor is set when securityIconColor changes`() {
fun `securityIcons is set when securityIcons changes`() {
val toolbar = mock(BrowserToolbar::class.java)
val displayToolbar = DisplayToolbar(testContext, toolbar)

displayToolbar.securityIconColor = Pair(R.color.photonBlue40, R.color.photonBlue40)
val insecure = testContext.getDrawable(R.drawable.mozac_ic_globe)
val secure = testContext.getDrawable(R.drawable.mozac_ic_lock)
displayToolbar.securityIcons = SiteSecurityIcons(insecure, secure)

assertEquals(R.color.photonBlue40, displayToolbar.securityIconColor.first)
assertEquals(R.color.photonBlue40, displayToolbar.securityIconColor.second)
assertEquals(insecure, displayToolbar.securityIcons.insecure)
assertEquals(secure, displayToolbar.securityIcons.secure)
}

@Test
fun `setSiteSecurity is called when securityIconColor changes`() {
fun `setSiteSecurity is called when securityIcons changes`() {
val toolbar = BrowserToolbar(testContext)
toolbar.displayToolbar

assertNull(toolbar.displayToolbar.siteSecurityIconView.colorFilter)
val icons = SiteSecurityIcons(mock(), mock())
assertNotEquals(icons, toolbar.displayToolbar.securityIcons)

toolbar.siteSecurityColor = Pair(R.color.photonBlue40, R.color.photonBlue40)

assertNotNull(toolbar.displayToolbar.siteSecurityIconView.colorFilter)
toolbar.siteSecurityIcons = icons
assertEquals(icons, toolbar.displayToolbar.securityIcons)
}

@Test
Expand Down

0 comments on commit 44bf9a0

Please sign in to comment.