From 782971f60ecb333c73f14464aca8a43a76f3adac Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Mon, 21 May 2018 11:42:07 -0700 Subject: [PATCH] Move Native Extensions from JSContext layer to ContextFactory Reviewed By: michalgr Differential Revision: D8057886 fbshipit-source-id: 45a63d909169c9a40414de9cd54bdb411f9f595d --- .../modules/deviceinfo/DeviceInfoModule.java | 11 ++++--- .../react/uimanager/DisplayMetricsHolder.java | 30 +++++++++++++++---- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.java index ea2f46948121ff..0b9e95b2401408 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.java @@ -13,13 +13,10 @@ import java.util.Map; import android.content.Context; -import android.util.DisplayMetrics; -import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.BaseJavaModule; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.WritableMap; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.uimanager.DisplayMetricsHolder; @@ -27,10 +24,12 @@ /** * Module that exposes Android Constants to JS. */ -@ReactModule(name = "DeviceInfo") +@ReactModule(name = DeviceInfoModule.sModuleName) public class DeviceInfoModule extends BaseJavaModule implements LifecycleEventListener { + static final String sModuleName = "DeviceInfo"; + private @Nullable ReactApplicationContext mReactApplicationContext; private float mFontScale; @@ -48,7 +47,7 @@ public DeviceInfoModule(Context context) { @Override public String getName() { - return "DeviceInfo"; + return sModuleName; } @Override @@ -88,6 +87,6 @@ public void emitUpdateDimensionsEvent() { mReactApplicationContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit("didUpdateDimensions", DisplayMetricsHolder.getDisplayMetricsMap(mFontScale)); + .emit("didUpdateDimensions", DisplayMetricsHolder.getDisplayMetricsNativeMap(mFontScale)); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/DisplayMetricsHolder.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/DisplayMetricsHolder.java index 4d2ad3b55d2d43..502242591f24ba 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/DisplayMetricsHolder.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/DisplayMetricsHolder.java @@ -11,6 +11,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; import android.content.Context; import android.os.Build; @@ -104,18 +106,37 @@ public static DisplayMetrics getScreenDisplayMetrics() { return sScreenDisplayMetrics; } - public static WritableNativeMap getDisplayMetricsMap(double fontScale) { + public static Map> getDisplayMetricsMap(double fontScale) { + Assertions.assertNotNull( + sWindowDisplayMetrics != null || sScreenDisplayMetrics != null, + "DisplayMetricsHolder must be initialized with initDisplayMetricsIfNotInitialized or initDisplayMetrics"); + final Map> result = new HashMap<>(); + result.put("windowPhysicalPixels", getPhysicalPixelsMap(sWindowDisplayMetrics, fontScale)); + result.put("screenPhysicalPixels", getPhysicalPixelsMap(sScreenDisplayMetrics, fontScale)); + return result; + } + + public static WritableNativeMap getDisplayMetricsNativeMap(double fontScale) { Assertions.assertNotNull( sWindowDisplayMetrics != null || sScreenDisplayMetrics != null, "DisplayMetricsHolder must be initialized with initDisplayMetricsIfNotInitialized or initDisplayMetrics"); final WritableNativeMap result = new WritableNativeMap(); - result.putMap("windowPhysicalPixels", getPhysicalPixelsMap(sWindowDisplayMetrics, fontScale)); - result.putMap("screenPhysicalPixels", getPhysicalPixelsMap(sScreenDisplayMetrics, fontScale)); + result.putMap("windowPhysicalPixels", getPhysicalPixelsNativeMap(sWindowDisplayMetrics, fontScale)); + result.putMap("screenPhysicalPixels", getPhysicalPixelsNativeMap(sScreenDisplayMetrics, fontScale)); + return result; + } + private static Map getPhysicalPixelsMap(DisplayMetrics displayMetrics, double fontScale) { + final Map result = new HashMap<>(); + result.put("width", displayMetrics.widthPixels); + result.put("height", displayMetrics.heightPixels); + result.put("scale", displayMetrics.density); + result.put("fontScale", fontScale); + result.put("densityDpi", displayMetrics.densityDpi); return result; } - private static WritableNativeMap getPhysicalPixelsMap(DisplayMetrics displayMetrics, double fontScale) { + private static WritableNativeMap getPhysicalPixelsNativeMap(DisplayMetrics displayMetrics, double fontScale) { final WritableNativeMap result = new WritableNativeMap(); result.putInt("width", displayMetrics.widthPixels); result.putInt("height", displayMetrics.heightPixels); @@ -124,5 +145,4 @@ private static WritableNativeMap getPhysicalPixelsMap(DisplayMetrics displayMetr result.putDouble("densityDpi", displayMetrics.densityDpi); return result; } - }