Skip to content

Commit bed5c59

Browse files
paulfthomasdsn5ft
authored andcommitted
[BadgeDrawable][a11y] Attach/detach badge contentDescription when using menuItem.
Resolves #2429 PiperOrigin-RevId: 439651014 (cherry picked from commit ee49c5a)
1 parent 51093e1 commit bed5c59

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

lib/java/com/google/android/material/badge/BadgeUtils.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import androidx.annotation.NonNull;
3434
import androidx.annotation.Nullable;
3535
import androidx.annotation.VisibleForTesting;
36+
import androidx.core.view.AccessibilityDelegateCompat;
37+
import androidx.core.view.ViewCompat;
38+
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
3639
import com.google.android.material.internal.ParcelableSparseArray;
3740
import com.google.android.material.internal.ToolbarUtils;
3841

@@ -96,7 +99,6 @@ public static void attachBadgeDrawable(
9699
anchor.getOverlay().add(badgeDrawable);
97100
}
98101
}
99-
100102
}
101103

102104
/**
@@ -131,11 +133,39 @@ public void run() {
131133
if (menuItemView != null) {
132134
setToolbarOffset(badgeDrawable, toolbar.getResources());
133135
BadgeUtils.attachBadgeDrawable(badgeDrawable, menuItemView, customBadgeParent);
136+
attachBadgeContentDescription(badgeDrawable, menuItemView);
134137
}
135138
}
136139
});
137140
}
138141

142+
private static void attachBadgeContentDescription(
143+
@NonNull final BadgeDrawable badgeDrawable, @NonNull View view) {
144+
if (VERSION.SDK_INT >= VERSION_CODES.Q && ViewCompat.hasAccessibilityDelegate(view)) {
145+
ViewCompat.setAccessibilityDelegate(
146+
view,
147+
new AccessibilityDelegateCompat(view.getAccessibilityDelegate()) {
148+
@Override
149+
public void onInitializeAccessibilityNodeInfo(
150+
View host, AccessibilityNodeInfoCompat info) {
151+
super.onInitializeAccessibilityNodeInfo(host, info);
152+
info.setContentDescription(badgeDrawable.getContentDescription());
153+
}
154+
});
155+
} else {
156+
ViewCompat.setAccessibilityDelegate(
157+
view,
158+
new AccessibilityDelegateCompat() {
159+
@Override
160+
public void onInitializeAccessibilityNodeInfo(
161+
View host, AccessibilityNodeInfoCompat info) {
162+
super.onInitializeAccessibilityNodeInfo(host, info);
163+
info.setContentDescription(badgeDrawable.getContentDescription());
164+
}
165+
});
166+
}
167+
}
168+
139169
/**
140170
* Detaches a BadgeDrawable from its associated anchor. For API 18+, the BadgeDrawable will be
141171
* removed from its anchor's ViewOverlay. For pre-API 18, the BadgeDrawable will be removed from
@@ -167,11 +197,29 @@ public static void detachBadgeDrawable(
167197
if (menuItemView != null) {
168198
removeToolbarOffset(badgeDrawable);
169199
detachBadgeDrawable(badgeDrawable, menuItemView);
200+
detachBadgeContentDescription(menuItemView);
170201
} else {
171202
Log.w(LOG_TAG, "Trying to remove badge from a null menuItemView: " + menuItemId);
172203
}
173204
}
174205

206+
private static void detachBadgeContentDescription(@NonNull View view) {
207+
if (VERSION.SDK_INT >= VERSION_CODES.Q && ViewCompat.hasAccessibilityDelegate(view)) {
208+
ViewCompat.setAccessibilityDelegate(
209+
view,
210+
new AccessibilityDelegateCompat(view.getAccessibilityDelegate()) {
211+
@Override
212+
public void onInitializeAccessibilityNodeInfo(
213+
View host, AccessibilityNodeInfoCompat info) {
214+
super.onInitializeAccessibilityNodeInfo(host, info);
215+
info.setContentDescription(null);
216+
}
217+
});
218+
} else {
219+
ViewCompat.setAccessibilityDelegate(view, null);
220+
}
221+
}
222+
175223
@VisibleForTesting
176224
static void setToolbarOffset(BadgeDrawable badgeDrawable, Resources resources) {
177225
badgeDrawable.setAdditionalHorizontalOffset(

0 commit comments

Comments
 (0)