1e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka/* 2e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * Copyright (C) 2014 The Android Open Source Project 3e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * 4e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License"); 5e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * you may not use this file except in compliance with the License. 6e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * You may obtain a copy of the License at 7e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * 8e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * http://www.apache.org/licenses/LICENSE-2.0 9e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * 10e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * Unless required by applicable law or agreed to in writing, software 11e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS, 12e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * See the License for the specific language governing permissions and 14e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * limitations under the License. 15e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka */ 16e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 17e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaokapackage com.android.inputmethod.keyboard.internal; 18e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 19e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaokaimport java.util.HashMap; 206bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaokaimport java.util.Locale; 21e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 22e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka/** 23e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * !!!!! DO NOT EDIT THIS FILE !!!!! 24e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * 25e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * This file is generated by tools/make-keyboard-text. The base template file is 265eb6ea9f27f2486c6688551bc0a117ec39bfe5a0Tadashi G. Takaoka * tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/ 275eb6ea9f27f2486c6688551bc0a117ec39bfe5a0Tadashi G. Takaoka * KeyboardTextsTable.tmpl 28e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * 29e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * This file must be updated when any text resources in keyboard layout files have been changed. 30e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * These text resources are referred as "!text/<resource_name>" in keyboard XML definitions, 31e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * and should be defined in 32e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * tools/make-keyboard-text/res/values-<locale>/donottranslate-more-keys.xml 33e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * 34e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * To update this file, please run the following commands. 35e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * $ cd $ANDROID_BUILD_TOP 36e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * $ mmm packages/inputmethods/LatinIME/tools/make-keyboard-text 375eb6ea9f27f2486c6688551bc0a117ec39bfe5a0Tadashi G. Takaoka * $ make-keyboard-text -java packages/inputmethods/LatinIME/java 38e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * 39e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * The updated source file will be generated to the following path (this file). 40e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/ 41e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka * KeyboardTextsTable.java 42e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka */ 43e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaokapublic final class KeyboardTextsTable { 44e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka // Name to index map. 45a91561aa58db1c43092c1caecc051a11fa5391c7Tadashi G. Takaoka private static final HashMap<String, Integer> sNameToIndexesMap = new HashMap<>(); 466bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka // Locale to texts table map. 47a91561aa58db1c43092c1caecc051a11fa5391c7Tadashi G. Takaoka private static final HashMap<String, String[]> sLocaleToTextsTableMap = new HashMap<>(); 4853a237323a3f5ba6e5ece341874812ac91bbbfc0Tadashi G. Takaoka // TODO: Remove this variable after debugging. 496bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka // Texts table to locale maps. 50a91561aa58db1c43092c1caecc051a11fa5391c7Tadashi G. Takaoka private static final HashMap<String[], String> sTextsTableToLocaleMap = new HashMap<>(); 51e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 52e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka public static String getText(final String name, final String[] textsTable) { 53e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka final Integer indexObj = sNameToIndexesMap.get(name); 54e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka if (indexObj == null) { 556bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka throw new RuntimeException("Unknown text name=" + name + " locale=" 566bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka + sTextsTableToLocaleMap.get(textsTable)); 57e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka } 58e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka final int index = indexObj; 59e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka final String text = (index < textsTable.length) ? textsTable[index] : null; 6053a237323a3f5ba6e5ece341874812ac91bbbfc0Tadashi G. Takaoka if (text != null) { 6153a237323a3f5ba6e5ece341874812ac91bbbfc0Tadashi G. Takaoka return text; 6253a237323a3f5ba6e5ece341874812ac91bbbfc0Tadashi G. Takaoka } 6353a237323a3f5ba6e5ece341874812ac91bbbfc0Tadashi G. Takaoka // Sanity check. 646bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka if (index >= 0 && index < TEXTS_DEFAULT.length) { 656bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka return TEXTS_DEFAULT[index]; 6653a237323a3f5ba6e5ece341874812ac91bbbfc0Tadashi G. Takaoka } 6753a237323a3f5ba6e5ece341874812ac91bbbfc0Tadashi G. Takaoka // Throw exception for debugging purpose. 6853a237323a3f5ba6e5ece341874812ac91bbbfc0Tadashi G. Takaoka throw new RuntimeException("Illegal index=" + index + " for name=" + name 696bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka + " locale=" + sTextsTableToLocaleMap.get(textsTable)); 70e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka } 71e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 726bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka public static String[] getTextsTable(final Locale locale) { 736bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka final String localeKey = locale.toString(); 746bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka if (sLocaleToTextsTableMap.containsKey(localeKey)) { 756bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka return sLocaleToTextsTableMap.get(localeKey); 766bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka } 776bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka final String languageKey = locale.getLanguage(); 786bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka if (sLocaleToTextsTableMap.containsKey(languageKey)) { 796bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka return sLocaleToTextsTableMap.get(languageKey); 806bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka } 816bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka return TEXTS_DEFAULT; 82e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka } 83e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 84e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka private static final String[] NAMES = { 850fe4d00068fb9ea85bfab083aa595082dd24e59cTadashi G. Takaoka // /* index:histogram */ "name", 86e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka /* @NAMES@ */ 87e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka }; 88e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 89e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka private static final String EMPTY = ""; 90e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 91e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka /* Default texts */ 926bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka private static final String[] TEXTS_DEFAULT = { 93e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka /* @DEFAULT_TEXTS@ */ 94e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka }; 95e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 96e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka /* @TEXTS@ */ 976bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka private static final Object[] LOCALES_AND_TEXTS = { 98ed509695a42455874aa9c047ed4343f636ef527bTadashi G. Takaoka // "locale", TEXT_ARRAY, /* numberOfNonNullText/lengthOf_TEXT_ARRAY localeName */ 996bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka /* @LOCALES_AND_TEXTS@ */ 100e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka }; 101e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 102e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka static { 103e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka for (int index = 0; index < NAMES.length; index++) { 104e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka sNameToIndexesMap.put(NAMES[index], index); 105e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka } 106e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka 1076bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka for (int i = 0; i < LOCALES_AND_TEXTS.length; i += 2) { 1086bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka final String locale = (String)LOCALES_AND_TEXTS[i]; 1096bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka final String[] textsTable = (String[])LOCALES_AND_TEXTS[i + 1]; 1106bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka sLocaleToTextsTableMap.put(locale, textsTable); 1116bb3556ff7b24a5a38d7cc4276017bda3a9a4bbaTadashi G. Takaoka sTextsTableToLocaleMap.put(textsTable, locale); 112e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka } 113e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka } 114e5320c1d1fb31c570841f656941758e5f064b896Tadashi G. Takaoka} 115