Skip to content

Commit

Permalink
Move Native Extensions from JSContext layer to ContextFactory
Browse files Browse the repository at this point in the history
Reviewed By: michalgr

Differential Revision: D8057886

fbshipit-source-id: 45a63d909169c9a40414de9cd54bdb411f9f595d
  • Loading branch information
Dmitry Zakharov authored and facebook-github-bot committed May 21, 2018
1 parent 33ffa79 commit 782971f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@
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;

/**
* 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;

Expand All @@ -48,7 +47,7 @@ public DeviceInfoModule(Context context) {

@Override
public String getName() {
return "DeviceInfo";
return sModuleName;
}

@Override
Expand Down Expand Up @@ -88,6 +87,6 @@ public void emitUpdateDimensionsEvent() {

mReactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("didUpdateDimensions", DisplayMetricsHolder.getDisplayMetricsMap(mFontScale));
.emit("didUpdateDimensions", DisplayMetricsHolder.getDisplayMetricsNativeMap(mFontScale));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,18 +106,37 @@ public static DisplayMetrics getScreenDisplayMetrics() {
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 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 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);
Expand All @@ -124,5 +145,4 @@ private static WritableNativeMap getPhysicalPixelsMap(DisplayMetrics displayMetr
result.putDouble("densityDpi", displayMetrics.densityDpi);
return result;
}

}

0 comments on commit 782971f

Please sign in to comment.