Convert Bridge-only checks to overridable functions

Summary:
Convert Bridge-only checks to overridable functions and make Bridgeless override them in ReactSurfaceView so that the checks will work for Bridgeless as well.

Issue fixed:
https://fb.workplace.com/groups/rn.support/permalink/24231137939841493/

Changelog:
[Android][Changed] - Convert Bridge-only calls to overridable functions

Reviewed By: javache

Differential Revision: D43063348

fbshipit-source-id: a1c181d27c1669f6033f3fb783c5a668b7c2585b
This commit is contained in:
Lulu Wu
2023-02-07 13:10:43 -08:00
committed by Facebook GitHub Bot
parent 9d98e2cb07
commit 1058bb8096
@@ -179,7 +179,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
mWasMeasured = true;
// Check if we were waiting for onMeasure to attach the root view.
if (mReactInstanceManager != null && !mIsAttachedToInstance) {
if (hasActiveReactInstance() && !isViewAttachedToReactInstance()) {
attachToReactInstanceManager();
} else if (measureSpecsUpdated || mLastWidth != width || mLastHeight != height) {
updateRootLayoutSpecs(true, mWidthMeasureSpec, mHeightMeasureSpec);
@@ -203,7 +203,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
if (!isDispatcherReady()) {
return;
}
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
ReactContext reactContext = getCurrentReactContext();
UIManager uiManager = UIManagerHelper.getUIManager(reactContext, getUIManagerType());
if (uiManager != null) {
@@ -220,7 +220,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
if (!isDispatcherReady()) {
return;
}
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
ReactContext reactContext = getCurrentReactContext();
UIManager uiManager = UIManagerHelper.getUIManager(reactContext, getUIManagerType());
if (uiManager != null) {
@@ -233,9 +233,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
}
private boolean isDispatcherReady() {
if (mReactInstanceManager == null
|| !mIsAttachedToInstance
|| mReactInstanceManager.getCurrentReactContext() == null) {
if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) {
FLog.w(TAG, "Unable to dispatch touch to JS as the catalyst instance has not been attached");
return false;
}
@@ -303,9 +301,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
@Override
public boolean dispatchKeyEvent(KeyEvent ev) {
if (mReactInstanceManager == null
|| !mIsAttachedToInstance
|| mReactInstanceManager.getCurrentReactContext() == null) {
if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) {
FLog.w(TAG, "Unable to handle key event as the catalyst instance has not been attached");
return super.dispatchKeyEvent(ev);
}
@@ -315,9 +311,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
if (mReactInstanceManager == null
|| !mIsAttachedToInstance
|| mReactInstanceManager.getCurrentReactContext() == null) {
if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) {
FLog.w(
TAG,
"Unable to handle focus changed event as the catalyst instance has not been attached");
@@ -330,9 +324,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
@Override
public void requestChildFocus(View child, View focused) {
if (mReactInstanceManager == null
|| !mIsAttachedToInstance
|| mReactInstanceManager.getCurrentReactContext() == null) {
if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) {
FLog.w(
TAG,
"Unable to handle child focus changed event as the catalyst instance has not been attached");
@@ -344,9 +336,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
}
protected void dispatchJSPointerEvent(MotionEvent event, boolean isCapture) {
if (mReactInstanceManager == null
|| !mIsAttachedToInstance
|| mReactInstanceManager.getCurrentReactContext() == null) {
if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) {
FLog.w(TAG, "Unable to dispatch touch to JS as the catalyst instance has not been attached");
return;
}
@@ -357,7 +347,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
FLog.w(TAG, "Unable to dispatch pointer events to JS before the dispatcher is available");
return;
}
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
ReactContext reactContext = getCurrentReactContext();
UIManager uiManager = UIManagerHelper.getUIManager(reactContext, getUIManagerType());
if (uiManager != null) {
@@ -367,9 +357,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
}
protected void dispatchJSTouchEvent(MotionEvent event) {
if (mReactInstanceManager == null
|| !mIsAttachedToInstance
|| mReactInstanceManager.getCurrentReactContext() == null) {
if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) {
FLog.w(TAG, "Unable to dispatch touch to JS as the catalyst instance has not been attached");
return;
}
@@ -377,7 +365,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
FLog.w(TAG, "Unable to dispatch touch to JS before the dispatcher is available");
return;
}
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
ReactContext reactContext = getCurrentReactContext();
UIManager uiManager = UIManagerHelper.getUIManager(reactContext, getUIManagerType());
if (uiManager != null) {
@@ -412,7 +400,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (mIsAttachedToInstance) {
if (isViewAttachedToReactInstance()) {
removeOnGlobalLayoutListener();
getViewTreeObserver().addOnGlobalLayoutListener(getCustomGlobalLayoutListener());
}
@@ -421,7 +409,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mIsAttachedToInstance) {
if (isViewAttachedToReactInstance()) {
removeOnGlobalLayoutListener();
}
}
@@ -571,7 +559,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
private void updateRootLayoutSpecs(
boolean measureSpecsChanged, final int widthMeasureSpec, final int heightMeasureSpec) {
ReactMarker.logMarker(ReactMarkerConstants.ROOT_VIEW_UPDATE_LAYOUT_SPECS_START);
if (mReactInstanceManager == null) {
if (!hasActiveReactInstance()) {
ReactMarker.logMarker(ReactMarkerConstants.ROOT_VIEW_UPDATE_LAYOUT_SPECS_END);
FLog.w(TAG, "Unable to update root layout specs for uninitialized ReactInstanceManager");
return;
@@ -585,7 +573,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
return;
}
final ReactContext reactApplicationContext = mReactInstanceManager.getCurrentReactContext();
final ReactContext reactApplicationContext = getCurrentReactContext();
if (reactApplicationContext != null) {
@Nullable
@@ -629,8 +617,8 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
// to be committed via the Scheduler, which will cause mounting instructions
// to be queued up and synchronously executed to delete and remove
// all the views in the hierarchy.
if (mReactInstanceManager != null) {
final ReactContext reactApplicationContext = mReactInstanceManager.getCurrentReactContext();
if (hasActiveReactInstance()) {
final ReactContext reactApplicationContext = getCurrentReactContext();
if (reactApplicationContext != null && isFabric()) {
@Nullable
UIManager uiManager =
@@ -729,11 +717,11 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
public void runApplication() {
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactRootView.runApplication");
try {
if (mReactInstanceManager == null || !mIsAttachedToInstance) {
if (!hasActiveReactInstance() || !isViewAttachedToReactInstance()) {
return;
}
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
ReactContext reactContext = getCurrentReactContext();
if (reactContext == null) {
return;
}
@@ -853,12 +841,12 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
@Override
public void handleException(final Throwable t) {
if (mReactInstanceManager == null || mReactInstanceManager.getCurrentReactContext() == null) {
if (!hasActiveReactContext()) {
throw new RuntimeException(t);
}
Exception e = new IllegalViewOperationException(t.getMessage(), this, t);
mReactInstanceManager.getCurrentReactContext().handleException(e);
getCurrentReactContext().handleException(e);
}
public void setIsFabric(boolean isFabric) {
@@ -876,14 +864,30 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
}
/* package */ void sendEvent(String eventName, @Nullable WritableMap params) {
if (mReactInstanceManager != null) {
mReactInstanceManager
.getCurrentReactContext()
if (hasActiveReactInstance()) {
getCurrentReactContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
}
public boolean hasActiveReactInstance() {
return mReactInstanceManager != null;
}
public boolean hasActiveReactContext() {
return mReactInstanceManager != null && mReactInstanceManager.getCurrentReactContext() != null;
}
@Nullable
public ReactContext getCurrentReactContext() {
return mReactInstanceManager.getCurrentReactContext();
}
public boolean isViewAttachedToReactInstance() {
return mIsAttachedToInstance;
}
private class CustomGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private final Rect mVisibleViewArea;
private final int mMinKeyboardHeightDetected;
@@ -900,9 +904,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
@Override
public void onGlobalLayout() {
if (mReactInstanceManager == null
|| !mIsAttachedToInstance
|| mReactInstanceManager.getCurrentReactContext() == null) {
if (!hasActiveReactContext() || !isViewAttachedToReactInstance()) {
return;
}
@@ -1061,7 +1063,7 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
private void emitUpdateDimensionsEvent() {
DeviceInfoModule deviceInfo =
mReactInstanceManager.getCurrentReactContext().getNativeModule(DeviceInfoModule.class);
getCurrentReactContext().getNativeModule(DeviceInfoModule.class);
if (deviceInfo != null) {
deviceInfo.emitUpdateDimensionsEvent();