Skip to content

Commit 8451585

Browse files
andreicoman11Facebook Github Bot 1
authored andcommitted
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
1 parent 48ab5eb commit 8451585

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java

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

1212
import javax.annotation.Nullable;
1313

14-
import android.app.Activity;
1514
import android.content.Context;
1615
import android.graphics.Rect;
1716
import android.os.Bundle;
1817
import android.util.AttributeSet;
18+
import android.util.DisplayMetrics;
1919
import android.view.MotionEvent;
2020
import android.view.Surface;
2121
import android.view.View;
@@ -346,6 +346,10 @@ private void checkForDeviceOrientationChanges() {
346346
return;
347347
}
348348
mDeviceRotation = rotation;
349+
// It's important to repopulate DisplayMetrics and export them before emitting the
350+
// orientation change event, so that the Dimensions object returns the correct new values.
351+
DisplayMetricsHolder.initDisplayMetrics(getContext());
352+
emitUpdateDimensionsEvent();
349353
emitOrientationChanged(rotation);
350354
}
351355

@@ -384,6 +388,30 @@ private void emitOrientationChanged(final int newRotation) {
384388
sendEvent("namedOrientationDidChange", map);
385389
}
386390

391+
private void emitUpdateDimensionsEvent() {
392+
DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics();
393+
DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics();
394+
395+
WritableMap windowDisplayMetricsMap = Arguments.createMap();
396+
windowDisplayMetricsMap.putInt("width", windowDisplayMetrics.widthPixels);
397+
windowDisplayMetricsMap.putInt("height", windowDisplayMetrics.heightPixels);
398+
windowDisplayMetricsMap.putDouble("scale", windowDisplayMetrics.density);
399+
windowDisplayMetricsMap.putDouble("fontScale", windowDisplayMetrics.scaledDensity);
400+
windowDisplayMetricsMap.putDouble("densityDpi", windowDisplayMetrics.densityDpi);
401+
402+
WritableMap screenDisplayMetricsMap = Arguments.createMap();
403+
screenDisplayMetricsMap.putInt("width", screenDisplayMetrics.widthPixels);
404+
screenDisplayMetricsMap.putInt("height", screenDisplayMetrics.heightPixels);
405+
screenDisplayMetricsMap.putDouble("scale", screenDisplayMetrics.density);
406+
screenDisplayMetricsMap.putDouble("fontScale", screenDisplayMetrics.scaledDensity);
407+
screenDisplayMetricsMap.putDouble("densityDpi", screenDisplayMetrics.densityDpi);
408+
409+
WritableMap dimensionsMap = Arguments.createMap();
410+
dimensionsMap.putMap("windowPhysicalPixels", windowDisplayMetricsMap);
411+
dimensionsMap.putMap("screenPhysicalPixels", screenDisplayMetricsMap);
412+
sendEvent("didUpdateDimensions", dimensionsMap);
413+
}
414+
387415
private void sendEvent(String eventName, @Nullable WritableMap params) {
388416
if (mReactInstanceManager != null) {
389417
mReactInstanceManager.getCurrentReactContext()

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99

1010
package com.facebook.react.uimanager;
1111

12+
import javax.annotation.Nullable;
13+
14+
import java.lang.reflect.InvocationTargetException;
15+
import java.lang.reflect.Method;
16+
1217
import android.content.Context;
1318
import android.os.Build;
1419
import android.util.DisplayMetrics;
1520
import android.view.Display;
1621
import android.view.WindowManager;
1722

18-
import java.lang.reflect.InvocationTargetException;
19-
import java.lang.reflect.Method;
20-
21-
import javax.annotation.Nullable;
22-
2323
import com.facebook.infer.annotation.Assertions;
2424

2525
/**
@@ -43,11 +43,15 @@ public class DisplayMetricsHolder {
4343
public static void setWindowDisplayMetrics(DisplayMetrics displayMetrics) {
4444
sWindowDisplayMetrics = displayMetrics;
4545
}
46-
46+
4747
public static void initDisplayMetricsIfNotInitialized(Context context) {
4848
if (DisplayMetricsHolder.getScreenDisplayMetrics() != null) {
4949
return;
5050
}
51+
initDisplayMetrics(context);
52+
}
53+
54+
public static void initDisplayMetrics(Context context) {
5155
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
5256
DisplayMetricsHolder.setWindowDisplayMetrics(displayMetrics);
5357

0 commit comments

Comments
 (0)