Skip to content

Commit

Permalink
Follow system theme on Android 10+.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Feb 14, 2020
1 parent 2e19d04 commit 4e7a926
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
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;

public class DynamicTheme {

public static final String DARK = "dark";
public static final String LIGHT = "light";
public static final String DARK = "dark";
public static final String LIGHT = "light";
public static final String SYSTEM = "system";

private int currentTheme;

Expand All @@ -30,16 +34,38 @@ public void onResume(Activity activity) {
}
}

protected int getSelectedTheme(Activity activity) {
private @StyleRes int getSelectedTheme(Activity activity) {
String theme = TextSecurePreferences.getTheme(activity);

if (theme.equals(DARK)) {
return R.style.TextSecure_DarkTheme;
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;
}

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 {
static void invoke(Activity activity) {
activity.overridePendingTransition(0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ public static void setExperienceDismissedVersionCode(Context context, int versio
}

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) {
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/values-v29/arrays.xml
Original file line number Diff line number Diff line change
@@ -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>
6 changes: 3 additions & 3 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/xml-v29/preferences_appearance.xml
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 4e7a926

Please sign in to comment.