-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added accessibilityRole Prop, added functionality support for role on…
… android Summary: Added a new property to View for Accessibility called `accessibilityRole`. This property merges functionality of existing properties: `accessibilityTraits` (iOS) and `accessibilityComponentType` (android). Currently, nine values are supported with equivalent behavior as `accessibilityTraits` (iOS) when `accessibilityRole` is set on iOS Voiceover and Android TalkBack ``` | 'none' | 'button' | 'link' | 'search' | 'image' | 'keyboardkey' | 'text' | 'adjustable' | 'tabbar' ``` They currently support similar behavior on talkback on Android and voice over on iOS Does not break functionality of existing properties, but have not tested for behavior of setting both this one and the old one. * iOS - I added a property accessibilityRoles, and basically remapped it to the same thing as accessibilityTraits. I also added in enum mappings for keyboardkey and tabbar. * Android - Also added a property accessibilityRoles, from the Android side. For the underlying native functionality, I built a helper class that is based off of AccessibilityRolesUtil.java from the accessibility team. Biggest changes made are that I defined my own enums if needed, and also set some properties to match the functionality of iOS Accessibility Traits. I also handled the logic for switch/case statements of setting roles for the android side on this file. Also, I currently haven't localized strings for setRoleDescription, but plan to. * Javascript - I added a view property accessibilityRoles in ViewPropTypes. Reviewed By: blavalla Differential Revision: D8756225 fbshipit-source-id: e03eec40cce86042551764f433e1defe7ee41b35
- Loading branch information
1 parent
ef3d8b2
commit c27b495
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
ReactAndroid/src/main/java/com/facebook/react/uimanager/AccessibilityRoleUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) 2004-present, Facebook, Inc. | ||
|
||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
package com.facebook.react.uimanager; | ||
|
||
import android.annotation.TargetApi; | ||
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 javax.annotation.Nullable; | ||
|
||
/** | ||
* Utility class that handles the addition of a "role" for accessibility to either a View or | ||
* AccessibilityNodeInfo. | ||
*/ | ||
|
||
public class AccessibilityRoleUtil { | ||
|
||
/** | ||
* These roles are defined by Google's TalkBack screen reader, and this list should be kept up to | ||
* date with their implementation. Details can be seen in their source code here: | ||
* | ||
* <p>https://github.com/google/talkback/blob/master/utils/src/main/java/Role.java | ||
*/ | ||
|
||
public enum AccessibilityRole { | ||
NONE(null), | ||
BUTTON("android.widget.Button"), | ||
IMAGE("android.widget.ImageView"), | ||
KEYBOARD_KEY("android.inputmethodservice.Keyboard$Key"), | ||
TEXT("android.widget.ViewGroup"), | ||
TAB_BAR("android.widget.TabWidget"); | ||
|
||
@Nullable private final String mValue; | ||
|
||
AccessibilityRole(String type) { | ||
mValue = type; | ||
} | ||
|
||
@Nullable | ||
public String getValue() { | ||
return mValue; | ||
} | ||
|
||
public static AccessibilityRole fromValue(String value) { | ||
for (AccessibilityRole role : AccessibilityRole.values()) { | ||
if (role.getValue() != null && role.getValue().equals(value)) { | ||
return role; | ||
} | ||
} | ||
return AccessibilityRole.NONE; | ||
} | ||
} | ||
|
||
private AccessibilityRoleUtil() { | ||
// No instances | ||
} | ||
|
||
public static void setRole(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)) { | ||
ViewCompat.setAccessibilityDelegate( | ||
view, | ||
new AccessibilityDelegateCompat() { | ||
@Override | ||
public void onInitializeAccessibilityNodeInfo( | ||
View host, AccessibilityNodeInfoCompat info) { | ||
super.onInitializeAccessibilityNodeInfo(host, info); | ||
setRole(info, role); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public static void setRole(AccessibilityNodeInfoCompat nodeInfo, final AccessibilityRole role) { | ||
nodeInfo.setClassName(role.getValue()); | ||
} | ||
|
||
/** | ||
* Variables and methods for setting accessibilityRole on view properties. | ||
*/ | ||
private static final String NONE = "none"; | ||
private static final String BUTTON = "button"; | ||
private static final String IMAGE = "image"; | ||
private static final String KEYBOARDKEY = "keyboardkey"; | ||
private static final String TEXT = "text"; | ||
private static final String TABBAR = "tabbar"; | ||
|
||
public static void updateAccessibilityRole(View view, String role) { | ||
if (role == null) { | ||
view.setAccessibilityDelegate(null); | ||
} | ||
switch (role) { | ||
case NONE: | ||
break; | ||
case BUTTON: | ||
setRole(view, AccessibilityRoleUtil.AccessibilityRole.BUTTON); | ||
break; | ||
case IMAGE: | ||
setRole(view, AccessibilityRoleUtil.AccessibilityRole.IMAGE); | ||
break; | ||
case KEYBOARDKEY: | ||
setRole(view, AccessibilityRoleUtil.AccessibilityRole.KEYBOARD_KEY); | ||
break; | ||
case TEXT: | ||
setRole(view, AccessibilityRoleUtil.AccessibilityRole.TEXT); | ||
break; | ||
case TABBAR: | ||
setRole(view, AccessibilityRoleUtil.AccessibilityRole.TAB_BAR); | ||
break; | ||
default: | ||
view.setAccessibilityDelegate(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters