/
BadgeState.java
775 lines (638 loc) · 27.4 KB
/
BadgeState.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*
* Copyright (C) 2021 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.badge;
import com.google.android.material.R;
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import static com.google.android.material.badge.BadgeDrawable.BADGE_CONTENT_NOT_TRUNCATED;
import static com.google.android.material.badge.BadgeDrawable.BADGE_RADIUS_NOT_SPECIFIED;
import static com.google.android.material.badge.BadgeDrawable.OFFSET_ALIGNMENT_MODE_LEGACY;
import static com.google.android.material.badge.BadgeDrawable.TOP_END;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
import androidx.annotation.Dimension;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.PluralsRes;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.annotation.StyleableRes;
import androidx.annotation.XmlRes;
import com.google.android.material.badge.BadgeDrawable.BadgeGravity;
import com.google.android.material.badge.BadgeDrawable.OffsetAlignmentMode;
import com.google.android.material.drawable.DrawableUtils;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.resources.TextAppearance;
import java.util.Locale;
/**
* Provides a {@link Parcelable} implementation ({@link State}) used to ensure the state of {@code
* BadgeDrawable} is saved and restored properly, and the default values of the states are correctly
* loaded during every restoration.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public final class BadgeState {
private static final String BADGE_RESOURCE_TAG = "badge";
private final State overridingState;
private final State currentState = new State();
final float badgeRadius;
final float badgeWithTextRadius;
final float badgeWidth;
final float badgeHeight;
final float badgeWithTextWidth;
final float badgeWithTextHeight;
final int horizontalInset;
final int horizontalInsetWithText;
@OffsetAlignmentMode
int offsetAlignmentMode;
BadgeState(
Context context,
@XmlRes int badgeResId,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes,
@Nullable State storedState) {
if (storedState == null) {
storedState = new State();
}
if (badgeResId != 0) {
storedState.badgeResId = badgeResId;
}
TypedArray a = generateTypedArray(context, storedState.badgeResId, defStyleAttr, defStyleRes);
Resources res = context.getResources();
badgeRadius =
a.getDimensionPixelSize(R.styleable.Badge_badgeRadius, BADGE_RADIUS_NOT_SPECIFIED);
horizontalInset =
context
.getResources()
.getDimensionPixelSize(R.dimen.mtrl_badge_horizontal_edge_offset);
horizontalInsetWithText =
context
.getResources()
.getDimensionPixelSize(R.dimen.mtrl_badge_text_horizontal_edge_offset);
badgeWithTextRadius =
a.getDimensionPixelSize(R.styleable.Badge_badgeWithTextRadius, BADGE_RADIUS_NOT_SPECIFIED);
badgeWidth =
a.getDimension(R.styleable.Badge_badgeWidth, res.getDimension(R.dimen.m3_badge_size));
badgeWithTextWidth =
a.getDimension(
R.styleable.Badge_badgeWithTextWidth,
res.getDimension(R.dimen.m3_badge_with_text_size));
badgeHeight =
a.getDimension(R.styleable.Badge_badgeHeight, res.getDimension(R.dimen.m3_badge_size));
badgeWithTextHeight =
a.getDimension(
R.styleable.Badge_badgeWithTextHeight,
res.getDimension(R.dimen.m3_badge_with_text_size));
offsetAlignmentMode =
a.getInt(R.styleable.Badge_offsetAlignmentMode, OFFSET_ALIGNMENT_MODE_LEGACY);
currentState.alpha = storedState.alpha == State.NOT_SET ? 255 : storedState.alpha;
// Only set the badge number if it exists in the style.
// Defaulting it to 0 means the badge will incorrectly show text when the user may want a
// numberless badge.
if (storedState.number != State.NOT_SET) {
currentState.number = storedState.number;
} else if (a.hasValue(R.styleable.Badge_number)) {
currentState.number = a.getInt(R.styleable.Badge_number, 0);
} else {
currentState.number = State.BADGE_NUMBER_NONE;
}
if (storedState.text != null) {
currentState.text = storedState.text;
} else if (a.hasValue(R.styleable.Badge_badgeText)) {
currentState.text = a.getString(R.styleable.Badge_badgeText);
}
currentState.contentDescriptionForText = storedState.contentDescriptionForText;
currentState.contentDescriptionNumberless =
storedState.contentDescriptionNumberless == null
? context.getString(R.string.mtrl_badge_numberless_content_description)
: storedState.contentDescriptionNumberless;
currentState.contentDescriptionQuantityStrings =
storedState.contentDescriptionQuantityStrings == 0
? R.plurals.mtrl_badge_content_description
: storedState.contentDescriptionQuantityStrings;
currentState.contentDescriptionExceedsMaxBadgeNumberRes =
storedState.contentDescriptionExceedsMaxBadgeNumberRes == 0
? R.string.mtrl_exceed_max_badge_number_content_description
: storedState.contentDescriptionExceedsMaxBadgeNumberRes;
currentState.isVisible = storedState.isVisible == null || storedState.isVisible;
currentState.maxCharacterCount =
storedState.maxCharacterCount == State.NOT_SET
? a.getInt(R.styleable.Badge_maxCharacterCount, BADGE_CONTENT_NOT_TRUNCATED)
: storedState.maxCharacterCount;
currentState.maxNumber =
storedState.maxNumber == State.NOT_SET
? a.getInt(R.styleable.Badge_maxNumber, BADGE_CONTENT_NOT_TRUNCATED)
: storedState.maxNumber;
currentState.badgeShapeAppearanceResId =
storedState.badgeShapeAppearanceResId == null
? a.getResourceId(
R.styleable.Badge_badgeShapeAppearance,
R.style.ShapeAppearance_M3_Sys_Shape_Corner_Full)
: storedState.badgeShapeAppearanceResId;
currentState.badgeShapeAppearanceOverlayResId =
storedState.badgeShapeAppearanceOverlayResId == null
? a.getResourceId(R.styleable.Badge_badgeShapeAppearanceOverlay, 0)
: storedState.badgeShapeAppearanceOverlayResId;
currentState.badgeWithTextShapeAppearanceResId =
storedState.badgeWithTextShapeAppearanceResId == null
? a.getResourceId(
R.styleable.Badge_badgeWithTextShapeAppearance,
R.style.ShapeAppearance_M3_Sys_Shape_Corner_Full)
: storedState.badgeWithTextShapeAppearanceResId;
currentState.badgeWithTextShapeAppearanceOverlayResId =
storedState.badgeWithTextShapeAppearanceOverlayResId == null
? a.getResourceId(R.styleable.Badge_badgeWithTextShapeAppearanceOverlay, 0)
: storedState.badgeWithTextShapeAppearanceOverlayResId;
currentState.backgroundColor =
storedState.backgroundColor == null
? readColorFromAttributes(context, a, R.styleable.Badge_backgroundColor)
: storedState.backgroundColor;
currentState.badgeTextAppearanceResId =
storedState.badgeTextAppearanceResId == null
? a.getResourceId(
R.styleable.Badge_badgeTextAppearance,
R.style.TextAppearance_MaterialComponents_Badge)
: storedState.badgeTextAppearanceResId;
// Only set the badge text color if this attribute has explicitly been set, otherwise use the
// text color specified in the TextAppearance.
if (storedState.badgeTextColor != null) {
currentState.badgeTextColor = storedState.badgeTextColor;
} else if (a.hasValue(R.styleable.Badge_badgeTextColor)) {
currentState.badgeTextColor =
readColorFromAttributes(context, a, R.styleable.Badge_badgeTextColor);
} else {
TextAppearance textAppearance =
new TextAppearance(context, currentState.badgeTextAppearanceResId);
currentState.badgeTextColor = textAppearance.getTextColor().getDefaultColor();
}
currentState.badgeGravity =
storedState.badgeGravity == null
? a.getInt(R.styleable.Badge_badgeGravity, TOP_END)
: storedState.badgeGravity;
currentState.badgeHorizontalPadding =
storedState.badgeHorizontalPadding == null
? a.getDimensionPixelSize(
R.styleable.Badge_badgeWidePadding,
res.getDimensionPixelSize(R.dimen.mtrl_badge_long_text_horizontal_padding))
: storedState.badgeHorizontalPadding;
currentState.badgeVerticalPadding =
storedState.badgeVerticalPadding == null
? a.getDimensionPixelSize(
R.styleable.Badge_badgeVerticalPadding,
res.getDimensionPixelSize(R.dimen.m3_badge_with_text_vertical_padding))
: storedState.badgeVerticalPadding;
currentState.horizontalOffsetWithoutText =
storedState.horizontalOffsetWithoutText == null
? a.getDimensionPixelOffset(R.styleable.Badge_horizontalOffset, 0)
: storedState.horizontalOffsetWithoutText;
currentState.verticalOffsetWithoutText =
storedState.verticalOffsetWithoutText == null
? a.getDimensionPixelOffset(R.styleable.Badge_verticalOffset, 0)
: storedState.verticalOffsetWithoutText;
// Set the offsets when the badge has text. Default to using the badge "dot" offsets
// (horizontalOffsetWithoutText and verticalOffsetWithoutText) if there is no offsets defined
// for badges with text.
currentState.horizontalOffsetWithText =
storedState.horizontalOffsetWithText == null
? a.getDimensionPixelOffset(
R.styleable.Badge_horizontalOffsetWithText,
currentState.horizontalOffsetWithoutText)
: storedState.horizontalOffsetWithText;
currentState.verticalOffsetWithText =
storedState.verticalOffsetWithText == null
? a.getDimensionPixelOffset(
R.styleable.Badge_verticalOffsetWithText, currentState.verticalOffsetWithoutText)
: storedState.verticalOffsetWithText;
currentState.largeFontVerticalOffsetAdjustment =
storedState.largeFontVerticalOffsetAdjustment == null
? a.getDimensionPixelOffset(
R.styleable.Badge_largeFontVerticalOffsetAdjustment, 0)
: storedState.largeFontVerticalOffsetAdjustment;
currentState.additionalHorizontalOffset =
storedState.additionalHorizontalOffset == null ? 0 : storedState.additionalHorizontalOffset;
currentState.additionalVerticalOffset =
storedState.additionalVerticalOffset == null ? 0 : storedState.additionalVerticalOffset;
currentState.autoAdjustToWithinGrandparentBounds =
storedState.autoAdjustToWithinGrandparentBounds == null
? a.getBoolean(R.styleable.Badge_autoAdjustToWithinGrandparentBounds, false)
: storedState.autoAdjustToWithinGrandparentBounds;
a.recycle();
if (storedState.numberLocale == null) {
currentState.numberLocale =
VERSION.SDK_INT >= VERSION_CODES.N
? Locale.getDefault(Locale.Category.FORMAT)
: Locale.getDefault();
} else {
currentState.numberLocale = storedState.numberLocale;
}
overridingState = storedState;
}
private TypedArray generateTypedArray(
Context context,
@XmlRes int badgeResId,
@AttrRes int defStyleAttr,
@StyleRes int defStyleRes) {
AttributeSet attrs = null;
@StyleRes int style = 0;
if (badgeResId != 0) {
attrs = DrawableUtils.parseDrawableXml(context, badgeResId, BADGE_RESOURCE_TAG);
style = attrs.getStyleAttribute();
}
if (style == 0) {
style = defStyleRes;
}
return ThemeEnforcement.obtainStyledAttributes(
context, attrs, R.styleable.Badge, defStyleAttr, style);
}
State getOverridingState() {
return overridingState;
}
boolean isVisible() {
return currentState.isVisible;
}
void setVisible(boolean visible) {
overridingState.isVisible = visible;
currentState.isVisible = visible;
}
boolean hasNumber() {
return currentState.number != State.BADGE_NUMBER_NONE;
}
int getNumber() {
return currentState.number;
}
void setNumber(int number) {
overridingState.number = number;
currentState.number = number;
}
void clearNumber() {
setNumber(State.BADGE_NUMBER_NONE);
}
boolean hasText() {
return currentState.text != null;
}
String getText() {
return currentState.text;
}
void setText(String text) {
overridingState.text = text;
currentState.text = text;
}
void clearText() {
setText(null);
}
int getAlpha() {
return currentState.alpha;
}
void setAlpha(int alpha) {
overridingState.alpha = alpha;
currentState.alpha = alpha;
}
int getMaxCharacterCount() {
return currentState.maxCharacterCount;
}
void setMaxCharacterCount(int maxCharacterCount) {
overridingState.maxCharacterCount = maxCharacterCount;
currentState.maxCharacterCount = maxCharacterCount;
}
int getMaxNumber() {
return currentState.maxNumber;
}
void setMaxNumber(int maxNumber) {
overridingState.maxNumber = maxNumber;
currentState.maxNumber = maxNumber;
}
@ColorInt
int getBackgroundColor() {
return currentState.backgroundColor;
}
void setBackgroundColor(@ColorInt int backgroundColor) {
overridingState.backgroundColor = backgroundColor;
currentState.backgroundColor = backgroundColor;
}
@ColorInt
int getBadgeTextColor() {
return currentState.badgeTextColor;
}
void setBadgeTextColor(@ColorInt int badgeTextColor) {
overridingState.badgeTextColor = badgeTextColor;
currentState.badgeTextColor = badgeTextColor;
}
@StyleRes
int getTextAppearanceResId() {
return currentState.badgeTextAppearanceResId;
}
void setTextAppearanceResId(@StyleRes int textAppearanceResId) {
overridingState.badgeTextAppearanceResId = textAppearanceResId;
currentState.badgeTextAppearanceResId = textAppearanceResId;
}
int getBadgeShapeAppearanceResId() {
return currentState.badgeShapeAppearanceResId;
}
void setBadgeShapeAppearanceResId(int shapeAppearanceResId) {
overridingState.badgeShapeAppearanceResId = shapeAppearanceResId;
currentState.badgeShapeAppearanceResId = shapeAppearanceResId;
}
int getBadgeShapeAppearanceOverlayResId() {
return currentState.badgeShapeAppearanceOverlayResId;
}
void setBadgeShapeAppearanceOverlayResId(int shapeAppearanceOverlayResId) {
overridingState.badgeShapeAppearanceOverlayResId = shapeAppearanceOverlayResId;
currentState.badgeShapeAppearanceOverlayResId = shapeAppearanceOverlayResId;
}
int getBadgeWithTextShapeAppearanceResId() {
return currentState.badgeWithTextShapeAppearanceResId;
}
void setBadgeWithTextShapeAppearanceResId(int shapeAppearanceResId) {
overridingState.badgeWithTextShapeAppearanceResId = shapeAppearanceResId;
currentState.badgeWithTextShapeAppearanceResId = shapeAppearanceResId;
}
int getBadgeWithTextShapeAppearanceOverlayResId() {
return currentState.badgeWithTextShapeAppearanceOverlayResId;
}
void setBadgeWithTextShapeAppearanceOverlayResId(int shapeAppearanceOverlayResId) {
overridingState.badgeWithTextShapeAppearanceOverlayResId = shapeAppearanceOverlayResId;
currentState.badgeWithTextShapeAppearanceOverlayResId = shapeAppearanceOverlayResId;
}
@BadgeGravity
int getBadgeGravity() {
return currentState.badgeGravity;
}
void setBadgeGravity(@BadgeGravity int badgeGravity) {
overridingState.badgeGravity = badgeGravity;
currentState.badgeGravity = badgeGravity;
}
@Px
int getBadgeHorizontalPadding() {
return currentState.badgeHorizontalPadding;
}
void setBadgeHorizontalPadding(@Px int horizontalPadding) {
overridingState.badgeHorizontalPadding = horizontalPadding;
currentState.badgeHorizontalPadding = horizontalPadding;
}
@Px
int getBadgeVerticalPadding() {
return currentState.badgeVerticalPadding;
}
void setBadgeVerticalPadding(@Px int verticalPadding) {
overridingState.badgeVerticalPadding = verticalPadding;
currentState.badgeVerticalPadding = verticalPadding;
}
@Dimension(unit = Dimension.PX)
int getHorizontalOffsetWithoutText() {
return currentState.horizontalOffsetWithoutText;
}
void setHorizontalOffsetWithoutText(@Dimension(unit = Dimension.PX) int offset) {
overridingState.horizontalOffsetWithoutText = offset;
currentState.horizontalOffsetWithoutText = offset;
}
@Dimension(unit = Dimension.PX)
int getVerticalOffsetWithoutText() {
return currentState.verticalOffsetWithoutText;
}
void setVerticalOffsetWithoutText(@Dimension(unit = Dimension.PX) int offset) {
overridingState.verticalOffsetWithoutText = offset;
currentState.verticalOffsetWithoutText = offset;
}
@Dimension(unit = Dimension.PX)
int getHorizontalOffsetWithText() {
return currentState.horizontalOffsetWithText;
}
void setHorizontalOffsetWithText(@Dimension(unit = Dimension.PX) int offset) {
overridingState.horizontalOffsetWithText = offset;
currentState.horizontalOffsetWithText = offset;
}
@Dimension(unit = Dimension.PX)
int getVerticalOffsetWithText() {
return currentState.verticalOffsetWithText;
}
void setVerticalOffsetWithText(@Dimension(unit = Dimension.PX) int offset) {
overridingState.verticalOffsetWithText = offset;
currentState.verticalOffsetWithText = offset;
}
@Dimension(unit = Dimension.PX)
int getLargeFontVerticalOffsetAdjustment() {
return currentState.largeFontVerticalOffsetAdjustment;
}
void setLargeFontVerticalOffsetAdjustment(@Dimension(unit = Dimension.PX) int offsetAdjustment) {
overridingState.largeFontVerticalOffsetAdjustment = offsetAdjustment;
currentState.largeFontVerticalOffsetAdjustment = offsetAdjustment;
}
@Dimension(unit = Dimension.PX)
int getAdditionalHorizontalOffset() {
return currentState.additionalHorizontalOffset;
}
void setAdditionalHorizontalOffset(@Dimension(unit = Dimension.PX) int offset) {
overridingState.additionalHorizontalOffset = offset;
currentState.additionalHorizontalOffset = offset;
}
@Dimension(unit = Dimension.PX)
int getAdditionalVerticalOffset() {
return currentState.additionalVerticalOffset;
}
void setAdditionalVerticalOffset(@Dimension(unit = Dimension.PX) int offset) {
overridingState.additionalVerticalOffset = offset;
currentState.additionalVerticalOffset = offset;
}
CharSequence getContentDescriptionForText() {
return currentState.contentDescriptionForText;
}
void setContentDescriptionForText(CharSequence contentDescription) {
overridingState.contentDescriptionForText = contentDescription;
currentState.contentDescriptionForText = contentDescription;
}
CharSequence getContentDescriptionNumberless() {
return currentState.contentDescriptionNumberless;
}
void setContentDescriptionNumberless(CharSequence contentDescriptionNumberless) {
overridingState.contentDescriptionNumberless = contentDescriptionNumberless;
currentState.contentDescriptionNumberless = contentDescriptionNumberless;
}
@PluralsRes
int getContentDescriptionQuantityStrings() {
return currentState.contentDescriptionQuantityStrings;
}
void setContentDescriptionQuantityStringsResource(@PluralsRes int stringsResource) {
overridingState.contentDescriptionQuantityStrings = stringsResource;
currentState.contentDescriptionQuantityStrings = stringsResource;
}
@StringRes
int getContentDescriptionExceedsMaxBadgeNumberStringResource() {
return currentState.contentDescriptionExceedsMaxBadgeNumberRes;
}
void setContentDescriptionExceedsMaxBadgeNumberStringResource(@StringRes int stringsResource) {
overridingState.contentDescriptionExceedsMaxBadgeNumberRes = stringsResource;
currentState.contentDescriptionExceedsMaxBadgeNumberRes = stringsResource;
}
Locale getNumberLocale() {
return currentState.numberLocale;
}
void setNumberLocale(Locale locale) {
overridingState.numberLocale = locale;
currentState.numberLocale = locale;
}
boolean isAutoAdjustedToGrandparentBounds() {
return currentState.autoAdjustToWithinGrandparentBounds;
}
void setAutoAdjustToGrandparentBounds(boolean autoAdjustToGrandparentBounds) {
overridingState.autoAdjustToWithinGrandparentBounds = autoAdjustToGrandparentBounds;
currentState.autoAdjustToWithinGrandparentBounds = autoAdjustToGrandparentBounds;
}
private static int readColorFromAttributes(
Context context, @NonNull TypedArray a, @StyleableRes int index) {
return MaterialResources.getColorStateList(context, a, index).getDefaultColor();
}
/**
* Internal {@link Parcelable} state used to represent, save, and restore {@link BadgeDrawable}
* states.
*/
public static final class State implements Parcelable {
/** Value of -1 denotes a numberless badge. */
private static final int BADGE_NUMBER_NONE = -1;
/** Value of -2 denotes a not-set state. */
private static final int NOT_SET = -2;
@XmlRes private int badgeResId;
@ColorInt private Integer backgroundColor;
@ColorInt private Integer badgeTextColor;
@StyleRes private Integer badgeTextAppearanceResId;
@StyleRes private Integer badgeShapeAppearanceResId;
@StyleRes private Integer badgeShapeAppearanceOverlayResId;
@StyleRes private Integer badgeWithTextShapeAppearanceResId;
@StyleRes private Integer badgeWithTextShapeAppearanceOverlayResId;
private int alpha = 255;
@Nullable private String text;
private int number = NOT_SET;
private int maxCharacterCount = NOT_SET;
private int maxNumber = NOT_SET;
private Locale numberLocale;
@Nullable private CharSequence contentDescriptionForText;
@Nullable private CharSequence contentDescriptionNumberless;
@PluralsRes private int contentDescriptionQuantityStrings;
@StringRes private int contentDescriptionExceedsMaxBadgeNumberRes;
@BadgeGravity private Integer badgeGravity;
private Boolean isVisible = true;
@Px private Integer badgeHorizontalPadding;
@Px private Integer badgeVerticalPadding;
@Dimension(unit = Dimension.PX)
private Integer horizontalOffsetWithoutText;
@Dimension(unit = Dimension.PX)
private Integer verticalOffsetWithoutText;
@Dimension(unit = Dimension.PX)
private Integer horizontalOffsetWithText;
@Dimension(unit = Dimension.PX)
private Integer verticalOffsetWithText;
@Dimension(unit = Dimension.PX)
private Integer additionalHorizontalOffset;
@Dimension(unit = Dimension.PX)
private Integer additionalVerticalOffset;
@Dimension(unit = Dimension.PX)
private Integer largeFontVerticalOffsetAdjustment;
private Boolean autoAdjustToWithinGrandparentBounds;
public State() {}
State(@NonNull Parcel in) {
badgeResId = in.readInt();
backgroundColor = (Integer) in.readSerializable();
badgeTextColor = (Integer) in.readSerializable();
badgeTextAppearanceResId = (Integer) in.readSerializable();
badgeShapeAppearanceResId = (Integer) in.readSerializable();
badgeShapeAppearanceOverlayResId = (Integer) in.readSerializable();
badgeWithTextShapeAppearanceResId = (Integer) in.readSerializable();
badgeWithTextShapeAppearanceOverlayResId = (Integer) in.readSerializable();
alpha = in.readInt();
text = in.readString();
number = in.readInt();
maxCharacterCount = in.readInt();
maxNumber = in.readInt();
contentDescriptionForText = in.readString();
contentDescriptionNumberless = in.readString();
contentDescriptionQuantityStrings = in.readInt();
badgeGravity = (Integer) in.readSerializable();
badgeHorizontalPadding = (Integer) in.readSerializable();
badgeVerticalPadding = (Integer) in.readSerializable();
horizontalOffsetWithoutText = (Integer) in.readSerializable();
verticalOffsetWithoutText = (Integer) in.readSerializable();
horizontalOffsetWithText = (Integer) in.readSerializable();
verticalOffsetWithText = (Integer) in.readSerializable();
largeFontVerticalOffsetAdjustment = (Integer) in.readSerializable();
additionalHorizontalOffset = (Integer) in.readSerializable();
additionalVerticalOffset = (Integer) in.readSerializable();
isVisible = (Boolean) in.readSerializable();
numberLocale = (Locale) in.readSerializable();
autoAdjustToWithinGrandparentBounds = (Boolean) in.readSerializable();
}
public static final Creator<State> CREATOR =
new Creator<State>() {
@NonNull
@Override
public BadgeState.State createFromParcel(@NonNull Parcel in) {
return new State(in);
}
@NonNull
@Override
public State[] newArray(int size) {
return new State[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(badgeResId);
dest.writeSerializable(backgroundColor);
dest.writeSerializable(badgeTextColor);
dest.writeSerializable(badgeTextAppearanceResId);
dest.writeSerializable(badgeShapeAppearanceResId);
dest.writeSerializable(badgeShapeAppearanceOverlayResId);
dest.writeSerializable(badgeWithTextShapeAppearanceResId);
dest.writeSerializable(badgeWithTextShapeAppearanceOverlayResId);
dest.writeInt(alpha);
dest.writeString(text);
dest.writeInt(number);
dest.writeInt(maxCharacterCount);
dest.writeInt(maxNumber);
dest.writeString(
contentDescriptionForText != null ? contentDescriptionForText.toString() : null);
dest.writeString(
contentDescriptionNumberless != null ? contentDescriptionNumberless.toString() : null);
dest.writeInt(contentDescriptionQuantityStrings);
dest.writeSerializable(badgeGravity);
dest.writeSerializable(badgeHorizontalPadding);
dest.writeSerializable(badgeVerticalPadding);
dest.writeSerializable(horizontalOffsetWithoutText);
dest.writeSerializable(verticalOffsetWithoutText);
dest.writeSerializable(horizontalOffsetWithText);
dest.writeSerializable(verticalOffsetWithText);
dest.writeSerializable(largeFontVerticalOffsetAdjustment);
dest.writeSerializable(additionalHorizontalOffset);
dest.writeSerializable(additionalVerticalOffset);
dest.writeSerializable(isVisible);
dest.writeSerializable(numberLocale);
dest.writeSerializable(autoAdjustToWithinGrandparentBounds);
}
}
}