Skip to content

Commit

Permalink
[BadgeDrawable][a11y] Attach/detach badge contentDescription when usi…
Browse files Browse the repository at this point in the history
…ng menuItem.

Resolves #2429

PiperOrigin-RevId: 439651014
(cherry picked from commit ee49c5a)
  • Loading branch information
paulfthomas authored and dsn5ft committed Apr 19, 2022
1 parent 51093e1 commit bed5c59
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion lib/java/com/google/android/material/badge/BadgeUtils.java
Expand Up @@ -33,6 +33,9 @@
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;

Expand Down Expand Up @@ -96,7 +99,6 @@ public static void attachBadgeDrawable(
anchor.getOverlay().add(badgeDrawable);
}
}

}

/**
Expand Down Expand Up @@ -131,11 +133,39 @@ public void run() {
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
Expand Down Expand Up @@ -167,11 +197,29 @@ public static void detachBadgeDrawable(
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(
Expand Down

0 comments on commit bed5c59

Please sign in to comment.