Skip to content

Commit

Permalink
refactor nav bar colour & colour patching
Browse files Browse the repository at this point in the history
  • Loading branch information
obfusk committed Oct 12, 2023
1 parent 665c777 commit 5226bf6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ protected void onPostCreate(@Nullable Bundle savedInstanceState) {
Utils.postPatchColors(this);
}

@Override
protected void onResume() {
super.onResume();
Utils.setNavigationBarColor(getWindow(), Utils.resolveBackgroundColor(this), !Utils.isDarkModeEnabled(this));
}

protected void enableToolbarBackButton() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ private boolean croppedIcon() {

@SuppressLint("DefaultLocale")
@Override
public void onResume() {
protected void onResume() {
super.onResume();

Log.i(TAG, "To view card: " + loyaltyCardId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public void onSaveInstanceState(Bundle savedInstanceState) {


@Override
public void onResume() {
protected void onResume() {
super.onResume();

Log.i(TAG, "To view card: " + loyaltyCardId);
Expand Down Expand Up @@ -634,11 +634,7 @@ public void onResume() {

// 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(window, darkenedColor, Utils.needsDarkForeground(darkenedColor));

int complementaryColor = Utils.getComplementaryColor(darkenedColor);
binding.fabEdit.setBackgroundTintList(ColorStateList.valueOf(complementaryColor));
Expand Down
21 changes: 14 additions & 7 deletions app/src/main/java/protect/card_locker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -669,19 +669,26 @@ public static void patchColors(AppCompatActivity activity) {
// 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));
}
}

Window window = activity.getWindow();
if (window != null && Build.VERSION.SDK_INT >= 27) {
public static void setNavigationBarColor(Window window, int color, boolean useLightBars) {
if (window != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
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;
Expand Down

0 comments on commit 5226bf6

Please sign in to comment.