-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Role and item announcement in Flatlist #31666
Changes from 1 commit
26d37b7
a40df5d
ec09520
55877da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,14 @@ | |
package com.facebook.react.uimanager; | ||
|
||
import android.content.Context; | ||
import android.graphics.Rect; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.text.SpannableString; | ||
import android.text.style.URLSpan; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.view.accessibility.AccessibilityEvent; | ||
import androidx.annotation.Nullable; | ||
import androidx.core.view.AccessibilityDelegateCompat; | ||
|
@@ -107,6 +109,7 @@ public enum AccessibilityRole { | |
TAB, | ||
TABLIST, | ||
TIMER, | ||
LIST, | ||
TOOLBAR; | ||
|
||
public static String getValue(AccessibilityRole role) { | ||
|
@@ -135,6 +138,8 @@ public static String getValue(AccessibilityRole role) { | |
return "android.widget.SpinButton"; | ||
case SWITCH: | ||
return "android.widget.Switch"; | ||
case LIST: | ||
return "android.widget.AbsListView"; | ||
case NONE: | ||
case LINK: | ||
case SUMMARY: | ||
|
@@ -204,6 +209,20 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo | |
} | ||
final ReadableArray accessibilityActions = | ||
(ReadableArray) host.getTag(R.id.accessibility_actions); | ||
final ReadableMap accessibilityCollectionInfo = | ||
(ReadableMap) host.getTag(R.id.accessibility_collection_info); | ||
|
||
|
||
if (accessibilityCollectionInfo != null) { | ||
int rowCount = accessibilityCollectionInfo.getInt("rowCount"); | ||
int columnCount = accessibilityCollectionInfo.getInt("columnCount"); | ||
boolean hierarchical = accessibilityCollectionInfo.getBoolean("hierarchical"); | ||
|
||
AccessibilityNodeInfoCompat.CollectionInfoCompat collectionInfoCompat = AccessibilityNodeInfoCompat.CollectionInfoCompat.obtain(rowCount, columnCount, hierarchical); | ||
info.setCollectionInfo(collectionInfoCompat); | ||
} | ||
|
||
|
||
if (accessibilityActions != null) { | ||
for (int i = 0; i < accessibilityActions.size(); i++) { | ||
final ReadableMap action = accessibilityActions.getMap(i); | ||
|
@@ -259,12 +278,54 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo | |
} | ||
} | ||
|
||
private boolean isViewVisible(View scrollView, View view) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you may be able to use AccessibilityNodeInfoCompat's isVisibleToUser method rather than calculating this by hand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Rect scrollBounds = new Rect(); | ||
scrollView.getDrawingRect(scrollBounds); | ||
float viewHeight = view.getHeight(); | ||
// Verify View is half visible | ||
float top = view.getY() + viewHeight / 2; | ||
float bottom = top + view.getHeight() - viewHeight / 2; | ||
|
||
if (scrollBounds.top < top && scrollBounds.bottom > bottom) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { | ||
super.onInitializeAccessibilityEvent(host, event); | ||
// Set item count and current item index on accessibility events for adjustable | ||
// in order to make Talkback announce the value of the adjustable | ||
final ReadableMap accessibilityValue = (ReadableMap) host.getTag(R.id.accessibility_value); | ||
final ReadableMap accessibilityCollectionInfo = (ReadableMap) host.getTag(R.id.accessibility_collection_info); | ||
if (accessibilityCollectionInfo != null) { | ||
event.setItemCount(accessibilityCollectionInfo.getInt("rowCount")); | ||
|
||
View contentView = ((ViewGroup) host).getChildAt(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably want to check if host is an instance of ViewGroup before casting it as such, otherwise you could end up with a ClassCastException here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
ReadableMap firstVisible = null; | ||
ReadableMap lastVisible = null; | ||
|
||
for(int index = 0; index < ((ViewGroup) contentView).getChildCount(); index++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same casting comment here as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
View nextChild = ((ViewGroup) contentView).getChildAt(index); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
boolean isVisible = isViewVisible(host, nextChild); | ||
if (isVisible == true) { | ||
if(firstVisible == null) { | ||
firstVisible = (ReadableMap) nextChild.getTag(R.id.accessibility_collection_item_info); | ||
} | ||
lastVisible = (ReadableMap) nextChild.getTag(R.id.accessibility_collection_item_info); | ||
} | ||
|
||
|
||
if (firstVisible != null && lastVisible != null) { | ||
event.setFromIndex(firstVisible.getInt("rowIndex")); | ||
event.setToIndex(lastVisible.getInt("rowIndex")); | ||
} | ||
} | ||
} | ||
|
||
if (accessibilityValue != null | ||
&& accessibilityValue.hasKey("min") | ||
&& accessibilityValue.hasKey("now") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow, I didn't realize we didn't have a role for list! Good catch!