From 8de8d475d4a2b765af6a689d7fc1a1a67132f6fc Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Thu, 21 Oct 2021 03:42:03 -0700 Subject: [PATCH] Add surfaceId helper to Event baseclass Summary: Colocate the surface ID generate with the code that consumes it. This allows us to re-use this method in other event emitter locations. Changelog: [Android][Changed] Add helper to get surfaceId for event dispatching Reviewed By: philIip Differential Revision: D31651881 fbshipit-source-id: 109e189f90261d3ba0077ffa519c3d12a9111439 --- .../react/uimanager/JSTouchDispatcher.java | 33 ++++--------------- .../react/uimanager/events/Event.java | 20 +++++++++++ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/JSTouchDispatcher.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/JSTouchDispatcher.java index 901961494cc..d494c6a0aef 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/JSTouchDispatcher.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/JSTouchDispatcher.java @@ -12,7 +12,7 @@ import android.view.ViewGroup; import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; import com.facebook.react.common.ReactConstants; -import com.facebook.react.uimanager.common.UIManagerType; +import com.facebook.react.uimanager.events.Event; import com.facebook.react.uimanager.events.EventDispatcher; import com.facebook.react.uimanager.events.TouchEvent; import com.facebook.react.uimanager.events.TouchEventCoalescingKeyHelper; @@ -51,25 +51,6 @@ public class JSTouchDispatcher { mTargetTag = -1; } - /** - * See Event.java. By contract, this surfaceId should be a valid SurfaceId in Fabric, and should - * ALWAYS return -1 in non-Fabric. - * - * @return - */ - private int getSurfaceId() { - if (mRootViewGroup != null - && mRootViewGroup instanceof ReactRoot - && ((ReactRoot) mRootViewGroup).getUIManagerType() == UIManagerType.FABRIC) { - if (mRootViewGroup.getContext() instanceof ThemedReactContext) { - ThemedReactContext context = (ThemedReactContext) mRootViewGroup.getContext(); - return context.getSurfaceId(); - } - return ((ReactRoot) mRootViewGroup).getRootViewTag(); - } - return -1; - } - /** * Main catalyst view is responsible for collecting and sending touch events to JS. This method * reacts for an incoming android native touch events ({@link MotionEvent}) and calls into {@link @@ -94,7 +75,7 @@ public class JSTouchDispatcher { mTargetTag = findTargetTagAndSetCoordinates(ev); eventDispatcher.dispatchEvent( TouchEvent.obtain( - getSurfaceId(), + Event.getSurfaceIdForView(mRootViewGroup), mTargetTag, TouchEventType.START, ev, @@ -119,7 +100,7 @@ public class JSTouchDispatcher { findTargetTagAndSetCoordinates(ev); eventDispatcher.dispatchEvent( TouchEvent.obtain( - getSurfaceId(), + Event.getSurfaceIdForView(mRootViewGroup), mTargetTag, TouchEventType.END, ev, @@ -134,7 +115,7 @@ public class JSTouchDispatcher { findTargetTagAndSetCoordinates(ev); eventDispatcher.dispatchEvent( TouchEvent.obtain( - getSurfaceId(), + Event.getSurfaceIdForView(mRootViewGroup), mTargetTag, TouchEventType.MOVE, ev, @@ -146,7 +127,7 @@ public class JSTouchDispatcher { // New pointer goes down, this can only happen after ACTION_DOWN is sent for the first pointer eventDispatcher.dispatchEvent( TouchEvent.obtain( - getSurfaceId(), + Event.getSurfaceIdForView(mRootViewGroup), mTargetTag, TouchEventType.START, ev, @@ -158,7 +139,7 @@ public class JSTouchDispatcher { // Exactly one of the pointers goes up eventDispatcher.dispatchEvent( TouchEvent.obtain( - getSurfaceId(), + Event.getSurfaceIdForView(mRootViewGroup), mTargetTag, TouchEventType.END, ev, @@ -207,7 +188,7 @@ public class JSTouchDispatcher { Assertions.assertNotNull(eventDispatcher) .dispatchEvent( TouchEvent.obtain( - getSurfaceId(), + Event.getSurfaceIdForView(mRootViewGroup), mTargetTag, TouchEventType.CANCEL, androidEvent, diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java index b205bdeb3c7..bf7b9a4150e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java @@ -7,10 +7,13 @@ package com.facebook.react.uimanager.events; +import android.view.View; import androidx.annotation.Nullable; import com.facebook.react.bridge.WritableMap; import com.facebook.react.common.SystemClock; import com.facebook.react.uimanager.IllegalViewOperationException; +import com.facebook.react.uimanager.ReactRoot; +import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.common.UIManagerType; /** @@ -40,6 +43,23 @@ public abstract class Event { private long mTimestampMs; private int mUniqueID = sUniqueID++; + /** + * This surfaceId should be a valid SurfaceId in Fabric, and should ALWAYS return -1 in + * non-Fabric. + */ + public static int getSurfaceIdForView(@Nullable View view) { + if (view != null + && view instanceof ReactRoot + && ((ReactRoot) view).getUIManagerType() == UIManagerType.FABRIC) { + if (view.getContext() instanceof ThemedReactContext) { + ThemedReactContext context = (ThemedReactContext) view.getContext(); + return context.getSurfaceId(); + } + return ((ReactRoot) view).getRootViewTag(); + } + return -1; + } + protected Event() {} @Deprecated