修复自动深浅色模式

This commit is contained in:
HapeLee committed 2025-06-18 13:41:35 +08:00
1 parent 372327b605
commit cd4caed170
9 files changed
+50 -58

No files matched your search

@@ -1,6 +1,7 @@
package io.legato.kazusa.base
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
@@ -16,22 +17,31 @@ import java.util.*
@Suppress("unused")
object AppContextWrapper {
@SuppressLint("ObsoleteSdkInt")
fun wrap(context: Context): Context {
val resources: Resources = context.resources
val configuration: Configuration = resources.configuration
val newConfig = Configuration(context.resources.configuration)
val targetLocale = getSetLocale(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(targetLocale)
configuration.setLocales(LocaleList(targetLocale))
} else {
@Suppress("DEPRECATION")
configuration.locale = targetLocale
}
configuration.fontScale = getFontScale(context)
return context.createConfigurationContext(configuration)
newConfig.setLocale(targetLocale)
newConfig.setLocales(LocaleList(targetLocale))
newConfig.fontScale = getFontScale(context)
return context.createConfigurationContext(newConfig)
}
fun applyLocaleAndFont(activity: Activity) {
val config = activity.resources.configuration
val locale = getSetLocale(activity)
val fontScale = getFontScale(activity)
val newConfig = Configuration(config)
newConfig.setLocale(locale)
newConfig.setLocales(LocaleList(locale))
newConfig.fontScale = fontScale
@Suppress("DEPRECATION")
activity.resources.updateConfiguration(newConfig, activity.resources.displayMetrics)
}
fun getFontScale(context: Context): Float {
var fontScale = context.getPrefInt(PreferKey.fontScale) / 10f
if (fontScale !in 0.8f..1.6f) {
@@ -7,6 +7,7 @@ import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
@@ -41,7 +42,6 @@ import androidx.core.graphics.drawable.toDrawable
abstract class BaseActivity<VB : ViewBinding>(
val fullScreen: Boolean = true,
private val theme: Theme = Theme.Auto,
private val toolBarTheme: Theme = Theme.Auto,
private val transparent: Boolean = false,
private val imageBg: Boolean = true
@@ -59,9 +59,6 @@ abstract class BaseActivity<VB : ViewBinding>(
}
}
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(AppContextWrapper.wrap(newBase))
}
override fun onCreateView(
parent: View?,
@@ -69,18 +66,19 @@ abstract class BaseActivity<VB : ViewBinding>(
context: Context,
attrs: AttributeSet
): View? {
if (AppConst.menuViewNames.contains(name) && parent?.parent is FrameLayout) {
//(parent.parent as View).setBackgroundColor(backgroundColor)
}
// if (AppConst.menuViewNames.contains(name) && parent?.parent is FrameLayout) {
// (parent.parent as View).setBackgroundColor(backgroundColor)
// }
return super.onCreateView(parent, name, context, attrs)
}
override fun onCreate(savedInstanceState: Bundle?) {
applyTheme()
initTheme()
window.decorView.disableAutoFill()
AppContextWrapper.applyLocaleAndFont(this)
super.onCreate(savedInstanceState)
//setupSystemBar()
setContentView(binding.root)
upBackgroundImage()
@@ -100,12 +98,13 @@ abstract class BaseActivity<VB : ViewBinding>(
//setupSystemBar()
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
//findViewById<TitleBar>(R.id.title_bar)
//?.onMultiWindowModeChanged(isInMultiWindow, fullScreen)
//setupSystemBar()
}
// override fun onConfigurationChanged(newConfig: Configuration) {
// super.onConfigurationChanged(newConfig)
// //findViewById<TitleBar>(R.id.title_bar)
// //Log.d("Config", "uiMode = ${newConfig.uiMode}")
// //?.onMultiWindowModeChanged(isInMultiWindow, fullScreen)
// //setupSystemBar()
// }
abstract fun onActivityCreated(savedInstanceState: Bundle?)
@@ -132,7 +131,7 @@ abstract class BaseActivity<VB : ViewBinding>(
open fun onCompatOptionsItemSelected(item: MenuItem) = super.onOptionsItemSelected(item)
open fun applyTheme() {
open fun initTheme() {
when (getPrefString("app_theme", "0")) {
"0" ->
{
@@ -10,7 +10,7 @@ abstract class VMBaseActivity<VB : ViewBinding, VM : ViewModel>(
toolBarTheme: Theme = Theme.Auto,
transparent: Boolean = false,
imageBg: Boolean = true
) : BaseActivity<VB>(fullScreen, theme, toolBarTheme, transparent, imageBg) {
) : BaseActivity<VB>(fullScreen, toolBarTheme, transparent, imageBg) {
protected abstract val viewModel: VM