HapeLee 1 год назад
Родитель
Сommit
cd4caed170

+ 2 - 2
app/src/main/AndroidManifest.xml

@@ -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
app/src/main/java/io/legato/kazusa/App.kt

@@ -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

+ 22 - 12
app/src/main/java/io/legato/kazusa/base/AppContextWrapper.kt

@@ -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) {

+ 15 - 16
app/src/main/java/io/legato/kazusa/base/BaseActivity.kt

@@ -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" ->
             {

+ 1 - 1
app/src/main/java/io/legato/kazusa/base/VMBaseActivity.kt

@@ -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 - 1
app/src/main/java/io/legato/kazusa/constant/Theme.kt

@@ -1,5 +1,5 @@
 package io.legato.kazusa.constant
 
 enum class Theme {
-    Dark, Light, Auto, Transparent, EInk, GR;
+    Dark, Light, Auto;
 }

+ 0 - 1
app/src/main/java/io/legato/kazusa/help/config/ThemeConfig.kt

@@ -57,7 +57,6 @@ object ThemeConfig {
     }
 
     fun applyDayNightInit(context: Context) {
-        //applyTheme(context)
         initNightMode()
     }
 

+ 1 - 3
app/src/main/java/io/legato/kazusa/ui/main/MainActivity.kt

@@ -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()

+ 0 - 10
app/src/main/res/values/themes.xml

@@ -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>