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