blob: 02202db52facf57322696ab489578dbcf7934a60 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070019import android.hardware.input.InputManager;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070020import android.os.Parcel;
21import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.text.method.MetaKeyKeyListener;
Jeff Brown6b53e8d2010-11-10 16:03:06 -080023import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
Jeff Brownc3643b92012-04-18 19:09:08 -070026import java.text.Normalizer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Jeff Brownc5ed5912010-07-14 18:48:53 -070028/**
Jeff Brown6b53e8d2010-11-10 16:03:06 -080029 * Describes the keys provided by a keyboard device and their associated labels.
Jeff Brownc5ed5912010-07-14 18:48:53 -070030 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -070031public class KeyCharacterMap implements Parcelable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 /**
33 * The id of the device's primary built in keyboard is always 0.
Jeff Brown1f245102010-11-18 20:53:46 -080034 *
35 * @deprecated This constant should no longer be used because there is no
36 * guarantee that a device has a built-in keyboard that can be used for
37 * typing text. There might not be a built-in keyboard, the built-in keyboard
38 * might be a {@link #NUMERIC} or {@link #SPECIAL_FUNCTION} keyboard, or there
39 * might be multiple keyboards installed including external keyboards.
40 * When interpreting key presses received from the framework, applications should
Jeff Brown46a5ae62010-11-30 19:52:29 -080041 * use the device id specified in the {@link KeyEvent} received.
Jeff Brown1f245102010-11-18 20:53:46 -080042 * When synthesizing key presses for delivery elsewhere or when translating key presses
43 * from unknown keyboards, applications should use the special {@link #VIRTUAL_KEYBOARD}
44 * device id.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 */
Jeff Brown1f245102010-11-18 20:53:46 -080046 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 public static final int BUILT_IN_KEYBOARD = 0;
48
Jeff Brown6b53e8d2010-11-10 16:03:06 -080049 /**
50 * The id of a generic virtual keyboard with a full layout that can be used to
51 * synthesize key events. Typically used with {@link #getEvents}.
52 */
53 public static final int VIRTUAL_KEYBOARD = -1;
54
55 /**
56 * A numeric (12-key) keyboard.
57 * <p>
58 * A numeric keyboard supports text entry using a multi-tap approach.
59 * It may be necessary to tap a key multiple times to generate the desired letter
60 * or symbol.
61 * </p><p>
62 * This type of keyboard is generally designed for thumb typing.
63 * </p>
64 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 public static final int NUMERIC = 1;
66
Jeff Brown6b53e8d2010-11-10 16:03:06 -080067 /**
68 * A keyboard with all the letters, but with more than one letter per key.
69 * <p>
70 * This type of keyboard is generally designed for thumb typing.
71 * </p>
72 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 public static final int PREDICTIVE = 2;
74
Jeff Brown6b53e8d2010-11-10 16:03:06 -080075 /**
76 * A keyboard with all the letters, and maybe some numbers.
77 * <p>
78 * An alphabetic keyboard supports text entry directly but may have a condensed
79 * layout with a small form factor. In contrast to a {@link #FULL full keyboard}, some
80 * symbols may only be accessible using special on-screen character pickers.
81 * In addition, to improve typing speed and accuracy, the framework provides
82 * special affordances for alphabetic keyboards such as auto-capitalization
83 * and toggled / locked shift and alt keys.
84 * </p><p>
85 * This type of keyboard is generally designed for thumb typing.
86 * </p>
87 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public static final int ALPHA = 3;
89
90 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -080091 * A full PC-style keyboard.
92 * <p>
93 * A full keyboard behaves like a PC keyboard. All symbols are accessed directly
94 * by pressing keys on the keyboard without on-screen support or affordances such
95 * as auto-capitalization.
96 * </p><p>
97 * This type of keyboard is generally designed for full two hand typing.
98 * </p>
99 */
100 public static final int FULL = 4;
101
102 /**
103 * A keyboard that is only used to control special functions rather than for typing.
104 * <p>
105 * A special function keyboard consists only of non-printing keys such as
106 * HOME and POWER that are not actually used for typing.
107 * </p>
108 */
109 public static final int SPECIAL_FUNCTION = 5;
110
111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * This private-use character is used to trigger Unicode character
113 * input by hex digits.
114 */
115 public static final char HEX_INPUT = '\uEF00';
116
117 /**
118 * This private-use character is used to bring up a character picker for
119 * miscellaneous symbols.
120 */
121 public static final char PICKER_DIALOG_INPUT = '\uEF01';
122
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800123 /**
124 * Modifier keys may be chorded with character keys.
125 *
126 * @see {#link #getModifierBehavior()} for more details.
127 */
128 public static final int MODIFIER_BEHAVIOR_CHORDED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Jeff Brownc5ed5912010-07-14 18:48:53 -0700130 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800131 * Modifier keys may be chorded with character keys or they may toggle
132 * into latched or locked states when pressed independently.
133 *
134 * @see {#link #getModifierBehavior()} for more details.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700135 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800136 public static final int MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED = 1;
137
Jeff Brownaccce942012-04-18 15:47:48 -0700138 /*
139 * This bit will be set in the return value of {@link #get(int, int)} if the
140 * key is a "dead key."
141 */
142 public static final int COMBINING_ACCENT = 0x80000000;
143
144 /**
145 * Mask the return value from {@link #get(int, int)} with this value to get
146 * a printable representation of the accent character of a "dead key."
147 */
148 public static final int COMBINING_ACCENT_MASK = 0x7FFFFFFF;
149
150 /* Characters used to display placeholders for dead keys. */
151 private static final int ACCENT_ACUTE = '\u00B4';
Jeff Brownc3643b92012-04-18 19:09:08 -0700152 private static final int ACCENT_BREVE = '\u02D8';
153 private static final int ACCENT_CARON = '\u02C7';
154 private static final int ACCENT_CEDILLA = '\u00B8';
Jeff Brown69b2be12012-04-25 15:10:54 -0700155 private static final int ACCENT_CIRCUMFLEX = '\u02C6';
Jeff Brownc3643b92012-04-18 19:09:08 -0700156 private static final int ACCENT_COMMA_ABOVE = '\u1FBD';
157 private static final int ACCENT_COMMA_ABOVE_RIGHT = '\u02BC';
158 private static final int ACCENT_DOT_ABOVE = '\u02D9';
Jeff Brown69b2be12012-04-25 15:10:54 -0700159 private static final int ACCENT_DOT_BELOW = '.'; // approximate
Jeff Brownc3643b92012-04-18 19:09:08 -0700160 private static final int ACCENT_DOUBLE_ACUTE = '\u02DD';
Jeff Brownaccce942012-04-18 15:47:48 -0700161 private static final int ACCENT_GRAVE = '\u02CB';
Jeff Brown69b2be12012-04-25 15:10:54 -0700162 private static final int ACCENT_HOOK_ABOVE = '\u02C0';
163 private static final int ACCENT_HORN = '\''; // approximate
Jeff Brownc3643b92012-04-18 19:09:08 -0700164 private static final int ACCENT_MACRON = '\u00AF';
165 private static final int ACCENT_MACRON_BELOW = '\u02CD';
166 private static final int ACCENT_OGONEK = '\u02DB';
167 private static final int ACCENT_REVERSED_COMMA_ABOVE = '\u02BD';
168 private static final int ACCENT_RING_ABOVE = '\u02DA';
Jeff Brown69b2be12012-04-25 15:10:54 -0700169 private static final int ACCENT_STROKE = '-'; // approximate
Jeff Brownaccce942012-04-18 15:47:48 -0700170 private static final int ACCENT_TILDE = '\u02DC';
Jeff Brownc3643b92012-04-18 19:09:08 -0700171 private static final int ACCENT_TURNED_COMMA_ABOVE = '\u02BB';
Jeff Brownaccce942012-04-18 15:47:48 -0700172 private static final int ACCENT_UMLAUT = '\u00A8';
Jeff Brown69b2be12012-04-25 15:10:54 -0700173 private static final int ACCENT_VERTICAL_LINE_ABOVE = '\u02C8';
174 private static final int ACCENT_VERTICAL_LINE_BELOW = '\u02CC';
Jeff Brownaccce942012-04-18 15:47:48 -0700175
176 /* Legacy dead key display characters used in previous versions of the API.
177 * We still support these characters by mapping them to their non-legacy version. */
178 private static final int ACCENT_GRAVE_LEGACY = '`';
179 private static final int ACCENT_CIRCUMFLEX_LEGACY = '^';
180 private static final int ACCENT_TILDE_LEGACY = '~';
181
Jean Chalard54865502013-02-25 19:56:32 -0800182 private static final int CHAR_SPACE = ' ';
183
Jeff Brownaccce942012-04-18 15:47:48 -0700184 /**
Jeff Brownc3643b92012-04-18 19:09:08 -0700185 * Maps Unicode combining diacritical to display-form dead key.
Jeff Brownaccce942012-04-18 15:47:48 -0700186 */
Jeff Brownc3643b92012-04-18 19:09:08 -0700187 private static final SparseIntArray sCombiningToAccent = new SparseIntArray();
188 private static final SparseIntArray sAccentToCombining = new SparseIntArray();
Jeff Brownaccce942012-04-18 15:47:48 -0700189 static {
Jeff Brownc3643b92012-04-18 19:09:08 -0700190 addCombining('\u0300', ACCENT_GRAVE);
191 addCombining('\u0301', ACCENT_ACUTE);
192 addCombining('\u0302', ACCENT_CIRCUMFLEX);
193 addCombining('\u0303', ACCENT_TILDE);
194 addCombining('\u0304', ACCENT_MACRON);
195 addCombining('\u0306', ACCENT_BREVE);
196 addCombining('\u0307', ACCENT_DOT_ABOVE);
197 addCombining('\u0308', ACCENT_UMLAUT);
Jeff Brown69b2be12012-04-25 15:10:54 -0700198 addCombining('\u0309', ACCENT_HOOK_ABOVE);
Jeff Brownc3643b92012-04-18 19:09:08 -0700199 addCombining('\u030A', ACCENT_RING_ABOVE);
200 addCombining('\u030B', ACCENT_DOUBLE_ACUTE);
201 addCombining('\u030C', ACCENT_CARON);
Jeff Brown69b2be12012-04-25 15:10:54 -0700202 addCombining('\u030D', ACCENT_VERTICAL_LINE_ABOVE);
Jeff Brownc3643b92012-04-18 19:09:08 -0700203 //addCombining('\u030E', ACCENT_DOUBLE_VERTICAL_LINE_ABOVE);
204 //addCombining('\u030F', ACCENT_DOUBLE_GRAVE);
205 //addCombining('\u0310', ACCENT_CANDRABINDU);
206 //addCombining('\u0311', ACCENT_INVERTED_BREVE);
207 addCombining('\u0312', ACCENT_TURNED_COMMA_ABOVE);
208 addCombining('\u0313', ACCENT_COMMA_ABOVE);
209 addCombining('\u0314', ACCENT_REVERSED_COMMA_ABOVE);
210 addCombining('\u0315', ACCENT_COMMA_ABOVE_RIGHT);
Jeff Brown69b2be12012-04-25 15:10:54 -0700211 addCombining('\u031B', ACCENT_HORN);
212 addCombining('\u0323', ACCENT_DOT_BELOW);
Jeff Brownc3643b92012-04-18 19:09:08 -0700213 //addCombining('\u0326', ACCENT_COMMA_BELOW);
214 addCombining('\u0327', ACCENT_CEDILLA);
215 addCombining('\u0328', ACCENT_OGONEK);
Jeff Brown69b2be12012-04-25 15:10:54 -0700216 addCombining('\u0329', ACCENT_VERTICAL_LINE_BELOW);
Jeff Brownc3643b92012-04-18 19:09:08 -0700217 addCombining('\u0331', ACCENT_MACRON_BELOW);
Jeff Brown69b2be12012-04-25 15:10:54 -0700218 addCombining('\u0335', ACCENT_STROKE);
Jeff Brownc3643b92012-04-18 19:09:08 -0700219 //addCombining('\u0342', ACCENT_PERISPOMENI);
220 //addCombining('\u0344', ACCENT_DIALYTIKA_TONOS);
221 //addCombining('\u0345', ACCENT_YPOGEGRAMMENI);
222
223 // One-way mappings to equivalent preferred accents.
224 sCombiningToAccent.append('\u0340', ACCENT_GRAVE);
225 sCombiningToAccent.append('\u0341', ACCENT_ACUTE);
226 sCombiningToAccent.append('\u0343', ACCENT_COMMA_ABOVE);
227
228 // One-way legacy mappings to preserve compatibility with older applications.
229 sAccentToCombining.append(ACCENT_GRAVE_LEGACY, '\u0300');
230 sAccentToCombining.append(ACCENT_CIRCUMFLEX_LEGACY, '\u0302');
231 sAccentToCombining.append(ACCENT_TILDE_LEGACY, '\u0303');
232 }
233
234 private static void addCombining(int combining, int accent) {
235 sCombiningToAccent.append(combining, accent);
236 sAccentToCombining.append(accent, combining);
Jeff Brownaccce942012-04-18 15:47:48 -0700237 }
238
239 /**
Jeff Brownc3643b92012-04-18 19:09:08 -0700240 * Maps combinations of (display-form) combining key and second character
Jeff Brownaccce942012-04-18 15:47:48 -0700241 * to combined output character.
Jeff Brownc3643b92012-04-18 19:09:08 -0700242 * These mappings are derived from the Unicode NFC tables as needed.
Jeff Brownaccce942012-04-18 15:47:48 -0700243 */
Jeff Brownc3643b92012-04-18 19:09:08 -0700244 private static final SparseIntArray sDeadKeyCache = new SparseIntArray();
245 private static final StringBuilder sDeadKeyBuilder = new StringBuilder();
Jeff Brown69b2be12012-04-25 15:10:54 -0700246 static {
247 // Non-standard decompositions.
248 // Stroke modifier for Finnish multilingual keyboard and others.
249 addDeadKey(ACCENT_STROKE, 'D', '\u0110');
250 addDeadKey(ACCENT_STROKE, 'G', '\u01e4');
251 addDeadKey(ACCENT_STROKE, 'H', '\u0126');
252 addDeadKey(ACCENT_STROKE, 'I', '\u0197');
253 addDeadKey(ACCENT_STROKE, 'L', '\u0141');
254 addDeadKey(ACCENT_STROKE, 'O', '\u00d8');
255 addDeadKey(ACCENT_STROKE, 'T', '\u0166');
256 addDeadKey(ACCENT_STROKE, 'd', '\u0111');
257 addDeadKey(ACCENT_STROKE, 'g', '\u01e5');
258 addDeadKey(ACCENT_STROKE, 'h', '\u0127');
259 addDeadKey(ACCENT_STROKE, 'i', '\u0268');
260 addDeadKey(ACCENT_STROKE, 'l', '\u0142');
261 addDeadKey(ACCENT_STROKE, 'o', '\u00f8');
262 addDeadKey(ACCENT_STROKE, 't', '\u0167');
263 }
264
265 private static void addDeadKey(int accent, int c, int result) {
266 final int combining = sAccentToCombining.get(accent);
267 if (combining == 0) {
268 throw new IllegalStateException("Invalid dead key declaration.");
269 }
270 final int combination = (combining << 16) | c;
271 sDeadKeyCache.put(combination, result);
272 }
Jeff Brownaccce942012-04-18 15:47:48 -0700273
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700274 public static final Parcelable.Creator<KeyCharacterMap> CREATOR =
275 new Parcelable.Creator<KeyCharacterMap>() {
276 public KeyCharacterMap createFromParcel(Parcel in) {
277 return new KeyCharacterMap(in);
278 }
279 public KeyCharacterMap[] newArray(int size) {
280 return new KeyCharacterMap[size];
281 }
282 };
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800283
Ashok Bhat0c3f9212014-01-08 14:15:02 +0000284 private long mPtr;
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800285
Ashok Bhat0c3f9212014-01-08 14:15:02 +0000286 private static native long nativeReadFromParcel(Parcel in);
287 private static native void nativeWriteToParcel(long ptr, Parcel out);
288 private static native void nativeDispose(long ptr);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800289
Ashok Bhat0c3f9212014-01-08 14:15:02 +0000290 private static native char nativeGetCharacter(long ptr, int keyCode, int metaState);
291 private static native boolean nativeGetFallbackAction(long ptr, int keyCode, int metaState,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800292 FallbackAction outFallbackAction);
Ashok Bhat0c3f9212014-01-08 14:15:02 +0000293 private static native char nativeGetNumber(long ptr, int keyCode);
294 private static native char nativeGetMatch(long ptr, int keyCode, char[] chars, int metaState);
295 private static native char nativeGetDisplayLabel(long ptr, int keyCode);
296 private static native int nativeGetKeyboardType(long ptr);
297 private static native KeyEvent[] nativeGetEvents(long ptr, char[] chars);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800298
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700299 private KeyCharacterMap(Parcel in) {
300 if (in == null) {
301 throw new IllegalArgumentException("parcel must not be null");
302 }
303 mPtr = nativeReadFromParcel(in);
304 if (mPtr == 0) {
305 throw new RuntimeException("Could not read KeyCharacterMap from parcel.");
306 }
307 }
308
309 // Called from native
Ashok Bhat0c3f9212014-01-08 14:15:02 +0000310 private KeyCharacterMap(long ptr) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800311 mPtr = ptr;
312 }
313
314 @Override
315 protected void finalize() throws Throwable {
316 if (mPtr != 0) {
317 nativeDispose(mPtr);
318 mPtr = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 }
320 }
321
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800322 /**
323 * Loads the key character maps for the keyboard with the specified device id.
324 *
325 * @param deviceId The device id of the keyboard.
326 * @return The associated key character map.
Jeff Brown7e1e21f2011-01-19 17:05:01 -0800327 * @throws {@link UnavailableException} if the key character map
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800328 * could not be loaded because it was malformed or the default key character map
329 * is missing from the system.
330 */
331 public static KeyCharacterMap load(int deviceId) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700332 final InputManager im = InputManager.getInstance();
333 InputDevice inputDevice = im.getInputDevice(deviceId);
334 if (inputDevice == null) {
335 inputDevice = im.getInputDevice(VIRTUAL_KEYBOARD);
336 if (inputDevice == null) {
337 throw new UnavailableException(
338 "Could not load key character map for device " + deviceId);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800339 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800340 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700341 return inputDevice.getKeyCharacterMap();
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700342 }
343
344 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800345 * Gets the Unicode character generated by the specified key and meta
346 * key state combination.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 * <p>
348 * Returns the Unicode character that the specified key would produce
349 * when the specified meta bits (see {@link MetaKeyKeyListener})
350 * were active.
351 * </p><p>
352 * Returns 0 if the key is not one that is used to type Unicode
353 * characters.
354 * </p><p>
355 * If the return value has bit {@link #COMBINING_ACCENT} set, the
356 * key is a "dead key" that should be combined with another to
357 * actually produce a character -- see {@link #getDeadChar} --
358 * after masking with {@link #COMBINING_ACCENT_MASK}.
359 * </p>
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800360 *
361 * @param keyCode The key code.
362 * @param metaState The meta key modifier state.
363 * @return The associated character or combining accent, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800365 public int get(int keyCode, int metaState) {
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800366 metaState = KeyEvent.normalizeMetaState(metaState);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800367 char ch = nativeGetCharacter(mPtr, keyCode, metaState);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800368
Jeff Brownc3643b92012-04-18 19:09:08 -0700369 int map = sCombiningToAccent.get(ch);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 if (map != 0) {
Jeff Brownaccce942012-04-18 15:47:48 -0700371 return map | COMBINING_ACCENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 } else {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800373 return ch;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375 }
376
377 /**
Jeff Brown49ed71d2010-12-06 17:13:33 -0800378 * Gets the fallback action to perform if the application does not
379 * handle the specified key.
380 * <p>
381 * When an application does not handle a particular key, the system may
382 * translate the key to an alternate fallback key (specified in the
383 * fallback action) and dispatch it to the application.
384 * The event containing the fallback key is flagged
385 * with {@link KeyEvent#FLAG_FALLBACK}.
386 * </p>
387 *
388 * @param keyCode The key code.
389 * @param metaState The meta key modifier state.
Jeff Brownfd23e3e2012-05-09 13:34:28 -0700390 * @return The fallback action, or null if none. Remember to recycle the fallback action.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800391 *
392 * @hide
393 */
Jeff Brownfd23e3e2012-05-09 13:34:28 -0700394 public FallbackAction getFallbackAction(int keyCode, int metaState) {
395 FallbackAction action = FallbackAction.obtain();
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800396 metaState = KeyEvent.normalizeMetaState(metaState);
Jeff Brownfd23e3e2012-05-09 13:34:28 -0700397 if (nativeGetFallbackAction(mPtr, keyCode, metaState, action)) {
398 action.metaState = KeyEvent.normalizeMetaState(action.metaState);
399 return action;
400 }
401 action.recycle();
402 return null;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800403 }
404
405 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800406 * Gets the number or symbol associated with the key.
407 * <p>
408 * The character value is returned, not the numeric value.
409 * If the key is not a number, but is a symbol, the symbol is retuned.
410 * </p><p>
411 * This method is intended to to support dial pads and other numeric or
412 * symbolic entry on keyboards where certain keys serve dual function
413 * as alphabetic and symbolic keys. This method returns the number
414 * or symbol associated with the key independent of whether the user
415 * has pressed the required modifier.
416 * </p><p>
417 * For example, on one particular keyboard the keys on the top QWERTY row generate
418 * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard
419 * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1'
420 * so that the user can type numbers without pressing ALT when it makes sense.
421 * </p>
422 *
423 * @param keyCode The key code.
424 * @return The associated numeric or symbolic character, or 0 if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800426 public char getNumber(int keyCode) {
427 return nativeGetNumber(mPtr, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
430 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800431 * Gets the first character in the character array that can be generated
432 * by the specified key code.
433 * <p>
434 * This is a convenience function that returns the same value as
435 * {@link #getMatch(int,char[],int) getMatch(keyCode, chars, 0)}.
436 * </p>
437 *
438 * @param keyCode The keycode.
439 * @param chars The array of matching characters to consider.
440 * @return The matching associated character, or 0 if none.
Kirill Grouchnikovf2c2ae02016-08-09 15:07:06 -0400441 * @throws {@link IllegalArgumentException} if the passed array of characters is null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800443 public char getMatch(int keyCode, char[] chars) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 return getMatch(keyCode, chars, 0);
445 }
446
447 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800448 * Gets the first character in the character array that can be generated
449 * by the specified key code. If there are multiple choices, prefers
450 * the one that would be generated with the specified meta key modifier state.
451 *
452 * @param keyCode The key code.
453 * @param chars The array of matching characters to consider.
454 * @param metaState The preferred meta key modifier state.
455 * @return The matching associated character, or 0 if none.
Kirill Grouchnikovf2c2ae02016-08-09 15:07:06 -0400456 * @throws {@link IllegalArgumentException} if the passed array of characters is null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800458 public char getMatch(int keyCode, char[] chars, int metaState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 if (chars == null) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800460 throw new IllegalArgumentException("chars must not be null.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
Jeff Brown49ed71d2010-12-06 17:13:33 -0800462
Jeff Brown28cbf4b2010-12-13 10:33:20 -0800463 metaState = KeyEvent.normalizeMetaState(metaState);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800464 return nativeGetMatch(mPtr, keyCode, chars, metaState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466
467 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800468 * Gets the primary character for this key.
469 * In other words, the label that is physically printed on it.
470 *
471 * @param keyCode The key code.
472 * @return The display label character, or 0 if none (eg. for non-printing keys).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800474 public char getDisplayLabel(int keyCode) {
475 return nativeGetDisplayLabel(mPtr, keyCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477
478 /**
Jean Chalard54865502013-02-25 19:56:32 -0800479 * Get the character that is produced by combining the dead key producing accent
480 * with the key producing character c.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 * For example, getDeadChar('`', 'e') returns &egrave;.
Jean Chalard54865502013-02-25 19:56:32 -0800482 * getDeadChar('^', ' ') returns '^' and getDeadChar('^', '^') returns '^'.
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800483 *
484 * @param accent The accent character. eg. '`'
485 * @param c The basic character.
486 * @return The combined character, or 0 if the characters cannot be combined.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800488 public static int getDeadChar(int accent, int c) {
Jean Chalard54865502013-02-25 19:56:32 -0800489 if (c == accent || CHAR_SPACE == c) {
490 // The same dead character typed twice or a dead character followed by a
491 // space should both produce the non-combining version of the combining char.
492 // In this case we don't even need to compute the combining character.
493 return accent;
494 }
495
Jeff Brownc3643b92012-04-18 19:09:08 -0700496 int combining = sAccentToCombining.get(accent);
497 if (combining == 0) {
498 return 0;
Jeff Brownaccce942012-04-18 15:47:48 -0700499 }
Jeff Brownc3643b92012-04-18 19:09:08 -0700500
501 final int combination = (combining << 16) | c;
502 int combined;
503 synchronized (sDeadKeyCache) {
504 combined = sDeadKeyCache.get(combination, -1);
505 if (combined == -1) {
506 sDeadKeyBuilder.setLength(0);
507 sDeadKeyBuilder.append((char)c);
508 sDeadKeyBuilder.append((char)combining);
509 String result = Normalizer.normalize(sDeadKeyBuilder, Normalizer.Form.NFC);
Jean Chalard54865502013-02-25 19:56:32 -0800510 combined = result.codePointCount(0, result.length()) == 1
511 ? result.codePointAt(0) : 0;
Jeff Brownc3643b92012-04-18 19:09:08 -0700512 sDeadKeyCache.put(combination, combined);
513 }
514 }
515 return combined;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800518 /**
519 * Describes the character mappings associated with a key.
520 *
521 * @deprecated instead use {@link KeyCharacterMap#getDisplayLabel(int)},
522 * {@link KeyCharacterMap#getNumber(int)} and {@link KeyCharacterMap#get(int, int)}.
523 */
524 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 public static class KeyData {
526 public static final int META_LENGTH = 4;
527
528 /**
529 * The display label (see {@link #getDisplayLabel}).
530 */
531 public char displayLabel;
532 /**
533 * The "number" value (see {@link #getNumber}).
534 */
535 public char number;
536 /**
537 * The character that will be generated in various meta states
538 * (the same ones used for {@link #get} and defined as
539 * {@link KeyEvent#META_SHIFT_ON} and {@link KeyEvent#META_ALT_ON}).
540 * <table>
541 * <tr><th>Index</th><th align="left">Value</th></tr>
542 * <tr><td>0</td><td>no modifiers</td></tr>
543 * <tr><td>1</td><td>caps</td></tr>
544 * <tr><td>2</td><td>alt</td></tr>
545 * <tr><td>3</td><td>caps + alt</td></tr>
546 * </table>
547 */
548 public char[] meta = new char[META_LENGTH];
549 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800552 * Get the character conversion data for a given key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800554 * @param keyCode The keyCode to query.
555 * @param results A {@link KeyData} instance that will be filled with the results.
556 * @return True if the key was mapped. If the key was not mapped, results is not modified.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 *
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800558 * @deprecated instead use {@link KeyCharacterMap#getDisplayLabel(int)},
559 * {@link KeyCharacterMap#getNumber(int)} or {@link KeyCharacterMap#get(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800561 @Deprecated
562 public boolean getKeyData(int keyCode, KeyData results) {
563 if (results.meta.length < KeyData.META_LENGTH) {
564 throw new IndexOutOfBoundsException(
565 "results.meta.length must be >= " + KeyData.META_LENGTH);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800567
568 char displayLabel = nativeGetDisplayLabel(mPtr, keyCode);
569 if (displayLabel == 0) {
570 return false;
571 }
572
573 results.displayLabel = displayLabel;
574 results.number = nativeGetNumber(mPtr, keyCode);
575 results.meta[0] = nativeGetCharacter(mPtr, keyCode, 0);
576 results.meta[1] = nativeGetCharacter(mPtr, keyCode, KeyEvent.META_SHIFT_ON);
577 results.meta[2] = nativeGetCharacter(mPtr, keyCode, KeyEvent.META_ALT_ON);
578 results.meta[3] = nativeGetCharacter(mPtr, keyCode,
579 KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON);
580 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582
583 /**
584 * Get an array of KeyEvent objects that if put into the input stream
585 * could plausibly generate the provided sequence of characters. It is
586 * not guaranteed that the sequence is the only way to generate these
587 * events or that it is optimal.
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800588 * <p>
589 * This function is primarily offered for instrumentation and testing purposes.
590 * It may fail to map characters to key codes. In particular, the key character
591 * map for the {@link #BUILT_IN_KEYBOARD built-in keyboard} device id may be empty.
592 * Consider using the key character map associated with the
593 * {@link #VIRTUAL_KEYBOARD virtual keyboard} device id instead.
594 * </p><p>
595 * For robust text entry, do not use this function. Instead construct a
596 * {@link KeyEvent} with action code {@link KeyEvent#ACTION_MULTIPLE} that contains
597 * the desired string using {@link KeyEvent#KeyEvent(long, String, int, int)}.
598 * </p>
599 *
600 * @param chars The sequence of characters to generate.
601 * @return An array of {@link KeyEvent} objects, or null if the given char array
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 * can not be generated using the current key character map.
Kirill Grouchnikovf2c2ae02016-08-09 15:07:06 -0400603 * @throws {@link IllegalArgumentException} if the passed array of characters is null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800605 public KeyEvent[] getEvents(char[] chars) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 if (chars == null) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800607 throw new IllegalArgumentException("chars must not be null.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700609 return nativeGetEvents(mPtr, chars);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
611
612 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800613 * Returns true if the specified key produces a glyph.
614 *
615 * @param keyCode The key code.
616 * @return True if the key is a printing key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800618 public boolean isPrintingKey(int keyCode) {
619 int type = Character.getType(nativeGetDisplayLabel(mPtr, keyCode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
621 switch (type)
622 {
623 case Character.SPACE_SEPARATOR:
624 case Character.LINE_SEPARATOR:
625 case Character.PARAGRAPH_SEPARATOR:
626 case Character.CONTROL:
627 case Character.FORMAT:
628 return false;
629 default:
630 return true;
631 }
632 }
633
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800634 /**
635 * Gets the keyboard type.
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700636 * Returns {@link #NUMERIC}, {@link #PREDICTIVE}, {@link #ALPHA}, {@link #FULL}
637 * or {@link #SPECIAL_FUNCTION}.
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800638 * <p>
639 * Different keyboard types have different semantics. Refer to the documentation
640 * associated with the keyboard type constants for details.
641 * </p>
642 *
643 * @return The keyboard type.
644 */
645 public int getKeyboardType() {
646 return nativeGetKeyboardType(mPtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648
649 /**
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800650 * Gets a constant that describes the behavior of this keyboard's modifier keys
651 * such as {@link KeyEvent#KEYCODE_SHIFT_LEFT}.
652 * <p>
653 * Currently there are two behaviors that may be combined:
654 * </p>
655 * <ul>
656 * <li>Chorded behavior: When the modifier key is pressed together with one or more
657 * character keys, the keyboard inserts the modified keys and
658 * then resets the modifier state when the modifier key is released.</li>
659 * <li>Toggled behavior: When the modifier key is pressed and released on its own
660 * it first toggles into a latched state. When latched, the modifier will apply
661 * to next character key that is pressed and will then reset itself to the initial state.
662 * If the modifier is already latched and the modifier key is pressed and release on
663 * its own again, then it toggles into a locked state. When locked, the modifier will
664 * apply to all subsequent character keys that are pressed until unlocked by pressing
665 * the modifier key on its own one more time to reset it to the initial state.
666 * Toggled behavior is useful for small profile keyboards designed for thumb typing.
667 * </ul>
668 * <p>
669 * This function currently returns {@link #MODIFIER_BEHAVIOR_CHORDED} when the
670 * {@link #getKeyboardType() keyboard type} is {@link #FULL} or {@link #SPECIAL_FUNCTION} and
671 * {@link #MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED} otherwise.
672 * In the future, the function may also take into account global keyboard
673 * accessibility settings, other user preferences, or new device capabilities.
674 * </p>
675 *
676 * @return The modifier behavior for this keyboard.
677 *
Elliot Waite54de7742017-01-11 15:30:35 -0800678 * @see #MODIFIER_BEHAVIOR_CHORDED
679 * @see #MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 */
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800681 public int getModifierBehavior() {
682 switch (getKeyboardType()) {
683 case FULL:
684 case SPECIAL_FUNCTION:
685 return MODIFIER_BEHAVIOR_CHORDED;
686 default:
687 return MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED;
688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 }
690
691 /**
692 * Queries the framework about whether any physical keys exist on the
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800693 * any keyboard attached to the device that are capable of producing the given key code.
694 *
695 * @param keyCode The key code to query.
696 * @return True if at least one attached keyboard supports the specified key code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 */
698 public static boolean deviceHasKey(int keyCode) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700699 return InputManager.getInstance().deviceHasKeys(new int[] { keyCode })[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800701
702 /**
703 * Queries the framework about whether any physical keys exist on the
704 * any keyboard attached to the device that are capable of producing the given
705 * array of key codes.
706 *
707 * @param keyCodes The array of key codes to query.
708 * @return A new array of the same size as the key codes array whose elements
709 * are set to true if at least one attached keyboard supports the corresponding key code
710 * at the same index in the key codes array.
711 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 public static boolean[] deviceHasKeys(int[] keyCodes) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700713 return InputManager.getInstance().deviceHasKeys(keyCodes);
714 }
715
716 @Override
717 public void writeToParcel(Parcel out, int flags) {
718 if (out == null) {
719 throw new IllegalArgumentException("parcel must not be null");
720 }
721 nativeWriteToParcel(mPtr, out);
722 }
723
724 @Override
725 public int describeContents() {
726 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 }
728
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800729 /**
730 * Thrown by {@link KeyCharacterMap#load} when a key character map could not be loaded.
731 */
Jeff Brown7e1e21f2011-01-19 17:05:01 -0800732 public static class UnavailableException extends AndroidRuntimeException {
733 public UnavailableException(String msg) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800734 super(msg);
735 }
736 }
Jeff Brown49ed71d2010-12-06 17:13:33 -0800737
738 /**
739 * Specifies a substitute key code and meta state as a fallback action
740 * for an unhandled key.
741 * @hide
742 */
743 public static final class FallbackAction {
Jeff Brownfd23e3e2012-05-09 13:34:28 -0700744 private static final int MAX_RECYCLED = 10;
745 private static final Object sRecycleLock = new Object();
746 private static FallbackAction sRecycleBin;
747 private static int sRecycledCount;
748
749 private FallbackAction next;
750
Jeff Brown49ed71d2010-12-06 17:13:33 -0800751 public int keyCode;
752 public int metaState;
Jeff Brownfd23e3e2012-05-09 13:34:28 -0700753
754 private FallbackAction() {
755 }
756
757 public static FallbackAction obtain() {
758 final FallbackAction target;
759 synchronized (sRecycleLock) {
760 if (sRecycleBin == null) {
761 target = new FallbackAction();
762 } else {
763 target = sRecycleBin;
764 sRecycleBin = target.next;
765 sRecycledCount--;
766 target.next = null;
767 }
768 }
769 return target;
770 }
771
772 public void recycle() {
773 synchronized (sRecycleLock) {
774 if (sRecycledCount < MAX_RECYCLED) {
775 next = sRecycleBin;
776 sRecycleBin = this;
777 sRecycledCount += 1;
778 } else {
779 next = null;
780 }
781 }
782 }
Jeff Brown49ed71d2010-12-06 17:13:33 -0800783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784}