Skip to content

Commit

Permalink
[TextInputLayout] Fix for TextInputLayout leak via AccessibilityManag…
Browse files Browse the repository at this point in the history
…er. (#2718)

[TextInputLayout] Fix for TextInputLayout leak via AccessibilityManager.

Adaptation of 673cefc for 1.6

Resolves #2615
  • Loading branch information
dsn5ft committed May 27, 2022
1 parent 768d0cf commit 948d5da
Showing 1 changed file with 53 additions and 15 deletions.
Expand Up @@ -40,12 +40,12 @@
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener;
import android.widget.AutoCompleteTextView;
import android.widget.AutoCompleteTextView.OnDismissListener;
import android.widget.EditText;
Expand All @@ -54,6 +54,8 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityManagerCompat;
import androidx.core.view.accessibility.AccessibilityManagerCompat.TouchExplorationStateChangeListener;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import com.google.android.material.animation.AnimationUtils;
import com.google.android.material.color.MaterialColors;
Expand Down Expand Up @@ -190,6 +192,38 @@ public void run() {
editText.setOnDismissListener(null);
}
}
if (previousIcon == TextInputLayout.END_ICON_DROPDOWN_MENU) {
textInputLayout.removeOnAttachStateChangeListener(onAttachStateChangeListener);
removeTouchExplorationStateChangeListenerIfNeeded();
}
}
};

private final OnAttachStateChangeListener onAttachStateChangeListener = new OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View ignored) {
addTouchExplorationStateChangeListenerIfNeeded();
}

@Override
public void onViewDetachedFromWindow(View ignored) {
removeTouchExplorationStateChangeListenerIfNeeded();
}
};

private final TouchExplorationStateChangeListener touchExplorationStateChangeListener =
new TouchExplorationStateChangeListener() {
@Override
public void onTouchExplorationStateChanged(boolean enabled) {
if (textInputLayout != null) {
final AutoCompleteTextView autoCompleteTextView =
(AutoCompleteTextView) textInputLayout.getEditText();
if (autoCompleteTextView != null && !isEditable(autoCompleteTextView)) {
ViewCompat.setImportantForAccessibility(
endIconView,
enabled ? IMPORTANT_FOR_ACCESSIBILITY_NO : IMPORTANT_FOR_ACCESSIBILITY_YES);
}
}
}
};

Expand Down Expand Up @@ -265,20 +299,8 @@ public void onClick(View v) {
initAnimators();
accessibilityManager =
(AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
accessibilityManager.addTouchExplorationStateChangeListener(
new TouchExplorationStateChangeListener() {
@Override
public void onTouchExplorationStateChanged(boolean enabled) {
if (textInputLayout.getEditText() != null
&& !isEditable(textInputLayout.getEditText())) {
ViewCompat.setImportantForAccessibility(
endIconView,
enabled ? IMPORTANT_FOR_ACCESSIBILITY_NO : IMPORTANT_FOR_ACCESSIBILITY_YES);
}
}
});
}
textInputLayout.addOnAttachStateChangeListener(onAttachStateChangeListener);
addTouchExplorationStateChangeListenerIfNeeded();
}

@Override
Expand Down Expand Up @@ -530,4 +552,20 @@ public void onAnimationUpdate(@NonNull ValueAnimator animation) {

return animator;
}

private void addTouchExplorationStateChangeListenerIfNeeded() {
if (accessibilityManager != null
&& textInputLayout != null
&& ViewCompat.isAttachedToWindow(textInputLayout)) {
AccessibilityManagerCompat.addTouchExplorationStateChangeListener(
accessibilityManager, touchExplorationStateChangeListener);
}
}

private void removeTouchExplorationStateChangeListenerIfNeeded() {
if (accessibilityManager != null) {
AccessibilityManagerCompat.removeTouchExplorationStateChangeListener(
accessibilityManager, touchExplorationStateChangeListener);
}
}
}

0 comments on commit 948d5da

Please sign in to comment.