Skip to content

Commit

Permalink
fix(android): dimensions api compatibility with lower version
Browse files Browse the repository at this point in the history
  • Loading branch information
iPel committed Nov 15, 2023
1 parent 5abd5ad commit 37c02b5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public void updateDimension(int width, int height, boolean shouldUseScreenDispla
systemUiVisibilityChanged);
}
}
DimensionsUtil.convertToDp(dimensionMap);
if (mEngineContext.getModuleManager() != null) {
mEngineContext.getModuleManager().getJavaScriptModule(Dimensions.class)
.set(dimensionMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ String getGlobalConfigs() {
.reviseDimensionIfNeed(context, dimensionMap, false,
false);
}
DimensionsUtil.convertToDp(dimensionMap);
globalParams.pushMap("Dimensions", dimensionMap);

String packageName = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,43 +184,63 @@ public static HippyMap getDimensions(int ww, int wh, Context context,

// construct param
HippyMap dimensionMap = new HippyMap();
getStatusBarHeight();
int statusBarHeight = getStatusBarHeight();
int navigationBarHeight = getNavigationBarHeight(context);
int statusBarHeight =
STATUS_BAR_HEIGHT > 0 ? Math.round(PixelUtil.px2dp(STATUS_BAR_HEIGHT)) : -1;
float windowWidth = (ww >= 0) ? PixelUtil.px2dp(ww) : ww;
float windowHeight = (wh >= 0) ? PixelUtil.px2dp(wh) : wh;
float screenDisplayWidth = PixelUtil.px2dp(screenDisplayMetrics.widthPixels);
float screenDisplayHeight = PixelUtil.px2dp(screenDisplayMetrics.heightPixels);
float windowDisplayWidth = PixelUtil.px2dp(windowDisplayMetrics.widthPixels);
float windowDisplayHeight = PixelUtil.px2dp(windowDisplayMetrics.heightPixels);
navigationBarHeight = Math.round(PixelUtil.px2dp(navigationBarHeight));

HippyMap windowDisplayMetricsMap = new HippyMap();
if (shouldUseScreenDisplay) {
windowDisplayMetricsMap.pushDouble("width", windowWidth >= 0.0f ? windowWidth : screenDisplayWidth);
windowDisplayMetricsMap.pushDouble("height", windowHeight >= 0.0f ? windowHeight : screenDisplayHeight);
windowDisplayMetricsMap.pushDouble("width", ww >= 0 ? ww : screenDisplayMetrics.widthPixels);
windowDisplayMetricsMap.pushDouble("height", wh >= 0 ? wh : screenDisplayMetrics.heightPixels);
windowDisplayMetricsMap.pushDouble("scale", screenDisplayMetrics.density);
windowDisplayMetricsMap.pushDouble("fontScale", screenDisplayMetrics.scaledDensity);
windowDisplayMetricsMap.pushDouble("densityDpi", screenDisplayMetrics.densityDpi);
} else {
windowDisplayMetricsMap.pushDouble("width", windowWidth >= 0.0f ? windowWidth : windowDisplayWidth);
windowDisplayMetricsMap.pushDouble("height", windowHeight >= 0.0f ? windowHeight : windowDisplayHeight);
windowDisplayMetricsMap.pushDouble("width", ww >= 0 ? ww : windowDisplayMetrics.widthPixels);
windowDisplayMetricsMap.pushDouble("height", wh >= 0 ? wh : windowDisplayMetrics.heightPixels);
windowDisplayMetricsMap.pushDouble("scale", windowDisplayMetrics.density);
windowDisplayMetricsMap.pushDouble("fontScale", windowDisplayMetrics.scaledDensity);
windowDisplayMetricsMap.pushDouble("densityDpi", windowDisplayMetrics.densityDpi);
}
windowDisplayMetricsMap.pushDouble("statusBarHeight", statusBarHeight);
windowDisplayMetricsMap.pushDouble("navigationBarHeight", navigationBarHeight);
dimensionMap.pushMap("windowPhysicalPixels", windowDisplayMetricsMap);

HippyMap screenDisplayMetricsMap = new HippyMap();
screenDisplayMetricsMap.pushDouble("width", screenDisplayWidth);
screenDisplayMetricsMap.pushDouble("height", screenDisplayHeight);
screenDisplayMetricsMap.pushDouble("width", screenDisplayMetrics.widthPixels);
screenDisplayMetricsMap.pushDouble("height", screenDisplayMetrics.heightPixels);
screenDisplayMetricsMap.pushDouble("scale", screenDisplayMetrics.density);
screenDisplayMetricsMap.pushDouble("fontScale", screenDisplayMetrics.scaledDensity);
screenDisplayMetricsMap.pushDouble("densityDpi", screenDisplayMetrics.densityDpi);
screenDisplayMetricsMap.pushDouble("statusBarHeight", statusBarHeight);
screenDisplayMetricsMap.pushDouble("navigationBarHeight", navigationBarHeight);
dimensionMap.pushMap("screenPhysicalPixels", screenDisplayMetricsMap);

return dimensionMap;
}

public static void convertToDp(HippyMap dimensionMap) {
if (dimensionMap != null) {
convertMap(dimensionMap.getMap("windowPhysicalPixels"));
convertMap(dimensionMap.getMap("screenPhysicalPixels"));
}
}

private static void convertMap(HippyMap map) {
if (map != null) {
double scale = map.getDouble("scale");
assert scale != 0;
div(map, "width", scale);
div(map, "height", scale);
div(map, "statusBarHeight", scale);
div(map, "navigationBarHeight", scale);
}
}

private static void div(HippyMap map, String key, double scale) {
double value = map.getDouble(key);
if (value > 0) {
map.pushDouble(key, value / scale);
}
}

}

0 comments on commit 37c02b5

Please sign in to comment.