KeyboardIconsSet.java revision ef5dfc480c7a3e3e34a20b7aacc731942e7a0578
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.keyboard.Keyboard;
25import com.android.inputmethod.latin.R;
26
27public class KeyboardIconsSet {
28    private static final String TAG = KeyboardIconsSet.class.getSimpleName();
29
30    public static final int ICON_UNDEFINED = 0;
31
32    // This should be aligned with Keyboard.keyIcon enum.
33    private static final int ICON_SHIFT_KEY = 1;
34    private static final int ICON_TO_SYMBOL_KEY = 2;
35    private static final int ICON_TO_SYMBOL_KEY_WITH_SHORTCUT = 3;
36    private static final int ICON_DELETE_KEY = 4;
37    private static final int ICON_DELETE_RTL_KEY = 5;
38    private static final int ICON_SETTINGS_KEY = 6; // This is also represented as "@icon/6" in xml.
39    private static final int ICON_SHORTCUT_KEY = 7;
40    private static final int ICON_SPACE_KEY = 8;
41    private static final int ICON_RETURN_KEY = 9;
42    private static final int ICON_SEARCH_KEY = 10;
43    private static final int ICON_TAB_KEY = 11;
44    // This should be aligned with Keyboard.keyIconShifted enum.
45    private static final int ICON_SHIFTED_SHIFT_KEY = 12;
46    // This should be aligned with Keyboard.keyIconPreview enum.
47    private static final int ICON_PREVIEW_TAB_KEY = 13;
48    private static final int ICON_PREVIEW_SETTINGS_KEY = 14;
49    private static final int ICON_PREVIEW_SHORTCUT_KEY = 15;
50
51    private static final int ICON_LAST = 15;
52
53    private final Drawable mIcons[] = new Drawable[ICON_LAST + 1];
54
55    private static final int getIconId(int attrIndex) {
56        switch (attrIndex) {
57        case R.styleable.Keyboard_iconShiftKey:
58            return ICON_SHIFT_KEY;
59        case R.styleable.Keyboard_iconToSymbolKey:
60            return ICON_TO_SYMBOL_KEY;
61        case R.styleable.Keyboard_iconToSymbolKeyWithShortcut:
62            return ICON_TO_SYMBOL_KEY_WITH_SHORTCUT;
63        case R.styleable.Keyboard_iconDeleteKey:
64            return ICON_DELETE_KEY;
65        case R.styleable.Keyboard_iconDeleteRtlKey:
66            return ICON_DELETE_RTL_KEY;
67        case R.styleable.Keyboard_iconSettingsKey:
68            return ICON_SETTINGS_KEY;
69        case R.styleable.Keyboard_iconShortcutKey:
70            return ICON_SHORTCUT_KEY;
71        case R.styleable.Keyboard_iconSpaceKey:
72            return ICON_SPACE_KEY;
73        case R.styleable.Keyboard_iconReturnKey:
74            return ICON_RETURN_KEY;
75        case R.styleable.Keyboard_iconSearchKey:
76            return ICON_SEARCH_KEY;
77        case R.styleable.Keyboard_iconTabKey:
78            return ICON_TAB_KEY;
79        case R.styleable.Keyboard_iconShiftedShiftKey:
80            return ICON_SHIFTED_SHIFT_KEY;
81        case R.styleable.Keyboard_iconPreviewTabKey:
82            return ICON_PREVIEW_TAB_KEY;
83        case R.styleable.Keyboard_iconPreviewSettingsKey:
84            return ICON_PREVIEW_SETTINGS_KEY;
85        case R.styleable.Keyboard_iconPreviewShortcutKey:
86            return ICON_PREVIEW_SHORTCUT_KEY;
87        default:
88            return ICON_UNDEFINED;
89        }
90    }
91
92    public void loadIcons(TypedArray keyboardAttrs) {
93        final int count = keyboardAttrs.getIndexCount();
94        for (int i = 0; i < count; i++) {
95            final int attrIndex = keyboardAttrs.getIndex(i);
96            final int iconId = getIconId(attrIndex);
97            if (iconId != ICON_UNDEFINED) {
98                try {
99                    final Drawable icon = keyboardAttrs.getDrawable(attrIndex);
100                    Keyboard.setDefaultBounds(icon);
101                    mIcons[iconId] = icon;
102                } catch (Resources.NotFoundException e) {
103                    Log.w(TAG, "Drawable resource for icon #" + iconId + " not found");
104                }
105            }
106        }
107    }
108
109    public Drawable getIcon(int iconId) {
110        if (iconId == ICON_UNDEFINED)
111            return null;
112        if (iconId < 0 || iconId >= mIcons.length)
113            throw new IllegalArgumentException("icon id is out of range: " + iconId);
114        return mIcons[iconId];
115    }
116}
117