From 9dca2d433e4880c23bd6ef99b404a26aada89a83 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Sun, 6 Sep 2020 15:20:34 +0200 Subject: [PATCH] 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 --- .../VectorSettingsPreferencesFragment.kt | 35 +++++++++++-------- .../vector/app/features/themes/ThemeUtils.kt | 12 +++++++ vector/src/main/res/values-de/strings_sc.xml | 3 +- vector/src/main/res/values/strings_sc.xml | 3 +- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPreferencesFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPreferencesFragment.kt index ae2203948c..d0eecbe006 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPreferencesFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPreferencesFragment.kt @@ -52,8 +52,9 @@ class VectorSettingsPreferencesFragment @Inject constructor( setUserInterfacePreferences() // Themes - findPreference(ThemeUtils.APPLICATION_THEME_KEY)!! - .onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> + val lightThemePref = findPreference(ThemeUtils.APPLICATION_THEME_KEY)!! + val darkThemePref = findPreference(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(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(BubbleThemeUtils.BUBBLE_STYLE_KEY)!! .onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, _ -> BubbleThemeUtils.invalidateBubbleStyle() diff --git a/vector/src/main/java/im/vector/app/features/themes/ThemeUtils.kt b/vector/src/main/java/im/vector/app/features/themes/ThemeUtils.kt index f2f050df9d..25862ccaf6 100644 --- a/vector/src/main/java/im/vector/app/features/themes/ThemeUtils.kt +++ b/vector/src/main/java/im/vector/app/features/themes/ThemeUtils.kt @@ -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 } diff --git a/vector/src/main/res/values-de/strings_sc.xml b/vector/src/main/res/values-de/strings_sc.xml index 1ea8d47530..80a6981811 100644 --- a/vector/src/main/res/values-de/strings_sc.xml +++ b/vector/src/main/res/values-de/strings_sc.xml @@ -9,7 +9,8 @@ Verstecke Teilnehmerveränderungen Verstecke Teilnehmerveränderungen und Reaktionen - Nacht-Design + Helles Design + Dunkles Design SC Hell SC Schwarz SC Dunkel diff --git a/vector/src/main/res/values/strings_sc.xml b/vector/src/main/res/values/strings_sc.xml index 93e3b47052..99b6359bbd 100644 --- a/vector/src/main/res/values/strings_sc.xml +++ b/vector/src/main/res/values/strings_sc.xml @@ -9,7 +9,8 @@ Hide member changes Hide member changes and reactions - Night Theme + Light theme + Dark theme SC Light SC Black SC Dark