Follow system theme on Android 10+.

This commit is contained in:
Greyson Parrelli 2020-02-13 13:09:54 -05:00
parent 2e19d0459b
commit 4e7a92637c
13 changed files with 107 additions and 50 deletions

View File

@ -7,6 +7,7 @@ import androidx.preference.ListPreference;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import java.util.Arrays;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,7 +3,10 @@ package org.thoughtcrime.securesms.util;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AppCompatDelegate;
import org.thoughtcrime.securesms.R;
@ -12,6 +15,7 @@ public class DynamicTheme {
public static final String DARK = "dark";
public static final String LIGHT = "light";
public static final String SYSTEM = "system";
private int currentTheme;
@ -30,14 +34,36 @@ public class DynamicTheme {
}
}
protected int getSelectedTheme(Activity activity) {
private @StyleRes int getSelectedTheme(Activity activity) {
String theme = TextSecurePreferences.getTheme(activity);
if (theme.equals(DARK)) {
if (theme.equals(SYSTEM) && systemThemeAvailable()) {
if (isSystemInDarkTheme(activity)) {
return getDarkThemeStyle();
} else {
return getLightThemeStyle();
}
} else if (theme.equals(DARK)) {
return getDarkThemeStyle();
} else {
return getLightThemeStyle();
}
}
protected @StyleRes int getLightThemeStyle() {
return R.style.TextSecure_LightTheme;
}
protected @StyleRes int getDarkThemeStyle() {
return R.style.TextSecure_DarkTheme;
}
return R.style.TextSecure_LightTheme;
public static boolean systemThemeAvailable() {
return Build.VERSION.SDK_INT >= 29;
}
private static boolean isSystemInDarkTheme(@NonNull Activity activity) {
return (activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
private static final class OverridePendingTransition {

View File

@ -886,7 +886,7 @@ public class TextSecurePreferences {
}
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 isVerifying(Context context) {

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/preferences__system_theme</item>
<item>@string/preferences__light_theme</item>
<item>@string/preferences__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"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<string-array name="language_entries">
<!-- zz --><item>@string/preferences__default</item>
@ -143,12 +143,12 @@
<item>vi</item>
</string-array>
<string-array name="pref_theme_entries">
<string-array name="pref_theme_entries" tools:ignore="InconsistentArrays">
<item>@string/preferences__light_theme</item>
<item>@string/preferences__dark_theme</item>
</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>dark</item>
</string-array>

View File

@ -1471,7 +1471,7 @@
<string name="preferences__linked_devices">Linked devices</string>
<string name="preferences__light_theme">Light</string>
<string name="preferences__dark_theme">Dark</string>
<string name="preferences__system_theme">System</string>
<string name="preferences__system_theme">System default</string>
<string name="preferences__appearance">Appearance</string>
<string name="preferences__theme">Theme</string>
<string name="preferences__default">Default</string>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<org.thoughtcrime.securesms.preferences.widgets.SignalListPreference
android:key="pref_theme"
android:title="@string/preferences__theme"
android:entries="@array/pref_theme_entries"
android:entryValues="@array/pref_theme_values"
android:defaultValue="system">
</org.thoughtcrime.securesms.preferences.widgets.SignalListPreference>
<org.thoughtcrime.securesms.preferences.widgets.SignalListPreference
android:key="pref_language"
android:title="@string/preferences__language"
android:entries="@array/language_entries"
android:entryValues="@array/language_values"
android:defaultValue="zz"/>
</PreferenceScreen>