forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 2
/
UIManagerHelper.java
218 lines (199 loc) · 8.63 KB
/
UIManagerHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.uimanager;
import static com.facebook.react.uimanager.common.UIManagerType.FABRIC;
import static com.facebook.react.uimanager.common.ViewUtil.getUIManagerType;
import android.content.Context;
import android.content.ContextWrapper;
import android.view.View;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.JSIModuleType;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactNoCrashSoftException;
import com.facebook.react.bridge.ReactSoftExceptionLogger;
import com.facebook.react.bridge.UIManager;
import com.facebook.react.uimanager.common.UIManagerType;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.uimanager.events.EventDispatcherProvider;
/** Helper class for {@link UIManager}. */
public class UIManagerHelper {
private static final String TAG = UIManagerHelper.class.getName();
public static final int PADDING_START_INDEX = 0;
public static final int PADDING_END_INDEX = 1;
public static final int PADDING_TOP_INDEX = 2;
public static final int PADDING_BOTTOM_INDEX = 3;
/** @return a {@link UIManager} that can handle the react tag received by parameter. */
@Nullable
public static UIManager getUIManagerForReactTag(ReactContext context, int reactTag) {
return getUIManager(context, getUIManagerType(reactTag));
}
/** @return a {@link UIManager} that can handle the react tag received by parameter. */
@Nullable
public static UIManager getUIManager(ReactContext context, @UIManagerType int uiManagerType) {
return getUIManager(context, uiManagerType, true);
}
@Nullable
private static UIManager getUIManager(
ReactContext context,
@UIManagerType int uiManagerType,
boolean returnNullIfCatalystIsInactive) {
if (context.isBridgeless()) {
@Nullable UIManager uiManager = (UIManager) context.getJSIModule(JSIModuleType.UIManager);
if (uiManager == null) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Cannot get UIManager because the instance hasn't been initialized yet."));
return null;
}
return uiManager;
}
if (!context.hasCatalystInstance()) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Cannot get UIManager because the context doesn't contain a CatalystInstance."));
return null;
}
// TODO T60461551: add tests to verify emission of events when the ReactContext is being turn
// down.
if (!context.hasActiveReactInstance()) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Cannot get UIManager because the context doesn't contain an active CatalystInstance."));
if (returnNullIfCatalystIsInactive) {
return null;
}
}
CatalystInstance catalystInstance = context.getCatalystInstance();
try {
return uiManagerType == FABRIC
? (UIManager) catalystInstance.getJSIModule(JSIModuleType.UIManager)
: catalystInstance.getNativeModule(UIManagerModule.class);
} catch (IllegalArgumentException ex) {
// TODO T67518514 Clean this up once we migrate everything over to bridgeless mode
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Cannot get UIManager for UIManagerType: " + uiManagerType));
return catalystInstance.getNativeModule(UIManagerModule.class);
}
}
/**
* @return the {@link EventDispatcher} that handles events for the reactTag received as a
* parameter.
*/
@Nullable
public static EventDispatcher getEventDispatcherForReactTag(ReactContext context, int reactTag) {
EventDispatcher eventDispatcher = getEventDispatcher(context, getUIManagerType(reactTag));
if (eventDispatcher == null) {
ReactSoftExceptionLogger.logSoftException(
TAG, new IllegalStateException("Cannot get EventDispatcher for reactTag " + reactTag));
}
return eventDispatcher;
}
/**
* @return the {@link EventDispatcher} that handles events for the {@link UIManagerType} received
* as a parameter.
*/
@Nullable
public static EventDispatcher getEventDispatcher(
ReactContext context, @UIManagerType int uiManagerType) {
// TODO T67518514 Clean this up once we migrate everything over to bridgeless mode
if (context.isBridgeless()) {
if (context instanceof ThemedReactContext) {
context = ((ThemedReactContext) context).getReactApplicationContext();
}
return ((EventDispatcherProvider) context).getEventDispatcher();
}
UIManager uiManager = getUIManager(context, uiManagerType, false);
if (uiManager == null) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Unable to find UIManager for UIManagerType " + uiManagerType));
return null;
}
EventDispatcher eventDispatcher = (EventDispatcher) uiManager.getEventDispatcher();
if (eventDispatcher == null) {
ReactSoftExceptionLogger.logSoftException(
TAG,
new IllegalStateException(
"Cannot get EventDispatcher for UIManagerType " + uiManagerType));
}
return eventDispatcher;
}
/**
* @return The {@link ReactContext} associated to the {@link View} received as a parameter.
* <p>We can't rely that the method View.getContext() will return the same context that was
* passed as a parameter during the construction of the View.
* <p>For example the AppCompatEditText class wraps the context received as a parameter in the
* constructor of the View into a TintContextWrapper object. See:
* https://android.googlesource.com/platform/frameworks/support/+/dd55716/v7/appcompat/src/android/support/v7/widget/AppCompatEditText.java#55
*/
public static ReactContext getReactContext(View view) {
Context context = view.getContext();
if (!(context instanceof ReactContext) && context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}
return (ReactContext) context;
}
/**
* @return Gets the surfaceId for the {@link ThemedReactContext} associated with a View, if
* possible, and then call getSurfaceId on it. See above (getReactContext) for additional
* context.
* <p>For RootViews, the root's rootViewTag is returned
* <p>Returns -1 for non-Fabric views
*/
public static int getSurfaceId(View view) {
if (view instanceof ReactRoot) {
ReactRoot rootView = (ReactRoot) view;
return rootView.getUIManagerType() == UIManagerType.FABRIC ? rootView.getRootViewTag() : -1;
}
int reactTag = view.getId();
// In non-Fabric we don't have (or use) SurfaceId
if (getUIManagerType(reactTag) == UIManagerType.DEFAULT) {
return -1;
}
Context context = view.getContext();
if (!(context instanceof ThemedReactContext) && context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}
int surfaceId = getSurfaceId(context);
if (surfaceId == -1) {
// All Fabric-managed Views (should) have a ThemedReactContext attached.
ReactSoftExceptionLogger.logSoftException(
TAG,
new IllegalStateException(
"Fabric View [" + reactTag + "] does not have SurfaceId associated with it"));
}
return surfaceId;
}
public static int getSurfaceId(Context context) {
if (context instanceof ThemedReactContext) {
return ((ThemedReactContext) context).getSurfaceId();
}
return -1;
}
/**
* @return the default padding used by Android EditText's. This method returns the padding in an
* array to avoid extra classloading during hot-path of RN Android.
*/
public static float[] getDefaultTextInputPadding(ThemedReactContext context) {
EditText editText = new EditText(context);
float[] padding = new float[4];
padding[PADDING_START_INDEX] = PixelUtil.toDIPFromPixel(ViewCompat.getPaddingStart(editText));
padding[PADDING_END_INDEX] = PixelUtil.toDIPFromPixel(ViewCompat.getPaddingEnd(editText));
padding[PADDING_TOP_INDEX] = PixelUtil.toDIPFromPixel(editText.getPaddingTop());
padding[PADDING_BOTTOM_INDEX] = PixelUtil.toDIPFromPixel(editText.getPaddingBottom());
return padding;
}
}