Android: move implementation for checking if keyboard is displayed

Move implementation for checking if keyboard is displayed on the screen
from QtInputConnection to QtInputDelegate.
The QtInputDelegate class is responsible for showing/hiding the keyboard
and handling its visibility. Moving mentioned implementation there seems
reasonable. Also, this implementation will be used internally by
QtInputDelegate in the future.

Task-number: QTBUG-130000
Change-Id: I47cf8e8fb2ee9bea535b9e009dd4b43b28f19b80
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 229cff37c0b6f822e9c22734e66d8ce117723d22)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Bartlomiej Moskal 2024-11-14 11:16:21 +01:00 committed by Qt Cherry-pick Bot
parent e27e9d4a20
commit 0a32e20b11
2 changed files with 35 additions and 26 deletions

View File

@ -16,8 +16,6 @@ import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputMethodManager;
import android.view.KeyEvent;
import android.view.WindowInsets;
import android.view.WindowInsets.Type;
import android.graphics.Rect;
import android.app.Activity;
import android.util.DisplayMetrics;
@ -68,9 +66,6 @@ class QtInputConnection extends BaseInputConnection
private static final int ID_COPY_URL = android.R.id.copyUrl;
private static final int ID_SWITCH_INPUT_METHOD = android.R.id.switchInputMethod;
private static final int ID_ADD_TO_DICTIONARY = android.R.id.addToDictionary;
// We can't rely on a hardcoded value, because screens have different resolutions.
// That is why we assume that the keyboard should be higher than 0.15 of the screen.
private static final float KEYBOARD_TO_SCREEN_RATIO = 0.15f;
private static final String QtTAG = "QtInputConnection";
@ -80,31 +75,11 @@ class QtInputConnection extends BaseInputConnection
@Override
public void run() {
// Check that the keyboard is really no longer there.
Activity activity = QtNative.activity();
if (activity == null) {
Log.w(QtTAG, "HideKeyboardRunnable: The activity reference is null");
return;
}
if (m_qtInputConnectionListener == null) {
Log.w(QtTAG, "HideKeyboardRunnable: QtInputConnectionListener is null");
return;
}
boolean isKeyboardHidden = true;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
Rect r = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
final int kbHeight = screenHeight - r.bottom;
isKeyboardHidden = kbHeight < screenHeight * KEYBOARD_TO_SCREEN_RATIO;
} else {
WindowInsets w = activity.getWindow().getDecorView().getRootWindowInsets();
isKeyboardHidden = !w.isVisible(Type.ime());
}
if (isKeyboardHidden)
if (m_qtInputConnectionListener.isKeyboardHidden())
m_qtInputConnectionListener.onHideKeyboardRunnableDone(false, System.nanoTime());
}
}
@ -114,6 +89,7 @@ class QtInputConnection extends BaseInputConnection
void onHideKeyboardRunnableDone(boolean visibility, long hideTimeStamp);
void onSendKeyEventDefaultCase();
void onEditTextChanged(QtEditText editText);
boolean isKeyboardHidden();
}
private final QtEditText m_view;

View File

@ -6,6 +6,7 @@ package org.qtproject.qt.android;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
@ -16,6 +17,8 @@ import android.view.InputDevice;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.WindowInsets;
import android.view.WindowInsets.Type;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
@ -45,6 +48,10 @@ class QtInputDelegate implements QtInputConnection.QtInputConnectionListener, Qt
private QtEditText m_currentEditText = null;
private InputMethodManager m_imm;
// We can't rely on a hardcoded value, because screens have different resolutions.
// That is why we assume that the keyboard should be higher than 0.15 of the screen.
private static final float KEYBOARD_TO_SCREEN_RATIO = 0.15f;
private boolean m_keyboardIsVisible = false;
private boolean m_isKeyboardHidingAnimationOngoing = false;
private long m_showHideTimeStamp = System.nanoTime();
@ -227,6 +234,32 @@ class QtInputDelegate implements QtInputConnection.QtInputConnectionListener, Qt
// QtInputInterface implementation end
// QtInputConnectionListener methods
@Override
public boolean isKeyboardHidden() {
Activity activity = QtNative.activity();
if (activity == null) {
Log.w(TAG, "isKeyboardHidden: The activity reference is null");
return true;
}
boolean isKeyboardHidden = true;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
Rect r = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
final int kbHeight = screenHeight - r.bottom;
isKeyboardHidden = kbHeight < screenHeight * KEYBOARD_TO_SCREEN_RATIO;
} else {
WindowInsets w = activity.getWindow().getDecorView().getRootWindowInsets();
isKeyboardHidden = !w.isVisible(Type.ime());
}
return isKeyboardHidden;
}
@Override
public void onSetClosing(boolean closing) {
if (!closing)