流海屏、异形屏判断(适配)

public static boolean hasNotch(Activity act) {
    return hasNotchAtHuawei(act) || hasNotchAtXiaoMi(act) ||
            hasNotchAtOPPO(act) || hasNotchAtVivo(act) || hasNotchP(act);
}
/**
 * 华为是否有流海屏
 *
 * @param context context
 * @return 是否有流海屏
 */
public static boolean hasNotchAtHuawei(Context context) {
    boolean ret = false;
    try {
        ClassLoader classLoader = context.getClassLoader();
        Class HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
        Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
        ret = (boolean) get.invoke(HwNotchSizeUtil);
    } catch (Exception e) {
        LogUtil.e("Notch", "hasNotchAtHuawei Exception");
    } finally {
        return ret;
    }
}
/**
 * 小米是否流海屏
 *
 * @param act
 * @return
 */
public static boolean hasNotchAtXiaoMi(Activity act) {
    String s = SystemPropertiesProxy.get(act, "ro.miui.notch", "0");
    if (s.equals("1")) {
        return true;
    } else {
        return false;
    }
}

/**
 * oppo是否有流海屏
 *
 * @param context context
 * @return 是否有流海屏
 */
public static boolean hasNotchAtOPPO(Context context) {
    return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
}

/**
 * vivo是否有流海屏
 *
 * @param context context
 * @return 是否有流海屏
 */
public static boolean hasNotchAtVivo(Context context) {
    final int VIVO_NOTCH = 0x00000020;//是否有刘海
    final int VIVO_FILLET = 0x00000008;//是否有圆角
    boolean ret = false;
    try {
        ClassLoader classLoader = context.getClassLoader();
        Class FtFeature = classLoader.loadClass("android.util.FtFeature");
        Method method = FtFeature.getMethod("isFeatureSupport", int.class);
        ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
    } catch (Exception e) {
        LogUtil.e("Notch", "hasNotchAtVivo Exception");
    } finally {
        return ret;
    }
}

/**
 * android P 是否流海屏
 *
 * @param act act
 * @return  当前设备是否为异形屏
 */
public static boolean hasNotchP(Activity act) {
    if (Build.VERSION.SDK_INT >= 28) {
        View decorView = act.getWindow().getDecorView();
        WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
        if (rootWindowInsets != null) {
            DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
            if (displayCutout != null) {
                List<Rect> boundingRects = displayCutout.getBoundingRects();
                if (boundingRects != null && !boundingRects.isEmpty()) {
                    return true;
                }
            }
        }
        return false;
    } else {
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值