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