Hide dark theme on devices older than Q

Most likely, the don't support it (although theoratically, the -night
resource qualifier has been around for a while).

Change-Id: I54f736c2b06c71bb520727b0c834e687cad09237
This commit is contained in:
SpiritCroc 2020-09-06 15:20:34 +02:00
parent 35aedafc2f
commit 9dca2d433e
4 changed files with 37 additions and 16 deletions

View File

@ -52,8 +52,9 @@ class VectorSettingsPreferencesFragment @Inject constructor(
setUserInterfacePreferences()
// Themes
findPreference<VectorListPreference>(ThemeUtils.APPLICATION_THEME_KEY)!!
.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
val lightThemePref = findPreference<VectorListPreference>(ThemeUtils.APPLICATION_THEME_KEY)!!
val darkThemePref = findPreference<VectorListPreference>(ThemeUtils.APPLICATION_DARK_THEME_KEY)!!
lightThemePref.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue is String) {
ThemeUtils.setApplicationLightTheme(requireContext().applicationContext, newValue)
// Restart the Activity
@ -63,21 +64,27 @@ class VectorSettingsPreferencesFragment @Inject constructor(
false
}
}
findPreference<VectorListPreference>(ThemeUtils.APPLICATION_DARK_THEME_KEY)!!
.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue is String) {
ThemeUtils.setApplicationDarkTheme(requireContext().applicationContext, newValue)
// Restart the Activity
activity?.let {
// Note: recreate does not apply the color correctly
it.startActivity(it.intent)
it.finish()
if (ThemeUtils.darkThemePossible()) {
lightThemePref.title = getString(R.string.settings_light_theme)
darkThemePref.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue is String) {
ThemeUtils.setApplicationDarkTheme(requireContext().applicationContext, newValue)
// Restart the Activity
activity?.let {
// Note: recreate does not apply the color correctly
it.startActivity(it.intent)
it.finish()
}
true
} else {
false
}
true
} else {
false
}
} else {
lightThemePref.title = getString(R.string.settings_theme)
darkThemePref.parent?.removePreference(darkThemePref)
}
findPreference<VectorListPreference>(BubbleThemeUtils.BUBBLE_STYLE_KEY)!!
.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, _ ->
BubbleThemeUtils.invalidateBubbleStyle()

View File

@ -20,6 +20,7 @@ import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.TypedValue
import android.view.Menu
import androidx.annotation.AttrRes
@ -55,7 +56,18 @@ object ThemeUtils {
private var mUseDarkTheme = false
private var mThemeInitialized = false
/**
* @return Whether a system-wide dark mode is available on this device
*/
fun darkThemePossible(): Boolean {
// Available since Android 10: https://developer.android.com/guide/topics/ui/look-and-feel/darktheme
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
}
fun shouldUseDarkTheme(context: Context): Boolean {
if (!darkThemePossible()) {
return false
}
val currentNightMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return currentNightMode == Configuration.UI_MODE_NIGHT_YES
}

View File

@ -9,7 +9,8 @@
<string name="settings_room_unread_kind_content">Verstecke Teilnehmerveränderungen</string>
<string name="settings_room_unread_kind_original_content">Verstecke Teilnehmerveränderungen und Reaktionen</string>
<string name="settings_dark_theme">Nacht-Design</string>
<string name="settings_light_theme">Helles Design</string>
<string name="settings_dark_theme">Dunkles Design</string>
<string name="sc_light_theme">SC Hell</string>
<string name="sc_theme">SC Schwarz</string>
<string name="sc_dark_theme">SC Dunkel</string>

View File

@ -9,7 +9,8 @@
<string name="settings_room_unread_kind_content">Hide member changes</string>
<string name="settings_room_unread_kind_original_content">Hide member changes and reactions</string>
<string name="settings_dark_theme">Night Theme</string>
<string name="settings_light_theme">Light theme</string>
<string name="settings_dark_theme">Dark theme</string>
<string name="sc_light_theme">SC Light</string>
<string name="sc_theme">SC Black</string>
<string name="sc_dark_theme">SC Dark</string>