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;
|
2012-06-29 07:32:45 +00:00
|
|
|
import android.util.SparseIntArray;
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2012-08-21 07:34:55 +00:00
|
|
|
import com.android.inputmethod.latin.CollectionUtils;
|
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.HashMap;
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class KeyboardIconsSet {
|
2011-06-22 13:45:03 +00:00
|
|
|
private static final String TAG = KeyboardIconsSet.class.getSimpleName();
|
|
|
|
|
2012-02-27 12:35:35 +00:00
|
|
|
public static final int ICON_UNDEFINED = 0;
|
2012-04-19 21:24:51 +00:00
|
|
|
private static final int ATTR_UNDEFINED = 0;
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2012-06-29 07:32:45 +00:00
|
|
|
private static final SparseIntArray ATTR_ID_TO_ICON_ID = new SparseIntArray();
|
2012-04-19 21:24:51 +00:00
|
|
|
|
2012-05-28 03:27:18 +00:00
|
|
|
// Icon name to icon id map.
|
2012-08-21 07:34:55 +00:00
|
|
|
private static final HashMap<String, Integer> sNameToIdsMap = CollectionUtils.newHashMap();
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2012-04-19 21:24:51 +00:00
|
|
|
private static final Object[] NAMES_AND_ATTR_IDS = {
|
|
|
|
"undefined", ATTR_UNDEFINED,
|
|
|
|
"shift_key", R.styleable.Keyboard_iconShiftKey,
|
|
|
|
"delete_key", R.styleable.Keyboard_iconDeleteKey,
|
|
|
|
"settings_key", R.styleable.Keyboard_iconSettingsKey,
|
|
|
|
"space_key", R.styleable.Keyboard_iconSpaceKey,
|
|
|
|
"enter_key", R.styleable.Keyboard_iconEnterKey,
|
|
|
|
"search_key", R.styleable.Keyboard_iconSearchKey,
|
|
|
|
"tab_key", R.styleable.Keyboard_iconTabKey,
|
|
|
|
"shortcut_key", R.styleable.Keyboard_iconShortcutKey,
|
|
|
|
"shortcut_for_label", R.styleable.Keyboard_iconShortcutForLabel,
|
|
|
|
"space_key_for_number_layout", R.styleable.Keyboard_iconSpaceKeyForNumberLayout,
|
|
|
|
"shift_key_shifted", R.styleable.Keyboard_iconShiftKeyShifted,
|
|
|
|
"shortcut_key_disabled", R.styleable.Keyboard_iconShortcutKeyDisabled,
|
|
|
|
"tab_key_preview", R.styleable.Keyboard_iconTabKeyPreview,
|
|
|
|
"language_switch_key", R.styleable.Keyboard_iconLanguageSwitchKey,
|
|
|
|
"zwnj_key", R.styleable.Keyboard_iconZwnjKey,
|
|
|
|
"zwj_key", R.styleable.Keyboard_iconZwjKey,
|
|
|
|
};
|
|
|
|
|
|
|
|
private static int NUM_ICONS = NAMES_AND_ATTR_IDS.length / 2;
|
|
|
|
private static final String[] ICON_NAMES = new String[NUM_ICONS];
|
|
|
|
private final Drawable[] mIcons = new Drawable[NUM_ICONS];
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2012-04-19 21:24:51 +00:00
|
|
|
static {
|
|
|
|
int iconId = ICON_UNDEFINED;
|
|
|
|
for (int i = 0; i < NAMES_AND_ATTR_IDS.length; i += 2) {
|
|
|
|
final String name = (String)NAMES_AND_ATTR_IDS[i];
|
|
|
|
final Integer attrId = (Integer)NAMES_AND_ATTR_IDS[i + 1];
|
|
|
|
if (attrId != ATTR_UNDEFINED) {
|
|
|
|
ATTR_ID_TO_ICON_ID.put(attrId, iconId);
|
|
|
|
}
|
2012-05-28 03:27:18 +00:00
|
|
|
sNameToIdsMap.put(name, iconId);
|
2012-04-19 21:24:51 +00:00
|
|
|
ICON_NAMES[iconId] = name;
|
|
|
|
iconId++;
|
2012-02-02 15:18:47 +00:00
|
|
|
}
|
2011-06-21 14:38:42 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 23:37:13 +00:00
|
|
|
public void loadIcons(final TypedArray keyboardAttrs) {
|
2012-06-29 07:32:45 +00:00
|
|
|
final int size = ATTR_ID_TO_ICON_ID.size();
|
|
|
|
for (int index = 0; index < size; index++) {
|
|
|
|
final int attrId = ATTR_ID_TO_ICON_ID.keyAt(index);
|
2011-12-18 16:11:09 +00:00
|
|
|
try {
|
|
|
|
final Drawable icon = keyboardAttrs.getDrawable(attrId);
|
|
|
|
setDefaultBounds(icon);
|
2012-02-02 15:18:47 +00:00
|
|
|
final Integer iconId = ATTR_ID_TO_ICON_ID.get(attrId);
|
|
|
|
mIcons[iconId] = icon;
|
2011-12-18 16:11:09 +00:00
|
|
|
} 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-02-02 15:18:47 +00:00
|
|
|
private static boolean isValidIconId(final int iconId) {
|
|
|
|
return iconId >= 0 && iconId < ICON_NAMES.length;
|
2012-01-20 10:57:07 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 15:18:47 +00:00
|
|
|
public static String getIconName(final int iconId) {
|
|
|
|
return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">";
|
2011-12-18 16:11:09 +00:00
|
|
|
}
|
|
|
|
|
2012-04-19 21:24:51 +00:00
|
|
|
static int getIconId(final String name) {
|
2012-05-28 03:27:18 +00:00
|
|
|
Integer iconId = sNameToIdsMap.get(name);
|
2012-02-02 15:18:47 +00:00
|
|
|
if (iconId != null) {
|
|
|
|
return iconId;
|
2012-02-02 12:24:09 +00:00
|
|
|
}
|
2012-02-02 15:18:47 +00:00
|
|
|
throw new RuntimeException("unknown icon name: " + name);
|
2012-02-02 12:24:09 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 15:18:47 +00:00
|
|
|
public Drawable getIconDrawable(final int iconId) {
|
|
|
|
if (isValidIconId(iconId)) {
|
|
|
|
return mIcons[iconId];
|
2011-12-18 16:11:09 +00:00
|
|
|
}
|
2012-02-02 15:18:47 +00:00
|
|
|
throw new RuntimeException("unknown icon id: " + getIconName(iconId));
|
2011-06-21 14:38:42 +00:00
|
|
|
}
|
2011-08-01 23:37:13 +00:00
|
|
|
|
2012-02-02 15:18:47 +00:00
|
|
|
private static void setDefaultBounds(final Drawable icon) {
|
2011-08-01 23:37:13 +00:00
|
|
|
if (icon != null) {
|
|
|
|
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
|
|
|
|
}
|
|
|
|
}
|
2011-06-21 14:38:42 +00:00
|
|
|
}
|