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");
      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 android.content.Context;
     20 import android.content.res.Resources;
     21 
     22 import com.android.inputmethod.annotations.UsedForTesting;
     23 import com.android.inputmethod.latin.CollectionUtils;
     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(final Context context) {
     64         final int referenceId = context.getApplicationInfo().labelRes;
     65         loadStringResourcesInternal(context, RESOURCE_NAMES, referenceId);
     66     }
     67 
     68     @UsedForTesting
     69     void loadStringResourcesInternal(final Context context, final String[] resourceNames,
     70             final int referenceId) {
     71         final Resources res = context.getResources();
     72         final String packageName = res.getResourcePackageName(referenceId);
     73         for (final String resName : resourceNames) {
     74             final int resId = res.getIdentifier(resName, "string", packageName);
     75             mResourceNameToTextsMap.put(resName, res.getString(resId));
     76         }
     77     }
     78 
     79     public String getText(final String name) {
     80         String text = mResourceNameToTextsMap.get(name);
     81         if (text != null) {
     82             return text;
     83         }
     84         final Integer id = sNameToIdsMap.get(name);
     85         if (id == null) throw new RuntimeException("Unknown label: " + name);
     86         text = (id < mTexts.length) ? mTexts[id] : null;
     87         return (text == null) ? LANGUAGE_DEFAULT[id] : text;
     88     }
     89 
     90     private static final String[] RESOURCE_NAMES = {
     91         // These texts' name should be aligned with the @string/<name> in values/strings.xml.
     92         // Labels for action.
     93         "label_go_key",
     94         // "label_search_key",
     95         "label_send_key",
     96         "label_next_key",
     97         "label_done_key",
     98         "label_previous_key",
     99         // Other labels.
    100         "label_pause_key",
    101         "label_wait_key",
    102     };
    103 
    104     private static final String[] NAMES = {
    105         /* @NAMES@ */
    106     };
    107 
    108     private static final String EMPTY = "";
    109 
    110     /* Default texts */
    111     private static final String[] LANGUAGE_DEFAULT = {
    112         /* @DEFAULT_TEXTS@ */
    113     };
    114 
    115     /* @TEXTS@ */
    116     private static final Object[] LANGUAGES_AND_TEXTS = {
    117         /* @LANGUAGES_AND_TEXTS@ */
    118     };
    119 
    120     static {
    121         int id = 0;
    122         for (final String name : NAMES) {
    123             sNameToIdsMap.put(name, id++);
    124         }
    125 
    126         for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) {
    127             final String language = (String)LANGUAGES_AND_TEXTS[i];
    128             final String[] texts = (String[])LANGUAGES_AND_TEXTS[i + 1];
    129             sLocaleToTextsMap.put(language, texts);
    130         }
    131     }
    132 }
    133