修复自动深浅色模式

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:alwaysRetainTaskState="true"
android:enableOnBackInvokedCallback="true"
android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|uiMode"
android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@@ -507,7 +507,7 @@
android:icon="@drawable/ic_web_service_noti"
android:label="legado Web Service"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
tools:targetApi="24">
>
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
+8 -12
View File
@@ -123,18 +123,14 @@ class App : Application() {
}
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(AppContextWrapper.wrap(base))
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val diff = newConfig.diff(oldConfig)
if ((diff and ActivityInfo.CONFIG_UI_MODE) != 0) {
applyDayNight(this)
}
oldConfig = Configuration(newConfig)
}
// override fun onConfigurationChanged(newConfig: Configuration) {
// super.onConfigurationChanged(newConfig)
// val diff = newConfig.diff(oldConfig)
// if ((diff and ActivityInfo.CONFIG_UI_MODE) != 0) {
// applyDayNight(this)
// }
// oldConfig = Configuration(newConfig)
// }
/**
* 尝试在安装了GMS的设备上(GMS或者MicroG)使用GMS内置的Conscrypt
@@ -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
@@ -1,5 +1,5 @@
package io.legato.kazusa.constant
enum class Theme {
Dark, Light, Auto, Transparent, EInk, GR;
Dark, Light, Auto;
}
@@ -57,7 +57,6 @@ object ThemeConfig {
}
fun applyDayNightInit(context: Context) {
//applyTheme(context)
initNightMode()
}
@@ -342,12 +342,10 @@ class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
}
override fun observeLiveBus() {
super.observeLiveBus()
viewModel.onUpBooksLiveData.observe(this) {
onUpBooksBadgeView.setBadgeCount(it)
}
observeEvent<String>(EventBus.RECREATE) {
recreate()
}
observeEvent<Boolean>(EventBus.NOTIFY_MAIN) {
binding.apply {
upBottomMenu()
-10
View File
@@ -2,16 +2,6 @@
//**************************************************************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">
<item name="android:windowIsTranslucent">true</item>