make theme depend on dark/night mode on android 10+

closes #3611
This commit is contained in:
Daniel Gultsch 2020-01-18 14:14:40 +01:00
parent e38a9cd729
commit 581eb5556f
7 changed files with 46 additions and 13 deletions

View file

@ -85,7 +85,7 @@ ext {
}
android {
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
minSdkVersion 16

View file

@ -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:

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="theme">automatic</string>
<string-array name="themes" tools:ignore="InconsistentArrays">
<item>@string/pref_theme_automatic</item>
<item>@string/pref_theme_light</item>
<item>@string/pref_theme_dark</item>
</string-array>
<string-array name="themes_values" tools:ignore="InconsistentArrays">
<item>automatic</item>
<item>light</item>
<item>dark</item>
</string-array>
</resources>

View file

@ -1,14 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="themes">
<item>@string/pref_theme_light</item>
<item>@string/pref_theme_dark</item>
</string-array>
<string-array name="themes_values">
<item>light</item>
<item>dark</item>
</string-array>
<string-array name="filesizes">
<item>@string/never</item>
<item>256 KiB</item>

View file

@ -17,7 +17,6 @@
<integer name="grace_period">144</integer>
<integer name="auto_accept_filesize">524288</integer>
<string name="picture_compression">auto</string>
<string name="theme">light</string>
<bool name="use_green_background">true</bool>
<string name="quick_action">recent</string>
<bool name="show_dynamic_tags">false</bool>

View file

@ -559,6 +559,7 @@
<string name="pref_privacy">Privacy</string>
<string name="pref_theme_options">Theme</string>
<string name="pref_theme_options_summary">Select the color palette</string>
<string name="pref_theme_automatic">Automatic</string>
<string name="pref_theme_light">Light theme</string>
<string name="pref_theme_dark">Dark theme</string>
<string name="pref_use_green_background">Green Background</string>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="theme">light</string>
<string-array name="themes">
<item>@string/pref_theme_light</item>
<item>@string/pref_theme_dark</item>
</string-array>
<string-array name="themes_values">
<item>light</item>
<item>dark</item>
</string-array>
</resources>