KeyboardIconsSet.java revision 42fcb2de641c4cd5d57f34889c8752401e35dcc8
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
35    private final Map<Integer, Drawable> mIcons = new HashMap<Integer, Drawable>();
36
37    // The key value should be aligned with the enum value of Keyboard.icon*.
38    private static final Map<Integer, Integer> ICONS_TO_ATTRS_MAP = new HashMap<Integer, Integer>();
39    private static final Collection<Integer> VALID_ATTRS;
40
41    static {
42        addIconIdMap(1, R.styleable.Keyboard_iconShiftKey);
43        addIconIdMap(2, R.styleable.Keyboard_iconDeleteKey);
44        // This is also represented as "@icon/3" in keyboard layout XML.
45        addIconIdMap(3, R.styleable.Keyboard_iconSettingsKey);
46        addIconIdMap(4, R.styleable.Keyboard_iconSpaceKey);
47        addIconIdMap(5, R.styleable.Keyboard_iconReturnKey);
48        addIconIdMap(6, R.styleable.Keyboard_iconSearchKey);
49        // This is also represented as "@icon/7" in keyboard layout XML.
50        addIconIdMap(7, R.styleable.Keyboard_iconTabKey);
51        addIconIdMap(8, R.styleable.Keyboard_iconShortcutKey);
52        addIconIdMap(9, R.styleable.Keyboard_iconShortcutForLabel);
53        addIconIdMap(10, R.styleable.Keyboard_iconSpaceKeyForNumberLayout);
54        addIconIdMap(11, R.styleable.Keyboard_iconShiftKeyShifted);
55        addIconIdMap(12, R.styleable.Keyboard_iconDisabledShortcutKey);
56        addIconIdMap(13, R.styleable.Keyboard_iconPreviewTabKey);
57        VALID_ATTRS = ICONS_TO_ATTRS_MAP.values();
58    }
59
60    private static void addIconIdMap(int iconId, int attrId) {
61        ICONS_TO_ATTRS_MAP.put(iconId, attrId);
62    }
63
64    public void loadIcons(final TypedArray keyboardAttrs) {
65        for (final Integer attrId : VALID_ATTRS) {
66            try {
67                final Drawable icon = keyboardAttrs.getDrawable(attrId);
68                if (icon == null) continue;
69                setDefaultBounds(icon);
70                mIcons.put(attrId, icon);
71            } catch (Resources.NotFoundException e) {
72                Log.w(TAG, "Drawable resource for icon #"
73                        + keyboardAttrs.getResources().getResourceEntryName(attrId)
74                        + " not found");
75            }
76        }
77    }
78
79    public Drawable getIconByIconId(final Integer iconId) {
80        if (iconId == ICON_UNDEFINED) {
81            return null;
82        }
83        final Integer attrId = ICONS_TO_ATTRS_MAP.get(iconId);
84        if (attrId == null) {
85            throw new IllegalArgumentException("icon id is out of range: " + iconId);
86        }
87        return getIconByAttrId(attrId);
88    }
89
90    public Drawable getIconByAttrId(final Integer attrId) {
91        if (!VALID_ATTRS.contains(attrId)) {
92            throw new IllegalArgumentException("unknown icon attribute id: " + attrId);
93        }
94        return mIcons.get(attrId);
95    }
96
97    private static Drawable setDefaultBounds(final Drawable icon)  {
98        if (icon != null) {
99            icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
100        }
101        return icon;
102    }
103}
104