From d3b8569ef7851cf5693b68fde0a4d59d34485d48 Mon Sep 17 00:00:00 2001 From: Katharine Chui Date: Wed, 2 Feb 2022 01:41:13 +0800 Subject: [PATCH] settings for pure black oled --- .../card_locker/CatimaAppCompatActivity.java | 18 +++++++++++++- .../protect/card_locker/MainActivity.java | 4 +++- .../main/java/protect/card_locker/Utils.java | 24 ++++++++++++++++++- .../card_locker/preferences/Settings.java | 5 ++++ .../preferences/SettingsActivity.java | 8 +++++++ app/src/main/res/values-night/themes.xml | 8 +++++++ app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/values/themes.xml | 4 ++++ app/src/main/res/xml/preferences.xml | 20 ++++++++++------ 9 files changed, 83 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java b/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java index 6e1ea26e7..9a3ff44dd 100644 --- a/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java +++ b/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java @@ -16,10 +16,21 @@ public class CatimaAppCompatActivity extends AppCompatActivity { super.attachBaseContext(Utils.updateBaseContextLocale(base)); } + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // XXX on the splash screen activity, aka the main activity, this has to be executed after applying dynamic colors, not before + // so running this only on non main for now + if (!this.getClass().getSimpleName().equals(MainActivity.class.getSimpleName())) { + Utils.patchOledDarkTheme(this); + } + } + @Override protected void onPostCreate(@Nullable Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); - // material you themes uses background color for top bar, let the layout underneath show through + // material 3 designer does not consider status bar colors + // XXX changing this in onCreate causes issues with the splash screen activity, so doing this here if (Build.VERSION.SDK_INT >= 23) { getWindow().setStatusBarColor(Color.TRANSPARENT); getWindow().getDecorView().setSystemUiVisibility(Utils.isDarkModeEnabled(this) ? 0 : View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); @@ -27,5 +38,10 @@ public class CatimaAppCompatActivity extends AppCompatActivity { // icons are always white back then getWindow().setStatusBarColor(Utils.isDarkModeEnabled(this) ? Color.TRANSPARENT : Color.argb(127, 0, 0, 0)); } + // XXX android 9 and below has a nasty rendering bug if the theme was patched earlier + // the splash screen activity needs the fix regardless to solve a dynamic color api issue + if (!this.getClass().getSimpleName().equals(MainActivity.class.getSimpleName())) { + Utils.postPatchOledDarkTheme(this); + } } } diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 7feea68cb..0f4906d0b 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -183,13 +183,15 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard // onPreCreate can't tell this activity uses a material theme due to splash screen, force color application here DynamicColors.applyIfAvailable(this); + Utils.patchOledDarkTheme(this); setTitle(R.string.app_name); setContentView(R.layout.main_activity); // XXX more dynamic color fixing due to splash screen + // without this the background color will get stuck with the old color before dynamic color TypedValue typedValue = new TypedValue(); - getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true); + getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true); findViewById(android.R.id.content).setBackgroundColor(typedValue.data); Toolbar toolbar = findViewById(R.id.toolbar); diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index 89a757865..f5e4fd328 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -14,6 +14,7 @@ import android.os.Build; import android.os.LocaleList; import android.provider.MediaStore; import android.util.Log; +import android.util.TypedValue; import android.widget.Toast; import com.google.zxing.BinaryBitmap; @@ -39,9 +40,11 @@ import java.util.GregorianCalendar; import java.util.Locale; import java.util.Map; +import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; import androidx.core.graphics.ColorUtils; import androidx.exifinterface.media.ExifInterface; + import protect.card_locker.preferences.Settings; public class Utils { @@ -457,6 +460,25 @@ public class Utils { R = 255 - R; G = 255 - G; B = 255 - B; - return R + (G << 8) + ( B << 16) + ( A << 24); + return R + (G << 8) + (B << 16) + (A << 24); + } + + // replace colors in the current theme + // use before views are inflated, after dynamic color + public static void patchOledDarkTheme(AppCompatActivity activity) { + if (isDarkModeEnabled(activity) && new Settings(activity).getOledDark()) { + activity.getTheme().applyStyle(R.style.DarkBackground, true); + } + } + + // XXX android 9 and below has issues with patched theme where the background becomes a + // rendering mess + // use after views are inflated + public static void postPatchOledDarkTheme(AppCompatActivity activity) { + if (isDarkModeEnabled(activity) && new Settings(activity).getOledDark()) { + TypedValue typedValue = new TypedValue(); + activity.getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true); + activity.findViewById(android.R.id.content).setBackgroundColor(typedValue.data); + } } } diff --git a/app/src/main/java/protect/card_locker/preferences/Settings.java b/app/src/main/java/protect/card_locker/preferences/Settings.java index 1583382be..bd77dc135 100644 --- a/app/src/main/java/protect/card_locker/preferences/Settings.java +++ b/app/src/main/java/protect/card_locker/preferences/Settings.java @@ -9,6 +9,7 @@ import androidx.annotation.IntegerRes; import androidx.annotation.StringRes; import androidx.appcompat.app.AppCompatDelegate; import androidx.preference.PreferenceManager; + import protect.card_locker.R; import protect.card_locker.Utils; @@ -102,4 +103,8 @@ public class Settings { public boolean getDisableLockscreenWhileViewingCard() { return getBoolean(R.string.settings_key_disable_lockscreen_while_viewing_card, true); } + + public boolean getOledDark() { + return getBoolean(R.string.settings_key_oled_dark, false); + } } diff --git a/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java b/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java index afb47e31c..bb710b1f7 100644 --- a/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java +++ b/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java @@ -18,6 +18,7 @@ import androidx.fragment.app.DialogFragment; import androidx.preference.ListPreference; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; + import nl.invissvenska.numberpickerpreference.NumberDialogPreference; import nl.invissvenska.numberpickerpreference.NumberPickerPreferenceDialogFragment; import protect.card_locker.CatimaAppCompatActivity; @@ -132,6 +133,13 @@ public class SettingsActivity extends CatimaAppCompatActivity { refreshActivity(true); return true; }); + + Preference oledDarkPreference = findPreference(getResources().getString(R.string.settings_key_oled_dark)); + assert oledDarkPreference != null; + oledDarkPreference.setOnPreferenceChangeListener((preference, newValue) -> { + refreshActivity(true); + return true; + }); } private void refreshActivity(boolean reloadMain) { diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml index f58017c6e..939599068 100644 --- a/app/src/main/res/values-night/themes.xml +++ b/app/src/main/res/values-night/themes.xml @@ -30,4 +30,12 @@ true + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 784b20df1..bf381222d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -109,6 +109,7 @@ pref_keep_screen_on Prevent screen lock pref_disable_lockscreen_while_viewing_card + pref_oled_dark sharedpreference_active_tab sharedpreference_privacy_policy_shown sharedpreference_sort @@ -212,6 +213,7 @@ Turn flashlight on Turn flashlight off Language + Pure black background for dark theme pref_locale System Select color diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index b5d76e47c..9b0486158 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,4 +1,5 @@ + + + +