• 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 
21 import java.util.HashMap;
22 
23 public class KeyboardCodesSet {
24     private static final HashMap<String, int[]> sLanguageToCodesMap =
25             new HashMap<String, int[]>();
26     private static final HashMap<String, Integer> sNameToIdMap = new HashMap<String, Integer>();
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_unspecified",
56         "key_left_parenthesis",
57         "key_right_parenthesis",
58         "key_less_than",
59         "key_greater_than",
60         "key_left_square_bracket",
61         "key_right_square_bracket",
62         "key_left_curly_bracket",
63         "key_right_curly_bracket",
64     };
65 
66     private static final int CODE_LEFT_PARENTHESIS = '(';
67     private static final int CODE_RIGHT_PARENTHESIS = ')';
68     private static final int CODE_LESS_THAN_SIGN = '<';
69     private static final int CODE_GREATER_THAN_SIGN = '>';
70     private static final int CODE_LEFT_SQUARE_BRACKET = '[';
71     private static final int CODE_RIGHT_SQUARE_BRACKET = ']';
72     private static final int CODE_LEFT_CURLY_BRACKET = '{';
73     private static final int CODE_RIGHT_CURLY_BRACKET = '}';
74 
75     private static final int[] DEFAULT = {
76         Keyboard.CODE_TAB,
77         Keyboard.CODE_ENTER,
78         Keyboard.CODE_SPACE,
79         Keyboard.CODE_SHIFT,
80         Keyboard.CODE_SWITCH_ALPHA_SYMBOL,
81         Keyboard.CODE_OUTPUT_TEXT,
82         Keyboard.CODE_DELETE,
83         Keyboard.CODE_SETTINGS,
84         Keyboard.CODE_SHORTCUT,
85         Keyboard.CODE_ACTION_ENTER,
86         Keyboard.CODE_ACTION_NEXT,
87         Keyboard.CODE_ACTION_PREVIOUS,
88         Keyboard.CODE_LANGUAGE_SWITCH,
89         Keyboard.CODE_UNSPECIFIED,
90         CODE_LEFT_PARENTHESIS,
91         CODE_RIGHT_PARENTHESIS,
92         CODE_LESS_THAN_SIGN,
93         CODE_GREATER_THAN_SIGN,
94         CODE_LEFT_SQUARE_BRACKET,
95         CODE_RIGHT_SQUARE_BRACKET,
96         CODE_LEFT_CURLY_BRACKET,
97         CODE_RIGHT_CURLY_BRACKET,
98     };
99 
100     private static final int[] RTL = {
101         DEFAULT[0],
102         DEFAULT[1],
103         DEFAULT[2],
104         DEFAULT[3],
105         DEFAULT[4],
106         DEFAULT[5],
107         DEFAULT[6],
108         DEFAULT[7],
109         DEFAULT[8],
110         DEFAULT[9],
111         DEFAULT[10],
112         DEFAULT[11],
113         DEFAULT[12],
114         DEFAULT[13],
115         CODE_RIGHT_PARENTHESIS,
116         CODE_LEFT_PARENTHESIS,
117         CODE_GREATER_THAN_SIGN,
118         CODE_LESS_THAN_SIGN,
119         CODE_RIGHT_SQUARE_BRACKET,
120         CODE_LEFT_SQUARE_BRACKET,
121         CODE_RIGHT_CURLY_BRACKET,
122         CODE_LEFT_CURLY_BRACKET,
123     };
124 
125     private static final String LANGUAGE_DEFAULT = "DEFAULT";
126     private static final String LANGUAGE_ARABIC = "ar";
127     private static final String LANGUAGE_PERSIAN = "fa";
128     private static final String LANGUAGE_HEBREW = "iw";
129 
130     private static final Object[] LANGUAGE_AND_CODES = {
131         LANGUAGE_DEFAULT, DEFAULT,
132         LANGUAGE_ARABIC, RTL,
133         LANGUAGE_PERSIAN, RTL,
134         LANGUAGE_HEBREW, RTL,
135     };
136 
137     static {
138         for (int i = 0; i < ID_TO_NAME.length; i++) {
sNameToIdMap.put(ID_TO_NAME[i], i)139             sNameToIdMap.put(ID_TO_NAME[i], i);
140         }
141 
142         for (int i = 0; i < LANGUAGE_AND_CODES.length; i += 2) {
143             final String language = (String)LANGUAGE_AND_CODES[i];
144             final int[] codes = (int[])LANGUAGE_AND_CODES[i + 1];
sLanguageToCodesMap.put(language, codes)145             sLanguageToCodesMap.put(language, codes);
146         }
147     }
148 }
149