/
MaterialButtonHelper.java
469 lines (408 loc) · 17.1 KB
/
MaterialButtonHelper.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.button;
import com.google.android.material.R;
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.annotation.ChecksSdkIntAtLeast;
import androidx.annotation.Dimension;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import com.google.android.material.color.MaterialColors;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.ripple.RippleDrawableCompat;
import com.google.android.material.ripple.RippleUtils;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;
import com.google.android.material.shape.Shapeable;
/** @hide */
@RestrictTo(LIBRARY_GROUP)
class MaterialButtonHelper {
@ChecksSdkIntAtLeast(api = VERSION_CODES.LOLLIPOP)
private static final boolean IS_MIN_LOLLIPOP = VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP;
private static final boolean IS_LOLLIPOP =
VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP && VERSION.SDK_INT <= VERSION_CODES.LOLLIPOP_MR1;
private final MaterialButton materialButton;
@NonNull private ShapeAppearanceModel shapeAppearanceModel;
private int insetLeft;
private int insetRight;
private int insetTop;
private int insetBottom;
private int cornerRadius;
private int strokeWidth;
@Nullable private Mode backgroundTintMode;
@Nullable private ColorStateList backgroundTint;
@Nullable private ColorStateList strokeColor;
@Nullable private ColorStateList rippleColor;
@Nullable private Drawable maskDrawable;
private boolean shouldDrawSurfaceColorStroke = false;
private boolean backgroundOverwritten = false;
private boolean cornerRadiusSet = false;
private boolean checkable;
private boolean toggleCheckedStateOnClick = true;
private LayerDrawable rippleDrawable;
private int elevation;
MaterialButtonHelper(MaterialButton button, @NonNull ShapeAppearanceModel shapeAppearanceModel) {
materialButton = button;
this.shapeAppearanceModel = shapeAppearanceModel;
}
void loadFromAttributes(@NonNull TypedArray attributes) {
insetLeft = attributes.getDimensionPixelOffset(R.styleable.MaterialButton_android_insetLeft, 0);
insetRight =
attributes.getDimensionPixelOffset(R.styleable.MaterialButton_android_insetRight, 0);
insetTop = attributes.getDimensionPixelOffset(R.styleable.MaterialButton_android_insetTop, 0);
insetBottom =
attributes.getDimensionPixelOffset(R.styleable.MaterialButton_android_insetBottom, 0);
// cornerRadius should override whatever corner radius is set in shapeAppearanceModel
if (attributes.hasValue(R.styleable.MaterialButton_cornerRadius)) {
cornerRadius = attributes.getDimensionPixelSize(R.styleable.MaterialButton_cornerRadius, -1);
setShapeAppearanceModel(shapeAppearanceModel.withCornerSize(cornerRadius));
cornerRadiusSet = true;
}
strokeWidth = attributes.getDimensionPixelSize(R.styleable.MaterialButton_strokeWidth, 0);
backgroundTintMode =
ViewUtils.parseTintMode(
attributes.getInt(R.styleable.MaterialButton_backgroundTintMode, -1), Mode.SRC_IN);
backgroundTint =
MaterialResources.getColorStateList(
materialButton.getContext(), attributes, R.styleable.MaterialButton_backgroundTint);
strokeColor =
MaterialResources.getColorStateList(
materialButton.getContext(), attributes, R.styleable.MaterialButton_strokeColor);
rippleColor =
MaterialResources.getColorStateList(
materialButton.getContext(), attributes, R.styleable.MaterialButton_rippleColor);
checkable = attributes.getBoolean(R.styleable.MaterialButton_android_checkable, false);
elevation = attributes.getDimensionPixelSize(R.styleable.MaterialButton_elevation, 0);
toggleCheckedStateOnClick =
attributes.getBoolean(R.styleable.MaterialButton_toggleCheckedStateOnClick, true);
// Store padding before setting background, since background overwrites padding values
int paddingStart = ViewCompat.getPaddingStart(materialButton);
int paddingTop = materialButton.getPaddingTop();
int paddingEnd = ViewCompat.getPaddingEnd(materialButton);
int paddingBottom = materialButton.getPaddingBottom();
// Update materialButton's background without triggering setBackgroundOverwritten()
if (attributes.hasValue(R.styleable.MaterialButton_android_background)) {
setBackgroundOverwritten();
} else {
updateBackground();
}
// Set the stored padding values
ViewCompat.setPaddingRelative(
materialButton,
paddingStart + insetLeft,
paddingTop + insetTop,
paddingEnd + insetRight,
paddingBottom + insetBottom);
}
private void updateBackground() {
materialButton.setInternalBackground(createBackground());
MaterialShapeDrawable materialShapeDrawable = getMaterialShapeDrawable();
if (materialShapeDrawable != null) {
materialShapeDrawable.setElevation(elevation);
// Workaround (b/231320562): Setting background will cause drawables wrapped inside a
// RippleDrawable lose their states, we need to reset the state here.
materialShapeDrawable.setState(materialButton.getDrawableState());
}
}
/**
* Method that is triggered when our initial background, created by {@link #createBackground()},
* has been overwritten with a new background. Sets the {@link #backgroundOverwritten} flag, which
* disables some of the functionality tied to our custom background.
*/
void setBackgroundOverwritten() {
backgroundOverwritten = true;
// AppCompatButton re-applies any tint that was set when background is changed, so we must
// pass our tints to AppCompatButton when background is overwritten.
materialButton.setSupportBackgroundTintList(backgroundTint);
materialButton.setSupportBackgroundTintMode(backgroundTintMode);
}
boolean isBackgroundOverwritten() {
return backgroundOverwritten;
}
@NonNull
private InsetDrawable wrapDrawableWithInset(Drawable drawable) {
return new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom);
}
void setSupportBackgroundTintList(@Nullable ColorStateList tintList) {
if (backgroundTint != tintList) {
backgroundTint = tintList;
if (getMaterialShapeDrawable() != null) {
DrawableCompat.setTintList(getMaterialShapeDrawable(), backgroundTint);
}
}
}
ColorStateList getSupportBackgroundTintList() {
return backgroundTint;
}
void setSupportBackgroundTintMode(@Nullable Mode mode) {
if (backgroundTintMode != mode) {
backgroundTintMode = mode;
if (getMaterialShapeDrawable() != null && backgroundTintMode != null) {
DrawableCompat.setTintMode(getMaterialShapeDrawable(), backgroundTintMode);
}
}
}
Mode getSupportBackgroundTintMode() {
return backgroundTintMode;
}
void setShouldDrawSurfaceColorStroke(boolean shouldDrawSurfaceColorStroke) {
this.shouldDrawSurfaceColorStroke = shouldDrawSurfaceColorStroke;
updateStroke();
}
/**
* Create RippleDrawable background for Lollipop (API 21) and later API versions
*
* @return Drawable representing background for this button.
*/
private Drawable createBackground() {
MaterialShapeDrawable backgroundDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
Context context = materialButton.getContext();
backgroundDrawable.initializeElevationOverlay(context);
DrawableCompat.setTintList(backgroundDrawable, backgroundTint);
if (backgroundTintMode != null) {
DrawableCompat.setTintMode(backgroundDrawable, backgroundTintMode);
}
backgroundDrawable.setStroke(strokeWidth, strokeColor);
MaterialShapeDrawable surfaceColorStrokeDrawable =
new MaterialShapeDrawable(shapeAppearanceModel);
surfaceColorStrokeDrawable.setTint(Color.TRANSPARENT);
surfaceColorStrokeDrawable.setStroke(
strokeWidth,
shouldDrawSurfaceColorStroke
? MaterialColors.getColor(materialButton, R.attr.colorSurface)
: Color.TRANSPARENT);
if (IS_MIN_LOLLIPOP) {
maskDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
DrawableCompat.setTint(maskDrawable, Color.WHITE);
rippleDrawable =
new RippleDrawable(
RippleUtils.sanitizeRippleDrawableColor(rippleColor),
wrapDrawableWithInset(
new LayerDrawable(
new Drawable[] {surfaceColorStrokeDrawable, backgroundDrawable})),
maskDrawable);
return rippleDrawable;
} else {
maskDrawable = new RippleDrawableCompat(shapeAppearanceModel);
DrawableCompat.setTintList(
maskDrawable, RippleUtils.sanitizeRippleDrawableColor(rippleColor));
rippleDrawable =
new LayerDrawable(
new Drawable[] {surfaceColorStrokeDrawable, backgroundDrawable, maskDrawable});
return wrapDrawableWithInset(rippleDrawable);
}
}
void updateMaskBounds(int height, int width) {
if (maskDrawable != null) {
maskDrawable.setBounds(insetLeft, insetTop, width - insetRight, height - insetBottom);
}
}
void setBackgroundColor(int color) {
if (getMaterialShapeDrawable() != null) {
getMaterialShapeDrawable().setTint(color);
}
}
void setRippleColor(@Nullable ColorStateList rippleColor) {
if (this.rippleColor != rippleColor) {
this.rippleColor = rippleColor;
if (IS_MIN_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawable) {
((RippleDrawable) materialButton.getBackground())
.setColor(RippleUtils.sanitizeRippleDrawableColor(rippleColor));
} else if (!IS_MIN_LOLLIPOP
&& materialButton.getBackground() instanceof RippleDrawableCompat) {
((RippleDrawableCompat) materialButton.getBackground())
.setTintList(RippleUtils.sanitizeRippleDrawableColor(rippleColor));
}
}
}
@Nullable
ColorStateList getRippleColor() {
return rippleColor;
}
void setStrokeColor(@Nullable ColorStateList strokeColor) {
if (this.strokeColor != strokeColor) {
this.strokeColor = strokeColor;
updateStroke();
}
}
@Nullable
ColorStateList getStrokeColor() {
return strokeColor;
}
void setStrokeWidth(int strokeWidth) {
if (this.strokeWidth != strokeWidth) {
this.strokeWidth = strokeWidth;
updateStroke();
}
}
int getStrokeWidth() {
return strokeWidth;
}
private void updateStroke() {
MaterialShapeDrawable materialShapeDrawable = getMaterialShapeDrawable();
MaterialShapeDrawable surfaceColorStrokeDrawable = getSurfaceColorStrokeDrawable();
if (materialShapeDrawable != null) {
materialShapeDrawable.setStroke(strokeWidth, strokeColor);
if (surfaceColorStrokeDrawable != null) {
surfaceColorStrokeDrawable.setStroke(
strokeWidth,
shouldDrawSurfaceColorStroke
? MaterialColors.getColor(materialButton, R.attr.colorSurface)
: Color.TRANSPARENT);
}
}
}
void setCornerRadius(int cornerRadius) {
// If cornerRadius wasn't set in the style, it would have a default value of -1. Therefore, for
// setCornerRadius(-1) to take effect, we need this cornerRadiusSet flag.
if (!cornerRadiusSet || this.cornerRadius != cornerRadius) {
this.cornerRadius = cornerRadius;
cornerRadiusSet = true;
setShapeAppearanceModel(shapeAppearanceModel.withCornerSize(cornerRadius));
}
}
int getCornerRadius() {
return cornerRadius;
}
@Nullable
private MaterialShapeDrawable getMaterialShapeDrawable(boolean getSurfaceColorStrokeDrawable) {
if (rippleDrawable != null && rippleDrawable.getNumberOfLayers() > 0) {
if (IS_MIN_LOLLIPOP) {
InsetDrawable insetDrawable = (InsetDrawable) rippleDrawable.getDrawable(0);
LayerDrawable layerDrawable = (LayerDrawable) insetDrawable.getDrawable();
return (MaterialShapeDrawable)
layerDrawable.getDrawable(getSurfaceColorStrokeDrawable ? 0 : 1);
} else {
return (MaterialShapeDrawable)
rippleDrawable.getDrawable(getSurfaceColorStrokeDrawable ? 0 : 1);
}
}
return null;
}
@Nullable
MaterialShapeDrawable getMaterialShapeDrawable() {
return getMaterialShapeDrawable(false);
}
void setCheckable(boolean checkable) {
this.checkable = checkable;
}
boolean isCheckable() {
return checkable;
}
boolean isToggleCheckedStateOnClick() {
return toggleCheckedStateOnClick;
}
void setToggleCheckedStateOnClick(boolean toggleCheckedStateOnClick) {
this.toggleCheckedStateOnClick = toggleCheckedStateOnClick;
}
@Nullable
private MaterialShapeDrawable getSurfaceColorStrokeDrawable() {
return getMaterialShapeDrawable(true);
}
private void updateButtonShape(@NonNull ShapeAppearanceModel shapeAppearanceModel) {
// There seems to be a bug to drawables that is affecting Lollipop, since invalidation is not
// changing an existing drawable shape. This is a fallback.
if (IS_LOLLIPOP && !backgroundOverwritten) {
// Store padding before setting background, since background overwrites padding values
int paddingStart = ViewCompat.getPaddingStart(materialButton);
int paddingTop = materialButton.getPaddingTop();
int paddingEnd = ViewCompat.getPaddingEnd(materialButton);
int paddingBottom = materialButton.getPaddingBottom();
updateBackground();
// Set the stored padding values
ViewCompat.setPaddingRelative(
materialButton, paddingStart, paddingTop, paddingEnd, paddingBottom);
} else {
if (getMaterialShapeDrawable() != null) {
getMaterialShapeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
}
if (getSurfaceColorStrokeDrawable() != null) {
getSurfaceColorStrokeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
}
if (getMaskDrawable() != null) {
getMaskDrawable().setShapeAppearanceModel(shapeAppearanceModel);
}
}
}
@Nullable
public Shapeable getMaskDrawable() {
if (rippleDrawable != null && rippleDrawable.getNumberOfLayers() > 1) {
if (rippleDrawable.getNumberOfLayers() > 2) {
// This is a LayerDrawable with 3 layers, so return the mask layer
return (Shapeable) rippleDrawable.getDrawable(2);
}
// This is a RippleDrawable, so return the mask layer
return (Shapeable) rippleDrawable.getDrawable(1);
}
return null;
}
void setShapeAppearanceModel(@NonNull ShapeAppearanceModel shapeAppearanceModel) {
this.shapeAppearanceModel = shapeAppearanceModel;
updateButtonShape(shapeAppearanceModel);
}
@NonNull
ShapeAppearanceModel getShapeAppearanceModel() {
return this.shapeAppearanceModel;
}
public void setInsetBottom(@Dimension int newInsetBottom) {
setVerticalInsets(insetTop, newInsetBottom);
}
public int getInsetBottom() {
return insetBottom;
}
public void setInsetTop(@Dimension int newInsetTop) {
setVerticalInsets(newInsetTop, insetBottom);
}
private void setVerticalInsets(@Dimension int newInsetTop, @Dimension int newInsetBottom) {
// Store padding before setting background, since background overwrites padding values
int paddingStart = ViewCompat.getPaddingStart(materialButton);
int paddingTop = materialButton.getPaddingTop();
int paddingEnd = ViewCompat.getPaddingEnd(materialButton);
int paddingBottom = materialButton.getPaddingBottom();
int oldInsetTop = insetTop;
int oldInsetBottom = insetBottom;
insetBottom = newInsetBottom;
insetTop = newInsetTop;
if (!backgroundOverwritten) {
updateBackground();
}
// Set the stored padding values
ViewCompat.setPaddingRelative(
materialButton,
paddingStart,
paddingTop + newInsetTop - oldInsetTop,
paddingEnd,
paddingBottom + newInsetBottom - oldInsetBottom);
}
public int getInsetTop() {
return insetTop;
}
}