Skip to content

Commit

Permalink
fix(android): adapter android 11 get status bar height
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and zoomchan-cxj committed Nov 24, 2021
1 parent 2b586b1 commit c918740
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 127 deletions.
4 changes: 2 additions & 2 deletions android/sdk/gradle.properties
Expand Up @@ -32,9 +32,9 @@ ARCHIVES_BASE_NAME=android-sdk
#
# Specifies Android SDK version
#
COMPILE_SDK_VERSION=29
COMPILE_SDK_VERSION=30
MIN_SDK_VERSION=19
TARGET_SDK_VERSION=29
TARGET_SDK_VERSION=30

#
# Specifies Android NDK version
Expand Down
Expand Up @@ -33,6 +33,7 @@
import com.tencent.mtt.hippy.dom.node.StyleNode;
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.mtt.hippy.utils.ContextHolder;
import com.tencent.mtt.hippy.utils.DimensionsUtil;
import com.tencent.mtt.hippy.utils.LogUtils;
import com.tencent.mtt.hippy.utils.PixelUtil;
import com.tencent.mtt.hippy.utils.UIThreadUtils;
Expand Down Expand Up @@ -338,47 +339,6 @@ public void deleteChild(int pId, int childId, int childIndex) {
if (parentView instanceof ViewGroup && childView != null) {
deleteChildRecursive((ViewGroup) parentView, childView, childIndex);
}
// else
// {
// mContext.getGlobalConfigs().getLogAdapter().log(TAG, "deleteChild error pId: " + pId + " childId: " + childId +(parentView instanceof ViewGroup)+ (childView != null));
// }
}

private static int statusBarHeight = -1;

public static int getStatusBarHeightFromSystem() {
if (statusBarHeight > 0) {
return statusBarHeight;
}

Class<?> c;
Object obj;
Field field;
int x;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
//noinspection ConstantConditions
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = ContextHolder.getAppContext().getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
statusBarHeight = -1;
e1.printStackTrace();
}

if (statusBarHeight < 1) {
try {
int statebarH_id = ContextHolder.getAppContext().getResources()
.getIdentifier("statebar_height", "dimen",
ContextHolder.getAppContext().getPackageName());
statusBarHeight = Math
.round(ContextHolder.getAppContext().getResources().getDimension(statebarH_id));
} catch (NotFoundException e) {
LogUtils.d("ControllerManager", "getStatusBarHeightFromSystem: " + e.getMessage());
}
}
return statusBarHeight;
}

@SuppressLint("Range")
Expand All @@ -394,7 +354,7 @@ public void measureInWindow(int id, Promise promise) {

// We need to remove the status bar from the height. getLocationOnScreen will include the
// status bar.
statusBarHeight = getStatusBarHeightFromSystem();
statusBarHeight = DimensionsUtil.getStatusBarHeight();
if (statusBarHeight > 0) {
outputBuffer[1] -= statusBarHeight;
}
Expand Down
Expand Up @@ -19,14 +19,17 @@
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.graphics.Insets;
import android.os.Build;
import android.provider.Settings;
import android.view.WindowInsets;
import androidx.annotation.NonNull;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import android.view.WindowMetrics;
import com.tencent.mtt.hippy.common.HippyMap;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -120,6 +123,52 @@ public static int getNavigationBarHeight(Context context) {
return result;
}

public static int getStatusBarHeight() {
Context context = ContextHolder.getAppContext();
assert context != null;
if (STATUS_BAR_HEIGHT > 0) {
return STATUS_BAR_HEIGHT;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
WindowManager wm = (WindowManager) ContextHolder.getAppContext()
.getSystemService(Context.WINDOW_SERVICE);
WindowMetrics windowMetrics = wm.getCurrentWindowMetrics();
WindowInsets windowInsets = windowMetrics.getWindowInsets();
Insets insets = windowInsets
.getInsetsIgnoringVisibility(WindowInsets.Type.statusBars());
STATUS_BAR_HEIGHT = insets.top;
} else {
Class<?> c;
Object obj;
Field field;
int x;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
//noinspection ConstantConditions
x = Integer.parseInt(field.get(obj).toString());
STATUS_BAR_HEIGHT = context.getResources().getDimensionPixelSize(x);
} catch (Exception e) {
STATUS_BAR_HEIGHT = -1;
e.printStackTrace();
}

if (STATUS_BAR_HEIGHT < 1) {
try {
int statebarH_id = context.getResources()
.getIdentifier("statebar_height", "dimen", context.getPackageName());
STATUS_BAR_HEIGHT = Math.round(context.getResources().getDimension(statebarH_id));
} catch (NotFoundException e) {
LogUtils.d("DimensionsUtil", "getDimensions: " + e.getMessage());
}
}
}

return STATUS_BAR_HEIGHT;
}

