1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package de.danoeh.antennapod.view;
import android.content.Context;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.view.ContextThemeWrapper;
import androidx.appcompat.widget.Toolbar;
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 final CollapsingToolbarLayout collapsingToolbar;
private final Toolbar toolbar;
private boolean isTinted = false;
public ToolbarIconTintManager(Context context, Toolbar toolbar, CollapsingToolbarLayout collapsingToolbar) {
this.context = context;
this.collapsingToolbar = collapsingToolbar;
this.toolbar = toolbar;
}
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
boolean tint = (collapsingToolbar.getHeight() + offset) > (2 * collapsingToolbar.getMinimumHeight());
if (isTinted != tint) {
isTinted = tint;
updateTint();
}
}
public void updateTint() {
if (isTinted) {
doTint(new ContextThemeWrapper(context, 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);
safeSetColorFilter(toolbar.getNavigationIcon(), null);
safeSetColorFilter(toolbar.getOverflowIcon(), null);
safeSetColorFilter(toolbar.getCollapseIcon(), null);
}
}
private void safeSetColorFilter(Drawable icon, PorterDuffColorFilter filter) {
if (icon != null) {
icon.setColorFilter(filter);
}
}
/**
* View expansion was changed. Icons need to be tinted
* @param themedContext ContextThemeWrapper with dark theme while expanded
*/
protected abstract void doTint(Context themedContext);
}
|