@@ -17,9 +17,13 @@ import android.app.Activity
17
17
import android.content.Context
18
18
import android.content.Intent
19
19
import android.content.SharedPreferences
20
+ import android.content.res.Configuration
21
+ import android.os.Build
22
+ import android.os.PowerManager
20
23
import android.widget.Toast
21
24
import androidx.preference.PreferenceManager
22
25
import com.github.odaridavid.designpatterns.R
26
+ import com.github.odaridavid.designpatterns.helpers.SdkUtils.versionUntil
23
27
import us.feras.mdv.MarkdownView
24
28
25
29
@@ -43,11 +47,33 @@ internal fun getThemedCss(context: Context): String {
43
47
return when (getCurrentTheme(context, sp)) {
44
48
ThemeUtils .THEME_DARK -> DARK_KOTLIN_CSS_PATH
45
49
ThemeUtils .THEME_LIGHT -> LIGHT_KOTLIN_CSS_PATH
46
- ThemeUtils .THEME_SYSTEM -> LIGHT_KOTLIN_CSS_PATH
50
+ ThemeUtils .THEME_SYSTEM -> {
51
+ if (versionUntil(Build .VERSION_CODES .P )) {
52
+ val powerManager = context.getSystemService(Context .POWER_SERVICE ) as PowerManager
53
+ onPowerSaverModeChange(powerManager)
54
+ } else {
55
+ onUiModeConfigChange(context)
56
+ }
57
+ }
47
58
else -> LIGHT_KOTLIN_CSS_PATH
48
59
}
49
60
}
50
61
62
+ private fun onUiModeConfigChange (context : Context ): String {
63
+ return when (context.resources.configuration.uiMode and Configuration .UI_MODE_NIGHT_MASK ) {
64
+ Configuration .UI_MODE_NIGHT_NO -> LIGHT_KOTLIN_CSS_PATH
65
+ Configuration .UI_MODE_NIGHT_YES -> DARK_KOTLIN_CSS_PATH
66
+ else -> LIGHT_KOTLIN_CSS_PATH
67
+ }
68
+ }
69
+
70
+ private fun onPowerSaverModeChange (powerManager : PowerManager ): String {
71
+ return if (powerManager.isPowerSaveMode)
72
+ DARK_KOTLIN_CSS_PATH
73
+ else
74
+ LIGHT_KOTLIN_CSS_PATH
75
+ }
76
+
51
77
internal fun getCurrentTheme (context : Context , sp : SharedPreferences ? ): String {
52
78
return sp?.getString(context.getString(R .string.key_theme_preference), ThemeUtils .THEME_LIGHT )
53
79
? : " default"
0 commit comments