Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QtEditText.java
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2012 BogDan Vatra <bogdan@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5package org.qtproject.qt.android;
6
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;
14
16
17class QtEditText extends View
18{
19 int m_initialCapsMode = 0;
20 int m_imeOptions = 0;
21 int m_inputType = InputType.TYPE_CLASS_TEXT;
22 boolean m_optionsChanged = false;
23 QtInputConnection m_inputConnection = null;
24
25 // input method hints - must be kept in sync with QTDIR/src/corelib/global/qnamespace.h
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;
33
34 private final int ImhDate = 0x80;
35 private final int ImhTime = 0x100;
36
37 private final int ImhPreferLatin = 0x200;
38
39 private final int ImhMultiLine = 0x400;
40
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;
49
50 private final QtInputConnectionListener m_qtInputConnectionListener;
51
52 public QtEditText(Context context, QtInputConnectionListener listener)
53 {
54 super(context);
55 setFocusable(true);
56 setFocusableInTouchMode(true);
57 m_qtInputConnectionListener = listener;
58 }
59
60 private void setImeOptions(int imeOptions)
61 {
62 if (m_imeOptions == imeOptions)
63 return;
64 m_imeOptions = m_imeOptions;
65 m_optionsChanged = true;
66 }
67
68 private void setInitialCapsMode(int initialCapsMode)
69 {
70 if (m_initialCapsMode == initialCapsMode)
71 return;
72 m_initialCapsMode = initialCapsMode;
73 m_optionsChanged = true;
74 }
75
76
77 private void setInputType(int inputType)
78 {
79 if (m_inputType == inputType)
80 return;
81 m_inputType = m_inputType;
82 m_optionsChanged = true;
83 }
84
85 @Override
86 public InputConnection onCreateInputConnection(EditorInfo outAttrs)
87 {
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;
93 }
94
95 @Override
96 public boolean onCheckIsTextEditor ()
97 {
98 return true;
99 }
100
101 @Override
102 public boolean onKeyDown (int keyCode, KeyEvent event)
103 {
104 if (null != m_inputConnection)
105 m_inputConnection.restartImmInput();
106
107 return super.onKeyDown(keyCode, event);
108 }
109
110 @Override
111 protected void onDraw(Canvas canvas) {
112 // DEBUG CODE
113 // canvas.drawARGB(127, 255, 0, 255);
114 super.onDraw(canvas);
115 }
116
117
118 public void setEditTextOptions(int enterKeyType, int inputHints)
119 {
120 int initialCapsMode = 0;
121 int imeOptions = imeOptionsFromEnterKeyType(enterKeyType);
122 int inputType = android.text.InputType.TYPE_CLASS_TEXT;
123
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);
129 }
130
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;
140 else
141 inputType |= android.text.InputType.TYPE_DATETIME_VARIATION_TIME;
142 } // else { TYPE_DATETIME_VARIATION_NORMAL(0) }
143 } else { // CLASS_TEXT
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) // not explicitly overridden
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;
155 }
156
157 if ((inputHints & ImhMultiLine) != 0) {
158 inputType |= android.text.InputType.TYPE_TEXT_FLAG_MULTI_LINE;
159 // Clear imeOptions for Multi-Line Type
160 // User should be able to insert new line in such case
161 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
162 }
163 if ((inputHints & (ImhNoPredictiveText | ImhSensitiveData | ImhHiddenText)) != 0)
164 inputType |= android.text.InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
165
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;
173 }
174 }
175
176 if (enterKeyType == 0 && (inputHints & ImhMultiLine) != 0)
177 imeOptions = android.view.inputmethod.EditorInfo.IME_FLAG_NO_ENTER_ACTION;
178
179 setInitialCapsMode(initialCapsMode);
180 setImeOptions(imeOptions);
181 setInputType(inputType);
182 }
183
184 private int imeOptionsFromEnterKeyType(int enterKeyType)
185 {
186 int imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
187
188 // enter key type - must be kept in sync with QTDIR/src/corelib/global/qnamespace.h
189 switch (enterKeyType) {
190 case 0: // EnterKeyDefault
191 break;
192 case 1: // EnterKeyReturn
193 imeOptions = android.view.inputmethod.EditorInfo.IME_FLAG_NO_ENTER_ACTION;
194 break;
195 case 2: // EnterKeyDone
196 break;
197 case 3: // EnterKeyGo
198 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_GO;
199 break;
200 case 4: // EnterKeySend
201 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_SEND;
202 break;
203 case 5: // EnterKeySearch
204 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_SEARCH;
205 break;
206 case 6: // EnterKeyNext
207 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_NEXT;
208 break;
209 case 7: // EnterKeyPrevious
210 imeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_PREVIOUS;
211 break;
212 }
213 return imeOptions;
214 }
215
216 private boolean isDisablePredictiveTextWorkaround(int inputHints)
217 {
218 return (inputHints & ImhNoPredictiveText) != 0 &&
219 System.getenv("QT_ANDROID_ENABLE_WORKAROUND_TO_DISABLE_PREDICTIVE_TEXT") != null;
220 }
221}
static void * context
struct _cl_event * event