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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-12-17 23:00:07 +00:00
|
|
|
// TODO: Make all enums private
|
2011-06-21 14:38:42 +00:00
|
|
|
// This should be aligned with Keyboard.keyIcon enum.
|
2011-12-17 23:00:07 +00:00
|
|
|
public static final int ICON_SHIFT_KEY = 1;
|
2011-08-03 04:29:24 +00:00
|
|
|
private static final int ICON_DELETE_KEY = 2;
|
2011-12-17 23:00:07 +00:00
|
|
|
// This is also represented as "@icon/3" in keyboard layout XML.
|
|
|
|
private static final int ICON_SETTINGS_KEY = 3;
|
|
|
|
public static final int ICON_SPACE_KEY = 4;
|
2011-08-03 04:29:24 +00:00
|
|
|
private static final int ICON_RETURN_KEY = 5;
|
|
|
|
private static final int ICON_SEARCH_KEY = 6;
|
2011-12-17 23:00:07 +00:00
|
|
|
// This is also represented as "@icon/7" in keyboard layout XML.
|
|
|
|
private static final int ICON_TAB_KEY = 7;
|
|
|
|
public static final int ICON_SHORTCUT_KEY = 8;
|
2011-08-03 04:29:24 +00:00
|
|
|
private static final int ICON_SHORTCUT_FOR_LABEL = 9;
|
2011-12-17 23:00:07 +00:00
|
|
|
public static final int ICON_SHORTCUT_KEY_DISABLED = 10;
|
|
|
|
private static final int ICON_SPACE_KEY_FOR_NUMBER_LAYOUT = 11;
|
|
|
|
public static final int ICON_SHIFT_KEY_SHIFTED = 12;
|
2011-06-21 14:38:42 +00:00
|
|
|
// This should be aligned with Keyboard.keyIconPreview enum.
|
2011-12-17 23:00:07 +00:00
|
|
|
private static final int ICON_PREVIEW_TAB_KEY = 13;
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2011-12-17 23:00:07 +00:00
|
|
|
private static final int ICON_LAST = 13;
|
2011-06-21 14:38:42 +00:00
|
|
|
|
|
|
|
private final Drawable mIcons[] = new Drawable[ICON_LAST + 1];
|
|
|
|
|
2011-08-01 23:37:13 +00:00
|
|
|
private static final int getIconId(final int attrIndex) {
|
2011-06-21 14:38:42 +00:00
|
|
|
switch (attrIndex) {
|
|
|
|
case R.styleable.Keyboard_iconShiftKey:
|
|
|
|
return ICON_SHIFT_KEY;
|
|
|
|
case R.styleable.Keyboard_iconDeleteKey:
|
|
|
|
return ICON_DELETE_KEY;
|
|
|
|
case R.styleable.Keyboard_iconSettingsKey:
|
|
|
|
return ICON_SETTINGS_KEY;
|
|
|
|
case R.styleable.Keyboard_iconSpaceKey:
|
|
|
|
return ICON_SPACE_KEY;
|
|
|
|
case R.styleable.Keyboard_iconReturnKey:
|
|
|
|
return ICON_RETURN_KEY;
|
|
|
|
case R.styleable.Keyboard_iconSearchKey:
|
|
|
|
return ICON_SEARCH_KEY;
|
|
|
|
case R.styleable.Keyboard_iconTabKey:
|
|
|
|
return ICON_TAB_KEY;
|
2011-08-03 04:29:24 +00:00
|
|
|
case R.styleable.Keyboard_iconShortcutKey:
|
|
|
|
return ICON_SHORTCUT_KEY;
|
|
|
|
case R.styleable.Keyboard_iconShortcutForLabel:
|
|
|
|
return ICON_SHORTCUT_FOR_LABEL;
|
2011-12-17 23:00:07 +00:00
|
|
|
case R.styleable.Keyboard_iconShortcutKeyDisabled:
|
|
|
|
return ICON_SHORTCUT_KEY_DISABLED;
|
|
|
|
case R.styleable.Keyboard_iconSpaceKeyForNumberLayout:
|
|
|
|
return ICON_SPACE_KEY_FOR_NUMBER_LAYOUT;
|
|
|
|
case R.styleable.Keyboard_iconShiftKeyShifted:
|
|
|
|
return ICON_SHIFT_KEY_SHIFTED;
|
2011-06-21 14:38:42 +00:00
|
|
|
case R.styleable.Keyboard_iconPreviewTabKey:
|
|
|
|
return ICON_PREVIEW_TAB_KEY;
|
|
|
|
default:
|
|
|
|
return ICON_UNDEFINED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-01 23:37:13 +00:00
|
|
|
public void loadIcons(final TypedArray keyboardAttrs) {
|
2011-06-21 14:38:42 +00:00
|
|
|
final int count = keyboardAttrs.getIndexCount();
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
final int attrIndex = keyboardAttrs.getIndex(i);
|
|
|
|
final int iconId = getIconId(attrIndex);
|
|
|
|
if (iconId != ICON_UNDEFINED) {
|
2011-06-22 13:45:03 +00:00
|
|
|
try {
|
2011-08-01 23:37:13 +00:00
|
|
|
mIcons[iconId] = setDefaultBounds(keyboardAttrs.getDrawable(attrIndex));
|
2011-06-22 13:45:03 +00:00
|
|
|
} catch (Resources.NotFoundException e) {
|
|
|
|
Log.w(TAG, "Drawable resource for icon #" + iconId + " not found");
|
|
|
|
}
|
2011-06-21 14:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-01 23:37:13 +00:00
|
|
|
public Drawable getIcon(final int iconId) {
|
2011-06-21 14:38:42 +00:00
|
|
|
if (iconId == ICON_UNDEFINED)
|
|
|
|
return null;
|
|
|
|
if (iconId < 0 || iconId >= mIcons.length)
|
|
|
|
throw new IllegalArgumentException("icon id is out of range: " + iconId);
|
|
|
|
return mIcons[iconId];
|
|
|
|
}
|
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
|
|
|
}
|