Add automatic theme

This commit is contained in:
mimi89999
2021-09-20 20:02:53 +02:00
parent 6f38af1582
commit f4bbc17751
7 changed files with 43 additions and 6 deletions

View File

@@ -142,6 +142,7 @@ public final class Constants {
public static final String KEY_SIGNATURES_TABLE_INITIALIZED = "key_signatures_table_initialized";
public static final class Theme {
public static final String AUTO = "auto";
public static final String LIGHT = "light";
public static final String DARK = "dark";
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.content.Context;
import android.content.res.Configuration;
import android.view.ContextThemeWrapper;
import org.sufficientlysecure.keychain.Constants;
@@ -33,13 +34,35 @@ public class ThemeChanger {
private int mLightResId;
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) {
Preferences preferences = Preferences.getPreferences(context);
// 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
// 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);
} else {
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.
*/
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)) {
return false;
}

View File

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

View File

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

View File

@@ -32,11 +32,15 @@
</string-array>
<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_dark</item>
</string-array>
<string-array name="theme_values" translatable="false">
<item>"auto"</item>
<item>"dark"</item>
<item>"light"</item>
</string-array>
<attr name="popupOverlayStyle" format="reference" />
</resources>

View File

@@ -314,8 +314,9 @@
<string name="usage_sign_and_encrypt">"Sign &amp; Encrypt"</string>
<string name="error">"Error"</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_dark">"Dark"</string>
<string name="strip">"Strip it"</string>
<!-- sentences -->

View File

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