From 3e78098c43368bc26b9ad7a735f0f735e6dfeb0e Mon Sep 17 00:00:00 2001 From: Tigermouthbear Date: Mon, 28 Dec 2020 23:32:06 -0500 Subject: [PATCH] Add System theme option and set as default; closes #904, closes #2387 --- CHANGES.md | 2 +- .../vector/app/features/themes/ThemeUtils.kt | 26 +++++++++++++------ vector/src/main/res/values/array.xml | 2 ++ vector/src/main/res/values/strings.xml | 1 + .../res/xml/vector_settings_preferences.xml | 2 +- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a028ef6f1e..5ab9ff733d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,7 +5,7 @@ Features ✨: - Enable url previews for notices (#2562) Improvements 🙌: - - + - Add System theme option and set as default (#904) (#2387) Bugfix 🐛: - Url previews sometimes attached to wrong message (#2561) 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 bba6b9c253..172303cff7 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 @@ -18,6 +18,8 @@ package im.vector.app.features.themes import android.app.Activity import android.content.Context +import android.content.res.Configuration +import android.content.res.Resources import android.graphics.drawable.Drawable import android.util.TypedValue import android.view.Menu @@ -39,6 +41,7 @@ object ThemeUtils { const val APPLICATION_THEME_KEY = "APPLICATION_THEME_KEY" // the theme possible values + private const val SYSTEM_THEME_VALUE = "system" private const val THEME_DARK_VALUE = "dark" private const val THEME_LIGHT_VALUE = "light" private const val THEME_BLACK_VALUE = "black" @@ -54,13 +57,11 @@ object ThemeUtils { } /** - * @return true if current theme is Light or Status + * @return true if current theme is Light/Status or current theme is System and system theme is light */ fun isLightTheme(context: Context): Boolean { - return when (getApplicationTheme(context)) { - THEME_LIGHT_VALUE -> true - else -> false - } + val theme = getApplicationTheme(context) + return theme == THEME_LIGHT_VALUE || (theme == SYSTEM_THEME_VALUE && !isSystemDarkTheme(context.resources)) } /** @@ -73,11 +74,11 @@ object ThemeUtils { val currentTheme = this.currentTheme.get() return if (currentTheme == null) { val prefs = DefaultSharedPreferences.getInstance(context) - var themeFromPref = prefs.getString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE) ?: THEME_LIGHT_VALUE + var themeFromPref = prefs.getString(APPLICATION_THEME_KEY, SYSTEM_THEME_VALUE) ?: SYSTEM_THEME_VALUE if (themeFromPref == "status") { // Migrate to light theme, which is the closest theme - themeFromPref = THEME_LIGHT_VALUE - prefs.edit { putString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE) } + themeFromPref = SYSTEM_THEME_VALUE + prefs.edit { putString(APPLICATION_THEME_KEY, SYSTEM_THEME_VALUE) } } this.currentTheme.set(themeFromPref) themeFromPref @@ -86,6 +87,13 @@ object ThemeUtils { } } + /** + * @return true if system theme is dark + */ + private fun isSystemDarkTheme(resources: Resources): Boolean { + return resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES + } + /** * Update the application theme * @@ -94,6 +102,7 @@ object ThemeUtils { fun setApplicationTheme(context: Context, aTheme: String) { currentTheme.set(aTheme) when (aTheme) { + SYSTEM_THEME_VALUE -> context.setTheme(if(isSystemDarkTheme(context.resources)) R.style.AppTheme_Dark else R.style.AppTheme_Light) THEME_DARK_VALUE -> context.setTheme(R.style.AppTheme_Dark) THEME_BLACK_VALUE -> context.setTheme(R.style.AppTheme_Black) else -> context.setTheme(R.style.AppTheme_Light) @@ -110,6 +119,7 @@ object ThemeUtils { */ fun setActivityTheme(activity: Activity, otherThemes: ActivityOtherThemes) { when (getApplicationTheme(activity)) { + SYSTEM_THEME_VALUE -> if(isSystemDarkTheme(activity.resources)) activity.setTheme(otherThemes.dark) THEME_DARK_VALUE -> activity.setTheme(otherThemes.dark) THEME_BLACK_VALUE -> activity.setTheme(otherThemes.black) } diff --git a/vector/src/main/res/values/array.xml b/vector/src/main/res/values/array.xml index e56844e2e8..2c4bc135e5 100644 --- a/vector/src/main/res/values/array.xml +++ b/vector/src/main/res/values/array.xml @@ -92,12 +92,14 @@ + @string/system_theme @string/light_theme @string/dark_theme @string/black_them + system light dark black diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index 76541460d2..355ac4d6d6 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -8,6 +8,7 @@ Latn + System Default Light Theme Dark Theme Black Theme diff --git a/vector/src/main/res/xml/vector_settings_preferences.xml b/vector/src/main/res/xml/vector_settings_preferences.xml index 74b1f882ee..6297b89e6c 100644 --- a/vector/src/main/res/xml/vector_settings_preferences.xml +++ b/vector/src/main/res/xml/vector_settings_preferences.xml @@ -13,7 +13,7 @@ app:fragment="im.vector.app.features.settings.locale.LocalePickerFragment" />