KeyboardIconsSet.java revision 761a6812312c48d04bcb91e5c4448364c4925731
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under 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;
23import android.util.SparseIntArray;
24
25import com.android.inputmethod.latin.R;
26import com.android.inputmethod.latin.utils.CollectionUtils;
27
28import java.util.HashMap;
29
30public final class KeyboardIconsSet {
31    private static final String TAG = KeyboardIconsSet.class.getSimpleName();
32
33    public static final String PREFIX_ICON = "!icon/";
34    public static final int ICON_UNDEFINED = 0;
35    private static final int ATTR_UNDEFINED = 0;
36
37    private static final String NAME_UNDEFINED = "undefined";
38    public static final String NAME_SHIFT_KEY = "shift_key";
39    public static final String NAME_SHIFT_KEY_SHIFTED = "shift_key_shifted";
40    public static final String NAME_DELETE_KEY = "delete_key";
41    public static final String NAME_SETTINGS_KEY = "settings_key";
42    public static final String NAME_SPACE_KEY = "space_key";
43    public static final String NAME_SPACE_KEY_FOR_NUMBER_LAYOUT = "space_key_for_number_layout";
44    public static final String NAME_ENTER_KEY = "enter_key";
45    public static final String NAME_GO_KEY = "go_key";
46    public static final String NAME_SEARCH_KEY = "search_key";
47    public static final String NAME_SEND_KEY = "send_key";
48    public static final String NAME_NEXT_KEY = "next_key";
49    public static final String NAME_DONE_KEY = "done_key";
50    public static final String NAME_PREVIOUS_KEY = "previous_key";
51    public static final String NAME_TAB_KEY = "tab_key";
52    public static final String NANE_TAB_KEY_PREVIEW = "tab_key_preview";
53    public static final String NAME_SHORTCUT_KEY = "shortcut_key";
54    public static final String NAME_SHORTCUT_KEY_DISABLED = "shortcut_key_disabled";
55    public static final String NAME_LANGUAGE_SWITCH_KEY = "language_switch_key";
56    public static final String NAME_ZWNJ_KEY = "zwnj_key";
57    public static final String NAME_ZWJ_KEY = "zwj_key";
58    public static final String NAME_EMOJI_KEY = "emoji_key";
59
60    private static final SparseIntArray ATTR_ID_TO_ICON_ID = new SparseIntArray();
61
62    // Icon name to icon id map.
63    private static final HashMap<String, Integer> sNameToIdsMap = CollectionUtils.newHashMap();
64
65    private static final Object[] NAMES_AND_ATTR_IDS = {
66        NAME_UNDEFINED,                   ATTR_UNDEFINED,
67        NAME_SHIFT_KEY,                   R.styleable.Keyboard_iconShiftKey,
68        NAME_DELETE_KEY,                  R.styleable.Keyboard_iconDeleteKey,
69        NAME_SETTINGS_KEY,                R.styleable.Keyboard_iconSettingsKey,
70        NAME_SPACE_KEY,                   R.styleable.Keyboard_iconSpaceKey,
71        NAME_ENTER_KEY,                   R.styleable.Keyboard_iconEnterKey,
72        NAME_GO_KEY,                      R.styleable.Keyboard_iconGoKey,
73        NAME_SEARCH_KEY,                  R.styleable.Keyboard_iconSearchKey,
74        NAME_SEND_KEY,                    R.styleable.Keyboard_iconSendKey,
75        NAME_NEXT_KEY,                    R.styleable.Keyboard_iconNextKey,
76        NAME_DONE_KEY,                    R.styleable.Keyboard_iconDoneKey,
77        NAME_PREVIOUS_KEY,                R.styleable.Keyboard_iconPreviousKey,
78        NAME_TAB_KEY,                     R.styleable.Keyboard_iconTabKey,
79        NAME_SHORTCUT_KEY,                R.styleable.Keyboard_iconShortcutKey,
80        NAME_SPACE_KEY_FOR_NUMBER_LAYOUT, R.styleable.Keyboard_iconSpaceKeyForNumberLayout,
81        NAME_SHIFT_KEY_SHIFTED,           R.styleable.Keyboard_iconShiftKeyShifted,
82        NAME_SHORTCUT_KEY_DISABLED,       R.styleable.Keyboard_iconShortcutKeyDisabled,
83        NANE_TAB_KEY_PREVIEW,             R.styleable.Keyboard_iconTabKeyPreview,
84        NAME_LANGUAGE_SWITCH_KEY,         R.styleable.Keyboard_iconLanguageSwitchKey,
85        NAME_ZWNJ_KEY,                    R.styleable.Keyboard_iconZwnjKey,
86        NAME_ZWJ_KEY,                     R.styleable.Keyboard_iconZwjKey,
87        NAME_EMOJI_KEY,                   R.styleable.Keyboard_iconEmojiKey,
88    };
89
90    private static int NUM_ICONS = NAMES_AND_ATTR_IDS.length / 2;
91    private static final String[] ICON_NAMES = new String[NUM_ICONS];
92    private final Drawable[] mIcons = new Drawable[NUM_ICONS];
93    private final int[] mIconResourceIds = new int[NUM_ICONS];
94
95    static {
96        int iconId = ICON_UNDEFINED;
97        for (int i = 0; i < NAMES_AND_ATTR_IDS.length; i += 2) {
98            final String name = (String)NAMES_AND_ATTR_IDS[i];
99            final Integer attrId = (Integer)NAMES_AND_ATTR_IDS[i + 1];
100            if (attrId != ATTR_UNDEFINED) {
101                ATTR_ID_TO_ICON_ID.put(attrId, iconId);
102            }
103            sNameToIdsMap.put(name, iconId);
104            ICON_NAMES[iconId] = name;
105            iconId++;
106        }
107    }
108
109    public void loadIcons(final TypedArray keyboardAttrs) {
110        final int size = ATTR_ID_TO_ICON_ID.size();
111        for (int index = 0; index < size; index++) {
112            final int attrId = ATTR_ID_TO_ICON_ID.keyAt(index);
113            try {
114                final Drawable icon = keyboardAttrs.getDrawable(attrId);
115                setDefaultBounds(icon);
116                final Integer iconId = ATTR_ID_TO_ICON_ID.get(attrId);
117                mIcons[iconId] = icon;
118                mIconResourceIds[iconId] = keyboardAttrs.getResourceId(attrId, 0);
119            } catch (Resources.NotFoundException e) {
120                Log.w(TAG, "Drawable resource for icon #"
121                        + keyboardAttrs.getResources().getResourceEntryName(attrId)
122                        + " not found");
123            }
124        }
125    }
126
127    private static boolean isValidIconId(final int iconId) {
128        return iconId >= 0 && iconId < ICON_NAMES.length;
129    }
130
131    public static String getIconName(final int iconId) {
132        return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">";
133    }
134
135    public static int getIconId(final String name) {
136        Integer iconId = sNameToIdsMap.get(name);
137        if (iconId != null) {
138            return iconId;
139        }
140        throw new RuntimeException("unknown icon name: " + name);
141    }
142
143    public int getIconResourceId(final String name) {
144        final int iconId = getIconId(name);
145        if (isValidIconId(iconId)) {
146            return mIconResourceIds[iconId];
147        }
148        throw new RuntimeException("unknown icon name: " + name);
149    }
150
151    public Drawable getIconDrawable(final int iconId) {
152        if (isValidIconId(iconId)) {
153            return mIcons[iconId];
154        }
155        throw new RuntimeException("unknown icon id: " + getIconName(iconId));
156    }
157
158    private static void setDefaultBounds(final Drawable icon)  {
159        if (icon != null) {
160            icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
161        }
162    }
163}
164