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