mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Introduce DeviceInfo as a new native module
Summary: The `UIManager` already has a lot of responsibilities and is deeply tied with React Native's view architecture. This diff separates out a `DeviceInfo` native module to provide information about screen dimensions and font scale, etc. Reviewed By: fkgozali Differential Revision: D4713834 fbshipit-source-id: f2ee93acf876a4221c29a8c731f5abeffbb97974
This commit is contained in:
1 parent
238fd4ad19
commit
95c1926193
25 files changed
+345
-125
No files matched your search
@@ -87,7 +87,6 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
|
||||
private final Map<String, Object> mModuleConstants;
|
||||
private final UIImplementation mUIImplementation;
|
||||
private final MemoryTrimCallback mMemoryTrimCallback = new MemoryTrimCallback();
|
||||
private float mFontScale;
|
||||
|
||||
private int mNextRootViewTag = 1;
|
||||
private int mBatchId = 0;
|
||||
@@ -100,8 +99,7 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
|
||||
super(reactContext);
|
||||
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(reactContext);
|
||||
mEventDispatcher = new EventDispatcher(reactContext);
|
||||
mFontScale = getReactApplicationContext().getResources().getConfiguration().fontScale;
|
||||
mModuleConstants = createConstants(viewManagerList, lazyViewManagersEnabled, mFontScale);
|
||||
mModuleConstants = createConstants(viewManagerList, lazyViewManagersEnabled);
|
||||
mUIImplementation = uiImplementationProvider
|
||||
.createUIImplementation(reactContext, viewManagerList, mEventDispatcher);
|
||||
|
||||
@@ -134,12 +132,6 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
|
||||
@Override
|
||||
public void onHostResume() {
|
||||
mUIImplementation.onHostResume();
|
||||
|
||||
float fontScale = getReactApplicationContext().getResources().getConfiguration().fontScale;
|
||||
if (mFontScale != fontScale) {
|
||||
mFontScale = fontScale;
|
||||
emitUpdateDimensionsEvent();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -163,15 +155,13 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
|
||||
|
||||
private static Map<String, Object> createConstants(
|
||||
List<ViewManager> viewManagerList,
|
||||
boolean lazyViewManagersEnabled,
|
||||
float fontScale) {
|
||||
boolean lazyViewManagersEnabled) {
|
||||
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_START);
|
||||
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateUIManagerConstants");
|
||||
try {
|
||||
return UIManagerModuleConstantsHelper.createConstants(
|
||||
viewManagerList,
|
||||
lazyViewManagersEnabled,
|
||||
fontScale);
|
||||
lazyViewManagersEnabled);
|
||||
} finally {
|
||||
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
|
||||
ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_END);
|
||||
@@ -550,16 +540,6 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements
|
||||
mUIImplementation.sendAccessibilityEvent(tag, eventType);
|
||||
}
|
||||
|
||||
public void emitUpdateDimensionsEvent() {
|
||||
sendEvent("didUpdateDimensions", UIManagerModuleConstants.getDimensionsConstants(mFontScale));
|
||||
}
|
||||
|
||||
private void sendEvent(String eventName, @Nullable WritableMap params) {
|
||||
getReactApplicationContext()
|
||||
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
||||
.emit(eventName, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule a block to be executed on the UI thread. Useful if you need to execute
|
||||
* view logic after all currently queued view updates have completed.
|
||||
|
||||
+1
-30
@@ -83,7 +83,7 @@ import com.facebook.react.uimanager.events.TouchEventType;
|
||||
.build();
|
||||
}
|
||||
|
||||
public static Map<String, Object> getConstants(float fontScale) {
|
||||
public static Map<String, Object> getConstants() {
|
||||
HashMap<String, Object> constants = new HashMap<String, Object>();
|
||||
constants.put(
|
||||
"UIView",
|
||||
@@ -97,10 +97,6 @@ import com.facebook.react.uimanager.events.TouchEventType;
|
||||
"ScaleAspectCenter",
|
||||
ImageView.ScaleType.CENTER_INSIDE.ordinal())));
|
||||
|
||||
constants.put(
|
||||
"Dimensions",
|
||||
getDimensionsConstants(fontScale));
|
||||
|
||||
constants.put(
|
||||
"StyleConstants",
|
||||
MapBuilder.of(
|
||||
@@ -133,29 +129,4 @@ import com.facebook.react.uimanager.events.TouchEventType;
|
||||
|
||||
return constants;
|
||||
}
|
||||
|
||||
public static WritableMap getDimensionsConstants(float fontScale) {
|
||||
DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics();
|
||||
DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics();
|
||||
|
||||
WritableMap windowDisplayMetricsMap = Arguments.createMap();
|
||||
windowDisplayMetricsMap.putInt("width", windowDisplayMetrics.widthPixels);
|
||||
windowDisplayMetricsMap.putInt("height", windowDisplayMetrics.heightPixels);
|
||||
windowDisplayMetricsMap.putDouble("scale", windowDisplayMetrics.density);
|
||||
windowDisplayMetricsMap.putDouble("fontScale", fontScale);
|
||||
windowDisplayMetricsMap.putDouble("densityDpi", windowDisplayMetrics.densityDpi);
|
||||
|
||||
WritableMap screenDisplayMetricsMap = Arguments.createMap();
|
||||
screenDisplayMetricsMap.putInt("width", screenDisplayMetrics.widthPixels);
|
||||
screenDisplayMetricsMap.putInt("height", screenDisplayMetrics.heightPixels);
|
||||
screenDisplayMetricsMap.putDouble("scale", screenDisplayMetrics.density);
|
||||
screenDisplayMetricsMap.putDouble("fontScale", fontScale);
|
||||
screenDisplayMetricsMap.putDouble("densityDpi", screenDisplayMetrics.densityDpi);
|
||||
|
||||
WritableMap dimensionsMap = Arguments.createMap();
|
||||
dimensionsMap.putMap("windowPhysicalPixels", windowDisplayMetricsMap);
|
||||
dimensionsMap.putMap("screenPhysicalPixels", screenDisplayMetricsMap);
|
||||
|
||||
return dimensionsMap;
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -43,9 +43,8 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
|
||||
*/
|
||||
/* package */ static Map<String, Object> createConstants(
|
||||
List<ViewManager> viewManagers,
|
||||
boolean lazyViewManagersEnabled,
|
||||
float fontScale) {
|
||||
Map<String, Object> constants = UIManagerModuleConstants.getConstants(fontScale);
|
||||
boolean lazyViewManagersEnabled) {
|
||||
Map<String, Object> constants = UIManagerModuleConstants.getConstants();
|
||||
Map bubblingEventTypesConstants = UIManagerModuleConstants.getBubblingEventTypeConstants();
|
||||
Map directEventTypesConstants = UIManagerModuleConstants.getDirectEventTypeConstants();
|
||||
|
||||
|
||||
Reference in new issue
Block a user