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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
package it.niedermann.owncloud.notes.branding;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreference;
import it.niedermann.owncloud.notes.R;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.JELLY_BEAN;
import static it.niedermann.owncloud.notes.branding.BrandingUtil.getSecondaryForegroundColorDependingOnTheme;
public class BrandedSwitchPreference extends SwitchPreference implements Branded {
@ColorInt
private Integer mainColor = null;
@ColorInt
private Integer textColor = null;
@Nullable
private Switch switchView;
public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public BrandedSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BrandedSwitchPreference(Context context) {
super(context);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
if (holder.itemView instanceof ViewGroup) {
switchView = findSwitchWidget(holder.itemView);
if (mainColor != null && textColor != null) {
applyBrand();
}
}
}
@Override
public void applyBrand(@ColorInt int mainColor, @ColorInt int textColor) {
this.mainColor = mainColor;
this.textColor = textColor;
// onBindViewHolder is called after applyBrand, therefore we have to store the given values and apply them later.
applyBrand();
}
private void applyBrand() {
if (switchView != null) {
final int finalMainColor = getSecondaryForegroundColorDependingOnTheme(getContext(), mainColor);
// int trackColor = Color.argb(77, Color.red(finalMainColor), Color.green(finalMainColor), Color.blue(finalMainColor));
DrawableCompat.setTintList(switchView.getThumbDrawable(), new ColorStateList(
new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
new int[]{finalMainColor, getContext().getResources().getColor(R.color.fg_default_low)}
));
DrawableCompat.setTintList(switchView.getTrackDrawable(), new ColorStateList(
new int[][]{new int[]{android.R.attr.state_checked}, new int[]{}},
new int[]{finalMainColor, getContext().getResources().getColor(R.color.fg_default_low)}
));
}
}
/**
* Recursively go through view tree until we find an android.widget.Switch
*
* @param view Root view to start searching
* @return A Switch class or null
* @see <a href="https://gist.github.com/marchold/45e22839eb94aa14dfb5">Source</a>
*/
private Switch findSwitchWidget(View view) {
if (view instanceof Switch) {
return (Switch) view;
}
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
Switch result = findSwitchWidget(child);
if (result != null) return result;
}
if (child instanceof Switch) {
return (Switch) child;
}
}
}
return null;
}
}
|