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 14, 2023
1 parent 5abd5ad commit 7a47155
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ protected void check() {
soLoader = new DefaultSoLoaderAdapter();
}
if (deviceAdapter == null) {
deviceAdapter = new DefaultDeviceAdapter();
deviceAdapter = new DefaultDeviceAdapter.NoOp();
}
if (logAdapter == null) {
logAdapter = new DefaultLogAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ public void updateDimension(int width, int height, boolean shouldUseScreenDispla
}
if (height < 0 || dimensionW == dimensionH) {
HippyDeviceAdapter deviceAdapter = mEngineContext.getGlobalConfigs().getDeviceAdapter();
if (deviceAdapter != null) {
deviceAdapter.reviseDimensionIfNeed(context, dimensionMap, shouldUseScreenDisplay,
if (deviceAdapter != null && dimensionMap != null) {
deviceAdapter.reviseDimensionInDpIfNeed(context, dimensionMap, shouldUseScreenDisplay,
systemUiVisibilityChanged);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public HippyGlobalConfigs build() {
mSoLoaderAdapter = new DefaultSoLoaderAdapter();
}
if (mDeviceAdapter == null) {
mDeviceAdapter = new DefaultDeviceAdapter();
mDeviceAdapter = new DefaultDeviceAdapter.NoOp();
}
if (mLogAdapter == null) {
mLogAdapter = new DefaultLogAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,32 @@

import com.tencent.mtt.hippy.common.HippyMap;

@SuppressWarnings("deprecation")
/**
* kept for compatibility, recommend to implement {@link HippyDeviceAdapter} directly
* @deprecated use {@link HippyDeviceAdapter} instead
*/
@Deprecated
public class DefaultDeviceAdapter implements HippyDeviceAdapter {

@SuppressWarnings("unused")
@Override
public void reviseDimensionIfNeed(Context context, HippyMap dimensionMap,
boolean shouldUseScreenDisplay, boolean systemUiVisibilityChanged) {
// Default do nothing here
}
@Deprecated
@Override
public void reviseDimensionIfNeed(Context context, HippyMap dimensionMap,
boolean shouldUseScreenDisplay, boolean systemUiVisibilityChanged) {
// Default do nothing here
}

public static final class NoOp implements HippyDeviceAdapter {

@Override
public void reviseDimensionIfNeed(Context context, HippyMap dimensionMap, boolean shouldUseScreenDisplay,
boolean systemUiVisibilityChanged) {
// do nothing
}

@Override
public void reviseDimensionInDpIfNeed(Context context, HippyMap dimensionMap, boolean shouldUseScreenDisplay,
boolean systemUiVisibilityChanged) {
// do nothing
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,65 @@

import android.content.Context;

import androidx.annotation.RestrictTo;
import com.tencent.mtt.hippy.common.HippyMap;

@SuppressWarnings("unused")
public interface HippyDeviceAdapter {

@SuppressWarnings({"EmptyMethod", "deprecation"})
void reviseDimensionIfNeed(Context context, HippyMap dimensionMap, boolean shouldUseScreenDisplay,
boolean systemUiVisibilityChanged);
/**
* kept for compatibility, recommend to implement {@link #reviseDimensionInDpIfNeed} and leave this method empty
* @deprecated use {@link #reviseDimensionInDpIfNeed} instead
*/
@Deprecated
void reviseDimensionIfNeed(Context context, HippyMap dimensionMap, boolean shouldUseScreenDisplay,
boolean systemUiVisibilityChanged);

default void reviseDimensionInDpIfNeed(Context context, HippyMap dimensionMap, boolean shouldUseScreenDisplay,
boolean systemUiVisibilityChanged) {
toPx(dimensionMap.getMap("windowPhysicalPixels"));
toPx(dimensionMap.getMap("screenPhysicalPixels"));
reviseDimensionIfNeed(context, dimensionMap, shouldUseScreenDisplay, systemUiVisibilityChanged);
toDp(dimensionMap.getMap("windowPhysicalPixels"));
toDp(dimensionMap.getMap("screenPhysicalPixels"));
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
static void times(HippyMap map, String key, double scale) {
double value = map.getDouble(key);
if (value != 0) {
map.pushDouble(key, value * scale);
}
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
static void div(HippyMap map, String key, double scale) {
double value = map.getDouble(key);
if (value != 0) {
map.pushDouble(key, value / scale);
}
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
static void toPx(HippyMap map) {
if (map != null) {
double scale = map.getDouble("scale");
assert scale != 0;
times(map, "width", scale);
times(map, "height", scale);
times(map, "statusBarHeight", scale);
times(map, "navigationBarHeight", scale);
}
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
static void toDp(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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,10 @@ String getGlobalConfigs() {
HippyMap globalParams = new HippyMap();
HippyMap dimensionMap = DimensionsUtil.getDimensions(-1, -1, context, false);

if (mContext.getGlobalConfigs() != null
if (dimensionMap != null && mContext.getGlobalConfigs() != null
&& mContext.getGlobalConfigs().getDeviceAdapter() != null) {
mContext.getGlobalConfigs().getDeviceAdapter()
.reviseDimensionIfNeed(context, dimensionMap, false,
.reviseDimensionInDpIfNeed(context, dimensionMap, false,
false);
}
globalParams.pushMap("Dimensions", dimensionMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,43 +184,43 @@ 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();
float windowScale = shouldUseScreenDisplay ? screenDisplayMetrics.density : windowDisplayMetrics.density;
if (shouldUseScreenDisplay) {
windowDisplayMetricsMap.pushDouble("width", windowWidth >= 0.0f ? windowWidth : screenDisplayWidth);
windowDisplayMetricsMap.pushDouble("height", windowHeight >= 0.0f ? windowHeight : screenDisplayHeight);
windowDisplayMetricsMap.pushDouble("scale", screenDisplayMetrics.density);
windowDisplayMetricsMap.pushDouble("width",
(ww >= 0 ? ww : screenDisplayMetrics.widthPixels) / windowScale);
windowDisplayMetricsMap.pushDouble("height",
(wh >= 0 ? wh : screenDisplayMetrics.heightPixels) / windowScale);
windowDisplayMetricsMap.pushDouble("scale", windowScale);
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("scale", windowDisplayMetrics.density);
windowDisplayMetricsMap.pushDouble("width",
(ww >= 0 ? ww : windowDisplayMetrics.widthPixels) / windowScale);
windowDisplayMetricsMap.pushDouble("height",
(wh >= 0 ? wh : windowDisplayMetrics.heightPixels) / windowScale);
windowDisplayMetricsMap.pushDouble("scale", windowScale);
windowDisplayMetricsMap.pushDouble("fontScale", windowDisplayMetrics.scaledDensity);
windowDisplayMetricsMap.pushDouble("densityDpi", windowDisplayMetrics.densityDpi);
}
windowDisplayMetricsMap.pushDouble("statusBarHeight", statusBarHeight);
windowDisplayMetricsMap.pushDouble("navigationBarHeight", navigationBarHeight);
windowDisplayMetricsMap.pushDouble("statusBarHeight", statusBarHeight > 0 ? statusBarHeight / windowScale : -1);
windowDisplayMetricsMap.pushDouble("navigationBarHeight", navigationBarHeight / windowScale);
dimensionMap.pushMap("windowPhysicalPixels", windowDisplayMetricsMap);

HippyMap screenDisplayMetricsMap = new HippyMap();
screenDisplayMetricsMap.pushDouble("width", screenDisplayWidth);
screenDisplayMetricsMap.pushDouble("height", screenDisplayHeight);
screenDisplayMetricsMap.pushDouble("scale", screenDisplayMetrics.density);
float screenScale = screenDisplayMetrics.density;
screenDisplayMetricsMap.pushDouble("width", screenDisplayMetrics.widthPixels / screenScale);
screenDisplayMetricsMap.pushDouble("height", screenDisplayMetrics.heightPixels / screenScale);
screenDisplayMetricsMap.pushDouble("scale", screenScale);
screenDisplayMetricsMap.pushDouble("fontScale", screenDisplayMetrics.scaledDensity);
screenDisplayMetricsMap.pushDouble("densityDpi", screenDisplayMetrics.densityDpi);
screenDisplayMetricsMap.pushDouble("statusBarHeight", statusBarHeight);
screenDisplayMetricsMap.pushDouble("navigationBarHeight", navigationBarHeight);
screenDisplayMetricsMap.pushDouble("statusBarHeight", statusBarHeight > 0 ? statusBarHeight / screenScale : -1);
screenDisplayMetricsMap.pushDouble("navigationBarHeight", navigationBarHeight / screenScale);
dimensionMap.pushMap("screenPhysicalPixels", screenDisplayMetricsMap);

return dimensionMap;
}
}

0 comments on commit 7a47155

Please sign in to comment.