package com.hconline.iso.uicore.utils import android.app.Activity import android.graphics.Rect import android.os.Build import android.provider.Settings import android.util.Log import android.view.View import android.view.WindowManager import android.widget.FrameLayout /** * webView 键盘遮挡输入框处理
* 适用于 全屏/非全屏/有底部导航栏/无底部导航栏 */ class WebViewKeyBoardListener( private val activity: Activity ) { private var mChildOfContent: View? = null private var usableHeightPrevious: Int = 0 private var frameLayoutParams: FrameLayout.LayoutParams? = null fun init() { val content = activity .findViewById<View>(android.R.id.content) as FrameLayout mChildOfContent = content.getChildAt(0) mChildOfContent!!.viewTreeObserver.addOnGlobalLayoutListener { possiblyResizeChildOfContent() } frameLayoutParams = mChildOfContent!! .layoutParams as FrameLayout.LayoutParams } private fun possiblyResizeChildOfContent() { val usableHeightNow = computeUsableHeight() if (usableHeightNow != usableHeightPrevious) { val usableHeightSansKeyboard = mChildOfContent!!.rootView .height - getNavHeight() val heightDifference = usableHeightSansKeyboard - usableHeightNow if (heightDifference > usableHeightSansKeyboard / 4) { // keyboard probably just became visible frameLayoutParams!!.height = usableHeightSansKeyboard - heightDifference } else { // keyboard probably just became hidden frameLayoutParams!!.height = usableHeightSansKeyboard } mChildOfContent!!.requestLayout() usableHeightPrevious = usableHeightNow } } private fun computeUsableHeight(): Int { val r = Rect() mChildOfContent!!.getWindowVisibleDisplayFrame(r) return r.bottom - r.top } private fun getNavHeight(): Int { val resources = activity.resources val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { //判断底部导航栏是否为显示状态 val navigationBarShowing = isNavigationBarShowing() if (navigationBarShowing) { val height = resources.getDimensionPixelSize(resourceId) return height } } return 0 } private fun isNavigationBarShowing(): Boolean { //判断手机底部是否支持导航栏显示 val haveNavigationBar = checkDeviceHasNavigationBar() if (haveNavigationBar) { if (Build.VERSION.SDK_INT >= 17) { val brand = Build.BRAND val mDeviceInfo = when { brand.toUpperCase() == "HUAWEI" -> "navigationbar_is_min" brand.toUpperCase() == "XIAOMI" -> "force_fsg_nav_bar" brand.toUpperCase() == "VIVO" -> "navigation_gesture_on" brand.toUpperCase() == "OPPO" -> "navigation_gesture_on" else -> "navigationbar_is_min" } //是否全屏 val isFullScree = (activity.window.attributes.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN if (Settings.Global.getInt( activity.contentResolver, mDeviceInfo, 0 ) == 0 && !isFullScree ) { return true } } } return false } private fun checkDeviceHasNavigationBar(): Boolean { var hasNavigationBar = false val rs = activity.resources val id = rs.getIdentifier("config_showNavigationBar", "bool", "android") if (id > 0) { hasNavigationBar = rs.getBoolean(id) } try { val systemPropertiesClass = Class.forName("android.os.SystemProperties"); val m = systemPropertiesClass.getMethod("get", String::class.java) val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String if ("1" == navBarOverride) { hasNavigationBar = false } else if ("0" == navBarOverride) { hasNavigationBar = true } } catch (e: Exception) { e.printStackTrace() } return hasNavigationBar } companion object { fun getInstance(activity: Activity): WebViewKeyBoardListener { val keyBoardListener = WebViewKeyBoardListener(activity) return keyBoardListener } } }