Skip to content

Commit

Permalink
uses getRealMetrics on display to get real height metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Jan 28, 2016
1 parent a322ab6 commit e09f29e
Showing 1 changed file with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@

import javax.annotation.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.facebook.csslayout.CSSLayoutContext;
import com.facebook.infer.annotation.Assertions;
Expand Down Expand Up @@ -84,7 +90,9 @@ public UIManagerModule(
UIImplementation uiImplementation) {
super(reactContext);
mEventDispatcher = new EventDispatcher(reactContext);
DisplayMetrics displayMetrics = reactContext.getResources().getDisplayMetrics();

DisplayMetrics displayMetrics = getDisplayMetrics();

DisplayMetricsHolder.setDisplayMetrics(displayMetrics);
mModuleConstants = createConstants(displayMetrics, viewManagerList);
mUIImplementation = uiImplementation;
Expand Down Expand Up @@ -452,4 +460,39 @@ public EventDispatcher getEventDispatcher() {
public void sendAccessibilityEvent(int tag, int eventType) {
mUIImplementation.sendAccessibilityEvent(tag, eventType);
}

private DisplayMetrics getDisplayMetrics() {
Context context = getReactApplicationContext();

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

// Get the real display metrics if we are using API level 17 or higher.
// The real metrics include system decor elements (e.g. soft menu bar).
//
// See: http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics)
if (Build.VERSION.SDK_INT >= 17){
display.getRealMetrics(displayMetrics);

} else {
// For 14 <= API level <= 16, we need to invoke getRawHeight and getRawWidth to get the real dimensions.
// Since react-native only supports API level 16+ we don't have to worry about other cases.
//
// Reflection exceptions are rethrown at runtime.
//
// See: http://stackoverflow.com/questions/14341041/how-to-get-real-screen-height-and-width/23861333#23861333
try {
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
displayMetrics.widthPixels = (Integer) mGetRawW.invoke(display);
displayMetrics.heightPixels = (Integer) mGetRawH.invoke(display);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
throw new RuntimeException("Error getting real dimensions for API level < 17", e);
}
}

return displayMetrics;
}

}

0 comments on commit e09f29e

Please sign in to comment.