2011-06-21 14:38:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.keyboard.internal;
|
|
|
|
|
2011-06-22 13:45:03 +00:00
|
|
|
import android.content.res.Resources;
|
2011-06-21 14:38:42 +00:00
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2011-06-22 13:45:03 +00:00
|
|
|
import android.util.Log;
|
2011-06-21 14:38:42 +00:00
|
|
|
|
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
|
2011-12-18 16:11:09 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2011-06-21 14:38:42 +00:00
|
|
|
public class KeyboardIconsSet {
|
2011-06-22 13:45:03 +00:00
|
|
|
private static final String TAG = KeyboardIconsSet.class.getSimpleName();
|
|
|
|
|
2011-06-21 14:38:42 +00:00
|
|
|
public static final int ICON_UNDEFINED = 0;
|
2012-01-25 05:54:28 +00:00
|
|
|
public static final int ATTR_UNDEFINED = 0;
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2011-12-18 16:11:09 +00:00
|
|
|
private final Map<Integer, Drawable> mIcons = new HashMap<Integer, Drawable>();
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2011-12-18 16:11:09 +00:00
|
|
|
// The key value should be aligned with the enum value of Keyboard.icon*.
|
2012-02-02 12:24:09 +00:00
|
|
|
private static final Map<Integer, Integer> ID_TO_ATTR_MAP = new HashMap<Integer, Integer>();
|
|
|
|
private static final Map<String, Integer> NAME_TO_ATTR_MAP = new HashMap<String, Integer>();
|
|
|
|
private static final Map<Integer, String> ATTR_TO_NAME_MAP = new HashMap<Integer, String>();
|
2011-12-18 16:11:09 +00:00
|
|
|
private static final Collection<Integer> VALID_ATTRS;
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2011-12-18 16:11:09 +00:00
|
|
|
static {
|
2012-01-20 10:57:07 +00:00
|
|
|
addIconIdMap(1, "shiftKey", R.styleable.Keyboard_iconShiftKey);
|
|
|
|
addIconIdMap(2, "deleteKey", R.styleable.Keyboard_iconDeleteKey);
|
|
|
|
addIconIdMap(3, "settingsKey", R.styleable.Keyboard_iconSettingsKey);
|
|
|
|
addIconIdMap(4, "spaceKey", R.styleable.Keyboard_iconSpaceKey);
|
|
|
|
addIconIdMap(5, "returnKey", R.styleable.Keyboard_iconReturnKey);
|
|
|
|
addIconIdMap(6, "searchKey", R.styleable.Keyboard_iconSearchKey);
|
|
|
|
addIconIdMap(7, "tabKey", R.styleable.Keyboard_iconTabKey);
|
|
|
|
addIconIdMap(8, "shortcutKey", R.styleable.Keyboard_iconShortcutKey);
|
|
|
|
addIconIdMap(9, "shortcutForLabel", R.styleable.Keyboard_iconShortcutForLabel);
|
|
|
|
addIconIdMap(10, "spaceKeyForNumberLayout",
|
|
|
|
R.styleable.Keyboard_iconSpaceKeyForNumberLayout);
|
|
|
|
addIconIdMap(11, "shiftKeyShifted", R.styleable.Keyboard_iconShiftKeyShifted);
|
|
|
|
addIconIdMap(12, "disabledShortcurKey", R.styleable.Keyboard_iconDisabledShortcutKey);
|
|
|
|
addIconIdMap(13, "previewTabKey", R.styleable.Keyboard_iconPreviewTabKey);
|
2012-02-02 12:24:09 +00:00
|
|
|
VALID_ATTRS = ID_TO_ATTR_MAP.values();
|
2011-12-18 16:11:09 +00:00
|
|
|
}
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2012-01-20 10:57:07 +00:00
|
|
|
private static void addIconIdMap(int iconId, String name, Integer attrId) {
|
2012-02-02 12:24:09 +00:00
|
|
|
ID_TO_ATTR_MAP.put(iconId, attrId);
|
|
|
|
NAME_TO_ATTR_MAP.put(name, attrId);
|
|
|
|
ATTR_TO_NAME_MAP.put(attrId, name);
|
2011-06-21 14:38:42 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 23:37:13 +00:00
|
|
|
public void loadIcons(final TypedArray keyboardAttrs) {
|
2011-12-18 16:11:09 +00:00
|
|
|
for (final Integer attrId : VALID_ATTRS) {
|
|
|
|
try {
|
|
|
|
final Drawable icon = keyboardAttrs.getDrawable(attrId);
|
|
|
|
if (icon == null) continue;
|
|
|
|
setDefaultBounds(icon);
|
|
|
|
mIcons.put(attrId, icon);
|
|
|
|
} catch (Resources.NotFoundException e) {
|
|
|
|
Log.w(TAG, "Drawable resource for icon #"
|
|
|
|
+ keyboardAttrs.getResources().getResourceEntryName(attrId)
|
|
|
|
+ " not found");
|
2011-06-21 14:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-20 10:57:07 +00:00
|
|
|
public static int getIconAttrId(final Integer iconId) {
|
2011-12-18 16:11:09 +00:00
|
|
|
if (iconId == ICON_UNDEFINED) {
|
2012-01-20 10:57:07 +00:00
|
|
|
return ATTR_UNDEFINED;
|
2011-12-18 16:11:09 +00:00
|
|
|
}
|
2012-02-02 12:24:09 +00:00
|
|
|
final Integer attrId = ID_TO_ATTR_MAP.get(iconId);
|
2011-12-18 16:11:09 +00:00
|
|
|
if (attrId == null) {
|
2011-06-21 14:38:42 +00:00
|
|
|
throw new IllegalArgumentException("icon id is out of range: " + iconId);
|
2011-12-18 16:11:09 +00:00
|
|
|
}
|
2012-01-20 10:57:07 +00:00
|
|
|
return attrId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getIconAttrId(final String iconName) {
|
2012-02-02 12:24:09 +00:00
|
|
|
final Integer attrId = NAME_TO_ATTR_MAP.get(iconName);
|
2012-01-20 10:57:07 +00:00
|
|
|
if (attrId == null) {
|
|
|
|
throw new IllegalArgumentException("unknown icon name: " + iconName);
|
|
|
|
}
|
|
|
|
return attrId;
|
2011-12-18 16:11:09 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 12:24:09 +00:00
|
|
|
public static String getIconName(final int attrId) {
|
|
|
|
if (attrId == ATTR_UNDEFINED) {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
if (ATTR_TO_NAME_MAP.containsKey(attrId)) {
|
|
|
|
return ATTR_TO_NAME_MAP.get(attrId);
|
|
|
|
}
|
|
|
|
return String.format("unknown<0x%08x>", attrId);
|
|
|
|
}
|
|
|
|
|
2011-12-18 16:11:09 +00:00
|
|
|
public Drawable getIconByAttrId(final Integer attrId) {
|
2012-01-20 10:57:07 +00:00
|
|
|
if (attrId == ATTR_UNDEFINED) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-12-18 16:11:09 +00:00
|
|
|
if (!VALID_ATTRS.contains(attrId)) {
|
|
|
|
throw new IllegalArgumentException("unknown icon attribute id: " + attrId);
|
|
|
|
}
|
|
|
|
return mIcons.get(attrId);
|
2011-06-21 14:38:42 +00:00
|
|
|
}
|
2011-08-01 23:37:13 +00:00
|
|
|
|
|
|
|
private static Drawable setDefaultBounds(final Drawable icon) {
|
|
|
|
if (icon != null) {
|
|
|
|
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
|
|
|
|
}
|
|
|
|
return icon;
|
|
|
|
}
|
2011-06-21 14:38:42 +00:00
|
|
|
}
|