LiuHaiScreenTools.java 源代码
package utils;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.DisplayCutout;
import android.view.View;
import android.view.WindowInsets;
import base.MyApplication;
import com.hichip.campro.R;
import java.util.List;
public class LiuHaiScreenTools {
private static int VIVO_FILLET = 8;
private static int VIVO_NOTCH = 32;
private static boolean isNotch = false;
private static int notchHeight;
public static boolean hasNotchAtXiaoMi() {
return getInt("ro.miui.notch") == 1;
}
public static int getInt(String str) {
if (!Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
return 0;
}
try {
Class<?> loadClass = MyApplication.getInstance().getClassLoader().loadClass("android.os.SystemProperties");
return ((Integer) loadClass.getMethod("getInt", String.class, Integer.TYPE).invoke(loadClass, new String(str), new Integer(0))).intValue();
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public static boolean hasNotchAtHuawei() {
try {
Class<?> loadClass = MyApplication.getInstance().getClassLoader().loadClass("com.huawei.android.util.HwNotchSizeUtil");
return ((Boolean) loadClass.getMethod("hasNotchInScreen", new Class[0]).invoke(loadClass, new Object[0])).booleanValue();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean hasNotchAtOPPO() {
return MyApplication.getInstance().getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}
public static boolean hasNotchAtVivo() {
try {
Class<?> loadClass = MyApplication.getInstance().getClassLoader().loadClass("android.util.FtFeature");
return ((Boolean) loadClass.getMethod("isFeatureSupport", Integer.TYPE).invoke(loadClass, Integer.valueOf(VIVO_NOTCH))).booleanValue();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static boolean hasNotchSamsung() {
if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
try {
Resources resources = MyApplication.getInstance().getResources();
int identifier = resources.getIdentifier("config_mainBuiltInDisplayCutout", "string", "android");
String string = identifier > 0 ? resources.getString(identifier) : null;
if (string != null) {
return !TextUtils.isEmpty(string);
}
return false;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
private static boolean isOtherBrandHasNotch(Activity activity2) {
WindowInsets rootWindowInsets;
DisplayCutout displayCutout;
List<Rect> boundingRects;
if (activity2 == null || activity2.getWindow() == null) {
return false;
}
return Build.VERSION.SDK_INT >= 23 && (rootWindowInsets = activity2.getWindow().getDecorView().getRootWindowInsets()) != null && Build.VERSION.SDK_INT >= 28 && (displayCutout = rootWindowInsets.getDisplayCutout()) != null && (boundingRects = displayCutout.getBoundingRects()) != null && boundingRects.size() > 0;
}
public static boolean hasNotch(Activity activity2) {
if (hasNotchAtXiaoMi() || hasNotchAtHuawei() || hasNotchAtOPPO() || hasNotchAtVivo() || hasNotchSamsung() || isOtherBrandHasNotch(activity2)) {
isNotch = true;
} else {
isNotch = false;
}
return isNotch;
}
public static int getNotchHeight(Activity activity2) {
WindowInsets rootWindowInsets;
DisplayCutout displayCutout;
List<Rect> boundingRects;
String lowerCase = Build.MANUFACTURER.toLowerCase();
if (!hasNotch(activity2)) {
return 0;
}
if (lowerCase.equalsIgnoreCase("xiaomi")) {
notchHeight = getSysStatusBarHeight();
} else if (lowerCase.equalsIgnoreCase("huawei") || lowerCase.equalsIgnoreCase("honour")) {
notchHeight = getNotchSizeAtHuaWei();
} else if (lowerCase.equalsIgnoreCase("vivo")) {
notchHeight = (int) TypedValue.applyDimension(1, 28.0f, MyApplication.getInstance().getResources().getDisplayMetrics());
} else if (lowerCase.equalsIgnoreCase("oppo")) {
notchHeight = 80;
} else if (Build.MANUFACTURER.equalsIgnoreCase("smartisan")) {
notchHeight = 82;
} else if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
notchHeight = 96;
} else {
if (activity2 != null && activity2.getWindow() != null) {
View decorView = activity2.getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= 23 && (rootWindowInsets = decorView.getRootWindowInsets()) != null && Build.VERSION.SDK_INT >= 28 && (displayCutout = rootWindowInsets.getDisplayCutout()) != null && (boundingRects = displayCutout.getBoundingRects()) != null && boundingRects.size() > 1 && boundingRects.get(0) != null) {
notchHeight = boundingRects.get(0).bottom;
}
}
if (notchHeight == 0) {
notchHeight = 82;
}
}
return notchHeight;
}
public static int getNotchSizeAtHuaWei() {
try {
Class<?> loadClass = MyApplication.getInstance().getClassLoader().loadClass("com.huawei.android.util.HwNotchSizeUtil");
return ((int[]) loadClass.getMethod("getNotchSize", new Class[0]).invoke(loadClass, new Object[0]))[1];
} catch (ClassNotFoundException e) {
e.printStackTrace();
return 0;
} catch (NoSuchMethodException e2) {
e2.printStackTrace();
return 0;
} catch (Exception e3) {
e3.printStackTrace();
return 0;
}
}
public static int getSysStatusBarHeight() {
Resources resources = MyApplication.getInstance().getResources();
int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
int dimensionPixelSize = identifier > 0 ? resources.getDimensionPixelSize(identifier) : 0;
return dimensionPixelSize == 0 ? resources.getDimensionPixelSize(R.dimen.create_barcode_height) : dimensionPixelSize;
}
}