From 329be8abbb6d9fbaa3a1f79188414cfccc27c798 Mon Sep 17 00:00:00 2001 From: Katharine Chui Date: Wed, 2 Feb 2022 00:28:27 +0800 Subject: [PATCH] various color fixes band-aid fixes for splash screen and material 3 dynamic color api change editbutton icon's color in card view based on editbutton background color make status bar transparent as per material 3, or tainted transparent for api level < 23 + light theme --- .../card_locker/CatimaAppCompatActivity.java | 18 ++++++++++++++++++ .../card_locker/LoyaltyCardViewActivity.java | 12 +++++++++++- .../java/protect/card_locker/MainActivity.java | 12 ++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java b/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java index 895cb0023..6e1ea26e7 100644 --- a/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java +++ b/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java @@ -1,7 +1,12 @@ package protect.card_locker; import android.content.Context; +import android.graphics.Color; +import android.os.Build; +import android.os.Bundle; +import android.view.View; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; public class CatimaAppCompatActivity extends AppCompatActivity { @@ -10,4 +15,17 @@ public class CatimaAppCompatActivity extends AppCompatActivity { // Apply chosen language super.attachBaseContext(Utils.updateBaseContextLocale(base)); } + + @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 + if (Build.VERSION.SDK_INT >= 23) { + getWindow().setStatusBarColor(Color.TRANSPARENT); + getWindow().getDecorView().setSystemUiVisibility(Utils.isDarkModeEnabled(this) ? 0 : View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); + } else { + // icons are always white back then + getWindow().setStatusBarColor(Utils.isDarkModeEnabled(this) ? Color.TRANSPARENT : Color.argb(127, 0, 0, 0)); + } + } } diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 79d11e440..e2654a158 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -50,6 +50,7 @@ import androidx.appcompat.widget.Toolbar; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.Guideline; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.ContextCompat; import androidx.core.graphics.ColorUtils; import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.widget.NestedScrollView; @@ -575,7 +576,16 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements maximizeButton.setColorFilter(textColor); minimizeButton.setColorFilter(textColor); bottomSheetButton.setColorFilter(textColor); - editButton.setBackgroundTintList(ColorStateList.valueOf(Utils.getComplementaryColor(darkenedColor))); + int complementaryColor = Utils.getComplementaryColor(darkenedColor); + editButton.setBackgroundTintList(ColorStateList.valueOf(complementaryColor)); + Drawable editButtonIcon = editButton.getDrawable(); + editButtonIcon.mutate(); + if (Utils.needsDarkForeground(complementaryColor)) { + editButtonIcon.setTint(ContextCompat.getColor(this, R.color.md_theme_light_onBackground)); + } else { + editButtonIcon.setTint(ContextCompat.getColor(this, R.color.md_theme_dark_onBackground)); + } + editButton.setImageDrawable(editButtonIcon); Bitmap icon = Utils.retrieveCardImage(this, loyaltyCard.id, ImageLocationType.icon); if (icon != null) { diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index e5721a3c5..7feea68cb 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -11,6 +11,7 @@ import android.database.CursorIndexOutOfBoundsException; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; +import android.util.TypedValue; import android.view.GestureDetector; import android.view.Menu; import android.view.MenuItem; @@ -28,6 +29,7 @@ import androidx.appcompat.widget.Toolbar; import androidx.core.splashscreen.SplashScreen; import androidx.recyclerview.widget.RecyclerView; +import com.google.android.material.color.DynamicColors; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.tabs.TabLayout; @@ -178,8 +180,18 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard protected void onCreate(Bundle inputSavedInstanceState) { super.onCreate(inputSavedInstanceState); SplashScreen.installSplashScreen(this); + + // onPreCreate can't tell this activity uses a material theme due to splash screen, force color application here + DynamicColors.applyIfAvailable(this); setTitle(R.string.app_name); + setContentView(R.layout.main_activity); + + // XXX more dynamic color fixing due to splash screen + TypedValue typedValue = new TypedValue(); + getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true); + findViewById(android.R.id.content).setBackgroundColor(typedValue.data); + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);