refactor out RootView tag number logic

Reviewed By: achen1

Differential Revision: D5609280

fbshipit-source-id: bc0c9f50b2938f05d5e43f50491ff6f0de154fb6
This commit is contained in:
Aaron Chiu 2017-08-15 10:41:15 -07:00 committed by Facebook Github Bot
parent ab9c788c2c
commit 353cb61400
5 changed files with 50 additions and 33 deletions

View File

@ -930,7 +930,6 @@ public class ReactInstanceManager {
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachRootViewToInstance"); Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachRootViewToInstance");
UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class); UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
final int rootTag = uiManagerModule.addRootView(rootView); final int rootTag = uiManagerModule.addRootView(rootView);
rootView.setRootViewTag(rootTag);
rootView.runApplication(); rootView.runApplication();
Systrace.beginAsyncSection( Systrace.beginAsyncSection(
TRACE_TAG_REACT_JAVA_BRIDGE, TRACE_TAG_REACT_JAVA_BRIDGE,

View File

@ -41,8 +41,10 @@ import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
import com.facebook.react.uimanager.DisplayMetricsHolder; import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.JSTouchDispatcher; import com.facebook.react.uimanager.JSTouchDispatcher;
import com.facebook.react.uimanager.PixelUtil; import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactRootViewTagGenerator;
import com.facebook.react.uimanager.RootView; import com.facebook.react.uimanager.RootView;
import com.facebook.react.uimanager.SizeMonitoringFrameLayout; import com.facebook.react.uimanager.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.TaggedRootView;
import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher; import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.systrace.Systrace; import com.facebook.systrace.Systrace;
@ -50,19 +52,17 @@ import javax.annotation.Nullable;
/** /**
* Default root view for catalyst apps. Provides the ability to listen for size changes so that a UI * Default root view for catalyst apps. Provides the ability to listen for size changes so that a UI
* manager can re-layout its elements. * manager can re-layout its elements. It delegates handling touch events for itself and child views
* It delegates handling touch events for itself and child views and sending those events to JS by * and sending those events to JS by using JSTouchDispatcher. This view is overriding {@link
* using JSTouchDispatcher. * ViewGroup#onInterceptTouchEvent} method in order to be notified about the events for all of its
* This view is overriding {@link ViewGroup#onInterceptTouchEvent} method in order to be notified * children and it's also overriding {@link ViewGroup#requestDisallowInterceptTouchEvent} to make
* about the events for all of its children and it's also overriding * sure that {@link ViewGroup#onInterceptTouchEvent} will get events even when some child view start
* {@link ViewGroup#requestDisallowInterceptTouchEvent} to make sure that * intercepting it. In case when no child view is interested in handling some particular touch event
* {@link ViewGroup#onInterceptTouchEvent} will get events even when some child view start * this view's {@link View#onTouchEvent} will still return true in order to be notified about all
* intercepting it. In case when no child view is interested in handling some particular * subsequent touch events related to that gesture (in case when JS code want to handle that
* touch event this view's {@link View#onTouchEvent} will still return true in order to be notified * gesture).
* about all subsequent touch events related to that gesture (in case when JS code want to handle
* that gesture).
*/ */
public class ReactRootView extends SizeMonitoringFrameLayout implements RootView { public class ReactRootView extends SizeMonitoringFrameLayout implements RootView, TaggedRootView {
/** /**
* Listener interface for react root view events * Listener interface for react root view events
@ -79,7 +79,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
private @Nullable Bundle mAppProperties; private @Nullable Bundle mAppProperties;
private @Nullable CustomGlobalLayoutListener mCustomGlobalLayoutListener; private @Nullable CustomGlobalLayoutListener mCustomGlobalLayoutListener;
private @Nullable ReactRootViewEventListener mRootViewEventListener; private @Nullable ReactRootViewEventListener mRootViewEventListener;
private int mRootViewTag; private int mRootViewTag = ReactRootViewTagGenerator.getNextRootViewTag();
private boolean mIsAttachedToInstance; private boolean mIsAttachedToInstance;
private boolean mContentAppeared; private boolean mContentAppeared;
private final JSTouchDispatcher mJSTouchDispatcher = new JSTouchDispatcher(this); private final JSTouchDispatcher mJSTouchDispatcher = new JSTouchDispatcher(this);
@ -261,6 +261,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
if (mReactInstanceManager != null && mIsAttachedToInstance) { if (mReactInstanceManager != null && mIsAttachedToInstance) {
mReactInstanceManager.detachRootView(this); mReactInstanceManager.detachRootView(this);
mIsAttachedToInstance = false; mIsAttachedToInstance = false;
mRootViewTag = ReactRootViewTagGenerator.getNextRootViewTag();
} }
} }
@ -363,14 +364,11 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
"the onDestroyView() of your hosting Fragment."); "the onDestroyView() of your hosting Fragment.");
} }
@Override
public int getRootViewTag() { public int getRootViewTag() {
return mRootViewTag; return mRootViewTag;
} }
public void setRootViewTag(int rootViewTag) {
mRootViewTag = rootViewTag;
}
private class CustomGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener { private class CustomGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private final Rect mVisibleViewArea; private final Rect mVisibleViewArea;
private final int mMinKeyboardHeightDetected; private final int mMinKeyboardHeightDetected;

View File

@ -0,0 +1,19 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.uimanager;
/** Incremental counter for React Root View tag. */
public class ReactRootViewTagGenerator {
// Keep in sync with ReactIOSTagHandles JS module - see that file for an explanation on why the
// increment here is 10.
private static final int ROOT_VIEW_TAG_INCREMENT = 10;
private static int sNextRootViewTag = 1;
public static synchronized int getNextRootViewTag() {
final int tag = sNextRootViewTag;
sNextRootViewTag += ROOT_VIEW_TAG_INCREMENT;
return tag;
}
}

View File

@ -0,0 +1,9 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.uimanager;
/** Interface for the a ReactRootView with a tag. */
public interface TaggedRootView {
int getRootViewTag();
}

View File

@ -9,15 +9,11 @@
package com.facebook.react.uimanager; package com.facebook.react.uimanager;
import javax.annotation.Nullable; import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_CONSTANTS_END;
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_CONSTANTS_START;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.ComponentCallbacks2; import android.content.ComponentCallbacks2;
import android.content.res.Configuration; import android.content.res.Configuration;
import com.facebook.common.logging.FLog; import com.facebook.common.logging.FLog;
import com.facebook.react.animation.Animation; import com.facebook.react.animation.Animation;
import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Callback;
@ -37,9 +33,10 @@ import com.facebook.react.uimanager.debug.NotThreadSafeViewHierarchyUpdateDebugL
import com.facebook.react.uimanager.events.EventDispatcher; import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.systrace.Systrace; import com.facebook.systrace.Systrace;
import com.facebook.systrace.SystraceMessage; import com.facebook.systrace.SystraceMessage;
import java.util.HashMap;
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_CONSTANTS_END; import java.util.List;
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_CONSTANTS_START; import java.util.Map;
import javax.annotation.Nullable;
/** /**
* <p>Native module to allow JS to create and update native Views.</p> * <p>Native module to allow JS to create and update native Views.</p>
@ -76,9 +73,6 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
protected static final String NAME = "UIManager"; protected static final String NAME = "UIManager";
// Keep in sync with ReactIOSTagHandles JS module - see that file for an explanation on why the
// increment here is 10
private static final int ROOT_VIEW_TAG_INCREMENT = 10;
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
private final EventDispatcher mEventDispatcher; private final EventDispatcher mEventDispatcher;
@ -86,7 +80,6 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
private final UIImplementation mUIImplementation; private final UIImplementation mUIImplementation;
private final MemoryTrimCallback mMemoryTrimCallback = new MemoryTrimCallback(); private final MemoryTrimCallback mMemoryTrimCallback = new MemoryTrimCallback();
private int mNextRootViewTag = 1;
private int mBatchId = 0; private int mBatchId = 0;
public UIManagerModule( public UIManagerModule(
@ -188,8 +181,7 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
Systrace.beginSection( Systrace.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"UIManagerModule.addRootView"); "UIManagerModule.addRootView");
final int tag = mNextRootViewTag; final int tag = ((TaggedRootView) rootView).getRootViewTag();
mNextRootViewTag += ROOT_VIEW_TAG_INCREMENT;
final int width; final int width;
final int height; final int height;