2222import static com .google .android .material .timepicker .TimePickerView .GENERIC_VIEW_ACCESSIBILITY_CLASS_NAME ;
2323
2424import android .content .Context ;
25- import android .content .res .ColorStateList ;
2625import android .content .res .Configuration ;
2726import android .os .Build .VERSION ;
2827import android .os .Build .VERSION_CODES ;
3736import android .widget .EditText ;
3837import android .widget .FrameLayout ;
3938import android .widget .TextView ;
40- import androidx .annotation .ColorInt ;
4139import androidx .annotation .NonNull ;
4240import androidx .annotation .Nullable ;
4341import androidx .annotation .VisibleForTesting ;
4442import androidx .core .view .AccessibilityDelegateCompat ;
4543import androidx .core .view .ViewCompat ;
4644import androidx .core .view .accessibility .AccessibilityNodeInfoCompat ;
4745import com .google .android .material .chip .Chip ;
48- import com .google .android .material .color .MaterialColors ;
4946import com .google .android .material .internal .TextWatcherAdapter ;
5047import com .google .android .material .internal .ViewUtils ;
5148import com .google .android .material .textfield .TextInputLayout ;
@@ -62,15 +59,6 @@ class ChipTextInputComboView extends FrameLayout implements Checkable {
6259 private final EditText editText ;
6360 private TextWatcher watcher ;
6461 private TextView label ;
65- private CharSequence chipText = "" ;
66-
67- private boolean hasError = false ;
68- private ColorStateList originalChipBackgroundColor ;
69- private ColorStateList originalChipTextColor ;
70- private ColorStateList originalEditTextColor ;
71- private ColorStateList originalEditTextCursorColor ;
72- private ColorStateList originalLabelColor ;
73- @ ColorInt private int originalChipStrokeColor ;
7462
7563 public ChipTextInputComboView (@ NonNull Context context ) {
7664 this (context , null );
@@ -117,14 +105,10 @@ public boolean isChecked() {
117105 @ Override
118106 public void setChecked (boolean checked ) {
119107 chip .setChecked (checked );
120- if (checked ) {
121- chip .setText ("" );
122- chip .setImportantForAccessibility (View .IMPORTANT_FOR_ACCESSIBILITY_NO );
123- } else {
124- chip .setText (chipText );
125- chip .setImportantForAccessibility (View .IMPORTANT_FOR_ACCESSIBILITY_YES );
126- }
127108 editText .setVisibility (checked ? VISIBLE : INVISIBLE );
109+ // TODO(b/247609386) Should not hide chip, we need the background in M3 (but not M2...).
110+ // Instead, the text in chip should be hidden.
111+ chip .setVisibility (checked ? GONE : VISIBLE );
128112 if (isChecked ()) {
129113 ViewUtils .requestFocusAndShowKeyboard (editText , /* useWindowInsetsController= */ false );
130114 }
@@ -137,21 +121,30 @@ public void toggle() {
137121
138122 public void setText (CharSequence text ) {
139123 String formattedText = formatText (text );
140- chipText = formattedText ;
141124 chip .setText (formattedText );
142125 if (!isEmpty (formattedText )) {
143126 editText .removeTextChangedListener (watcher );
144127
145128 editText .setText (formattedText );
146- setAccessibilityDelegate (editText , chipText .toString (), label .getText ());
129+ ViewCompat .setAccessibilityDelegate (
130+ editText ,
131+ new AccessibilityDelegateCompat () {
132+ @ Override
133+ public void onInitializeAccessibilityNodeInfo (
134+ @ NonNull View host , @ NonNull AccessibilityNodeInfoCompat info ) {
135+ super .onInitializeAccessibilityNodeInfo (host , info );
136+ info .setText (formattedText );
137+ info .setHintText (label .getText ());
138+ }
139+ });
147140
148141 editText .addTextChangedListener (watcher );
149142 }
150143 }
151144
152145 @ VisibleForTesting
153146 CharSequence getChipText () {
154- return chipText ;
147+ return chip . getText () ;
155148 }
156149
157150 private String formatText (CharSequence text ) {
@@ -191,91 +184,21 @@ public void setChipDelegate(AccessibilityDelegateCompat clickActionDelegate) {
191184 ViewCompat .setAccessibilityDelegate (chip , clickActionDelegate );
192185 }
193186
194- public void setError (boolean hasError ) {
195- if (this .hasError == hasError ) {
196- return ;
197- }
198- this .hasError = hasError ;
199-
200- if (hasError ) {
201- applyErrorColors ();
202- } else {
203- clearErrorColors ();
204- }
205- }
206-
207- private void applyErrorColors () {
208- originalChipBackgroundColor = chip .getChipBackgroundColor ();
209- originalChipTextColor = chip .getTextColors ();
210- originalEditTextColor = editText .getTextColors ();
211- originalLabelColor = label .getTextColors ();
212- originalChipStrokeColor = textInputLayout .getBoxStrokeColor ();
213-
214- // TODO(b/394610420): tokens and ColorStateList with error state
215- ColorStateList colorErrorContainer =
216- MaterialColors .getColorStateListOrNull (getContext (), R .attr .colorErrorContainer );
217- ColorStateList colorOnErrorContainer =
218- MaterialColors .getColorStateListOrNull (getContext (), R .attr .colorOnErrorContainer );
219- if (colorErrorContainer != null && colorOnErrorContainer != null ) {
220- chip .setChipBackgroundColor (colorErrorContainer );
221- chip .setTextColor (colorOnErrorContainer );
222- editText .setTextColor (colorOnErrorContainer );
223- textInputLayout .setBoxStrokeColor (colorOnErrorContainer .getDefaultColor ());
224- label .setTextColor (colorOnErrorContainer );
225- if (VERSION .SDK_INT >= VERSION_CODES .Q ) {
226- originalEditTextCursorColor = textInputLayout .getCursorColor ();
227- textInputLayout .setCursorColor (colorOnErrorContainer );
228- }
229- }
230- }
231-
232- private void clearErrorColors () {
233- chip .setChipBackgroundColor (originalChipBackgroundColor );
234- chip .setTextColor (originalChipTextColor );
235- editText .setTextColor (originalEditTextColor );
236- textInputLayout .setBoxStrokeColor (originalChipStrokeColor );
237- label .setTextColor (originalLabelColor );
238- if (VERSION .SDK_INT >= VERSION_CODES .Q ) {
239- textInputLayout .setCursorColor (originalEditTextCursorColor );
240- }
241- }
242-
243- public boolean hasError () {
244- return hasError ;
245- }
246-
247187 private class TextFormatter extends TextWatcherAdapter {
248188
249189 private static final String DEFAULT_TEXT = "00" ;
250190
251191 @ Override
252192 public void afterTextChanged (Editable editable ) {
253193 if (isEmpty (editable )) {
254- chipText = formatText (DEFAULT_TEXT );
194+ chip . setText ( formatText (DEFAULT_TEXT ) );
255195 return ;
256196 }
257197 String formattedText = formatText (editable );
258- chipText = isEmpty (formattedText ) ? formatText (DEFAULT_TEXT ) : formattedText ;
259- setAccessibilityDelegate (editText , editable .toString (), label .getText ());
198+ chip .setText (isEmpty (formattedText ) ? formatText (DEFAULT_TEXT ) : formattedText );
260199 }
261200 }
262201
263- private void setAccessibilityDelegate (
264- @ NonNull View view , CharSequence text , @ Nullable CharSequence hint ) {
265- ViewCompat .setAccessibilityDelegate (
266- view ,
267- new AccessibilityDelegateCompat () {
268- @ Override
269- public void onInitializeAccessibilityNodeInfo (
270- @ NonNull View host , @ NonNull AccessibilityNodeInfoCompat info ) {
271- super .onInitializeAccessibilityNodeInfo (host , info );
272- info .setText (text );
273- info .setHintText (hint );
274- info .setMaxTextLength (2 );
275- }
276- });
277- }
278-
279202 @ Override
280203 protected void onConfigurationChanged (Configuration newConfig ) {
281204 super .onConfigurationChanged (newConfig );
0 commit comments