Added logic to only override role description that is said on TalkBack if it's in english.

Summary:
Context:
On Android, I am currently overriding the role description for TalkBack on certain Roles. Currently, these are done only in English.
I needed to add a checker that only overrides these role descriptions if the language of the phone is set

Changes:
* Added a language checker before I call setRoleDescription
* Put the strings into variables.

Note:
This is done to prioritize a codemod for accessibilityRole. Eventually, we hope to add support for other languages on these roles.

Reviewed By: blavalla

Differential Revision: D8884991

fbshipit-source-id: 3182eb5856ea57939fb25b17522d4b79ef2078e3
This commit is contained in:
Ziqi Chen 2018-07-24 16:46:03 -07:00 committed by Facebook Github Bot
parent dfcf9c50b6
commit 9f01e4ccff
2 changed files with 48 additions and 17 deletions

View File

@ -6,13 +6,16 @@
package com.facebook.react.uimanager;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.AccessibilityDelegateCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import com.facebook.react.R;
import com.facebook.react.bridge.ReadableArray;
import java.util.Locale;
import javax.annotation.Nullable;
/**
@ -32,7 +35,7 @@ public class AccessibilityRoleUtil {
public enum AccessibilityRole {
NONE(null),
BUTTON("android.widget.Button"),
LINK("android.widget.Button"),
LINK("android.widget.ViewGroup"),
SEARCH("android.widget.EditText"),
IMAGE("android.widget.ImageView"),
IMAGEBUTTON("android.widget.ImageView"),
@ -65,7 +68,7 @@ public class AccessibilityRoleUtil {
// No instances
}
public static void setRole(View view, final AccessibilityRole role) {
public static void setRole(final View view, final AccessibilityRole role) {
// if a view already has an accessibility delegate, replacing it could cause problems,
// so leave it alone.
if (!ViewCompat.hasAccessibilityDelegate(view)) {
@ -76,32 +79,42 @@ public class AccessibilityRoleUtil {
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
setRole(info, role);
setRole(info, role, view.getContext());
}
});
}
}
public static void setRole(AccessibilityNodeInfoCompat nodeInfo, final AccessibilityRole role) {
/**
* Strings for setting the Role Description in english
*/
//TODO: Eventually support fot other languages on talkback
public static void setRole(AccessibilityNodeInfoCompat nodeInfo, final AccessibilityRole role, final Context context) {
nodeInfo.setClassName(role.getValue());
if (role.equals(AccessibilityRole.LINK)) {
nodeInfo.setRoleDescription("Link");
}
if (role.equals(AccessibilityRole.SEARCH)) {
nodeInfo.setRoleDescription("Search Field");
}
if (role.equals(AccessibilityRole.IMAGE)) {
nodeInfo.setRoleDescription("Image");
if (Locale.getDefault().getLanguage().equals(new Locale("en").getLanguage())) {
if (role.equals(AccessibilityRole.LINK)) {
nodeInfo.setRoleDescription(context.getString(R.string.link_description));
}
if (role.equals(AccessibilityRole.SEARCH)) {
nodeInfo.setRoleDescription(context.getString(R.string.search_description));
}
if (role.equals(AccessibilityRole.IMAGE)) {
nodeInfo.setRoleDescription(context.getString(R.string.image_description));
}
if (role.equals(AccessibilityRole.IMAGEBUTTON)) {
nodeInfo.setRoleDescription(context.getString(R.string.image_button_description));
}
if (role.equals(AccessibilityRole.ADJUSTABLE)) {
nodeInfo.setRoleDescription(context.getString(R.string.adjustable_description));
}
}
if (role.equals(AccessibilityRole.IMAGEBUTTON)) {
nodeInfo.setRoleDescription("Button Image");
nodeInfo.setClickable(true);
}
if (role.equals(AccessibilityRole.ADJUSTABLE)) {
nodeInfo.setRoleDescription("Adjustable");
}
}
/**
* Method for setting accessibilityRole on view properties.
*/

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string
name="link_description"
>Link</string>
<string
name="search_description"
>Search Field</string>
<string
name="image_description"
>Image</string>
<string
name="image_button_description"
>Button, Image</string>
<string
name="adjustable_description"
>Adjustable</string>
</resources>