forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xplat/js/react-native-github/packages/react-native/ReactAndroid/src/m…
…ain/java/com/facebook/react/views/scroll/ReactScrollViewAccessibilityDelegate.java (facebook#48530) Summary: Pull Request resolved: facebook#48530 Changelog: [Internal] Reviewed By: tdn120 Differential Revision: D67912326
- Loading branch information
1 parent
f7a5db3
commit 7b1aa80
Showing
2 changed files
with
136 additions
and
161 deletions.
There are no files selected for viewing
161 changes: 0 additions & 161 deletions
161
...d/src/main/java/com/facebook/react/views/scroll/ReactScrollViewAccessibilityDelegate.java
This file was deleted.
Oops, something went wrong.
136 changes: 136 additions & 0 deletions
136
...oid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewAccessibilityDelegate.kt
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,136 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* 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.views.scroll | ||
|
||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.view.accessibility.AccessibilityEvent | ||
import androidx.core.view.AccessibilityDelegateCompat | ||
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat | ||
import com.facebook.react.R | ||
import com.facebook.react.bridge.AssertionException | ||
import com.facebook.react.bridge.ReactSoftExceptionLogger | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.facebook.react.uimanager.ReactAccessibilityDelegate | ||
import com.facebook.react.uimanager.ReactAccessibilityDelegate.AccessibilityRole | ||
|
||
internal class ReactScrollViewAccessibilityDelegate : AccessibilityDelegateCompat() { | ||
|
||
private val TAG: String = ReactScrollViewAccessibilityDelegate::class.java.simpleName | ||
|
||
override fun onInitializeAccessibilityEvent(host: View, event: AccessibilityEvent) { | ||
super.onInitializeAccessibilityEvent(host, event) | ||
if (host is ReactScrollView || host is ReactHorizontalScrollView) { | ||
onInitializeAccessibilityEventInternal(host, event) | ||
} else { | ||
ReactSoftExceptionLogger.logSoftException( | ||
TAG, | ||
AssertionException( | ||
"ReactScrollViewAccessibilityDelegate should only be used with ReactScrollView or ReactHorizontalScrollView, not with class: ${host.javaClass.simpleName}")) | ||
} | ||
} | ||
|
||
override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfoCompat) { | ||
super.onInitializeAccessibilityNodeInfo(host, info) | ||
if (host is ReactScrollView || host is ReactHorizontalScrollView) { | ||
onInitializeAccessibilityNodeInfoInternal(host, info) | ||
} else { | ||
ReactSoftExceptionLogger.logSoftException( | ||
TAG, | ||
AssertionException( | ||
"ReactScrollViewAccessibilityDelegate should only be used with ReactScrollView or ReactHorizontalScrollView, not with class: ${host.javaClass.simpleName}")) | ||
} | ||
} | ||
|
||
private fun onInitializeAccessibilityEventInternal(view: View, event: AccessibilityEvent) { | ||
val accessibilityCollection = | ||
view.getTag(R.id.accessibility_collection) as? ReadableMap ?: return | ||
|
||
event.itemCount = accessibilityCollection.getInt("itemCount") | ||
|
||
val contentView = (view as? ViewGroup)?.getChildAt(0) as? ViewGroup ?: return | ||
|
||
var firstVisibleIndex: Int? = null | ||
var lastVisibleIndex: Int? = null | ||
|
||
for (index in 0..<contentView.childCount) { | ||
val nextChild = contentView.getChildAt(index) | ||
val isVisible: Boolean = | ||
if (view is ReactScrollView) { | ||
view.isPartiallyScrolledInView(nextChild) | ||
} else if (view is ReactHorizontalScrollView) { | ||
view.isPartiallyScrolledInView(nextChild) | ||
} else { | ||
return | ||
} | ||
var accessibilityCollectionItem: ReadableMap? = | ||
nextChild.getTag(R.id.accessibility_collection_item) as ReadableMap | ||
|
||
if (nextChild !is ViewGroup) { | ||
return | ||
} | ||
|
||
// If this child's accessibilityCollectionItem is null, we'll check one more | ||
// nested child. | ||
// Happens when getItemLayout is not passed in FlatList which adds an additional | ||
// View in the hierarchy. | ||
if (nextChild.childCount > 0 && accessibilityCollectionItem == null) { | ||
val nestedNextChild = nextChild.getChildAt(0) | ||
if (nestedNextChild != null) { | ||
val nestedChildAccessibility = | ||
nestedNextChild.getTag(R.id.accessibility_collection_item) as? ReadableMap | ||
if (nestedChildAccessibility != null) { | ||
accessibilityCollectionItem = nestedChildAccessibility | ||
} | ||
} | ||
} | ||
|
||
if (isVisible && accessibilityCollectionItem != null) { | ||
if (firstVisibleIndex == null) { | ||
firstVisibleIndex = accessibilityCollectionItem.getInt("itemIndex") | ||
} | ||
lastVisibleIndex = accessibilityCollectionItem.getInt("itemIndex") | ||
} | ||
|
||
if (firstVisibleIndex != null && lastVisibleIndex != null) { | ||
event.fromIndex = firstVisibleIndex | ||
event.toIndex = lastVisibleIndex | ||
} | ||
} | ||
} | ||
|
||
private fun onInitializeAccessibilityNodeInfoInternal( | ||
view: View, | ||
info: AccessibilityNodeInfoCompat | ||
) { | ||
val accessibilityRole = AccessibilityRole.fromViewTag(view) | ||
|
||
if (accessibilityRole != null) { | ||
ReactAccessibilityDelegate.setRole(info, accessibilityRole, view.context) | ||
} | ||
|
||
val accessibilityCollection = view.getTag(R.id.accessibility_collection) as? ReadableMap | ||
|
||
if (accessibilityCollection != null) { | ||
val rowCount = accessibilityCollection.getInt("rowCount") | ||
val columnCount = accessibilityCollection.getInt("columnCount") | ||
val hierarchical = accessibilityCollection.getBoolean("hierarchical") | ||
|
||
val collectionInfoCompat = | ||
AccessibilityNodeInfoCompat.CollectionInfoCompat.obtain( | ||
rowCount, columnCount, hierarchical) | ||
info.setCollectionInfo(collectionInfoCompat) | ||
} | ||
|
||
if (view is ReactScrollView) { | ||
info.isScrollable = view.scrollEnabled | ||
} else if (view is ReactHorizontalScrollView) { | ||
info.isScrollable = view.scrollEnabled | ||
} | ||
} | ||
} |