Android携帯電話の劉海屏の判断と情報の取得を記録する

7200 ワード

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>(2)
            paramTypes[0] = String::class.javaPrimitiveType
            paramTypes[1] = Int::class.javaPrimitiveType
            val getInt = SystemProperties.getMethod("getInt", *paramTypes)
            val params = arrayOfNulls(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")
                    }
                }
            }
        }
    }


}