pull/1395/head
gedoor 3 years ago
parent cffce34d1d
commit 33a7b83b27
  1. 2
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  2. 48
      app/src/main/java/io/legado/app/lib/theme/ATH.kt
  3. 98
      app/src/main/java/io/legado/app/lib/theme/TintHelper.kt
  4. 23
      app/src/main/java/io/legado/app/utils/ActivityExtensions.kt

@ -158,7 +158,7 @@ abstract class BaseActivity<VB : ViewBinding>(
private fun setupSystemBar() { private fun setupSystemBar() {
if (fullScreen && !isInMultiWindow) { if (fullScreen && !isInMultiWindow) {
ATH.fullScreen(this) fullScreen(true)
} }
val isTransparentStatusBar = AppConfig.isTransparentStatusBar val isTransparentStatusBar = AppConfig.isTransparentStatusBar
val statusBarColor = ThemeStore.statusBarColor(this, isTransparentStatusBar) val statusBarColor = ThemeStore.statusBarColor(this, isTransparentStatusBar)

@ -1,13 +1,7 @@
package io.legado.app.lib.theme package io.legado.app.lib.theme
import android.annotation.SuppressLint
import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.graphics.drawable.GradientDrawable import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.view.View import android.view.View
import android.view.WindowManager
import android.widget.EdgeEffect import android.widget.EdgeEffect
import android.widget.ScrollView import android.widget.ScrollView
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
@ -25,48 +19,6 @@ import splitties.init.appCtx
@Suppress("unused", "MemberVisibilityCanBePrivate") @Suppress("unused", "MemberVisibilityCanBePrivate")
object ATH { object ATH {
@SuppressLint("CommitPrefEdits")
fun didThemeValuesChange(context: Context, since: Long): Boolean {
return ThemeStore.isConfigured(context) && ThemeStore.prefs(context).getLong(
ThemeStorePrefKeys.VALUES_CHANGED,
-1
) > since
}
fun fullScreen(activity: Activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
activity.window.setDecorFitsSystemWindows(true)
}
fullScreenO(activity)
activity.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
}
@Suppress("DEPRECATION")
private fun fullScreenO(activity: Activity) {
activity.window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
activity.window.clearFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
)
}
fun setTaskDescriptionColorAuto(activity: Activity) {
setTaskDescriptionColor(activity, ThemeStore.primaryColor(activity))
}
fun setTaskDescriptionColor(activity: Activity, @ColorInt color: Int) {
val color1: Int = ColorUtils.stripAlpha(color)
@Suppress("DEPRECATION")
activity.setTaskDescription(
ActivityManager.TaskDescription(
activity.title as String,
null,
color1
)
)
}
fun setTint( fun setTint(
view: View, view: View,
@ColorInt color: Int, @ColorInt color: Int,

@ -6,7 +6,6 @@ import android.content.res.ColorStateList
import android.graphics.PorterDuff import android.graphics.PorterDuff
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.graphics.drawable.RippleDrawable import android.graphics.drawable.RippleDrawable
import android.os.Build
import android.view.View import android.view.View
import android.widget.* import android.widget.*
import androidx.annotation.CheckResult import androidx.annotation.CheckResult
@ -68,10 +67,8 @@ object TintHelper {
when (view) { when (view) {
is Button -> { is Button -> {
sl = getDisabledColorStateList(color, disabled) sl = getDisabledColorStateList(color, disabled)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && view.getBackground() is RippleDrawable) { val rd = view.getBackground() as RippleDrawable
val rd = view.getBackground() as RippleDrawable rd.setColor(ColorStateList.valueOf(rippleColor))
rd.setColor(ColorStateList.valueOf(rippleColor))
}
// Disabled text color state for buttons, may get overridden later by ATE tags // Disabled text color state for buttons, may get overridden later by ATE tags
view.setTextColor( view.setTextColor(
@ -133,8 +130,10 @@ object TintHelper {
} }
fun setTintAuto( fun setTintAuto(
view: View, @ColorInt color: Int, view: View,
isBackground: Boolean, isDark: Boolean @ColorInt color: Int,
isBackground: Boolean,
isDark: Boolean
) { ) {
var isBg = isBackground var isBg = isBackground
if (!isBg) { if (!isBg) {
@ -166,26 +165,22 @@ object TintHelper {
else -> isBg = true else -> isBg = true
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && // Ripples for the above views (e.g. when you tap and hold a switch or checkbox)
!isBg && view.background is RippleDrawable val rd = view.background as RippleDrawable
) { @SuppressLint("PrivateResource") val unchecked = ContextCompat.getColor(
// Ripples for the above views (e.g. when you tap and hold a switch or checkbox) view.context,
val rd = view.background as RippleDrawable if (isDark) R.color.ripple_material_dark else R.color.ripple_material_light
@SuppressLint("PrivateResource") val unchecked = ContextCompat.getColor( )
view.context, val checked = ColorUtils.adjustAlpha(color, 0.4f)
if (isDark) R.color.ripple_material_dark else R.color.ripple_material_light val sl = ColorStateList(
) arrayOf(
val checked = ColorUtils.adjustAlpha(color, 0.4f) intArrayOf(-android.R.attr.state_activated, -android.R.attr.state_checked),
val sl = ColorStateList( intArrayOf(android.R.attr.state_activated),
arrayOf( intArrayOf(android.R.attr.state_checked)
intArrayOf(-android.R.attr.state_activated, -android.R.attr.state_checked), ),
intArrayOf(android.R.attr.state_activated), intArrayOf(unchecked, checked, checked)
intArrayOf(android.R.attr.state_checked) )
), rd.setColor(sl)
intArrayOf(unchecked, checked, checked)
)
rd.setColor(sl)
}
} }
if (isBg) { if (isBg) {
// Need to tint the isBackground of a view // Need to tint the isBackground of a view
@ -223,14 +218,7 @@ object TintHelper {
color color
) )
) )
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { radioButton.buttonTintList = sl
radioButton.buttonTintList = sl
} else {
radioButton.buttonDrawable = createTintedDrawable(
ContextCompat.getDrawable(radioButton.context, R.drawable.abc_btn_radio_material),
sl
)
}
} }
fun setTint(seekBar: SeekBar, @ColorInt color: Int, useDarker: Boolean) { fun setTint(seekBar: SeekBar, @ColorInt color: Int, useDarker: Boolean) {
@ -241,15 +229,8 @@ object TintHelper {
if (useDarker) R.color.ate_control_disabled_dark else R.color.ate_control_disabled_light if (useDarker) R.color.ate_control_disabled_dark else R.color.ate_control_disabled_light
) )
) )
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { seekBar.thumbTintList = s1
seekBar.thumbTintList = s1 seekBar.progressTintList = s1
seekBar.progressTintList = s1
} else {
val progressDrawable = createTintedDrawable(seekBar.progressDrawable, s1)
seekBar.progressDrawable = progressDrawable
val thumbDrawable = createTintedDrawable(seekBar.thumb, s1)
seekBar.thumb = thumbDrawable
}
} }
@JvmOverloads @JvmOverloads
@ -258,17 +239,10 @@ object TintHelper {
skipIndeterminate: Boolean = false skipIndeterminate: Boolean = false
) { ) {
val sl = ColorStateList.valueOf(color) val sl = ColorStateList.valueOf(color)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { progressBar.progressTintList = sl
progressBar.progressTintList = sl progressBar.secondaryProgressTintList = sl
progressBar.secondaryProgressTintList = sl if (!skipIndeterminate)
if (!skipIndeterminate) progressBar.indeterminateTintList = sl
progressBar.indeterminateTintList = sl
} else {
if (!skipIndeterminate && progressBar.indeterminateDrawable != null)
progressBar.indeterminateDrawable.setTint(color)
if (progressBar.progressDrawable != null)
progressBar.progressDrawable.setTint(color)
}
} }
@ -320,18 +294,7 @@ object TintHelper {
color color
) )
) )
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { box.buttonTintList = sl
box.buttonTintList = sl
} else {
val drawable =
createTintedDrawable(
ContextCompat.getDrawable(
box.context,
R.drawable.abc_btn_check_material
), sl
)
box.buttonDrawable = drawable
}
} }
fun setTint(image: ImageView, @ColorInt color: Int) { fun setTint(image: ImageView, @ColorInt color: Int) {
@ -463,6 +426,7 @@ object TintHelper {
return drawable1 return drawable1
} }
@SuppressLint("DiscouragedPrivateApi", "SoonBlockedPrivateApi")
fun setCursorTint(editText: EditText, @ColorInt color: Int) { fun setCursorTint(editText: EditText, @ColorInt color: Int) {
try { try {
val fCursorDrawableRes = TextView::class.java.getDeclaredField("mCursorDrawableRes") val fCursorDrawableRes = TextView::class.java.getDeclaredField("mCursorDrawableRes")

@ -42,6 +42,29 @@ val Activity.windowSize: DisplayMetrics
return displayMetrics return displayMetrics
} }
fun Activity.fullScreen(fullScreen: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(fullScreen)
}
@Suppress("DEPRECATION")
if (fullScreen) {
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
window.clearFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
} else {
window.decorView.systemUiVisibility = 0
window.addFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
)
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
}
}
/** /**
* 设置状态栏颜色 * 设置状态栏颜色
*/ */

Loading…
Cancel
Save