Skip to content

Commit 0949e9c

Browse files
afohrmandsn5ft
authored andcommitted
[Adaptive][Side Sheet] Set accessibility focus to the side sheet when expanded.
PiperOrigin-RevId: 493938007 (cherry picked from commit 48a3a75)
1 parent 305e19d commit 0949e9c

File tree

1 file changed

+10
-54
lines changed

1 file changed

+10
-54
lines changed

lib/java/com/google/android/material/sidesheet/SideSheetBehavior.java

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import android.content.Context;
2525
import android.content.res.ColorStateList;
2626
import android.content.res.TypedArray;
27-
import android.os.Build.VERSION;
28-
import android.os.Build.VERSION_CODES;
2927
import android.os.Parcel;
3028
import android.os.Parcelable;
3129
import android.util.AttributeSet;
@@ -56,9 +54,7 @@
5654
import com.google.android.material.shape.MaterialShapeDrawable;
5755
import com.google.android.material.shape.ShapeAppearanceModel;
5856
import java.lang.ref.WeakReference;
59-
import java.util.HashMap;
6057
import java.util.LinkedHashSet;
61-
import java.util.Map;
6258
import java.util.Set;
6359

6460
/**
@@ -78,8 +74,6 @@ public class SideSheetBehavior<V extends View> extends CoordinatorLayout.Behavio
7874

7975
private static final int NO_MAX_SIZE = -1;
8076

81-
private static final boolean UPDATE_IMPORTANT_FOR_ACCESSIBILITY_ON_SIBLINGS = false;
82-
8377
private float maximumVelocity;
8478

8579
@Nullable private MaterialShapeDrawable materialShapeDrawable;
@@ -118,8 +112,6 @@ public class SideSheetBehavior<V extends View> extends CoordinatorLayout.Behavio
118112

119113
@NonNull private final Set<SideSheetCallback> callbacks = new LinkedHashSet<>();
120114

121-
@Nullable private Map<View, Integer> importantForAccessibilityMap;
122-
123115
public SideSheetBehavior() {}
124116

125117
public SideSheetBehavior(@NonNull Context context, @Nullable AttributeSet attrs) {
@@ -574,9 +566,7 @@ void setStateInternal(@SheetState int state) {
574566
}
575567

576568
if (state == STATE_EXPANDED) {
577-
updateImportantForAccessibility(true);
578-
} else if (state == STATE_HIDDEN) {
579-
updateImportantForAccessibility(false);
569+
updateAccessibilityFocusOnExpansion();
580570
}
581571

582572
for (SheetCallback callback : callbacks) {
@@ -828,53 +818,19 @@ public static <V extends View> SideSheetBehavior<V> from(@NonNull V view) {
828818
return (SideSheetBehavior<V>) behavior;
829819
}
830820

831-
private void updateImportantForAccessibility(boolean expanded) {
821+
private void updateAccessibilityFocusOnExpansion() {
832822
if (viewRef == null) {
833823
return;
834824
}
835-
836-
ViewParent viewParent = viewRef.get().getParent();
837-
if (!(viewParent instanceof CoordinatorLayout)) {
838-
return;
839-
}
840-
841-
CoordinatorLayout parent = (CoordinatorLayout) viewParent;
842-
final int childCount = parent.getChildCount();
843-
if ((VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) && expanded) {
844-
if (importantForAccessibilityMap == null) {
845-
importantForAccessibilityMap = new HashMap<>(childCount);
846-
} else {
847-
// The important for accessibility values of the child views have been saved already.
848-
return;
849-
}
850-
}
851-
852-
for (int i = 0; i < childCount; i++) {
853-
final View child = parent.getChildAt(i);
854-
if (child == viewRef.get()) {
855-
continue;
856-
}
857-
if (expanded) {
858-
// Saves the important for accessibility value of the child view.
859-
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
860-
importantForAccessibilityMap.put(child, child.getImportantForAccessibility());
861-
}
862-
if (UPDATE_IMPORTANT_FOR_ACCESSIBILITY_ON_SIBLINGS) {
863-
ViewCompat.setImportantForAccessibility(
864-
child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
865-
// If the siblings of the sheet have been set to not important for a11y, move the focus
866-
// to the sheet when expanded.
867-
viewRef.get().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
868-
}
869-
} else {
870-
if (UPDATE_IMPORTANT_FOR_ACCESSIBILITY_ON_SIBLINGS
871-
&& importantForAccessibilityMap != null
872-
&& importantForAccessibilityMap.containsKey(child)) {
873-
// Restores the original important for accessibility value of the child view.
874-
ViewCompat.setImportantForAccessibility(child, importantForAccessibilityMap.get(child));
875-
}
876-
importantForAccessibilityMap = null;
825+
View view = viewRef.get();
826+
if (view instanceof ViewGroup && ((ViewGroup) view).getChildCount() > 0) {
827+
ViewGroup viewContainer = (ViewGroup) view;
828+
View firstNestedChild = viewContainer.getChildAt(0);
829+
if (firstNestedChild != null) {
830+
firstNestedChild.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
877831
}
832+
} else {
833+
view.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
878834
}
879835
}
880836

0 commit comments

Comments
 (0)