Android: Implement input connection listener in QtInputDelegate

This way we can just pass the input delegate as the listener for
QtEditText when we create it, and having the listener in a separate
member doesn't provide a real benefit anyway.

Task-number: QTBUG-118139
Change-Id: I0125c87ecd39eed550a120ea8326d2c50a1b016e
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 0e95d3ab57f4f57e65f7bb6019064ce4e31135a3)
Reviewed-by: Tinja Paavoseppä <tinja.paavoseppa@qt.io>
This commit is contained in:
Tinja Paavoseppä 2024-02-08 12:38:03 +02:00
parent 467749ce99
commit 76a40b2e73
4 changed files with 25 additions and 31 deletions

View File

@ -47,17 +47,13 @@ class QtEditText extends View
private final int ImhUrlCharactersOnly = 0x400000; private final int ImhUrlCharactersOnly = 0x400000;
private final int ImhLatinOnly = 0x800000; private final int ImhLatinOnly = 0x800000;
private QtInputConnectionListener m_qtInputConnectionListener; private final QtInputConnectionListener m_qtInputConnectionListener;
public QtEditText(Context context) public QtEditText(Context context, QtInputConnectionListener listener)
{ {
super(context); super(context);
setFocusable(true); setFocusable(true);
setFocusableInTouchMode(true); setFocusableInTouchMode(true);
}
public void setQtInputConnectionListener(QtInputConnectionListener listener)
{
m_qtInputConnectionListener = listener; m_qtInputConnectionListener = listener;
} }

View File

@ -21,7 +21,7 @@ import android.view.inputmethod.InputMethodManager;
import org.qtproject.qt.android.QtInputConnection.QtInputConnectionListener; import org.qtproject.qt.android.QtInputConnection.QtInputConnectionListener;
/** @noinspection FieldCanBeLocal*/ /** @noinspection FieldCanBeLocal*/
class QtInputDelegate { class QtInputDelegate implements QtInputConnection.QtInputConnectionListener {
// keyboard methods // keyboard methods
public static native void keyDown(int key, int unicode, int modifier, boolean autoRepeat); public static native void keyDown(int key, int unicode, int modifier, boolean autoRepeat);
@ -83,31 +83,31 @@ class QtInputDelegate {
} }
private final KeyboardVisibilityListener m_keyboardVisibilityListener; private final KeyboardVisibilityListener m_keyboardVisibilityListener;
private final QtInputConnectionListener m_inputConnectionListener;
QtInputDelegate(Activity activity, KeyboardVisibilityListener listener) QtInputDelegate(Activity activity, KeyboardVisibilityListener listener)
{ {
this.m_keyboardVisibilityListener = listener; this.m_keyboardVisibilityListener = listener;
m_imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); m_imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
m_inputConnectionListener = new QtInputConnectionListener() {
@Override
public void onSetClosing(boolean closing) {
if (!closing)
setKeyboardVisibility(true, System.nanoTime());
}
@Override
public void onHideKeyboardRunnableDone(boolean visibility, long hideTimeStamp) {
setKeyboardVisibility(visibility, hideTimeStamp);
}
@Override
public void onSendKeyEventDefaultCase() {
hideSoftwareKeyboard();
}
};
} }
// QtInputConnectionListener methods
@Override
public void onSetClosing(boolean closing) {
if (!closing)
setKeyboardVisibility(true, System.nanoTime());
}
@Override
public void onHideKeyboardRunnableDone(boolean visibility, long hideTimeStamp) {
setKeyboardVisibility(visibility, hideTimeStamp);
}
@Override
public void onSendKeyEventDefaultCase() {
hideSoftwareKeyboard();
}
// QtInputConnectionListener methods
public boolean isKeyboardVisible() public boolean isKeyboardVisible()
{ {
return m_keyboardIsVisible; return m_keyboardIsVisible;
@ -172,9 +172,6 @@ class QtInputDelegate {
void setFocusedView(QtEditText currentEditText) void setFocusedView(QtEditText currentEditText)
{ {
m_currentEditText = currentEditText; m_currentEditText = currentEditText;
// TODO rather set the listener when creating the edit text
if (m_currentEditText != null)
m_currentEditText.setQtInputConnectionListener(m_inputConnectionListener);
} }
public void showSoftwareKeyboard(Activity activity, QtLayout layout, public void showSoftwareKeyboard(Activity activity, QtLayout layout,

View File

@ -26,11 +26,11 @@ class QtWindow extends QtLayout implements QtSurfaceInterface {
private static native void setSurface(int windowId, Surface surface); private static native void setSurface(int windowId, Surface surface);
static native void windowFocusChanged(boolean hasFocus, int id); static native void windowFocusChanged(boolean hasFocus, int id);
public QtWindow(Context context, QtWindow parentWindow) public QtWindow(Context context, QtWindow parentWindow, QtInputDelegate delegate)
{ {
super(context); super(context);
setId(View.generateViewId()); setId(View.generateViewId());
m_editText = new QtEditText(context); m_editText = new QtEditText(context, delegate);
setParent(parentWindow); setParent(parentWindow);
setFocusableInTouchMode(true); setFocusableInTouchMode(true);

View File

@ -57,7 +57,8 @@ QAndroidPlatformWindow::QAndroidPlatformWindow(QWindow *window)
m_nativeQtWindow = QJniObject::construct<QtJniTypes::QtWindow>( m_nativeQtWindow = QJniObject::construct<QtJniTypes::QtWindow>(
QNativeInterface::QAndroidApplication::context(), QNativeInterface::QAndroidApplication::context(),
m_nativeParentQtWindow); m_nativeParentQtWindow,
QtAndroid::qtInputDelegate());
m_nativeViewId = m_nativeQtWindow.callMethod<jint>("getId"); m_nativeViewId = m_nativeQtWindow.callMethod<jint>("getId");
if (window->isTopLevel()) if (window->isTopLevel())