Skip to content

Commit

Permalink
[M3][SearchBar] Fixed issue that caused the wrong component to be foc…
Browse files Browse the repository at this point in the history
…used on automatically when talkback is active.

PiperOrigin-RevId: 493910945
(cherry picked from commit e2c8cba)
  • Loading branch information
raajkumars authored and dsn5ft committed Dec 9, 2022
1 parent f707836 commit 8cbfd20
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
37 changes: 36 additions & 1 deletion lib/java/com/google/android/material/search/SearchBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.EditText;
import android.widget.ImageButton;
Expand All @@ -57,6 +58,8 @@
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.MarginLayoutParamsCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityManagerCompat;
import androidx.core.view.accessibility.AccessibilityManagerCompat.TouchExplorationStateChangeListener;
import androidx.core.widget.TextViewCompat;
import androidx.customview.view.AbsSavedState;
import com.google.android.material.appbar.AppBarLayout;
Expand Down Expand Up @@ -130,14 +133,17 @@ public class SearchBar extends Toolbar {
private final Drawable defaultNavigationIcon;
private final boolean tintNavigationIcon;
private final boolean forceDefaultNavigationOnClickListener;

@Nullable private View centerView;
@Nullable private Integer navigationIconTint;
@Nullable private Drawable originalNavigationIconBackground;
private int menuResId = -1;
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 @@ -194,6 +200,35 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
ViewCompat.setElevation(this, elevation);
initTextView(textAppearanceResId, text, hint);
initBackground(shapeAppearanceModel, 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) {
AccessibilityManagerCompat.addTouchExplorationStateChangeListener(
accessibilityManager, touchExplorationStateChangeListener);
}

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

private void validateAttributes(@Nullable AttributeSet attributeSet) {
Expand Down
23 changes: 18 additions & 5 deletions lib/java/com/google/android/material/search/SearchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
Expand Down Expand Up @@ -119,6 +120,7 @@
@SuppressWarnings("RestrictTo")
public class SearchView extends FrameLayout implements CoordinatorLayout.AttachedBehavior {

private static final long TALKBACK_FOCUS_CHANGE_DELAY_MS = 100;
private static final int DEF_STYLE_RES = R.style.Widget_Material3_SearchView;

final View scrim;
Expand Down Expand Up @@ -791,17 +793,28 @@ void requestFocusAndShowKeyboardIfNeeded() {

/** Requests focus on the main {@link EditText} and shows the soft keyboard. */
public void requestFocusAndShowKeyboard() {
editText.post(
// Without a delay requesting focus on edit text fails when talkback is active.
editText.postDelayed(
() -> {
editText.requestFocus();
if (editText.requestFocus()) {
// Workaround for talkback issue when clear button is clicked
editText.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
}
ViewUtils.showKeyboard(editText, useWindowInsetsController);
});
},
TALKBACK_FOCUS_CHANGE_DELAY_MS);
}

/** Clears focus on the main {@link EditText} and hides the soft keyboard. */
public void clearFocusAndHideKeyboard() {
editText.clearFocus();
ViewUtils.hideKeyboard(editText, useWindowInsetsController);
editText.post(
() -> {
editText.clearFocus();
if (searchBar != null) {
searchBar.requestFocus();
}
ViewUtils.hideKeyboard(editText, useWindowInsetsController);
});
}

boolean isAdjustNothingSoftInputMode() {
Expand Down

0 comments on commit 8cbfd20

Please sign in to comment.