5package org.qtproject.qt.android;
7import android.content.Context;
8import android.graphics.Canvas;
9import android.text.InputType;
10import android.view.View;
11import android.view.inputmethod.EditorInfo;
12import android.view.inputmethod.InputConnection;
13import android.view.KeyEvent;
17class QtEditText
extends View
19 int m_initialCapsMode = 0;
21 int m_inputType = InputType.TYPE_CLASS_TEXT;
22 boolean m_optionsChanged =
false;
23 QtInputConnection m_inputConnection =
null;
26 private final int ImhHiddenText = 0x1;
27 private final int ImhSensitiveData = 0x2;
28 private final int ImhNoAutoUppercase = 0x4;
29 private final int ImhPreferNumbers = 0x8;
30 private final int ImhPreferUppercase = 0x10;
31 private final int ImhPreferLowercase = 0x20;
32 private final int ImhNoPredictiveText = 0x40;
34 private final int ImhDate = 0x80;
35 private final int ImhTime = 0x100;
37 private final int ImhPreferLatin = 0x200;
39 private final int ImhMultiLine = 0x400;
41 private final int ImhDigitsOnly = 0x10000;
42 private final int ImhFormattedNumbersOnly = 0x20000;
43 private final int ImhUppercaseOnly = 0x40000;
44 private final int ImhLowercaseOnly = 0x80000;
45 private final int ImhDialableCharactersOnly = 0x100000;
46 private final int ImhEmailCharactersOnly = 0x200000;
47 private final int ImhUrlCharactersOnly = 0x400000;
48 private final int ImhLatinOnly = 0x800000;
50 private final QtInputConnectionListener m_qtInputConnectionListener;
52 public QtEditText(Context
context, QtInputConnectionListener listener)
56 setFocusableInTouchMode(
true);
57 m_qtInputConnectionListener = listener;
60 private void setImeOptions(
int imeOptions)
62 if (m_imeOptions == imeOptions)
64 m_imeOptions = m_imeOptions;
65 m_optionsChanged =
true;
68 private void setInitialCapsMode(
int initialCapsMode)
70 if (m_initialCapsMode == initialCapsMode)
72 m_initialCapsMode = initialCapsMode;
73 m_optionsChanged =
true;
77 private void setInputType(
int inputType)
79 if (m_inputType == inputType)
81 m_inputType = m_inputType;
82 m_optionsChanged =
true;
86 public InputConnection onCreateInputConnection(EditorInfo outAttrs)
88 outAttrs.inputType = m_inputType;
89 outAttrs.imeOptions = m_imeOptions;
90 outAttrs.initialCapsMode = m_initialCapsMode;
91 m_inputConnection =
new QtInputConnection(
this,m_qtInputConnectionListener);
92 return m_inputConnection;
96 public boolean onCheckIsTextEditor ()
104 if (
null != m_inputConnection)
105 m_inputConnection.restartImmInput();
107 return super.onKeyDown(keyCode,
event);
111 protected void onDraw(Canvas canvas) {
114 super.onDraw(canvas);
118 public void setEditTextOptions(
int enterKeyType,
int inputHints)
120 int initialCapsMode = 0;
121 int imeOptions = imeOptionsFromEnterKeyType(enterKeyType);
122 int inputType = android.text.InputType.TYPE_CLASS_TEXT;
124 if ((inputHints & (ImhPreferNumbers | ImhDigitsOnly | ImhFormattedNumbersOnly)) != 0) {
125 inputType = android.text.InputType.TYPE_CLASS_NUMBER;
126 if ((inputHints & ImhFormattedNumbersOnly) != 0) {
127 inputType |= (android.text.InputType.TYPE_NUMBER_FLAG_DECIMAL
128 | android.text.InputType.TYPE_NUMBER_FLAG_SIGNED);
131 if ((inputHints & ImhHiddenText) != 0)
132 inputType |= android.text.InputType.TYPE_NUMBER_VARIATION_PASSWORD;
133 }
else if ((inputHints & ImhDialableCharactersOnly) != 0) {
134 inputType = android.text.InputType.TYPE_CLASS_PHONE;
135 }
else if ((inputHints & (ImhDate | ImhTime)) != 0) {
136 inputType = android.text.InputType.TYPE_CLASS_DATETIME;
137 if ((inputHints & (ImhDate | ImhTime)) != (ImhDate | ImhTime)) {
138 if ((inputHints & ImhDate) != 0)
139 inputType |= android.text.InputType.TYPE_DATETIME_VARIATION_DATE;
141 inputType |= android.text.InputType.TYPE_DATETIME_VARIATION_TIME;
144 if ((inputHints & ImhHiddenText) != 0) {
145 inputType |= android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD;
146 }
else if ((inputHints & ImhSensitiveData) != 0 ||
147 isDisablePredictiveTextWorkaround(inputHints)) {
148 inputType |= android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
149 }
else if ((inputHints & ImhUrlCharactersOnly) != 0) {
150 inputType |= android.text.InputType.TYPE_TEXT_VARIATION_URI;
151 if (enterKeyType == 0)
152 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_GO;
153 }
else if ((inputHints & ImhEmailCharactersOnly) != 0) {
154 inputType |= android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
157 if ((inputHints & ImhMultiLine) != 0) {
158 inputType |= android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE;
161 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
163 if ((inputHints & (ImhNoPredictiveText | ImhSensitiveData | ImhHiddenText)) != 0)
164 inputType |= android.text.InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
166 if ((inputHints & ImhUppercaseOnly) != 0) {
167 initialCapsMode |= android.text.TextUtils.CAP_MODE_CHARACTERS;
168 inputType |= android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
169 }
else if ((inputHints & ImhLowercaseOnly) == 0
170 && (inputHints & ImhNoAutoUppercase) == 0) {
171 initialCapsMode |= android.text.TextUtils.CAP_MODE_SENTENCES;
172 inputType |= android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
176 if (enterKeyType == 0 && (inputHints & ImhMultiLine) != 0)
177 imeOptions = android.view.inputmethod.EditorInfo.IME_FLAG_NO_ENTER_ACTION;
179 setInitialCapsMode(initialCapsMode);
180 setImeOptions(imeOptions);
181 setInputType(inputType);
184 private int imeOptionsFromEnterKeyType(
int enterKeyType)
186 int imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
189 switch (enterKeyType) {
193 imeOptions = android.view.inputmethod.EditorInfo.IME_FLAG_NO_ENTER_ACTION;
198 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_GO;
201 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_SEND;
204 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_SEARCH;
207 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_NEXT;
210 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_PREVIOUS;
216 private boolean isDisablePredictiveTextWorkaround(
int inputHints)
218 return (inputHints & ImhNoPredictiveText) != 0 &&
219 System.getenv(
"QT_ANDROID_ENABLE_WORKAROUND_TO_DISABLE_PREDICTIVE_TEXT") !=
null;