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.CollectionUtils;
     26 import com.android.inputmethod.latin.R;
     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     };
     60 
     61     private static int NUM_ICONS = NAMES_AND_ATTR_IDS.length / 2;
     62     private static final String[] ICON_NAMES = new String[NUM_ICONS];
     63     private final Drawable[] mIcons = new Drawable[NUM_ICONS];
     64 
     65     static {
     66         int iconId = ICON_UNDEFINED;
     67         for (int i = 0; i < NAMES_AND_ATTR_IDS.length; i += 2) {
     68             final String name = (String)NAMES_AND_ATTR_IDS[i];
     69             final Integer attrId = (Integer)NAMES_AND_ATTR_IDS[i + 1];
     70             if (attrId != ATTR_UNDEFINED) {
     71                 ATTR_ID_TO_ICON_ID.put(attrId,  iconId);
     72             }
     73             sNameToIdsMap.put(name, iconId);
     74             ICON_NAMES[iconId] = name;
     75             iconId++;
     76         }
     77     }
     78 
     79     public void loadIcons(final TypedArray keyboardAttrs) {
     80         final int size = ATTR_ID_TO_ICON_ID.size();
     81         for (int index = 0; index < size; index++) {
     82             final int attrId = ATTR_ID_TO_ICON_ID.keyAt(index);
     83             try {
     84                 final Drawable icon = keyboardAttrs.getDrawable(attrId);
     85                 setDefaultBounds(icon);
     86                 final Integer iconId = ATTR_ID_TO_ICON_ID.get(attrId);
     87                 mIcons[iconId] = icon;
     88             } catch (Resources.NotFoundException e) {
     89                 Log.w(TAG, "Drawable resource for icon #"
     90                         + keyboardAttrs.getResources().getResourceEntryName(attrId)
     91                         + " not found");
     92             }
     93         }
     94     }
     95 
     96     private static boolean isValidIconId(final int iconId) {
     97         return iconId >= 0 && iconId < ICON_NAMES.length;
     98     }
     99 
    100     public static String getIconName(final int iconId) {
    101         return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">";
    102     }
    103 
    104     static int getIconId(final String name) {
    105         Integer iconId = sNameToIdsMap.get(name);
    106         if (iconId != null) {
    107             return iconId;
    108         }
    109         throw new RuntimeException("unknown icon name: " + name);
    110     }
    111 
    112     public Drawable getIconDrawable(final int iconId) {
    113         if (isValidIconId(iconId)) {
    114             return mIcons[iconId];
    115         }
    116         throw new RuntimeException("unknown icon id: " + getIconName(iconId));
    117     }
    118 
    119     private static void setDefaultBounds(final Drawable icon)  {
    120         if (icon != null) {
    121             icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
    122         }
    123     }
    124 }
    125