The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Mathew Inwood | e5ad598 | 2018-08-17 15:07:52 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 20 | import android.hardware.input.InputManager; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.text.method.MetaKeyKeyListener; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 24 | import android.util.AndroidRuntimeException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.util.SparseIntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 27 | import java.text.Normalizer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 29 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 30 | * Describes the keys provided by a keyboard device and their associated labels. |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 31 | */ |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 32 | public class KeyCharacterMap implements Parcelable { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | /** |
| 34 | * The id of the device's primary built in keyboard is always 0. |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 35 | * |
| 36 | * @deprecated This constant should no longer be used because there is no |
| 37 | * guarantee that a device has a built-in keyboard that can be used for |
| 38 | * typing text. There might not be a built-in keyboard, the built-in keyboard |
| 39 | * might be a {@link #NUMERIC} or {@link #SPECIAL_FUNCTION} keyboard, or there |
| 40 | * might be multiple keyboards installed including external keyboards. |
| 41 | * When interpreting key presses received from the framework, applications should |
Jeff Brown | 46a5ae6 | 2010-11-30 19:52:29 -0800 | [diff] [blame] | 42 | * use the device id specified in the {@link KeyEvent} received. |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 43 | * When synthesizing key presses for delivery elsewhere or when translating key presses |
| 44 | * from unknown keyboards, applications should use the special {@link #VIRTUAL_KEYBOARD} |
| 45 | * device id. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | */ |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 47 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | public static final int BUILT_IN_KEYBOARD = 0; |
| 49 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 50 | /** |
| 51 | * The id of a generic virtual keyboard with a full layout that can be used to |
| 52 | * synthesize key events. Typically used with {@link #getEvents}. |
| 53 | */ |
| 54 | public static final int VIRTUAL_KEYBOARD = -1; |
| 55 | |
| 56 | /** |
| 57 | * A numeric (12-key) keyboard. |
| 58 | * <p> |
| 59 | * A numeric keyboard supports text entry using a multi-tap approach. |
| 60 | * It may be necessary to tap a key multiple times to generate the desired letter |
| 61 | * or symbol. |
| 62 | * </p><p> |
| 63 | * This type of keyboard is generally designed for thumb typing. |
| 64 | * </p> |
| 65 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | public static final int NUMERIC = 1; |
| 67 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 68 | /** |
| 69 | * A keyboard with all the letters, but with more than one letter per key. |
| 70 | * <p> |
| 71 | * This type of keyboard is generally designed for thumb typing. |
| 72 | * </p> |
| 73 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | public static final int PREDICTIVE = 2; |
| 75 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 76 | /** |
| 77 | * A keyboard with all the letters, and maybe some numbers. |
| 78 | * <p> |
| 79 | * An alphabetic keyboard supports text entry directly but may have a condensed |
| 80 | * layout with a small form factor. In contrast to a {@link #FULL full keyboard}, some |
| 81 | * symbols may only be accessible using special on-screen character pickers. |
| 82 | * In addition, to improve typing speed and accuracy, the framework provides |
| 83 | * special affordances for alphabetic keyboards such as auto-capitalization |
| 84 | * and toggled / locked shift and alt keys. |
| 85 | * </p><p> |
| 86 | * This type of keyboard is generally designed for thumb typing. |
| 87 | * </p> |
| 88 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | public static final int ALPHA = 3; |
| 90 | |
| 91 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 92 | * A full PC-style keyboard. |
| 93 | * <p> |
| 94 | * A full keyboard behaves like a PC keyboard. All symbols are accessed directly |
| 95 | * by pressing keys on the keyboard without on-screen support or affordances such |
| 96 | * as auto-capitalization. |
| 97 | * </p><p> |
| 98 | * This type of keyboard is generally designed for full two hand typing. |
| 99 | * </p> |
| 100 | */ |
| 101 | public static final int FULL = 4; |
| 102 | |
| 103 | /** |
| 104 | * A keyboard that is only used to control special functions rather than for typing. |
| 105 | * <p> |
| 106 | * A special function keyboard consists only of non-printing keys such as |
| 107 | * HOME and POWER that are not actually used for typing. |
| 108 | * </p> |
| 109 | */ |
| 110 | public static final int SPECIAL_FUNCTION = 5; |
| 111 | |
| 112 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | * This private-use character is used to trigger Unicode character |
| 114 | * input by hex digits. |
| 115 | */ |
| 116 | public static final char HEX_INPUT = '\uEF00'; |
| 117 | |
| 118 | /** |
| 119 | * This private-use character is used to bring up a character picker for |
| 120 | * miscellaneous symbols. |
| 121 | */ |
| 122 | public static final char PICKER_DIALOG_INPUT = '\uEF01'; |
| 123 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 124 | /** |
| 125 | * Modifier keys may be chorded with character keys. |
| 126 | * |
| 127 | * @see {#link #getModifierBehavior()} for more details. |
| 128 | */ |
| 129 | public static final int MODIFIER_BEHAVIOR_CHORDED = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 131 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 132 | * Modifier keys may be chorded with character keys or they may toggle |
| 133 | * into latched or locked states when pressed independently. |
| 134 | * |
| 135 | * @see {#link #getModifierBehavior()} for more details. |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 136 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 137 | public static final int MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED = 1; |
| 138 | |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 139 | /* |
| 140 | * This bit will be set in the return value of {@link #get(int, int)} if the |
| 141 | * key is a "dead key." |
| 142 | */ |
| 143 | public static final int COMBINING_ACCENT = 0x80000000; |
| 144 | |
| 145 | /** |
| 146 | * Mask the return value from {@link #get(int, int)} with this value to get |
| 147 | * a printable representation of the accent character of a "dead key." |
| 148 | */ |
| 149 | public static final int COMBINING_ACCENT_MASK = 0x7FFFFFFF; |
| 150 | |
| 151 | /* Characters used to display placeholders for dead keys. */ |
| 152 | private static final int ACCENT_ACUTE = '\u00B4'; |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 153 | private static final int ACCENT_BREVE = '\u02D8'; |
| 154 | private static final int ACCENT_CARON = '\u02C7'; |
| 155 | private static final int ACCENT_CEDILLA = '\u00B8'; |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 156 | private static final int ACCENT_CIRCUMFLEX = '\u02C6'; |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 157 | private static final int ACCENT_COMMA_ABOVE = '\u1FBD'; |
| 158 | private static final int ACCENT_COMMA_ABOVE_RIGHT = '\u02BC'; |
| 159 | private static final int ACCENT_DOT_ABOVE = '\u02D9'; |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 160 | private static final int ACCENT_DOT_BELOW = '.'; // approximate |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 161 | private static final int ACCENT_DOUBLE_ACUTE = '\u02DD'; |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 162 | private static final int ACCENT_GRAVE = '\u02CB'; |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 163 | private static final int ACCENT_HOOK_ABOVE = '\u02C0'; |
| 164 | private static final int ACCENT_HORN = '\''; // approximate |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 165 | private static final int ACCENT_MACRON = '\u00AF'; |
| 166 | private static final int ACCENT_MACRON_BELOW = '\u02CD'; |
| 167 | private static final int ACCENT_OGONEK = '\u02DB'; |
| 168 | private static final int ACCENT_REVERSED_COMMA_ABOVE = '\u02BD'; |
| 169 | private static final int ACCENT_RING_ABOVE = '\u02DA'; |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 170 | private static final int ACCENT_STROKE = '-'; // approximate |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 171 | private static final int ACCENT_TILDE = '\u02DC'; |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 172 | private static final int ACCENT_TURNED_COMMA_ABOVE = '\u02BB'; |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 173 | private static final int ACCENT_UMLAUT = '\u00A8'; |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 174 | private static final int ACCENT_VERTICAL_LINE_ABOVE = '\u02C8'; |
| 175 | private static final int ACCENT_VERTICAL_LINE_BELOW = '\u02CC'; |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 176 | |
| 177 | /* Legacy dead key display characters used in previous versions of the API. |
| 178 | * We still support these characters by mapping them to their non-legacy version. */ |
| 179 | private static final int ACCENT_GRAVE_LEGACY = '`'; |
| 180 | private static final int ACCENT_CIRCUMFLEX_LEGACY = '^'; |
| 181 | private static final int ACCENT_TILDE_LEGACY = '~'; |
| 182 | |
Jean Chalard | 5486550 | 2013-02-25 19:56:32 -0800 | [diff] [blame] | 183 | private static final int CHAR_SPACE = ' '; |
| 184 | |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 185 | /** |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 186 | * Maps Unicode combining diacritical to display-form dead key. |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 187 | */ |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 188 | private static final SparseIntArray sCombiningToAccent = new SparseIntArray(); |
| 189 | private static final SparseIntArray sAccentToCombining = new SparseIntArray(); |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 190 | static { |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 191 | addCombining('\u0300', ACCENT_GRAVE); |
| 192 | addCombining('\u0301', ACCENT_ACUTE); |
| 193 | addCombining('\u0302', ACCENT_CIRCUMFLEX); |
| 194 | addCombining('\u0303', ACCENT_TILDE); |
| 195 | addCombining('\u0304', ACCENT_MACRON); |
| 196 | addCombining('\u0306', ACCENT_BREVE); |
| 197 | addCombining('\u0307', ACCENT_DOT_ABOVE); |
| 198 | addCombining('\u0308', ACCENT_UMLAUT); |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 199 | addCombining('\u0309', ACCENT_HOOK_ABOVE); |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 200 | addCombining('\u030A', ACCENT_RING_ABOVE); |
| 201 | addCombining('\u030B', ACCENT_DOUBLE_ACUTE); |
| 202 | addCombining('\u030C', ACCENT_CARON); |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 203 | addCombining('\u030D', ACCENT_VERTICAL_LINE_ABOVE); |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 204 | //addCombining('\u030E', ACCENT_DOUBLE_VERTICAL_LINE_ABOVE); |
| 205 | //addCombining('\u030F', ACCENT_DOUBLE_GRAVE); |
| 206 | //addCombining('\u0310', ACCENT_CANDRABINDU); |
| 207 | //addCombining('\u0311', ACCENT_INVERTED_BREVE); |
| 208 | addCombining('\u0312', ACCENT_TURNED_COMMA_ABOVE); |
| 209 | addCombining('\u0313', ACCENT_COMMA_ABOVE); |
| 210 | addCombining('\u0314', ACCENT_REVERSED_COMMA_ABOVE); |
| 211 | addCombining('\u0315', ACCENT_COMMA_ABOVE_RIGHT); |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 212 | addCombining('\u031B', ACCENT_HORN); |
| 213 | addCombining('\u0323', ACCENT_DOT_BELOW); |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 214 | //addCombining('\u0326', ACCENT_COMMA_BELOW); |
| 215 | addCombining('\u0327', ACCENT_CEDILLA); |
| 216 | addCombining('\u0328', ACCENT_OGONEK); |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 217 | addCombining('\u0329', ACCENT_VERTICAL_LINE_BELOW); |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 218 | addCombining('\u0331', ACCENT_MACRON_BELOW); |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 219 | addCombining('\u0335', ACCENT_STROKE); |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 220 | //addCombining('\u0342', ACCENT_PERISPOMENI); |
| 221 | //addCombining('\u0344', ACCENT_DIALYTIKA_TONOS); |
| 222 | //addCombining('\u0345', ACCENT_YPOGEGRAMMENI); |
| 223 | |
| 224 | // One-way mappings to equivalent preferred accents. |
| 225 | sCombiningToAccent.append('\u0340', ACCENT_GRAVE); |
| 226 | sCombiningToAccent.append('\u0341', ACCENT_ACUTE); |
| 227 | sCombiningToAccent.append('\u0343', ACCENT_COMMA_ABOVE); |
| 228 | |
| 229 | // One-way legacy mappings to preserve compatibility with older applications. |
| 230 | sAccentToCombining.append(ACCENT_GRAVE_LEGACY, '\u0300'); |
| 231 | sAccentToCombining.append(ACCENT_CIRCUMFLEX_LEGACY, '\u0302'); |
| 232 | sAccentToCombining.append(ACCENT_TILDE_LEGACY, '\u0303'); |
| 233 | } |
| 234 | |
| 235 | private static void addCombining(int combining, int accent) { |
| 236 | sCombiningToAccent.append(combining, accent); |
| 237 | sAccentToCombining.append(accent, combining); |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 241 | * Maps combinations of (display-form) combining key and second character |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 242 | * to combined output character. |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 243 | * These mappings are derived from the Unicode NFC tables as needed. |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 244 | */ |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 245 | private static final SparseIntArray sDeadKeyCache = new SparseIntArray(); |
| 246 | private static final StringBuilder sDeadKeyBuilder = new StringBuilder(); |
Jeff Brown | 69b2be1 | 2012-04-25 15:10:54 -0700 | [diff] [blame] | 247 | static { |
| 248 | // Non-standard decompositions. |
| 249 | // Stroke modifier for Finnish multilingual keyboard and others. |
| 250 | addDeadKey(ACCENT_STROKE, 'D', '\u0110'); |
| 251 | addDeadKey(ACCENT_STROKE, 'G', '\u01e4'); |
| 252 | addDeadKey(ACCENT_STROKE, 'H', '\u0126'); |
| 253 | addDeadKey(ACCENT_STROKE, 'I', '\u0197'); |
| 254 | addDeadKey(ACCENT_STROKE, 'L', '\u0141'); |
| 255 | addDeadKey(ACCENT_STROKE, 'O', '\u00d8'); |
| 256 | addDeadKey(ACCENT_STROKE, 'T', '\u0166'); |
| 257 | addDeadKey(ACCENT_STROKE, 'd', '\u0111'); |
| 258 | addDeadKey(ACCENT_STROKE, 'g', '\u01e5'); |
| 259 | addDeadKey(ACCENT_STROKE, 'h', '\u0127'); |
| 260 | addDeadKey(ACCENT_STROKE, 'i', '\u0268'); |
| 261 | addDeadKey(ACCENT_STROKE, 'l', '\u0142'); |
| 262 | addDeadKey(ACCENT_STROKE, 'o', '\u00f8'); |
| 263 | addDeadKey(ACCENT_STROKE, 't', '\u0167'); |
| 264 | } |
| 265 | |
| 266 | private static void addDeadKey(int accent, int c, int result) { |
| 267 | final int combining = sAccentToCombining.get(accent); |
| 268 | if (combining == 0) { |
| 269 | throw new IllegalStateException("Invalid dead key declaration."); |
| 270 | } |
| 271 | final int combination = (combining << 16) | c; |
| 272 | sDeadKeyCache.put(combination, result); |
| 273 | } |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 274 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 275 | public static final Parcelable.Creator<KeyCharacterMap> CREATOR = |
| 276 | new Parcelable.Creator<KeyCharacterMap>() { |
| 277 | public KeyCharacterMap createFromParcel(Parcel in) { |
| 278 | return new KeyCharacterMap(in); |
| 279 | } |
| 280 | public KeyCharacterMap[] newArray(int size) { |
| 281 | return new KeyCharacterMap[size]; |
| 282 | } |
| 283 | }; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 284 | |
Ashok Bhat | 0c3f921 | 2014-01-08 14:15:02 +0000 | [diff] [blame] | 285 | private long mPtr; |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 286 | |
Ashok Bhat | 0c3f921 | 2014-01-08 14:15:02 +0000 | [diff] [blame] | 287 | private static native long nativeReadFromParcel(Parcel in); |
| 288 | private static native void nativeWriteToParcel(long ptr, Parcel out); |
| 289 | private static native void nativeDispose(long ptr); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 290 | |
Ashok Bhat | 0c3f921 | 2014-01-08 14:15:02 +0000 | [diff] [blame] | 291 | private static native char nativeGetCharacter(long ptr, int keyCode, int metaState); |
| 292 | private static native boolean nativeGetFallbackAction(long ptr, int keyCode, int metaState, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 293 | FallbackAction outFallbackAction); |
Ashok Bhat | 0c3f921 | 2014-01-08 14:15:02 +0000 | [diff] [blame] | 294 | private static native char nativeGetNumber(long ptr, int keyCode); |
| 295 | private static native char nativeGetMatch(long ptr, int keyCode, char[] chars, int metaState); |
| 296 | private static native char nativeGetDisplayLabel(long ptr, int keyCode); |
| 297 | private static native int nativeGetKeyboardType(long ptr); |
| 298 | private static native KeyEvent[] nativeGetEvents(long ptr, char[] chars); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 299 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 300 | private KeyCharacterMap(Parcel in) { |
| 301 | if (in == null) { |
| 302 | throw new IllegalArgumentException("parcel must not be null"); |
| 303 | } |
| 304 | mPtr = nativeReadFromParcel(in); |
| 305 | if (mPtr == 0) { |
| 306 | throw new RuntimeException("Could not read KeyCharacterMap from parcel."); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // Called from native |
Mathew Inwood | e5ad598 | 2018-08-17 15:07:52 +0100 | [diff] [blame] | 311 | @UnsupportedAppUsage |
Ashok Bhat | 0c3f921 | 2014-01-08 14:15:02 +0000 | [diff] [blame] | 312 | private KeyCharacterMap(long ptr) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 313 | mPtr = ptr; |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | protected void finalize() throws Throwable { |
| 318 | if (mPtr != 0) { |
| 319 | nativeDispose(mPtr); |
| 320 | mPtr = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 324 | /** |
| 325 | * Loads the key character maps for the keyboard with the specified device id. |
| 326 | * |
| 327 | * @param deviceId The device id of the keyboard. |
| 328 | * @return The associated key character map. |
Jeff Brown | 7e1e21f | 2011-01-19 17:05:01 -0800 | [diff] [blame] | 329 | * @throws {@link UnavailableException} if the key character map |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 330 | * could not be loaded because it was malformed or the default key character map |
| 331 | * is missing from the system. |
| 332 | */ |
| 333 | public static KeyCharacterMap load(int deviceId) { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 334 | final InputManager im = InputManager.getInstance(); |
| 335 | InputDevice inputDevice = im.getInputDevice(deviceId); |
| 336 | if (inputDevice == null) { |
| 337 | inputDevice = im.getInputDevice(VIRTUAL_KEYBOARD); |
| 338 | if (inputDevice == null) { |
| 339 | throw new UnavailableException( |
| 340 | "Could not load key character map for device " + deviceId); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 341 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 342 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 343 | return inputDevice.getKeyCharacterMap(); |
Jeff Brown | 9df6e7a | 2012-04-05 11:49:26 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 347 | * Gets the Unicode character generated by the specified key and meta |
| 348 | * key state combination. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | * <p> |
| 350 | * Returns the Unicode character that the specified key would produce |
| 351 | * when the specified meta bits (see {@link MetaKeyKeyListener}) |
| 352 | * were active. |
| 353 | * </p><p> |
| 354 | * Returns 0 if the key is not one that is used to type Unicode |
| 355 | * characters. |
| 356 | * </p><p> |
| 357 | * If the return value has bit {@link #COMBINING_ACCENT} set, the |
| 358 | * key is a "dead key" that should be combined with another to |
| 359 | * actually produce a character -- see {@link #getDeadChar} -- |
| 360 | * after masking with {@link #COMBINING_ACCENT_MASK}. |
| 361 | * </p> |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 362 | * |
| 363 | * @param keyCode The key code. |
| 364 | * @param metaState The meta key modifier state. |
| 365 | * @return The associated character or combining accent, or 0 if none. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 367 | public int get(int keyCode, int metaState) { |
Jeff Brown | 28cbf4b | 2010-12-13 10:33:20 -0800 | [diff] [blame] | 368 | metaState = KeyEvent.normalizeMetaState(metaState); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 369 | char ch = nativeGetCharacter(mPtr, keyCode, metaState); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 370 | |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 371 | int map = sCombiningToAccent.get(ch); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | if (map != 0) { |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 373 | return map | COMBINING_ACCENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | } else { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 375 | return ch; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 380 | * Gets the fallback action to perform if the application does not |
| 381 | * handle the specified key. |
| 382 | * <p> |
| 383 | * When an application does not handle a particular key, the system may |
| 384 | * translate the key to an alternate fallback key (specified in the |
| 385 | * fallback action) and dispatch it to the application. |
| 386 | * The event containing the fallback key is flagged |
| 387 | * with {@link KeyEvent#FLAG_FALLBACK}. |
| 388 | * </p> |
| 389 | * |
| 390 | * @param keyCode The key code. |
| 391 | * @param metaState The meta key modifier state. |
Jeff Brown | fd23e3e | 2012-05-09 13:34:28 -0700 | [diff] [blame] | 392 | * @return The fallback action, or null if none. Remember to recycle the fallback action. |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 393 | * |
| 394 | * @hide |
| 395 | */ |
Jeff Brown | fd23e3e | 2012-05-09 13:34:28 -0700 | [diff] [blame] | 396 | public FallbackAction getFallbackAction(int keyCode, int metaState) { |
| 397 | FallbackAction action = FallbackAction.obtain(); |
Jeff Brown | 28cbf4b | 2010-12-13 10:33:20 -0800 | [diff] [blame] | 398 | metaState = KeyEvent.normalizeMetaState(metaState); |
Jeff Brown | fd23e3e | 2012-05-09 13:34:28 -0700 | [diff] [blame] | 399 | if (nativeGetFallbackAction(mPtr, keyCode, metaState, action)) { |
| 400 | action.metaState = KeyEvent.normalizeMetaState(action.metaState); |
| 401 | return action; |
| 402 | } |
| 403 | action.recycle(); |
| 404 | return null; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 408 | * Gets the number or symbol associated with the key. |
| 409 | * <p> |
| 410 | * The character value is returned, not the numeric value. |
| 411 | * If the key is not a number, but is a symbol, the symbol is retuned. |
| 412 | * </p><p> |
| 413 | * This method is intended to to support dial pads and other numeric or |
| 414 | * symbolic entry on keyboards where certain keys serve dual function |
| 415 | * as alphabetic and symbolic keys. This method returns the number |
| 416 | * or symbol associated with the key independent of whether the user |
| 417 | * has pressed the required modifier. |
| 418 | * </p><p> |
| 419 | * For example, on one particular keyboard the keys on the top QWERTY row generate |
| 420 | * numbers when ALT is pressed such that ALT-Q maps to '1'. So for that keyboard |
| 421 | * when {@link #getNumber} is called with {@link KeyEvent#KEYCODE_Q} it returns '1' |
| 422 | * so that the user can type numbers without pressing ALT when it makes sense. |
| 423 | * </p> |
| 424 | * |
| 425 | * @param keyCode The key code. |
| 426 | * @return The associated numeric or symbolic character, or 0 if none. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 428 | public char getNumber(int keyCode) { |
| 429 | return nativeGetNumber(mPtr, keyCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 433 | * Gets the first character in the character array that can be generated |
| 434 | * by the specified key code. |
| 435 | * <p> |
| 436 | * This is a convenience function that returns the same value as |
| 437 | * {@link #getMatch(int,char[],int) getMatch(keyCode, chars, 0)}. |
| 438 | * </p> |
| 439 | * |
| 440 | * @param keyCode The keycode. |
| 441 | * @param chars The array of matching characters to consider. |
| 442 | * @return The matching associated character, or 0 if none. |
Kirill Grouchnikov | f2c2ae0 | 2016-08-09 15:07:06 -0400 | [diff] [blame] | 443 | * @throws {@link IllegalArgumentException} if the passed array of characters is null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 445 | public char getMatch(int keyCode, char[] chars) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | return getMatch(keyCode, chars, 0); |
| 447 | } |
| 448 | |
| 449 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 450 | * Gets the first character in the character array that can be generated |
| 451 | * by the specified key code. If there are multiple choices, prefers |
| 452 | * the one that would be generated with the specified meta key modifier state. |
| 453 | * |
| 454 | * @param keyCode The key code. |
| 455 | * @param chars The array of matching characters to consider. |
| 456 | * @param metaState The preferred meta key modifier state. |
| 457 | * @return The matching associated character, or 0 if none. |
Kirill Grouchnikov | f2c2ae0 | 2016-08-09 15:07:06 -0400 | [diff] [blame] | 458 | * @throws {@link IllegalArgumentException} if the passed array of characters is null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 460 | public char getMatch(int keyCode, char[] chars, int metaState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | if (chars == null) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 462 | throw new IllegalArgumentException("chars must not be null."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 464 | |
Jeff Brown | 28cbf4b | 2010-12-13 10:33:20 -0800 | [diff] [blame] | 465 | metaState = KeyEvent.normalizeMetaState(metaState); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 466 | return nativeGetMatch(mPtr, keyCode, chars, metaState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 470 | * Gets the primary character for this key. |
| 471 | * In other words, the label that is physically printed on it. |
| 472 | * |
| 473 | * @param keyCode The key code. |
| 474 | * @return The display label character, or 0 if none (eg. for non-printing keys). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 476 | public char getDisplayLabel(int keyCode) { |
| 477 | return nativeGetDisplayLabel(mPtr, keyCode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /** |
Jean Chalard | 5486550 | 2013-02-25 19:56:32 -0800 | [diff] [blame] | 481 | * Get the character that is produced by combining the dead key producing accent |
| 482 | * with the key producing character c. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | * For example, getDeadChar('`', 'e') returns è. |
Jean Chalard | 5486550 | 2013-02-25 19:56:32 -0800 | [diff] [blame] | 484 | * getDeadChar('^', ' ') returns '^' and getDeadChar('^', '^') returns '^'. |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 485 | * |
| 486 | * @param accent The accent character. eg. '`' |
| 487 | * @param c The basic character. |
| 488 | * @return The combined character, or 0 if the characters cannot be combined. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 490 | public static int getDeadChar(int accent, int c) { |
Jean Chalard | 5486550 | 2013-02-25 19:56:32 -0800 | [diff] [blame] | 491 | if (c == accent || CHAR_SPACE == c) { |
| 492 | // The same dead character typed twice or a dead character followed by a |
| 493 | // space should both produce the non-combining version of the combining char. |
| 494 | // In this case we don't even need to compute the combining character. |
| 495 | return accent; |
| 496 | } |
| 497 | |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 498 | int combining = sAccentToCombining.get(accent); |
| 499 | if (combining == 0) { |
| 500 | return 0; |
Jeff Brown | accce94 | 2012-04-18 15:47:48 -0700 | [diff] [blame] | 501 | } |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 502 | |
| 503 | final int combination = (combining << 16) | c; |
| 504 | int combined; |
| 505 | synchronized (sDeadKeyCache) { |
| 506 | combined = sDeadKeyCache.get(combination, -1); |
| 507 | if (combined == -1) { |
| 508 | sDeadKeyBuilder.setLength(0); |
| 509 | sDeadKeyBuilder.append((char)c); |
| 510 | sDeadKeyBuilder.append((char)combining); |
| 511 | String result = Normalizer.normalize(sDeadKeyBuilder, Normalizer.Form.NFC); |
Jean Chalard | 5486550 | 2013-02-25 19:56:32 -0800 | [diff] [blame] | 512 | combined = result.codePointCount(0, result.length()) == 1 |
| 513 | ? result.codePointAt(0) : 0; |
Jeff Brown | c3643b9 | 2012-04-18 19:09:08 -0700 | [diff] [blame] | 514 | sDeadKeyCache.put(combination, combined); |
| 515 | } |
| 516 | } |
| 517 | return combined; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 520 | /** |
| 521 | * Describes the character mappings associated with a key. |
| 522 | * |
| 523 | * @deprecated instead use {@link KeyCharacterMap#getDisplayLabel(int)}, |
| 524 | * {@link KeyCharacterMap#getNumber(int)} and {@link KeyCharacterMap#get(int, int)}. |
| 525 | */ |
| 526 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | public static class KeyData { |
| 528 | public static final int META_LENGTH = 4; |
| 529 | |
| 530 | /** |
| 531 | * The display label (see {@link #getDisplayLabel}). |
| 532 | */ |
| 533 | public char displayLabel; |
| 534 | /** |
| 535 | * The "number" value (see {@link #getNumber}). |
| 536 | */ |
| 537 | public char number; |
| 538 | /** |
| 539 | * The character that will be generated in various meta states |
| 540 | * (the same ones used for {@link #get} and defined as |
| 541 | * {@link KeyEvent#META_SHIFT_ON} and {@link KeyEvent#META_ALT_ON}). |
| 542 | * <table> |
| 543 | * <tr><th>Index</th><th align="left">Value</th></tr> |
| 544 | * <tr><td>0</td><td>no modifiers</td></tr> |
| 545 | * <tr><td>1</td><td>caps</td></tr> |
| 546 | * <tr><td>2</td><td>alt</td></tr> |
| 547 | * <tr><td>3</td><td>caps + alt</td></tr> |
| 548 | * </table> |
| 549 | */ |
| 550 | public char[] meta = new char[META_LENGTH]; |
| 551 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 552 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 554 | * Get the character conversion data for a given key code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | * |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 556 | * @param keyCode The keyCode to query. |
| 557 | * @param results A {@link KeyData} instance that will be filled with the results. |
| 558 | * @return True if the key was mapped. If the key was not mapped, results is not modified. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | * |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 560 | * @deprecated instead use {@link KeyCharacterMap#getDisplayLabel(int)}, |
| 561 | * {@link KeyCharacterMap#getNumber(int)} or {@link KeyCharacterMap#get(int, int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 563 | @Deprecated |
| 564 | public boolean getKeyData(int keyCode, KeyData results) { |
| 565 | if (results.meta.length < KeyData.META_LENGTH) { |
| 566 | throw new IndexOutOfBoundsException( |
| 567 | "results.meta.length must be >= " + KeyData.META_LENGTH); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 569 | |
| 570 | char displayLabel = nativeGetDisplayLabel(mPtr, keyCode); |
| 571 | if (displayLabel == 0) { |
| 572 | return false; |
| 573 | } |
| 574 | |
| 575 | results.displayLabel = displayLabel; |
| 576 | results.number = nativeGetNumber(mPtr, keyCode); |
| 577 | results.meta[0] = nativeGetCharacter(mPtr, keyCode, 0); |
| 578 | results.meta[1] = nativeGetCharacter(mPtr, keyCode, KeyEvent.META_SHIFT_ON); |
| 579 | results.meta[2] = nativeGetCharacter(mPtr, keyCode, KeyEvent.META_ALT_ON); |
| 580 | results.meta[3] = nativeGetCharacter(mPtr, keyCode, |
| 581 | KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON); |
| 582 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Get an array of KeyEvent objects that if put into the input stream |
| 587 | * could plausibly generate the provided sequence of characters. It is |
| 588 | * not guaranteed that the sequence is the only way to generate these |
| 589 | * events or that it is optimal. |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 590 | * <p> |
| 591 | * This function is primarily offered for instrumentation and testing purposes. |
| 592 | * It may fail to map characters to key codes. In particular, the key character |
| 593 | * map for the {@link #BUILT_IN_KEYBOARD built-in keyboard} device id may be empty. |
| 594 | * Consider using the key character map associated with the |
| 595 | * {@link #VIRTUAL_KEYBOARD virtual keyboard} device id instead. |
| 596 | * </p><p> |
| 597 | * For robust text entry, do not use this function. Instead construct a |
| 598 | * {@link KeyEvent} with action code {@link KeyEvent#ACTION_MULTIPLE} that contains |
| 599 | * the desired string using {@link KeyEvent#KeyEvent(long, String, int, int)}. |
| 600 | * </p> |
| 601 | * |
| 602 | * @param chars The sequence of characters to generate. |
| 603 | * @return An array of {@link KeyEvent} objects, or null if the given char array |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | * can not be generated using the current key character map. |
Kirill Grouchnikov | f2c2ae0 | 2016-08-09 15:07:06 -0400 | [diff] [blame] | 605 | * @throws {@link IllegalArgumentException} if the passed array of characters is null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 607 | public KeyEvent[] getEvents(char[] chars) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | if (chars == null) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 609 | throw new IllegalArgumentException("chars must not be null."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 611 | return nativeGetEvents(mPtr, chars); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 615 | * Returns true if the specified key produces a glyph. |
| 616 | * |
| 617 | * @param keyCode The key code. |
| 618 | * @return True if the key is a printing key. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 620 | public boolean isPrintingKey(int keyCode) { |
| 621 | int type = Character.getType(nativeGetDisplayLabel(mPtr, keyCode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | |
| 623 | switch (type) |
| 624 | { |
| 625 | case Character.SPACE_SEPARATOR: |
| 626 | case Character.LINE_SEPARATOR: |
| 627 | case Character.PARAGRAPH_SEPARATOR: |
| 628 | case Character.CONTROL: |
| 629 | case Character.FORMAT: |
| 630 | return false; |
| 631 | default: |
| 632 | return true; |
| 633 | } |
| 634 | } |
| 635 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 636 | /** |
| 637 | * Gets the keyboard type. |
Jeff Brown | 9df6e7a | 2012-04-05 11:49:26 -0700 | [diff] [blame] | 638 | * Returns {@link #NUMERIC}, {@link #PREDICTIVE}, {@link #ALPHA}, {@link #FULL} |
| 639 | * or {@link #SPECIAL_FUNCTION}. |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 640 | * <p> |
| 641 | * Different keyboard types have different semantics. Refer to the documentation |
| 642 | * associated with the keyboard type constants for details. |
| 643 | * </p> |
| 644 | * |
| 645 | * @return The keyboard type. |
| 646 | */ |
| 647 | public int getKeyboardType() { |
| 648 | return nativeGetKeyboardType(mPtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /** |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 652 | * Gets a constant that describes the behavior of this keyboard's modifier keys |
| 653 | * such as {@link KeyEvent#KEYCODE_SHIFT_LEFT}. |
| 654 | * <p> |
| 655 | * Currently there are two behaviors that may be combined: |
| 656 | * </p> |
| 657 | * <ul> |
| 658 | * <li>Chorded behavior: When the modifier key is pressed together with one or more |
| 659 | * character keys, the keyboard inserts the modified keys and |
| 660 | * then resets the modifier state when the modifier key is released.</li> |
| 661 | * <li>Toggled behavior: When the modifier key is pressed and released on its own |
| 662 | * it first toggles into a latched state. When latched, the modifier will apply |
| 663 | * to next character key that is pressed and will then reset itself to the initial state. |
| 664 | * If the modifier is already latched and the modifier key is pressed and release on |
| 665 | * its own again, then it toggles into a locked state. When locked, the modifier will |
| 666 | * apply to all subsequent character keys that are pressed until unlocked by pressing |
| 667 | * the modifier key on its own one more time to reset it to the initial state. |
| 668 | * Toggled behavior is useful for small profile keyboards designed for thumb typing. |
| 669 | * </ul> |
| 670 | * <p> |
| 671 | * This function currently returns {@link #MODIFIER_BEHAVIOR_CHORDED} when the |
| 672 | * {@link #getKeyboardType() keyboard type} is {@link #FULL} or {@link #SPECIAL_FUNCTION} and |
| 673 | * {@link #MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED} otherwise. |
| 674 | * In the future, the function may also take into account global keyboard |
| 675 | * accessibility settings, other user preferences, or new device capabilities. |
| 676 | * </p> |
| 677 | * |
| 678 | * @return The modifier behavior for this keyboard. |
| 679 | * |
Elliot Waite | 54de774 | 2017-01-11 15:30:35 -0800 | [diff] [blame] | 680 | * @see #MODIFIER_BEHAVIOR_CHORDED |
| 681 | * @see #MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | */ |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 683 | public int getModifierBehavior() { |
| 684 | switch (getKeyboardType()) { |
| 685 | case FULL: |
| 686 | case SPECIAL_FUNCTION: |
| 687 | return MODIFIER_BEHAVIOR_CHORDED; |
| 688 | default: |
| 689 | return MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED; |
| 690 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Queries the framework about whether any physical keys exist on the |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 695 | * any keyboard attached to the device that are capable of producing the given key code. |
| 696 | * |
| 697 | * @param keyCode The key code to query. |
| 698 | * @return True if at least one attached keyboard supports the specified key code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | */ |
| 700 | public static boolean deviceHasKey(int keyCode) { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 701 | return InputManager.getInstance().deviceHasKeys(new int[] { keyCode })[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 702 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 703 | |
| 704 | /** |
| 705 | * Queries the framework about whether any physical keys exist on the |
| 706 | * any keyboard attached to the device that are capable of producing the given |
| 707 | * array of key codes. |
| 708 | * |
| 709 | * @param keyCodes The array of key codes to query. |
| 710 | * @return A new array of the same size as the key codes array whose elements |
| 711 | * are set to true if at least one attached keyboard supports the corresponding key code |
| 712 | * at the same index in the key codes array. |
| 713 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | public static boolean[] deviceHasKeys(int[] keyCodes) { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 715 | return InputManager.getInstance().deviceHasKeys(keyCodes); |
| 716 | } |
| 717 | |
| 718 | @Override |
| 719 | public void writeToParcel(Parcel out, int flags) { |
| 720 | if (out == null) { |
| 721 | throw new IllegalArgumentException("parcel must not be null"); |
| 722 | } |
| 723 | nativeWriteToParcel(mPtr, out); |
| 724 | } |
| 725 | |
| 726 | @Override |
| 727 | public int describeContents() { |
| 728 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 731 | /** |
| 732 | * Thrown by {@link KeyCharacterMap#load} when a key character map could not be loaded. |
| 733 | */ |
Jeff Brown | 7e1e21f | 2011-01-19 17:05:01 -0800 | [diff] [blame] | 734 | public static class UnavailableException extends AndroidRuntimeException { |
| 735 | public UnavailableException(String msg) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 736 | super(msg); |
| 737 | } |
| 738 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 739 | |
| 740 | /** |
| 741 | * Specifies a substitute key code and meta state as a fallback action |
| 742 | * for an unhandled key. |
| 743 | * @hide |
| 744 | */ |
| 745 | public static final class FallbackAction { |
Jeff Brown | fd23e3e | 2012-05-09 13:34:28 -0700 | [diff] [blame] | 746 | private static final int MAX_RECYCLED = 10; |
| 747 | private static final Object sRecycleLock = new Object(); |
| 748 | private static FallbackAction sRecycleBin; |
| 749 | private static int sRecycledCount; |
| 750 | |
| 751 | private FallbackAction next; |
| 752 | |
Mathew Inwood | e5ad598 | 2018-08-17 15:07:52 +0100 | [diff] [blame] | 753 | @UnsupportedAppUsage |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 754 | public int keyCode; |
Mathew Inwood | e5ad598 | 2018-08-17 15:07:52 +0100 | [diff] [blame] | 755 | @UnsupportedAppUsage |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 756 | public int metaState; |
Jeff Brown | fd23e3e | 2012-05-09 13:34:28 -0700 | [diff] [blame] | 757 | |
| 758 | private FallbackAction() { |
| 759 | } |
| 760 | |
| 761 | public static FallbackAction obtain() { |
| 762 | final FallbackAction target; |
| 763 | synchronized (sRecycleLock) { |
| 764 | if (sRecycleBin == null) { |
| 765 | target = new FallbackAction(); |
| 766 | } else { |
| 767 | target = sRecycleBin; |
| 768 | sRecycleBin = target.next; |
| 769 | sRecycledCount--; |
| 770 | target.next = null; |
| 771 | } |
| 772 | } |
| 773 | return target; |
| 774 | } |
| 775 | |
| 776 | public void recycle() { |
| 777 | synchronized (sRecycleLock) { |
| 778 | if (sRecycledCount < MAX_RECYCLED) { |
| 779 | next = sRecycleBin; |
| 780 | sRecycleBin = this; |
| 781 | sRecycledCount += 1; |
| 782 | } else { |
| 783 | next = null; |
| 784 | } |
| 785 | } |
| 786 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 787 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | } |