Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for resource-id #21101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ rn_android_library(
react_native_target("java/com/facebook/react/modules/debug:interfaces"),
react_native_target("java/com/facebook/react/shell:shell"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_target("java/com/facebook/react/uimanager/util:util"),
react_native_target("res:uimanager"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.ViewManagerRegistry;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.uimanager.util.ReactFindViewUtil;
import java.util.Arrays;
import com.facebook.react.uimanager.util.ReactFindViewUtil;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -77,7 +79,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(rootView);

mScreenshotingFrameLayout = new ScreenshotingFrameLayout(this);
mScreenshotingFrameLayout.setId(ROOT_VIEW_ID);
ReactFindViewUtil.setReactTag(mScreenshotingFrameLayout, ROOT_VIEW_ID);
rootView.addView(mScreenshotingFrameLayout);

mReactRootView = new ReactRootView(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.util.ReactFindViewUtil;
import com.facebook.react.views.text.ReactRawTextManager;
import com.facebook.react.views.text.ReactTextViewManager;
import com.facebook.react.views.view.ReactViewManager;
Expand Down Expand Up @@ -97,7 +98,7 @@ public void run() {

public void testFlexUIRendered() {
FrameLayout rootView = createRootView();
jsModule.renderFlexTestApplication(rootView.getId());
jsModule.renderFlexTestApplication(ReactFindViewUtil.getReactTag(rootView));
waitForBridgeAndUIIdle();

assertEquals(1, rootView.getChildCount());
Expand All @@ -121,7 +122,7 @@ public void testFlexUIRendered() {
// Find what could be different and make the test independent of env
// public void testFlexWithTextViews() {
// FrameLayout rootView = createRootView();
// jsModule.renderFlexWithTextApplication(rootView.getId());
// jsModule.renderFlexWithTextApplication(ReactFindViewUtil.getReactTag(rootView));
// waitForBridgeAndUIIdle();
//
// assertEquals(1, rootView.getChildCount());
Expand Down Expand Up @@ -159,7 +160,7 @@ public void testFlexUIRendered() {

public void testAbsolutePositionUIRendered() {
FrameLayout rootView = createRootView();
jsModule.renderAbsolutePositionTestApplication(rootView.getId());
jsModule.renderAbsolutePositionTestApplication(ReactFindViewUtil.getReactTag(rootView));
waitForBridgeAndUIIdle();

assertEquals(1, rootView.getChildCount());
Expand All @@ -173,7 +174,7 @@ public void testAbsolutePositionUIRendered() {

public void testUpdatePositionInList() {
FrameLayout rootView = createRootView();
jsModule.renderUpdatePositionInListTestApplication(rootView.getId());
jsModule.renderUpdatePositionInListTestApplication(ReactFindViewUtil.getReactTag(rootView));
waitForBridgeAndUIIdle();

ViewGroup containerView = getViewByTestId(rootView, "container");
Expand Down Expand Up @@ -202,7 +203,7 @@ public void testUpdatePositionInList() {

public void testAbsolutePositionBottomRightUIRendered() {
FrameLayout rootView = createRootView();
jsModule.renderAbsolutePositionBottomRightTestApplication(rootView.getId());
jsModule.renderAbsolutePositionBottomRightTestApplication(ReactFindViewUtil.getReactTag(rootView));
waitForBridgeAndUIIdle();

assertEquals(1, rootView.getChildCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ public void testMetionsInputColors() throws Throwable {

eventDispatcher.dispatchEvent(
new ReactTextChangedEvent(
reactEditText.getId(),
ReactFindViewUtil.getReactTag(reactEditText),
newText.toString(),
(int) PixelUtil.toDIPFromPixel(contentWidth),
(int) PixelUtil.toDIPFromPixel(contentHeight),
reactEditText.incrementAndGetEventCounter()));

eventDispatcher.dispatchEvent(
new ReactTextInputEvent(
reactEditText.getId(),
ReactFindViewUtil.getReactTag(reactEditText),
newText.toString(),
"",
start,
Expand All @@ -186,15 +186,15 @@ public void testMetionsInputColors() throws Throwable {

eventDispatcher.dispatchEvent(
new ReactTextChangedEvent(
reactEditText.getId(),
ReactFindViewUtil.getReactTag(reactEditText),
newText.toString(),
(int) PixelUtil.toDIPFromPixel(contentWidth),
(int) PixelUtil.toDIPFromPixel(contentHeight),
reactEditText.incrementAndGetEventCounter()));

eventDispatcher.dispatchEvent(
new ReactTextInputEvent(
reactEditText.getId(),
ReactFindViewUtil.getReactTag(reactEditText),
moreText,
"",
start,
Expand All @@ -218,15 +218,15 @@ public void testMetionsInputColors() throws Throwable {

eventDispatcher.dispatchEvent(
new ReactTextChangedEvent(
reactEditText.getId(),
ReactFindViewUtil.getReactTag(reactEditText),
newText.toString(),
(int) PixelUtil.toDIPFromPixel(contentWidth),
(int) PixelUtil.toDIPFromPixel(contentHeight),
reactEditText.incrementAndGetEventCounter()));

eventDispatcher.dispatchEvent(
new ReactTextInputEvent(
reactEditText.getId(),
ReactFindViewUtil.getReactTag(reactEditText),
moreText,
"",
start,
Expand Down
1 change: 1 addition & 0 deletions ReactAndroid/src/main/java/com/facebook/react/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rn_android_library(
react_native_target("java/com/facebook/react/modules/systeminfo:systeminfo"),
react_native_target("java/com/facebook/react/modules/toast:toast"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_target("java/com/facebook/react/uimanager/util:util"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
react_native_target("java/com/facebook/react/views/imagehelper:imagehelper"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.uimanager.util.ReactFindViewUtil;
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
import com.facebook.soloader.SoLoader;
import com.facebook.systrace.Systrace;
Expand Down Expand Up @@ -719,7 +720,7 @@ public void attachRootView(ReactRootView rootView) {

// Reset view content as it's going to be populated by the application content from JS.
rootView.removeAllViews();
rootView.setId(View.NO_ID);
ReactFindViewUtil.setReactTag(rootView, View.NO_ID);

// 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.
Expand Down Expand Up @@ -1077,7 +1078,7 @@ private void tearDownReactContext(ReactContext reactContext) {
synchronized (mAttachedRootViews) {
for (ReactRootView rootView : mAttachedRootViews) {
rootView.removeAllViews();
rootView.setId(View.NO_ID);
ReactFindViewUtil.setReactTag(rootView, View.NO_ID);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rn_android_library(
react_native_dep("third-party/java/okio:okio"),
react_native_target("java/com/facebook/debug/holder:holder"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_target("java/com/facebook/react/uimanager/util:util"),
react_native_target("java/com/facebook/debug/tags:tags"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
Expand Down
3 changes: 2 additions & 1 deletion ReactAndroid/src/main/java/com/facebook/react/touch/BUCK
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "rn_android_library")
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")

rn_android_library(
name = "touch",
Expand All @@ -9,5 +9,6 @@ rn_android_library(
deps = [
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_target("java/com/facebook/react/uimanager/util:util"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import android.view.ViewGroup;
import android.view.ViewParent;

import com.facebook.react.uimanager.util.ReactFindViewUtil;

/**
* This class coordinates JSResponder commands for {@link UIManagerModule}. It should be set as
* OnInterceptTouchEventListener for all newly created native views that implements
Expand Down Expand Up @@ -68,7 +70,7 @@ public boolean onInterceptTouchEvent(ViewGroup v, MotionEvent event) {
// Therefore since "UP" event is the last event in a gesture, we should just let it reach the
// original target that is a child view of {@param v}.
// http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)
return v.getId() == currentJSResponder;
return ReactFindViewUtil.getReactTag(v) == currentJSResponder;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

package com.facebook.react.uimanager;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.IdRes;
import android.view.View;
import android.view.ViewParent;
import com.facebook.react.R;
Expand Down Expand Up @@ -51,6 +53,7 @@ public abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
*/
public static final String PROP_TEST_ID = "testID";
public static final String PROP_NATIVE_ID = "nativeID";
public static final String RESOURCE_ID = "id";

private static MatrixMathHelper.MatrixDecompositionContext sMatrixDecompositionContext =
new MatrixMathHelper.MatrixDecompositionContext();
Expand Down Expand Up @@ -100,6 +103,9 @@ public void setRenderToHardwareTexture(T view, boolean useHWTexture) {

@ReactProp(name = PROP_TEST_ID)
public void setTestId(T view, String testId) {
String packageName = view.getContext().getPackageName();
@IdRes int viewId = view.getResources().getIdentifier(testId, RESOURCE_ID, packageName);
view.setId(viewId);
view.setTag(R.id.react_test_id, testId);

// temporarily set the tag and keyed tags to avoid end to end test regressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.facebook.react.uimanager.common.SizeMonitoringFrameLayout;
import com.facebook.react.uimanager.layoutanimation.LayoutAnimationController;
import com.facebook.react.uimanager.layoutanimation.LayoutAnimationListener;
import com.facebook.react.uimanager.util.ReactFindViewUtil;
import com.facebook.systrace.Systrace;
import com.facebook.systrace.SystraceMessage;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -264,7 +265,7 @@ public synchronized void createView(
// Use android View id field to store React tag. This is possible since we don't inflate
// React views from layout xmls. Thus it is easier to just reuse that field instead of
// creating another (potentially much more expensive) mapping from view to React tag
view.setId(tag);
ReactFindViewUtil.setReactTag(view, tag);
if (initialProps != null) {
viewManager.updateProperties(view, initialProps);
}
Expand All @@ -282,13 +283,15 @@ private static String constructManageChildrenErrorMessage(
StringBuilder stringBuilder = new StringBuilder();

if (null != viewToManage) {
stringBuilder.append("View tag:" + viewToManage.getId() + "\n");
stringBuilder.append("View tag:" + ReactFindViewUtil.getReactTag(viewToManage) + "\n");
stringBuilder.append(" children(" + viewManager.getChildCount(viewToManage) + "): [\n");
for (int index=0; index<viewManager.getChildCount(viewToManage); index+=16) {
for (int innerOffset=0;
((index+innerOffset) < viewManager.getChildCount(viewToManage)) && innerOffset < 16;
innerOffset++) {
stringBuilder.append(viewManager.getChildAt(viewToManage, index+innerOffset).getId() + ",");
View child = viewManager.getChildAt(viewToManage, index+innerOffset);
int reactTag = ReactFindViewUtil.getReactTag(child);
stringBuilder.append(reactTag + ",");
}
stringBuilder.append("\n");
}
Expand Down Expand Up @@ -408,7 +411,7 @@ public synchronized void manageChildren(

if (mLayoutAnimationEnabled &&
mLayoutAnimator.shouldAnimateLayout(viewToRemove) &&
arrayContains(tagsToDelete, viewToRemove.getId())) {
arrayContains(tagsToDelete, ReactFindViewUtil.getReactTag(viewToRemove))) {
// The view will be removed and dropped by the 'delete' layout animation
// instead, so do nothing
} else {
Expand Down Expand Up @@ -542,7 +545,7 @@ protected synchronized final void addRootViewGroup(
int tag,
ViewGroup view,
ThemedReactContext themedContext) {
if (view.getId() != View.NO_ID) {
if (ReactFindViewUtil.getReactTag(view) != View.NO_ID) {
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, " +
Expand All @@ -552,32 +555,33 @@ protected synchronized final void addRootViewGroup(
mTagsToViews.put(tag, view);
mTagsToViewManagers.put(tag, mRootViewManager);
mRootTags.put(tag, true);
view.setId(tag);
ReactFindViewUtil.setReactTag(view, tag);
}

/**
* Releases all references to given native View.
*/
protected synchronized void dropView(View view) {
UiThreadUtil.assertOnUiThread();
if (!mRootTags.get(view.getId())) {
int reactTag = ReactFindViewUtil.getReactTag(view);
if (!mRootTags.get(reactTag)) {
// For non-root views we notify viewmanager with {@link ViewManager#onDropInstance}
resolveViewManager(view.getId()).onDropViewInstance(view);
resolveViewManager(reactTag).onDropViewInstance(view);
}
ViewManager viewManager = mTagsToViewManagers.get(view.getId());
ViewManager viewManager = mTagsToViewManagers.get(reactTag);
if (view instanceof ViewGroup && viewManager instanceof ViewGroupManager) {
ViewGroup viewGroup = (ViewGroup) view;
ViewGroupManager viewGroupManager = (ViewGroupManager) viewManager;
for (int i = viewGroupManager.getChildCount(viewGroup) - 1; i >= 0; i--) {
View child = viewGroupManager.getChildAt(viewGroup, i);
if (mTagsToViews.get(child.getId()) != null) {
if (mTagsToViews.get(ReactFindViewUtil.getReactTag(child)) != null) {
dropView(child);
}
}
viewGroupManager.removeAllViews(viewGroup);
}
mTagsToViews.remove(view.getId());
mTagsToViewManagers.remove(view.getId());
mTagsToViews.remove(reactTag);
mTagsToViewManagers.remove(reactTag);
}

public synchronized void removeRootView(int rootViewTag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.touch.ReactHitSlopView;
import com.facebook.react.uimanager.util.ReactFindViewUtil;

/**
* Class responsible for identifying which react view should handle a given {@link MotionEvent}.
Expand Down Expand Up @@ -85,7 +86,7 @@ public static int findTargetTagAndCoordinatesForTouch(
float[] viewCoords,
@Nullable int[] nativeViewTag) {
UiThreadUtil.assertOnUiThread();
int targetTag = viewGroup.getId();
int targetTag = ReactFindViewUtil.getReactTag(viewGroup);
// Store eventCoords in array so that they are modified to be relative to the targetView found.
viewCoords[0] = eventX;
viewCoords[1] = eventY;
Expand All @@ -94,7 +95,7 @@ public static int findTargetTagAndCoordinatesForTouch(
View reactTargetView = findClosestReactAncestor(nativeTargetView);
if (reactTargetView != null) {
if (nativeViewTag != null) {
nativeViewTag[0] = reactTargetView.getId();
nativeViewTag[0] = ReactFindViewUtil.getReactTag(reactTargetView);
}
targetTag = getTouchTargetForView(reactTargetView, viewCoords[0], viewCoords[1]);
}
Expand All @@ -103,7 +104,7 @@ public static int findTargetTagAndCoordinatesForTouch(
}

private static View findClosestReactAncestor(View view) {
while (view != null && view.getId() <= 0) {
while (view != null && ReactFindViewUtil.getReactTag(view) <= 0) {
view = (View) view.getParent();
}
return view;
Expand Down Expand Up @@ -239,7 +240,7 @@ private static boolean isTransformedTouchPointInView(
// ViewGroup).
if (view instanceof ReactCompoundView) {
int reactTag = ((ReactCompoundView)view).reactTagForTouch(eventCoords[0], eventCoords[1]);
if (reactTag != view.getId()) {
if (reactTag != ReactFindViewUtil.getReactTag(view)) {
// make sure we exclude the View itself because of the PointerEvents.BOX_NONE
return view;
}
Expand Down Expand Up @@ -271,7 +272,7 @@ private static int getTouchTargetForView(View targetView, float eventX, float ev
// {@link #findTouchTargetView()}.
return ((ReactCompoundView) targetView).reactTagForTouch(eventX, eventY);
}
return targetView.getId();
return ReactFindViewUtil.getReactTag(targetView);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,13 @@ public static void notifyViewRendered(View view) {
Object tag = view.getTag(R.id.view_tag_native_id);
return tag instanceof String ? (String) tag : null;
}

public static Integer getReactTag(View view) {
Object tag = view.getTag(R.id.react_tag_id);
return tag instanceof Integer ? (Integer) tag : View.NO_ID;
}

public static void setReactTag(View view, int reactTag) {
view.setTag(R.id.react_tag_id, reactTag);
}
}
Loading