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