public static HippyMap getDimensions(int windowWidth, int windowHeight, Context context,
boolean shouldUseScreenDisplay) {
if (context == null) {
Expand Down Expand Up @@ -152,34 +201,7 @@ public static HippyMap getDimensions(int windowWidth, int windowHeight, Context

// construct param
HippyMap dimensionMap = new HippyMap();
if (STATUS_BAR_HEIGHT < 0) {
Class<?> c;
Object obj;
Field field;
int x;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
//noinspection ConstantConditions
x = Integer.parseInt(field.get(obj).toString());
STATUS_BAR_HEIGHT = context.getResources().getDimensionPixelSize(x);
} catch (Exception e) {
STATUS_BAR_HEIGHT = -1;
e.printStackTrace();
}

if (STATUS_BAR_HEIGHT < 1) {
try {
int statebarH_id = context.getResources()
.getIdentifier("statebar_height", "dimen", context.getPackageName());
STATUS_BAR_HEIGHT = Math.round(context.getResources().getDimension(statebarH_id));
} catch (NotFoundException e) {
LogUtils.d("DimensionsUtil", "getDimensions: " + e.getMessage());
}
}
}

getStatusBarHeight();
int navigationBarHeight = getNavigationBarHeight(context);
HippyMap windowDisplayMetricsMap = new HippyMap();

Expand Down
Expand Up @@ -40,6 +40,7 @@
import com.tencent.mtt.hippy.HippyInstanceContext;
import com.tencent.mtt.hippy.HippyInstanceLifecycleEventListener;
import com.tencent.mtt.hippy.utils.ContextHolder;
import com.tencent.mtt.hippy.utils.DimensionsUtil;
import com.tencent.mtt.hippy.utils.LogUtils;
import com.tencent.mtt.hippy.views.view.HippyViewGroup;

Expand Down Expand Up @@ -210,56 +211,6 @@ public Dialog getDialog() {
return mDialog;
}

static int mStatusBarHeight = -1;
static boolean hasCheckStatusBarHeight = false;

public int getStatusBarHeightFixed() {
if (mStatusBarHeight == -1) {
mStatusBarHeight = getStatusBarHeightFromSystem();

hasCheckStatusBarHeight = true;
}
return mStatusBarHeight;
}

private static int statusBarHeight = -1;

@SuppressWarnings("ConstantConditions")
public static int getStatusBarHeightFromSystem() {
if (statusBarHeight > 0) {
return statusBarHeight;
}

Class<?> c;
Object obj;
Field field;
int x;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = ContextHolder.getAppContext().getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
statusBarHeight = -1;
e1.printStackTrace();
}

if (statusBarHeight < 1) {
try {
int statebarH_id = ContextHolder.getAppContext().getResources()
.getIdentifier("statebar_height", "dimen",
ContextHolder.getAppContext().getPackageName());
statusBarHeight = Math
.round(ContextHolder.getAppContext().getResources().getDimension(statebarH_id));
} catch (NotFoundException e) {
LogUtils.d("HippyModalHostView", "getStatusBarHeightFromSystem: " + e.getMessage());
statusBarHeight = 0;
}
}
return statusBarHeight;
}

public void setDialogBar(boolean isDarkIcon) {
try {
Window window = mDialog.getWindow();
Expand Down Expand Up @@ -454,11 +405,12 @@ protected View createContentView(View hostView) {
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (mEnterImmersionStatusBar && mStatusBarHeight != -1
int statusBarHeight = DimensionsUtil.getStatusBarHeight();
if (mEnterImmersionStatusBar && statusBarHeight != -1
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
canvas.save();
canvas.clipRect(0, 0, getMeasuredWidth(), mStatusBarHeight);
canvas.clipRect(0, 0, getMeasuredWidth(), statusBarHeight);
canvas.drawColor(0x40000000);
canvas.restore();
}
Expand All @@ -467,7 +419,7 @@ protected void dispatchDraw(Canvas canvas) {
if (mEnterImmersionStatusBar && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.topMargin = -1 * getStatusBarHeightFixed();
params.topMargin = -1 * DimensionsUtil.getStatusBarHeight();
frameLayout.addView(hostView, params);
} else {
frameLayout.addView(hostView);
Expand Down
4 changes: 2 additions & 2 deletions examples/android-demo/example/build.gradle
Expand Up @@ -19,13 +19,13 @@ if (propFile.exists()) {
}

android {
compileSdkVersion 29
compileSdkVersion 30
defaultConfig {
applicationId "com.tencent.mtt.hippy.example"
minSdkVersion 19
// noinspection ExpiredTargetSdkVersion
//noinspection OldTargetApi
targetSdkVersion 29
targetSdkVersion 30
ndk {
if (include_abi_armeabi_v7a.toBoolean()) {
abiFilters 'armeabi-v7a'
Expand Down

0 comments on commit c918740

Please sign in to comment.