2010-11-19 22:57:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Google Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
package com.android.inputmethod.keyboard;
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
import com.android.inputmethod.keyboard.KeyboardParser.ParseException;
|
|
|
|
import com.android.inputmethod.latin.R;
|
2010-11-19 22:57:24 +00:00
|
|
|
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.content.res.XmlResourceParser;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
import java.util.ArrayList;
|
2010-11-19 22:57:24 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
public class KeyStyles {
|
|
|
|
private static final String TAG = "KeyStyles";
|
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 {
|
2010-12-14 06:31:47 +00:00
|
|
|
public CharSequence[] getTextArray(TypedArray a, int index);
|
2010-11-19 22:57:24 +00:00
|
|
|
public Drawable getDrawable(TypedArray a, int index);
|
|
|
|
public CharSequence getText(TypedArray a, int index);
|
|
|
|
public int getInt(TypedArray a, int index, int defaultValue);
|
|
|
|
public int getFlag(TypedArray a, int index, int defaultValue);
|
|
|
|
public boolean getBoolean(TypedArray a, int index, boolean defaultValue);
|
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
/* package */ static class EmptyKeyStyle implements KeyStyle {
|
2010-11-19 22:57:24 +00:00
|
|
|
private EmptyKeyStyle() {
|
2010-12-10 06:24:28 +00:00
|
|
|
// Nothing to do.
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
@Override
|
|
|
|
public CharSequence[] getTextArray(TypedArray a, int index) {
|
|
|
|
return parseTextArray(a, index);
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2010-11-19 22:57:24 +00:00
|
|
|
public Drawable getDrawable(TypedArray a, int index) {
|
|
|
|
return a.getDrawable(index);
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2010-11-19 22:57:24 +00:00
|
|
|
public CharSequence getText(TypedArray a, int index) {
|
|
|
|
return a.getText(index);
|
|
|
|
}
|
|
|
|
|
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
|
2010-11-19 22:57:24 +00:00
|
|
|
public int getFlag(TypedArray a, int index, int defaultValue) {
|
|
|
|
return a.getInt(index, defaultValue);
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2010-11-19 22:57:24 +00:00
|
|
|
public boolean getBoolean(TypedArray a, int index, boolean defaultValue) {
|
|
|
|
return a.getBoolean(index, defaultValue);
|
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
protected static CharSequence[] parseTextArray(TypedArray a, int index) {
|
|
|
|
if (!a.hasValue(index))
|
|
|
|
return null;
|
|
|
|
final CharSequence text = a.getText(index);
|
|
|
|
return parseCsvText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ static CharSequence[] parseCsvText(CharSequence text) {
|
|
|
|
final int size = text.length();
|
|
|
|
if (size == 0) return null;
|
|
|
|
if (size == 1) return new CharSequence[] { text };
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
ArrayList<CharSequence> list = null;
|
|
|
|
int start = 0;
|
|
|
|
for (int pos = 0; pos < size; pos++) {
|
|
|
|
final char c = text.charAt(pos);
|
|
|
|
if (c == ',') {
|
|
|
|
if (list == null) list = new ArrayList<CharSequence>();
|
|
|
|
if (sb.length() == 0) {
|
|
|
|
list.add(text.subSequence(start, pos));
|
|
|
|
} else {
|
|
|
|
list.add(sb.toString());
|
|
|
|
sb.setLength(0);
|
|
|
|
}
|
|
|
|
start = pos + 1;
|
|
|
|
continue;
|
|
|
|
} else if (c == '\\') {
|
|
|
|
if (start == pos) {
|
|
|
|
// Skip escape character at the beginning of the value.
|
|
|
|
start++;
|
|
|
|
pos++;
|
|
|
|
} else {
|
|
|
|
if (start < pos && sb.length() == 0)
|
|
|
|
sb.append(text.subSequence(start, pos));
|
|
|
|
pos++;
|
|
|
|
if (pos < size)
|
|
|
|
sb.append(text.charAt(pos));
|
|
|
|
}
|
|
|
|
} else if (sb.length() > 0) {
|
|
|
|
sb.append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (list == null) {
|
|
|
|
return new CharSequence[] { sb.length() > 0 ? sb : text.subSequence(start, size) };
|
|
|
|
} else {
|
|
|
|
list.add(sb.length() > 0 ? sb : text.subSequence(start, size));
|
|
|
|
return list.toArray(new CharSequence[list.size()]);
|
|
|
|
}
|
|
|
|
}
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
private static class DeclaredKeyStyle extends EmptyKeyStyle {
|
2010-11-19 22:57:24 +00:00
|
|
|
private final HashMap<Integer, Object> mAttributes = new HashMap<Integer, Object>();
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
@Override
|
|
|
|
public CharSequence[] getTextArray(TypedArray a, int index) {
|
|
|
|
return a.hasValue(index)
|
|
|
|
? super.getTextArray(a, index) : (CharSequence[])mAttributes.get(index);
|
|
|
|
}
|
|
|
|
|
2010-11-19 22:57:24 +00:00
|
|
|
@Override
|
|
|
|
public Drawable getDrawable(TypedArray a, int index) {
|
|
|
|
return a.hasValue(index)
|
|
|
|
? super.getDrawable(a, index) : (Drawable)mAttributes.get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CharSequence getText(TypedArray a, int index) {
|
|
|
|
return a.hasValue(index)
|
|
|
|
? super.getText(a, index) : (CharSequence)mAttributes.get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-12-14 06:31:47 +00:00
|
|
|
public int getInt(TypedArray a, int index, int defaultValue) {
|
2010-11-19 22:57:24 +00:00
|
|
|
final Integer value = (Integer)mAttributes.get(index);
|
2010-12-14 06:31:47 +00:00
|
|
|
return super.getInt(a, index, (value != null) ? value : defaultValue);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getFlag(TypedArray a, int index, int defaultValue) {
|
|
|
|
final Integer value = (Integer)mAttributes.get(index);
|
|
|
|
return super.getFlag(a, index, defaultValue) | (value != null ? value : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean getBoolean(TypedArray a, int index, boolean defaultValue) {
|
|
|
|
final Boolean value = (Boolean)mAttributes.get(index);
|
|
|
|
return super.getBoolean(a, index, (value != null) ? value : defaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
private DeclaredKeyStyle() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
private void parseKeyStyleAttributes(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);
|
2010-12-14 06:31:47 +00:00
|
|
|
readText(keyAttr, R.styleable.Keyboard_Key_keyLabel);
|
|
|
|
readFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelOption);
|
|
|
|
readTextArray(keyAttr, R.styleable.Keyboard_Key_popupCharacters);
|
|
|
|
readInt(keyAttr, R.styleable.Keyboard_Key_maxPopupKeyboardColumn);
|
|
|
|
readText(keyAttr, R.styleable.Keyboard_Key_keyOutputText);
|
|
|
|
readDrawable(keyAttr, R.styleable.Keyboard_Key_keyIcon);
|
|
|
|
readDrawable(keyAttr, R.styleable.Keyboard_Key_iconPreview);
|
|
|
|
readDrawable(keyAttr, R.styleable.Keyboard_Key_keyHintIcon);
|
|
|
|
readDrawable(keyAttr, R.styleable.Keyboard_Key_shiftedIcon);
|
2011-04-07 07:12:00 +00:00
|
|
|
readBoolean(keyAttr, R.styleable.Keyboard_Key_isFunctional);
|
2010-12-14 06:31:47 +00:00
|
|
|
readBoolean(keyAttr, R.styleable.Keyboard_Key_isSticky);
|
|
|
|
readBoolean(keyAttr, R.styleable.Keyboard_Key_isRepeatable);
|
2011-02-20 06:25:57 +00:00
|
|
|
readBoolean(keyAttr, R.styleable.Keyboard_Key_enabled);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void readDrawable(TypedArray a, int index) {
|
|
|
|
if (a.hasValue(index))
|
|
|
|
mAttributes.put(index, a.getDrawable(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void readText(TypedArray a, int index) {
|
|
|
|
if (a.hasValue(index))
|
|
|
|
mAttributes.put(index, a.getText(index));
|
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
private void readInt(TypedArray a, int index) {
|
2010-11-19 22:57:24 +00:00
|
|
|
if (a.hasValue(index))
|
2010-12-14 06:31:47 +00:00
|
|
|
mAttributes.put(index, a.getInt(index, 0));
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void readFlag(TypedArray a, int index) {
|
|
|
|
final Integer value = (Integer)mAttributes.get(index);
|
|
|
|
if (a.hasValue(index))
|
|
|
|
mAttributes.put(index, a.getInt(index, 0) | (value != null ? value : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void readBoolean(TypedArray a, int index) {
|
|
|
|
if (a.hasValue(index))
|
|
|
|
mAttributes.put(index, a.getBoolean(index, false));
|
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
private void readTextArray(TypedArray a, int index) {
|
|
|
|
final CharSequence[] value = parseTextArray(a, index);
|
|
|
|
if (value != null)
|
|
|
|
mAttributes.put(index, value);
|
2010-11-19 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addParent(DeclaredKeyStyle parentStyle) {
|
|
|
|
mAttributes.putAll(parentStyle.mAttributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
public void parseKeyStyleAttributes(TypedArray keyStyleAttr, TypedArray keyAttrs,
|
2010-11-19 22:57:24 +00:00
|
|
|
XmlResourceParser parser) {
|
2010-12-14 06:31:47 +00:00
|
|
|
String styleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_styleName);
|
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s styleName=%s />",
|
|
|
|
KeyboardParser.TAG_KEY_STYLE, styleName));
|
2010-11-19 22:57:24 +00:00
|
|
|
if (mStyles.containsKey(styleName))
|
|
|
|
throw new ParseException("duplicate key style declared: " + styleName, parser);
|
|
|
|
|
|
|
|
final DeclaredKeyStyle style = new DeclaredKeyStyle();
|
2010-12-14 06:31:47 +00:00
|
|
|
if (keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_parentStyle)) {
|
|
|
|
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);
|
|
|
|
if (parent == null)
|
|
|
|
throw new ParseException("Unknown parentStyle " + parent, parser);
|
|
|
|
style.addParent(parent);
|
|
|
|
}
|
|
|
|
style.parseKeyStyleAttributes(keyAttrs);
|
|
|
|
mStyles.put(styleName, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
public KeyStyle getKeyStyle(String styleName) {
|
|
|
|
return mStyles.get(styleName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public KeyStyle getEmptyKeyStyle() {
|
|
|
|
return EMPTY_KEY_STYLE;
|
|
|
|
}
|
|
|
|
}
|