From 1f6d3d94ed102521a9dc7bbe22fc8ac4134f313e Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Sun, 24 Sep 2023 12:46:47 +0200 Subject: [PATCH] Make app background follow theme and appearance --- .../cringe_authenticator/MainActivity.java | 2 ++ .../unlock/UnlockActivity.java | 1 + .../cringe_authenticator/util/Theme.java | 32 +++++++++++++++---- .../cringe_authenticator/util/ThemeUtil.java | 12 +++++++ app/src/main/res/layout/activity_main.xml | 1 + app/src/main/res/layout/activity_unlock.xml | 2 +- 6 files changed, 42 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java index 709242b..2f98b45 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/MainActivity.java @@ -32,6 +32,7 @@ import com.cringe_studios.cringe_authenticator.util.NavigationUtil; import com.cringe_studios.cringe_authenticator.util.OTPDatabase; import com.cringe_studios.cringe_authenticator.util.SettingsUtil; import com.cringe_studios.cringe_authenticator.util.StyledDialogBuilder; +import com.cringe_studios.cringe_authenticator.util.ThemeUtil; import com.cringe_studios.cringe_authenticator_library.OTPType; import com.google.mlkit.vision.common.InputImage; @@ -136,6 +137,7 @@ public class MainActivity extends BaseActivity { binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); + ThemeUtil.loadBackground(this); setSupportActionBar(binding.toolbar); diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/unlock/UnlockActivity.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/unlock/UnlockActivity.java index 568971d..6de16df 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/unlock/UnlockActivity.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/unlock/UnlockActivity.java @@ -60,6 +60,7 @@ public class UnlockActivity extends BaseActivity { binding = ActivityUnlockBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); + ThemeUtil.loadBackground(this); if(SettingsUtil.isBiometricEncryption(this) && BiometricUtil.isSupported(this)) { Runnable onSuccess = () -> { diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/Theme.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/Theme.java index d22f8a5..205a79d 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/Theme.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/Theme.java @@ -1,5 +1,6 @@ package com.cringe_studios.cringe_authenticator.util; +import androidx.annotation.DrawableRes; import androidx.annotation.StringRes; import androidx.annotation.StyleRes; @@ -7,12 +8,12 @@ import com.cringe_studios.cringe_authenticator.R; public enum Theme { - BLUE_GREEN(R.string.theme_blue_green, R.style.Theme_CringeAuthenticator_Blue_Green), - RED_BLUE(R.string.theme_red_blue, R.style.Theme_CringeAuthenticator_Red_Blue), - PINK_GREEN(R.string.theme_pink_green, R.style.Theme_CringeAuthenticator_Pink_Green), - BLUE_YELLOW(R.string.theme_blue_yellow, R.style.Theme_CringeAuthenticator_Blue_Yellow), - GREEN_YELLOW(R.string.theme_green_yellow, R.style.Theme_CringeAuthenticator_Green_Yellow), - ORANGE_TURQUOISE(R.string.theme_orange_turquoise, R.style.Theme_CringeAuthenticator_Orange_Turquoise), + BLUE_GREEN(R.string.theme_blue_green, R.style.Theme_CringeAuthenticator_Blue_Green, R.drawable.background_blue_green_light, R.drawable.background_blue_green), + RED_BLUE(R.string.theme_red_blue, R.style.Theme_CringeAuthenticator_Red_Blue, R.drawable.background_red_blue_light, R.drawable.background_red_blue), + PINK_GREEN(R.string.theme_pink_green, R.style.Theme_CringeAuthenticator_Pink_Green, R.drawable.background_pink_green_light, R.drawable.background_pink_green), + BLUE_YELLOW(R.string.theme_blue_yellow, R.style.Theme_CringeAuthenticator_Blue_Yellow, R.drawable.background_blue_yellow_light, R.drawable.background_blue_yellow), + GREEN_YELLOW(R.string.theme_green_yellow, R.style.Theme_CringeAuthenticator_Green_Yellow, R.drawable.background_green_yellow_light, R.drawable.background_green_yellow), + ORANGE_TURQUOISE(R.string.theme_orange_turquoise, R.style.Theme_CringeAuthenticator_Orange_Turquoise, R.drawable.background_orange_turquoise_light, R.drawable.background_orange_turquoise), ; @StringRes @@ -21,9 +22,17 @@ public enum Theme { @StyleRes private final int style; - Theme(@StringRes int name, @StyleRes int style) { + @DrawableRes + private final int lightBackground; + + @DrawableRes + private final int darkBackground; + + Theme(@StringRes int name, @StyleRes int style, @DrawableRes int lightBackground, @DrawableRes int darkBackground) { this.name = name; this.style = style; + this.lightBackground = lightBackground; + this.darkBackground = darkBackground; } @StringRes @@ -36,4 +45,13 @@ public enum Theme { return style; } + @DrawableRes + public int getLightBackground() { + return lightBackground; + } + + @DrawableRes + public int getDarkBackground() { + return darkBackground; + } } diff --git a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/ThemeUtil.java b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/ThemeUtil.java index 903655b..427dc4a 100644 --- a/app/src/main/java/com/cringe_studios/cringe_authenticator/util/ThemeUtil.java +++ b/app/src/main/java/com/cringe_studios/cringe_authenticator/util/ThemeUtil.java @@ -1,6 +1,8 @@ package com.cringe_studios.cringe_authenticator.util; import android.annotation.SuppressLint; +import android.util.Log; +import android.view.View; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; @@ -16,4 +18,14 @@ public class ThemeUtil { AppCompatDelegate.setDefaultNightMode(SettingsUtil.getAppearance(activity).getValue()); } + public static void loadBackground(AppCompatActivity activity) { + Theme theme = SettingsUtil.getTheme(activity); + Appearance appearance = SettingsUtil.getAppearance(activity); + + View v = activity.findViewById(R.id.app_background); + if(v != null) { + v.setBackgroundResource(appearance == Appearance.LIGHT ? theme.getLightBackground() : theme.getDarkBackground()); + } + } + } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d680b80..069d3a9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,6 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" + android:id="@+id/app_background" tools:context=".MainActivity">