最終采用方法4
第一種暫未發(fā)現(xiàn)其他機型有問題,所以結(jié)合MIUI判斷邏輯較為合適
1. 通過Insets檢測是否有左/右滑動手勢區(qū)域
/**
* 檢測發(fā)現(xiàn) MIUI 所有情況下 left、right 均為0
*/
fun hasLeftGesture(insets: WindowInsetsCompat): Boolean {
val inset = insets.getInsets(WindowInsetsCompat.Type.systemGestures())
return inset.left > 0
}
2. 通過系統(tǒng)固定值判斷 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. 結(jié)合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
}
}