KeyboardIconsSet.java revision b4fbbe57f574ce6e6a5827156f875fe7d3eb5089
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    // The value should be aligned with the enum value of Key.keyIcon.
32    public static final int ICON_UNDEFINED = 0;
33    private static final int NUM_ICONS = 14;
34
35    private final Drawable[] mIcons = new Drawable[NUM_ICONS + 1];
36
37    private static final HashMap<Integer, Integer> ATTR_ID_TO_ICON_ID
38            = new HashMap<Integer, Integer>();
39    private static final HashMap<String, Integer> NAME_TO_ICON_ID = new HashMap<String, Integer>();
40    private static final String[] ICON_NAMES = new String[NUM_ICONS + 1];
41
42    private static final int ATTR_UNDEFINED = 0;
43    static {
44        // The key value should be aligned with the enum value of Key.keyIcon.
45        addIconIdMap(0, "undefined", ATTR_UNDEFINED);
46        addIconIdMap(1, "shiftKey", R.styleable.Keyboard_iconShiftKey);
47        addIconIdMap(2, "deleteKey", R.styleable.Keyboard_iconDeleteKey);
48        addIconIdMap(3, "settingsKey", R.styleable.Keyboard_iconSettingsKey);
49        addIconIdMap(4, "spaceKey", R.styleable.Keyboard_iconSpaceKey);
50        addIconIdMap(5, "returnKey", R.styleable.Keyboard_iconReturnKey);
51        addIconIdMap(6, "searchKey", R.styleable.Keyboard_iconSearchKey);
52        addIconIdMap(7, "tabKey", R.styleable.Keyboard_iconTabKey);
53        addIconIdMap(8, "shortcutKey", R.styleable.Keyboard_iconShortcutKey);
54        addIconIdMap(9, "shortcutForLabel", R.styleable.Keyboard_iconShortcutForLabel);
55        addIconIdMap(10, "spaceKeyForNumberLayout",
56                R.styleable.Keyboard_iconSpaceKeyForNumberLayout);
57        addIconIdMap(11, "shiftKeyShifted", R.styleable.Keyboard_iconShiftKeyShifted);
58        addIconIdMap(12, "disabledShortcurKey", R.styleable.Keyboard_iconDisabledShortcutKey);
59        addIconIdMap(13, "previewTabKey", R.styleable.Keyboard_iconPreviewTabKey);
60        addIconIdMap(14, "languageSwitchKey", R.styleable.Keyboard_iconLanguageSwitchKey);
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