KeyboardIconsSet.java revision cf41aff251ecc94b729307ede05208a104fcd8b0
1/* 2 * Copyright (C) 2011 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 17package com.android.inputmethod.keyboard.internal; 18 19import android.content.res.Resources; 20import android.content.res.TypedArray; 21import android.graphics.drawable.Drawable; 22import android.util.Log; 23 24import com.android.inputmethod.latin.R; 25 26import java.util.HashMap; 27 28public class KeyboardIconsSet { 29 private static final String TAG = KeyboardIconsSet.class.getSimpleName(); 30 31 public static final int ICON_UNDEFINED = 0; 32 private static final int ATTR_UNDEFINED = 0; 33 34 private static final HashMap<Integer, Integer> ATTR_ID_TO_ICON_ID 35 = new HashMap<Integer, Integer>(); 36 37 // Lower case icon name to icon id map. 38 private static final HashMap<String, Integer> sLowerCaseNameToIdsMap = 39 new HashMap<String, Integer>(); 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 sLowerCaseNameToIdsMap.put(name, iconId); 74 ICON_NAMES[iconId] = name; 75 iconId++; 76 } 77 } 78 79 public void loadIcons(final TypedArray keyboardAttrs) { 80 for (final Integer attrId : ATTR_ID_TO_ICON_ID.keySet()) { 81 try { 82 final Drawable icon = keyboardAttrs.getDrawable(attrId); 83 setDefaultBounds(icon); 84 final Integer iconId = ATTR_ID_TO_ICON_ID.get(attrId); 85 mIcons[iconId] = icon; 86 } catch (Resources.NotFoundException e) { 87 Log.w(TAG, "Drawable resource for icon #" 88 + keyboardAttrs.getResources().getResourceEntryName(attrId) 89 + " not found"); 90 } 91 } 92 } 93 94 private static boolean isValidIconId(final int iconId) { 95 return iconId >= 0 && iconId < ICON_NAMES.length; 96 } 97 98 public static String getIconName(final int iconId) { 99 return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">"; 100 } 101 102 static int getIconId(final String name) { 103 Integer iconId = sLowerCaseNameToIdsMap.get(name); 104 if (iconId == null) { 105 iconId = sLowerCaseNameToIdsMap.get(name.toLowerCase()); 106 } 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