Преглед изворни кода

fix: 修复颜色字符串非法时 Color.parseColor 崩溃的问题

ensureColorInts 和 ensureAccentColorInts 中对 toColorInt() 加 try-catch 保护,
解析失败时回退到字段默认值,避免数据库中存储损坏颜色字符串导致崩溃
HapeLee пре 4 месеци
родитељ
комит
502f7fbe12
1 измењених фајлова са 18 додато и 14 уклоњено
  1. 18 14
      app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt

+ 18 - 14
app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt

@@ -1141,21 +1141,25 @@ object ReadBookConfig {
             }
         }
 
+        private fun String.toColorIntSafe(fallback: Int): Int {
+            return runCatching { toColorInt() }.getOrDefault(fallback)
+        }
+
         private fun ensureColorInts() {
             if (initColorInt) {
                 return
             }
-            textColorIntEInk = textColorEInk.toColorInt()
-            textColorIntNight = textColorNight.toColorInt()
-            textColorInt = textColor.toColorInt()
-            shadowColorNightInt = shadowColorN.toColorInt()
-            shadowColorInt = shadowColor.toColorInt()
-            menuBgColorInt = menuBgColor.toColorInt()
-            menuBgColorNightInt = menuBgColorNight.toColorInt()
-            menuAcColorInt = menuAcColor.toColorInt()
-            menuAcColorNightInt = menuAcColorNight.toColorInt()
-            underlineColorInt = underlineColor.toColorInt()
-            underlineColorNightInt = underlineColorNight.toColorInt()
+            textColorIntEInk = textColorEInk.toColorIntSafe(0xFF000000.toInt())
+            textColorIntNight = textColorNight.toColorIntSafe(0xFFADADAD.toInt())
+            textColorInt = textColor.toColorIntSafe(0xFF3E3D3B.toInt())
+            shadowColorNightInt = shadowColorN.toColorIntSafe(0xFF3E3D3B.toInt())
+            shadowColorInt = shadowColor.toColorIntSafe(0xFF3E3D3B.toInt())
+            menuBgColorInt = menuBgColor.toColorIntSafe(-1)
+            menuBgColorNightInt = menuBgColorNight.toColorIntSafe(-1)
+            menuAcColorInt = menuAcColor.toColorIntSafe(-1)
+            menuAcColorNightInt = menuAcColorNight.toColorIntSafe(-1)
+            underlineColorInt = underlineColor.toColorIntSafe(0xFF3E3D3B.toInt())
+            underlineColorNightInt = underlineColorNight.toColorIntSafe(0xFFADADAD.toInt())
             initColorInt = true
         }
 
@@ -1163,9 +1167,9 @@ object ReadBookConfig {
             if (initAccentColorInt) {
                 return
             }
-            textAccentColorIntEInk = textAccentColorEInk.toColorInt()
-            textAccentColorIntNight = textAccentColorNight.toColorInt()
-            textAccentColorInt = textAccentColor.toColorInt()
+            textAccentColorIntEInk = textAccentColorEInk.toColorIntSafe(0xFF000000.toInt())
+            textAccentColorIntNight = textAccentColorNight.toColorIntSafe(0xFFFE4D55.toInt())
+            textAccentColorInt = textAccentColor.toColorIntSafe(0xFF834E00.toInt())
             initAccentColorInt = true
         }