Skip to content

Commit

Permalink
Allow DeviceInfoModule to be instantiated with only an android Context
Browse files Browse the repository at this point in the history
Reviewed By: javache

Differential Revision: D4772673

fbshipit-source-id: 80894faa076ccfddf0fe28bd4d7b6aaeba6c38a5
  • Loading branch information
mhorowitz authored and facebook-github-bot committed Mar 31, 2017
1 parent 1916b38 commit ec4b854
Showing 1 changed file with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
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.ReactContextBaseJavaModule;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule;
Expand All @@ -29,16 +30,21 @@
* Module that exposes Android Constants to JS.
*/
@ReactModule(name = "DeviceInfo")
public class DeviceInfoModule extends ReactContextBaseJavaModule implements
public class DeviceInfoModule extends BaseJavaModule implements
LifecycleEventListener {

private @Nullable ReactApplicationContext mReactApplicationContext;
private float mFontScale;

public DeviceInfoModule(
ReactApplicationContext reactContext) {
super(reactContext);
public DeviceInfoModule(ReactApplicationContext reactContext) {
this((Context) reactContext);
mReactApplicationContext = reactContext;
}

mFontScale = getReactApplicationContext().getResources().getConfiguration().fontScale;
public DeviceInfoModule(Context context) {
mReactApplicationContext = null;
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(context);
mFontScale = context.getResources().getConfiguration().fontScale;
}

@Override
Expand All @@ -57,7 +63,11 @@ public String getName() {

@Override
public void onHostResume() {
float fontScale = getReactApplicationContext().getResources().getConfiguration().fontScale;
if (mReactApplicationContext == null) {
return;
}

float fontScale = mReactApplicationContext.getResources().getConfiguration().fontScale;
if (mFontScale != fontScale) {
mFontScale = fontScale;
emitUpdateDimensionsEvent();
Expand All @@ -73,7 +83,11 @@ public void onHostDestroy() {
}

public void emitUpdateDimensionsEvent() {
getReactApplicationContext()
if (mReactApplicationContext == null) {
return;
}

mReactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("didUpdateDimensions", getDimensionsConstants());
}
Expand Down

0 comments on commit ec4b854

Please sign in to comment.