Skip to content

Commit 95e7b45

Browse files
Material Design Teamkendrickumstattd
authored andcommitted
[BottomSheet][A11y] Add half-expanded state description for accessibility
PiperOrigin-RevId: 765144604
1 parent 7428b2b commit 95e7b45

File tree

86 files changed

+360
-240
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+360
-240
lines changed

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ void onLayout(@NonNull View bottomSheet) {}
231231

232232
private static final int NO_MAX_SIZE = -1;
233233

234-
private static final int VIEW_INDEX_BOTTOM_SHEET = 0;
234+
@VisibleForTesting
235+
static final int VIEW_INDEX_BOTTOM_SHEET = 0;
235236

236237
private static final int INVALID_POSITION = -1;
237238

@@ -352,6 +353,10 @@ void onLayout(@NonNull View bottomSheet) {}
352353

353354
@VisibleForTesting
354355
final SparseIntArray expandHalfwayActionIds = new SparseIntArray();
356+
@VisibleForTesting
357+
final SparseIntArray expandActionIds = new SparseIntArray();
358+
@VisibleForTesting
359+
final SparseIntArray collapseActionIds = new SparseIntArray();
355360

356361
public BottomSheetBehavior() {}
357362

@@ -2409,24 +2414,30 @@ private void updateAccessibilityActions(View view, int viewIndex) {
24092414
switch (state) {
24102415
case STATE_EXPANDED:
24112416
{
2412-
int nextState = fitToContents ? STATE_COLLAPSED : STATE_HALF_EXPANDED;
2413-
replaceAccessibilityActionForState(
2414-
view, AccessibilityActionCompat.ACTION_COLLAPSE, nextState);
2417+
collapseActionIds.put(
2418+
viewIndex,
2419+
addAccessibilityActionForState(
2420+
view, R.string.bottomsheet_action_collapse, STATE_COLLAPSED));
24152421
break;
24162422
}
24172423
case STATE_HALF_EXPANDED:
24182424
{
2419-
replaceAccessibilityActionForState(
2420-
view, AccessibilityActionCompat.ACTION_COLLAPSE, STATE_COLLAPSED);
2421-
replaceAccessibilityActionForState(
2422-
view, AccessibilityActionCompat.ACTION_EXPAND, STATE_EXPANDED);
2425+
collapseActionIds.put(
2426+
viewIndex,
2427+
addAccessibilityActionForState(
2428+
view, R.string.bottomsheet_action_collapse, STATE_COLLAPSED));
2429+
expandActionIds.put(
2430+
viewIndex,
2431+
addAccessibilityActionForState(
2432+
view, R.string.bottomsheet_action_expand, STATE_EXPANDED));
24232433
break;
24242434
}
24252435
case STATE_COLLAPSED:
24262436
{
2427-
int nextState = fitToContents ? STATE_EXPANDED : STATE_HALF_EXPANDED;
2428-
replaceAccessibilityActionForState(
2429-
view, AccessibilityActionCompat.ACTION_EXPAND, nextState);
2437+
expandActionIds.put(
2438+
viewIndex,
2439+
addAccessibilityActionForState(
2440+
view, R.string.bottomsheet_action_expand, STATE_EXPANDED));
24302441
break;
24312442
}
24322443
case STATE_HIDDEN:
@@ -2440,15 +2451,27 @@ private void clearAccessibilityAction(View view, int viewIndex) {
24402451
if (view == null) {
24412452
return;
24422453
}
2454+
ViewCompat.removeAccessibilityAction(view, AccessibilityNodeInfoCompat.ACTION_DISMISS);
24432455
ViewCompat.removeAccessibilityAction(view, AccessibilityNodeInfoCompat.ACTION_COLLAPSE);
24442456
ViewCompat.removeAccessibilityAction(view, AccessibilityNodeInfoCompat.ACTION_EXPAND);
2445-
ViewCompat.removeAccessibilityAction(view, AccessibilityNodeInfoCompat.ACTION_DISMISS);
2457+
2458+
int expandActionId = expandActionIds.get(viewIndex, View.NO_ID);
2459+
if (expandActionId != View.NO_ID) {
2460+
ViewCompat.removeAccessibilityAction(view, expandActionId);
2461+
expandActionIds.delete(viewIndex);
2462+
}
24462463

24472464
int expandHalfwayActionId = expandHalfwayActionIds.get(viewIndex, View.NO_ID);
24482465
if (expandHalfwayActionId != View.NO_ID) {
24492466
ViewCompat.removeAccessibilityAction(view, expandHalfwayActionId);
24502467
expandHalfwayActionIds.delete(viewIndex);
24512468
}
2469+
2470+
int collapseActionId = collapseActionIds.get(viewIndex, View.NO_ID);
2471+
if (collapseActionId != View.NO_ID) {
2472+
ViewCompat.removeAccessibilityAction(view, collapseActionId);
2473+
collapseActionIds.delete(viewIndex);
2474+
}
24522475
}
24532476

24542477
private void replaceAccessibilityActionForState(

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

Lines changed: 89 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import android.os.Handler;
2727
import android.os.Looper;
2828
import androidx.appcompat.widget.AppCompatImageView;
29+
import android.text.TextUtils;
2930
import android.util.AttributeSet;
3031
import android.view.GestureDetector;
3132
import android.view.GestureDetector.OnGestureListener;
@@ -42,6 +43,7 @@
4243
import androidx.coordinatorlayout.widget.CoordinatorLayout;
4344
import androidx.core.view.AccessibilityDelegateCompat;
4445
import androidx.core.view.ViewCompat;
46+
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
4547
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat;
4648
import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback;
4749

@@ -50,8 +52,8 @@
5052
* BottomSheetBehavior}. This view will automatically handle the accessibility interaction when the
5153
* accessibility service is enabled. When you add a drag handle to a bottom sheet and the user
5254
* enables the accessibility service, the drag handle will become important for accessibility and
53-
* clickable. Clicking the drag handle will toggle the bottom sheet between its collapsed and
54-
* expanded states.
55+
* clickable. Clicking the drag handle will toggle the bottom sheet between its collapsed,
56+
* half expanded, and expanded states.
5557
*/
5658
public class BottomSheetDragHandleView extends AppCompatImageView implements
5759
AccessibilityStateChangeListener {
@@ -76,9 +78,11 @@ public class BottomSheetDragHandleView extends AppCompatImageView implements
7678
private boolean hasClickListener = false;
7779

7880
private final String clickToExpandActionLabel =
79-
getResources().getString(R.string.bottomsheet_action_expand);
81+
getResources().getString(R.string.bottomsheet_action_expand_description);
82+
private final String clickToHalfExpandActionLabel =
83+
getResources().getString(R.string.bottomsheet_action_half_expand_description);
8084
private final String clickToCollapseActionLabel =
81-
getResources().getString(R.string.bottomsheet_action_collapse);
85+
getResources().getString(R.string.bottomsheet_action_collapse_description);
8286

8387
private final BottomSheetCallback bottomSheetCallback =
8488
new BottomSheetCallback() {
@@ -152,6 +156,39 @@ public void onPopulateAccessibilityEvent(View host, @NonNull AccessibilityEvent
152156
expandOrCollapseBottomSheetIfPossible();
153157
}
154158
}
159+
160+
@Override
161+
public void onInitializeAccessibilityNodeInfo(
162+
@NonNull View host, @NonNull AccessibilityNodeInfoCompat info) {
163+
super.onInitializeAccessibilityNodeInfo(host, info);
164+
if (!hasAttachedBehavior()) {
165+
return;
166+
}
167+
168+
CharSequence originalDescription = getContentDescription();
169+
String stateName = null;
170+
171+
switch (bottomSheetBehavior.getState()) {
172+
case BottomSheetBehavior.STATE_COLLAPSED:
173+
stateName = getResources().getString(R.string.bottomsheet_state_collapsed);
174+
break;
175+
case BottomSheetBehavior.STATE_EXPANDED:
176+
stateName = getResources().getString(R.string.bottomsheet_state_expanded);
177+
break;
178+
case BottomSheetBehavior.STATE_HALF_EXPANDED:
179+
stateName = getResources().getString(R.string.bottomsheet_state_half_expanded);
180+
break;
181+
default: // fall out
182+
}
183+
184+
if (!TextUtils.isEmpty(stateName)) {
185+
CharSequence newDescription =
186+
TextUtils.isEmpty(originalDescription)
187+
? stateName
188+
: stateName + ". " + originalDescription;
189+
info.setContentDescription(newDescription);
190+
}
191+
}
155192
});
156193
}
157194

