5package org.qtproject.qt.android;
7import android.annotation.TargetApi;
8import android.content.Context;
9import android.os.Build;
10import android.util.Log;
11import android.view.inputmethod.TextAttribute;
12import android.view.inputmethod.BaseInputConnection;
13import android.view.inputmethod.CompletionInfo;
14import android.view.inputmethod.ExtractedText;
15import android.view.inputmethod.ExtractedTextRequest;
16import android.view.inputmethod.InputMethodManager;
17import android.view.KeyEvent;
18import android.view.inputmethod.InputConnection;
23 int partialStartOffset;
30class QtNativeInputConnection
32 static native
boolean beginBatchEdit();
33 static native
boolean endBatchEdit();
34 static native
boolean commitText(
String text,
int newCursorPosition);
36 static native
boolean deleteSurroundingText(
int leftLength,
int rightLength);
37 static native
boolean finishComposingText();
38 static native
int getCursorCapsMode(
int reqModes);
39 static native QtExtractedText getExtractedText(
int hintMaxChars,
int hintMaxLines,
int flags);
43 static native
boolean replaceText(
int start,
int end,
String text,
int newCursorPosition);
44 static native
boolean setComposingText(
String text,
int newCursorPosition);
45 static native
boolean setComposingRegion(
int start,
int end);
46 static native
boolean setSelection(
int start,
int end);
47 static native
boolean selectAll();
48 static native
boolean cut();
49 static native
boolean copy();
50 static native
boolean copyURL();
51 static native
boolean paste();
52 static native
boolean updateCursorPosition();
53 static native
void reportFullscreenMode(
boolean enabled);
54 static native
boolean fullscreenMode();
57class QtInputConnection
extends BaseInputConnection
59 private static final int ID_SELECT_ALL = android.R.id.selectAll;
60 private static final int ID_CUT = android.R.id.cut;
61 private static final int ID_COPY = android.R.id.copy;
62 private static final int ID_PASTE = android.R.id.paste;
63 private static final int ID_COPY_URL = android.R.id.copyUrl;
64 private static final int ID_SWITCH_INPUT_METHOD = android.R.id.switchInputMethod;
65 private static final int ID_ADD_TO_DICTIONARY = android.R.id.addToDictionary;
66 private static final int KEYBOARD_CHECK_DELAY_MS = 100;
68 private static final String QtTAG =
"QtInputConnection";
70 private int m_extractedRequestToken = 0;
71 private boolean m_isComposing =
false;
72 private boolean m_duringBatchEdit =
false;
75 class HideKeyboardRunnable
implements Runnable {
76 private int m_numberOfAttempts = 10;
81 if (m_qtInputConnectionListener ==
null) {
82 Log.w(QtTAG,
"HideKeyboardRunnable: QtInputConnectionListener is null");
86 if (m_qtInputConnectionListener.keyboardTransitionInProgress()
87 && m_numberOfAttempts > 0) {
89 m_view.postDelayed(
this, KEYBOARD_CHECK_DELAY_MS);
93 if (m_qtInputConnectionListener.isKeyboardHidden())
107 private final QtEditText m_view;
108 private final InputMethodManager m_imm;
110 private void setClosing(
boolean closing)
112 if (
android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
114 m_view.postDelayed(
new HideKeyboardRunnable(), KEYBOARD_CHECK_DELAY_MS);
115 else if (m_qtInputConnectionListener !=
null)
116 m_qtInputConnectionListener.onSetClosing(
false);
120 QtInputConnection(QtEditText targetView, QtInputConnectionListener listener)
122 super(targetView,
true);
124 m_imm = (InputMethodManager)m_view.getContext().getSystemService(
126 m_qtInputConnectionListener = listener;
129 void restartImmInput()
131 if (QtNativeInputConnection.fullscreenMode() && !m_duringBatchEdit) {
133 m_imm.restartInput(m_view);
138 private void updateFullScreenExtractedText()
140 if (!QtNativeInputConnection.fullscreenMode())
143 if (m_duringBatchEdit || m_extractedRequestToken == 0)
146 ExtractedTextRequest
request =
new ExtractedTextRequest();
147 request.token = m_extractedRequestToken;
149 m_imm.updateExtractedText(m_view, m_extractedRequestToken, extractedText);
153 public boolean beginBatchEdit()
156 m_duringBatchEdit =
true;
157 return QtNativeInputConnection.beginBatchEdit();
161 public boolean reportFullscreenMode (
boolean enabled)
163 QtNativeInputConnection.reportFullscreenMode(
enabled);
166 return Build.VERSION.SDK_INT < Build.VERSION_CODES.O;
170 public boolean endBatchEdit()
173 boolean result = QtNativeInputConnection.endBatchEdit();
174 if (m_duringBatchEdit) {
175 m_duringBatchEdit =
false;
176 updateFullScreenExtractedText();
182 public boolean commitCompletion(CompletionInfo text)
185 updateFullScreenExtractedText();
186 return QtNativeInputConnection.commitCompletion(
text.getText().toString(),
text.getPosition());
190 public boolean commitText(CharSequence text,
int newCursorPosition)
193 boolean result = QtNativeInputConnection.commitText(
text.toString(), newCursorPosition);
194 updateFullScreenExtractedText();
199 public boolean deleteSurroundingText(
int leftLength,
int rightLength)
202 boolean result = QtNativeInputConnection.deleteSurroundingText(leftLength, rightLength);
203 updateFullScreenExtractedText();
208 public boolean finishComposingText()
212 m_isComposing =
false;
213 updateFullScreenExtractedText();
214 return QtNativeInputConnection.finishComposingText();
218 public int getCursorCapsMode(
int reqModes)
220 return QtNativeInputConnection.getCursorCapsMode(reqModes);
224 public ExtractedText getExtractedText(ExtractedTextRequest
request,
int flags)
226 QtExtractedText qExtractedText = QtNativeInputConnection.getExtractedText(
request.hintMaxChars,
229 if (qExtractedText ==
null)
232 ExtractedText extractedText =
new ExtractedText();
233 extractedText.partialEndOffset = qExtractedText.partialEndOffset;
234 extractedText.partialStartOffset = qExtractedText.partialStartOffset;
235 extractedText.selectionEnd = qExtractedText.selectionEnd;
236 extractedText.selectionStart = qExtractedText.selectionStart;
237 extractedText.startOffset = qExtractedText.startOffset;
238 extractedText.text = qExtractedText.text;
240 if (
flags == InputConnection.GET_EXTRACTED_TEXT_MONITOR)
241 m_extractedRequestToken =
request.token;
243 return extractedText;
246 public CharSequence getSelectedText(
int flags)
248 return QtNativeInputConnection.getSelectedText(
flags);
252 public CharSequence getTextAfterCursor(
int length,
int flags)
254 return QtNativeInputConnection.getTextAfterCursor(
length,
flags);
258 public CharSequence getTextBeforeCursor(
int length,
int flags)
260 return QtNativeInputConnection.getTextBeforeCursor(
length,
flags);
264 public boolean performContextMenuAction(
int id)
268 return QtNativeInputConnection.selectAll();
270 return QtNativeInputConnection.copy();
272 return QtNativeInputConnection.copyURL();
274 return QtNativeInputConnection.cut();
276 return QtNativeInputConnection.paste();
277 case ID_SWITCH_INPUT_METHOD:
279 m_imm.showInputMethodPicker();
282 case ID_ADD_TO_DICTIONARY:
293 return super.performContextMenuAction(
id);
297 public boolean sendKeyEvent(KeyEvent
event)
302 finishComposingText();
303 if (
event.getKeyCode() ==
KeyEvent.KEYCODE_ENTER && m_view !=
null) {
305 switch (m_view.m_imeOptions) {
306 case android.view.inputmethod.EditorInfo.IME_ACTION_NEXT:
308 event.getEventTime(),
311 event.getRepeatCount(),
312 event.getMetaState());
313 return super.sendKeyEvent(fakeEvent);
314 case android.view.inputmethod.EditorInfo.IME_ACTION_PREVIOUS:
316 event.getEventTime(),
319 event.getRepeatCount(),
321 return super.sendKeyEvent(fakeEvent);
322 case android.view.inputmethod.EditorInfo.IME_FLAG_NO_ENTER_ACTION:
326 if (m_qtInputConnectionListener !=
null)
327 m_qtInputConnectionListener.onSendKeyEventDefaultCase();
331 return super.sendKeyEvent(
event);
335 public boolean setComposingText(CharSequence text,
int newCursorPosition)
338 m_isComposing =
true;
339 boolean result = QtNativeInputConnection.setComposingText(
text.toString(), newCursorPosition);
340 updateFullScreenExtractedText();
346 public boolean setComposingText(CharSequence text,
int newCursorPosition, TextAttribute textAttribute)
348 return setComposingText(text, newCursorPosition);
353 public boolean setComposingRegion(
int start,
int end, TextAttribute textAttribute)
355 return setComposingRegion(
start,
end);
360 public boolean commitText(CharSequence text,
int newCursorPosition, TextAttribute textAttribute)
362 return commitText(text, newCursorPosition);
367 public boolean replaceText(
int start,
int end, CharSequence text,
int newCursorPosition, TextAttribute textAttribute)
370 updateFullScreenExtractedText();
371 return QtNativeInputConnection.replaceText(
start,
end,
text.toString(), newCursorPosition);
375 public boolean setComposingRegion(
int start,
int end)
378 updateFullScreenExtractedText();
379 return QtNativeInputConnection.setComposingRegion(
start,
end);
383 public boolean setSelection(
int start,
int end)
388 boolean result = QtNativeInputConnection.setSelection(
start,
end);
389 updateFullScreenExtractedText();
static jobject getExtractedText(JNIEnv *env, jobject, int hintMaxChars, int hintMaxLines, jint flags)
GLenum GLuint GLenum GLsizei length
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
QNetworkRequest request(url)
[0]