1
0
Fork 0

Fixes #4005 - Move getColorFromAttr to Context

master
Tiger Oakes 2019-07-22 16:59:24 -04:00 committed by Christian Sadilek
parent c8148e61d5
commit c2e6e25bef
19 changed files with 55 additions and 98 deletions

View File

@ -388,7 +388,7 @@ class BrowserFragment : Fragment(), BackHandler {
view = view)
}
toolbarComponent.getView().setOnSiteSecurityClickedListener {
toolbarComponent.setOnSiteSecurityClickedListener {
showQuickSettingsDialog()
}

View File

@ -5,11 +5,7 @@
package org.mozilla.fenix.components.toolbar
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.component_search.*
import mozilla.components.browser.toolbar.BrowserToolbar
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.mvi.Action
import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.Change
@ -32,8 +28,6 @@ class ToolbarComponent(
viewModelProvider
) {
fun getView(): BrowserToolbar = uiView.toolbar
override fun initView() = ToolbarUIView(
sessionId,
isPrivate,
@ -44,22 +38,10 @@ class ToolbarComponent(
init {
bind()
applyTheme()
}
private fun applyTheme() {
getView().suggestionBackgroundColor = ContextCompat.getColor(
container.context,
R.color.suggestion_highlight_color
)
getView().textColor = ContextCompat.getColor(
container.context,
ThemeManager.resolveAttribute(R.attr.primaryText, container.context)
)
getView().hintColor = ContextCompat.getColor(
container.context,
ThemeManager.resolveAttribute(R.attr.secondaryText, container.context)
)
fun setOnSiteSecurityClickedListener(listener: () -> Unit) {
uiView.toolbar.setOnSiteSecurityClickedListener(listener)
}
}

View File

@ -17,6 +17,7 @@ import mozilla.components.support.ktx.android.util.dpToPx
import org.mozilla.fenix.R
import org.mozilla.fenix.customtabs.CustomTabToolbarMenu
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getColorFromAttr
import org.mozilla.fenix.mvi.UIView
class ToolbarUIView(
@ -59,6 +60,10 @@ class ToolbarUIView(
textColor = ContextCompat.getColor(context, R.color.photonGrey30)
hint = context.getString(R.string.search_hint)
suggestionBackgroundColor = ContextCompat.getColor(context, R.color.suggestion_highlight_color)
textColor = context.getColorFromAttr(R.attr.primaryText)
hintColor = context.getColorFromAttr(R.attr.secondaryText)
}
with(view.context) {

View File

@ -15,13 +15,18 @@ import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.view.ContextThemeWrapper
import android.view.View
import android.view.ViewGroup
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import mozilla.components.browser.search.SearchEngineManager
import mozilla.components.support.base.log.Log
import mozilla.components.support.base.log.Log.Priority.WARN
import org.mozilla.fenix.FenixApplication
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.components.Components
import org.mozilla.fenix.components.metrics.MetricController
@ -94,3 +99,15 @@ fun Context.share(text: String, subject: String = ""): Boolean {
*/
fun Context.getRootView(): View? =
asActivity()?.window?.decorView?.findViewById<View>(android.R.id.content) as? ViewGroup
/**
* Returns the color resource corresponding to the attribute.
*/
@ColorRes
fun Context.getColorResFromAttr(@AttrRes attr: Int) = ThemeManager.resolveAttribute(attr, this)
/**
* Returns the color int corresponding to the attribute.
*/
@ColorInt
fun Context.getColorFromAttr(@AttrRes attr: Int) = ContextCompat.getColor(this, getColorResFromAttr(attr))

View File

@ -1,23 +0,0 @@
/* 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 org.mozilla.fenix.ext
import android.content.Context
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import org.mozilla.fenix.ThemeManager
/**
* Returns the color resource corresponding to the attribute.
*/
@ColorRes
fun Int.getColorIntFromAttr(context: Context): Int = ThemeManager.resolveAttribute(this, context)
/**
* Returns the color int corresponding to the attribute.
*/
@ColorInt
fun Int.getColorFromAttr(context: Context): Int = ContextCompat.getColor(context, this.getColorIntFromAttr(context))

View File

@ -48,7 +48,7 @@ class SwipeToDeleteCallback(
) {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
val icon = ContextCompat.getDrawable(recyclerView.context, R.drawable.ic_delete)
icon?.setTint(R.attr.destructive.getColorFromAttr(recyclerView.context))
icon?.setTint(recyclerView.context.getColorFromAttr(R.attr.destructive))
val backgroundDrawable = when {
viewHolder is TabInCollectionViewHolder && viewHolder.isLastTab -> {

View File

@ -95,7 +95,7 @@ class TabInCollectionViewHolder(
view.background = ContextCompat.getDrawable(view.context, R.drawable.rounded_bottom_corners)
divider_line.visibility = View.GONE
} else {
view.backgroundColor = R.attr.above.getColorFromAttr(view.context)
view.backgroundColor = view.context.getColorFromAttr(R.attr.above)
divider_line.visibility = View.VISIBLE
}
}

View File

@ -91,8 +91,8 @@ class LibraryFragment : Fragment() {
private fun setToolbarColor() {
val toolbar = (activity as AppCompatActivity).findViewById<Toolbar>(R.id.navigationToolbar)
val backgroundColor = R.attr.foundation.getColorFromAttr(context!!)
val foregroundColor = R.attr.primaryText.getColorFromAttr(context!!)
val backgroundColor = context!!.getColorFromAttr(R.attr.foundation)
val foregroundColor = context!!.getColorFromAttr(R.attr.primaryText)
toolbar.setBackgroundColor(backgroundColor)
toolbar.setTitleTextColor(foregroundColor)

View File

@ -14,7 +14,7 @@ import mozilla.appservices.places.BookmarkRoot
import mozilla.components.concept.storage.BookmarkNode
import mozilla.components.support.base.feature.BackHandler
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.getColorIntFromAttr
import org.mozilla.fenix.ext.getColorResFromAttr
import org.mozilla.fenix.library.LibraryPageView
/**
@ -194,7 +194,7 @@ class BookmarkView(
context.getString(R.string.bookmarks_multi_select_title, mode.selectedItems.size)
setToolbarColors(
R.color.white_color,
R.attr.accentHighContrast.getColorIntFromAttr(context)
context.getColorResFromAttr(R.attr.accentHighContrast)
)
}
@ -202,8 +202,8 @@ class BookmarkView(
bookmarkAdapter.updateData(root, BookmarkState.Mode.Normal)
setTitle(root)
setToolbarColors(
R.attr.primaryText.getColorIntFromAttr(context),
R.attr.foundation.getColorIntFromAttr(context)
context.getColorResFromAttr(R.attr.primaryText),
context.getColorResFromAttr(R.attr.foundation)
)
}

View File

@ -71,7 +71,7 @@ class AddBookmarkFolderFragment : Fragment() {
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.bookmarks_add_folder, menu)
menu.findItem(R.id.confirm_add_folder_button).icon.colorFilter =
PorterDuffColorFilter(R.attr.primaryText.getColorFromAttr(context!!), SRC_IN)
PorterDuffColorFilter(context!!.getColorFromAttr(R.attr.primaryText), SRC_IN)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {

View File

@ -147,7 +147,7 @@ class EditBookmarkFragment : Fragment() {
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.bookmarks_edit, menu)
menu.findItem(R.id.delete_bookmark_button).icon.colorFilter =
PorterDuffColorFilter(R.attr.primaryText.getColorFromAttr(context!!), SRC_IN)
PorterDuffColorFilter(context!!.getColorFromAttr(R.attr.primaryText), SRC_IN)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {

View File

@ -16,7 +16,7 @@ import mozilla.components.concept.storage.BookmarkNode
import mozilla.components.concept.storage.BookmarkNodeType
import mozilla.components.support.ktx.android.util.dpToPx
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.getColorIntFromAttr
import org.mozilla.fenix.ext.getColorResFromAttr
import org.mozilla.fenix.library.bookmarks.BookmarksSharedViewModel
class SelectBookmarkFolderAdapter(private val sharedViewModel: BookmarksSharedViewModel) :
@ -86,12 +86,7 @@ class SelectBookmarkFolderAdapter(private val sharedViewModel: BookmarksSharedVi
}
fun bind(folder: BookmarkNodeWithDepth, selected: Boolean, selectionInterface: SelectionInterface) {
val backgroundTint =
if (selected) {
R.attr.accentBright.getColorIntFromAttr(containerView!!.context)
} else {
R.attr.neutral.getColorIntFromAttr(containerView!!.context)
}
val backgroundTintAttr = if (selected) R.attr.accentBright else R.attr.neutral
// Center the bookmark title since we don't have a url
val constraintSet = ConstraintSet()
@ -101,6 +96,7 @@ class SelectBookmarkFolderAdapter(private val sharedViewModel: BookmarksSharedVi
)
constraintSet.applyTo(bookmark_layout)
val backgroundTint = containerView!!.context.getColorResFromAttr(backgroundTintAttr)
val backgroundTintList = ContextCompat.getColorStateList(containerView.context, backgroundTint)
bookmark_favicon.backgroundTintList = backgroundTintList
val res = if (selected) R.drawable.mozac_ic_check else R.drawable.ic_folder_icon

View File

@ -110,7 +110,7 @@ class SelectBookmarkFolderFragment : Fragment(), AccountObserver {
if (!visitedAddBookmark) {
inflater.inflate(R.menu.bookmarks_select_folder, menu)
menu.findItem(R.id.add_folder_button).icon.colorFilter =
PorterDuffColorFilter(R.attr.primaryText.getColorFromAttr(context!!), SRC_IN)
PorterDuffColorFilter(context!!.getColorFromAttr(R.attr.primaryText), SRC_IN)
}
}

View File

@ -23,7 +23,7 @@ import kotlinx.android.synthetic.main.delete_history_button.*
import mozilla.components.support.base.feature.BackHandler
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.asActivity
import org.mozilla.fenix.ext.getColorIntFromAttr
import org.mozilla.fenix.ext.getColorResFromAttr
/**
* Interface for the HistoryViewInteractor. This interface is implemented by objects that want
@ -135,7 +135,7 @@ class HistoryView(
context.getString(R.string.history_multi_select_title, selectedItemSize)
setToolbarColors(
R.color.white_color,
R.attr.accentHighContrast.getColorIntFromAttr(context!!)
context!!.getColorResFromAttr(R.attr.accentHighContrast)
)
}
@ -144,8 +144,8 @@ class HistoryView(
delete_history_button?.isVisible = !isEmpty
history_empty_view.isVisible = isEmpty
setToolbarColors(
R.attr.primaryText.getColorIntFromAttr(context!!),
R.attr.foundation.getColorIntFromAttr(context)
context!!.getColorResFromAttr(R.attr.primaryText),
context.getColorResFromAttr(R.attr.foundation)
)
}

View File

@ -17,7 +17,6 @@ import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import kotlinx.android.synthetic.main.fragment_search.*
@ -33,9 +32,9 @@ import org.jetbrains.anko.backgroundDrawable
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.components.StoreProvider
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.getColorFromAttr
import org.mozilla.fenix.ext.getSpannable
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.search.awesomebar.AwesomeBarView
@ -220,12 +219,7 @@ class SearchFragment : Fragment(), BackHandler {
val color = if (showShortcuts) R.attr.contrastText else R.attr.primaryText
search_shortcuts_button.compoundDrawables[0]?.setTint(
ContextCompat.getColor(
this,
ThemeManager.resolveAttribute(color, this)
)
)
search_shortcuts_button.compoundDrawables[0]?.setTint(getColorFromAttr(color))
}
}

View File

@ -5,10 +5,10 @@ package org.mozilla.fenix.search.awesomebar
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import android.graphics.PorterDuff.Mode.SRC_IN
import android.graphics.PorterDuffColorFilter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toBitmap
import kotlinx.android.extensions.LayoutContainer
import mozilla.components.browser.awesomebar.BrowserAwesomeBar
@ -24,8 +24,8 @@ import mozilla.components.feature.search.SearchUseCases
import mozilla.components.feature.session.SessionUseCases
import mozilla.components.feature.tabs.TabsUseCases
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getColorFromAttr
import org.mozilla.fenix.search.SearchEngineSource
import org.mozilla.fenix.search.SearchState
@ -111,10 +111,7 @@ class AwesomeBarView(
init {
with(container.context) {
val primaryTextColor = ContextCompat.getColor(
this,
ThemeManager.resolveAttribute(R.attr.primaryText, this)
)
val primaryTextColor = getColorFromAttr(R.attr.primaryText)
val draw = getDrawable(R.drawable.ic_link)!!
draw.setColorFilter(primaryTextColor, SRC_IN)
@ -201,12 +198,7 @@ class AwesomeBarView(
private fun createSuggestionProviderForEngine(engine: SearchEngine): SearchSuggestionProvider {
return with(container.context) {
val draw = getDrawable(R.drawable.ic_search)
draw?.setColorFilter(
ContextCompat.getColor(
this,
ThemeManager.resolveAttribute(R.attr.primaryText, this)
), SRC_IN
)
draw?.colorFilter = PorterDuffColorFilter(getColorFromAttr(R.attr.primaryText), SRC_IN)
SearchSuggestionProvider(
components.search.searchEngineManager.getDefaultSearchEngine(this, engine.name),

View File

@ -16,7 +16,7 @@ import mozilla.components.concept.storage.HistoryStorage
import mozilla.components.feature.toolbar.ToolbarAutocompleteFeature
import mozilla.components.support.ktx.android.content.res.pxToDp
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.ext.getColorFromAttr
import org.mozilla.fenix.search.SearchState
/**
@ -78,15 +78,9 @@ class ToolbarView(
hint = context.getString(R.string.search_hint)
textColor = ContextCompat.getColor(
container.context,
ThemeManager.resolveAttribute(R.attr.primaryText, container.context)
)
textColor = container.context.getColorFromAttr(R.attr.primaryText)
hintColor = ContextCompat.getColor(
container.context,
ThemeManager.resolveAttribute(R.attr.secondaryText, container.context)
)
hintColor = container.context.getColorFromAttr(R.attr.secondaryText)
suggestionBackgroundColor = ContextCompat.getColor(
container.context,

View File

@ -48,7 +48,7 @@ object SupportUtils {
}
fun createCustomTabIntent(context: Context, url: String) = Intent(Intent.ACTION_VIEW).apply {
putExtra(context.getString(R.string.intent_extra_toolbar_color), R.attr.foundation.getColorFromAttr(context))
putExtra(context.getString(R.string.intent_extra_toolbar_color), context.getColorFromAttr(R.attr.foundation))
putExtra(context.getString(R.string.intent_extra_session), true)
setClassName(context.applicationContext, IntentReceiverActivity::class.java.name)
data = Uri.parse(url)
@ -56,7 +56,7 @@ object SupportUtils {
}
fun createAuthCustomTabIntent(context: Context, url: String) = Intent(Intent.ACTION_VIEW).apply {
putExtra(context.getString(R.string.intent_extra_toolbar_color), R.attr.foundation.getColorFromAttr(context))
putExtra(context.getString(R.string.intent_extra_toolbar_color), context.getColorFromAttr(R.attr.foundation))
putExtra(context.getString(R.string.intent_extra_session), true)
putExtra(context.getString(R.string.intent_extra_auth), true)
setClassName(context.applicationContext, IntentReceiverActivity::class.java.name)

View File

@ -48,7 +48,7 @@ class ClearableEditText @JvmOverloads constructor(
super.onTextChanged(text, start, lengthBefore, lengthAfter)
val drawable = if (shouldShowClearButton(lengthAfter)) {
context.getDrawable(R.drawable.ic_clear)?.apply {
colorFilter = PorterDuffColorFilter(R.attr.primaryText.getColorFromAttr(context), SRC_IN)
colorFilter = PorterDuffColorFilter(context.getColorFromAttr(R.attr.primaryText), SRC_IN)
}
} else {
null