2121import static androidx .core .graphics .ColorUtils .blendARGB ;
2222import static com .google .android .material .theme .overlay .MaterialThemeOverlay .wrap ;
2323
24- import android .annotation .SuppressLint ;
2524import android .content .Context ;
2625import android .content .res .ColorStateList ;
2726import android .graphics .PorterDuff ;
3130import android .os .Build .VERSION ;
3231import android .os .Build .VERSION_CODES ;
3332import androidx .appcompat .content .res .AppCompatResources ;
34- import androidx .appcompat .graphics .drawable .AnimatedStateListDrawableCompat ;
3533import androidx .appcompat .widget .DrawableUtils ;
3634import androidx .appcompat .widget .SwitchCompat ;
3735import androidx .appcompat .widget .TintTypedArray ;
4240import androidx .annotation .Nullable ;
4341import androidx .core .graphics .drawable .DrawableCompat ;
4442import com .google .android .material .internal .ThemeEnforcement ;
45- import com .google .android .material .internal .ViewUtils ;
46- import java .lang .reflect .Field ;
4743import java .util .Arrays ;
4844
4945/**
@@ -55,9 +51,6 @@ public class MaterialSwitch extends SwitchCompat {
5551 private static final int DEF_STYLE_RES = R .style .Widget_Material3_CompoundButton_MaterialSwitch ;
5652 private static final int [] STATE_SET_WITH_ICON = { R .attr .state_with_icon };
5753
58- @ NonNull private final SwitchWidth switchWidth = SwitchWidth .create (this );
59- @ NonNull private final ThumbPosition thumbPosition = new ThumbPosition ();
60-
6154 @ Nullable private Drawable thumbDrawable ;
6255 @ Nullable private Drawable thumbIconDrawable ;
6356
@@ -115,25 +108,15 @@ public MaterialSwitch(@NonNull Context context, @Nullable AttributeSet attrs, in
115108
116109 attributes .recycle ();
117110
111+ setEnforceSwitchWidth (false );
112+
118113 refreshThumbDrawable ();
119114 refreshTrackDrawable ();
120115 }
121116
122- // TODO(b/227338106): remove this workaround and move to use setEnforceSwitchWidth(false) after
123- // AppCompat 1.6.0-stable is released.
124- @ Override
125- public void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
126- super .onMeasure (widthMeasureSpec , heightMeasureSpec );
127- switchWidth .set (getSwitchMinWidth ());
128- }
129-
130117 @ Override
131118 public void invalidate () {
132- // ThumbPosition update will trigger invalidate(), update thumb/track tint here.
133- if (thumbPosition != null ) {
134- // This may happen when super classes' constructors call this method.
135- updateDrawableTints ();
136- }
119+ updateDrawableTints ();
137120 super .invalidate ();
138121 }
139122
@@ -151,32 +134,6 @@ protected int[] onCreateDrawableState(int extraSpace) {
151134 return drawableState ;
152135 }
153136
154- // TODO(b/227338106): remove this workaround and move to use setEnforceSwitchWidth(false) after
155- // AppCompat 1.6.0-stable is released.
156- @ Override
157- public int getCompoundPaddingLeft () {
158- if (!ViewUtils .isLayoutRtl (this )) {
159- return super .getCompoundPaddingLeft ();
160- }
161- // Compound paddings are used during onMeasure() to decide the component width, at that time
162- // the switch width is not overridden yet so we need to adjust the value to make measurement
163- // right. This can be removed after the workaround is removed.
164- return super .getCompoundPaddingLeft () - switchWidth .get () + getSwitchMinWidth ();
165- }
166-
167- // TODO(b/227338106): remove this workaround and move to use setEnforceSwitchWidth(false) after
168- // AppCompat 1.6.0-stable is released.
169- @ Override
170- public int getCompoundPaddingRight () {
171- if (ViewUtils .isLayoutRtl (this )) {
172- return super .getCompoundPaddingRight ();
173- }
174- // Compound paddings are used during onMeasure() to decide the component width, at that time
175- // the switch width is not overridden yet so we need to adjust the value to make measurement
176- // right. This can be removed after the workaround is removed.
177- return super .getCompoundPaddingRight () - switchWidth .get () + getSwitchMinWidth ();
178- }
179-
180137 @ Override
181138 public void setThumbDrawable (@ Nullable Drawable drawable ) {
182139 thumbDrawable = drawable ;
@@ -405,12 +362,6 @@ public PorterDuff.Mode getTrackDecorationTintMode() {
405362 return trackDecorationTintMode ;
406363 }
407364
408- // TODO(b/227338106): remove this workaround to use super.getThumbPosition() directly after
409- // AppCompat 1.6.0-stable is released.
410- private float getThumbPos () {
411- return thumbPosition .get ();
412- }
413-
414365 private void refreshThumbDrawable () {
415366 thumbDrawable =
416367 createTintableDrawableIfNeeded (thumbDrawable , thumbTintList , getThumbTintMode ());
@@ -502,7 +453,7 @@ private void updateDrawableTints() {
502453 return ;
503454 }
504455
505- float thumbPosition = getThumbPos ();
456+ float thumbPosition = getThumbPosition ();
506457
507458 if (thumbTintList != null ) {
508459 setInterpolatedDrawableTintIfPossible (
@@ -575,18 +526,7 @@ private static void setInterpolatedDrawableTintIfPossible(
575526 if (drawable == null || tint == null ) {
576527 return ;
577528 }
578- // TODO(b/232529333): remove this workaround after updating AppCompat version to 1.6.
579- if (drawable instanceof AnimatedStateListDrawableCompat
580- && VERSION .SDK_INT < VERSION_CODES .LOLLIPOP ) {
581- DrawableCompat .setTintList (
582- drawable ,
583- ColorStateList .valueOf (
584- blendARGB (
585- tint .getColorForState (stateUnchecked , 0 ),
586- tint .getColorForState (stateChecked , 0 ),
587- thumbPosition )));
588- return ;
589- }
529+
590530 DrawableCompat .setTint (
591531 drawable ,
592532 blendARGB (
@@ -608,88 +548,4 @@ private static Drawable createTintableDrawableIfNeeded(
608548 }
609549 return drawable ;
610550 }
611-
612- // TODO(b/227338106): remove this workaround and move to use setEnforceSwitchWidth(false) after
613- // AppCompat 1.6.0-stable is released.
614- @ SuppressLint ("PrivateApi" )
615- private static final class SwitchWidth {
616-
617- @ NonNull private final MaterialSwitch materialSwitch ;
618- @ Nullable private final Field switchWidthField ;
619-
620- @ NonNull
621- static SwitchWidth create (@ NonNull MaterialSwitch materialSwitch ) {
622- return new SwitchWidth (materialSwitch , createSwitchWidthField ());
623- }
624-
625- private SwitchWidth (@ NonNull MaterialSwitch materialSwitch , @ Nullable Field switchWidthField ) {
626- this .materialSwitch = materialSwitch ;
627- this .switchWidthField = switchWidthField ;
628- }
629-
630- int get () {
631- try {
632- if (switchWidthField != null ) {
633- return switchWidthField .getInt (materialSwitch );
634- }
635- } catch (IllegalAccessException e ) {
636- // Fall through
637- }
638- // Return getSwitchMinWidth() so no width adjustment will be done.
639- return materialSwitch .getSwitchMinWidth ();
640- }
641-
642- void set (int switchWidth ) {
643- try {
644- if (switchWidthField != null ) {
645- switchWidthField .setInt (materialSwitch , switchWidth );
646- }
647- } catch (IllegalAccessException e ) {
648- // Fall through
649- }
650- }
651-
652- @ Nullable
653- private static Field createSwitchWidthField () {
654- try {
655- Field switchWidthField = SwitchCompat .class .getDeclaredField ("mSwitchWidth" );
656- switchWidthField .setAccessible (true );
657- return switchWidthField ;
658- } catch (NoSuchFieldException | SecurityException e ) {
659- return null ;
660- }
661- }
662- }
663-
664- // TODO(b/227338106): remove this workaround to use super.getThumbPosition() directly after
665- // AppCompat 1.6.0-stable is released.
666- @ SuppressLint ("PrivateApi" )
667- private final class ThumbPosition {
668- private final Field thumbPositionField ;
669-
670- private ThumbPosition () {
671- thumbPositionField = createThumbPositionField ();
672- }
673-
674- float get () {
675- try {
676- if (thumbPositionField != null ) {
677- return thumbPositionField .getFloat (MaterialSwitch .this );
678- }
679- } catch (IllegalAccessException e ) {
680- // Fall through
681- }
682- return isChecked () ? 1 : 0 ;
683- }
684-
685- private Field createThumbPositionField () {
686- try {
687- Field thumbPositionField = SwitchCompat .class .getDeclaredField ("mThumbPosition" );
688- thumbPositionField .setAccessible (true );
689- return thumbPositionField ;
690- } catch (Exception e ) {
691- return null ;
692- }
693- }
694- }
695551}
0 commit comments