27
27
import android .graphics .PorterDuff .Mode ;
28
28
import android .graphics .drawable .Drawable ;
29
29
import android .graphics .drawable .LayerDrawable ;
30
- import android .os .Build .VERSION ;
31
- import android .os .Build .VERSION_CODES ;
32
30
import androidx .appcompat .content .res .AppCompatResources ;
33
- import androidx .appcompat .widget .DrawableUtils ;
34
31
import androidx .appcompat .widget .SwitchCompat ;
35
32
import androidx .appcompat .widget .TintTypedArray ;
36
33
import android .util .AttributeSet ;
37
- import android .view .Gravity ;
38
34
import androidx .annotation .DrawableRes ;
39
35
import androidx .annotation .NonNull ;
40
36
import androidx .annotation .Nullable ;
41
37
import androidx .core .graphics .drawable .DrawableCompat ;
38
+ import com .google .android .material .internal .DrawableUtils ;
42
39
import com .google .android .material .internal .ThemeEnforcement ;
43
- import java . util . Arrays ;
40
+ import com . google . android . material . internal . ViewUtils ;
44
41
45
42
/**
46
43
* A class that creates a Material Themed Switch. This class is intended to provide a brand new
@@ -95,15 +92,15 @@ public MaterialSwitch(@NonNull Context context, @Nullable AttributeSet attrs, in
95
92
thumbIconDrawable = attributes .getDrawable (R .styleable .MaterialSwitch_thumbIcon );
96
93
thumbIconTintList = attributes .getColorStateList (R .styleable .MaterialSwitch_thumbIconTint );
97
94
thumbIconTintMode =
98
- DrawableUtils .parseTintMode (
95
+ ViewUtils .parseTintMode (
99
96
attributes .getInt (R .styleable .MaterialSwitch_thumbIconTintMode , -1 ), Mode .SRC_IN );
100
97
101
98
trackDecorationDrawable =
102
99
attributes .getDrawable (R .styleable .MaterialSwitch_trackDecoration );
103
100
trackDecorationTintList =
104
101
attributes .getColorStateList (R .styleable .MaterialSwitch_trackDecorationTint );
105
102
trackDecorationTintMode =
106
- DrawableUtils .parseTintMode (
103
+ ViewUtils .parseTintMode (
107
104
attributes .getInt (R .styleable .MaterialSwitch_trackDecorationTintMode , -1 ), Mode .SRC_IN );
108
105
109
106
attributes .recycle ();
@@ -128,8 +125,8 @@ protected int[] onCreateDrawableState(int extraSpace) {
128
125
mergeDrawableStates (drawableState , STATE_SET_WITH_ICON );
129
126
}
130
127
131
- currentStateUnchecked = getUncheckedState (drawableState );
132
- currentStateChecked = getCheckedState (drawableState );
128
+ currentStateUnchecked = DrawableUtils . getUncheckedState (drawableState );
129
+ currentStateChecked = DrawableUtils . getCheckedState (drawableState );
133
130
134
131
return drawableState ;
135
132
}
@@ -364,67 +361,26 @@ public PorterDuff.Mode getTrackDecorationTintMode() {
364
361
365
362
private void refreshThumbDrawable () {
366
363
thumbDrawable =
367
- createTintableDrawableIfNeeded (thumbDrawable , thumbTintList , getThumbTintMode ());
364
+ DrawableUtils .createTintableDrawableIfNeeded (
365
+ thumbDrawable , thumbTintList , getThumbTintMode ());
368
366
thumbIconDrawable =
369
- createTintableDrawableIfNeeded (thumbIconDrawable , thumbIconTintList , thumbIconTintMode );
367
+ DrawableUtils .createTintableDrawableIfNeeded (
368
+ thumbIconDrawable , thumbIconTintList , thumbIconTintMode );
370
369
371
370
updateDrawableTints ();
372
371
373
- super .setThumbDrawable (compositeThumbAndIconDrawable (thumbDrawable , thumbIconDrawable ));
372
+ super .setThumbDrawable (
373
+ DrawableUtils .compositeTwoLayeredDrawable (thumbDrawable , thumbIconDrawable ));
374
374
375
375
refreshDrawableState ();
376
376
}
377
377
378
- @ Nullable
379
- private static Drawable compositeThumbAndIconDrawable (
380
- @ Nullable Drawable thumbDrawable , @ Nullable Drawable thumbIconDrawable ) {
381
- if (thumbDrawable == null ) {
382
- return thumbIconDrawable ;
383
- }
384
- if (thumbIconDrawable == null ) {
385
- return thumbDrawable ;
386
- }
387
- LayerDrawable drawable = new LayerDrawable (new Drawable []{thumbDrawable , thumbIconDrawable });
388
- int iconNewWidth ;
389
- int iconNewHeight ;
390
- if (thumbIconDrawable .getIntrinsicWidth () <= thumbDrawable .getIntrinsicWidth ()
391
- && thumbIconDrawable .getIntrinsicHeight () <= thumbDrawable .getIntrinsicHeight ()) {
392
- // If the icon is smaller than the thumb in both its width and height, keep icon's size.
393
- iconNewWidth = thumbIconDrawable .getIntrinsicWidth ();
394
- iconNewHeight = thumbIconDrawable .getIntrinsicHeight ();
395
- } else {
396
- float thumbIconRatio =
397
- (float ) thumbIconDrawable .getIntrinsicWidth () / thumbIconDrawable .getIntrinsicHeight ();
398
- float thumbRatio =
399
- (float ) thumbDrawable .getIntrinsicWidth () / thumbDrawable .getIntrinsicHeight ();
400
- if (thumbIconRatio >= thumbRatio ) {
401
- // If the icon is wider in ratio than the thumb, shrink it according to its width.
402
- iconNewWidth = thumbDrawable .getIntrinsicWidth ();
403
- iconNewHeight = (int ) (iconNewWidth / thumbIconRatio );
404
- } else {
405
- // If the icon is taller in ratio than the thumb, shrink it according to its height.
406
- iconNewHeight = thumbDrawable .getIntrinsicHeight ();
407
- iconNewWidth = (int ) (thumbIconRatio * iconNewHeight );
408
- }
409
- }
410
- // Centers the icon inside the thumb. Before M there's no layer gravity support, we need to use
411
- // layer insets to adjust the icon position manually.
412
- if (VERSION .SDK_INT >= VERSION_CODES .M ) {
413
- drawable .setLayerSize (1 , iconNewWidth , iconNewHeight );
414
- drawable .setLayerGravity (1 , Gravity .CENTER );
415
- } else {
416
- int horizontalInset = (thumbDrawable .getIntrinsicWidth () - iconNewWidth ) / 2 ;
417
- int verticalInset = (thumbDrawable .getIntrinsicHeight () - iconNewHeight ) / 2 ;
418
- drawable .setLayerInset (1 , horizontalInset , verticalInset , horizontalInset , verticalInset );
419
- }
420
- return drawable ;
421
- }
422
-
423
378
private void refreshTrackDrawable () {
424
379
trackDrawable =
425
- createTintableDrawableIfNeeded (trackDrawable , trackTintList , getTrackTintMode ());
380
+ DrawableUtils .createTintableDrawableIfNeeded (
381
+ trackDrawable , trackTintList , getTrackTintMode ());
426
382
trackDecorationDrawable =
427
- createTintableDrawableIfNeeded (
383
+ DrawableUtils . createTintableDrawableIfNeeded (
428
384
trackDecorationDrawable , trackDecorationTintList , trackDecorationTintMode );
429
385
430
386
updateDrawableTints ();
@@ -484,34 +440,6 @@ private void updateDrawableTints() {
484
440
}
485
441
}
486
442
487
- /** Returns a new state that removes the checked state from the input state. */
488
- private static int [] getUncheckedState (int [] state ) {
489
- int [] newState = new int [state .length ];
490
- int i = 0 ;
491
- for (int subState : state ) {
492
- if (subState != android .R .attr .state_checked ) {
493
- newState [i ++] = subState ;
494
- }
495
- }
496
- return newState ;
497
- }
498
-
499
- /** Returns a new state that adds the checked state to the input state. */
500
- private static int [] getCheckedState (int [] state ) {
501
- for (int i = 0 ; i < state .length ; i ++) {
502
- if (state [i ] == android .R .attr .state_checked ) {
503
- return state ;
504
- } else if (state [i ] == 0 ) {
505
- int [] newState = state .clone ();
506
- newState [i ] = android .R .attr .state_checked ;
507
- return newState ;
508
- }
509
- }
510
- int [] newState = Arrays .copyOf (state , state .length + 1 );
511
- newState [state .length ] = android .R .attr .state_checked ;
512
- return newState ;
513
- }
514
-
515
443
/**
516
444
* Tints the given drawable with the interpolated color according to the provided thumb position
517
445
* between unchecked and checked states. The reference color in unchecked and checked states will
@@ -534,18 +462,4 @@ private static void setInterpolatedDrawableTintIfPossible(
534
462
tint .getColorForState (stateChecked , 0 ),
535
463
thumbPosition ));
536
464
}
537
-
538
- private static Drawable createTintableDrawableIfNeeded (
539
- Drawable drawable , ColorStateList tintList , Mode tintMode ) {
540
- if (drawable == null ) {
541
- return null ;
542
- }
543
- if (tintList != null ) {
544
- drawable = DrawableCompat .wrap (drawable ).mutate ();
545
- if (tintMode != null ) {
546
- DrawableCompat .setTintMode (drawable , tintMode );
547
- }
548
- }
549
- return drawable ;
550
- }
551
465
}
0 commit comments