Skip to content

Commit e968929

Browse files
drchenpaulfthomas
authored andcommitted
[Switch] Update to AppCompat 1.5.0-beta01 and remove workarounds (also update compileSdkVersion/targetSdkVersion to 32)
PiperOrigin-RevId: 460970462
1 parent b18f45e commit e968929

File tree

3 files changed

+9
-153
lines changed

3 files changed

+9
-153
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ allprojects {
1919
}
2020

2121
ext {
22-
compileSdkVersion = 31
22+
compileSdkVersion = 32
2323
minSdkVersion = 14
24-
targetSdkVersion = 31
24+
targetSdkVersion = 32
2525

2626
androidXVersions = [
2727
annotation : '1.2.0',
28-
appCompat : '1.4.0',
28+
appCompat : '1.5.0-beta01', // To include SwitchCompat fixes
2929
cardView : '1.0.0',
3030
constraintlayout : '2.0.1',
3131
coordinatorlayout : '1.1.0',

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ the `com.android.support:design:28.0.0` dependency.
7575
7676
In order to use the latest versions of Material Components for Android and the
7777
AndroidX Jetpack libraries, you will have to install the latest version of
78-
Android Studio and update your app's `compileSdkVersion` to `31`.
78+
Android Studio and update your app's `compileSdkVersion` to `32`.
7979
8080
As part of migrating to Android 12, you'll need to add `android:exported` to any
8181
activities, services, or broadcast receivers in your manifest that use intent

lib/java/com/google/android/material/materialswitch/MaterialSwitch.java

Lines changed: 5 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static androidx.core.graphics.ColorUtils.blendARGB;
2222
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
2323

24-
import android.annotation.SuppressLint;
2524
import android.content.Context;
2625
import android.content.res.ColorStateList;
2726
import android.graphics.PorterDuff;
@@ -31,7 +30,6 @@
3130
import android.os.Build.VERSION;
3231
import android.os.Build.VERSION_CODES;
3332
import androidx.appcompat.content.res.AppCompatResources;
34-
import androidx.appcompat.graphics.drawable.AnimatedStateListDrawableCompat;
3533
import androidx.appcompat.widget.DrawableUtils;
3634
import androidx.appcompat.widget.SwitchCompat;
3735
import androidx.appcompat.widget.TintTypedArray;
@@ -42,8 +40,6 @@
4240
import androidx.annotation.Nullable;
4341
import androidx.core.graphics.drawable.DrawableCompat;
4442
import com.google.android.material.internal.ThemeEnforcement;
45-
import com.google.android.material.internal.ViewUtils;
46-
import java.lang.reflect.Field;
4743
import 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

Comments
 (0)