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 46 private static final int ICON_LAST = 11; 47 48 private final Drawable mIcons[] = new Drawable[ICON_LAST + 1]; 49 50 private static final int getIconId(final int attrIndex) { 51 switch (attrIndex) { 52 case R.styleable.Keyboard_iconShiftKey: 53 return ICON_SHIFT_KEY; 54 case R.styleable.Keyboard_iconDeleteKey: 55 return ICON_DELETE_KEY; 56 case R.styleable.Keyboard_iconSettingsKey: 57 return ICON_SETTINGS_KEY; 58 case R.styleable.Keyboard_iconSpaceKey: 59 return ICON_SPACE_KEY; 60 case R.styleable.Keyboard_iconReturnKey: 61 return ICON_RETURN_KEY; 62 case R.styleable.Keyboard_iconSearchKey: 63 return ICON_SEARCH_KEY; 64 case R.styleable.Keyboard_iconTabKey: 65 return ICON_TAB_KEY; 66 case R.styleable.Keyboard_iconShortcutKey: 67 return ICON_SHORTCUT_KEY; 68 case R.styleable.Keyboard_iconShortcutForLabel: 69 return ICON_SHORTCUT_FOR_LABEL; 70 case R.styleable.Keyboard_iconShiftedShiftKey: 71 return ICON_SHIFTED_SHIFT_KEY; 72 case R.styleable.Keyboard_iconPreviewTabKey: 73 return ICON_PREVIEW_TAB_KEY; 74 default: 75 return ICON_UNDEFINED; 76 } 77 } 78 79 public void loadIcons(final TypedArray keyboardAttrs) { 80 final int count = keyboardAttrs.getIndexCount(); 81 for (int i = 0; i < count; i++) { 82 final int attrIndex = keyboardAttrs.getIndex(i); 83 final int iconId = getIconId(attrIndex); 84 if (iconId != ICON_UNDEFINED) { 85 try { 86 mIcons[iconId] = setDefaultBounds(keyboardAttrs.getDrawable(attrIndex)); 87 } catch (Resources.NotFoundException e) { 88 Log.w(TAG, "Drawable resource for icon #" + iconId + " not found"); 89 } 90 } 91 } 92 } 93 94 public Drawable getIcon(final int iconId) { 95 if (iconId == ICON_UNDEFINED) 96 return null; 97 if (iconId < 0 || iconId >= mIcons.length) 98 throw new IllegalArgumentException("icon id is out of range: " + iconId); 99 return mIcons[iconId]; 100 } 101 102 private static Drawable setDefaultBounds(final Drawable icon) { 103 if (icon != null) { 104 icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); 105 } 106 return icon; 107 } 108 } 109