Home | History | Annotate | Download | only in internal
      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 android.content.Context;
     20 import android.content.res.Resources;
     21 
     22 import com.android.inputmethod.latin.CollectionUtils;
     23 import com.android.inputmethod.latin.R;
     24 
     25 import java.util.HashMap;
     26 
     27 /**
     28  * !!!!! DO NOT EDIT THIS FILE !!!!!
     29  *
     30  * This file is generated by tools/maketext. The base template file is
     31  *   tools/maketext/res/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.tmpl
     32  *
     33  * This file must be updated when any text resources in keyboard layout files have been changed.
     34  * These text resources are referred as "!text/<resource_name>" in keyboard XML definitions,
     35  * and should be defined in
     36  *   tools/maketext/res/values-<locale>/donottranslate-more-keys.xml
     37  *
     38  * To update this file, please run the following commands.
     39  *   $ cd $ANDROID_BUILD_TOP
     40  *   $ mmm packages/inputmethods/LatinIME/tools/maketext
     41  *   $ maketext -java packages/inputmethods/LatinIME/java/src
     42  *
     43  * The updated source file will be generated to the following path (this file).
     44  *   packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/
     45  *   KeyboardTextsSet.java
     46  */
     47 public final class KeyboardTextsSet {
     48     // Language to texts map.
     49     private static final HashMap<String, String[]> sLocaleToTextsMap = CollectionUtils.newHashMap();
     50     private static final HashMap<String, Integer> sNameToIdsMap = CollectionUtils.newHashMap();
     51 
     52     private String[] mTexts;
     53     // Resource name to text map.
     54     private HashMap<String, String> mResourceNameToTextsMap = CollectionUtils.newHashMap();
     55 
     56     public void setLanguage(final String language) {
     57         mTexts = sLocaleToTextsMap.get(language);
     58         if (mTexts == null) {
     59             mTexts = LANGUAGE_DEFAULT;
     60         }
     61     }
     62 
     63     public void loadStringResources(Context context) {
     64         loadStringResourcesInternal(context, RESOURCE_NAMES, R.string.english_ime_name);
     65     }
     66 
     67     /* package for test */
     68     void loadStringResourcesInternal(Context context, final String[] resourceNames,
     69             int referenceId) {
     70         final Resources res = context.getResources();
     71         final String packageName = res.getResourcePackageName(referenceId);
     72         for (final String resName : resourceNames) {
     73             final int resId = res.getIdentifier(resName, "string", packageName);
     74             mResourceNameToTextsMap.put(resName, res.getString(resId));
     75         }
     76     }
     77 
     78     public String getText(final String name) {
     79         String text = mResourceNameToTextsMap.get(name);
     80         if (text != null) {
     81             return text;
     82         }
     83         final Integer id = sNameToIdsMap.get(name);
     84         if (id == null) throw new RuntimeException("Unknown label: " + name);
     85         text = (id < mTexts.length) ? mTexts[id] : null;
     86         return (text == null) ? LANGUAGE_DEFAULT[id] : text;
     87     }
     88 
     89     private static final String[] RESOURCE_NAMES = {
     90         // These texts' name should be aligned with the @string/<name> in values/strings.xml.
     91         // Labels for action.
     92         "label_go_key",
     93         // "label_search_key",
     94         "label_send_key",
     95         "label_next_key",
     96         "label_done_key",
     97         "label_previous_key",
     98         // Other labels.
     99         "label_to_alpha_key",
    100         "label_to_symbol_key",
    101         "label_to_symbol_with_microphone_key",
    102         "label_pause_key",
    103         "label_wait_key",
    104     };
    105 
    106     private static final String[] NAMES = {
    107         /* @NAMES@ */
    108     };
    109 
    110     private static final String EMPTY = "";
    111 
    112     /* Default texts */
    113     private static final String[] LANGUAGE_DEFAULT = {
    114         /* @DEFAULT_TEXTS@ */
    115     };
    116 
    117     /* @TEXTS@ */
    118     private static final Object[] LANGUAGES_AND_TEXTS = {
    119         /* @LANGUAGES_AND_TEXTS@ */
    120     };
    121 
    122     static {
    123         int id = 0;
    124         for (final String name : NAMES) {
    125             sNameToIdsMap.put(name, id++);
    126         }
    127 
    128         for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) {
    129             final String language = (String)LANGUAGES_AND_TEXTS[i];
    130             final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1];
    131             sLocaleToTextsMap.put(language, texts);
    132         }
    133     }
    134 }
    135