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 
17 package com.android.inputmethod.keyboard.internal;
18 
19 import android.content.res.Resources;
20 import android.content.res.TypedArray;
21 import android.graphics.drawable.Drawable;
22 import android.util.Log;
23 import android.util.SparseIntArray;
24 
25 import com.android.inputmethod.latin.R;
26 
27 import java.util.HashMap;
28 
29 public final class KeyboardIconsSet {
30     private static final String TAG = KeyboardIconsSet.class.getSimpleName();
31 
32     public static final String PREFIX_ICON = "!icon/";
33     public static final int ICON_UNDEFINED = 0;
34     private static final int ATTR_UNDEFINED = 0;
35 
36     private static final String NAME_UNDEFINED = "undefined";
37     public static final String NAME_SHIFT_KEY = "shift_key";
38     public static final String NAME_SHIFT_KEY_SHIFTED = "shift_key_shifted";
39     public static final String NAME_DELETE_KEY = "delete_key";
40     public static final String NAME_SETTINGS_KEY = "settings_key";
41     public static final String NAME_SPACE_KEY = "space_key";
42     public static final String NAME_SPACE_KEY_FOR_NUMBER_LAYOUT = "space_key_for_number_layout";
43     public static final String NAME_ENTER_KEY = "enter_key";
44     public static final String NAME_GO_KEY = "go_key";
45     public static final String NAME_SEARCH_KEY = "search_key";
46     public static final String NAME_SEND_KEY = "send_key";
47     public static final String NAME_NEXT_KEY = "next_key";
48     public static final String NAME_DONE_KEY = "done_key";
49     public static final String NAME_PREVIOUS_KEY = "previous_key";
50     public static final String NAME_TAB_KEY = "tab_key";
51     public static final String NAME_SHORTCUT_KEY = "shortcut_key";
52     public static final String NAME_SHORTCUT_KEY_DISABLED = "shortcut_key_disabled";
53     public static final String NAME_LANGUAGE_SWITCH_KEY = "language_switch_key";
54     public static final String NAME_ZWNJ_KEY = "zwnj_key";
55     public static final String NAME_ZWJ_KEY = "zwj_key";
56     public static final String NAME_EMOJI_ACTION_KEY = "emoji_action_key";
57     public static final String NAME_EMOJI_NORMAL_KEY = "emoji_normal_key";
58 
59     private static final SparseIntArray ATTR_ID_TO_ICON_ID = new SparseIntArray();
60 
61     // Icon name to icon id map.
62     private static final HashMap<String, Integer> sNameToIdsMap = new HashMap<>();
63 
64     private static final Object[] NAMES_AND_ATTR_IDS = {
65         NAME_UNDEFINED,                   ATTR_UNDEFINED,
66         NAME_SHIFT_KEY,                   R.styleable.Keyboard_iconShiftKey,
67         NAME_DELETE_KEY,                  R.styleable.Keyboard_iconDeleteKey,
68         NAME_SETTINGS_KEY,                R.styleable.Keyboard_iconSettingsKey,
69         NAME_SPACE_KEY,                   R.styleable.Keyboard_iconSpaceKey,
70         NAME_ENTER_KEY,                   R.styleable.Keyboard_iconEnterKey,
71         NAME_GO_KEY,                      R.styleable.Keyboard_iconGoKey,
72         NAME_SEARCH_KEY,                  R.styleable.Keyboard_iconSearchKey,
73         NAME_SEND_KEY,                    R.styleable.Keyboard_iconSendKey,
74         NAME_NEXT_KEY,                    R.styleable.Keyboard_iconNextKey,
75         NAME_DONE_KEY,                    R.styleable.Keyboard_iconDoneKey,
76         NAME_PREVIOUS_KEY,                R.styleable.Keyboard_iconPreviousKey,
77         NAME_TAB_KEY,                     R.styleable.Keyboard_iconTabKey,
78         NAME_SHORTCUT_KEY,                R.styleable.Keyboard_iconShortcutKey,
79         NAME_SPACE_KEY_FOR_NUMBER_LAYOUT, R.styleable.Keyboard_iconSpaceKeyForNumberLayout,
80         NAME_SHIFT_KEY_SHIFTED,           R.styleable.Keyboard_iconShiftKeyShifted,
81         NAME_SHORTCUT_KEY_DISABLED,       R.styleable.Keyboard_iconShortcutKeyDisabled,
82         NAME_LANGUAGE_SWITCH_KEY,         R.styleable.Keyboard_iconLanguageSwitchKey,
83         NAME_ZWNJ_KEY,                    R.styleable.Keyboard_iconZwnjKey,
84         NAME_ZWJ_KEY,                     R.styleable.Keyboard_iconZwjKey,
85         NAME_EMOJI_ACTION_KEY,            R.styleable.Keyboard_iconEmojiActionKey,
86         NAME_EMOJI_NORMAL_KEY,            R.styleable.Keyboard_iconEmojiNormalKey,
87     };
88 
89     private static int NUM_ICONS = NAMES_AND_ATTR_IDS.length / 2;
90     private static final String[] ICON_NAMES = new String[NUM_ICONS];
91     private final Drawable[] mIcons = new Drawable[NUM_ICONS];
92     private final int[] mIconResourceIds = new int[NUM_ICONS];
93 
94     static {
95         int iconId = ICON_UNDEFINED;
96         for (int i = 0; i < NAMES_AND_ATTR_IDS.length; i += 2) {
97             final String name = (String)NAMES_AND_ATTR_IDS[i];
98             final Integer attrId = (Integer)NAMES_AND_ATTR_IDS[i + 1];
99             if (attrId != ATTR_UNDEFINED) {
ATTR_ID_TO_ICON_ID.put(attrId, iconId)100                 ATTR_ID_TO_ICON_ID.put(attrId, iconId);
101             }
sNameToIdsMap.put(name, iconId)102             sNameToIdsMap.put(name, iconId);
103             ICON_NAMES[iconId] = name;
104             iconId++;
105         }
106     }
107 
loadIcons(final TypedArray keyboardAttrs)108     public void loadIcons(final TypedArray keyboardAttrs) {
109         final int size = ATTR_ID_TO_ICON_ID.size();
110         for (int index = 0; index < size; index++) {
111             final int attrId = ATTR_ID_TO_ICON_ID.keyAt(index);
112             try {
113                 final Drawable icon = keyboardAttrs.getDrawable(attrId);
114                 setDefaultBounds(icon);
115                 final Integer iconId = ATTR_ID_TO_ICON_ID.get(attrId);
116                 mIcons[iconId] = icon;
117                 mIconResourceIds[iconId] = keyboardAttrs.getResourceId(attrId, 0);
118             } catch (Resources.NotFoundException e) {
119                 Log.w(TAG, "Drawable resource for icon #"
120                         + keyboardAttrs.getResources().getResourceEntryName(attrId)
121                         + " not found");
122             }
123         }
124     }
125 
isValidIconId(final int iconId)126     private static boolean isValidIconId(final int iconId) {
127         return iconId >= 0 && iconId < ICON_NAMES.length;
128     }
129 
getIconName(final int iconId)130     public static String getIconName(final int iconId) {
131         return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">";
132     }
133 
getIconId(final String name)134     public static int getIconId(final String name) {
135         Integer iconId = sNameToIdsMap.get(name);
136         if (iconId != null) {
137             return iconId;
138         }
139         throw new RuntimeException("unknown icon name: " + name);
140     }
141 
getIconResourceId(final String name)142     public int getIconResourceId(final String name) {
143         final int iconId = getIconId(name);
144         if (isValidIconId(iconId)) {
145             return mIconResourceIds[iconId];
146         }
147         throw new RuntimeException("unknown icon name: " + name);
148     }
149 
getIconDrawable(final int iconId)150     public Drawable getIconDrawable(final int iconId) {
151         if (isValidIconId(iconId)) {
152             return mIcons[iconId];
153         }
154         throw new RuntimeException("unknown icon id: " + getIconName(iconId));
155     }
156 
setDefaultBounds(final Drawable icon)157     private static void setDefaultBounds(final Drawable icon)  {
158         if (icon != null) {
159             icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
160         }
161     }
162 }
163