KeyboardIconsSet.java revision e7948a8692a102c89aca70a79ca62573d77efde3
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; 27import java.util.Map; 28 29public class KeyboardIconsSet { 30 private static final String TAG = KeyboardIconsSet.class.getSimpleName(); 31 32 public static final int ICON_UNDEFINED = 0; 33 // The value should be aligned with the enum value of Key.keyIcon. 34 public static final int ICON_SPACE = 4; 35 private static final int NUM_ICONS = 13; 36 37 private final Drawable[] mIcons = new Drawable[NUM_ICONS + 1]; 38 39 private static final Map<Integer, Integer> ATTR_ID_TO_ICON_ID = new HashMap<Integer, Integer>(); 40 private static final Map<String, Integer> NAME_TO_ICON_ID = new HashMap<String, Integer>(); 41 private static final String[] ICON_NAMES = new String[NUM_ICONS + 1]; 42 43 private static final int ATTR_UNDEFINED = 0; 44 static { 45 // The key value should be aligned with the enum value of Key.keyIcon. 46 addIconIdMap(0, "undefined", ATTR_UNDEFINED); 47 addIconIdMap(1, "shiftKey", R.styleable.Keyboard_iconShiftKey); 48 addIconIdMap(2, "deleteKey", R.styleable.Keyboard_iconDeleteKey); 49 addIconIdMap(3, "settingsKey", R.styleable.Keyboard_iconSettingsKey); 50 addIconIdMap(4, "spaceKey", R.styleable.Keyboard_iconSpaceKey); 51 addIconIdMap(5, "returnKey", R.styleable.Keyboard_iconReturnKey); 52 addIconIdMap(6, "searchKey", R.styleable.Keyboard_iconSearchKey); 53 addIconIdMap(7, "tabKey", R.styleable.Keyboard_iconTabKey); 54 addIconIdMap(8, "shortcutKey", R.styleable.Keyboard_iconShortcutKey); 55 addIconIdMap(9, "shortcutForLabel", R.styleable.Keyboard_iconShortcutForLabel); 56 addIconIdMap(10, "spaceKeyForNumberLayout", 57 R.styleable.Keyboard_iconSpaceKeyForNumberLayout); 58 addIconIdMap(11, "shiftKeyShifted", R.styleable.Keyboard_iconShiftKeyShifted); 59 addIconIdMap(12, "disabledShortcurKey", R.styleable.Keyboard_iconDisabledShortcutKey); 60 addIconIdMap(13, "previewTabKey", R.styleable.Keyboard_iconPreviewTabKey); 61 } 62 63 private static void addIconIdMap(int iconId, String name, int attrId) { 64 if (attrId != ATTR_UNDEFINED) { 65 ATTR_ID_TO_ICON_ID.put(attrId, iconId); 66 } 67 NAME_TO_ICON_ID.put(name, iconId); 68 ICON_NAMES[iconId] = name; 69 } 70 71 public void loadIcons(final TypedArray keyboardAttrs) { 72 for (final Integer attrId : ATTR_ID_TO_ICON_ID.keySet()) { 73 try { 74 final Drawable icon = keyboardAttrs.getDrawable(attrId); 75 setDefaultBounds(icon); 76 final Integer iconId = ATTR_ID_TO_ICON_ID.get(attrId); 77 mIcons[iconId] = icon; 78 } catch (Resources.NotFoundException e) { 79 Log.w(TAG, "Drawable resource for icon #" 80 + keyboardAttrs.getResources().getResourceEntryName(attrId) 81 + " not found"); 82 } 83 } 84 } 85 86 private static boolean isValidIconId(final int iconId) { 87 return iconId >= 0 && iconId < ICON_NAMES.length; 88 } 89 90 public static String getIconName(final int iconId) { 91 return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">"; 92 } 93 94 public static int getIconId(final String name) { 95 final Integer iconId = NAME_TO_ICON_ID.get(name); 96 if (iconId != null) { 97 return iconId; 98 } 99 throw new RuntimeException("unknown icon name: " + name); 100 } 101 102 public Drawable getIconDrawable(final int iconId) { 103 if (isValidIconId(iconId)) { 104 return mIcons[iconId]; 105 } 106 throw new RuntimeException("unknown icon id: " + getIconName(iconId)); 107 } 108 109 private static void setDefaultBounds(final Drawable icon) { 110 if (icon != null) { 111 icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); 112 } 113 } 114} 115