refactor nav bar colour & colour patching

This commit is contained in:
FC (Fay) Stegerman
2023-10-12 03:47:11 +02:00
committed by Sylvia van Os
parent 0b01604c4e
commit 7fa8ae8697
4 changed files with 28 additions and 15 deletions

View File

@@ -48,6 +48,12 @@ public class CatimaAppCompatActivity extends AppCompatActivity {
Utils.postPatchColors(this);
}
@Override
protected void onResume() {
super.onResume();
Utils.setNavigationBarColor(this, null, Utils.resolveBackgroundColor(this), !Utils.isDarkModeEnabled(this));
}
protected void enableToolbarBackButton() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {

View File

@@ -777,7 +777,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements
@SuppressLint("DefaultLocale")
@Override
public void onResume() {
protected void onResume() {
super.onResume();
Log.i(TAG, "To view card: " + loyaltyCardId);

View File

@@ -47,7 +47,6 @@ import androidx.core.graphics.BlendModeColorFilterCompat;
import androidx.core.graphics.BlendModeCompat;
import androidx.core.graphics.ColorUtils;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import com.google.android.material.color.MaterialColors;
@@ -555,7 +554,7 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
@Override
public void onResume() {
protected void onResume() {
super.onResume();
Log.i(TAG, "To view card: " + loyaltyCardId);
@@ -636,11 +635,7 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
// Set bottomAppBar and system navigation bar color
binding.bottomAppBar.setBackgroundColor(darkenedColor);
if (window != null && Build.VERSION.SDK_INT >= 27) {
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(window, binding.getRoot());
wic.setAppearanceLightNavigationBars(Utils.needsDarkForeground(darkenedColor));
window.setNavigationBarColor(darkenedColor);
}
Utils.setNavigationBarColor(null, window, darkenedColor, Utils.needsDarkForeground(darkenedColor));
int complementaryColor = Utils.getComplementaryColor(darkenedColor);
binding.fabEdit.setBackgroundTintList(ColorStateList.valueOf(complementaryColor));

View File

@@ -28,6 +28,7 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
@@ -669,21 +670,32 @@ public class Utils {
// rendering mess
// use after views are inflated
public static void postPatchColors(AppCompatActivity activity) {
TypedValue typedValue = new TypedValue();
activity.getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true);
activity.findViewById(android.R.id.content).setBackgroundColor(typedValue.data);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
activity.findViewById(android.R.id.content).setBackgroundColor(resolveBackgroundColor(activity));
}
}
if (Build.VERSION.SDK_INT >= 27) {
Window window = activity.getWindow();
// Either pass an Activity on which to call getWindow() or an existing Window (may be null) returned by that function.
public static void setNavigationBarColor(@Nullable AppCompatActivity activity, @Nullable Window window, int color, boolean useLightBars) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
if (window == null && activity != null) {
window = activity.getWindow();
}
if (window != null) {
View decorView = window.getDecorView();
WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(window, decorView);
wic.setAppearanceLightNavigationBars(!isDarkModeEnabled(activity));
window.setNavigationBarColor(typedValue.data);
wic.setAppearanceLightNavigationBars(useLightBars);
window.setNavigationBarColor(color);
}
}
}
public static int resolveBackgroundColor(AppCompatActivity activity) {
TypedValue typedValue = new TypedValue();
activity.getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true);
return typedValue.data;
}
public static int getHeaderColorFromImage(Bitmap image, int fallback) {
if (image == null) {
return fallback;