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
This commit is contained in:
Andrei Coman 2016-09-06 08:34:02 -07:00 committed by Facebook Github Bot 1
parent 48ab5eb436
commit 8451585f38
2 changed files with 39 additions and 7 deletions

View File

@ -11,11 +11,11 @@ package com.facebook.react;
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 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
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 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
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()

View File

@ -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 class DisplayMetricsHolder {
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);