Migrate lookup of EventDispatcher to not depend on UIManagerModule

Summary:
This diff migrates all the lookups of EventDispatcher to not depend on UIManagerModule anymore.
This refactor is necessary because:
- Users running in Fabric / Venice should not load on the UIManagerModule class
- D25858934 will introduce a change that will break all of these callsites

In the migration I'm relying on the method UIManagerHelper.getEventDispatcherFromReactTag() that returns the correct EventDispatcher for a reactTag.

I'm planning to land this change early in the week (to catch potential errors in alpha / beta versions)

As a followup we need to deprecate and prevent developers to continue using getNativeModule(UIManagerModule.class) moving forward. That will be part of another diff

changelog: [internal] internal

Reviewed By: JoshuaGross

Differential Revision: D25858933

fbshipit-source-id: e26c99759307517b5bef483274fe0e0d71bb4c6c
This commit is contained in:
David Vacca
2021-01-11 15:43:58 -08:00
committed by Facebook GitHub Bot
parent 6a9525c9b0
commit 5348a98207
8 changed files with 48 additions and 39 deletions
@@ -20,7 +20,7 @@ import com.facebook.react.common.MapBuilder;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ViewManagerDelegate;
import com.facebook.react.uimanager.annotations.ReactProp;
@@ -56,12 +56,13 @@ public class ReactDrawerLayoutManager extends ViewGroupManager<ReactDrawerLayout
@Override
protected void addEventEmitters(ThemedReactContext reactContext, ReactDrawerLayout view) {
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
if (uiManager == null) {
EventDispatcher eventDispatcher =
UIManagerHelper.getEventDispatcherForReactTag(reactContext, view.getId());
if (eventDispatcher == null) {
return;
}
view.addDrawerListener(new DrawerEventEmitter(view, uiManager.getEventDispatcher()));
view.addDrawerListener(new DrawerEventEmitter(view, eventDispatcher));
}
@Override
@@ -122,6 +122,7 @@ public class ReactModalHostManager extends ViewGroupManager<ReactModalHostView>
dispatcher.dispatchEvent(new ShowEvent(view.getId()));
}
});
view.setEventDispatcher(dispatcher);
}
}
@@ -202,6 +202,10 @@ public class ReactModalHostView extends ViewGroup
mPropertyRequiresNewDialog = true;
}
void setEventDispatcher(EventDispatcher eventDispatcher) {
mHostView.setEventDispatcher(eventDispatcher);
}
@Override
public void onHostResume() {
// We show the dialog again when the host resumes
@@ -393,6 +397,7 @@ public class ReactModalHostView extends ViewGroup
private boolean hasAdjustedSize = false;
private int viewWidth;
private int viewHeight;
private EventDispatcher mEventDispatcher;
private final FabricViewStateManager mFabricViewStateManager = new FabricViewStateManager();
@@ -402,6 +407,10 @@ public class ReactModalHostView extends ViewGroup
super(context);
}
private void setEventDispatcher(EventDispatcher eventDispatcher) {
mEventDispatcher = eventDispatcher;
}
@Override
protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
@@ -494,13 +503,13 @@ public class ReactModalHostView extends ViewGroup
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
mJSTouchDispatcher.handleTouchEvent(event, getEventDispatcher());
mJSTouchDispatcher.handleTouchEvent(event, mEventDispatcher);
return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mJSTouchDispatcher.handleTouchEvent(event, getEventDispatcher());
mJSTouchDispatcher.handleTouchEvent(event, mEventDispatcher);
super.onTouchEvent(event);
// In case when there is no children interested in handling touch event, we return true from
// the root view in order to receive subsequent events related to that gesture
@@ -509,7 +518,7 @@ public class ReactModalHostView extends ViewGroup
@Override
public void onChildStartedNativeGesture(MotionEvent androidEvent) {
mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, getEventDispatcher());
mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, mEventDispatcher);
}
@Override
@@ -518,11 +527,6 @@ public class ReactModalHostView extends ViewGroup
// even when some other view disallow that
}
private EventDispatcher getEventDispatcher() {
ReactContext reactContext = getReactContext();
return reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
}
@Override
public FabricViewStateManager getFabricViewStateManager() {
return mFabricViewStateManager;
@@ -26,10 +26,11 @@ import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.ViewManagerDelegate;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.viewmanagers.SliderManagerDelegate;
import com.facebook.react.viewmanagers.SliderManagerInterface;
import com.facebook.yoga.YogaMeasureFunction;
@@ -95,16 +96,13 @@ public class ReactSliderManager extends SimpleViewManager<ReactSlider>
@Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
ReactContext reactContext = (ReactContext) seekbar.getContext();
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
EventDispatcher eventDispatcher =
UIManagerHelper.getEventDispatcherForReactTag(reactContext, seekbar.getId());
if (uiManager != null) {
uiManager
.getEventDispatcher()
.dispatchEvent(
new ReactSliderEvent(
seekbar.getId(),
((ReactSlider) seekbar).toRealProgress(progress),
fromUser));
if (eventDispatcher != null) {
eventDispatcher.dispatchEvent(
new ReactSliderEvent(
seekbar.getId(), ((ReactSlider) seekbar).toRealProgress(progress), fromUser));
}
}
@@ -114,15 +112,14 @@ public class ReactSliderManager extends SimpleViewManager<ReactSlider>
@Override
public void onStopTrackingTouch(SeekBar seekbar) {
ReactContext reactContext = (ReactContext) seekbar.getContext();
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
EventDispatcher eventDispatcher =
UIManagerHelper.getEventDispatcherForReactTag(reactContext, seekbar.getId());
if (uiManager != null) {
uiManager
.getEventDispatcher()
.dispatchEvent(
new ReactSlidingCompleteEvent(
seekbar.getId(),
((ReactSlider) seekbar).toRealProgress(seekbar.getProgress())));
if (eventDispatcher != null) {
eventDispatcher.dispatchEvent(
new ReactSlidingCompleteEvent(
seekbar.getId(),
((ReactSlider) seekbar).toRealProgress(seekbar.getProgress())));
}
}
};
@@ -45,6 +45,7 @@ import com.facebook.react.bridge.ReactSoftException;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.uimanager.FabricViewStateManager;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.views.text.CustomLetterSpacingSpan;
import com.facebook.react.views.text.CustomLineHeightSpan;
import com.facebook.react.views.text.CustomStyleSpan;
@@ -119,6 +120,7 @@ public class ReactEditText extends AppCompatEditText
protected boolean mIsSettingTextFromState = false;
private static final KeyListener sKeyListener = QwertyKeyListener.getInstanceForFullKeyboard();
private @Nullable EventDispatcher mEventDispatcher;
public ReactEditText(Context context) {
super(context);
@@ -247,7 +249,8 @@ public class ReactEditText extends AppCompatEditText
InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
if (inputConnection != null && mOnKeyPress) {
inputConnection =
new ReactEditTextInputConnectionWrapper(inputConnection, reactContext, this);
new ReactEditTextInputConnectionWrapper(
inputConnection, reactContext, this, mEventDispatcher);
}
if (isMultiline() && getBlurOnSubmit()) {
@@ -1043,6 +1046,10 @@ public class ReactEditText extends AppCompatEditText
TextLayoutManager.setCachedSpannabledForTag(getId(), sb);
}
void setEventDispatcher(@Nullable EventDispatcher eventDispatcher) {
mEventDispatcher = eventDispatcher;
}
/**
* This class will redirect *TextChanged calls to the listeners only in the case where the text is
* changed by the user, and not explicitly set by JS.
@@ -12,9 +12,7 @@ import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
/**
@@ -61,11 +59,12 @@ class ReactEditTextInputConnectionWrapper extends InputConnectionWrapper {
private @Nullable String mKey = null;
public ReactEditTextInputConnectionWrapper(
InputConnection target, final ReactContext reactContext, final ReactEditText editText) {
InputConnection target,
final ReactContext reactContext,
final ReactEditText editText,
EventDispatcher eventDispatcher) {
super(target, false);
mEventDispatcher =
Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class))
.getEventDispatcher();
mEventDispatcher = eventDispatcher;
mEditText = editText;
}
@@ -968,6 +968,7 @@ public class ReactTextInputManager extends BaseViewManager<ReactEditText, Layout
@Override
protected void addEventEmitters(
final ThemedReactContext reactContext, final ReactEditText editText) {
editText.setEventDispatcher(getEventDispatcher(reactContext, editText));
editText.addTextChangedListener(new ReactTextInputTextWatcher(reactContext, editText));
editText.setOnFocusChangeListener(
new View.OnFocusChangeListener() {