2010-11-19 22:57:24 +00:00
|
|
|
/*
|
2011-05-20 03:09:57 +00:00
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2010-11-19 22:57:24 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-06-22 02:53:02 +00:00
|
|
|
package com.android.inputmethod.keyboard.internal;
|
2010-11-19 22:57:24 +00:00
|
|
|
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2011-12-18 10:54:08 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
2011-06-21 14:38:42 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
2011-12-18 10:54:08 +00:00
|
|
|
import com.android.inputmethod.latin.XmlParseUtils;
|
2011-06-21 14:38:42 +00:00
|
|
|
|
2011-10-06 09:40:32 +00:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
2011-12-14 09:11:27 +00:00
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
2011-10-06 09:40:32 +00:00
|
|
|
|
2010-11-19 22:57:24 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
public class KeyStyles {
|
2012-01-20 07:19:51 +00:00
|
|
|
private static final String TAG = KeyStyles.class.getSimpleName();
|
2010-12-14 06:31:47 +00:00
|
|
|
private static final boolean DEBUG = false;
|
2010-11-19 22:57:24 +00:00
|
|
|
|
|
|
|
private final HashMap<String, DeclaredKeyStyle> mStyles =
|
|
|
|
new HashMap<String, DeclaredKeyStyle>();
|
|
|
|
private static final KeyStyle EMPTY_KEY_STYLE = new EmptyKeyStyle();
|
|
|
|
|
|
|
|
public interface KeyStyle {
|
2012-01-25 08:01:53 +00:00
|
|
|
public String[] getStringArray(TypedArray a, int index);
|
|
|
|
public String getString(TypedArray a, int index);
|
2010-11-19 22:57:24 +00:00
|
|
|
public int getInt(TypedArray a, int index, int defaultValue);
|
2012-02-07 08:30:54 +00:00
|
|
|
public int getFlag(TypedArray a, int index);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 08:30:54 +00:00
|
|
|
static class EmptyKeyStyle implements KeyStyle {
|
2010-12-14 06:31:47 +00:00
|
|
|
@Override
|
2012-01-25 08:01:53 +00:00
|
|
|
public String[] getStringArray(TypedArray a, int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
return KeyStyles.parseStringArray(a, index);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2012-01-25 08:01:53 +00:00
|
|
|
public String getString(TypedArray a, int index) {
|
|
|
|
return a.getString(index);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2010-11-19 22:57:24 +00:00
|
|
|
public int getInt(TypedArray a, int index, int defaultValue) {
|
|
|
|
return a.getInt(index, defaultValue);
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2012-02-07 08:30:54 +00:00
|
|
|
public int getFlag(TypedArray a, int index) {
|
|
|
|
return a.getInt(index, 0);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 08:30:54 +00:00
|
|
|
static class DeclaredKeyStyle implements KeyStyle {
|
|
|
|
private final HashMap<Integer, Object> mStyleAttributes = new HashMap<Integer, Object>();
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
@Override
|
2012-01-25 08:01:53 +00:00
|
|
|
public String[] getStringArray(TypedArray a, int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
|
|
|
return parseStringArray(a, index);
|
|
|
|
}
|
|
|
|
return (String[])mStyleAttributes.get(index);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
|
|
|
|
2010-11-19 22:57:24 +00:00
|
|
|
@Override
|
2012-01-25 08:01:53 +00:00
|
|
|
public String getString(TypedArray a, int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
|
|
|
return a.getString(index);
|
|
|
|
}
|
|
|
|
return (String)mStyleAttributes.get(index);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-12-14 06:31:47 +00:00
|
|
|
public int getInt(TypedArray a, int index, int defaultValue) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
|
|
|
return a.getInt(index, defaultValue);
|
|
|
|
}
|
|
|
|
final Integer styleValue = (Integer)mStyleAttributes.get(index);
|
|
|
|
return styleValue != null ? styleValue : defaultValue;
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-02-07 08:30:54 +00:00
|
|
|
public int getFlag(TypedArray a, int index) {
|
|
|
|
final int value = a.getInt(index, 0);
|
|
|
|
final Integer styleValue = (Integer)mStyleAttributes.get(index);
|
|
|
|
return (styleValue != null ? styleValue : 0) | value;
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 08:30:54 +00:00
|
|
|
void readKeyAttributes(TypedArray keyAttr) {
|
2010-11-19 22:57:24 +00:00
|
|
|
// TODO: Currently not all Key attributes can be declared as style.
|
2010-12-20 11:30:26 +00:00
|
|
|
readInt(keyAttr, R.styleable.Keyboard_Key_code);
|
2011-11-30 08:54:58 +00:00
|
|
|
readInt(keyAttr, R.styleable.Keyboard_Key_altCode);
|
2012-01-25 08:01:53 +00:00
|
|
|
readString(keyAttr, R.styleable.Keyboard_Key_keyLabel);
|
|
|
|
readString(keyAttr, R.styleable.Keyboard_Key_keyOutputText);
|
|
|
|
readString(keyAttr, R.styleable.Keyboard_Key_keyHintLabel);
|
|
|
|
readStringArray(keyAttr, R.styleable.Keyboard_Key_moreKeys);
|
2012-01-30 02:14:45 +00:00
|
|
|
readStringArray(keyAttr, R.styleable.Keyboard_Key_additionalMoreKeys);
|
2011-11-22 03:55:38 +00:00
|
|
|
readFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelFlags);
|
2011-06-21 14:38:42 +00:00
|
|
|
readInt(keyAttr, R.styleable.Keyboard_Key_keyIcon);
|
2011-12-18 16:11:09 +00:00
|
|
|
readInt(keyAttr, R.styleable.Keyboard_Key_keyIconDisabled);
|
2011-06-21 14:38:42 +00:00
|
|
|
readInt(keyAttr, R.styleable.Keyboard_Key_keyIconPreview);
|
2011-08-31 06:26:32 +00:00
|
|
|
readInt(keyAttr, R.styleable.Keyboard_Key_maxMoreKeysColumn);
|
2011-09-15 05:21:46 +00:00
|
|
|
readInt(keyAttr, R.styleable.Keyboard_Key_backgroundType);
|
2011-11-22 03:55:38 +00:00
|
|
|
readFlag(keyAttr, R.styleable.Keyboard_Key_keyActionFlags);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 08:01:53 +00:00
|
|
|
private void readString(TypedArray a, int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
|
|
|
mStyleAttributes.put(index, a.getString(index));
|
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
private void readInt(TypedArray a, int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
|
|
|
mStyleAttributes.put(index, a.getInt(index, 0));
|
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void readFlag(TypedArray a, int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
final Integer value = (Integer)mStyleAttributes.get(index);
|
|
|
|
if (a.hasValue(index)) {
|
|
|
|
mStyleAttributes.put(index, a.getInt(index, 0) | (value != null ? value : 0));
|
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 08:01:53 +00:00
|
|
|
private void readStringArray(TypedArray a, int index) {
|
|
|
|
final String[] value = parseStringArray(a, index);
|
2012-02-07 08:30:54 +00:00
|
|
|
if (value != null) {
|
|
|
|
mStyleAttributes.put(index, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addParentStyleAttributes(DeclaredKeyStyle parentStyle) {
|
|
|
|
mStyleAttributes.putAll(parentStyle.mStyleAttributes);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
2012-02-07 08:30:54 +00:00
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2012-02-07 08:30:54 +00:00
|
|
|
static String[] parseStringArray(TypedArray a, int index) {
|
|
|
|
if (a.hasValue(index)) {
|
|
|
|
return KeySpecParser.parseCsvString(
|
|
|
|
a.getString(index), a.getResources(), R.string.english_ime_name);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
2012-02-07 08:30:54 +00:00
|
|
|
return null;
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
public void parseKeyStyleAttributes(TypedArray keyStyleAttr, TypedArray keyAttrs,
|
2011-12-14 09:11:27 +00:00
|
|
|
XmlPullParser parser) throws XmlPullParserException {
|
2011-08-01 01:23:39 +00:00
|
|
|
final String styleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_styleName);
|
2012-02-07 08:30:54 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, String.format("<%s styleName=%s />",
|
|
|
|
Keyboard.Builder.TAG_KEY_STYLE, styleName));
|
2012-02-17 07:14:14 +00:00
|
|
|
if (mStyles.containsKey(styleName)) {
|
|
|
|
Log.d(TAG, "key-style " + styleName + " is overridden at "
|
|
|
|
+ parser.getPositionDescription());
|
|
|
|
}
|
2012-02-07 08:30:54 +00:00
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
|
|
|
|
final DeclaredKeyStyle style = new DeclaredKeyStyle();
|
2010-12-14 06:31:47 +00:00
|
|
|
if (keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_parentStyle)) {
|
2011-08-01 01:23:39 +00:00
|
|
|
final String parentStyle = keyStyleAttr.getString(
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_KeyStyle_parentStyle);
|
2010-11-19 22:57:24 +00:00
|
|
|
final DeclaredKeyStyle parent = mStyles.get(parentStyle);
|
2012-02-07 08:30:54 +00:00
|
|
|
if (parent == null) {
|
2011-12-14 09:11:27 +00:00
|
|
|
throw new XmlParseUtils.ParseException(
|
|
|
|
"Unknown parentStyle " + parentStyle, parser);
|
2012-02-07 08:30:54 +00:00
|
|
|
}
|
|
|
|
style.addParentStyleAttributes(parent);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
2012-02-07 08:30:54 +00:00
|
|
|
style.readKeyAttributes(keyAttrs);
|
2010-11-19 22:57:24 +00:00
|
|
|
mStyles.put(styleName, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
public KeyStyle getKeyStyle(String styleName) {
|
|
|
|
return mStyles.get(styleName);
|
|
|
|
}
|
|
|
|
|
2011-10-28 04:31:31 +00:00
|
|
|
public static KeyStyle getEmptyKeyStyle() {
|
2010-11-19 22:57:24 +00:00
|
|
|
return EMPTY_KEY_STYLE;
|
|
|
|
}
|
|
|
|
}
|