Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TextInputLayout] Fix for TextInputLayout leak via AccessibilityManager. #2718

Merged
merged 3 commits into from May 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}
}
}