修复自动深浅色模式

This commit is contained in:
HapeLee
2025-06-18 13:41:35 +08:00
parent 372327b605
commit cd4caed170
9 changed files with 50 additions and 58 deletions
+2 -2
View File
@@ -114,7 +114,7 @@
android:name=".ui.main.MainActivity" android:name=".ui.main.MainActivity"
android:alwaysRetainTaskState="true" android:alwaysRetainTaskState="true"
android:enableOnBackInvokedCallback="true" android:enableOnBackInvokedCallback="true"
android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|uiMode" android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@@ -507,7 +507,7 @@
android:icon="@drawable/ic_web_service_noti" android:icon="@drawable/ic_web_service_noti"
android:label="legado Web Service" android:label="legado Web Service"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE" android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
tools:targetApi="24"> >
<intent-filter> <intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" /> <action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter> </intent-filter>
+8 -12
View File
@@ -123,18 +123,14 @@ class App : Application() {
} }
} }
override fun attachBaseContext(base: Context) { // override fun onConfigurationChanged(newConfig: Configuration) {
super.attachBaseContext(AppContextWrapper.wrap(base)) // super.onConfigurationChanged(newConfig)
} // val diff = newConfig.diff(oldConfig)
// if ((diff and ActivityInfo.CONFIG_UI_MODE) != 0) {
override fun onConfigurationChanged(newConfig: Configuration) { // applyDayNight(this)
super.onConfigurationChanged(newConfig) // }
val diff = newConfig.diff(oldConfig) // oldConfig = Configuration(newConfig)
if ((diff and ActivityInfo.CONFIG_UI_MODE) != 0) { // }
applyDayNight(this)
}
oldConfig = Configuration(newConfig)
}
/** /**
* 尝试在安装了GMS的设备上(GMS或者MicroG)使用GMS内置的Conscrypt * 尝试在安装了GMS的设备上(GMS或者MicroG)使用GMS内置的Conscrypt
@@ -1,6 +1,7 @@
package io.legato.kazusa.base package io.legato.kazusa.base
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context import android.content.Context
import android.content.res.Configuration import android.content.res.Configuration
import android.content.res.Resources import android.content.res.Resources
@@ -16,22 +17,31 @@ import java.util.*
@Suppress("unused") @Suppress("unused")
object AppContextWrapper { object AppContextWrapper {
@SuppressLint("ObsoleteSdkInt")
fun wrap(context: Context): Context { fun wrap(context: Context): Context {
val resources: Resources = context.resources val newConfig = Configuration(context.resources.configuration)
val configuration: Configuration = resources.configuration
val targetLocale = getSetLocale(context) val targetLocale = getSetLocale(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { newConfig.setLocale(targetLocale)
configuration.setLocale(targetLocale) newConfig.setLocales(LocaleList(targetLocale))
configuration.setLocales(LocaleList(targetLocale)) newConfig.fontScale = getFontScale(context)
} else { return context.createConfigurationContext(newConfig)
@Suppress("DEPRECATION")
configuration.locale = targetLocale
}
configuration.fontScale = getFontScale(context)
return context.createConfigurationContext(configuration)
} }
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 { fun getFontScale(context: Context): Float {
var fontScale = context.getPrefInt(PreferKey.fontScale) / 10f var fontScale = context.getPrefInt(PreferKey.fontScale) / 10f
if (fontScale !in 0.8f..1.6f) { if (fontScale !in 0.8f..1.6f) {
@@ -7,6 +7,7 @@ import android.graphics.drawable.BitmapDrawable
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.MotionEvent import android.view.MotionEvent
@@ -41,7 +42,6 @@ import androidx.core.graphics.drawable.toDrawable
abstract class BaseActivity<VB : ViewBinding>( abstract class BaseActivity<VB : ViewBinding>(
val fullScreen: Boolean = true, val fullScreen: Boolean = true,
private val theme: Theme = Theme.Auto,
private val toolBarTheme: Theme = Theme.Auto, private val toolBarTheme: Theme = Theme.Auto,
private val transparent: Boolean = false, private val transparent: Boolean = false,
private val imageBg: Boolean = true 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( override fun onCreateView(
parent: View?, parent: View?,
@@ -69,18 +66,19 @@ abstract class BaseActivity<VB : ViewBinding>(
context: Context, context: Context,
attrs: AttributeSet attrs: AttributeSet
): View? { ): View? {
if (AppConst.menuViewNames.contains(name) && parent?.parent is FrameLayout) { // if (AppConst.menuViewNames.contains(name) && parent?.parent is FrameLayout) {
//(parent.parent as View).setBackgroundColor(backgroundColor) // (parent.parent as View).setBackgroundColor(backgroundColor)
} // }
return super.onCreateView(parent, name, context, attrs) return super.onCreateView(parent, name, context, attrs)
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
applyTheme() initTheme()
window.decorView.disableAutoFill() window.decorView.disableAutoFill()
AppContextWrapper.applyLocaleAndFont(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
//setupSystemBar() //setupSystemBar()
setContentView(binding.root) setContentView(binding.root)
upBackgroundImage() upBackgroundImage()
@@ -100,12 +98,13 @@ abstract class BaseActivity<VB : ViewBinding>(
//setupSystemBar() //setupSystemBar()
} }
override fun onConfigurationChanged(newConfig: Configuration) { // override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig) // super.onConfigurationChanged(newConfig)
//findViewById<TitleBar>(R.id.title_bar) // //findViewById<TitleBar>(R.id.title_bar)
//?.onMultiWindowModeChanged(isInMultiWindow, fullScreen) // //Log.d("Config", "uiMode = ${newConfig.uiMode}")
//setupSystemBar() // //?.onMultiWindowModeChanged(isInMultiWindow, fullScreen)
} // //setupSystemBar()
// }
abstract fun onActivityCreated(savedInstanceState: Bundle?) abstract fun onActivityCreated(savedInstanceState: Bundle?)
@@ -132,7 +131,7 @@ abstract class BaseActivity<VB : ViewBinding>(
open fun onCompatOptionsItemSelected(item: MenuItem) = super.onOptionsItemSelected(item) open fun onCompatOptionsItemSelected(item: MenuItem) = super.onOptionsItemSelected(item)
open fun applyTheme() { open fun initTheme() {
when (getPrefString("app_theme", "0")) { when (getPrefString("app_theme", "0")) {
"0" -> "0" ->
{ {
@@ -10,7 +10,7 @@ abstract class VMBaseActivity<VB : ViewBinding, VM : ViewModel>(
toolBarTheme: Theme = Theme.Auto, toolBarTheme: Theme = Theme.Auto,
transparent: Boolean = false, transparent: Boolean = false,
imageBg: Boolean = true imageBg: Boolean = true
) : BaseActivity<VB>(fullScreen, theme, toolBarTheme, transparent, imageBg) { ) : BaseActivity<VB>(fullScreen, toolBarTheme, transparent, imageBg) {
protected abstract val viewModel: VM protected abstract val viewModel: VM
@@ -1,5 +1,5 @@
package io.legato.kazusa.constant package io.legato.kazusa.constant
enum class Theme { enum class Theme {
Dark, Light, Auto, Transparent, EInk, GR; Dark, Light, Auto;
} }
@@ -57,7 +57,6 @@ object ThemeConfig {
} }
fun applyDayNightInit(context: Context) { fun applyDayNightInit(context: Context) {
//applyTheme(context)
initNightMode() initNightMode()
} }
@@ -342,12 +342,10 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
} }
override fun observeLiveBus() { override fun observeLiveBus() {
super.observeLiveBus()
viewModel.onUpBooksLiveData.observe(this) { viewModel.onUpBooksLiveData.observe(this) {
onUpBooksBadgeView.setBadgeCount(it) onUpBooksBadgeView.setBadgeCount(it)
} }
observeEvent<String>(EventBus.RECREATE) {
recreate()
}
observeEvent<Boolean>(EventBus.NOTIFY_MAIN) { observeEvent<Boolean>(EventBus.NOTIFY_MAIN) {
binding.apply { binding.apply {
upBottomMenu() upBottomMenu()
-10
View File
@@ -2,16 +2,6 @@
//**************************************************************Theme******************************************************************************// //**************************************************************Theme******************************************************************************//
<!-- &lt;!&ndash; 亮色主题 &ndash;&gt;-->
<!-- <style name="AppTheme.Light" parent="Base.AppTheme">-->
<!-- <item name="android:windowLightStatusBar">true</item>-->
<!-- </style>-->
<!-- &lt;!&ndash; 暗色主题 &ndash;&gt;-->
<!-- <style name="AppTheme.Dark" parent="Base.AppTheme">-->
<!-- <item name="android:windowLightStatusBar">false</item>-->
<!-- </style>-->
<!-- 透明主题 --> <!-- 透明主题 -->
<style name="AppTheme.Transparent" parent="Base.AppTheme"> <style name="AppTheme.Transparent" parent="Base.AppTheme">
<item name="android:windowIsTranslucent">true</item> <item name="android:windowIsTranslucent">true</item>