KeyboardCodesSet.java revision 3bbd50c36b1e9b9e3b26ab510853021515886498
1/* 2 * Copyright (C) 2012 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 com.android.inputmethod.keyboard.internal; 18 19import com.android.inputmethod.latin.Constants; 20import com.android.inputmethod.latin.utils.CollectionUtils; 21import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; 22 23import java.util.HashMap; 24import java.util.Locale; 25 26public final class KeyboardCodesSet { 27 public static final String PREFIX_CODE = "!code/"; 28 29 private static final HashMap<String, Integer> sNameToIdMap = CollectionUtils.newHashMap(); 30 31 private int[] mCodes = DEFAULT; 32 33 public void setLocale(final Locale locale) { 34 mCodes = SubtypeLocaleUtils.isRtlLanguage(locale) ? RTL : DEFAULT; 35 } 36 37 public int getCode(final String name) { 38 Integer id = sNameToIdMap.get(name); 39 if (id == null) throw new RuntimeException("Unknown key code: " + name); 40 return mCodes[id]; 41 } 42 43 private static final String[] ID_TO_NAME = { 44 "key_tab", 45 "key_enter", 46 "key_space", 47 "key_shift", 48 "key_capslock", 49 "key_switch_alpha_symbol", 50 "key_output_text", 51 "key_delete", 52 "key_settings", 53 "key_shortcut", 54 "key_action_next", 55 "key_action_previous", 56 "key_shift_enter", 57 "key_language_switch", 58 "key_emoji", 59 "key_alpha_from_emoji", 60 "key_unspecified", 61 "key_left_parenthesis", 62 "key_right_parenthesis", 63 "key_less_than", 64 "key_greater_than", 65 "key_left_square_bracket", 66 "key_right_square_bracket", 67 "key_left_curly_bracket", 68 "key_right_curly_bracket", 69 }; 70 71 private static final int CODE_LEFT_PARENTHESIS = '('; 72 private static final int CODE_RIGHT_PARENTHESIS = ')'; 73 private static final int CODE_LESS_THAN_SIGN = '<'; 74 private static final int CODE_GREATER_THAN_SIGN = '>'; 75 private static final int CODE_LEFT_SQUARE_BRACKET = '['; 76 private static final int CODE_RIGHT_SQUARE_BRACKET = ']'; 77 private static final int CODE_LEFT_CURLY_BRACKET = '{'; 78 private static final int CODE_RIGHT_CURLY_BRACKET = '}'; 79 80 // This array should be aligned with the array RTL below. 81 private static final int[] DEFAULT = { 82 Constants.CODE_TAB, 83 Constants.CODE_ENTER, 84 Constants.CODE_SPACE, 85 Constants.CODE_SHIFT, 86 Constants.CODE_CAPSLOCK, 87 Constants.CODE_SWITCH_ALPHA_SYMBOL, 88 Constants.CODE_OUTPUT_TEXT, 89 Constants.CODE_DELETE, 90 Constants.CODE_SETTINGS, 91 Constants.CODE_SHORTCUT, 92 Constants.CODE_ACTION_NEXT, 93 Constants.CODE_ACTION_PREVIOUS, 94 Constants.CODE_SHIFT_ENTER, 95 Constants.CODE_LANGUAGE_SWITCH, 96 Constants.CODE_EMOJI, 97 Constants.CODE_ALPHA_FROM_EMOJI, 98 Constants.CODE_UNSPECIFIED, 99 CODE_LEFT_PARENTHESIS, 100 CODE_RIGHT_PARENTHESIS, 101 CODE_LESS_THAN_SIGN, 102 CODE_GREATER_THAN_SIGN, 103 CODE_LEFT_SQUARE_BRACKET, 104 CODE_RIGHT_SQUARE_BRACKET, 105 CODE_LEFT_CURLY_BRACKET, 106 CODE_RIGHT_CURLY_BRACKET, 107 }; 108 109 private static final int[] RTL = { 110 DEFAULT[0], 111 DEFAULT[1], 112 DEFAULT[2], 113 DEFAULT[3], 114 DEFAULT[4], 115 DEFAULT[5], 116 DEFAULT[6], 117 DEFAULT[7], 118 DEFAULT[8], 119 DEFAULT[9], 120 DEFAULT[10], 121 DEFAULT[11], 122 DEFAULT[12], 123 DEFAULT[13], 124 DEFAULT[14], 125 DEFAULT[15], 126 DEFAULT[16], 127 CODE_RIGHT_PARENTHESIS, 128 CODE_LEFT_PARENTHESIS, 129 CODE_GREATER_THAN_SIGN, 130 CODE_LESS_THAN_SIGN, 131 CODE_RIGHT_SQUARE_BRACKET, 132 CODE_LEFT_SQUARE_BRACKET, 133 CODE_RIGHT_CURLY_BRACKET, 134 CODE_LEFT_CURLY_BRACKET, 135 }; 136 137 static { 138 if (DEFAULT.length != RTL.length || DEFAULT.length != ID_TO_NAME.length) { 139 throw new RuntimeException("Internal inconsistency"); 140 } 141 for (int i = 0; i < ID_TO_NAME.length; i++) { 142 sNameToIdMap.put(ID_TO_NAME[i], i); 143 } 144 } 145} 146