2010-10-21 10:05:53 +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-10-21 10:05:53 +00:00
|
|
|
|
2011-03-25 19:40:57 +00:00
|
|
|
import com.android.inputmethod.compat.EditorInfoCompatUtils;
|
2010-12-02 09:46:21 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
2010-10-21 10:05:53 +00:00
|
|
|
|
2010-12-10 06:24:28 +00:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
2010-10-21 10:05:53 +00:00
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.content.res.XmlResourceParser;
|
2010-10-21 16:24:23 +00:00
|
|
|
import android.util.Log;
|
2010-10-21 10:05:53 +00:00
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.util.Xml;
|
|
|
|
import android.view.InflateException;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2010-12-14 06:31:47 +00:00
|
|
|
import java.util.Arrays;
|
2010-10-21 16:24:23 +00:00
|
|
|
import java.util.List;
|
2010-10-21 10:05:53 +00:00
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
/**
|
|
|
|
* Parser for BaseKeyboard.
|
|
|
|
*
|
2010-12-02 09:46:21 +00:00
|
|
|
* This class parses Keyboard XML file and fill out keys in Keyboard.
|
2010-10-21 16:24:23 +00:00
|
|
|
* The Keyboard XML file looks like:
|
|
|
|
* <pre>
|
|
|
|
* >!-- xml/keyboard.xml --<
|
|
|
|
* >Keyboard keyboard_attributes*<
|
|
|
|
* >!-- Keyboard Content --<
|
|
|
|
* >Row row_attributes*<
|
|
|
|
* >!-- Row Content --<
|
|
|
|
* >Key key_attributes* /<
|
|
|
|
* >Spacer horizontalGap="0.2in" /<
|
|
|
|
* >include keyboardLayout="@xml/other_keys"<
|
|
|
|
* ...
|
|
|
|
* >/Row<
|
|
|
|
* >include keyboardLayout="@xml/other_rows"<
|
|
|
|
* ...
|
|
|
|
* >/Keyboard<
|
|
|
|
* </pre>
|
2010-11-11 23:28:14 +00:00
|
|
|
* The XML file which is included in other file must have >merge< as root element, such as:
|
2010-10-21 16:24:23 +00:00
|
|
|
* <pre>
|
|
|
|
* >!-- xml/other_keys.xml --<
|
|
|
|
* >merge<
|
|
|
|
* >Key key_attributes* /<
|
|
|
|
* ...
|
|
|
|
* >/merge<
|
|
|
|
* </pre>
|
|
|
|
* and
|
|
|
|
* <pre>
|
|
|
|
* >!-- xml/other_rows.xml --<
|
|
|
|
* >merge<
|
|
|
|
* >Row row_attributes*<
|
|
|
|
* >Key key_attributes* /<
|
|
|
|
* >/Row<
|
|
|
|
* ...
|
|
|
|
* >/merge<
|
|
|
|
* </pre>
|
2010-11-11 23:28:14 +00:00
|
|
|
* You can also use switch-case-default tags to select Rows and Keys.
|
|
|
|
* <pre>
|
|
|
|
* >switch<
|
|
|
|
* >case case_attribute*<
|
|
|
|
* >!-- Any valid tags at switch position --<
|
|
|
|
* >/case<
|
|
|
|
* ...
|
|
|
|
* >default<
|
|
|
|
* >!-- Any valid tags at switch position --<
|
|
|
|
* >/default<
|
|
|
|
* >/switch<
|
|
|
|
* </pre>
|
2010-11-19 22:57:24 +00:00
|
|
|
* You can declare Key style and specify styles within Key tags.
|
|
|
|
* <pre>
|
2010-11-11 23:28:14 +00:00
|
|
|
* >switch<
|
|
|
|
* >case colorScheme="white"<
|
2010-11-19 22:57:24 +00:00
|
|
|
* >key-style styleName="shift-key" parentStyle="modifier-key"
|
|
|
|
* keyIcon="@drawable/sym_keyboard_shift"
|
|
|
|
* /<
|
2010-11-11 23:28:14 +00:00
|
|
|
* >/case<
|
|
|
|
* >case colorScheme="black"<
|
2010-11-19 22:57:24 +00:00
|
|
|
* >key-style styleName="shift-key" parentStyle="modifier-key"
|
|
|
|
* keyIcon="@drawable/sym_bkeyboard_shift"
|
|
|
|
* /<
|
2010-11-11 23:28:14 +00:00
|
|
|
* >/case<
|
|
|
|
* >/switch<
|
|
|
|
* ...
|
2010-11-19 22:57:24 +00:00
|
|
|
* >Key keyStyle="shift-key" ... /<
|
|
|
|
* </pre>
|
2010-10-21 16:24:23 +00:00
|
|
|
*/
|
2010-11-19 22:57:24 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public class KeyboardParser {
|
2011-02-20 05:44:59 +00:00
|
|
|
private static final String TAG = KeyboardParser.class.getSimpleName();
|
2010-12-14 06:31:47 +00:00
|
|
|
private static final boolean DEBUG = false;
|
2010-10-21 16:24:23 +00:00
|
|
|
|
2010-10-21 10:05:53 +00:00
|
|
|
// Keyboard XML Tags
|
|
|
|
private static final String TAG_KEYBOARD = "Keyboard";
|
|
|
|
private static final String TAG_ROW = "Row";
|
|
|
|
private static final String TAG_KEY = "Key";
|
|
|
|
private static final String TAG_SPACER = "Spacer";
|
|
|
|
private static final String TAG_INCLUDE = "include";
|
|
|
|
private static final String TAG_MERGE = "merge";
|
2010-11-11 23:28:14 +00:00
|
|
|
private static final String TAG_SWITCH = "switch";
|
|
|
|
private static final String TAG_CASE = "case";
|
|
|
|
private static final String TAG_DEFAULT = "default";
|
2010-12-14 06:31:47 +00:00
|
|
|
public static final String TAG_KEY_STYLE = "key-style";
|
2010-11-11 23:28:14 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
private final Keyboard mKeyboard;
|
2010-10-21 10:05:53 +00:00
|
|
|
private final Resources mResources;
|
|
|
|
|
|
|
|
private int mCurrentX = 0;
|
|
|
|
private int mCurrentY = 0;
|
|
|
|
private int mMaxRowWidth = 0;
|
|
|
|
private int mTotalHeight = 0;
|
|
|
|
private Row mCurrentRow = null;
|
2010-11-19 22:57:24 +00:00
|
|
|
private final KeyStyles mKeyStyles = new KeyStyles();
|
2010-10-21 10:05:53 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public KeyboardParser(Keyboard keyboard, Resources res) {
|
2010-10-21 10:05:53 +00:00
|
|
|
mKeyboard = keyboard;
|
|
|
|
mResources = res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMaxRowWidth() {
|
|
|
|
return mMaxRowWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTotalHeight() {
|
|
|
|
return mTotalHeight;
|
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
public void parseKeyboard(int resId) throws XmlPullParserException, IOException {
|
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s> %s", TAG_KEYBOARD, mKeyboard.mId));
|
|
|
|
final XmlResourceParser parser = mResources.getXml(resId);
|
2010-10-21 16:24:23 +00:00
|
|
|
int event;
|
2010-12-10 06:24:28 +00:00
|
|
|
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
|
|
|
|
if (event == XmlPullParser.START_TAG) {
|
2010-10-21 16:24:23 +00:00
|
|
|
final String tag = parser.getName();
|
|
|
|
if (TAG_KEYBOARD.equals(tag)) {
|
|
|
|
parseKeyboardAttributes(parser);
|
|
|
|
parseKeyboardContent(parser, mKeyboard.getKeys());
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
throw new IllegalStartTag(parser, TAG_KEYBOARD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void parseKeyboardAttributes(XmlResourceParser parser) {
|
2010-12-02 09:46:21 +00:00
|
|
|
final Keyboard keyboard = mKeyboard;
|
2010-12-14 06:31:47 +00:00
|
|
|
final TypedArray keyboardAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard);
|
2010-12-14 06:31:47 +00:00
|
|
|
final TypedArray keyAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
|
|
|
R.styleable.Keyboard_Key);
|
|
|
|
try {
|
|
|
|
final int displayHeight = keyboard.getDisplayHeight();
|
|
|
|
final int keyboardHeight = (int)keyboardAttr.getDimension(
|
|
|
|
R.styleable.Keyboard_keyboardHeight, displayHeight / 2);
|
|
|
|
final int maxKeyboardHeight = getDimensionOrFraction(keyboardAttr,
|
|
|
|
R.styleable.Keyboard_maxKeyboardHeight, displayHeight, displayHeight / 2);
|
|
|
|
// Keyboard height will not exceed maxKeyboardHeight.
|
|
|
|
final int height = Math.min(keyboardHeight, maxKeyboardHeight);
|
|
|
|
final int width = keyboard.getDisplayWidth();
|
|
|
|
|
|
|
|
keyboard.setKeyboardHeight(height);
|
|
|
|
keyboard.setKeyWidth(getDimensionOrFraction(keyboardAttr,
|
|
|
|
R.styleable.Keyboard_keyWidth, width, width / 10));
|
|
|
|
keyboard.setRowHeight(getDimensionOrFraction(keyboardAttr,
|
|
|
|
R.styleable.Keyboard_rowHeight, height, 50));
|
|
|
|
keyboard.setHorizontalGap(getDimensionOrFraction(keyboardAttr,
|
|
|
|
R.styleable.Keyboard_horizontalGap, width, 0));
|
|
|
|
keyboard.setVerticalGap(getDimensionOrFraction(keyboardAttr,
|
|
|
|
R.styleable.Keyboard_verticalGap, height, 0));
|
|
|
|
keyboard.setPopupKeyboardResId(keyboardAttr.getResourceId(
|
|
|
|
R.styleable.Keyboard_popupKeyboardTemplate, 0));
|
|
|
|
|
|
|
|
keyboard.setMaxPopupKeyboardColumn(keyAttr.getInt(
|
|
|
|
R.styleable.Keyboard_Key_maxPopupKeyboardColumn, 5));
|
|
|
|
} finally {
|
|
|
|
keyAttr.recycle();
|
|
|
|
keyboardAttr.recycle();
|
|
|
|
}
|
2010-10-21 16:24:23 +00:00
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
|
2010-11-11 23:28:14 +00:00
|
|
|
private void parseKeyboardContent(XmlResourceParser parser, List<Key> keys)
|
2010-10-21 16:24:23 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2010-10-21 10:05:53 +00:00
|
|
|
int event;
|
2010-12-10 06:24:28 +00:00
|
|
|
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
|
|
|
|
if (event == XmlPullParser.START_TAG) {
|
2010-10-21 16:24:23 +00:00
|
|
|
final String tag = parser.getName();
|
2010-10-21 10:05:53 +00:00
|
|
|
if (TAG_ROW.equals(tag)) {
|
2010-12-14 06:31:47 +00:00
|
|
|
Row row = new Row(mResources, mKeyboard, parser);
|
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s>", TAG_ROW));
|
2010-11-11 23:28:14 +00:00
|
|
|
if (keys != null)
|
|
|
|
startRow(row);
|
|
|
|
parseRowContent(parser, row, keys);
|
2010-10-21 10:05:53 +00:00
|
|
|
} else if (TAG_INCLUDE.equals(tag)) {
|
2010-10-21 16:24:23 +00:00
|
|
|
parseIncludeKeyboardContent(parser, keys);
|
2010-11-11 23:28:14 +00:00
|
|
|
} else if (TAG_SWITCH.equals(tag)) {
|
|
|
|
parseSwitchKeyboardContent(parser, keys);
|
2010-11-19 22:57:24 +00:00
|
|
|
} else if (TAG_KEY_STYLE.equals(tag)) {
|
|
|
|
parseKeyStyle(parser, keys);
|
2010-10-21 10:05:53 +00:00
|
|
|
} else {
|
2010-10-21 16:24:23 +00:00
|
|
|
throw new IllegalStartTag(parser, TAG_ROW);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
2010-12-10 06:24:28 +00:00
|
|
|
} else if (event == XmlPullParser.END_TAG) {
|
2010-10-21 16:24:23 +00:00
|
|
|
final String tag = parser.getName();
|
|
|
|
if (TAG_KEYBOARD.equals(tag)) {
|
2010-10-21 10:05:53 +00:00
|
|
|
endKeyboard(mKeyboard.getVerticalGap());
|
2010-10-21 16:24:23 +00:00
|
|
|
break;
|
2010-12-14 06:31:47 +00:00
|
|
|
} else if (TAG_CASE.equals(tag) || TAG_DEFAULT.equals(tag)
|
|
|
|
|| TAG_MERGE.equals(tag)) {
|
|
|
|
if (DEBUG) Log.d(TAG, String.format("</%s>", tag));
|
2010-10-21 16:24:23 +00:00
|
|
|
break;
|
2010-11-19 22:57:24 +00:00
|
|
|
} else if (TAG_KEY_STYLE.equals(tag)) {
|
|
|
|
continue;
|
2010-10-21 10:05:53 +00:00
|
|
|
} else {
|
2010-10-21 16:24:23 +00:00
|
|
|
throw new IllegalEndTag(parser, TAG_ROW);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private void parseRowContent(XmlResourceParser parser, Row row, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
int event;
|
2010-12-10 06:24:28 +00:00
|
|
|
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
|
|
|
|
if (event == XmlPullParser.START_TAG) {
|
2010-10-21 16:24:23 +00:00
|
|
|
final String tag = parser.getName();
|
|
|
|
if (TAG_KEY.equals(tag)) {
|
|
|
|
parseKey(parser, row, keys);
|
|
|
|
} else if (TAG_SPACER.equals(tag)) {
|
|
|
|
parseSpacer(parser, keys);
|
|
|
|
} else if (TAG_INCLUDE.equals(tag)) {
|
|
|
|
parseIncludeRowContent(parser, row, keys);
|
2010-11-11 23:28:14 +00:00
|
|
|
} else if (TAG_SWITCH.equals(tag)) {
|
|
|
|
parseSwitchRowContent(parser, row, keys);
|
2010-11-19 22:57:24 +00:00
|
|
|
} else if (TAG_KEY_STYLE.equals(tag)) {
|
|
|
|
parseKeyStyle(parser, keys);
|
2010-10-21 16:24:23 +00:00
|
|
|
} else {
|
|
|
|
throw new IllegalStartTag(parser, TAG_KEY);
|
|
|
|
}
|
2010-12-10 06:24:28 +00:00
|
|
|
} else if (event == XmlPullParser.END_TAG) {
|
2010-10-21 16:24:23 +00:00
|
|
|
final String tag = parser.getName();
|
|
|
|
if (TAG_ROW.equals(tag)) {
|
2010-12-14 06:31:47 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("</%s>", TAG_ROW));
|
2010-10-21 16:24:23 +00:00
|
|
|
if (keys != null)
|
|
|
|
endRow();
|
|
|
|
break;
|
2010-12-14 06:31:47 +00:00
|
|
|
} else if (TAG_CASE.equals(tag) || TAG_DEFAULT.equals(tag)
|
|
|
|
|| TAG_MERGE.equals(tag)) {
|
|
|
|
if (DEBUG) Log.d(TAG, String.format("</%s>", tag));
|
2010-10-21 16:24:23 +00:00
|
|
|
break;
|
2010-11-19 22:57:24 +00:00
|
|
|
} else if (TAG_KEY_STYLE.equals(tag)) {
|
|
|
|
continue;
|
2010-10-21 16:24:23 +00:00
|
|
|
} else {
|
|
|
|
throw new IllegalEndTag(parser, TAG_KEY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private void parseKey(XmlResourceParser parser, Row row, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
if (keys == null) {
|
|
|
|
checkEndTag(TAG_KEY, parser);
|
|
|
|
} else {
|
2010-12-14 06:31:47 +00:00
|
|
|
Key key = new Key(mResources, row, mCurrentX, mCurrentY, parser, mKeyStyles);
|
2011-02-20 05:44:59 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s%s keyLabel=%s code=%d popupCharacters=%s />",
|
|
|
|
TAG_KEY, (key.mEnabled ? "" : " disabled"), key.mLabel, key.mCode,
|
2010-12-14 06:31:47 +00:00
|
|
|
Arrays.toString(key.mPopupCharacters)));
|
2010-10-21 16:24:23 +00:00
|
|
|
checkEndTag(TAG_KEY, parser);
|
|
|
|
keys.add(key);
|
2010-12-20 11:30:26 +00:00
|
|
|
if (key.mCode == Keyboard.CODE_SHIFT)
|
2010-10-21 16:24:23 +00:00
|
|
|
mKeyboard.getShiftKeys().add(key);
|
|
|
|
endKey(key);
|
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private void parseSpacer(XmlResourceParser parser, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
if (keys == null) {
|
|
|
|
checkEndTag(TAG_SPACER, parser);
|
|
|
|
} else {
|
2010-12-14 06:31:47 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s />", TAG_SPACER));
|
2010-10-21 16:24:23 +00:00
|
|
|
final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard);
|
|
|
|
final int gap = getDimensionOrFraction(a, R.styleable.Keyboard_horizontalGap,
|
2010-12-08 05:36:41 +00:00
|
|
|
mKeyboard.getDisplayWidth(), 0);
|
2010-10-21 16:24:23 +00:00
|
|
|
a.recycle();
|
|
|
|
checkEndTag(TAG_SPACER, parser);
|
|
|
|
setSpacer(gap);
|
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private void parseIncludeKeyboardContent(XmlResourceParser parser, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
parseIncludeInternal(parser, null, keys);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private void parseIncludeRowContent(XmlResourceParser parser, Row row, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
parseIncludeInternal(parser, row, keys);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private void parseIncludeInternal(XmlResourceParser parser, Row row, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
if (keys == null) {
|
|
|
|
checkEndTag(TAG_INCLUDE, parser);
|
|
|
|
} else {
|
|
|
|
final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_Include);
|
2010-10-21 16:24:23 +00:00
|
|
|
final int keyboardLayout = a.getResourceId(
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_Include_keyboardLayout, 0);
|
2010-10-21 16:24:23 +00:00
|
|
|
a.recycle();
|
2010-10-21 10:05:53 +00:00
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
checkEndTag(TAG_INCLUDE, parser);
|
|
|
|
if (keyboardLayout == 0)
|
|
|
|
throw new ParseException("No keyboardLayout attribute in <include/>", parser);
|
2010-12-14 06:31:47 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s keyboardLayout=%s />",
|
|
|
|
TAG_INCLUDE, mResources.getResourceEntryName(keyboardLayout)));
|
2010-10-21 16:24:23 +00:00
|
|
|
parseMerge(mResources.getLayout(keyboardLayout), row, keys);
|
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private void parseMerge(XmlResourceParser parser, Row row, List<Key> keys)
|
2010-10-21 10:05:53 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
int event;
|
2010-12-10 06:24:28 +00:00
|
|
|
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
|
|
|
|
if (event == XmlPullParser.START_TAG) {
|
2010-11-11 23:28:14 +00:00
|
|
|
final String tag = parser.getName();
|
2010-10-21 16:24:23 +00:00
|
|
|
if (TAG_MERGE.equals(tag)) {
|
|
|
|
if (row == null) {
|
|
|
|
parseKeyboardContent(parser, keys);
|
|
|
|
} else {
|
|
|
|
parseRowContent(parser, row, keys);
|
|
|
|
}
|
|
|
|
break;
|
2010-10-21 10:05:53 +00:00
|
|
|
} else {
|
2010-10-21 16:24:23 +00:00
|
|
|
throw new ParseException(
|
|
|
|
"Included keyboard layout must have <merge> root element", parser);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 23:28:14 +00:00
|
|
|
private void parseSwitchKeyboardContent(XmlResourceParser parser, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
parseSwitchInternal(parser, null, keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void parseSwitchRowContent(XmlResourceParser parser, Row row, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
parseSwitchInternal(parser, row, keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void parseSwitchInternal(XmlResourceParser parser, Row row, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
2010-12-14 06:31:47 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s> %s", TAG_SWITCH, mKeyboard.mId));
|
2010-11-11 23:28:14 +00:00
|
|
|
boolean selected = false;
|
|
|
|
int event;
|
2010-12-10 06:24:28 +00:00
|
|
|
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
|
|
|
|
if (event == XmlPullParser.START_TAG) {
|
2010-11-11 23:28:14 +00:00
|
|
|
final String tag = parser.getName();
|
|
|
|
if (TAG_CASE.equals(tag)) {
|
|
|
|
selected |= parseCase(parser, row, selected ? null : keys);
|
|
|
|
} else if (TAG_DEFAULT.equals(tag)) {
|
|
|
|
selected |= parseDefault(parser, row, selected ? null : keys);
|
|
|
|
} else {
|
|
|
|
throw new IllegalStartTag(parser, TAG_KEY);
|
|
|
|
}
|
2010-12-10 06:24:28 +00:00
|
|
|
} else if (event == XmlPullParser.END_TAG) {
|
2010-11-11 23:28:14 +00:00
|
|
|
final String tag = parser.getName();
|
|
|
|
if (TAG_SWITCH.equals(tag)) {
|
2010-12-14 06:31:47 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("</%s>", TAG_SWITCH));
|
2010-11-11 23:28:14 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
throw new IllegalEndTag(parser, TAG_KEY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean parseCase(XmlResourceParser parser, Row row, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
final boolean selected = parseCaseCondition(parser);
|
|
|
|
if (row == null) {
|
|
|
|
// Processing Rows.
|
|
|
|
parseKeyboardContent(parser, selected ? keys : null);
|
|
|
|
} else {
|
|
|
|
// Processing Keys.
|
|
|
|
parseRowContent(parser, row, selected ? keys : null);
|
|
|
|
}
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean parseCaseCondition(XmlResourceParser parser) {
|
2010-11-20 02:45:30 +00:00
|
|
|
final KeyboardId id = mKeyboard.mId;
|
2010-11-11 23:28:14 +00:00
|
|
|
if (id == null)
|
|
|
|
return true;
|
2010-11-20 02:45:30 +00:00
|
|
|
|
|
|
|
final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_Case);
|
2010-11-22 00:40:38 +00:00
|
|
|
final TypedArray viewAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.KeyboardView);
|
2010-11-20 02:45:30 +00:00
|
|
|
try {
|
2010-11-22 00:40:38 +00:00
|
|
|
final boolean modeMatched = matchInteger(a,
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_Case_mode, id.mMode);
|
2011-02-20 03:54:14 +00:00
|
|
|
final boolean passwordInputMatched = matchBoolean(a,
|
|
|
|
R.styleable.Keyboard_Case_passwordInput, id.mPasswordInput);
|
2010-11-22 00:40:38 +00:00
|
|
|
final boolean settingsKeyMatched = matchBoolean(a,
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_Case_hasSettingsKey, id.mHasSettingsKey);
|
2010-11-22 00:40:38 +00:00
|
|
|
final boolean voiceEnabledMatched = matchBoolean(a,
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_Case_voiceKeyEnabled, id.mVoiceKeyEnabled);
|
2010-11-22 00:40:38 +00:00
|
|
|
final boolean voiceKeyMatched = matchBoolean(a,
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_Case_hasVoiceKey, id.mHasVoiceKey);
|
2010-11-22 00:40:38 +00:00
|
|
|
final boolean colorSchemeMatched = matchInteger(viewAttr,
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.KeyboardView_colorScheme, id.mColorScheme);
|
2011-02-18 05:16:29 +00:00
|
|
|
// As noted at {@link KeyboardId} class, we are interested only in enum value masked by
|
|
|
|
// {@link android.view.inputmethod.EditorInfo#IME_MASK_ACTION} and
|
|
|
|
// {@link android.view.inputmethod.EditorInfo#IME_FLAG_NO_ENTER_ACTION}. So matching
|
2010-11-20 00:04:52 +00:00
|
|
|
// this attribute with id.mImeOptions as integer value is enough for our purpose.
|
2011-02-18 05:16:29 +00:00
|
|
|
final boolean imeActionMatched = matchInteger(a,
|
|
|
|
R.styleable.Keyboard_Case_imeAction, id.mImeAction);
|
2011-02-18 06:33:44 +00:00
|
|
|
final boolean languageCodeMatched = matchString(a,
|
|
|
|
R.styleable.Keyboard_Case_languageCode, id.mLocale.getLanguage());
|
2011-02-23 08:09:24 +00:00
|
|
|
final boolean countryCodeMatched = matchString(a,
|
|
|
|
R.styleable.Keyboard_Case_countryCode, id.mLocale.getCountry());
|
2011-02-20 03:54:14 +00:00
|
|
|
final boolean selected = modeMatched && passwordInputMatched && settingsKeyMatched
|
|
|
|
&& voiceEnabledMatched && voiceKeyMatched && colorSchemeMatched
|
2011-02-23 08:09:24 +00:00
|
|
|
&& imeActionMatched && languageCodeMatched && countryCodeMatched;
|
2010-11-20 02:45:30 +00:00
|
|
|
|
2011-02-23 08:09:24 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s%s%s%s%s%s%s%s%s%s> %s", TAG_CASE,
|
2010-12-14 06:31:47 +00:00
|
|
|
textAttr(KeyboardId.modeName(
|
|
|
|
a.getInt(R.styleable.Keyboard_Case_mode, -1)), "mode"),
|
|
|
|
textAttr(KeyboardId.colorSchemeName(
|
2011-03-03 05:34:11 +00:00
|
|
|
viewAttr.getInt(
|
|
|
|
R.styleable.KeyboardView_colorScheme, -1)), "colorSchemeName"),
|
2011-02-20 03:54:14 +00:00
|
|
|
booleanAttr(a, R.styleable.Keyboard_Case_passwordInput, "passwordInput"),
|
2010-12-14 06:31:47 +00:00
|
|
|
booleanAttr(a, R.styleable.Keyboard_Case_hasSettingsKey, "hasSettingsKey"),
|
|
|
|
booleanAttr(a, R.styleable.Keyboard_Case_voiceKeyEnabled, "voiceKeyEnabled"),
|
|
|
|
booleanAttr(a, R.styleable.Keyboard_Case_hasVoiceKey, "hasVoiceKey"),
|
2011-03-25 19:40:57 +00:00
|
|
|
textAttr(EditorInfoCompatUtils.imeOptionsName(
|
2011-02-18 05:16:29 +00:00
|
|
|
a.getInt(R.styleable.Keyboard_Case_imeAction, -1)), "imeAction"),
|
2011-02-18 06:33:44 +00:00
|
|
|
textAttr(a.getString(R.styleable.Keyboard_Case_languageCode), "languageCode"),
|
2011-02-23 08:09:24 +00:00
|
|
|
textAttr(a.getString(R.styleable.Keyboard_Case_countryCode), "countryCode"),
|
2010-12-14 06:31:47 +00:00
|
|
|
Boolean.toString(selected)));
|
2010-11-20 02:45:30 +00:00
|
|
|
|
|
|
|
return selected;
|
|
|
|
} finally {
|
|
|
|
a.recycle();
|
2010-11-22 00:40:38 +00:00
|
|
|
viewAttr.recycle();
|
2010-11-11 23:28:14 +00:00
|
|
|
}
|
2010-11-20 02:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean matchInteger(TypedArray a, int index, int value) {
|
|
|
|
// If <case> does not have "index" attribute, that means this <case> is wild-card for the
|
|
|
|
// attribute.
|
|
|
|
return !a.hasValue(index) || a.getInt(index, 0) == value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean matchBoolean(TypedArray a, int index, boolean value) {
|
|
|
|
// If <case> does not have "index" attribute, that means this <case> is wild-card for the
|
|
|
|
// attribute.
|
|
|
|
return !a.hasValue(index) || a.getBoolean(index, false) == value;
|
2010-11-11 23:28:14 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 06:33:44 +00:00
|
|
|
private static boolean matchString(TypedArray a, int index, String value) {
|
|
|
|
// If <case> does not have "index" attribute, that means this <case> is wild-card for the
|
|
|
|
// attribute.
|
|
|
|
return !a.hasValue(index) || a.getString(index).equals(value);
|
|
|
|
}
|
|
|
|
|
2010-11-11 23:28:14 +00:00
|
|
|
private boolean parseDefault(XmlResourceParser parser, Row row, List<Key> keys)
|
|
|
|
throws XmlPullParserException, IOException {
|
2010-12-14 06:31:47 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s>", TAG_DEFAULT));
|
2010-11-11 23:28:14 +00:00
|
|
|
if (row == null) {
|
|
|
|
parseKeyboardContent(parser, keys);
|
|
|
|
} else {
|
|
|
|
parseRowContent(parser, row, keys);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-10 06:24:28 +00:00
|
|
|
private void parseKeyStyle(XmlResourceParser parser, List<Key> keys) {
|
2010-12-14 06:31:47 +00:00
|
|
|
TypedArray keyStyleAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_KeyStyle);
|
2010-11-19 22:57:24 +00:00
|
|
|
TypedArray keyAttrs = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_Key);
|
2010-11-19 22:57:24 +00:00
|
|
|
try {
|
2010-12-14 06:31:47 +00:00
|
|
|
if (!keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_styleName))
|
2010-11-19 22:57:24 +00:00
|
|
|
throw new ParseException("<" + TAG_KEY_STYLE
|
|
|
|
+ "/> needs styleName attribute", parser);
|
|
|
|
if (keys != null)
|
2010-12-14 06:31:47 +00:00
|
|
|
mKeyStyles.parseKeyStyleAttributes(keyStyleAttr, keyAttrs, parser);
|
2010-11-19 22:57:24 +00:00
|
|
|
} finally {
|
2010-12-14 06:31:47 +00:00
|
|
|
keyStyleAttr.recycle();
|
2010-11-19 22:57:24 +00:00
|
|
|
keyAttrs.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private static void checkEndTag(String tag, XmlResourceParser parser)
|
2010-10-21 10:05:53 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2010-12-10 06:24:28 +00:00
|
|
|
if (parser.next() == XmlPullParser.END_TAG && tag.equals(parser.getName()))
|
2010-10-21 16:24:23 +00:00
|
|
|
return;
|
|
|
|
throw new NonEmptyTag(tag, parser);
|
|
|
|
}
|
|
|
|
|
2010-11-11 23:28:14 +00:00
|
|
|
private void startRow(Row row) {
|
|
|
|
mCurrentX = 0;
|
|
|
|
mCurrentRow = row;
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private void endRow() {
|
|
|
|
if (mCurrentRow == null)
|
|
|
|
throw new InflateException("orphant end row tag");
|
2010-12-08 05:36:41 +00:00
|
|
|
mCurrentY += mCurrentRow.mDefaultHeight;
|
2010-10-21 16:24:23 +00:00
|
|
|
mCurrentRow = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void endKey(Key key) {
|
2010-12-02 11:54:32 +00:00
|
|
|
mCurrentX += key.mGap + key.mWidth;
|
2010-10-21 16:24:23 +00:00
|
|
|
if (mCurrentX > mMaxRowWidth)
|
|
|
|
mMaxRowWidth = mCurrentX;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void endKeyboard(int defaultVerticalGap) {
|
|
|
|
mTotalHeight = mCurrentY - defaultVerticalGap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setSpacer(int gap) {
|
|
|
|
mCurrentX += gap;
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static int getDimensionOrFraction(TypedArray a, int index, int base, int defValue) {
|
|
|
|
final TypedValue value = a.peekValue(index);
|
|
|
|
if (value == null)
|
|
|
|
return defValue;
|
|
|
|
if (value.type == TypedValue.TYPE_DIMENSION) {
|
|
|
|
return a.getDimensionPixelOffset(index, defValue);
|
|
|
|
} else if (value.type == TypedValue.TYPE_FRACTION) {
|
|
|
|
// Round it to avoid values like 47.9999 from getting truncated
|
|
|
|
return Math.round(a.getFraction(index, base, base, defValue));
|
|
|
|
}
|
|
|
|
return defValue;
|
|
|
|
}
|
2010-10-21 16:24:23 +00:00
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
2010-11-19 22:57:24 +00:00
|
|
|
public static class ParseException extends InflateException {
|
2010-10-21 16:24:23 +00:00
|
|
|
public ParseException(String msg, XmlResourceParser parser) {
|
|
|
|
super(msg + " at line " + parser.getLineNumber());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
private static class IllegalStartTag extends ParseException {
|
|
|
|
public IllegalStartTag(XmlResourceParser parser, String parent) {
|
|
|
|
super("Illegal start tag " + parser.getName() + " in " + parent, parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
private static class IllegalEndTag extends ParseException {
|
|
|
|
public IllegalEndTag(XmlResourceParser parser, String parent) {
|
|
|
|
super("Illegal end tag " + parser.getName() + " in " + parent, parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
private static class NonEmptyTag extends ParseException {
|
|
|
|
public NonEmptyTag(String tag, XmlResourceParser parser) {
|
|
|
|
super(tag + " must be empty tag", parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
private static String textAttr(String value, String name) {
|
|
|
|
return value != null ? String.format(" %s=%s", name, value) : "";
|
2010-11-20 02:45:30 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
private static String booleanAttr(TypedArray a, int index, String name) {
|
|
|
|
return a.hasValue(index) ? String.format(" %s=%s", name, a.getBoolean(index, false)) : "";
|
2010-11-20 02:45:30 +00:00
|
|
|
}
|
|
|
|
}
|