Skip to content

Commit

Permalink
Fix issue mopsicus#50
Browse files Browse the repository at this point in the history
keyboard height calculation now consider navigation bar
  • Loading branch information
PrisedRabbit committed Jan 19, 2021
1 parent 44bee38 commit 5f3a282
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}
}
}
}
Binary file modified Unity/Assets/Plugins/Android/Common.aar
Binary file not shown.
Binary file modified Unity/Assets/Plugins/Android/Mobileinput.aar
Binary file not shown.
4 changes: 0 additions & 4 deletions Unity/Assets/Scripts/UnityMobileInput/MobileInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 5f3a282

Please sign in to comment.