66 */
77package it .niedermann .owncloud .notes .branding ;
88
9- import android .annotation .SuppressLint ;
109import android .content .Context ;
1110import android .util .AttributeSet ;
1211import android .view .View ;
1312import android .view .ViewGroup ;
14- import android .widget .Switch ;
1513
1614import androidx .annotation .ColorInt ;
15+ import androidx .annotation .NonNull ;
1716import androidx .annotation .Nullable ;
1817import androidx .preference .PreferenceViewHolder ;
19- import androidx .preference .SwitchPreference ;
18+ import androidx .preference .SwitchPreferenceCompat ;
2019
21- public class BrandedSwitchPreference extends SwitchPreference implements Branded {
20+ import com .google .android .material .materialswitch .MaterialSwitch ;
21+
22+ import it .niedermann .owncloud .notes .R ;
23+
24+ public class BrandedSwitchPreference extends SwitchPreferenceCompat implements Branded {
2225
2326 @ ColorInt
2427 private Integer mainColor = null ;
2528
26- @ SuppressLint ("UseSwitchCompatOrMaterialCode" )
2729 @ Nullable
28- private Switch switchView ;
30+ private MaterialSwitch switchView ;
2931
3032 public BrandedSwitchPreference (Context context , AttributeSet attrs , int defStyleAttr , int defStyleRes ) {
3133 super (context , attrs , defStyleAttr , defStyleRes );
34+ setWidgetLayoutResource (R .layout .preference_switch );
3235 }
3336
3437 public BrandedSwitchPreference (Context context , AttributeSet attrs , int defStyleAttr ) {
3538 super (context , attrs , defStyleAttr );
39+ setWidgetLayoutResource (R .layout .preference_switch );
3640 }
3741
3842 public BrandedSwitchPreference (Context context , AttributeSet attrs ) {
3943 super (context , attrs );
44+ setWidgetLayoutResource (R .layout .preference_switch );
4045 }
4146
4247 public BrandedSwitchPreference (Context context ) {
4348 super (context );
49+ setWidgetLayoutResource (R .layout .preference_switch );
4450 }
4551
4652 @ Override
47- public void onBindViewHolder (PreferenceViewHolder holder ) {
53+ public void onBindViewHolder (@ NonNull PreferenceViewHolder holder ) {
4854 super .onBindViewHolder (holder );
4955
5056 if (holder .itemView instanceof ViewGroup ) {
@@ -65,7 +71,7 @@ public void applyBrand(@ColorInt int color) {
6571 private void applyBrand () {
6672 if (switchView != null ) {
6773 final var util = BrandingUtil .of (mainColor , getContext ());
68- util .platform . colorSwitch (switchView );
74+ util .material . colorMaterialSwitch (switchView );
6975 }
7076 }
7177
@@ -76,19 +82,19 @@ private void applyBrand() {
7682 * @return A Switch class or null
7783 * @see <a href="https://gist.github.com/marchold/45e22839eb94aa14dfb5">Source</a>
7884 */
79- private Switch findSwitchWidget (View view ) {
80- if (view instanceof Switch ) {
81- return (Switch ) view ;
85+ private MaterialSwitch findSwitchWidget (View view ) {
86+ if (view instanceof MaterialSwitch ) {
87+ return (MaterialSwitch ) view ;
8288 }
8389 if (view instanceof ViewGroup viewGroup ) {
8490 for (int i = 0 ; i < viewGroup .getChildCount (); i ++) {
8591 final var child = viewGroup .getChildAt (i );
8692 if (child instanceof ViewGroup ) {
87- @ SuppressLint ( "UseSwitchCompatOrMaterialCode" ) final var result = findSwitchWidget (child );
93+ final var result = findSwitchWidget (child );
8894 if (result != null ) return result ;
8995 }
90- if (child instanceof Switch ) {
91- return (Switch ) child ;
96+ if (child instanceof MaterialSwitch ) {
97+ return (MaterialSwitch ) child ;
9298 }
9399 }
94100 }
0 commit comments