2010-10-21 10:05:53 +00:00
|
|
|
/*
|
2011-05-20 03:09:57 +00:00
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2010-10-21 10:05:53 +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-10-21 10:05:53 +00:00
|
|
|
|
2011-04-21 06:35:07 +00:00
|
|
|
import android.content.Context;
|
2010-10-21 10:05:53 +00:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.content.res.XmlResourceParser;
|
2011-07-29 00:05:40 +00:00
|
|
|
import android.util.DisplayMetrics;
|
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;
|
|
|
|
|
2011-06-15 04:38:58 +00:00
|
|
|
import com.android.inputmethod.compat.EditorInfoCompatUtils;
|
2011-06-23 12:23:44 +00:00
|
|
|
import com.android.inputmethod.keyboard.Key;
|
2011-06-22 02:53:02 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
|
|
|
import com.android.inputmethod.keyboard.KeyboardId;
|
2011-06-15 04:38:58 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
2010-10-21 10:05:53 +00:00
|
|
|
import java.io.IOException;
|
2010-12-14 06:31:47 +00:00
|
|
|
import java.util.Arrays;
|
2010-10-21 10:05:53 +00:00
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
/**
|
2011-08-02 21:35:18 +00:00
|
|
|
* Keyboard Building helper.
|
2010-10-21 16:24:23 +00:00
|
|
|
*
|
2011-08-02 21:35:18 +00:00
|
|
|
* This class parses Keyboard XML file and eventually build a 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<
|
2011-06-22 13:45:03 +00:00
|
|
|
* >case mode="email"<
|
|
|
|
* >key-style styleName="f1-key" parentStyle="modifier-key"
|
|
|
|
* keyLabel=".com"
|
2010-11-19 22:57:24 +00:00
|
|
|
* /<
|
2010-11-11 23:28:14 +00:00
|
|
|
* >/case<
|
2011-06-22 13:45:03 +00:00
|
|
|
* >case mode="url"<
|
|
|
|
* >key-style styleName="f1-key" parentStyle="modifier-key"
|
|
|
|
* keyLabel="http://"
|
2010-11-19 22:57:24 +00:00
|
|
|
* /<
|
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
|
|
|
|
2011-08-02 21:35:18 +00:00
|
|
|
public class KeyboardBuilder<KP extends KeyboardParams> {
|
|
|
|
private static final String TAG = KeyboardBuilder.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
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
protected final KP mParams;
|
|
|
|
protected final Context mContext;
|
|
|
|
protected final Resources mResources;
|
|
|
|
private final DisplayMetrics mDisplayMetrics;
|
2010-10-21 10:05:53 +00:00
|
|
|
|
|
|
|
private int mCurrentY = 0;
|
|
|
|
private Row mCurrentRow = null;
|
2011-07-19 00:14:50 +00:00
|
|
|
private boolean mLeftEdge;
|
2011-08-16 09:33:40 +00:00
|
|
|
private boolean mTopEdge;
|
2011-07-19 00:14:50 +00:00
|
|
|
private Key mRightEdgeKey = null;
|
2010-11-19 22:57:24 +00:00
|
|
|
private final KeyStyles mKeyStyles = new KeyStyles();
|
2010-10-21 10:05:53 +00:00
|
|
|
|
2011-09-02 08:38:59 +00:00
|
|
|
/**
|
|
|
|
* Container for keys in the keyboard. All keys in a row are at the same Y-coordinate.
|
|
|
|
* Some of the key size defaults can be overridden per row from what the {@link Keyboard}
|
|
|
|
* defines.
|
|
|
|
*/
|
|
|
|
public static class Row {
|
|
|
|
/** Default width of a key in this row. */
|
|
|
|
public final float mDefaultKeyWidth;
|
|
|
|
/** Default height of a key in this row. */
|
|
|
|
public final int mRowHeight;
|
|
|
|
|
|
|
|
public final int mCurrentY;
|
|
|
|
// Will be updated by {@link Key}'s constructor.
|
|
|
|
public float mCurrentX;
|
|
|
|
|
|
|
|
public Row(Resources res, KeyboardParams params, XmlResourceParser parser, int y) {
|
|
|
|
final int keyboardWidth = params.mWidth;
|
|
|
|
final int keyboardHeight = params.mHeight;
|
|
|
|
TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser),
|
|
|
|
R.styleable.Keyboard);
|
|
|
|
mDefaultKeyWidth = KeyboardBuilder.getDimensionOrFraction(a,
|
|
|
|
R.styleable.Keyboard_keyWidth, keyboardWidth, params.mDefaultKeyWidth);
|
|
|
|
mRowHeight = (int)KeyboardBuilder.getDimensionOrFraction(a,
|
|
|
|
R.styleable.Keyboard_rowHeight, keyboardHeight, params.mDefaultRowHeight);
|
|
|
|
a.recycle();
|
|
|
|
|
|
|
|
mCurrentY = y;
|
|
|
|
mCurrentX = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-02 21:35:18 +00:00
|
|
|
public KeyboardBuilder(Context context, KP params) {
|
2011-06-17 02:55:42 +00:00
|
|
|
mContext = context;
|
|
|
|
final Resources res = context.getResources();
|
2010-10-21 10:05:53 +00:00
|
|
|
mResources = res;
|
2011-07-29 00:05:40 +00:00
|
|
|
mDisplayMetrics = res.getDisplayMetrics();
|
|
|
|
|
|
|
|
mParams = params;
|
|
|
|
mParams.mHorizontalEdgesPadding = (int)res.getDimension(
|
|
|
|
R.dimen.keyboard_horizontal_edges_padding);
|
|
|
|
|
|
|
|
mParams.GRID_WIDTH = res.getInteger(R.integer.config_keyboard_grid_width);
|
|
|
|
mParams.GRID_HEIGHT = res.getInteger(R.integer.config_keyboard_grid_height);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-08-02 21:35:18 +00:00
|
|
|
public KeyboardBuilder<KP> load(KeyboardId id) {
|
2011-07-29 00:05:40 +00:00
|
|
|
mParams.mId = id;
|
|
|
|
try {
|
|
|
|
parseKeyboard(id.getXmlId());
|
|
|
|
} catch (XmlPullParserException e) {
|
|
|
|
Log.w(TAG, "keyboard XML parse error: " + e);
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, "keyboard XML parse error: " + e);
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
return this;
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
public Keyboard build() {
|
|
|
|
return new Keyboard(mParams);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseKeyboard(int resId) throws XmlPullParserException, IOException {
|
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s> %s", TAG_KEYBOARD, mParams.mId));
|
2010-12-14 06:31:47 +00:00
|
|
|
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);
|
2011-04-08 05:45:19 +00:00
|
|
|
startKeyboard();
|
2011-07-29 00:05:40 +00:00
|
|
|
parseKeyboardContent(parser, false);
|
2010-10-21 16:24:23 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
throw new IllegalStartTag(parser, TAG_KEYBOARD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-21 06:35:07 +00:00
|
|
|
public static String parseKeyboardLocale(
|
|
|
|
Context context, int resId) throws XmlPullParserException, IOException {
|
|
|
|
final Resources res = context.getResources();
|
|
|
|
final XmlResourceParser parser = res.getXml(resId);
|
|
|
|
if (parser == null) return "";
|
|
|
|
int event;
|
|
|
|
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
|
|
|
|
if (event == XmlPullParser.START_TAG) {
|
|
|
|
final String tag = parser.getName();
|
|
|
|
if (TAG_KEYBOARD.equals(tag)) {
|
|
|
|
final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
|
|
|
|
R.styleable.Keyboard);
|
|
|
|
return keyboardAttr.getString(R.styleable.Keyboard_keyboardLocale);
|
|
|
|
} else {
|
|
|
|
throw new IllegalStartTag(parser, TAG_KEYBOARD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
private void parseKeyboardAttributes(XmlResourceParser parser) {
|
2011-07-29 00:05:40 +00:00
|
|
|
final int displayWidth = mDisplayMetrics.widthPixels;
|
2011-06-17 02:55:42 +00:00
|
|
|
final TypedArray keyboardAttr = mContext.obtainStyledAttributes(
|
|
|
|
Xml.asAttributeSet(parser), R.styleable.Keyboard, R.attr.keyboardStyle,
|
|
|
|
R.style.Keyboard);
|
2010-12-14 06:31:47 +00:00
|
|
|
final TypedArray keyAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
|
|
|
R.styleable.Keyboard_Key);
|
|
|
|
try {
|
2011-07-29 00:05:40 +00:00
|
|
|
final int displayHeight = mDisplayMetrics.heightPixels;
|
2010-12-14 06:31:47 +00:00
|
|
|
final int keyboardHeight = (int)keyboardAttr.getDimension(
|
|
|
|
R.styleable.Keyboard_keyboardHeight, displayHeight / 2);
|
2011-09-02 08:05:24 +00:00
|
|
|
final int maxKeyboardHeight = (int)getDimensionOrFraction(keyboardAttr,
|
2011-05-12 05:49:18 +00:00
|
|
|
R.styleable.Keyboard_maxKeyboardHeight, displayHeight, displayHeight / 2);
|
2011-09-02 08:05:24 +00:00
|
|
|
int minKeyboardHeight = (int)getDimensionOrFraction(keyboardAttr,
|
2011-05-12 05:49:18 +00:00
|
|
|
R.styleable.Keyboard_minKeyboardHeight, displayHeight, displayHeight / 2);
|
|
|
|
if (minKeyboardHeight < 0) {
|
|
|
|
// Specified fraction was negative, so it should be calculated against display
|
|
|
|
// width.
|
2011-09-02 08:05:24 +00:00
|
|
|
minKeyboardHeight = -(int)getDimensionOrFraction(keyboardAttr,
|
2011-05-12 05:49:18 +00:00
|
|
|
R.styleable.Keyboard_minKeyboardHeight, displayWidth, displayWidth / 2);
|
|
|
|
}
|
|
|
|
// Keyboard height will not exceed maxKeyboardHeight and will not be less than
|
|
|
|
// minKeyboardHeight.
|
2011-07-29 00:05:40 +00:00
|
|
|
mParams.mOccupiedHeight = Math.max(
|
2011-05-12 05:49:18 +00:00
|
|
|
Math.min(keyboardHeight, maxKeyboardHeight), minKeyboardHeight);
|
2011-07-29 00:05:40 +00:00
|
|
|
mParams.mOccupiedWidth = mParams.mId.mWidth;
|
2011-09-02 08:05:24 +00:00
|
|
|
mParams.mTopPadding = (int)getDimensionOrFraction(keyboardAttr,
|
2011-07-29 00:05:40 +00:00
|
|
|
R.styleable.Keyboard_keyboardTopPadding, mParams.mOccupiedHeight, 0);
|
2011-09-02 08:05:24 +00:00
|
|
|
mParams.mBottomPadding = (int)getDimensionOrFraction(keyboardAttr,
|
2011-07-29 00:05:40 +00:00
|
|
|
R.styleable.Keyboard_keyboardBottomPadding, mParams.mOccupiedHeight, 0);
|
|
|
|
|
|
|
|
final int height = mParams.mOccupiedHeight;
|
|
|
|
final int width = mParams.mOccupiedWidth - mParams.mHorizontalEdgesPadding * 2
|
|
|
|
- mParams.mHorizontalCenterPadding;
|
|
|
|
mParams.mHeight = height;
|
|
|
|
mParams.mWidth = width;
|
2011-09-02 08:05:24 +00:00
|
|
|
mParams.mDefaultKeyWidth = (int)getDimensionOrFraction(keyboardAttr,
|
2011-07-29 00:05:40 +00:00
|
|
|
R.styleable.Keyboard_keyWidth, width, width / 10);
|
2011-09-02 08:05:24 +00:00
|
|
|
mParams.mDefaultRowHeight = (int)getDimensionOrFraction(keyboardAttr,
|
2011-07-29 00:05:40 +00:00
|
|
|
R.styleable.Keyboard_rowHeight, height, height / 4);
|
2011-09-02 08:05:24 +00:00
|
|
|
mParams.mHorizontalGap = (int)getDimensionOrFraction(keyboardAttr,
|
2011-07-29 00:05:40 +00:00
|
|
|
R.styleable.Keyboard_horizontalGap, width, 0);
|
2011-09-02 08:05:24 +00:00
|
|
|
mParams.mVerticalGap = (int)getDimensionOrFraction(keyboardAttr,
|
2011-07-29 00:05:40 +00:00
|
|
|
R.styleable.Keyboard_verticalGap, height, 0);
|
|
|
|
|
2011-08-02 21:35:18 +00:00
|
|
|
mParams.mIsRtlKeyboard = keyboardAttr.getBoolean(
|
|
|
|
R.styleable.Keyboard_isRtlKeyboard, false);
|
2011-08-31 06:26:32 +00:00
|
|
|
mParams.mMoreKeysTemplate = keyboardAttr.getResourceId(
|
|
|
|
R.styleable.Keyboard_moreKeysTemplate, 0);
|
2011-08-31 05:22:53 +00:00
|
|
|
mParams.mMaxMiniKeyboardColumn = keyAttr.getInt(
|
2011-08-31 06:26:32 +00:00
|
|
|
R.styleable.Keyboard_Key_maxMoreKeysColumn, 5);
|
2011-07-29 00:05:40 +00:00
|
|
|
|
|
|
|
mParams.mIconsSet.loadIcons(keyboardAttr);
|
2010-12-14 06:31:47 +00:00
|
|
|
} finally {
|
|
|
|
keyAttr.recycle();
|
|
|
|
keyboardAttr.recycle();
|
|
|
|
}
|
2010-10-21 16:24:23 +00:00
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseKeyboardContent(XmlResourceParser parser, boolean skip)
|
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)) {
|
2011-08-02 00:02:47 +00:00
|
|
|
Row row = parseRowAttributes(parser);
|
2010-12-14 06:31:47 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s>", TAG_ROW));
|
2011-07-29 00:05:40 +00:00
|
|
|
if (!skip)
|
2010-11-11 23:28:14 +00:00
|
|
|
startRow(row);
|
2011-07-29 00:05:40 +00:00
|
|
|
parseRowContent(parser, row, skip);
|
2010-10-21 10:05:53 +00:00
|
|
|
} else if (TAG_INCLUDE.equals(tag)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseIncludeKeyboardContent(parser, skip);
|
2010-11-11 23:28:14 +00:00
|
|
|
} else if (TAG_SWITCH.equals(tag)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseSwitchKeyboardContent(parser, skip);
|
2010-11-19 22:57:24 +00:00
|
|
|
} else if (TAG_KEY_STYLE.equals(tag)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseKeyStyle(parser, skip);
|
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)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
endKeyboard();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-02 00:02:47 +00:00
|
|
|
private Row parseRowAttributes(XmlResourceParser parser) {
|
|
|
|
final TypedArray a = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
|
|
|
R.styleable.Keyboard);
|
|
|
|
try {
|
|
|
|
if (a.hasValue(R.styleable.Keyboard_horizontalGap))
|
|
|
|
throw new IllegalAttribute(parser, "horizontalGap");
|
|
|
|
if (a.hasValue(R.styleable.Keyboard_verticalGap))
|
|
|
|
throw new IllegalAttribute(parser, "verticalGap");
|
2011-09-02 04:48:27 +00:00
|
|
|
return new Row(mResources, mParams, parser, mCurrentY);
|
2011-08-02 00:02:47 +00:00
|
|
|
} finally {
|
|
|
|
a.recycle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseRowContent(XmlResourceParser parser, Row row, boolean skip)
|
2010-10-21 16:24:23 +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-10-21 16:24:23 +00:00
|
|
|
final String tag = parser.getName();
|
|
|
|
if (TAG_KEY.equals(tag)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseKey(parser, row, skip);
|
2010-10-21 16:24:23 +00:00
|
|
|
} else if (TAG_SPACER.equals(tag)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseSpacer(parser, row, skip);
|
2010-10-21 16:24:23 +00:00
|
|
|
} else if (TAG_INCLUDE.equals(tag)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseIncludeRowContent(parser, row, skip);
|
2010-11-11 23:28:14 +00:00
|
|
|
} else if (TAG_SWITCH.equals(tag)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseSwitchRowContent(parser, row, skip);
|
2010-11-19 22:57:24 +00:00
|
|
|
} else if (TAG_KEY_STYLE.equals(tag)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseKeyStyle(parser, skip);
|
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));
|
2011-07-29 00:05:40 +00:00
|
|
|
if (!skip)
|
2011-09-02 04:48:27 +00:00
|
|
|
endRow(row);
|
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 16:24:23 +00:00
|
|
|
} else {
|
|
|
|
throw new IllegalEndTag(parser, TAG_KEY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseKey(XmlResourceParser parser, Row row, boolean skip)
|
2010-10-21 16:24:23 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2011-07-29 00:05:40 +00:00
|
|
|
if (skip) {
|
2010-10-21 16:24:23 +00:00
|
|
|
checkEndTag(TAG_KEY, parser);
|
|
|
|
} else {
|
2011-09-02 04:48:27 +00:00
|
|
|
Key key = new Key(mResources, mParams, row, parser, mKeyStyles);
|
2011-08-31 06:26:32 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s%s keyLabel=%s code=%d moreKeys=%s />",
|
2011-06-23 12:23:44 +00:00
|
|
|
TAG_KEY, (key.isEnabled() ? "" : " disabled"), key.mLabel, key.mCode,
|
2011-08-31 06:26:32 +00:00
|
|
|
Arrays.toString(key.mMoreKeys)));
|
2010-10-21 16:24:23 +00:00
|
|
|
checkEndTag(TAG_KEY, parser);
|
2011-07-29 00:05:40 +00:00
|
|
|
mParams.onAddKey(key);
|
2010-10-21 16:24:23 +00:00
|
|
|
endKey(key);
|
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseSpacer(XmlResourceParser parser, Row row, boolean skip)
|
2010-10-21 16:24:23 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2011-07-29 00:05:40 +00:00
|
|
|
if (skip) {
|
2010-10-21 16:24:23 +00:00
|
|
|
checkEndTag(TAG_SPACER, parser);
|
|
|
|
} else {
|
2010-12-14 06:31:47 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s />", TAG_SPACER));
|
2011-05-30 11:05:50 +00:00
|
|
|
final TypedArray keyboardAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard);
|
2011-05-30 11:05:50 +00:00
|
|
|
if (keyboardAttr.hasValue(R.styleable.Keyboard_horizontalGap))
|
|
|
|
throw new IllegalAttribute(parser, "horizontalGap");
|
2011-07-29 00:05:40 +00:00
|
|
|
final int keyboardWidth = mParams.mWidth;
|
2011-09-02 08:05:24 +00:00
|
|
|
final float keyWidth = getDimensionOrFraction(keyboardAttr,
|
|
|
|
R.styleable.Keyboard_keyWidth, keyboardWidth, row.mDefaultKeyWidth);
|
2011-05-30 11:05:50 +00:00
|
|
|
keyboardAttr.recycle();
|
|
|
|
|
|
|
|
final TypedArray keyAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser),
|
|
|
|
R.styleable.Keyboard_Key);
|
2011-09-02 08:05:24 +00:00
|
|
|
float keyXPos = getDimensionOrFraction(keyAttr,
|
2011-09-02 04:48:27 +00:00
|
|
|
R.styleable.Keyboard_Key_keyXPos, keyboardWidth, row.mCurrentX);
|
2011-05-30 11:05:50 +00:00
|
|
|
if (keyXPos < 0) {
|
|
|
|
// If keyXPos is negative, the actual x-coordinate will be display_width + keyXPos.
|
2011-06-28 07:32:39 +00:00
|
|
|
keyXPos += keyboardWidth;
|
2011-05-30 11:05:50 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
checkEndTag(TAG_SPACER, parser);
|
2011-09-02 04:48:27 +00:00
|
|
|
setSpacer(keyXPos, keyWidth, row);
|
2010-10-21 16:24:23 +00:00
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseIncludeKeyboardContent(XmlResourceParser parser, boolean skip)
|
2010-10-21 16:24:23 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseIncludeInternal(parser, null, skip);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseIncludeRowContent(XmlResourceParser parser, Row row, boolean skip)
|
2010-10-21 16:24:23 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseIncludeInternal(parser, row, skip);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseIncludeInternal(XmlResourceParser parser, Row row, boolean skip)
|
2010-10-21 16:24:23 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2011-07-29 00:05:40 +00:00
|
|
|
if (skip) {
|
2010-10-21 16:24:23 +00:00
|
|
|
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)));
|
2011-07-29 00:05:40 +00:00
|
|
|
parseMerge(mResources.getLayout(keyboardLayout), row, skip);
|
2010-10-21 16:24:23 +00:00
|
|
|
}
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseMerge(XmlResourceParser parser, Row row, boolean skip)
|
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) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseKeyboardContent(parser, skip);
|
2010-10-21 16:24:23 +00:00
|
|
|
} else {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseRowContent(parser, row, skip);
|
2010-10-21 16:24:23 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseSwitchKeyboardContent(XmlResourceParser parser, boolean skip)
|
2010-11-11 23:28:14 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseSwitchInternal(parser, null, skip);
|
2010-11-11 23:28:14 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseSwitchRowContent(XmlResourceParser parser, Row row, boolean skip)
|
2010-11-11 23:28:14 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseSwitchInternal(parser, row, skip);
|
2010-11-11 23:28:14 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseSwitchInternal(XmlResourceParser parser, Row row, boolean skip)
|
2010-11-11 23:28:14 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
2011-07-29 00:05:40 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s> %s", TAG_SWITCH, mParams.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)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
selected |= parseCase(parser, row, selected ? true : skip);
|
2010-11-11 23:28:14 +00:00
|
|
|
} else if (TAG_DEFAULT.equals(tag)) {
|
2011-07-29 00:05:40 +00:00
|
|
|
selected |= parseDefault(parser, row, selected ? true : skip);
|
2010-11-11 23:28:14 +00:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private boolean parseCase(XmlResourceParser parser, Row row, boolean skip)
|
2010-11-11 23:28:14 +00:00
|
|
|
throws XmlPullParserException, IOException {
|
|
|
|
final boolean selected = parseCaseCondition(parser);
|
|
|
|
if (row == null) {
|
|
|
|
// Processing Rows.
|
2011-07-29 00:05:40 +00:00
|
|
|
parseKeyboardContent(parser, selected ? skip : true);
|
2010-11-11 23:28:14 +00:00
|
|
|
} else {
|
|
|
|
// Processing Keys.
|
2011-07-29 00:05:40 +00:00
|
|
|
parseRowContent(parser, row, selected ? skip : true);
|
2010-11-11 23:28:14 +00:00
|
|
|
}
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean parseCaseCondition(XmlResourceParser parser) {
|
2011-07-29 00:05:40 +00:00
|
|
|
final KeyboardId id = mParams.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 {
|
2011-06-02 17:18:57 +00:00
|
|
|
final boolean modeMatched = matchTypedValue(a,
|
|
|
|
R.styleable.Keyboard_Case_mode, id.mMode, KeyboardId.modeName(id.mMode));
|
2011-06-17 09:25:54 +00:00
|
|
|
final boolean navigateActionMatched = matchBoolean(a,
|
|
|
|
R.styleable.Keyboard_Case_navigateAction, id.mNavigateAction);
|
2011-02-20 03:54:14 +00:00
|
|
|
final boolean passwordInputMatched = matchBoolean(a,
|
|
|
|
R.styleable.Keyboard_Case_passwordInput, id.mPasswordInput);
|
2011-06-23 12:55:56 +00:00
|
|
|
final boolean hasSettingsKeyMatched = matchBoolean(a,
|
2010-12-02 09:46:21 +00:00
|
|
|
R.styleable.Keyboard_Case_hasSettingsKey, id.mHasSettingsKey);
|
2011-06-23 12:55:56 +00:00
|
|
|
final boolean f2KeyModeMatched = matchInteger(a,
|
|
|
|
R.styleable.Keyboard_Case_f2KeyMode, id.mF2KeyMode);
|
|
|
|
final boolean clobberSettingsKeyMatched = matchBoolean(a,
|
|
|
|
R.styleable.Keyboard_Case_clobberSettingsKey, id.mClobberSettingsKey);
|
2011-08-01 01:34:54 +00:00
|
|
|
final boolean shortcutKeyEnabledMatched = matchBoolean(a,
|
|
|
|
R.styleable.Keyboard_Case_shortcutKeyEnabled, id.mShortcutKeyEnabled);
|
|
|
|
final boolean hasShortcutKeyMatched = matchBoolean(a,
|
|
|
|
R.styleable.Keyboard_Case_hasShortcutKey, id.mHasShortcutKey);
|
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-06-02 17:18:57 +00:00
|
|
|
final boolean localeCodeMatched = matchString(a,
|
|
|
|
R.styleable.Keyboard_Case_localeCode, id.mLocale.toString());
|
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-06-17 09:25:54 +00:00
|
|
|
final boolean selected = modeMatched && navigateActionMatched && passwordInputMatched
|
2011-06-23 12:55:56 +00:00
|
|
|
&& hasSettingsKeyMatched && f2KeyModeMatched && clobberSettingsKeyMatched
|
2011-08-01 01:34:54 +00:00
|
|
|
&& shortcutKeyEnabledMatched && hasShortcutKeyMatched && imeActionMatched &&
|
2011-06-23 12:55:56 +00:00
|
|
|
localeCodeMatched && languageCodeMatched && countryCodeMatched;
|
2010-11-20 02:45:30 +00:00
|
|
|
|
2011-06-23 12:55:56 +00:00
|
|
|
if (DEBUG) Log.d(TAG, String.format("<%s%s%s%s%s%s%s%s%s%s%s%s%s> %s", TAG_CASE,
|
2011-06-02 17:18:57 +00:00
|
|
|
textAttr(a.getString(R.styleable.Keyboard_Case_mode), "mode"),
|
2011-06-17 09:25:54 +00:00
|
|
|
booleanAttr(a, R.styleable.Keyboard_Case_navigateAction, "navigateAction"),
|
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"),
|
2011-06-23 12:55:56 +00:00
|
|
|
textAttr(KeyboardId.f2KeyModeName(
|
|
|
|
a.getInt(R.styleable.Keyboard_Case_f2KeyMode, -1)), "f2KeyMode"),
|
|
|
|
booleanAttr(a, R.styleable.Keyboard_Case_clobberSettingsKey,
|
|
|
|
"clobberSettingsKey"),
|
2011-08-01 01:34:54 +00:00
|
|
|
booleanAttr(
|
|
|
|
a, R.styleable.Keyboard_Case_shortcutKeyEnabled, "shortcutKeyEnabled"),
|
|
|
|
booleanAttr(a, R.styleable.Keyboard_Case_hasShortcutKey, "hasShortcutKey"),
|
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-06-02 17:18:57 +00:00
|
|
|
textAttr(a.getString(R.styleable.Keyboard_Case_localeCode), "localeCode"),
|
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.
|
2011-06-02 17:18:57 +00:00
|
|
|
return !a.hasValue(index) || stringArrayContains(a.getString(index).split("\\|"), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean matchTypedValue(TypedArray a, int index, int intValue, String strValue) {
|
|
|
|
// If <case> does not have "index" attribute, that means this <case> is wild-card for the
|
|
|
|
// attribute.
|
|
|
|
final TypedValue v = a.peekValue(index);
|
|
|
|
if (v == null)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (isIntegerValue(v)) {
|
|
|
|
return intValue == a.getInt(index, 0);
|
|
|
|
} else if (isStringValue(v)) {
|
|
|
|
return stringArrayContains(a.getString(index).split("\\|"), strValue);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean stringArrayContains(String[] array, String value) {
|
|
|
|
for (final String elem : array) {
|
|
|
|
if (elem.equals(value))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-02-18 06:33:44 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private boolean parseDefault(XmlResourceParser parser, Row row, boolean skip)
|
2010-11-11 23:28:14 +00:00
|
|
|
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) {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseKeyboardContent(parser, skip);
|
2010-11-11 23:28:14 +00:00
|
|
|
} else {
|
2011-07-29 00:05:40 +00:00
|
|
|
parseRowContent(parser, row, skip);
|
2010-11-11 23:28:14 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void parseKeyStyle(XmlResourceParser parser, boolean skip) {
|
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);
|
2011-07-29 00:05:40 +00:00
|
|
|
if (!skip)
|
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);
|
|
|
|
}
|
|
|
|
|
2011-04-08 05:45:19 +00:00
|
|
|
private void startKeyboard() {
|
2011-07-29 00:05:40 +00:00
|
|
|
mCurrentY += mParams.mTopPadding;
|
2011-08-16 09:33:40 +00:00
|
|
|
mTopEdge = true;
|
2011-04-08 05:45:19 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 23:28:14 +00:00
|
|
|
private void startRow(Row row) {
|
2011-09-02 04:48:27 +00:00
|
|
|
row.mCurrentX = 0;
|
|
|
|
setSpacer(row.mCurrentX, mParams.mHorizontalEdgesPadding, row);
|
2010-11-11 23:28:14 +00:00
|
|
|
mCurrentRow = row;
|
2011-07-19 00:14:50 +00:00
|
|
|
mLeftEdge = true;
|
|
|
|
mRightEdgeKey = null;
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 04:48:27 +00:00
|
|
|
private void endRow(Row row) {
|
2010-10-21 16:24:23 +00:00
|
|
|
if (mCurrentRow == null)
|
|
|
|
throw new InflateException("orphant end row tag");
|
2011-07-21 21:32:16 +00:00
|
|
|
if (mRightEdgeKey != null) {
|
|
|
|
mRightEdgeKey.addEdgeFlags(Keyboard.EDGE_RIGHT);
|
|
|
|
mRightEdgeKey = null;
|
|
|
|
}
|
2011-09-02 04:48:27 +00:00
|
|
|
setSpacer(row.mCurrentX, mParams.mHorizontalEdgesPadding, row);
|
2011-07-29 00:05:40 +00:00
|
|
|
mCurrentY += mCurrentRow.mRowHeight;
|
2010-10-21 16:24:23 +00:00
|
|
|
mCurrentRow = null;
|
2011-08-16 09:33:40 +00:00
|
|
|
mTopEdge = false;
|
2010-10-21 16:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void endKey(Key key) {
|
2011-07-19 00:14:50 +00:00
|
|
|
if (mLeftEdge) {
|
|
|
|
key.addEdgeFlags(Keyboard.EDGE_LEFT);
|
|
|
|
mLeftEdge = false;
|
|
|
|
}
|
2011-08-16 09:33:40 +00:00
|
|
|
if (mTopEdge) {
|
|
|
|
key.addEdgeFlags(Keyboard.EDGE_TOP);
|
|
|
|
}
|
2011-07-19 00:14:50 +00:00
|
|
|
mRightEdgeKey = key;
|
2010-10-21 16:24:23 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 00:05:40 +00:00
|
|
|
private void endKeyboard() {
|
2010-10-21 16:24:23 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 08:05:24 +00:00
|
|
|
private void setSpacer(float keyXPos, float width, Row row) {
|
2011-09-02 04:48:27 +00:00
|
|
|
row.mCurrentX = keyXPos + width;
|
2011-07-19 00:14:50 +00:00
|
|
|
mLeftEdge = false;
|
|
|
|
mRightEdgeKey = null;
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 08:05:24 +00:00
|
|
|
public static float getDimensionOrFraction(TypedArray a, int index, int base, float defValue) {
|
2010-10-21 10:05:53 +00:00
|
|
|
final TypedValue value = a.peekValue(index);
|
|
|
|
if (value == null)
|
|
|
|
return defValue;
|
2011-06-02 17:18:57 +00:00
|
|
|
if (isFractionValue(value)) {
|
2011-09-02 08:05:24 +00:00
|
|
|
return a.getFraction(index, base, base, defValue);
|
2011-06-02 17:18:57 +00:00
|
|
|
} else if (isDimensionValue(value)) {
|
2011-09-02 08:05:24 +00:00
|
|
|
return a.getDimension(index, defValue);
|
2011-06-02 17:18:57 +00:00
|
|
|
} else if (isIntegerValue(value)) {
|
2011-05-30 11:05:50 +00:00
|
|
|
// For enum value.
|
2011-09-02 08:05:24 +00:00
|
|
|
return a.getInt(index, 0);
|
2010-10-21 10:05:53 +00:00
|
|
|
}
|
|
|
|
return defValue;
|
|
|
|
}
|
2010-10-21 16:24:23 +00:00
|
|
|
|
2011-06-02 17:18:57 +00:00
|
|
|
private static boolean isFractionValue(TypedValue v) {
|
|
|
|
return v.type == TypedValue.TYPE_FRACTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isDimensionValue(TypedValue v) {
|
|
|
|
return v.type == TypedValue.TYPE_DIMENSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isIntegerValue(TypedValue v) {
|
|
|
|
return v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isStringValue(TypedValue v) {
|
|
|
|
return v.type == TypedValue.TYPE_STRING;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-30 11:05:50 +00:00
|
|
|
@SuppressWarnings("serial")
|
|
|
|
private static class IllegalAttribute extends ParseException {
|
|
|
|
public IllegalAttribute(XmlResourceParser parser, String attribute) {
|
|
|
|
super("Tag " + parser.getName() + " has illegal attribute " + attribute, parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-21 16:24:23 +00:00
|
|
|
@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
|
|
|
}
|
|
|
|
}
|