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
|
|
|
*
|
2013-01-21 12:52:57 +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
|
2010-11-19 22:57:24 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2010-11-19 22:57:24 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2010-11-19 22:57:24 +00:00
|
|
|
*/
|
|
|
|
|
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;
|
2012-06-29 07:32:45 +00:00
|
|
|
import android.util.SparseArray;
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2011-06-21 14:38:42 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
|
|
|
import com.android.inputmethod.latin.utils.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;
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class KeyStylesSet {
|
2012-08-30 05:22:40 +00:00
|
|
|
private static final String TAG = KeyStylesSet.class.getSimpleName();
|
2010-12-14 06:31:47 +00:00
|
|
|
private static final boolean DEBUG = false;
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
private final HashMap<String, KeyStyle> mStyles = CollectionUtils.newHashMap();
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
private final KeyboardTextsSet mTextsSet;
|
2012-04-05 05:59:55 +00:00
|
|
|
private final KeyStyle mEmptyKeyStyle;
|
2012-05-01 07:51:38 +00:00
|
|
|
private static final String EMPTY_STYLE_NAME = "<empty>";
|
2012-04-05 05:59:55 +00:00
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public KeyStylesSet(final KeyboardTextsSet textsSet) {
|
2012-04-19 23:39:11 +00:00
|
|
|
mTextsSet = textsSet;
|
2012-08-30 05:22:40 +00:00
|
|
|
mEmptyKeyStyle = new EmptyKeyStyle(textsSet);
|
2012-05-01 07:51:38 +00:00
|
|
|
mStyles.put(EMPTY_STYLE_NAME, mEmptyKeyStyle);
|
2012-04-05 05:59:55 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
private static final class EmptyKeyStyle extends KeyStyle {
|
2012-08-30 05:22:40 +00:00
|
|
|
EmptyKeyStyle(final KeyboardTextsSet textsSet) {
|
|
|
|
super(textsSet);
|
2012-04-05 05:59:55 +00:00
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
@Override
|
2012-08-30 05:22:40 +00:00
|
|
|
public String[] getStringArray(final TypedArray a, final int index) {
|
2012-04-05 05:59:55 +00:00
|
|
|
return parseStringArray(a, index);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2012-08-30 05:22:40 +00:00
|
|
|
public String getString(final TypedArray a, final int index) {
|
2012-04-05 05:59:55 +00:00
|
|
|
return parseString(a, index);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2012-08-30 05:22:40 +00:00
|
|
|
public int getInt(final TypedArray a, final int index, final int defaultValue) {
|
2010-11-19 22:57:24 +00:00
|
|
|
return a.getInt(index, defaultValue);
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2013-08-19 02:02:44 +00:00
|
|
|
public int getFlags(final TypedArray a, final int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
return a.getInt(index, 0);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
private static final class DeclaredKeyStyle extends KeyStyle {
|
2012-08-30 05:22:40 +00:00
|
|
|
private final HashMap<String, KeyStyle> mStyles;
|
2012-05-01 07:51:38 +00:00
|
|
|
private final String mParentStyleName;
|
2012-08-22 05:22:20 +00:00
|
|
|
private final SparseArray<Object> mStyleAttributes = CollectionUtils.newSparseArray();
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public DeclaredKeyStyle(final String parentStyleName, final KeyboardTextsSet textsSet,
|
|
|
|
final HashMap<String, KeyStyle> styles) {
|
|
|
|
super(textsSet);
|
2012-05-01 07:51:38 +00:00
|
|
|
mParentStyleName = parentStyleName;
|
2012-08-30 05:22:40 +00:00
|
|
|
mStyles = styles;
|
2012-04-05 05:59:55 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
@Override
|
2012-08-30 05:22:40 +00:00
|
|
|
public String[] getStringArray(final TypedArray a, final int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
|
|
|
return parseStringArray(a, index);
|
|
|
|
}
|
2012-06-29 07:32:45 +00:00
|
|
|
final Object value = mStyleAttributes.get(index);
|
|
|
|
if (value != null) {
|
|
|
|
return (String[])value;
|
2012-05-01 07:51:38 +00:00
|
|
|
}
|
|
|
|
final KeyStyle parentStyle = mStyles.get(mParentStyleName);
|
|
|
|
return parentStyle.getStringArray(a, index);
|
2010-12-14 06:31:47 +00:00
|
|
|
}
|
|
|
|
|
2010-11-19 22:57:24 +00:00
|
|
|
@Override
|
2012-08-30 05:22:40 +00:00
|
|
|
public String getString(final TypedArray a, final int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
2012-04-05 05:59:55 +00:00
|
|
|
return parseString(a, index);
|
2012-02-07 08:30:54 +00:00
|
|
|
}
|
2012-06-29 07:32:45 +00:00
|
|
|
final Object value = mStyleAttributes.get(index);
|
|
|
|
if (value != null) {
|
|
|
|
return (String)value;
|
2012-05-01 07:51:38 +00:00
|
|
|
}
|
|
|
|
final KeyStyle parentStyle = mStyles.get(mParentStyleName);
|
|
|
|
return parentStyle.getString(a, index);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-08-30 05:22:40 +00:00
|
|
|
public int getInt(final TypedArray a, final int index, final int defaultValue) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
|
|
|
return a.getInt(index, defaultValue);
|
|
|
|
}
|
2012-06-29 07:32:45 +00:00
|
|
|
final Object value = mStyleAttributes.get(index);
|
|
|
|
if (value != null) {
|
|
|
|
return (Integer)value;
|
2012-05-01 07:51:38 +00:00
|
|
|
}
|
|
|
|
final KeyStyle parentStyle = mStyles.get(mParentStyleName);
|
|
|
|
return parentStyle.getInt(a, index, defaultValue);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-08-19 02:02:44 +00:00
|
|
|
public int getFlags(final TypedArray a, final int index) {
|
|
|
|
final Integer value = (Integer)mStyleAttributes.get(index);
|
|
|
|
final int flags = a.getInt(index, (value != null) ? value : 0);
|
2012-05-01 07:51:38 +00:00
|
|
|
final KeyStyle parentStyle = mStyles.get(mParentStyleName);
|
2013-08-19 02:02:44 +00:00
|
|
|
return flags | parentStyle.getFlags(a, index);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public void readKeyAttributes(final TypedArray keyAttr) {
|
2010-11-19 22:57:24 +00:00
|
|
|
// TODO: Currently not all Key attributes can be declared as style.
|
2012-04-09 08:48:32 +00:00
|
|
|
readString(keyAttr, R.styleable.Keyboard_Key_code);
|
|
|
|
readString(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);
|
2013-08-19 02:02:44 +00:00
|
|
|
readFlags(keyAttr, R.styleable.Keyboard_Key_keyLabelFlags);
|
2012-04-19 21:24:51 +00:00
|
|
|
readString(keyAttr, R.styleable.Keyboard_Key_keyIcon);
|
|
|
|
readString(keyAttr, R.styleable.Keyboard_Key_keyIconDisabled);
|
|
|
|
readString(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);
|
2013-08-19 02:02:44 +00:00
|
|
|
readFlags(keyAttr, R.styleable.Keyboard_Key_keyActionFlags);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
private void readString(final TypedArray a, final int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
2012-04-05 05:59:55 +00:00
|
|
|
mStyleAttributes.put(index, parseString(a, index));
|
2012-02-07 08:30:54 +00:00
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
private void readInt(final TypedArray a, final 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
|
|
|
}
|
|
|
|
|
2013-08-19 02:02:44 +00:00
|
|
|
private void readFlags(final TypedArray a, final int index) {
|
2012-02-07 08:30:54 +00:00
|
|
|
if (a.hasValue(index)) {
|
2012-04-05 05:59:55 +00:00
|
|
|
final Integer value = (Integer)mStyleAttributes.get(index);
|
2012-02-07 08:30:54 +00:00
|
|
|
mStyleAttributes.put(index, a.getInt(index, 0) | (value != null ? value : 0));
|
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
private void readStringArray(final TypedArray a, final int index) {
|
2012-04-05 05:59:55 +00:00
|
|
|
if (a.hasValue(index)) {
|
|
|
|
mStyleAttributes.put(index, parseStringArray(a, index));
|
2012-02-07 08:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public void parseKeyStyleAttributes(final TypedArray keyStyleAttr, final TypedArray keyAttrs,
|
|
|
|
final 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 />",
|
2012-08-30 05:22:40 +00:00
|
|
|
KeyboardBuilder.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
|
|
|
|
2012-05-01 07:51:38 +00:00
|
|
|
String parentStyleName = EMPTY_STYLE_NAME;
|
2010-12-14 06:31:47 +00:00
|
|
|
if (keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_parentStyle)) {
|
2012-05-01 07:51:38 +00:00
|
|
|
parentStyleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_parentStyle);
|
|
|
|
if (!mStyles.containsKey(parentStyleName)) {
|
2011-12-14 09:11:27 +00:00
|
|
|
throw new XmlParseUtils.ParseException(
|
2012-05-01 07:51:38 +00:00
|
|
|
"Unknown parentStyle " + parentStyleName, parser);
|
2012-02-07 08:30:54 +00:00
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
2012-08-30 05:22:40 +00:00
|
|
|
final DeclaredKeyStyle style = new DeclaredKeyStyle(parentStyleName, mTextsSet, mStyles);
|
2012-02-07 08:30:54 +00:00
|
|
|
style.readKeyAttributes(keyAttrs);
|
2010-11-19 22:57:24 +00:00
|
|
|
mStyles.put(styleName, style);
|
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public KeyStyle getKeyStyle(final TypedArray keyAttr, final XmlPullParser parser)
|
2012-05-01 07:51:38 +00:00
|
|
|
throws XmlParseUtils.ParseException {
|
|
|
|
if (!keyAttr.hasValue(R.styleable.Keyboard_Key_keyStyle)) {
|
|
|
|
return mEmptyKeyStyle;
|
|
|
|
}
|
|
|
|
final String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle);
|
|
|
|
if (!mStyles.containsKey(styleName)) {
|
|
|
|
throw new XmlParseUtils.ParseException("Unknown key style: " + styleName, parser);
|
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
return mStyles.get(styleName);
|
|
|
|
}
|
|
|
|
}
|