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

Commit

Permalink
Merge #4395 #4542
Browse files Browse the repository at this point in the history
4395: Uplift Theme.resolveAttribute from Fenix r=pocmo a=NotWoods

Uplifts functions from Fenix to get a color int from a theme.



4542:  Public Suffix List update (20190926-142145) r=pocmo a=MickeyMoz



Co-authored-by: Tiger Oakes <toakes@mozilla.com>
Co-authored-by: MickeyMoz <sebastian@mozilla.com>
  • Loading branch information
3 people committed Sep 26, 2019
3 parents 9af4fdd + ea9df0d + 0cead88 commit 639f5c3
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

package mozilla.components.browser.menu.item

import android.util.TypedValue
import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatImageView
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.browser.menu.R
import mozilla.components.support.ktx.android.content.res.resolveAttribute

private val defaultHighlight = BrowserMenuHighlightableItem.Highlight(0, 0, 0, 0)

Expand Down Expand Up @@ -109,14 +109,10 @@ class BrowserMenuHighlightableItem(
}
}
} else {
val selectableItemBackground = TypedValue()
view.context.theme.resolveAttribute(
android.R.attr.selectableItemBackground,
selectableItemBackground,
true
)
val selectableItemBackground =
view.context.theme.resolveAttribute(android.R.attr.selectableItemBackground)

view.setBackgroundResource(selectableItemBackground.resourceId)
view.setBackgroundResource(selectableItemBackground)
with(endImageView) {
setImageResource(0)
visibility = View.GONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package mozilla.components.browser.menu.item

import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
Expand All @@ -14,6 +13,7 @@ import androidx.appcompat.widget.AppCompatImageButton
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.browser.menu.BrowserMenuItem
import mozilla.components.browser.menu.R
import mozilla.components.support.ktx.android.content.res.resolveAttribute

/**
* A toolbar of buttons to show inside the browser menu.
Expand All @@ -29,20 +29,15 @@ class BrowserMenuItemToolbar(
val layout = view as LinearLayout
layout.removeAllViews()

val selectableBackground = TypedValue().also {
layout.context.theme.resolveAttribute(
android.R.attr.selectableItemBackgroundBorderless,
it,
true
)
}
val selectableBackground =
layout.context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless)
val iconHeight = view.resources.getDimensionPixelSize(R.dimen.mozac_browser_menu_item_image_text_icon_height)

for (item in items) {
val button = AppCompatImageButton(layout.context)
item.bind(button)

button.setBackgroundResource(selectableBackground.resourceId)
button.setBackgroundResource(selectableBackground)
button.setOnClickListener {
item.listener.invoke()
menu.dismiss()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.view.View.OnFocusChangeListener
import android.view.ViewGroup
Expand Down Expand Up @@ -38,6 +37,7 @@ import mozilla.components.concept.toolbar.AutocompleteResult
import mozilla.components.concept.toolbar.Toolbar
import mozilla.components.support.base.android.Padding
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.ktx.android.content.res.resolveAttribute
import mozilla.components.ui.autocomplete.AutocompleteView
import mozilla.components.ui.autocomplete.InlineAutocompleteEditText
import mozilla.components.ui.autocomplete.OnFilterListener
Expand Down Expand Up @@ -349,14 +349,9 @@ class BrowserToolbar @JvmOverloads constructor(
listener.invoke()
}

val outValue = TypedValue()

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

displayToolbar.siteSecurityIconView.setBackgroundResource(outValue.resourceId)
displayToolbar.siteSecurityIconView.setBackgroundResource(
context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless)
)
}
}

Expand All @@ -372,14 +367,9 @@ class BrowserToolbar @JvmOverloads constructor(
listener.invoke()
}

val outValue = TypedValue()

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

displayToolbar.trackingProtectionIconView.setBackgroundResource(outValue.resourceId)
displayToolbar.trackingProtectionIconView.setBackgroundResource(
context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package mozilla.components.browser.toolbar.display

import android.content.Context
import android.content.res.ColorStateList
import android.util.TypedValue
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.ColorInt
Expand All @@ -19,6 +18,7 @@ import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.BrowserMenuHighlightableItem
import mozilla.components.browser.toolbar.R
import mozilla.components.browser.toolbar.facts.emitOpenMenuFact
import mozilla.components.support.ktx.android.content.res.resolveAttribute

@Suppress("ViewConstructor") // This view is only instantiated in code
internal class MenuButton(
Expand All @@ -35,12 +35,7 @@ internal class MenuButton(
}

init {
val outValue = TypedValue()
context.theme.resolveAttribute(
android.R.attr.selectableItemBackgroundBorderless,
outValue,
true)
setBackgroundResource(outValue.resourceId)
setBackgroundResource(context.theme.resolveAttribute(android.R.attr.selectableItemBackground))

visibility = View.GONE
isClickable = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
package mozilla.components.concept.toolbar

import android.graphics.drawable.Drawable
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatImageButton
import androidx.appcompat.widget.AppCompatImageView
import mozilla.components.support.base.android.Padding
import mozilla.components.support.ktx.android.content.res.resolveAttribute
import mozilla.components.support.ktx.android.view.setPadding
import java.lang.ref.WeakReference

Expand Down Expand Up @@ -197,17 +197,14 @@ interface Toolbar {
imageButton.contentDescription = contentDescription
imageButton.setOnClickListener { listener.invoke() }

if (background == 0) {
val outValue = TypedValue()
parent.context.theme.resolveAttribute(
android.R.attr.selectableItemBackgroundBorderless,
outValue,
true
)
imageButton.setBackgroundResource(outValue.resourceId)
@DrawableRes
val backgroundResource = if (background == 0) {
parent.context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless)
} else {
imageButton.setBackgroundResource(background)
background
}

imageButton.setBackgroundResource(backgroundResource)
padding?.let { imageButton.setPadding(it) }
}

Expand Down Expand Up @@ -248,17 +245,14 @@ interface Toolbar {

updateViewState()

if (background == 0) {
val outValue = TypedValue()
parent.context.theme.resolveAttribute(
android.R.attr.selectableItemBackgroundBorderless,
outValue,
true)

imageButton.setBackgroundResource(outValue.resourceId)
@DrawableRes
val backgroundResource = if (background == 0) {
parent.context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless)
} else {
imageButton.setBackgroundResource(background)
background
}

imageButton.setBackgroundResource(backgroundResource)
padding?.let { imageButton.setPadding(it) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

package mozilla.components.feature.tabs.toolbar

import android.util.TypedValue
import android.view.HapticFeedbackConstants
import android.view.View
import android.view.ViewGroup
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.concept.toolbar.Toolbar
import mozilla.components.feature.tabs.R
import mozilla.components.support.ktx.android.content.res.resolveAttribute
import mozilla.components.ui.tabcounter.TabCounter
import java.lang.ref.WeakReference

Expand All @@ -38,9 +38,9 @@ class TabCounterToolbarButton(
}

// Set selectableItemBackgroundBorderless
val outValue = TypedValue()
parent.context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true)
view.setBackgroundResource(outValue.resourceId)
val selectableItemBackgroundBorderless =
parent.context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless)
view.setBackgroundResource(selectableItemBackgroundBorderless)
return view
}

Expand Down
Binary file modified components/lib/publicsuffixlist/src/main/assets/publicsuffixes
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.hardware.camera2.CameraManager
import android.os.Process
import android.view.accessibility.AccessibilityManager
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.checkSelfPermission
import androidx.core.content.getSystemService
import mozilla.components.support.base.log.Log
import mozilla.components.support.ktx.R
import mozilla.components.support.ktx.android.content.res.resolveAttribute

/**
* The (visible) version name of the application, as specified by the <manifest> tag's versionName
Expand Down Expand Up @@ -130,3 +134,10 @@ inline fun Context.runOnlyInMainProcess(block: () -> Unit) {
block()
}
}

/**
* Returns the color int corresponding to the attribute.
*/
@ColorInt
fun Context.getColorFromAttr(@AttrRes attr: Int) =
ContextCompat.getColor(this, theme.resolveAttribute(attr))
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* 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.support.ktx.android.content.res

import android.content.res.Resources
import android.util.TypedValue
import androidx.annotation.AnyRes
import androidx.annotation.AttrRes

/**
* Resolves the resource ID corresponding to the given attribute.
*
* @sample
* context.theme.resolveAttribute(R.attr.textColor) == R.color.light_text_color
*/
@AnyRes
fun Resources.Theme.resolveAttribute(@AttrRes attribute: Int): Int {
val outValue = TypedValue()
resolveAttribute(attribute, outValue, true)
return outValue.resourceId
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mock-maker-inline
// This allows mocking final classes (classes are final by default in Kotlin)
8 changes: 6 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ permalink: /changelog/
* **browser-session**, **browser-state**, **feature-contextmenu**, **feature-downloads**
* * ⚠️ **This is a breaking change**: Removed the `download` property from `Session`. Downloads can now only be observed on a `BrowserState` from the `browser-state` component. Therefore `ContextMenuUseCases` and `DownloadsUseCases` now require a `BrowserStore` instance.

* **support-ktx**
* Adds `Resources.Theme.resolveAttribute(Int)` to quickly get a resource ID from a theme.
* Adds `Context.getColorFromAttr` to get a color int from an attribute.

# 14.0.1

* [Commits](https://github.com/mozilla-mobile/android-components/compare/v14.0.0...v14.0.1)
Expand Down Expand Up @@ -82,7 +86,7 @@ permalink: /changelog/

* **tools-detekt**
* New (internal-only) component with custom detekt rules.

* **service-glean**
***This is a breaking change**: Glean.initialize() must be called on the main thread.

Expand Down Expand Up @@ -115,7 +119,7 @@ permalink: /changelog/

* **feature-tabs**
* ⚠️ **This is a breaking change**: Methods that have been accepting a parent `Session` parameter now expect the parent id (`String`).

* **browser-menu**
* Adds the ability to create a BrowserMenuImageSwitch, a BrowserMenuSwitch with icon

Expand Down

0 comments on commit 639f5c3

Please sign in to comment.