Skip to content

Commit c4871f4

Browse files
dsn5fthunterstich
authored andcommitted
[Bottom Sheet] Update a11y actions and drag handle click to not include collapsed state if skipCollapsed=true && hideable=true
PiperOrigin-RevId: 883185600
1 parent 94364b3 commit c4871f4

4 files changed

Lines changed: 63 additions & 18 deletions

File tree

lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,10 @@ public boolean getSkipCollapsed() {
12461246
return skipCollapsed;
12471247
}
12481248

1249+
boolean canCollapse() {
1250+
return !skipCollapsed || !hideable;
1251+
}
1252+
12491253
/**
12501254
* Sets whether this bottom sheet can be collapsed/expanded by dragging. Note: When disabling
12511255
* dragging, an app will require to implement a custom way to expand/collapse the bottom sheet
@@ -2503,18 +2507,22 @@ private void updateAccessibilityActions(View view, int viewIndex) {
25032507
switch (state) {
25042508
case STATE_EXPANDED:
25052509
{
2506-
collapseActionIds.put(
2507-
viewIndex,
2508-
addAccessibilityActionForState(
2509-
view, R.string.bottomsheet_action_collapse, STATE_COLLAPSED));
2510+
if (canCollapse()) {
2511+
collapseActionIds.put(
2512+
viewIndex,
2513+
addAccessibilityActionForState(
2514+
view, R.string.bottomsheet_action_collapse, STATE_COLLAPSED));
2515+
}
25102516
break;
25112517
}
25122518
case STATE_HALF_EXPANDED:
25132519
{
2514-
collapseActionIds.put(
2515-
viewIndex,
2516-
addAccessibilityActionForState(
2517-
view, R.string.bottomsheet_action_collapse, STATE_COLLAPSED));
2520+
if (canCollapse()) {
2521+
collapseActionIds.put(
2522+
viewIndex,
2523+
addAccessibilityActionForState(
2524+
view, R.string.bottomsheet_action_collapse, STATE_COLLAPSED));
2525+
}
25182526
expandActionIds.put(
25192527
viewIndex,
25202528
addAccessibilityActionForState(

lib/java/com/google/android/material/bottomsheet/BottomSheetDragHandleView.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,18 @@ private int getNextState() {
326326
: BottomSheetBehavior.STATE_EXPANDED;
327327
break;
328328
case BottomSheetBehavior.STATE_EXPANDED:
329-
nextState =
330-
canHalfExpand
331-
? BottomSheetBehavior.STATE_HALF_EXPANDED
332-
: BottomSheetBehavior.STATE_COLLAPSED;
329+
if (canHalfExpand) {
330+
nextState = BottomSheetBehavior.STATE_HALF_EXPANDED;
331+
} else if (bottomSheetBehavior.canCollapse()) {
332+
nextState = BottomSheetBehavior.STATE_COLLAPSED;
333+
}
333334
break;
334335
case BottomSheetBehavior.STATE_HALF_EXPANDED:
335-
nextState =
336-
clickToExpand
337-
? BottomSheetBehavior.STATE_EXPANDED
338-
: BottomSheetBehavior.STATE_COLLAPSED;
336+
if (clickToExpand) {
337+
nextState = BottomSheetBehavior.STATE_EXPANDED;
338+
} else if (bottomSheetBehavior.canCollapse()) {
339+
nextState = BottomSheetBehavior.STATE_COLLAPSED;
340+
}
339341
break;
340342
default: // fall out
341343
}

lib/javatests/com/google/android/material/bottomsheet/BottomSheetDragHandleTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,40 @@ public void test_customActionSetInExpandedStateWhenHideable() {
415415
assertThat(hasAccessibilityAction(dragHandleView, ACTION_DISMISS.getId())).isTrue();
416416
}
417417

418+
@Test
419+
public void test_customActionSetInExpandedStateWhenHideableAndSkipCollapsed() {
420+
activity.bottomSheetBehavior.setHideable(true);
421+
activity.bottomSheetBehavior.setSkipCollapsed(true);
422+
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
423+
activity.addViewToBottomSheet(dragHandleView);
424+
shadowOf(accessibilityManager).setEnabled(true);
425+
426+
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
427+
428+
BottomSheetBehavior<View> behavior = activity.bottomSheetBehavior;
429+
assertThat(hasCustomAccessibilityAction(behavior.expandHalfwayActionIds)).isFalse();
430+
assertThat(hasCustomAccessibilityAction(behavior.expandActionIds)).isFalse();
431+
assertThat(hasCustomAccessibilityAction(behavior.collapseActionIds)).isFalse();
432+
assertThat(hasAccessibilityAction(dragHandleView, ACTION_DISMISS.getId())).isTrue();
433+
}
434+
435+
@Test
436+
public void test_customActionSetInExpandedStateWhenNotHideableAndSkipCollapsed() {
437+
activity.bottomSheetBehavior.setHideable(false);
438+
activity.bottomSheetBehavior.setSkipCollapsed(true);
439+
activity.bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
440+
activity.addViewToBottomSheet(dragHandleView);
441+
shadowOf(accessibilityManager).setEnabled(true);
442+
443+
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
444+
445+
BottomSheetBehavior<View> behavior = activity.bottomSheetBehavior;
446+
assertThat(hasCustomAccessibilityAction(behavior.expandHalfwayActionIds)).isFalse();
447+
assertThat(hasCustomAccessibilityAction(behavior.expandActionIds)).isFalse();
448+
assertThat(hasCustomAccessibilityAction(behavior.collapseActionIds)).isTrue();
449+
assertThat(hasAccessibilityAction(dragHandleView, ACTION_DISMISS.getId())).isFalse();
450+
}
451+
418452
private void assertImportantForAccessibility(boolean important) {
419453
if (important) {
420454
assertThat(dragHandleView.getImportantForAccessibility())

tests/javatests/com/google/android/material/bottomsheet/BottomSheetBehaviorTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,8 +1149,9 @@ private static void assertAccessibilityActions(
11491149
boolean hasHalfExpandAction =
11501150
state != BottomSheetBehavior.STATE_HALF_EXPANDED && !behavior.isFitToContents();
11511151
boolean hasCollapseAction =
1152-
state == BottomSheetBehavior.STATE_EXPANDED
1153-
|| state == BottomSheetBehavior.STATE_HALF_EXPANDED;
1152+
(state == BottomSheetBehavior.STATE_EXPANDED
1153+
|| state == BottomSheetBehavior.STATE_HALF_EXPANDED)
1154+
&& (!behavior.getSkipCollapsed() || !behavior.isHideable());
11541155
boolean hasDismissAction = state != BottomSheetBehavior.STATE_HIDDEN && behavior.isHideable();
11551156
assertThat(
11561157
hasCustomAccessibilityAction(behavior.expandActionIds),

0 commit comments

Comments
 (0)