From 6d4fff2e5ccaffb6b0255a139f2ae8e009278948 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Wed, 28 Jul 2021 09:07:57 -0700 Subject: [PATCH] Fix android view dimensions Summary: This diff fixes the Android View dimensions in VR PixelUtil.toSPFromPixel and PixelUtil.getDisplayMetricDensity() are both using getScreenDisplayMetrics() to perform conversion of dimensions. This is not correct because we should take into consideration the density of the Context / Activity instead of the Screen. This problem didn't raise before in Fabric Android because it seems that android OS on phones usually share the scale between the screen and the Activity? These two methods are only used in Fabric and they were introduced by D9583972 (https://github.com/facebook/react-native/commit/5c0da011cbaa788c52519f8091157ca6d87d8abb) and D9173758 (https://github.com/facebook/react-native/commit/8b5e3fc16b1e58441318b6ada629dcff572dd120) As part of this diff I'm also deleting the method toSPFromPixel in favor of toDIPFromPixel because I noticed the usages of these methods are meant to use toDIPFromPixel() changelog: [Internal] internal Reviewed By: JoshuaGross Differential Revision: D29864944 fbshipit-source-id: a0a093c120bde21a6cf9e1043a83c31e870d4368 --- .../main/java/com/facebook/react/uimanager/PixelUtil.java | 7 +------ .../com/facebook/react/views/text/TextLayoutManager.java | 8 ++++---- .../react/views/text/TextLayoutManagerMapBuffer.java | 8 ++++---- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.java index 8400527f6fa..e4d9c7ee224 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/PixelUtil.java @@ -24,11 +24,6 @@ public class PixelUtil { return toPixelFromDIP((float) value); } - /** Convert from PX to SP */ - public static float toSPFromPixel(float value) { - return value / DisplayMetricsHolder.getScreenDisplayMetrics().scaledDensity; - } - /** Convert from SP to PX */ public static float toPixelFromSP(float value) { return toPixelFromSP(value, Float.NaN); @@ -58,6 +53,6 @@ public class PixelUtil { /** @return {@link float} that represents the density of the display metrics for device screen. */ public static float getDisplayMetricDensity() { - return DisplayMetricsHolder.getScreenDisplayMetrics().density; + return DisplayMetricsHolder.getWindowDisplayMetrics().density; } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java index bbb57f87e88..18b34eaf5b6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java @@ -493,16 +493,16 @@ public class TextLayoutManager { // The attachment array returns the positions of each of the attachments as attachmentsPositions[attachmentPosition] = - PixelUtil.toSPFromPixel(placeholderTopPosition); + PixelUtil.toDIPFromPixel(placeholderTopPosition); attachmentsPositions[attachmentPosition + 1] = - PixelUtil.toSPFromPixel(placeholderLeftPosition); + PixelUtil.toDIPFromPixel(placeholderLeftPosition); attachmentIndex++; } } } - float widthInSP = PixelUtil.toSPFromPixel(calculatedWidth); - float heightInSP = PixelUtil.toSPFromPixel(calculatedHeight); + float widthInSP = PixelUtil.toDIPFromPixel(calculatedWidth); + float heightInSP = PixelUtil.toDIPFromPixel(calculatedHeight); if (ENABLE_MEASURE_LOGGING) { FLog.e( diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManagerMapBuffer.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManagerMapBuffer.java index 422d986a636..0e15307b868 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManagerMapBuffer.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManagerMapBuffer.java @@ -514,16 +514,16 @@ public class TextLayoutManagerMapBuffer { // The attachment array returns the positions of each of the attachments as attachmentsPositions[attachmentPosition] = - PixelUtil.toSPFromPixel(placeholderTopPosition); + PixelUtil.toDIPFromPixel(placeholderTopPosition); attachmentsPositions[attachmentPosition + 1] = - PixelUtil.toSPFromPixel(placeholderLeftPosition); + PixelUtil.toDIPFromPixel(placeholderLeftPosition); attachmentIndex++; } } } - float widthInSP = PixelUtil.toSPFromPixel(calculatedWidth); - float heightInSP = PixelUtil.toSPFromPixel(calculatedHeight); + float widthInSP = PixelUtil.toDIPFromPixel(calculatedWidth); + float heightInSP = PixelUtil.toDIPFromPixel(calculatedHeight); if (ENABLE_MEASURE_LOGGING) { FLog.e(