KeyboardIconsSet.java revision b19a6b9fc55910bd241bee3b312169a818cb721d
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    // The value should be aligned with the enum value of Key.keyIcon.
33    public static final int ICON_UNDEFINED = 0;
34    private static final int NUM_ICONS = 13;
35
36    private final Drawable[] mIcons = new Drawable[NUM_ICONS + 1];
37
38    private static final Map<Integer, Integer> ATTR_ID_TO_ICON_ID = new HashMap<Integer, Integer>();
39    private static final Map<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    }
61
62    private static void addIconIdMap(int iconId, String name, int attrId) {
63        if (attrId != ATTR_UNDEFINED) {
64            ATTR_ID_TO_ICON_ID.put(attrId,  iconId);
65        }
66        NAME_TO_ICON_ID.put(name, iconId);
67        ICON_NAMES[iconId] = name;
68    }
69
70    public void loadIcons(final TypedArray keyboardAttrs) {
71        for (final Integer attrId : ATTR_ID_TO_ICON_ID.keySet()) {
72            try {
73                final Drawable icon = keyboardAttrs.getDrawable(attrId);
74                setDefaultBounds(icon);
75                final Integer iconId = ATTR_ID_TO_ICON_ID.get(attrId);
76                mIcons[iconId] = icon;
77            } catch (Resources.NotFoundException e) {
78                Log.w(TAG, "Drawable resource for icon #"
79                        + keyboardAttrs.getResources().getResourceEntryName(attrId)
80                        + " not found");
81            }
82        }
83    }
84
85    private static boolean isValidIconId(final int iconId) {
86        return iconId >= 0 && iconId < ICON_NAMES.length;
87    }
88
89    public static String getIconName(final int iconId) {
90        return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">";
91    }
92
93    public static int getIconId(final String name) {
94        final Integer iconId = NAME_TO_ICON_ID.get(name);
95        if (iconId != null) {
96            return iconId;
97        }
98        throw new RuntimeException("unknown icon name: " + name);
99    }
100
101    public Drawable getIconDrawable(final int iconId) {
102        if (isValidIconId(iconId)) {
103            return mIcons[iconId];
104        }
105        throw new RuntimeException("unknown icon id: " + getIconName(iconId));
106    }
107
108    private static void setDefaultBounds(final Drawable icon)  {
109        if (icon != null) {
110            icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
111        }
112    }
113}
114