• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.inputmethod.keyboard.internal;
18 
19 import com.android.inputmethod.keyboard.Keyboard;
20 import com.android.inputmethod.latin.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 
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_enter",
52         "key_action_next",
53         "key_action_previous",
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     private static final int[] DEFAULT = {
77         Keyboard.CODE_TAB,
78         Keyboard.CODE_ENTER,
79         Keyboard.CODE_SPACE,
80         Keyboard.CODE_SHIFT,
81         Keyboard.CODE_SWITCH_ALPHA_SYMBOL,
82         Keyboard.CODE_OUTPUT_TEXT,
83         Keyboard.CODE_DELETE,
84         Keyboard.CODE_SETTINGS,
85         Keyboard.CODE_SHORTCUT,
86         Keyboard.CODE_ACTION_ENTER,
87         Keyboard.CODE_ACTION_NEXT,
88         Keyboard.CODE_ACTION_PREVIOUS,
89         Keyboard.CODE_LANGUAGE_SWITCH,
90         Keyboard.CODE_RESEARCH,
91         Keyboard.CODE_UNSPECIFIED,
92         CODE_LEFT_PARENTHESIS,
93         CODE_RIGHT_PARENTHESIS,
94         CODE_LESS_THAN_SIGN,
95         CODE_GREATER_THAN_SIGN,
96         CODE_LEFT_SQUARE_BRACKET,
97         CODE_RIGHT_SQUARE_BRACKET,
98         CODE_LEFT_CURLY_BRACKET,
99         CODE_RIGHT_CURLY_BRACKET,
100     };
101 
102     private static final int[] RTL = {
103         DEFAULT[0],
104         DEFAULT[1],
105         DEFAULT[2],
106         DEFAULT[3],
107         DEFAULT[4],
108         DEFAULT[5],
109         DEFAULT[6],
110         DEFAULT[7],
111         DEFAULT[8],
112         DEFAULT[9],
113         DEFAULT[10],
114         DEFAULT[11],
115         DEFAULT[12],
116         DEFAULT[13],
117         DEFAULT[14],
118         CODE_RIGHT_PARENTHESIS,
119         CODE_LEFT_PARENTHESIS,
120         CODE_GREATER_THAN_SIGN,
121         CODE_LESS_THAN_SIGN,
122         CODE_RIGHT_SQUARE_BRACKET,
123         CODE_LEFT_SQUARE_BRACKET,
124         CODE_RIGHT_CURLY_BRACKET,
125         CODE_LEFT_CURLY_BRACKET,
126     };
127 
128     private static final String LANGUAGE_DEFAULT = "DEFAULT";
129     private static final String LANGUAGE_ARABIC = "ar";
130     private static final String LANGUAGE_PERSIAN = "fa";
131     private static final String LANGUAGE_HEBREW = "iw";
132 
133     private static final Object[] LANGUAGE_AND_CODES = {
134         LANGUAGE_DEFAULT, DEFAULT,
135         LANGUAGE_ARABIC, RTL,
136         LANGUAGE_PERSIAN, RTL,
137         LANGUAGE_HEBREW, RTL,
138     };
139 
140     static {
141         for (int i = 0; i < ID_TO_NAME.length; i++) {
sNameToIdMap.put(ID_TO_NAME[i], i)142             sNameToIdMap.put(ID_TO_NAME[i], i);
143         }
144 
145         for (int i = 0; i < LANGUAGE_AND_CODES.length; i += 2) {
146             final String language = (String)LANGUAGE_AND_CODES[i];
147             final int[] codes = (int[])LANGUAGE_AND_CODES[i + 1];
sLanguageToCodesMap.put(language, codes)148             sLanguageToCodesMap.put(language, codes);
149         }
150     }
151 }
152