Skip to content

Commit

Permalink
[SearchBar] Fix a11y issue where SearchBar darkens when focused with …
Browse files Browse the repository at this point in the history
…TalkBack

PiperOrigin-RevId: 679614664
  • Loading branch information
dsn5ft committed Oct 1, 2024
1 parent 246fa67 commit 97d957c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
35 changes: 0 additions & 35 deletions lib/java/com/google/android/material/search/SearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.EditText;
import android.widget.ImageButton;
Expand Down Expand Up @@ -148,10 +146,6 @@ public class SearchBar extends Toolbar {
private boolean defaultScrollFlagsEnabled;
private MaterialShapeDrawable backgroundShape;

@Nullable private final AccessibilityManager accessibilityManager;
private final TouchExplorationStateChangeListener touchExplorationStateChangeListener =
(boolean enabled) -> setFocusableInTouchMode(enabled);

public SearchBar(@NonNull Context context) {
this(context, null);
}
Expand Down Expand Up @@ -209,35 +203,6 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
ViewCompat.setElevation(this, elevation);
initTextView(textAppearanceResId, text, hint);
initBackground(shapeAppearanceModel, backgroundColor, elevation, strokeWidth, strokeColor);

accessibilityManager =
(AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
setupTouchExplorationStateChangeListener();
}

private void setupTouchExplorationStateChangeListener() {
if (accessibilityManager != null) {
// Handle the case where touch exploration is already enabled.
if (accessibilityManager.isEnabled() && accessibilityManager.isTouchExplorationEnabled()) {
setFocusableInTouchMode(true);
}

// Handle the case where touch exploration state can change while the view is active.
addOnAttachStateChangeListener(
new OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View ignored) {
accessibilityManager.addTouchExplorationStateChangeListener(
touchExplorationStateChangeListener);
}

@Override
public void onViewDetachedFromWindow(View ignored) {
accessibilityManager.removeTouchExplorationStateChangeListener(
touchExplorationStateChangeListener);
}
});
}
}

private void validateAttributes(@Nullable AttributeSet attributeSet) {
Expand Down
7 changes: 4 additions & 3 deletions lib/java/com/google/android/material/search/SearchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,10 @@ private void setTransitionState(
}

updateListeningForBackCallbacks(state);

if (searchBar != null && state == TransitionState.HIDDEN) {
searchBar.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
}
}

private void updateModalForAccessibility(@NonNull TransitionState state) {
Expand Down Expand Up @@ -932,9 +936,6 @@ public void clearFocusAndHideKeyboard() {
editText.post(
() -> {
editText.clearFocus();
if (searchBar != null) {
searchBar.requestFocus();
}
ViewUtils.hideKeyboard(editText, useWindowInsetsController);
});
}
Expand Down

0 comments on commit 97d957c

Please sign in to comment.