Move Native Extensions from JSContext layer to ContextFactory

Reviewed By: michalgr

Differential Revision: D8057886

fbshipit-source-id: 45a63d909169c9a40414de9cd54bdb411f9f595d
This commit is contained in:
Dmitry Zakharov 2018-05-21 11:42:07 -07:00 committed by Facebook Github Bot
parent 33ffa79a51
commit 782971f60e
2 changed files with 31 additions and 12 deletions

View File

@ -13,13 +13,10 @@ import java.util.HashMap;
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 @@ import com.facebook.react.uimanager.DisplayMetricsHolder;
/**
* 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 class DeviceInfoModule extends BaseJavaModule implements
@Override
public String getName() {
return "DeviceInfo";
return sModuleName;
}
@Override
@ -88,6 +87,6 @@ public class DeviceInfoModule extends BaseJavaModule implements
mReactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("didUpdateDimensions", DisplayMetricsHolder.getDisplayMetricsMap(mFontScale));
.emit("didUpdateDimensions", DisplayMetricsHolder.getDisplayMetricsNativeMap(mFontScale));
}
}

View File

@ -11,6 +11,8 @@ import javax.annotation.Nullable;
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 class DisplayMetricsHolder {
return sScreenDisplayMetrics;
}
public static WritableNativeMap getDisplayMetricsMap(double fontScale) {
public static Map<String, Map<String, Object>> getDisplayMetricsMap(double fontScale) {
Assertions.assertNotNull(
sWindowDisplayMetrics != null || sScreenDisplayMetrics != null,
"DisplayMetricsHolder must be initialized with initDisplayMetricsIfNotInitialized or initDisplayMetrics");
final Map<String, Map<String, Object>> 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 WritableNativeMap getPhysicalPixelsMap(DisplayMetrics displayMetrics, double fontScale) {
private static Map<String, Object> getPhysicalPixelsMap(DisplayMetrics displayMetrics, double fontScale) {
final Map<String, Object> 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 getPhysicalPixelsNativeMap(DisplayMetrics displayMetrics, double fontScale) {
final WritableNativeMap result = new WritableNativeMap();
result.putInt("width", displayMetrics.widthPixels);
result.putInt("height", displayMetrics.heightPixels);
@ -124,5 +145,4 @@ public class DisplayMetricsHolder {
result.putDouble("densityDpi", displayMetrics.densityDpi);
return result;
}
}