Permalink
Browse files

don't block attaching ReactRootView on measuring

Reviewed By: achen1

Differential Revision: D5117394

fbshipit-source-id: 00f65a59247a75d4b42240fe25935aa9bd8948b1
  • Loading branch information...
AaaChiuuu authored and facebook-github-bot committed May 31, 2017
1 parent 23a34d4 commit 8125ce520df54ce29f8e70bcf25e4eef7623be3c
@@ -25,7 +25,6 @@
import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
import com.facebook.react.modules.systeminfo.AndroidInfoModule;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.UIImplementation;
import com.facebook.react.uimanager.UIImplementationProvider;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
@@ -66,7 +65,7 @@ private ReactRootView createRootView() {
final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
rootView.setLayoutParams(
new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels));
uiManager.addMeasuredRootView(rootView);
uiManager.addRootView(rootView);
// We add the root view by posting to the main thread so wait for that to complete so that the
// root view tag is added to the view
waitForIdleSync();
@@ -228,7 +227,7 @@ public void testAbsolutePositionBottomRightUIRendered() {
public void _testCenteredText(String text) {
ReactRootView rootView = new ReactRootView(getContext());
int rootTag = uiManager.addMeasuredRootView(rootView);
int rootTag = uiManager.addRootView(rootView);
jsModule.renderCenteredTextViewTestApplication(rootTag, text);
waitForBridgeAndUIIdle();
@@ -27,7 +27,6 @@
import com.facebook.react.modules.appstate.AppStateModule;
import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
import com.facebook.react.modules.systeminfo.AndroidInfoModule;
import com.facebook.react.uimanager.UIImplementation;
import com.facebook.react.uimanager.UIImplementationProvider;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
@@ -98,7 +97,7 @@ public void run() {
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
mRootView.setLayoutParams(
new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels));
int rootTag = mUIManager.addMeasuredRootView(mRootView);
int rootTag = mUIManager.addRootView(mRootView);
mInstance.getJSModule(ProgressBarTestModule.class).renderProgressBarApplication(rootTag);
waitForBridgeAndUIIdle();
}
@@ -23,7 +23,6 @@
import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
import com.facebook.react.modules.systeminfo.AndroidInfoModule;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.UIImplementation;
import com.facebook.react.uimanager.UIImplementationProvider;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
@@ -76,7 +75,7 @@ public void run() {
.build();
mRootView = new ReactRootView(getContext());
mRootTag = uiManager.addMeasuredRootView(mRootView);
mRootTag = uiManager.addRootView(mRootView);
}
public void testViewRenderedWithCorrectProperties() {
@@ -613,7 +613,7 @@ public void showDevOptionsDialog() {
* be re-attached.
*/
@ThreadConfined(UI)
public void attachMeasuredRootView(ReactRootView rootView) {
public void attachRootView(ReactRootView rootView) {
UiThreadUtil.assertOnUiThread();
mAttachedRootViews.add(rootView);
@@ -624,7 +624,7 @@ public void attachMeasuredRootView(ReactRootView rootView) {
// If react context is being created in the background, JS application will be started
// automatically when creation completes, as root view is part of the attached root view list.
if (mCreateReactContextThread == null && mCurrentReactContext != null) {
attachMeasuredRootViewToInstance(rootView, mCurrentReactContext.getCatalystInstance());
attachRootViewToInstance(rootView, mCurrentReactContext.getCatalystInstance());
}
}
@@ -814,7 +814,7 @@ private void setupReactContext(ReactApplicationContext reactContext) {
ReactMarker.logMarker(ATTACH_MEASURED_ROOT_VIEWS_START);
synchronized (mAttachedRootViews) {
for (ReactRootView rootView : mAttachedRootViews) {
attachMeasuredRootViewToInstance(rootView, catalystInstance);
attachRootViewToInstance(rootView, catalystInstance);
}
}
ReactMarker.logMarker(ATTACH_MEASURED_ROOT_VIEWS_END);
@@ -850,16 +850,16 @@ public void run() {
}
}
private void attachMeasuredRootViewToInstance(
private void attachRootViewToInstance(
final ReactRootView rootView,
CatalystInstance catalystInstance) {
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachMeasuredRootViewToInstance");
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachRootViewToInstance");
if (!mSetupReactContextInBackgroundEnabled) {
UiThreadUtil.assertOnUiThread();
}
UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
final int rootTag = uiManagerModule.addMeasuredRootView(rootView);
final int rootTag = uiManagerModule.addRootView(rootView);
rootView.setRootViewTag(rootTag);
rootView.runApplication();
Systrace.beginAsyncSection(
@@ -78,8 +78,7 @@
private @Nullable CustomGlobalLayoutListener mCustomGlobalLayoutListener;
private @Nullable ReactRootViewEventListener mRootViewEventListener;
private int mRootViewTag;
private boolean mWasMeasured = false;
private boolean mIsAttachedToInstance = false;
private boolean mIsAttachedToInstance;
private final JSTouchDispatcher mJSTouchDispatcher = new JSTouchDispatcher(this);
public ReactRootView(Context context) {
@@ -102,7 +101,6 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
mWasMeasured = true;
// Check if we were waiting for onMeasure to attach the root view.
if (mReactInstanceManager != null && !mIsAttachedToInstance) {
attachToReactInstanceManager();
@@ -222,12 +220,7 @@ public void startReactApplication(
mReactInstanceManager.createReactContextInBackground();
}
// We need to wait for the initial onMeasure, if this view has not yet been measured, we set
// which will make this view startReactApplication itself to instance manager once onMeasure
// is called.
if (mWasMeasured) {
attachToReactInstanceManager();
}
attachToReactInstanceManager();
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
@@ -303,13 +296,12 @@ public void setAppProperties(@Nullable Bundle appProperties) {
}
/**
* Is used by unit test to setup mWasMeasured and mIsAttachedToWindow flags, that will let this
* Is used by unit test to setup mIsAttachedToWindow flags, that will let this
* view to be properly attached to catalyst instance by startReactApplication call
*/
@VisibleForTesting
/* package */ void simulateAttachForTesting() {
mIsAttachedToInstance = true;
mWasMeasured = true;
}
private CustomGlobalLayoutListener getCustomGlobalLayoutListener() {
@@ -327,7 +319,7 @@ private void attachToReactInstanceManager() {
}
mIsAttachedToInstance = true;
Assertions.assertNotNull(mReactInstanceManager).attachMeasuredRootView(this);
Assertions.assertNotNull(mReactInstanceManager).attachRootView(this);
getViewTreeObserver().addOnGlobalLayoutListener(getCustomGlobalLayoutListener());
} finally {
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
@@ -362,6 +354,7 @@ public void setRootViewTag(int rootViewTag) {
private int mDeviceRotation = 0;
/* package */ CustomGlobalLayoutListener() {
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(getContext().getApplicationContext());
mVisibleViewArea = new Rect();
mMinKeyboardHeightDetected = (int) PixelUtil.toPixelFromDIP(60);
}
@@ -487,7 +487,7 @@ public synchronized void setChildren(
}
/**
* See {@link UIManagerModule#addMeasuredRootView}.
* See {@link UIManagerModule#addRootView}.
*/
public synchronized void addRootView(
int tag,
@@ -504,7 +504,7 @@ protected synchronized final void addRootViewGroup(
throw new IllegalViewOperationException(
"Trying to add a root view with an explicit id already set. React Native uses " +
"the id field to track react tags and will overwrite this field. If that is fine, " +
"explicitly overwrite the id field to View.NO_ID before calling addMeasuredRootView.");
"explicitly overwrite the id field to View.NO_ID before calling addRootView.");
}
mTagsToViews.put(tag, view);
@@ -52,5 +52,4 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mOnSizeChangedListener.onSizeChanged(w, h, oldw, oldh);
}
}
}
@@ -14,7 +14,6 @@
import java.util.List;
import com.facebook.common.logging.FLog;
import com.facebook.yoga.YogaDirection;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.animation.Animation;
import com.facebook.react.bridge.Arguments;
@@ -30,6 +29,7 @@
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.SystraceMessage;
import com.facebook.yoga.YogaDirection;
/**
* An class that is used to receive React commands from JS and translate them into a
@@ -181,22 +181,22 @@ public void onCatalystInstanceDestroy() {
* Note that this must be called after getWidth()/getHeight() actually return something. See
* CatalystApplicationFragment as an example.
*
* TODO(6242243): Make addMeasuredRootView thread safe
* TODO(6242243): Make addRootView thread safe
* NB: this method is horribly not-thread-safe.
*/
public int addMeasuredRootView(final SizeMonitoringFrameLayout rootView) {
public int addRootView(final SizeMonitoringFrameLayout rootView) {
Systrace.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"UIManagerModule.addMeasuredRootView");
"UIManagerModule.addRootView");
final int tag = mNextRootViewTag;
mNextRootViewTag += ROOT_VIEW_TAG_INCREMENT;
final int width;
final int height;
// If LayoutParams sets size explicitly, we can use that. Otherwise get the size from the view.
if (rootView.getLayoutParams() != null &&
rootView.getLayoutParams().width > 0 &&
rootView.getLayoutParams().height > 0) {
rootView.getLayoutParams().width > 0 &&
rootView.getLayoutParams().height > 0) {
width = rootView.getLayoutParams().width;
height = rootView.getLayoutParams().height;
} else {
@@ -206,7 +206,7 @@ public int addMeasuredRootView(final SizeMonitoringFrameLayout rootView) {
final ReactApplicationContext reactApplicationContext = getReactApplicationContext();
final ThemedReactContext themedRootContext =
new ThemedReactContext(reactApplicationContext, rootView.getContext());
new ThemedReactContext(reactApplicationContext, rootView.getContext());
mUIImplementation.registerRootView(rootView, tag, width, height, themedRootContext);
@@ -151,7 +151,7 @@ public void testHierarchyWithView() {
ReactRootView rootView =
new ReactRootView(RuntimeEnvironment.application.getApplicationContext());
int rootTag = uiManager.addMeasuredRootView(rootView);
int rootTag = uiManager.addRootView(rootView);
int viewTag = rootTag + 1;
int subViewTag = viewTag + 1;
@@ -609,7 +609,7 @@ public void testRemoveSubviewsFromContainerWithID() {
UIManagerModule uiManager = getUIManagerModule();
ReactRootView rootView =
new ReactRootView(RuntimeEnvironment.application.getApplicationContext());
int rootTag = uiManager.addMeasuredRootView(rootView);
int rootTag = uiManager.addRootView(rootView);
final int containerTag = rootTag + 1;
final int containerSiblingTag = containerTag + 1;
@@ -662,7 +662,7 @@ public void testRemoveSubviewsFromContainerWithID() {
private ViewGroup createSimpleTextHierarchy(UIManagerModule uiManager, String text) {
ReactRootView rootView =
new ReactRootView(RuntimeEnvironment.application.getApplicationContext());
int rootTag = uiManager.addMeasuredRootView(rootView);
int rootTag = uiManager.addRootView(rootView);
int textTag = rootTag + 1;
int rawTextTag = textTag + 1;
@@ -701,7 +701,7 @@ private ViewGroup createSimpleTextHierarchy(UIManagerModule uiManager, String te
private TestMoveDeleteHierarchy createMoveDeleteHierarchy(UIManagerModule uiManager) {
ReactRootView rootView = new ReactRootView(mReactContext);
int rootTag = uiManager.addMeasuredRootView(rootView);
int rootTag = uiManager.addRootView(rootView);
TestMoveDeleteHierarchy hierarchy = new TestMoveDeleteHierarchy(rootView, rootTag);
@@ -379,7 +379,7 @@ private ReactRootView createText(
JavaOnlyMap textProps,
JavaOnlyMap rawTextProps) {
ReactRootView rootView = new ReactRootView(RuntimeEnvironment.application);
int rootTag = uiManager.addMeasuredRootView(rootView);
int rootTag = uiManager.addRootView(rootView);
int textTag = rootTag + 1;
int rawTextTag = textTag + 1;
@@ -91,7 +91,7 @@ public void testPropsApplied() {
ReactRootView rootView = new ReactRootView(RuntimeEnvironment.application);
rootView.setLayoutParams(new ReactRootView.LayoutParams(100, 100));
int rootTag = uiManager.addMeasuredRootView(rootView);
int rootTag = uiManager.addRootView(rootView);
int textInputTag = rootTag + 1;
final String hintStr = "placeholder text";
@@ -125,7 +125,7 @@ public void testPropsUpdate() {
ReactRootView rootView = new ReactRootView(RuntimeEnvironment.application);
rootView.setLayoutParams(new ReactRootView.LayoutParams(100, 100));
int rootTag = uiManager.addMeasuredRootView(rootView);
int rootTag = uiManager.addRootView(rootView);
int textInputTag = rootTag + 1;
final String hintStr = "placeholder text";

0 comments on commit 8125ce5

Please sign in to comment.