From 2aa88a2921307906b232ce415e2f1b213ee391cb Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Sat, 11 Jul 2020 18:42:44 +0200 Subject: [PATCH] Fix two minor issues in themes - Neatly map configured themes to styles - Make the dark NoActionBar themes inherit from Dark/AMOLED --- .../com/beemdevelopment/aegis/ThemeMap.java | 23 ++++++++++ .../aegis/ui/AboutActivity.java | 6 +-- .../aegis/ui/AegisActivity.java | 45 ++++++++++--------- .../aegis/ui/AuthActivity.java | 30 ++----------- .../aegis/ui/ScannerActivity.java | 3 +- app/src/main/res/layout/activity_auth.xml | 13 ++---- app/src/main/res/values/styles.xml | 4 +- 7 files changed, 59 insertions(+), 65 deletions(-) create mode 100644 app/src/main/java/com/beemdevelopment/aegis/ThemeMap.java diff --git a/app/src/main/java/com/beemdevelopment/aegis/ThemeMap.java b/app/src/main/java/com/beemdevelopment/aegis/ThemeMap.java new file mode 100644 index 00000000..e8fd4eb3 --- /dev/null +++ b/app/src/main/java/com/beemdevelopment/aegis/ThemeMap.java @@ -0,0 +1,23 @@ +package com.beemdevelopment.aegis; + +import com.google.common.collect.ImmutableMap; + +import java.util.Map; + +public class ThemeMap { + private ThemeMap() { + + } + + public static final Map DEFAULT = ImmutableMap.of( + Theme.LIGHT, R.style.AppTheme, + Theme.DARK, R.style.AppTheme_Dark, + Theme.AMOLED, R.style.AppTheme_TrueBlack + ); + + public static final Map NO_ACTION_BAR = ImmutableMap.of( + Theme.LIGHT, R.style.AppTheme_Light_NoActionBar, + Theme.DARK, R.style.AppTheme_Dark_NoActionBar, + Theme.AMOLED, R.style.AppTheme_TrueBlack_NoActionBar + ); +} diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/AboutActivity.java b/app/src/main/java/com/beemdevelopment/aegis/ui/AboutActivity.java index cff204e1..c9fab821 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/AboutActivity.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/AboutActivity.java @@ -12,8 +12,6 @@ import android.widget.TextView; import android.widget.Toast; import androidx.annotation.AttrRes; -import androidx.annotation.ColorInt; -import androidx.annotation.ColorRes; import androidx.annotation.StringRes; import androidx.core.view.LayoutInflaterCompat; @@ -80,7 +78,7 @@ public class AboutActivity extends AegisActivity { View btnChangelog = findViewById(R.id.btn_changelog); btnChangelog.setOnClickListener(v -> { - ChangelogDialog.create().setTheme(getCurrentTheme()).show(getSupportFragmentManager(), "CHANGELOG_DIALOG"); + ChangelogDialog.create().setTheme(getConfiguredTheme()).show(getSupportFragmentManager(), "CHANGELOG_DIALOG"); }); } @@ -118,7 +116,7 @@ public class AboutActivity extends AegisActivity { private void showLicenseDialog() { String stylesheet = getString(R.string.custom_notices_format_style); - int backgroundColorResource = getCurrentTheme() == Theme.AMOLED ? R.attr.cardBackgroundFocused : R.attr.cardBackground; + int backgroundColorResource = getConfiguredTheme() == Theme.AMOLED ? R.attr.cardBackgroundFocused : R.attr.cardBackground; String backgroundColor = getThemeColorAsHex(backgroundColorResource); String textColor = getThemeColorAsHex(R.attr.primaryText); String licenseColor = getThemeColorAsHex(R.attr.cardBackgroundFocused); diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/AegisActivity.java b/app/src/main/java/com/beemdevelopment/aegis/ui/AegisActivity.java index d5515c4c..08fed966 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/AegisActivity.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/AegisActivity.java @@ -13,14 +13,16 @@ import com.beemdevelopment.aegis.AegisApplication; import com.beemdevelopment.aegis.Preferences; import com.beemdevelopment.aegis.R; import com.beemdevelopment.aegis.Theme; +import com.beemdevelopment.aegis.ThemeMap; import com.beemdevelopment.aegis.vault.VaultManagerException; import java.util.Locale; +import java.util.Map; public abstract class AegisActivity extends AppCompatActivity implements AegisApplication.LockListener { private boolean _resumed; private AegisApplication _app; - private Theme _currentTheme; + private Theme _configuredTheme; @Override protected void onCreate(Bundle savedInstanceState) { @@ -28,7 +30,7 @@ public abstract class AegisActivity extends AppCompatActivity implements AegisAp // set the theme and locale before creating the activity Preferences prefs = getPreferences(); - setPreferredTheme(prefs.getCurrentTheme()); + onSetTheme(); setLocale(prefs.getLocale()); super.onCreate(savedInstanceState); @@ -84,9 +86,26 @@ public abstract class AegisActivity extends AppCompatActivity implements AegisAp return _app.getPreferences(); } - protected void setPreferredTheme(Theme theme) { + /** + * Called when the activity is expected to set its theme. + */ + protected void onSetTheme() { + setTheme(ThemeMap.DEFAULT); + } + + /** + * Sets the theme of the activity. The actual style that is set is picked from the + * given map, based on the theme configured by the user. + */ + protected void setTheme(Map themeMap) { + int theme = themeMap.get(getConfiguredTheme()); + setTheme(theme); + } + + protected Theme getConfiguredTheme() { + Theme theme = getPreferences().getCurrentTheme(); + if (theme == Theme.SYSTEM || theme == Theme.SYSTEM_AMOLED) { - // set the theme based on the system theme int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; switch (currentNightMode) { case Configuration.UI_MODE_NIGHT_NO: @@ -98,19 +117,7 @@ public abstract class AegisActivity extends AppCompatActivity implements AegisAp } } - _currentTheme = theme; - - switch (theme) { - case LIGHT: - setTheme(R.style.AppTheme); - break; - case DARK: - setTheme(R.style.AppTheme_Dark); - break; - case AMOLED: - setTheme(R.style.AppTheme_TrueBlack); - break; - } + return theme; } protected void setLocale(Locale locale) { @@ -146,8 +153,4 @@ public abstract class AegisActivity extends AppCompatActivity implements AegisAp private boolean isOrphan() { return !(this instanceof MainActivity) && !(this instanceof AuthActivity) && _app.isVaultLocked(); } - - protected Theme getCurrentTheme() { - return _currentTheme; - } } diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/AuthActivity.java b/app/src/main/java/com/beemdevelopment/aegis/ui/AuthActivity.java index 9d189fad..18b0aae2 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/AuthActivity.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/AuthActivity.java @@ -2,7 +2,6 @@ package com.beemdevelopment.aegis.ui; import android.content.Context; import android.content.Intent; -import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; import android.view.KeyEvent; @@ -26,7 +25,7 @@ import com.beemdevelopment.aegis.AegisApplication; import com.beemdevelopment.aegis.CancelAction; import com.beemdevelopment.aegis.Preferences; import com.beemdevelopment.aegis.R; -import com.beemdevelopment.aegis.Theme; +import com.beemdevelopment.aegis.ThemeMap; import com.beemdevelopment.aegis.crypto.KeyStoreHandle; import com.beemdevelopment.aegis.crypto.KeyStoreHandleException; import com.beemdevelopment.aegis.crypto.MasterKey; @@ -159,31 +158,8 @@ public class AuthActivity extends AegisActivity { } @Override - protected void setPreferredTheme(Theme theme) { - if (theme == Theme.SYSTEM || theme == Theme.SYSTEM_AMOLED) { - // set the theme based on the system theme - int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; - switch (currentNightMode) { - case Configuration.UI_MODE_NIGHT_NO: - theme = Theme.LIGHT; - break; - case Configuration.UI_MODE_NIGHT_YES: - theme = theme == Theme.SYSTEM_AMOLED ? Theme.AMOLED : Theme.DARK; - break; - } - } - - switch (theme) { - case LIGHT: - setTheme(R.style.AppTheme_Light_NoActionBar); - break; - case DARK: - setTheme(R.style.AppTheme_Dark_NoActionBar); - break; - case AMOLED: - setTheme(R.style.AppTheme_TrueBlack_NoActionBar); - break; - } + protected void onSetTheme() { + setTheme(ThemeMap.NO_ACTION_BAR); } private void selectPassword() { diff --git a/app/src/main/java/com/beemdevelopment/aegis/ui/ScannerActivity.java b/app/src/main/java/com/beemdevelopment/aegis/ui/ScannerActivity.java index e07953a5..f6a66ab8 100644 --- a/app/src/main/java/com/beemdevelopment/aegis/ui/ScannerActivity.java +++ b/app/src/main/java/com/beemdevelopment/aegis/ui/ScannerActivity.java @@ -17,7 +17,6 @@ import androidx.camera.view.PreviewView; import androidx.core.content.ContextCompat; import com.beemdevelopment.aegis.R; -import com.beemdevelopment.aegis.Theme; import com.beemdevelopment.aegis.helpers.QrCodeAnalyzer; import com.beemdevelopment.aegis.otp.GoogleAuthInfo; import com.beemdevelopment.aegis.otp.GoogleAuthInfoException; @@ -77,7 +76,7 @@ public class ScannerActivity extends AegisActivity implements QrCodeAnalyzer.Lis } @Override - protected void setPreferredTheme(Theme theme) { + protected void onSetTheme() { setTheme(R.style.AppTheme_Fullscreen); } diff --git a/app/src/main/res/layout/activity_auth.xml b/app/src/main/res/layout/activity_auth.xml index b725c792..3d10805a 100644 --- a/app/src/main/res/layout/activity_auth.xml +++ b/app/src/main/res/layout/activity_auth.xml @@ -31,9 +31,9 @@ android:fontFamily="sans-serif-light" android:singleLine="false" android:text="@string/authentication_multiline" - android:textColor="?attr/authText" - android:textFontWeight="500" - android:textSize="46sp" /> + android:textStyle="bold" + android:textSize="46sp" + android:textColor="?attr/authText" /> + android:inputType="textPassword" /> - true - -