1 /*
2  * Copyright (C) 2014 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 java.util.HashMap;
20 import java.util.Locale;
21 
22 /**
23  * !!!!! DO NOT EDIT THIS FILE !!!!!
24  *
25  * This file is generated by tools/make-keyboard-text. The base template file is
26  *   tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/
27  *   KeyboardTextsTable.tmpl
28  *
29  * This file must be updated when any text resources in keyboard layout files have been changed.
30  * These text resources are referred as "!text/<resource_name>" in keyboard XML definitions,
31  * and should be defined in
32  *   tools/make-keyboard-text/res/values-<locale>/donottranslate-more-keys.xml
33  *
34  * To update this file, please run the following commands.
35  *   $ cd $ANDROID_BUILD_TOP
36  *   $ mmm packages/inputmethods/LatinIME/tools/make-keyboard-text
37  *   $ make-keyboard-text -java packages/inputmethods/LatinIME/java
38  *
39  * The updated source file will be generated to the following path (this file).
40  *   packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/
41  *   KeyboardTextsTable.java
42  */
43 public final class KeyboardTextsTable {
44     // Name to index map.
45     private static final HashMap<String, Integer> sNameToIndexesMap = new HashMap<>();
46     // Locale to texts table map.
47     private static final HashMap<String, String[]> sLocaleToTextsTableMap = new HashMap<>();
48     // TODO: Remove this variable after debugging.
49     // Texts table to locale maps.
50     private static final HashMap<String[], String> sTextsTableToLocaleMap = new HashMap<>();
51 
52     public static String getText(final String name, final String[] textsTable) {
53         final Integer indexObj = sNameToIndexesMap.get(name);
54         if (indexObj == null) {
55             throw new RuntimeException("Unknown text name=" + name + " locale="
56                     + sTextsTableToLocaleMap.get(textsTable));
57         }
58         final int index = indexObj;
59         final String text = (index < textsTable.length) ? textsTable[index] : null;
60         if (text != null) {
61             return text;
62         }
63         // Validity check.
64         if (index >= 0 && index < TEXTS_DEFAULT.length) {
65             return TEXTS_DEFAULT[index];
66         }
67         // Throw exception for debugging purpose.
68         throw new RuntimeException("Illegal index=" + index + " for name=" + name
69                 + " locale=" + sTextsTableToLocaleMap.get(textsTable));
70     }
71 
72     public static String[] getTextsTable(final Locale locale) {
73         final String localeKey = locale.toString();
74         if (sLocaleToTextsTableMap.containsKey(localeKey)) {
75             return sLocaleToTextsTableMap.get(localeKey);
76         }
77         final String languageKey = locale.getLanguage();
78         if (sLocaleToTextsTableMap.containsKey(languageKey)) {
79             return sLocaleToTextsTableMap.get(languageKey);
80         }
81         return TEXTS_DEFAULT;
82     }
83 
84     private static final String[] NAMES = {
85     //  /* index:histogram */ "name",
86         /* @NAMES@ */
87     };
88 
89     private static final String EMPTY = "";
90 
91     /* Default texts */
92     private static final String[] TEXTS_DEFAULT = {
93         /* @DEFAULT_TEXTS@ */
94     };
95 
96     /* @TEXTS@ */
97     private static final Object[] LOCALES_AND_TEXTS = {
98     // "locale", TEXT_ARRAY,  /* numberOfNonNullText/lengthOf_TEXT_ARRAY localeName */
99         /* @LOCALES_AND_TEXTS@ */
100     };
101 
102     static {
103         for (int index = 0; index < NAMES.length; index++) {
104             sNameToIndexesMap.put(NAMES[index], index);
105         }
106 
107         for (int i = 0; i < LOCALES_AND_TEXTS.length; i += 2) {
108             final String locale = (String)LOCALES_AND_TEXTS[i];
109             final String[] textsTable = (String[])LOCALES_AND_TEXTS[i + 1];
110             sLocaleToTextsTableMap.put(locale, textsTable);
111             sTextsTableToLocaleMap.put(textsTable, locale);
112         }
113     }
114 }
115