Permalink
Browse files

Working for RN0.29.0

  • Loading branch information...
BelinChung committed Jul 7, 2016
1 parent d745f46 commit 6f1a73d31787b7e13cf4f36eee2bb9c7332f69a1
@@ -1,6 +1,6 @@
## ExtraDimensions
This module allows you to access additional display metrics on Android devices.
This module allows you to access additional display metrics on Android devices. (RN 0.29.0+)
- Actual width and height of the screen (including elements such as soft menu bar)
- Soft menu height
@@ -35,17 +35,17 @@ view needs to fill up the real screen size.
}
```
3. Register module (in MainActivity.java)
3. Register module (in MainApplication.java)
```
import ca.jaysoo.extradimensions.ExtraDimensionsPackage; // <--- import
public class MainActivity extends ReactActivity {
public class MainApplication extends Application implements ReactApplication {
......
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ExtraDimensionsPackage(this) // <--- add here
new ExtraDimensionsPackage() // <--- add here
);
}
......
@@ -3,35 +3,54 @@
import java.lang.Math;
import java.lang.reflect.InvocationTargetException;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.provider.Settings;
import android.content.res.Resources;
import android.view.WindowManager;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import java.util.HashMap;
import java.util.Map;
import java.lang.reflect.Field;
public class ExtraDimensionsModule extends ReactContextBaseJavaModule {
private Activity mCurrentActivity;
public class ExtraDimensionsModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
public ExtraDimensionsModule(ReactApplicationContext reactContext, Activity activity) {
private ReactContext mReactContext;
public ExtraDimensionsModule(ReactApplicationContext reactContext) {
super(reactContext);
mCurrentActivity = activity;
mReactContext = reactContext;
mReactContext.addLifecycleEventListener(this);
}
@Override
public String getName() {
return "ExtraDimensions";
}
@Override
public void onHostDestroy() {
}
@Override
public void onHostPause() {
}
@Override
public void onHostResume() {
}
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
@@ -44,7 +63,8 @@ public String getName() {
//
// See: http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics)
if (Build.VERSION.SDK_INT >= 17) {
Display display = mCurrentActivity.getWindowManager().getDefaultDisplay();
Display display = ((WindowManager) mReactContext.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
try {
Display.class.getMethod("getRealMetrics", DisplayMetrics.class).invoke(display, metrics);
} catch (InvocationTargetException e) {
@@ -76,7 +96,8 @@ private float getSoftMenuBarHeight(DisplayMetrics metrics) {
final Context ctx = getReactApplicationContext();
final DisplayMetrics usableMetrics = ctx.getResources().getDisplayMetrics();
mCurrentActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
((WindowManager) mReactContext.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getMetrics(metrics);
final int usableHeight = usableMetrics.heightPixels;
return Math.max(0, realHeight - usableHeight / metrics.density);
@@ -136,5 +157,4 @@ protected static float getNormalNavigationBarHeight(final Context ctx) {
}
return 0;
}
}
}
@@ -1,7 +1,5 @@
package ca.jaysoo.extradimensions;
import android.app.Activity;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
@@ -13,16 +11,11 @@
import java.util.List;
public class ExtraDimensionsPackage implements ReactPackage {
private Activity mCurrentActivity;
public ExtraDimensionsPackage(Activity activity) {
mCurrentActivity = activity;
}
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(
new ExtraDimensionsModule(reactContext, mCurrentActivity)
new ExtraDimensionsModule(reactContext)
);
}
@@ -35,6 +28,4 @@ public ExtraDimensionsPackage(Activity activity) {
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.asList();
}
}
}

1 comment on commit 6f1a73d

@pqkluan

This comment has been minimized.

pqkluan commented on 6f1a73d Aug 4, 2016

Thanks for this 👍

Please sign in to comment.