From 581eb5556fc7f7017f841efd253885e0a4b5f2df Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 18 Jan 2020 14:14:40 +0100 Subject: [PATCH] make theme depend on dark/night mode on android 10+ closes #3611 --- build.gradle | 2 +- .../siacs/conversations/utils/ThemeHelper.java | 17 ++++++++++++++--- src/main/res/values-v29/theme-settings.xml | 16 ++++++++++++++++ src/main/res/values/arrays.xml | 8 -------- src/main/res/values/defaults.xml | 1 - src/main/res/values/strings.xml | 1 + src/main/res/values/theme-settings.xml | 14 ++++++++++++++ 7 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 src/main/res/values-v29/theme-settings.xml create mode 100644 src/main/res/values/theme-settings.xml diff --git a/build.gradle b/build.gradle index 5b1443ad2..872ec5b32 100644 --- a/build.gradle +++ b/build.gradle @@ -85,7 +85,7 @@ ext { } android { - compileSdkVersion 28 + compileSdkVersion 29 defaultConfig { minSdkVersion 16 diff --git a/src/main/java/eu/siacs/conversations/utils/ThemeHelper.java b/src/main/java/eu/siacs/conversations/utils/ThemeHelper.java index 32feb61b0..bd50ab0ae 100644 --- a/src/main/java/eu/siacs/conversations/utils/ThemeHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/ThemeHelper.java @@ -31,8 +31,10 @@ package eu.siacs.conversations.utils; import android.content.Context; import android.content.SharedPreferences; +import android.content.res.Configuration; import android.content.res.Resources; import android.content.res.TypedArray; +import android.os.Build; import android.preference.PreferenceManager; import android.support.annotation.StyleRes; import android.support.design.widget.Snackbar; @@ -45,10 +47,10 @@ import eu.siacs.conversations.ui.SettingsActivity; public class ThemeHelper { - public static int find(Context context) { + public static int find(final Context context) { final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); final Resources resources = context.getResources(); - final boolean dark = sharedPreferences.getString(SettingsActivity.THEME, resources.getString(R.string.theme)).equals("dark"); + final boolean dark = isDark(sharedPreferences, resources); final String fontSize = sharedPreferences.getString("font_size", resources.getString(R.string.default_font_size)); switch (fontSize) { case "medium": @@ -63,7 +65,7 @@ public class ThemeHelper { public static int findDialog(Context context) { final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); final Resources resources = context.getResources(); - final boolean dark = sharedPreferences.getString(SettingsActivity.THEME, resources.getString(R.string.theme)).equals("dark"); + final boolean dark = isDark(sharedPreferences, resources); final String fontSize = sharedPreferences.getString("font_size", resources.getString(R.string.default_font_size)); switch (fontSize) { case "medium": @@ -75,6 +77,15 @@ public class ThemeHelper { } } + private static boolean isDark(final SharedPreferences sharedPreferences, final Resources resources) { + final String setting = sharedPreferences.getString(SettingsActivity.THEME, resources.getString(R.string.theme)); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && "automatic".equals(setting)) { + return (resources.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES; + } else { + return "dark".equals(setting); + } + } + public static boolean isDark(@StyleRes int id) { switch (id) { case R.style.ConversationsTheme_Dark: diff --git a/src/main/res/values-v29/theme-settings.xml b/src/main/res/values-v29/theme-settings.xml new file mode 100644 index 000000000..5cd93386f --- /dev/null +++ b/src/main/res/values-v29/theme-settings.xml @@ -0,0 +1,16 @@ + + + + automatic + + @string/pref_theme_automatic + @string/pref_theme_light + @string/pref_theme_dark + + + automatic + light + dark + + + diff --git a/src/main/res/values/arrays.xml b/src/main/res/values/arrays.xml index 50ca38c6e..64865db75 100644 --- a/src/main/res/values/arrays.xml +++ b/src/main/res/values/arrays.xml @@ -1,14 +1,6 @@ - - @string/pref_theme_light - @string/pref_theme_dark - - - light - dark - @string/never 256 KiB diff --git a/src/main/res/values/defaults.xml b/src/main/res/values/defaults.xml index bfe8ae953..3c1de4ebc 100644 --- a/src/main/res/values/defaults.xml +++ b/src/main/res/values/defaults.xml @@ -17,7 +17,6 @@ 144 524288 auto - light true recent false diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 03ecafea9..ccf8138ac 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -559,6 +559,7 @@ Privacy Theme Select the color palette + Automatic Light theme Dark theme Green Background diff --git a/src/main/res/values/theme-settings.xml b/src/main/res/values/theme-settings.xml new file mode 100644 index 000000000..b770b1b26 --- /dev/null +++ b/src/main/res/values/theme-settings.xml @@ -0,0 +1,14 @@ + + + + light + + @string/pref_theme_light + @string/pref_theme_dark + + + light + dark + + +