Skip to content

Fix jumping talkback triggering scroll when reaching a view with accessibilityOrder #52231

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

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -3659,6 +3659,7 @@ public class com/facebook/react/uimanager/ReactAccessibilityDelegate : androidx/
public static final field TOP_ACCESSIBILITY_ACTION_EVENT Ljava/lang/String;
public static final field sActionIdMap Ljava/util/HashMap;
public fun <init> (Landroid/view/View;ZI)V
public fun cleanUp ()V
public static fun createNodeInfoFromView (Landroid/view/View;)Landroidx/core/view/accessibility/AccessibilityNodeInfoCompat;
public fun getAccessibilityNodeProvider (Landroid/view/View;)Landroidx/core/view/accessibility/AccessibilityNodeProviderCompat;
protected fun getHostView ()Landroid/view/View;
Expand Down
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 @@ -186,6 +187,16 @@ public void onDropViewInstance(@NonNull T view) {
if (focusChangeListener instanceof BaseVMFocusChangeListener) {
((BaseVMFocusChangeListener) focusChangeListener).detach(view);
}

AccessibilityDelegateCompat axDelegate = ViewCompat.getAccessibilityDelegate(view);

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

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

// Currently, layout listener is only attached when transform or transformOrigin is set.
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 @@ -256,10 +261,29 @@ private void populateAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompa

@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
// If we set an accessibility order then all the focusing logic should go through our custom
// virtual view tree hierarchy and ignore the default path
ReadableArray axOrderIds = (ReadableArray) mView.getTag(R.id.accessibility_order);
if (axOrderIds != null && axOrderIds.size() != 0) {
info.setContentDescription("");
info.setFocusable(false);

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) {
Expand All @@ -278,7 +302,6 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo
return;
}

super.onInitializeAccessibilityNodeInfo(host, info);
populateAccessibilityNodeInfo(host, info);
}

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 @@ -76,10 +75,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 +95,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 +161,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