|
33 | 33 | import androidx.annotation.NonNull; |
34 | 34 | import androidx.annotation.Nullable; |
35 | 35 | import androidx.annotation.VisibleForTesting; |
| 36 | +import androidx.core.view.AccessibilityDelegateCompat; |
| 37 | +import androidx.core.view.ViewCompat; |
| 38 | +import androidx.core.view.accessibility.AccessibilityNodeInfoCompat; |
36 | 39 | import com.google.android.material.internal.ParcelableSparseArray; |
37 | 40 | import com.google.android.material.internal.ToolbarUtils; |
38 | 41 |
|
@@ -96,7 +99,6 @@ public static void attachBadgeDrawable( |
96 | 99 | anchor.getOverlay().add(badgeDrawable); |
97 | 100 | } |
98 | 101 | } |
99 | | - |
100 | 102 | } |
101 | 103 |
|
102 | 104 | /** |
@@ -131,11 +133,39 @@ public void run() { |
131 | 133 | if (menuItemView != null) { |
132 | 134 | setToolbarOffset(badgeDrawable, toolbar.getResources()); |
133 | 135 | BadgeUtils.attachBadgeDrawable(badgeDrawable, menuItemView, customBadgeParent); |
| 136 | + attachBadgeContentDescription(badgeDrawable, menuItemView); |
134 | 137 | } |
135 | 138 | } |
136 | 139 | }); |
137 | 140 | } |
138 | 141 |
|
| 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 | + |
139 | 169 | /** |
140 | 170 | * Detaches a BadgeDrawable from its associated anchor. For API 18+, the BadgeDrawable will be |
141 | 171 | * removed from its anchor's ViewOverlay. For pre-API 18, the BadgeDrawable will be removed from |
@@ -167,11 +197,29 @@ public static void detachBadgeDrawable( |
167 | 197 | if (menuItemView != null) { |
168 | 198 | removeToolbarOffset(badgeDrawable); |
169 | 199 | detachBadgeDrawable(badgeDrawable, menuItemView); |
| 200 | + detachBadgeContentDescription(menuItemView); |
170 | 201 | } else { |
171 | 202 | Log.w(LOG_TAG, "Trying to remove badge from a null menuItemView: " + menuItemId); |
172 | 203 | } |
173 | 204 | } |
174 | 205 |
|
| 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 | + |
175 | 223 | @VisibleForTesting |
176 | 224 | static void setToolbarOffset(BadgeDrawable badgeDrawable, Resources resources) { |
177 | 225 | badgeDrawable.setAdditionalHorizontalOffset( |
|
0 commit comments