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