Dark theme / DayNight: Add option to follow system

fix maybe #1748. Maybe not, because actually I don't understand why
issue #1748 even happened because we don't use the system background for
the bubble but an own one (see conversation_item_sent.xml:50). Probably
the system is somehow trying to forcefully create a dark mode.

So, let's see whether this PR fixes the issue, if not, I'll try again.
This commit is contained in:
Hocuri 2020-12-19 19:16:18 +01:00
parent cfea049418
commit 1ff6a3a18d
No known key found for this signature in database
GPG key ID: 0D45AD9007D18DCD
7 changed files with 115 additions and 19 deletions

16
res/values-v29/arrays.xml Normal file
View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string-array name="pref_theme_entries" tools:ignore="InconsistentArrays">
<item>@string/pref_system_default</item>
<item>@string/pref_light_theme</item>
<item>@string/pref_dark_theme</item>
</string-array>
<string-array name="pref_theme_values" translatable="false" tools:ignore="InconsistentArrays">
<item>system</item>
<item>light</item>
<item>dark</item>
</string-array>
</resources>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources xmlns:tools="http://schemas.android.com/tools">
<!-- take care, language_entries and languages_values are in sync ... <!-- take care, language_entries and languages_values are in sync ...
... some one has to clean up this mess some day ... --> ... some one has to clean up this mess some day ... -->
@ -84,12 +84,12 @@
<item>te</item> <item>te</item>
</string-array> </string-array>
<string-array name="pref_theme_entries"> <string-array name="pref_theme_entries" tools:ignore="InconsistentArrays">
<item>@string/pref_light_theme</item> <item>@string/pref_light_theme</item>
<item>@string/pref_dark_theme</item> <item>@string/pref_dark_theme</item>
</string-array> </string-array>
<string-array name="pref_theme_values" translatable="false"> <string-array name="pref_theme_values" translatable="false" tools:ignore="InconsistentArrays">
<item>light</item> <item>light</item>
<item>dark</item> <item>dark</item>
</string-array> </string-array>

View file

@ -457,6 +457,7 @@
<string name="pref_silent">Silent</string> <string name="pref_silent">Silent</string>
<string name="pref_privacy">Privacy</string> <string name="pref_privacy">Privacy</string>
<string name="pref_chats_and_media">Chats and media</string> <string name="pref_chats_and_media">Chats and media</string>
<string name="pref_system_default">System default</string>
<!-- Translators: "light" in the meaning "opposite of dark" --> <!-- Translators: "light" in the meaning "opposite of dark" -->
<string name="pref_light_theme">Light</string> <string name="pref_light_theme">Light</string>
<string name="pref_dark_theme">Dark</string> <string name="pref_dark_theme">Dark</string>

View file

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="pref_theme"
android:title="@string/pref_theme"
android:entries="@array/pref_theme_entries"
android:entryValues="@array/pref_theme_values"
android:defaultValue="system"/>
<Preference
android:key="pref_chat_background"
android:title="@string/pref_background"/>
<ListPreference
android:key="pref_message_body_text_size"
android:title="@string/pref_message_text_size"
android:entries="@array/pref_message_font_size_entries"
android:entryValues="@array/pref_message_font_size_values"
android:defaultValue="16"/>
<org.thoughtcrime.securesms.components.SwitchPreferenceCompat
android:defaultValue="false"
android:key="pref_system_emoji"
android:title="@string/pref_use_system_emoji"
android:summary="@string/pref_use_system_emoji_explain" />
<ListPreference
android:key="pref_language"
android:title="@string/pref_language"
android:entries="@array/language_entries"
android:entryValues="@array/language_values"
android:defaultValue="zz"/>
</PreferenceScreen>

View file

@ -1,16 +1,15 @@
package org.thoughtcrime.securesms.util; package org.thoughtcrime.securesms.util;
import android.app.Activity; import androidx.annotation.StyleRes;
import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.R;
public class DynamicNoActionBarTheme extends DynamicTheme { public class DynamicNoActionBarTheme extends DynamicTheme {
@Override protected @StyleRes int getLightThemeStyle() {
protected int getSelectedTheme(Activity activity) {
String theme = Prefs.getTheme(activity);
if (theme.equals("dark")) return R.style.TextSecure_DarkNoActionBar;
return R.style.TextSecure_LightNoActionBar; return R.style.TextSecure_LightNoActionBar;
} }
protected @StyleRes int getDarkThemeStyle() {
return R.style.TextSecure_DarkNoActionBar;
}
} }

View file

@ -3,19 +3,37 @@ package org.thoughtcrime.securesms.util;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.R;
public class DynamicTheme { public class DynamicTheme {
public static final String DARK = "dark"; public static final String DARK = "dark";
public static final String LIGHT = "light"; public static final String LIGHT = "light";
public static final String SYSTEM = "system";
//private static boolean isDarkTheme;
private int currentTheme; private int currentTheme;
public void onCreate(Activity activity) { public void onCreate(Activity activity) {
//boolean wasDarkTheme = isDarkTheme;
currentTheme = getSelectedTheme(activity); currentTheme = getSelectedTheme(activity);
//isDarkTheme = isDarkTheme(activity);
activity.setTheme(currentTheme); activity.setTheme(currentTheme);
// In case you introduce a CachedInflater and there are problems with the dark mode, uncomment
// this line and the line in onResume():
//if (isDarkTheme != wasDarkTheme) {
//CachedInflater.from(activity).clear();
//}
} }
public void onResume(Activity activity) { public void onResume(Activity activity) {
@ -25,19 +43,45 @@ public class DynamicTheme {
OverridePendingTransition.invoke(activity); OverridePendingTransition.invoke(activity);
activity.startActivity(intent); activity.startActivity(intent);
OverridePendingTransition.invoke(activity); OverridePendingTransition.invoke(activity);
//CachedInflater.from(activity).clear();
} }
} }
protected int getSelectedTheme(Activity activity) { private @StyleRes int getSelectedTheme(Activity activity) {
String theme = Prefs.getTheme(activity); if (isDarkTheme(activity)) {
return getDarkThemeStyle();
if (theme.equals(DARK)) return R.style.TextSecure_DarkTheme; } else {
return getLightThemeStyle();
}
}
protected @StyleRes int getLightThemeStyle() {
return R.style.TextSecure_LightTheme; return R.style.TextSecure_LightTheme;
} }
public static boolean isDarkTheme(Context context) { protected @StyleRes int getDarkThemeStyle() {
return Prefs.getTheme(context).equals(DARK); return R.style.TextSecure_DarkTheme;
}
static boolean systemThemeAvailable() {
return Build.VERSION.SDK_INT >= 29;
}
/**
* Takes the system theme into account.
*/
public static boolean isDarkTheme(@NonNull Context context) {
String theme = Prefs.getTheme(context);
if (theme.equals(SYSTEM) && systemThemeAvailable()) {
return isSystemInDarkTheme(context);
} else {
return theme.equals(DARK);
}
}
private static boolean isSystemInDarkTheme(@NonNull Context context) {
return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
} }
private static final class OverridePendingTransition { private static final class OverridePendingTransition {

View file

@ -138,7 +138,7 @@ public class Prefs {
} }
public static String getTheme(Context context) { public static String getTheme(Context context) {
return getStringPreference(context, THEME_PREF, "light"); return getStringPreference(context, THEME_PREF, DynamicTheme.systemThemeAvailable() ? DynamicTheme.SYSTEM : DynamicTheme.LIGHT);
} }
public static boolean isScreenLockTimeoutEnabled(Context context) { public static boolean isScreenLockTimeoutEnabled(Context context) {