From 055d029bc0bacb32f57cf4770238b6e1b0f7f5d2 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Fri, 8 Jan 2021 18:08:30 -0800 Subject: [PATCH] Android implementation of sendAccessibilityEvent Summary: This is the Android native implementation of sendAccessibilityEvent for Fabric. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D25852418 fbshipit-source-id: cb51e667a7f673da6b9c9e539770225b02bdc902 --- .../react/fabric/FabricUIManager.java | 18 +++++++++++++++++ .../com/facebook/react/fabric/jni/Binding.cpp | 20 +++++++++++++++++++ .../com/facebook/react/fabric/jni/Binding.h | 4 ++++ 3 files changed, 42 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index 60aca9feefb..09610be63d4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -25,6 +25,7 @@ import android.content.Context; import android.graphics.Point; import android.os.SystemClock; import android.view.View; +import android.view.accessibility.AccessibilityEvent; import androidx.annotation.AnyThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -1005,6 +1006,23 @@ public class FabricUIManager implements UIManager, LifecycleEventListener { addMountItem(new SendAccessibilityEvent(reactTag, eventType)); } + @AnyThread + @ThreadConfined(ANY) + public void sendAccessibilityEventFromJS(int reactTag, String eventTypeJS) { + int eventType; + if ("focus".equals(eventTypeJS)) { + eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED; + } else if ("windowStateChange".equals(eventTypeJS)) { + eventType = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED; + } else if ("click".equals(eventTypeJS)) { + eventType = AccessibilityEvent.TYPE_VIEW_CLICKED; + } else { + throw new IllegalArgumentException( + "sendAccessibilityEventFromJS: invalid eventType " + eventTypeJS); + } + addMountItem(new SendAccessibilityEvent(reactTag, eventType)); + } + /** * Set the JS responder for the view associated with the tags received as a parameter. * diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp index 41388f46f10..75ec98c011b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp @@ -1071,6 +1071,26 @@ void Binding::schedulerDidDispatchCommand( localJavaUIManager, shadowView.tag, command.get(), argsArray.get()); } +void Binding::schedulerDidSendAccessibilityEvent( + const ShadowView &shadowView, + std::string const &eventType) { + jni::global_ref localJavaUIManager = getJavaUIManager(); + if (!localJavaUIManager) { + LOG(ERROR) + << "Binding::schedulerDidDispatchCommand: JavaUIManager disappeared"; + return; + } + + local_ref eventTypeStr = make_jstring(eventType); + + static auto sendAccessibilityEventFromJS = + jni::findClassStatic(Binding::UIManagerJavaDescriptor) + ->getMethod("sendAccessibilityEventFromJS"); + + sendAccessibilityEventFromJS( + localJavaUIManager, shadowView.tag, eventTypeStr.get()); +} + void Binding::schedulerDidSetJSResponder( SurfaceId surfaceId, const ShadowView &shadowView, diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h index 377762eb806..f2ac6cd479a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h @@ -136,6 +136,10 @@ class Binding : public jni::HybridClass, std::string const &commandName, folly::dynamic const args) override; + void schedulerDidSendAccessibilityEvent( + const ShadowView &shadowView, + std::string const &eventType) override; + void schedulerDidSetJSResponder( SurfaceId surfaceId, const ShadowView &shadowView,