From 388911e4e7b27a627def6e276562018e7e400c4f Mon Sep 17 00:00:00 2001 From: fabriziobertoglio1987 Date: Tue, 31 May 2022 19:59:40 +0800 Subject: [PATCH] retrieve child node description AccessibilityNodeInfoCompat getRoleDescription returns null with custom roles, for this reason a custom logic needs to be implemented to retrieve custom role descriptions Previously child node description were never announced as allways returning null The commit includes also an examples of test which includes a TextInput non accessible child component. The issue is that the TextInput is allays accessible with talkback so I will need to understand how to handle this scenario --- .../uimanager/ReactAccessibilityDelegate.java | 35 ++++++++++++++++--- .../Accessibility/AccessibilityExample.js | 19 ++++++---- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java index 29292c7e16ea2e..7db489a0bc564c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java @@ -191,6 +191,15 @@ public static AccessibilityRole fromValue(@Nullable String value) { } throw new IllegalArgumentException("Invalid accessibility role value: " + value); } + + public static @Nullable String fromAnyValue(@Nullable String value) { + for (AccessibilityRole role : AccessibilityRole.values()) { + if (getValue(role).equalsIgnoreCase(value)) { + return role.toString(); + } + } + return null; + } } private final HashMap mAccessibilityActionsMap; @@ -304,8 +313,11 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo if (testId != null) { info.setViewIdResourceName(testId); } - boolean missingTextOrDescription = - info.getText() == null && info.getContentDescription() == null; + // need to check that string length is not == 0 + boolean missingContentDescription = TextUtils.isEmpty(info.getContentDescription()); + boolean missingText = TextUtils.isEmpty(info.getText()); + boolean missingTextOrDescription = missingContentDescription && missingText; + // add check on placeholder boolean hasContentToAnnounce = accessibilityActions != null || accessibilityState != null @@ -963,6 +975,20 @@ private static void addStateSegments( } } + public static @Nullable String getRoleDescription(AccessibilityNodeInfoCompat node) { + CharSequence role = node.getRoleDescription(); + String roleDescription; + + if (role == null || role == "") { + roleDescription = + ReactAccessibilityDelegate.AccessibilityRole.fromAnyValue((String) node.getClassName()); + } else { + roleDescription = role.toString(); + } + + return roleDescription; + } + /** * Creates the text that Google's TalkBack screen reader will read aloud for a given {@link View}. * This may be any combination of the {@link View}'s {@code text}, {@code contentDescription}, and @@ -994,7 +1020,7 @@ public static CharSequence getTalkbackDescription( final boolean hasNodeText = !TextUtils.isEmpty(nodeText); final boolean isEditText = view instanceof EditText; - CharSequence roleDescription = node.getRoleDescription(); + String roleDescription = getRoleDescription(node); // The original flipper implementation would check isActionableForAccessibility // The check was removed for this reason https://bit.ly/3wPnmPE boolean disabled = !node.isEnabled(); @@ -1037,8 +1063,7 @@ public static CharSequence getTalkbackDescription( // role if (roleDescription != null) { - String roleString = roleDescription.toString(); - talkbackSegments.append(roleString + delimiter); + talkbackSegments.append(roleDescription + delimiter); } // disabled diff --git a/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js b/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js index a8737f7d383410..69a7e7296bfa14 100644 --- a/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js +++ b/packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js @@ -281,16 +281,21 @@ class AccessibilityExample extends React.Component<{}> { - - - + + Text number 1 + + + + + );