|
40 | 40 | import android.text.TextWatcher; |
41 | 41 | import android.view.MotionEvent; |
42 | 42 | import android.view.View; |
| 43 | +import android.view.View.OnAttachStateChangeListener; |
43 | 44 | import android.view.View.OnClickListener; |
44 | 45 | import android.view.View.OnFocusChangeListener; |
45 | 46 | import android.view.View.OnTouchListener; |
46 | 47 | import android.view.accessibility.AccessibilityEvent; |
47 | 48 | import android.view.accessibility.AccessibilityManager; |
48 | | -import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener; |
49 | 49 | import android.widget.AutoCompleteTextView; |
50 | 50 | import android.widget.AutoCompleteTextView.OnDismissListener; |
51 | 51 | import android.widget.EditText; |
|
54 | 54 | import androidx.annotation.NonNull; |
55 | 55 | import androidx.annotation.Nullable; |
56 | 56 | import androidx.core.view.ViewCompat; |
| 57 | +import androidx.core.view.accessibility.AccessibilityManagerCompat; |
| 58 | +import androidx.core.view.accessibility.AccessibilityManagerCompat.TouchExplorationStateChangeListener; |
57 | 59 | import androidx.core.view.accessibility.AccessibilityNodeInfoCompat; |
58 | 60 | import com.google.android.material.animation.AnimationUtils; |
59 | 61 | import com.google.android.material.color.MaterialColors; |
@@ -190,6 +192,38 @@ public void run() { |
190 | 192 | editText.setOnDismissListener(null); |
191 | 193 | } |
192 | 194 | } |
| 195 | + if (previousIcon == TextInputLayout.END_ICON_DROPDOWN_MENU) { |
| 196 | + textInputLayout.removeOnAttachStateChangeListener(onAttachStateChangeListener); |
| 197 | + removeTouchExplorationStateChangeListenerIfNeeded(); |
| 198 | + } |
| 199 | + } |
| 200 | + }; |
| 201 | + |
| 202 | + private final OnAttachStateChangeListener onAttachStateChangeListener = new OnAttachStateChangeListener() { |
| 203 | + @Override |
| 204 | + public void onViewAttachedToWindow(View ignored) { |
| 205 | + addTouchExplorationStateChangeListenerIfNeeded(); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public void onViewDetachedFromWindow(View ignored) { |
| 210 | + removeTouchExplorationStateChangeListenerIfNeeded(); |
| 211 | + } |
| 212 | + }; |
| 213 | + |
| 214 | + private final TouchExplorationStateChangeListener touchExplorationStateChangeListener = |
| 215 | + new TouchExplorationStateChangeListener() { |
| 216 | + @Override |
| 217 | + public void onTouchExplorationStateChanged(boolean enabled) { |
| 218 | + if (textInputLayout != null) { |
| 219 | + final AutoCompleteTextView autoCompleteTextView = |
| 220 | + (AutoCompleteTextView) textInputLayout.getEditText(); |
| 221 | + if (autoCompleteTextView != null && !isEditable(autoCompleteTextView)) { |
| 222 | + ViewCompat.setImportantForAccessibility( |
| 223 | + endIconView, |
| 224 | + enabled ? IMPORTANT_FOR_ACCESSIBILITY_NO : IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 225 | + } |
| 226 | + } |
193 | 227 | } |
194 | 228 | }; |
195 | 229 |
|
@@ -265,20 +299,8 @@ public void onClick(View v) { |
265 | 299 | initAnimators(); |
266 | 300 | accessibilityManager = |
267 | 301 | (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE); |
268 | | - if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) { |
269 | | - accessibilityManager.addTouchExplorationStateChangeListener( |
270 | | - new TouchExplorationStateChangeListener() { |
271 | | - @Override |
272 | | - public void onTouchExplorationStateChanged(boolean enabled) { |
273 | | - if (textInputLayout.getEditText() != null |
274 | | - && !isEditable(textInputLayout.getEditText())) { |
275 | | - ViewCompat.setImportantForAccessibility( |
276 | | - endIconView, |
277 | | - enabled ? IMPORTANT_FOR_ACCESSIBILITY_NO : IMPORTANT_FOR_ACCESSIBILITY_YES); |
278 | | - } |
279 | | - } |
280 | | - }); |
281 | | - } |
| 302 | + textInputLayout.addOnAttachStateChangeListener(onAttachStateChangeListener); |
| 303 | + addTouchExplorationStateChangeListenerIfNeeded(); |
282 | 304 | } |
283 | 305 |
|
284 | 306 | @Override |
@@ -530,4 +552,20 @@ public void onAnimationUpdate(@NonNull ValueAnimator animation) { |
530 | 552 |
|
531 | 553 | return animator; |
532 | 554 | } |
| 555 | + |
| 556 | + private void addTouchExplorationStateChangeListenerIfNeeded() { |
| 557 | + if (accessibilityManager != null |
| 558 | + && textInputLayout != null |
| 559 | + && ViewCompat.isAttachedToWindow(textInputLayout)) { |
| 560 | + AccessibilityManagerCompat.addTouchExplorationStateChangeListener( |
| 561 | + accessibilityManager, touchExplorationStateChangeListener); |
| 562 | + } |
| 563 | + } |
| 564 | + |
| 565 | + private void removeTouchExplorationStateChangeListenerIfNeeded() { |
| 566 | + if (accessibilityManager != null) { |
| 567 | + AccessibilityManagerCompat.removeTouchExplorationStateChangeListener( |
| 568 | + accessibilityManager, touchExplorationStateChangeListener); |
| 569 | + } |
| 570 | + } |
533 | 571 | } |
0 commit comments