Skip to content

Commit

Permalink
Fixing issue where toolbar and status bar icons would be black on dar…
Browse files Browse the repository at this point in the history
…k background (AntennaPod#6274)
  • Loading branch information
JonathanZopf committed Apr 12, 2023
1 parent 39d309e commit 6bef4df
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
Expand Up @@ -117,7 +117,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

AppBarLayout appBar = root.findViewById(R.id.appBar);
CollapsingToolbarLayout collapsingToolbar = root.findViewById(R.id.collapsing_toolbar);
ToolbarIconTintManager iconTintManager = new ToolbarIconTintManager(getContext(), toolbar, collapsingToolbar) {
ToolbarIconTintManager iconTintManager = new ToolbarIconTintManager(getActivity(), toolbar, collapsingToolbar) {
@Override
protected void doTint(Context themedContext) {
toolbar.getMenu().findItem(R.id.visit_website_item)
Expand Down
Expand Up @@ -151,7 +151,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
viewBinding.progressBar.setVisibility(View.VISIBLE);

ToolbarIconTintManager iconTintManager = new ToolbarIconTintManager(
getContext(), viewBinding.toolbar, viewBinding.collapsingToolbar) {
getActivity(), viewBinding.toolbar, viewBinding.collapsingToolbar) {
@Override
protected void doTint(Context themedContext) {
viewBinding.toolbar.getMenu().findItem(R.id.refresh_item)
Expand Down
@@ -1,44 +1,73 @@
package de.danoeh.antennapod.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.ContextThemeWrapper;

import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import de.danoeh.antennapod.R;

public abstract class ToolbarIconTintManager implements AppBarLayout.OnOffsetChangedListener {
private final Context context;
private static final int defaultStatusBarHeight = 24; //See https://m2.material.io/design/platform-guidance/android-bars.html#status-bar
private final Activity activity;
private final CollapsingToolbarLayout collapsingToolbar;
private final MaterialToolbar toolbar;
private boolean isTinted = false;
private boolean isWhiteIconsStatusBar = false;

public ToolbarIconTintManager(Context context, MaterialToolbar toolbar, CollapsingToolbarLayout collapsingToolbar) {
this.context = context;
public ToolbarIconTintManager(Activity activity, MaterialToolbar toolbar, CollapsingToolbarLayout collapsingToolbar) {
this.activity = activity;
this.collapsingToolbar = collapsingToolbar;
this.toolbar = toolbar;
}

/**
* Change appearance of status bar and toolbar in order to fix issue #6274.
* Sets them black or white depending on whether these elements are currently over header or not.
* @param appBarLayout the {@link AppBarLayout} which offset has changed
* @param offset the vertical offset for the parent {@link AppBarLayout}, in px
*/
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
boolean tint = (collapsingToolbar.getHeight() + offset) > (2 * collapsingToolbar.getMinimumHeight());
if (isTinted != tint) {
isTinted = tint;
//Convert status bar height from dp to px
int statusBarHeightPx = defaultStatusBarHeight * activity.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT;
double ratio = (float) (collapsingToolbar.getHeight() + offset) / collapsingToolbar.getMinimumHeight();

//Check if status bar and/or toolbar need to be changed to reflect appearance
boolean whiteIconsStatusBar = -offset / 2 < statusBarHeightPx || ratio < 1.5;
boolean tintToolbar = ratio > 1.5;

//Change appearance of status bar only when needed to reduce overhead
if (isWhiteIconsStatusBar != whiteIconsStatusBar) {
isWhiteIconsStatusBar = whiteIconsStatusBar;
WindowInsetsControllerCompat windowInsetController = WindowCompat.getInsetsController(activity.getWindow(), activity.getWindow().getDecorView());
windowInsetController.setAppearanceLightStatusBars(whiteIconsStatusBar);
}

//Tint toolbar if needed
if (isTinted != tintToolbar) {
isTinted = tintToolbar;
updateTint();
}
}

public void updateTint() {
if (isTinted) {
doTint(new ContextThemeWrapper(context, R.style.Theme_AntennaPod_Dark));
doTint(new ContextThemeWrapper(activity, R.style.Theme_AntennaPod_Dark));
safeSetColorFilter(toolbar.getNavigationIcon(), new PorterDuffColorFilter(0xffffffff, Mode.SRC_ATOP));
safeSetColorFilter(toolbar.getOverflowIcon(), new PorterDuffColorFilter(0xffffffff, Mode.SRC_ATOP));
safeSetColorFilter(toolbar.getCollapseIcon(), new PorterDuffColorFilter(0xffffffff, Mode.SRC_ATOP));
} else {
doTint(context);
doTint(activity);
safeSetColorFilter(toolbar.getNavigationIcon(), null);
safeSetColorFilter(toolbar.getOverflowIcon(), null);
safeSetColorFilter(toolbar.getCollapseIcon(), null);
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -23,7 +23,7 @@ project.ext {
// AndroidX
annotationVersion = "1.2.0"
appcompatVersion = "1.3.1"
coreVersion = "1.5.0"
coreVersion = "1.9.0"
fragmentVersion = "1.3.6"
mediaVersion = "1.4.3"
paletteVersion = "1.0.0"
Expand Down

0 comments on commit 6bef4df

Please sign in to comment.