[BadgeDrawable][a11y] Attach/detach badge contentDescription when using menuItem.

Resolves https://github.com/material-components/material-components-android/issues/2429

PiperOrigin-RevId: 439651014
(cherry picked from commit ee49c5aa8bed9b0c3a15421725b4f3164c3c6973)
This commit is contained in:
pfthomas 2022-04-05 15:47:01 -04:00 committed by Dan Nizri
parent 51093e19c0
commit bed5c5997c

View File

@ -33,6 +33,9 @@ import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import com.google.android.material.internal.ParcelableSparseArray;
import com.google.android.material.internal.ToolbarUtils;
@ -96,7 +99,6 @@ public class BadgeUtils {
anchor.getOverlay().add(badgeDrawable);
}
}
}
/**
@ -131,11 +133,39 @@ public class BadgeUtils {
if (menuItemView != null) {
setToolbarOffset(badgeDrawable, toolbar.getResources());
BadgeUtils.attachBadgeDrawable(badgeDrawable, menuItemView, customBadgeParent);
attachBadgeContentDescription(badgeDrawable, menuItemView);
}
}
});
}
private static void attachBadgeContentDescription(
@NonNull final BadgeDrawable badgeDrawable, @NonNull View view) {
if (VERSION.SDK_INT >= VERSION_CODES.Q && ViewCompat.hasAccessibilityDelegate(view)) {
ViewCompat.setAccessibilityDelegate(
view,
new AccessibilityDelegateCompat(view.getAccessibilityDelegate()) {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(badgeDrawable.getContentDescription());
}
});
} else {
ViewCompat.setAccessibilityDelegate(
view,
new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(badgeDrawable.getContentDescription());
}
});
}
}
/**
* Detaches a BadgeDrawable from its associated anchor. For API 18+, the BadgeDrawable will be
* removed from its anchor's ViewOverlay. For pre-API 18, the BadgeDrawable will be removed from
@ -167,11 +197,29 @@ public class BadgeUtils {
if (menuItemView != null) {
removeToolbarOffset(badgeDrawable);
detachBadgeDrawable(badgeDrawable, menuItemView);
detachBadgeContentDescription(menuItemView);
} else {
Log.w(LOG_TAG, "Trying to remove badge from a null menuItemView: " + menuItemId);
}
}
private static void detachBadgeContentDescription(@NonNull View view) {
if (VERSION.SDK_INT >= VERSION_CODES.Q && ViewCompat.hasAccessibilityDelegate(view)) {
ViewCompat.setAccessibilityDelegate(
view,
new AccessibilityDelegateCompat(view.getAccessibilityDelegate()) {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(null);
}
});
} else {
ViewCompat.setAccessibilityDelegate(view, null);
}
}
@VisibleForTesting
static void setToolbarOffset(BadgeDrawable badgeDrawable, Resources resources) {
badgeDrawable.setAdditionalHorizontalOffset(