Skip to content

Fix accessibilityOrder when referencing both a parent and its children #52115

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import com.facebook.common.logging.FLog;
import com.facebook.react.R;
Expand Down Expand Up @@ -169,6 +170,20 @@ public BaseViewManager(@Nullable ReactApplicationContext reactContext) {
return view;
}

@Override
public void onDropViewInstance(@NonNull T view) {
super.onDropViewInstance(view);
AccessibilityDelegateCompat axDelegate = ViewCompat.getAccessibilityDelegate(view);

if (axDelegate instanceof ReactAccessibilityDelegate) {
((ReactAccessibilityDelegate) axDelegate).cleanUp();
}

if (view instanceof ViewGroup) {
((ViewGroup) view).setOnHierarchyChangeListener(null);
}
}

@Override
protected void addEventEmitters(@NonNull ThemedReactContext reactContext, @NonNull T view) {
super.addEventEmitters(reactContext, view);
Expand Down Expand Up @@ -340,6 +355,9 @@ public void setAccessibilityOrder(@NonNull T view, @Nullable ReadableArray nativ
@Override
public void onChildViewAdded(View parent, View child) {
view.setTag(R.id.accessibility_order_dirty, true);
child.setTag(R.id.accessibility_order_parent, parent);
ReactAccessibilityDelegate.setDelegate(
child, child.isFocusable(), child.getImportantForAccessibility());

// We also want to listen to changes on the hierarchy of nested ViewGroups
if (child instanceof ViewGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -72,6 +73,10 @@ public class ReactAccessibilityDelegate extends ExploreByTouchHelper {
private Handler mHandler;
private final HashMap<Integer, String> mAccessibilityActionsMap;

@Nullable
private AccessibilityManager.AccessibilityStateChangeListener accessibilityStateChangeListener =
null;

@Nullable View mAccessibilityLabelledBy;

static {
Expand Down Expand Up @@ -112,6 +117,7 @@ public static void setDelegate(
if (!ViewCompat.hasAccessibilityDelegate(view)
&& (view.getTag(R.id.accessibility_role) != null
|| view.getTag(R.id.accessibility_order) != null
|| (view.getTag(R.id.accessibility_order_parent) != null && view.isFocusable())
|| view.getTag(R.id.accessibility_state) != null
|| view.getTag(R.id.accessibility_actions) != null
|| view.getTag(R.id.react_test_id) != null
Expand Down Expand Up @@ -261,6 +267,22 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo
ReadableArray axOrderIds = (ReadableArray) mView.getTag(R.id.accessibility_order);
if (axOrderIds != null && axOrderIds.size() != 0) {

AccessibilityManager am =
(AccessibilityManager) host.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);

if (accessibilityStateChangeListener == null && am != null) {
AccessibilityManager.AccessibilityStateChangeListener newAccessibilityStateChangeListener =
enabled -> {
if (!enabled) {
ReactAxOrderHelper.restoreSubtreeFocusability(host);
host.setTag(R.id.accessibility_order_dirty, true);
}
};

am.addAccessibilityStateChangeListener(newAccessibilityStateChangeListener);
accessibilityStateChangeListener = newAccessibilityStateChangeListener;
}

Boolean isAxOrderDirty = (Boolean) mView.getTag(R.id.accessibility_order_dirty);
if (isAxOrderDirty != null && isAxOrderDirty) {
List<String> axOrderIdsList = new ArrayList<>();
Expand Down Expand Up @@ -517,7 +539,8 @@ protected boolean onPerformActionForVirtualView(

@Override
public @Nullable AccessibilityNodeProviderCompat getAccessibilityNodeProvider(View host) {
if (mView.getTag(R.id.accessibility_order) != null) {
if ((mView.getTag(R.id.accessibility_order) != null)
|| ((mView.getTag(R.id.accessibility_order_parent) != null) && mView.isFocusable())) {
return super.getAccessibilityNodeProvider(host);
}

Expand Down Expand Up @@ -1098,4 +1121,17 @@ public static AccessibilityRole fromValue(@Nullable String value) {
}
}
}

// In case a view with accessibilityOrder is unmounted we need a way to clean up the listener on
// this delegate
public void cleanUp() {
if (accessibilityStateChangeListener != null) {
AccessibilityManager am =
(AccessibilityManager) mView.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
if (am != null) {
am.removeAccessibilityStateChangeListener(accessibilityStateChangeListener);
}
accessibilityStateChangeListener = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package com.facebook.react.uimanager
import android.graphics.Rect
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.facebook.react.R
import com.facebook.react.bridge.ReadableArray

Expand Down Expand Up @@ -47,14 +46,8 @@ private object ReactAxOrderHelper {

if (isIncluded && view.isFocusable) {
axOrderViews[axOrderIds.indexOf(nativeId)].add(view)
if (parent != view) {
view.setTag(R.id.accessibility_order_parent, parent)
}
} else if (isContained && view.isFocusable) {
axOrderViews[axOrderIds.indexOf(containerId)].add(view)
if (parent != view) {
view.setTag(R.id.accessibility_order_parent, parent)
}
}

if (isNestedAxOrder) {
Expand All @@ -76,10 +69,6 @@ private object ReactAxOrderHelper {
}
}

if (!isIncluded && !isContained && parent != view && view !is TextView) {
view.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
}

// Don't traverse the children of a nested accessibility order
if (view is ViewGroup) {
val axChildren: ArrayList<View> = getAxChildren(view)
Expand All @@ -100,6 +89,13 @@ private object ReactAxOrderHelper {
}
}
}

if (!isIncluded && !isContained && parent != view) {
if (view.getTag(R.id.original_focusability) == null) {
view.setTag(R.id.original_focusability, view.isFocusable)
}
view.isFocusable = false
}
}

traverseAndBuildAxOrder(
Expand Down Expand Up @@ -159,4 +155,21 @@ private object ReactAxOrderHelper {
}
return axChildren
}

@JvmStatic
public fun restoreSubtreeFocusability(view: View) {
val originalFocusability = view.getTag(R.id.original_focusability)
if (originalFocusability is Boolean) {
view.isFocusable = originalFocusability
}

if (view is ViewGroup) {
for (i in 0 until view.childCount) {
val child = view.getChildAt(i)
if (child != null) {
restoreSubtreeFocusability(child)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<!-- tag is used to store the current state of the accessibility order tree-->
<item type="id" name="accessibility_order_dirty"/>

<!-- tag is used to store the original focusability value of a view within the accessibility order tree if it was changed-->
<item type="id" name="original_focusability"/>

<!-- tag is used to store the nativeID tag -->
<item type="id" name="view_tag_instance_handle"/>

Expand Down
Loading