@@ -225,10 +262,24 @@ private void onBottomSheetStateChanged(@BottomSheetBehavior.State int state) {
225262
} else if (state == BottomSheetBehavior.STATE_EXPANDED) {
226263
clickToExpand = false;
227264
} // Else keep the original settings
265+
int nextState = getNextState();
266+
String text = null;
267+
switch (nextState) {
268+
case BottomSheetBehavior.STATE_COLLAPSED:
269+
text = clickToCollapseActionLabel;
270+
break;
271+
case BottomSheetBehavior.STATE_EXPANDED:
272+
text = clickToExpandActionLabel;
273+
break;
274+
case BottomSheetBehavior.STATE_HALF_EXPANDED:
275+
text = clickToHalfExpandActionLabel;
276+
break;
277+
default: // fall out
278+
}
228279
ViewCompat.replaceAccessibilityAction(
229280
this,
230281
AccessibilityActionCompat.ACTION_CLICK,
231-
clickToExpand ? clickToExpandActionLabel : clickToCollapseActionLabel,
282+
text,
232283
(v, args) -> expandOrCollapseBottomSheetIfPossible());
233284
}
234285

@@ -250,27 +301,44 @@ private boolean expandOrCollapseBottomSheetIfPossible() {
250301
if (!hasAttachedBehavior()) {
251302
return false;
252303
}
304+
int nextState = getNextState();
305+
if (nextState != -1) {
306+
bottomSheetBehavior.setState(nextState);
307+
}
308+
return true;
309+
}
310+
311+
private int getNextState() {
312+
int nextState = -1;
313+
if (!hasAttachedBehavior()) {
314+
return nextState;
315+
}
253316
boolean canHalfExpand =
254317
!bottomSheetBehavior.isFitToContents()
255318
&& !bottomSheetBehavior.shouldSkipHalfExpandedStateWhenDragging();
256319
int currentState = bottomSheetBehavior.getState();
257-
int nextState;
258-
if (currentState == BottomSheetBehavior.STATE_COLLAPSED) {
259-
nextState =
260-
canHalfExpand
261-
? BottomSheetBehavior.STATE_HALF_EXPANDED
262-
: BottomSheetBehavior.STATE_EXPANDED;
263-
} else if (currentState == BottomSheetBehavior.STATE_EXPANDED) {
264-
nextState =
265-
canHalfExpand
266-
? BottomSheetBehavior.STATE_HALF_EXPANDED
267-
: BottomSheetBehavior.STATE_COLLAPSED;
268-
} else {
269-
nextState =
270-
clickToExpand ? BottomSheetBehavior.STATE_EXPANDED : BottomSheetBehavior.STATE_COLLAPSED;
320+
switch (currentState) {
321+
case BottomSheetBehavior.STATE_COLLAPSED:
322+
nextState =
323+
canHalfExpand
324+
? BottomSheetBehavior.STATE_HALF_EXPANDED
325+
: BottomSheetBehavior.STATE_EXPANDED;
326+
break;
327+
case BottomSheetBehavior.STATE_EXPANDED:
328+
nextState =
329+
canHalfExpand
330+
? BottomSheetBehavior.STATE_HALF_EXPANDED
331+
: BottomSheetBehavior.STATE_COLLAPSED;
332+
break;
333+
case BottomSheetBehavior.STATE_HALF_EXPANDED:
334+
nextState =
335+
clickToExpand
336+
? BottomSheetBehavior.STATE_EXPANDED
337+
: BottomSheetBehavior.STATE_COLLAPSED;
338+
break;
339+
default: // fall out
271340
}
272-
bottomSheetBehavior.setState(nextState);
273-
return true;
341+
return nextState;
274342
}
275343

276344
/**

lib/java/com/google/android/material/bottomsheet/res/values-af/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Vou halfpad uit</string>
1919
<string name="bottomsheet_drag_handle_content_description">Sleephandvatsel</string>
20-
<string name="bottomsheet_action_expand">Vou die onderste blad uit</string>
21-
<string name="bottomsheet_action_collapse">Vou die onderste blad in</string>
20+
<string name="bottomsheet_action_expand_description">Vou die onderste blad uit</string>
21+
<string name="bottomsheet_action_collapse_description">Vou die onderste blad in</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-am/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">ግማሽ መንገድ ዘርጋ</string>
1919
<string name="bottomsheet_drag_handle_content_description">መያዣ ይጎትቱ</string>
20-
<string name="bottomsheet_action_expand">የግርጌ ሉሁን ይዘርጉ</string>
21-
<string name="bottomsheet_action_collapse">የግርጌ ሉሁን ይሰብስቡ</string>
20+
<string name="bottomsheet_action_expand_description">የግርጌ ሉሁን ይዘርጉ</string>
21+
<string name="bottomsheet_action_collapse_description">የግርጌ ሉሁን ይሰብስቡ</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-ar/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">توسيع البطاقة السفلية</string>
1919
<string name="bottomsheet_drag_handle_content_description">مقبض السحب</string>
20-
<string name="bottomsheet_action_expand">توسيع البطاقة السفلية</string>
21-
<string name="bottomsheet_action_collapse">تصغير البطاقة السفلية</string>
20+
<string name="bottomsheet_action_expand_description">توسيع البطاقة السفلية</string>
21+
<string name="bottomsheet_action_collapse_description">تصغير البطاقة السفلية</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-as/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Expand halfway</string>
1919
<string name="bottomsheet_drag_handle_content_description">Drag handle</string>
20-
<string name="bottomsheet_action_expand">Expand the bottom sheet</string>
21-
<string name="bottomsheet_action_collapse">Collapse the bottom sheet</string>
20+
<string name="bottomsheet_action_expand_description">Expand the bottom sheet</string>
21+
<string name="bottomsheet_action_collapse_description">Collapse the bottom sheet</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-az/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Tam genişləndirin</string>
1919
<string name="bottomsheet_drag_handle_content_description">Dəstək</string>
20-
<string name="bottomsheet_action_expand">Aşağıdakı vərəqi genişləndirin</string>
21-
<string name="bottomsheet_action_collapse">Aşağıdakı vərəqi yığcamlaşdırın</string>
20+
<string name="bottomsheet_action_expand_description">Aşağıdakı vərəqi genişləndirin</string>
21+
<string name="bottomsheet_action_collapse_description">Aşağıdakı vərəqi yığcamlaşdırın</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-b+es+419/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Expandir hasta la mitad</string>
1919
<string name="bottomsheet_drag_handle_content_description">Controlador de arrastre</string>
20-
<string name="bottomsheet_action_expand">Expandir la hoja inferior</string>
21-
<string name="bottomsheet_action_collapse">Contraer la hoja inferior</string>
20+
<string name="bottomsheet_action_expand_description">Expandir la hoja inferior</string>
21+
<string name="bottomsheet_action_collapse_description">Contraer la hoja inferior</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-b+sr+Latn/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Proširite do pola</string>
1919
<string name="bottomsheet_drag_handle_content_description">Ručica za prevlačenje</string>
20-
<string name="bottomsheet_action_expand">Proširite donju tabelu</string>
21-
<string name="bottomsheet_action_collapse">Skupite donju tabelu</string>
20+
<string name="bottomsheet_action_expand_description">Proširite donju tabelu</string>
21+
<string name="bottomsheet_action_collapse_description">Skupite donju tabelu</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-be/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Разгарнуць напалову</string>
1919
<string name="bottomsheet_drag_handle_content_description">Маркер перацягвання</string>
20-
<string name="bottomsheet_action_expand">Разгарнуць ніжні аркуш</string>
21-
<string name="bottomsheet_action_collapse">Згарнуць ніжні аркуш</string>
20+
<string name="bottomsheet_action_expand_description">Разгарнуць ніжні аркуш</string>
21+
<string name="bottomsheet_action_collapse_description">Згарнуць ніжні аркуш</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-bg/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Разгъване наполовина</string>
1919
<string name="bottomsheet_drag_handle_content_description">Манипулатор за преместване с плъзгане</string>
20-
<string name="bottomsheet_action_expand">Разгъване на долния лист</string>
21-
<string name="bottomsheet_action_collapse">Свиване на долния лист</string>
20+
<string name="bottomsheet_action_expand_description">Разгъване на долния лист</string>
21+
<string name="bottomsheet_action_collapse_description">Свиване на долния лист</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-bn/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">অর্ধেক প্রসারিত করুন</string>
1919
<string name="bottomsheet_drag_handle_content_description">টেনে আনার হ্যান্ডেল</string>
20-
<string name="bottomsheet_action_expand">স্ক্রিনের নিচে অ্যাটাচ করা শিট বড় করুন</string>
21-
<string name="bottomsheet_action_collapse">স্ক্রিনের নিচে অ্যাটাচ করা শিট আড়াল করুন</string>
20+
<string name="bottomsheet_action_expand_description">স্ক্রিনের নিচে অ্যাটাচ করা শিট বড় করুন</string>
21+
<string name="bottomsheet_action_collapse_description">স্ক্রিনের নিচে অ্যাটাচ করা শিট আড়াল করুন</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-bs/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Proširivanje dopola</string>
1919
<string name="bottomsheet_drag_handle_content_description">Ručica za prevlačenje</string>
20-
<string name="bottomsheet_action_expand">Proširivanje donje tabele</string>
21-
<string name="bottomsheet_action_collapse">Sužavanje donje tabele</string>
20+
<string name="bottomsheet_action_expand_description">Proširivanje donje tabele</string>
21+
<string name="bottomsheet_action_collapse_description">Sužavanje donje tabele</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-ca/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Desplega fins a la meitat</string>
1919
<string name="bottomsheet_drag_handle_content_description">Ansa per arrossegar</string>
20-
<string name="bottomsheet_action_expand">Desplega el full inferior</string>
21-
<string name="bottomsheet_action_collapse">Replega el full inferior</string>
20+
<string name="bottomsheet_action_expand_description">Desplega el full inferior</string>
21+
<string name="bottomsheet_action_collapse_description">Replega el full inferior</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-cs/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Rozbalit napůl</string>
1919
<string name="bottomsheet_drag_handle_content_description">Úchyt pro přetažení</string>
20-
<string name="bottomsheet_action_expand">Rozbalit spodní tabulku</string>
21-
<string name="bottomsheet_action_collapse">Sbalit spodní tabulku</string>
20+
<string name="bottomsheet_action_expand_description">Rozbalit spodní tabulku</string>
21+
<string name="bottomsheet_action_collapse_description">Sbalit spodní tabulku</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-da/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Udvid halvdelen</string>
1919
<string name="bottomsheet_drag_handle_content_description">Håndtag</string>
20-
<string name="bottomsheet_action_expand">Udvid feltet i bunden</string>
21-
<string name="bottomsheet_action_collapse">Skjul feltet i bunden</string>
20+
<string name="bottomsheet_action_expand_description">Udvid feltet i bunden</string>
21+
<string name="bottomsheet_action_collapse_description">Skjul feltet i bunden</string>
2222
</resources>

lib/java/com/google/android/material/bottomsheet/res/values-de/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<resources>
1818
<string name="bottomsheet_action_expand_halfway">Zur Hälfte maximieren</string>
1919
<string name="bottomsheet_drag_handle_content_description">Ziehpunkt</string>
20-
<string name="bottomsheet_action_expand">Ansicht am unteren Rand maximieren</string>
21-
<string name="bottomsheet_action_collapse">Ansicht am unteren Rand minimieren</string>
20+
<string name="bottomsheet_action_expand_description">Ansicht am unteren Rand maximieren</string>
21+
<string name="bottomsheet_action_collapse_description">Ansicht am unteren Rand minimieren</string>
2222
</resources>

0 commit comments

Comments
 (0)