流海屏、異形屏判斷(適配)

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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章