Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fix #75

Open
wants to merge 4 commits into
base: update-deps
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions demo-main/src/main/res/layout/activity_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
android:theme="?actionBarTheme"
app:popupTheme="?popupTheme"
app:title="@string/drawer_layout"
app:titleTextColor="?menuIconColor"
tools:ignore="UnusedAttribute"
/>

Expand Down
1 change: 1 addition & 0 deletions demo-simple-java/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="?popupTheme"
app:titleTextColor="?menuIconColor"
/>

</com.google.android.material.appbar.AppBarLayout>
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/jaredrummler/cyanea/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.jaredrummler.cyanea

internal object Constants {
internal const val NONE_TIMESTAMP = 0L
internal const val LIGHT_ACTIONBAR_LUMINANCE_FACTOR = 0.75
internal const val LIGHT_ACTIONBAR_LUMINANCE_FACTOR = 0.6
}

internal object PrefKeys {
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/java/com/jaredrummler/cyanea/Cyanea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Cyanea private constructor(private val prefs: SharedPreferences) {
/** True if the [baseTheme] is [LIGHT] */
val isLight get() = baseTheme == LIGHT
/** True if the [primary] color is a dark color */
val isActionBarDark get() = ColorUtils.isDarkColor(primary, 0.75)
val isActionBarDark get() = ColorUtils.isDarkColor(primary, LIGHT_ACTIONBAR_LUMINANCE_FACTOR)
/** True if the [primary] color is a light color */
val isActionBarLight get() = !isActionBarDark
/** True if the theme has been modified at least once */
Expand Down Expand Up @@ -553,7 +553,7 @@ class Cyanea private constructor(private val prefs: SharedPreferences) {
fun background(@ColorInt color: Int): Editor {
val lighter = ColorUtils.lighter(color, DEFAULT_LIGHTER_FACTOR)
val darker = ColorUtils.darker(color, DEFAULT_DARKER_FACTOR)
val isDarkColor = ColorUtils.isDarkColor(color, LIGHT_ACTIONBAR_LUMINANCE_FACTOR)
val isDarkColor = ColorUtils.isDarkColor(color)
if (isDarkColor) {
baseTheme(DARK)
backgroundDark(color)
Expand Down
14 changes: 14 additions & 0 deletions library/src/main/java/com/jaredrummler/cyanea/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
package com.jaredrummler.cyanea

import android.content.res.Resources
import android.graphics.BlendMode
import android.graphics.BlendModeColorFilter
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.TypedValue
import com.jaredrummler.cyanea.utils.Reflection


internal fun Resources.getKey(id: Int, resolveRefs: Boolean = true) = getValue(id, resolveRefs).let {
it.assetCookie.toLong() shl 32 or it.data.toLong()
}
Expand All @@ -34,3 +39,12 @@ internal fun Resources.getValue(id: Int, resolveRefs: Boolean = true) = TypedVal
id, value, resolveRefs)
}
}

internal fun Drawable.setColorFilterCompat(color: Int, mode: PorterDuff.Mode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
colorFilter = BlendModeColorFilter(color, BlendMode.valueOf(mode.name))
} else {
@Suppress("DEPRECATION")
setColorFilter(color, mode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.XmlRes
Expand All @@ -39,6 +40,7 @@ import com.jaredrummler.android.colorpicker.ColorPreferenceCompat
import com.jaredrummler.cyanea.Cyanea
import com.jaredrummler.cyanea.R
import com.jaredrummler.cyanea.app.BaseCyaneaActivity
import com.jaredrummler.cyanea.tinting.EdgeEffectTint
import com.jaredrummler.cyanea.tinting.SystemBarTint
import com.jaredrummler.cyanea.utils.ColorUtils

Expand Down Expand Up @@ -165,6 +167,12 @@ open class CyaneaSettingsFragment : PreferenceFragmentCompat(), OnPreferenceChan
}
}

override fun onCreateRecyclerView(inflater: LayoutInflater?, parent: ViewGroup?, savedInstanceState: Bundle?): RecyclerView {
return super.onCreateRecyclerView(inflater, parent, savedInstanceState).apply {
EdgeEffectTint.setEdgeGlowColor(this, cyanea.primary)
}
}

private fun setupNavBarPref() {
ColorUtils.isDarkColor(cyanea.primary, 0.75).let { isDarkEnough ->
prefColorNavBar.isEnabled = isDarkEnough || VERSION.SDK_INT >= VERSION_CODES.O
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.jaredrummler.cyanea.tinting

import android.app.Activity
import android.graphics.BlendMode.SRC_IN
import android.graphics.BlendModeColorFilter
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.os.Build
Expand All @@ -32,8 +30,10 @@ import android.widget.ScrollView
import androidx.annotation.ColorInt
import androidx.core.widget.EdgeEffectCompat
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.jaredrummler.cyanea.Cyanea
import com.jaredrummler.cyanea.setColorFilterCompat
import com.jaredrummler.cyanea.utils.Reflection

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ class EdgeEffectTint(private val view: ViewGroup) {
}
for (name in arrayOf("mEdge", "mGlow")) {
val drawable = Reflection.getFieldValue<Drawable?>(edgeEffect, name)
drawable?.colorFilter = BlendModeColorFilter(color, SRC_IN)
drawable?.setColorFilterCompat(color, PorterDuff.Mode.SRC_IN)
drawable?.callback = null // free up any references
}
} catch (e: Exception) {
Expand All @@ -105,6 +105,7 @@ class EdgeEffectTint(private val view: ViewGroup) {
* * [NestedScrollView]
* * [ViewPager]
* * [WebView]
* * [RecyclerView]
*
* @param view The view to set the edge color
* @param color The color value
Expand All @@ -118,6 +119,7 @@ class EdgeEffectTint(private val view: ViewGroup) {
is NestedScrollView -> setEdgeGlowColor(view, color)
is ViewPager -> setEdgeGlowColor(view, color)
is WebView -> setEdgeGlowColor(view, color)
is RecyclerView -> setEdgeGlowColor(view, color)
else -> return false
}
return true
Expand Down Expand Up @@ -155,13 +157,18 @@ class EdgeEffectTint(private val view: ViewGroup) {
* @param color The color value
*/
private fun setEdgeGlowColor(listView: AbsListView, @ColorInt color: Int) {
try {
for (name in arrayOf("mEdgeGlowTop", "mEdgeGlowBottom")) {
Reflection.getFieldValue<EdgeEffect?>(listView, name)?.let { edgeEffect ->
setEdgeEffectColor(edgeEffect, color)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
listView.topEdgeEffectColor = color
listView.bottomEdgeEffectColor = color
} else {
try {
for (name in arrayOf("mEdgeGlowTop", "mEdgeGlowBottom")) {
Reflection.getFieldValue<EdgeEffect?>(listView, name)?.let { edgeEffect ->
setEdgeEffectColor(edgeEffect, color)
}
}
} catch (ignored: Exception) {
}
} catch (ignored: Exception) {
}
}

Expand All @@ -172,13 +179,18 @@ class EdgeEffectTint(private val view: ViewGroup) {
* @param color The color value
*/
private fun setEdgeGlowColor(hsv: HorizontalScrollView, @ColorInt color: Int) {
try {
for (name in arrayOf("mEdgeGlowLeft", "mEdgeGlowRight")) {
val edgeEffect = Reflection.getFieldValue<EdgeEffect?>(hsv, name)
edgeEffect?.let { setEdgeEffectColor(it, color) }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
hsv.leftEdgeEffectColor = color
hsv.rightEdgeEffectColor = color
} else {
try {
for (name in arrayOf("mEdgeGlowLeft", "mEdgeGlowRight")) {
val edgeEffect = Reflection.getFieldValue<EdgeEffect?>(hsv, name)
edgeEffect?.let { setEdgeEffectColor(it, color) }
}
} catch (e: Exception) {
Cyanea.log(TAG, "Error setting edge glow color on HorizontalScrollView", e)
}
} catch (e: Exception) {
Cyanea.log(TAG, "Error setting edge glow color on HorizontalScrollView", e)
}
}

Expand All @@ -189,13 +201,18 @@ class EdgeEffectTint(private val view: ViewGroup) {
* @param color The color value
*/
private fun setEdgeGlowColor(scrollView: ScrollView, color: Int) {
try {
for (name in arrayOf("mEdgeGlowTop", "mEdgeGlowBottom")) {
val edgeEffect = Reflection.getFieldValue<EdgeEffect?>(scrollView, name)
edgeEffect?.let { setEdgeEffectColor(it, color) }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
scrollView.topEdgeEffectColor = color
scrollView.bottomEdgeEffectColor = color
} else {
try {
for (name in arrayOf("mEdgeGlowTop", "mEdgeGlowBottom")) {
val edgeEffect = Reflection.getFieldValue<EdgeEffect?>(scrollView, name)
edgeEffect?.let { setEdgeEffectColor(it, color) }
}
} catch (e: Exception) {
Cyanea.log(TAG, "Error setting edge glow color on ScrollView", e)
}
} catch (e: Exception) {
Cyanea.log(TAG, "Error setting edge glow color on ScrollView", e)
}
}

Expand Down Expand Up @@ -245,5 +262,23 @@ class EdgeEffectTint(private val view: ViewGroup) {
Cyanea.log(TAG, "Error setting edge glow color on WebView", e)
}
}

/**
* Set the edge-effect color on a [RecyclerView].
*
* @param recyclerView
* the RecyclerView
* @param color
* the color value
*/
private fun setEdgeGlowColor(recyclerView: RecyclerView, color: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
recyclerView.edgeEffectFactory = object : RecyclerView.EdgeEffectFactory() {
override fun createEdgeEffect(view: RecyclerView, direction: Int): EdgeEffect {
return EdgeEffect(view.context).apply { this.color = color }
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.annotation.DrawableRes
import androidx.appcompat.view.menu.MenuItemImpl
import androidx.appcompat.widget.ActionMenuView
import com.jaredrummler.cyanea.Cyanea
import com.jaredrummler.cyanea.setColorFilterCompat
import com.jaredrummler.cyanea.utils.Reflection

/**
Expand Down Expand Up @@ -164,14 +165,14 @@ class MenuTint(
actionBar.navigationIcon?.let { icon ->
menuIconColor?.let { color ->
val navigationIcon = icon.mutate()
navigationIcon.colorFilter = BlendModeColorFilter(color, BlendMode.SRC_IN)
navigationIcon.setColorFilterCompat(color, PorterDuff.Mode.SRC_IN)
}
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && actionBar is Toolbar) {
actionBar.navigationIcon?.let { icon ->
menuIconColor?.let { color ->
val navigationIcon = icon.mutate()
navigationIcon.colorFilter = BlendModeColorFilter(color, BlendMode.SRC_IN)
navigationIcon.setColorFilterCompat(color, PorterDuff.Mode.SRC_IN)
}
}
}
Expand Down Expand Up @@ -206,7 +207,7 @@ class MenuTint(
fun colorMenuItem(menuItem: MenuItem, color: Int?, alpha: Int? = null) {
menuItem.icon?.let { icon ->
val drawable = icon.mutate()
color?.let { drawable.colorFilter = BlendModeColorFilter(color, SRC_IN) }
color?.let { drawable.setColorFilterCompat(color, PorterDuff.Mode.SRC_IN) }
alpha?.let { drawable.alpha = it }
menuItem.icon = drawable
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import com.jaredrummler.cyanea.Cyanea
import com.jaredrummler.cyanea.setColorFilterCompat
import com.jaredrummler.cyanea.utils.Reflection

/**
Expand Down Expand Up @@ -58,7 +59,7 @@ class WidgetTint private constructor() {
?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
} else {
Reflection.getFieldValue<Drawable?>(scroller, "mThumbDrawable")
?.colorFilter = BlendModeColorFilter(color, SRC_ATOP)
?.setColorFilterCompat(color, PorterDuff.Mode.SRC_ATOP)
}
} catch (e: Exception) {
Cyanea.log(TAG, "Error tinting the fast scroll thumb", e)
Expand All @@ -79,7 +80,7 @@ class WidgetTint private constructor() {
Reflection.getField(editor, "mCursorDrawable")?.let { fCursorDrawable ->
val cursorDrawableRes = fCursorDrawableRes.getInt(textView)
ContextCompat.getDrawable(textView.context, cursorDrawableRes)?.let { drawable ->
drawable.colorFilter = BlendModeColorFilter(color, SRC_IN)
drawable.setColorFilterCompat(color, PorterDuff.Mode.SRC_IN)
val drawables = arrayOf(drawable, drawable)
fCursorDrawable.set(editor, drawables)
}
Expand Down
19 changes: 19 additions & 0 deletions library/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<resources>
<string name="label_theme_picker">Выбор темы</string>

<string name="pref_theme_title">Темы</string>
<string name="pref_theme_summary">Выбор предустановленной темы</string>

<string name="pref_primary_color_title">Основной цвет</string>
<string name="pref_primary_color_summary">Наиболее используемый в приложении.</string>

<string name="pref_accent_color_title">Цветовой акцент</string>
<string name="pref_accent_color_summary">Акцент, которым выделяются части интерфейса.</string>

<string name="pref_background_color_title">Цвет фона</string>
<string name="pref_background_color_summary">Цвет подложки у содержимого приложения</string>
<string name="pref_category_appearance">Внешний вид</string>

<string name="pref_colored_navigation_bar_title">Цвет панели навигации</string>
<string name="pref_colored_navigation_bar_summary">Применить основной цвет к панели навигации</string>
</resources>