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
This commit is contained in:
Pieter De Baets
2021-10-21 03:43:49 -07:00
committed by Facebook GitHub Bot
parent 03e513de41
commit 8de8d475d4
2 changed files with 27 additions and 26 deletions
@@ -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,
@@ -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<T extends 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