Skip to content

Commit 50fd080

Browse files
committed
uses getRealMetrics on display to get real height metrics
1 parent 0b15418 commit 50fd080

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111

1212
import javax.annotation.Nullable;
1313

14+
import java.lang.reflect.InvocationTargetException;
15+
import java.lang.reflect.Method;
1416
import java.util.Arrays;
1517
import java.util.List;
1618
import java.util.Map;
1719

20+
import android.content.Context;
21+
import android.os.Build;
1822
import android.util.DisplayMetrics;
23+
import android.view.Display;
24+
import android.view.WindowManager;
1925

2026
import com.facebook.csslayout.CSSLayoutContext;
2127
import com.facebook.infer.annotation.Assertions;
@@ -84,7 +90,9 @@ public UIManagerModule(
8490
UIImplementation uiImplementation) {
8591
super(reactContext);
8692
mEventDispatcher = new EventDispatcher(reactContext);
87-
DisplayMetrics displayMetrics = reactContext.getResources().getDisplayMetrics();
93+
94+
DisplayMetrics displayMetrics = getDisplayMetrics();
95+
8896
DisplayMetricsHolder.setDisplayMetrics(displayMetrics);
8997
mModuleConstants = createConstants(displayMetrics, viewManagerList);
9098
mUIImplementation = uiImplementation;
@@ -452,4 +460,39 @@ public EventDispatcher getEventDispatcher() {
452460
public void sendAccessibilityEvent(int tag, int eventType) {
453461
mUIImplementation.sendAccessibilityEvent(tag, eventType);
454462
}
463+
464+
private DisplayMetrics getDisplayMetrics() {
465+
Context context = getReactApplicationContext();
466+
467+
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
468+
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
469+
Display display = wm.getDefaultDisplay();
470+
471+
// Get the real display metrics if we are using API level 17 or higher.
472+
// The real metrics include system decor elements (e.g. soft menu bar).
473+
//
474+
// See: http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics)
475+
if (Build.VERSION.SDK_INT >= 17){
476+
display.getRealMetrics(displayMetrics);
477+
478+
} else {
479+
// For 14 <= API level <= 16, we need to invoke getRawHeight and getRawWidth to get the real dimensions.
480+
// Since react-native only supports API level 16+ we don't have to worry about other cases.
481+
//
482+
// Reflection exceptions are rethrown at runtime.
483+
//
484+
// See: http://stackoverflow.com/questions/14341041/how-to-get-real-screen-height-and-width/23861333#23861333
485+
try {
486+
Method mGetRawH = Display.class.getMethod("getRawHeight");
487+
Method mGetRawW = Display.class.getMethod("getRawWidth");
488+
displayMetrics.widthPixels = (Integer) mGetRawW.invoke(display);
489+
displayMetrics.heightPixels = (Integer) mGetRawH.invoke(display);
490+
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
491+
throw new RuntimeException("Error getting real dimensions for API level < 17", e);
492+
}
493+
}
494+
495+
return displayMetrics;
496+
}
497+
455498
}

0 commit comments

Comments
 (0)