Android-Keyboard: Use new show() / hide() methods for keyboard control

Starting with API level 30, Android introduced new
[0]WindowInsetsController.show(int) and [1]hide(int) methods for
controlling the keyboard visibility. This commit updates the code to use
these methods on devices running API 30 and above.

[0]https://developer.android.com/reference/android/view/WindowInsetsController#show(int)
[1]https://developer.android.com/reference/android/view/WindowInsetsController#hide(int)

Task-number: QTBUG-98984
Pick-to: 6.8
Change-Id: Icfcad3430cd269353572f66f72114238045f7756
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 3823d99a688813d2692eb0d4b99939a65bb6ee6a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 75e575e18f0fbfa8571d9044beea3cf3939881ab)
This commit is contained in:
Bartlomiej Moskal 2025-04-08 14:36:56 +02:00 committed by Qt Cherry-pick Bot
parent a72d1c424a
commit 2aae64c041

View File

@ -19,6 +19,8 @@ import android.view.KeyEvent;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.WindowInsets; import android.view.WindowInsets;
import android.view.WindowInsets.Type; import android.view.WindowInsets.Type;
import android.view.WindowInsetsAnimationController;
import android.view.WindowInsetsAnimationControlListener;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.ViewTreeObserver; import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
@ -127,25 +129,31 @@ class QtInputDelegate implements QtInputConnection.QtInputConnectionListener, Qt
} }
} }
@Override private void showKeyboard(Activity activity,
public void showSoftwareKeyboard(Activity activity,
final int x, final int y, final int width, final int height, final int x, final int y, final int width, final int height,
final int inputHints, final int enterKeyType) final int inputHints, final int enterKeyType)
{ {
if (m_imm == null) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return; activity.getWindow().getInsetsController().controlWindowInsetsAnimation(
WindowInsets.Type.ime(), -1, null, null,
new WindowInsetsAnimationControlListener() {
@Override
public void onCancelled(WindowInsetsAnimationController controller) { }
QtNative.runAction(() -> { @Override
if (m_imm == null || m_currentEditText == null) public void onReady(WindowInsetsAnimationController controller, int types) { }
return;
if (updateSoftInputMode(activity, height)) @Override
return; public void onFinished(WindowInsetsAnimationController controller) {
QtNativeInputConnection.updateCursorPosition();
m_currentEditText.setEditTextOptions(enterKeyType, inputHints); setKeyboardVisibility(true, System.nanoTime());
m_currentEditText.setLayoutParams(new QtLayout.LayoutParams(width, height, x, y)); if (m_softInputMode == 0)
m_currentEditText.requestFocus(); probeForKeyboardHeight(activity, x, y, width, height,
m_currentEditText.postDelayed(() -> { inputHints, enterKeyType);
}
});
activity.getWindow().getInsetsController().show(Type.ime());
} else {
if (m_imm == null) if (m_imm == null)
return; return;
m_imm.showSoftInput(m_currentEditText, 0, new ResultReceiver(new Handler()) { m_imm.showSoftInput(m_currentEditText, 0, new ResultReceiver(new Handler()) {
@ -170,6 +178,29 @@ class QtInputDelegate implements QtInputConnection.QtInputConnectionListener, Qt
} }
} }
}); });
}
}
@Override
public void showSoftwareKeyboard(Activity activity,
final int x, final int y, final int width, final int height,
final int inputHints, final int enterKeyType)
{
if (m_imm == null)
return;
QtNative.runAction(() -> {
if (m_imm == null || m_currentEditText == null)
return;
if (updateSoftInputMode(activity, height))
return;
m_currentEditText.setEditTextOptions(enterKeyType, inputHints);
m_currentEditText.setLayoutParams(new QtLayout.LayoutParams(width, height, x, y));
m_currentEditText.requestFocus();
m_currentEditText.postDelayed(() -> {
showKeyboard(activity, x, y, width, height, inputHints, enterKeyType);
if (m_currentEditText.m_optionsChanged) { if (m_currentEditText.m_optionsChanged) {
m_imm.restartInput(m_currentEditText); m_imm.restartInput(m_currentEditText);
m_currentEditText.m_optionsChanged = false; m_currentEditText.m_optionsChanged = false;
@ -228,6 +259,27 @@ class QtInputDelegate implements QtInputConnection.QtInputConnectionListener, Qt
if (m_imm == null || m_currentEditText == null) if (m_imm == null || m_currentEditText == null)
return; return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Activity activity = QtNative.activity();
if (activity == null) {
Log.w(TAG, "hideSoftwareKeyboard: The activity reference is null");
return;
}
activity.getWindow().getInsetsController().controlWindowInsetsAnimation(
WindowInsets.Type.ime(), -1, null, null,
new WindowInsetsAnimationControlListener() {
@Override
public void onCancelled(WindowInsetsAnimationController controller) { }
@Override
public void onReady(WindowInsetsAnimationController controller, int types) { }
@Override
public void onFinished(WindowInsetsAnimationController controller) {
setKeyboardVisibility(false, System.nanoTime());
}
});
activity.getWindow().getInsetsController().hide(Type.ime());
} else {
m_imm.hideSoftInputFromWindow(m_currentEditText.getWindowToken(), 0, m_imm.hideSoftInputFromWindow(m_currentEditText.getWindowToken(), 0,
new ResultReceiver(new Handler()) { new ResultReceiver(new Handler()) {
@Override @Override
@ -244,6 +296,7 @@ class QtInputDelegate implements QtInputConnection.QtInputConnectionListener, Qt
} }
} }
}); });
}
}); });
} }
@ -271,7 +324,7 @@ class QtInputDelegate implements QtInputConnection.QtInputConnectionListener, Qt
boolean isKeyboardHidden = true; boolean isKeyboardHidden = true;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
Rect r = new Rect(); Rect r = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
DisplayMetrics metrics = new DisplayMetrics(); DisplayMetrics metrics = new DisplayMetrics();