Android 檢測導航類型 手勢導航 按鍵導航 Detect Gesture Navigation

最終采用方法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
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容