Merge branch 'auto_theme'

This commit is contained in:
Vincent Breitmoser
2022-12-27 22:27:00 +01:00
7 changed files with 43 additions and 6 deletions

View File

@@ -147,6 +147,7 @@ public final class Constants {
public static final String KEY_SIGNATURES_TABLE_INITIALIZED = "key_signatures_table_initialized"; public static final String KEY_SIGNATURES_TABLE_INITIALIZED = "key_signatures_table_initialized";
public static final class Theme { public static final class Theme {
public static final String AUTO = "auto";
public static final String LIGHT = "light"; public static final String LIGHT = "light";
public static final String DARK = "dark"; public static final String DARK = "dark";
public static final String DEFAULT = Constants.Pref.Theme.LIGHT; public static final String DEFAULT = Constants.Pref.Theme.LIGHT;

View File

@@ -19,6 +19,7 @@ package org.sufficientlysecure.keychain.ui.util;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
@@ -33,13 +34,35 @@ public class ThemeChanger {
private int mLightResId; private int mLightResId;
private int mDarkResId; private int mDarkResId;
static private boolean isDark(final Preferences preferences, final Configuration config) {
final String preferedTheme = preferences.getTheme();
switch (preferedTheme) {
case Constants.Pref.Theme.AUTO:
final int currentNightMode = config.uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
return false;
case Configuration.UI_MODE_NIGHT_YES:
return true;
default:
throw new IllegalStateException("Unknown uiMode");
}
case Constants.Pref.Theme.LIGHT:
return false;
case Constants.Pref.Theme.DARK:
return true;
default:
throw new IllegalStateException("Unknown theme");
}
}
static public ContextThemeWrapper getDialogThemeWrapper(Context context) { static public ContextThemeWrapper getDialogThemeWrapper(Context context) {
Preferences preferences = Preferences.getPreferences(context); Preferences preferences = Preferences.getPreferences(context);
// if the dialog is displayed from the application class, design is missing. // if the dialog is displayed from the application class, design is missing.
// hack to get holo design (which is not automatically applied due to activity's // hack to get holo design (which is not automatically applied due to activity's
// Theme.NoDisplay) // Theme.NoDisplay)
if (Constants.Pref.Theme.DARK.equals(preferences.getTheme())) { if (isDark(preferences, context.getResources().getConfiguration())) {
return new ContextThemeWrapper(context, R.style.Theme_Keychain_Dark); return new ContextThemeWrapper(context, R.style.Theme_Keychain_Dark);
} else { } else {
return new ContextThemeWrapper(context, R.style.Theme_Keychain_Light); return new ContextThemeWrapper(context, R.style.Theme_Keychain_Light);
@@ -63,7 +86,11 @@ public class ThemeChanger {
* the caller can re-create the activity, if need be. * the caller can re-create the activity, if need be.
*/ */
public boolean changeTheme() { public boolean changeTheme() {
String newTheme = mPreferences.getTheme(); String newTheme = Constants.Pref.Theme.LIGHT;
if (isDark(mPreferences, mContext.getResources().getConfiguration())) {
newTheme = Constants.Pref.Theme.DARK;
}
if (mCurrentTheme != null && mCurrentTheme.equals(newTheme)) { if (mCurrentTheme != null && mCurrentTheme.equals(newTheme)) {
return false; return false;
} }

View File

@@ -14,7 +14,7 @@
android:elevation="4dp" android:elevation="4dp"
android:minHeight="?attr/actionBarSize" android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:popupTheme="?popupOverlayStyle"
tools:ignore="UnusedAttribute" /> tools:ignore="UnusedAttribute" />
<FrameLayout <FrameLayout

View File

@@ -148,7 +148,7 @@
android:touchscreenBlocksFocus="false" android:touchscreenBlocksFocus="false"
android:transitionGroup="false" android:transitionGroup="false"
app:layout_collapseMode="pin" app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:popupTheme="?popupOverlayStyle"
tools:ignore="UnusedAttribute" /> tools:ignore="UnusedAttribute" />
</com.google.android.material.appbar.CollapsingToolbarLayout> </com.google.android.material.appbar.CollapsingToolbarLayout>

View File

@@ -32,11 +32,15 @@
</string-array> </string-array>
<string-array name="theme_entries" translatable="false"> <string-array name="theme_entries" translatable="false">
<item>@string/theme_dark</item> <item>@string/theme_auto</item>
<item>@string/theme_light</item> <item>@string/theme_light</item>
<item>@string/theme_dark</item>
</string-array> </string-array>
<string-array name="theme_values" translatable="false"> <string-array name="theme_values" translatable="false">
<item>"auto"</item>
<item>"dark"</item> <item>"dark"</item>
<item>"light"</item> <item>"light"</item>
</string-array> </string-array>
<attr name="popupOverlayStyle" format="reference" />
</resources> </resources>

View File

@@ -314,8 +314,9 @@
<string name="usage_sign_and_encrypt">"Sign &amp; Encrypt"</string> <string name="usage_sign_and_encrypt">"Sign &amp; Encrypt"</string>
<string name="error">"Error"</string> <string name="error">"Error"</string>
<string name="error_message">"Error: %s"</string> <string name="error_message">"Error: %s"</string>
<string name="theme_dark">"Dark"</string> <string name="theme_auto">"Automatic"</string>
<string name="theme_light">"Light"</string> <string name="theme_light">"Light"</string>
<string name="theme_dark">"Dark"</string>
<string name="strip">"Strip it"</string> <string name="strip">"Strip it"</string>
<!-- sentences --> <!-- sentences -->

View File

@@ -7,6 +7,8 @@
<item name="colorAccent">#2196f3</item> <item name="colorAccent">#2196f3</item>
<item name="colorBrightToolbar">#dddddd</item> <item name="colorBrightToolbar">#dddddd</item>
<item name="popupOverlayStyle">@style/ThemeOverlay.AppCompat.Light</item>
<item name="colorFab">#2196f3</item> <item name="colorFab">#2196f3</item>
<item name="colorFabPressed">#1976d2</item> <item name="colorFabPressed">#1976d2</item>
<item name="colorFabText">#fafafa</item> <item name="colorFabText">#fafafa</item>
@@ -48,6 +50,8 @@
<item name="colorAccent">#2196f3</item> <item name="colorAccent">#2196f3</item>
<item name="colorBrightToolbar">#808080</item> <item name="colorBrightToolbar">#808080</item>
<item name="popupOverlayStyle">@style/ThemeOverlay.AppCompat.Dark</item>
<item name="colorFab">#2196f3</item> <item name="colorFab">#2196f3</item>
<item name="colorFabPressed">#1976d2</item> <item name="colorFabPressed">#1976d2</item>
<item name="colorFabText">#fafafa</item> <item name="colorFabText">#fafafa</item>