Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2011 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.res.Resources;
     20 import android.content.res.TypedArray;
     21 import android.graphics.drawable.Drawable;
     22 import android.util.Log;
     23 import android.util.SparseIntArray;
     24 
     25 import com.android.inputmethod.latin.R;
     26 import com.android.inputmethod.latin.utils.CollectionUtils;
     27 
     28 import java.util.HashMap;
     29 
     30 public final class KeyboardIconsSet {
     31     private static final String TAG = KeyboardIconsSet.class.getSimpleName();
     32 
     33     public static final int ICON_UNDEFINED = 0;
     34     private static final int ATTR_UNDEFINED = 0;
     35 
     36     private static final SparseIntArray ATTR_ID_TO_ICON_ID = new SparseIntArray();
     37 
     38     // Icon name to icon id map.
     39     private static final HashMap<String, Integer> sNameToIdsMap = CollectionUtils.newHashMap();
     40 
     41     private static final Object[] NAMES_AND_ATTR_IDS = {
     42         "undefined",                    ATTR_UNDEFINED,
     43         "shift_key",                    R.styleable.Keyboard_iconShiftKey,
     44         "delete_key",                   R.styleable.Keyboard_iconDeleteKey,
     45         "settings_key",                 R.styleable.Keyboard_iconSettingsKey,
     46         "space_key",                    R.styleable.Keyboard_iconSpaceKey,
     47         "enter_key",                    R.styleable.Keyboard_iconEnterKey,
     48         "search_key",                   R.styleable.Keyboard_iconSearchKey,
     49         "tab_key",                      R.styleable.Keyboard_iconTabKey,
     50         "shortcut_key",                 R.styleable.Keyboard_iconShortcutKey,
     51         "shortcut_for_label",           R.styleable.Keyboard_iconShortcutForLabel,
     52         "space_key_for_number_layout",  R.styleable.Keyboard_iconSpaceKeyForNumberLayout,
     53         "shift_key_shifted",            R.styleable.Keyboard_iconShiftKeyShifted,
     54         "shortcut_key_disabled",        R.styleable.Keyboard_iconShortcutKeyDisabled,
     55         "tab_key_preview",              R.styleable.Keyboard_iconTabKeyPreview,
     56         "language_switch_key",          R.styleable.Keyboard_iconLanguageSwitchKey,
     57         "zwnj_key",                     R.styleable.Keyboard_iconZwnjKey,
     58         "zwj_key",                      R.styleable.Keyboard_iconZwjKey,
     59         "emoji_key",                    R.styleable.Keyboard_iconEmojiKey,
     60     };
     61 
     62     private static int NUM_ICONS = NAMES_AND_ATTR_IDS.length / 2;
     63     private static final String[] ICON_NAMES = new String[NUM_ICONS];
     64     private final Drawable[] mIcons = new Drawable[NUM_ICONS];
     65 
     66     static {
     67         int iconId = ICON_UNDEFINED;
     68         for (int i = 0; i < NAMES_AND_ATTR_IDS.length; i += 2) {
     69             final String name = (String)NAMES_AND_ATTR_IDS[i];
     70             final Integer attrId = (Integer)NAMES_AND_ATTR_IDS[i + 1];
     71             if (attrId != ATTR_UNDEFINED) {
     72                 ATTR_ID_TO_ICON_ID.put(attrId,  iconId);
     73             }
     74             sNameToIdsMap.put(name, iconId);
     75             ICON_NAMES[iconId] = name;
     76             iconId++;
     77         }
     78     }
     79 
     80     public void loadIcons(final TypedArray keyboardAttrs) {
     81         final int size = ATTR_ID_TO_ICON_ID.size();
     82         for (int index = 0; index < size; index++) {
     83             final int attrId = ATTR_ID_TO_ICON_ID.keyAt(index);
     84             try {
     85                 final Drawable icon = keyboardAttrs.getDrawable(attrId);
     86                 setDefaultBounds(icon);
     87                 final Integer iconId = ATTR_ID_TO_ICON_ID.get(attrId);
     88                 mIcons[iconId] = icon;
     89             } catch (Resources.NotFoundException e) {
     90                 Log.w(TAG, "Drawable resource for icon #"
     91                         + keyboardAttrs.getResources().getResourceEntryName(attrId)
     92                         + " not found");
     93             }
     94         }
     95     }
     96 
     97     private static boolean isValidIconId(final int iconId) {
     98         return iconId >= 0 && iconId < ICON_NAMES.length;
     99     }
    100 
    101     public static String getIconName(final int iconId) {
    102         return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">";
    103     }
    104 
    105     static int getIconId(final String name) {
    106         Integer iconId = sNameToIdsMap.get(name);
    107         if (iconId != null) {
    108             return iconId;
    109         }
    110         throw new RuntimeException("unknown icon name: " + name);
    111     }
    112 
    113     public Drawable getIconDrawable(final int iconId) {
    114         if (isValidIconId(iconId)) {
    115             return mIcons[iconId];
    116         }
    117         throw new RuntimeException("unknown icon id: " + getIconName(iconId));
    118     }
    119 
    120     private static void setDefaultBounds(final Drawable icon)  {
    121         if (icon != null) {
    122             icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
    123         }
    124     }
    125 }
    126