Android 检测导航类型 手势导航 按键导航 Detect Gesture Navigation

最终采用方法4
第一种暂未发现其他机型有问题,所以结合MIUI判断逻辑较为合适

1. 通过Insets检测是否有左/右滑动手势区域

/**
 * 检测发现 MIUI 所有情况下 left、right 均为0
 */
fun hasLeftGesture(insets: WindowInsetsCompat): Boolean {
    val inset = insets.getInsets(WindowInsetsCompat.Type.systemGestures())
    return inset.left > 0
}


2. 通过系统固定值判断 0 三键; 1 双键;2 手势;

/**
 * 实测 荣耀 70 MagicUI 6.1 ,小米10 MIUI 13 全为 0
 */
fun isGestureNav(): Boolean {
    val resources: Resources = Resources.getSystem()
    return try {
        val resourceId =
            resources.getIdentifier("config_navBarInteractionMode", "integer", "android")
        if (resourceId > 0) {
            resources.getInteger(resourceId) == 2
        } else false
    } catch (e: Exception) {
        false
    }
}

3. 通过像素高度判断

实测高度差不太多,无法判断
比如MagicUI,手势导航:108,按钮导航 :114

4. 结合Insets与MIUI判断

val isMIUI: Boolean by lazy {
    !TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"))
}
private fun isGestureNav(insets: WindowInsetsCompat): Boolean {
    if (AppUtils.isMIUI) {
        val miNavBarMode =
            Settings.Global.getInt(Utils.getApp().contentResolver, "force_fsg_nav_bar", 0)
        return miNavBarMode != 0
    }
    val inset = insets.getInsets(WindowInsetsCompat.Type.systemGestures())
    return inset.left > 0
}

private fun getSystemProperty(propName: String): String? {
    return try {
        val p = Runtime.getRuntime().exec("getprop $propName")
        BufferedReader(InputStreamReader(p.inputStream), 1024).use {
            it.readLine()
        }
    } catch (_: Exception) {
        return null
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容