diff --git a/Android/UnityMobileInput/mobileinput/src/main/java/ru/mopsicus/mobileinput/KeyboardProvider.java b/Android/UnityMobileInput/mobileinput/src/main/java/ru/mopsicus/mobileinput/KeyboardProvider.java index 0333cbd..084e50a 100644 --- a/Android/UnityMobileInput/mobileinput/src/main/java/ru/mopsicus/mobileinput/KeyboardProvider.java +++ b/Android/UnityMobileInput/mobileinput/src/main/java/ru/mopsicus/mobileinput/KeyboardProvider.java @@ -19,6 +19,11 @@ import android.view.ViewTreeObserver; import android.view.WindowManager; import android.widget.PopupWindow; +import android.util.Log; +import android.view.Display; +import android.view.Window; +import android.content.Context; +import android.util.DisplayMetrics; public class KeyboardProvider extends PopupWindow { @@ -47,6 +52,9 @@ public KeyboardProvider(Activity activity, ViewGroup parent, KeyboardObserver li setHeight(WindowManager.LayoutParams.MATCH_PARENT); setBackgroundDrawable(new ColorDrawable(0)); showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0); + + navBarHeight = getNavigationBarHeight(); + popupView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { @@ -67,30 +75,62 @@ private int getScreenOrientation() { return activity.getResources().getConfiguration().orientation; } + private int heightMax; + private int navBarHeight; + // Handler to get keyboard height private void handleOnGlobalLayout() { - Point screenSize = new Point(); - activity.getWindowManager().getDefaultDisplay().getSize(screenSize); - Rect rect = new Rect(); + + Rect rect = new Rect(); popupView.getWindowVisibleDisplayFrame(rect); + if (rect.bottom > heightMax) { + heightMax = rect.bottom; + } + int keyboardHeight = heightMax - rect.bottom; + if (keyboardHeight > 0) + { + keyboardHeight += navBarHeight; + } int orientation = getScreenOrientation(); - int keyboardHeight = screenSize.y - rect.bottom; - float height = keyboardHeight / (float) screenSize.y; - if (keyboardHeight == 0) { - notifyKeyboardHeight(0, 0, orientation); - } else if (orientation == Configuration.ORIENTATION_PORTRAIT) { - this.keyboardPortraitHeight = keyboardHeight; - notifyKeyboardHeight(height, keyboardPortraitHeight, orientation); - } else { - this.keyboardLandscapeHeight = keyboardHeight; - notifyKeyboardHeight(height, keyboardLandscapeHeight, orientation); + notifyKeyboardHeight(keyboardHeight, keyboardHeight, orientation); + } + + private int getNavigationBarHeight() { + if (!hasSoftKeys()) + { + return 0; + } + Resources resources = activity.getResources(); + int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (resourceId > 0) { + return resources.getDimensionPixelSize(resourceId); } + return 0; } + public boolean hasSoftKeys() { + Display d = activity.getWindowManager().getDefaultDisplay(); + + DisplayMetrics realDisplayMetrics = new DisplayMetrics(); + d.getRealMetrics(realDisplayMetrics); + + int realHeight = realDisplayMetrics.heightPixels; + int realWidth = realDisplayMetrics.widthPixels; + + DisplayMetrics displayMetrics = new DisplayMetrics(); + d.getMetrics(displayMetrics); + + int displayHeight = displayMetrics.heightPixels; + int displayWidth = displayMetrics.widthPixels; + + boolean hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0; + return hasSoftwareKeys; + } + // Send data observer private void notifyKeyboardHeight(float height, int keyboardHeight, int orientation) { if (observer != null) { observer.onKeyboardHeight(height, keyboardHeight, orientation); } } -} +} \ No newline at end of file diff --git a/Unity/Assets/Plugins/Android/Common.aar b/Unity/Assets/Plugins/Android/Common.aar index b028160..f498bef 100644 Binary files a/Unity/Assets/Plugins/Android/Common.aar and b/Unity/Assets/Plugins/Android/Common.aar differ diff --git a/Unity/Assets/Plugins/Android/Mobileinput.aar b/Unity/Assets/Plugins/Android/Mobileinput.aar index decc4dd..b7fa75e 100644 Binary files a/Unity/Assets/Plugins/Android/Mobileinput.aar and b/Unity/Assets/Plugins/Android/Mobileinput.aar differ diff --git a/Unity/Assets/Scripts/UnityMobileInput/MobileInput.cs b/Unity/Assets/Scripts/UnityMobileInput/MobileInput.cs index 29f7057..c6d000f 100644 --- a/Unity/Assets/Scripts/UnityMobileInput/MobileInput.cs +++ b/Unity/Assets/Scripts/UnityMobileInput/MobileInput.cs @@ -173,11 +173,7 @@ public void OnData (JsonObject data) { case KEYBOARD_ACTION: bool isShow = response["show"]; int height = 0; -#if UNITY_ANDROID - height = (int) (response["height"] * (float) Screen.height); -#elif UNITY_IOS height = response["height"]; -#endif OnShowKeyboard (isShow, height); break; default: