Permalink
Browse files

Fix dimensions on orientation change

Summary:
This adds the `didUpdateDimensions` event that already exists on iOS, and
updates the internal native dimensions data that needs repopulation after
orientation change.

Reviewed By: foghina

Differential Revision: D3819364

fbshipit-source-id: fc2b3b4d1991d101e5de4439ccef2189bc65fd58
  • Loading branch information...
1 parent 48ab5eb commit 8451585f3861776a1d8f67b8b0dfd1e9cc705ed6 @andreicoman11 andreicoman11 committed with Facebook Github Bot 1 Sep 6, 2016
@@ -11,11 +11,11 @@
import javax.annotation.Nullable;
-import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.AttributeSet;
+import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
@@ -346,6 +346,10 @@ private void checkForDeviceOrientationChanges() {
return;
}
mDeviceRotation = rotation;
+ // It's important to repopulate DisplayMetrics and export them before emitting the
+ // orientation change event, so that the Dimensions object returns the correct new values.
+ DisplayMetricsHolder.initDisplayMetrics(getContext());
+ emitUpdateDimensionsEvent();
emitOrientationChanged(rotation);
}
@@ -384,6 +388,30 @@ private void emitOrientationChanged(final int newRotation) {
sendEvent("namedOrientationDidChange", map);
}
+ private void emitUpdateDimensionsEvent() {
+ DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics();
+ DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics();
+
+ WritableMap windowDisplayMetricsMap = Arguments.createMap();
+ windowDisplayMetricsMap.putInt("width", windowDisplayMetrics.widthPixels);
+ windowDisplayMetricsMap.putInt("height", windowDisplayMetrics.heightPixels);
+ windowDisplayMetricsMap.putDouble("scale", windowDisplayMetrics.density);
+ windowDisplayMetricsMap.putDouble("fontScale", windowDisplayMetrics.scaledDensity);
+ windowDisplayMetricsMap.putDouble("densityDpi", windowDisplayMetrics.densityDpi);
+
+ WritableMap screenDisplayMetricsMap = Arguments.createMap();
+ screenDisplayMetricsMap.putInt("width", screenDisplayMetrics.widthPixels);
+ screenDisplayMetricsMap.putInt("height", screenDisplayMetrics.heightPixels);
+ screenDisplayMetricsMap.putDouble("scale", screenDisplayMetrics.density);
+ screenDisplayMetricsMap.putDouble("fontScale", screenDisplayMetrics.scaledDensity);
+ screenDisplayMetricsMap.putDouble("densityDpi", screenDisplayMetrics.densityDpi);
+
+ WritableMap dimensionsMap = Arguments.createMap();
+ dimensionsMap.putMap("windowPhysicalPixels", windowDisplayMetricsMap);
+ dimensionsMap.putMap("screenPhysicalPixels", screenDisplayMetricsMap);
+ sendEvent("didUpdateDimensions", dimensionsMap);
+ }
+
private void sendEvent(String eventName, @Nullable WritableMap params) {
if (mReactInstanceManager != null) {
mReactInstanceManager.getCurrentReactContext()
@@ -9,17 +9,17 @@
package com.facebook.react.uimanager;
+import javax.annotation.Nullable;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-
-import javax.annotation.Nullable;
-
import com.facebook.infer.annotation.Assertions;
/**
@@ -43,11 +43,15 @@
public static void setWindowDisplayMetrics(DisplayMetrics displayMetrics) {
sWindowDisplayMetrics = displayMetrics;
}
-
+
public static void initDisplayMetricsIfNotInitialized(Context context) {
if (DisplayMetricsHolder.getScreenDisplayMetrics() != null) {
return;
}
+ initDisplayMetrics(context);
+ }
+
+ public static void initDisplayMetrics(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
DisplayMetricsHolder.setWindowDisplayMetrics(displayMetrics);

0 comments on commit 8451585

Please sign in to comment.