天天看點

記錄安卓手機 劉海屏判斷及擷取資訊

object NotchSupportUtil {

    //----------------------huawei
    fun hasNotchAtHuawei(context: Context): Boolean {
        var ret = false
        try {
            val classLoader = context.getClassLoader()
            val HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil")
            val get = HwNotchSizeUtil.getMethod("hasNotchInScreen")
            ret = get.invoke(HwNotchSizeUtil) as Boolean
        } catch (e: ClassNotFoundException) {
            Log.e("Notch", "hasNotchAtHuawei ClassNotFoundException")
        } catch (e: NoSuchMethodException) {
            Log.e("Notch", "hasNotchAtHuawei NoSuchMethodException")
        } catch (e: Exception) {
            Log.e("Notch", "hasNotchAtHuawei Exception")
        } finally {
            return ret
        }
    }

    //擷取劉海尺寸:width、height
    //int[0]值為劉海寬度 int[1]值為劉海高度
    fun getNotchSizeAtHuawei(context: Context): IntArray {
        var ret = intArrayOf(0, 0)
        try {
            val cl = context.classLoader
            val HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil")
            val get = HwNotchSizeUtil.getMethod("getNotchSize")
            ret = get.invoke(HwNotchSizeUtil) as IntArray
        } catch (e: ClassNotFoundException) {
            Log.e("Notch", "getNotchSizeAtHuawei ClassNotFoundException")
        } catch (e: NoSuchMethodException) {
            Log.e("Notch", "getNotchSizeAtHuawei NoSuchMethodException")
        } catch (e: Exception) {
            Log.e("Notch", "getNotchSizeAtHuawei Exception")
        } finally {
            return ret
        }
    }

    //------------------------vivo
    val VIVO_NOTCH = 0x00000020//是否有劉海
    val VIVO_FILLET = 0x00000008//是否有圓角
//    vivo不提供接口擷取劉海尺寸,目前vivo的劉海寬為100dp,高為27dp。

    fun hasNotchAtVivo(context: Context): Boolean {
        var ret = false
        try {
            val classLoader = context.classLoader
            val FtFeature = classLoader.loadClass("android.util.FtFeature")
            val method = FtFeature.getMethod("isFeatureSupport", Int::class.javaPrimitiveType)
            ret = method.invoke(FtFeature, VIVO_NOTCH) as Boolean
        } catch (e: ClassNotFoundException) {
            Log.e("Notch", "hasNotchAtVivo ClassNotFoundException")
        } catch (e: NoSuchMethodException) {
            Log.e("Notch", "hasNotchAtVivo NoSuchMethodException")
        } catch (e: Exception) {
            Log.e("Notch", "hasNotchAtVivo Exception")
        } finally {
            return ret
        }
    }
//------------------------oppo
    /**
     * OPPO不提供接口擷取劉海尺寸,目前其有劉海屏的機型尺寸規格都是統一的。不排除以後機型會有變化。
    其顯示屏寬度為1080px,高度為2280px。劉海區域則都是寬度為324px, 高度為80px。
     */
    fun hasNotchAtOPPO(context: Context): Boolean {
        return context.packageManager.hasSystemFeature("com.oppo.feature.screen.heteromorphism")
    }


    /**小米手機擷取劉海高度
     * 其他手機也可以通過這個方法來間接避開劉海屏,但是有可能有些手機的劉海屏高度會高于狀态欄的高度,是以這個方法擷取到的結果并不一定安全。
     */
    fun getStatusBarHeight(context: Context): Int {
        var statusBarHeight = 0
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            statusBarHeight = context.resources.getDimensionPixelSize(resourceId)
        }
        return statusBarHeight
    }

    /**
     * 是否是劉海螢幕
     * true fasle
     */
    fun hasNotch(context: Context): Boolean {
        val maunfacturer = Build.MANUFACTURER
        when (maunfacturer) {
            "XIAOMI", "xiaomi" -> return hasNotchAtXiaomi(context)
            "HUAWEI", "huawei" -> return hasNotchAtHuawei(context)
            "VIVO", "vivo" -> return hasNotchAtVivo(context)
            "OPPO" -> return hasNotchAtOPPO(context)
            else -> return isAndroidP(context.getTopActivity())
        }
        return false
    }


    /**
     * Android P 劉海屏判斷
     * @param activity
     * @return
     */
    fun isAndroidP(context: Activity): Boolean {
        val decorView = context.window.decorView
        if (decorView != null && Build.VERSION.SDK_INT >= 28) {
            val windowInsets = decorView.rootWindowInsets
            if (windowInsets != null) {
                val disCutoutCount = windowInsets.displayCutout
                return null != disCutoutCount
            }
        }
        return false
    }


    /**
     * 小米劉海屏判斷.
     * @return 0 if it is not notch ; return 1 means notch
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    //--------------小米
    fun hasNotchAtXiaomi(context: Context): Boolean {
        var ret = false
        var result: Int
        try {
            val classLoader = context.classLoader
            val SystemProperties = classLoader.loadClass("android.os.SystemProperties")
            val paramTypes = arrayOfNulls<Class<*>>(2)
            paramTypes[0] = String::class.javaPrimitiveType
            paramTypes[1] = Int::class.javaPrimitiveType
            val getInt = SystemProperties.getMethod("getInt", *paramTypes)
            val params = arrayOfNulls<Any>(2)
            params[0] = "ro.miui.notch"
            params[1] = 0
            result = getInt.invoke(SystemProperties, params) as Int
            return 1 == result

        } catch (e: ClassNotFoundException) {
            Log.e("Notch", "hasNotchAtVivo ClassNotFoundException")
        } catch (e: NoSuchMethodException) {
            Log.e("Notch", "hasNotchAtVivo NoSuchMethodException")
        } catch (e: Exception) {
            Log.e("Notch", "hasNotchAtVivo Exception")
        } finally {
            return ret
        }

        return false
    }


    /**
     * 擷取劉海螢幕的資訊
     */
    fun getNotchParams(activity: Activity) {
        if(Build.VERSION.SDK_INT >= 28){
            val decorView2 = activity.window.decorView
            decorView2.post {
                val displayCutout = decorView2.rootWindowInsets.displayCutout
                Log.e("TAG", "安全區域距離螢幕左邊的距離 SafeInsetLeft:" + displayCutout.safeInsetLeft)
                Log.e("TAG", "安全區域距離螢幕右部的距離 SafeInsetRight:" + displayCutout.safeInsetRight)
                Log.e("TAG", "安全區域距離螢幕頂部的距離 SafeInsetTop:" + displayCutout.safeInsetTop)
                Log.e("TAG", "安全區域距離螢幕底部的距離 SafeInsetBottom:" + displayCutout.safeInsetBottom)

                val rects = displayCutout.boundingRects
                if (rects == null || rects.size == 0) {
                    Log.e("TAG", "不是劉海屏")
                } else {
                    Log.e("TAG", "劉海屏數量:" + rects.size)
                    for (rect in rects) {
                        Log.e("TAG", "劉海屏區域:$rect")
                    }
                }
            }
        }
    }


}
           

繼續閱讀