最终采用方法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
}
}