package com.develop.food.base.utils

import android.app.Activity
import android.provider.Settings
import android.view.inputmethod.InputMethodInfo
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity

object SettingKeyBoardUtils {
    //设置键盘语言
     fun setTingKeyBoard(activity: Activity, language : String) {
        var str = ""
        var keyhashCode = -921088104 // 默认英文键盘
        str = if (language == "en"){
            "en_US"
        }else{
            language
        }


        val imm = activity.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
        // 取得当前所有的输入法
        var infos = imm.getInputMethodList() as List<InputMethodInfo>
        //筛选是系统语言
        infos.filter {  it.packageName.equals("com.android.inputmethod.latin")}
        infos.forEach{
            var sum = it.subtypeCount
            for (i in 0 until  sum){
                val  subtype = it.getSubtypeAt(i)
                val locale = subtype.locale
                if (locale == str){
                    //德国有3个 ，需要指定才能显示键盘切换键
                    if ("de"==str){
                        keyhashCode = 774684257
                    }else{
                        keyhashCode = subtype.hashCode()
                    }
                }
            }
        }

        //设置键盘语言
        Settings.Secure.putInt(activity.contentResolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE,
            keyhashCode);

    }
}