2010-12-02 09:46:21 +00:00
|
|
|
/*
|
2011-05-20 03:09:57 +00:00
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2010-12-02 09:46:21 +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-12-02 09:46:21 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2010-12-02 09:46:21 +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-12-02 09:46:21 +00:00
|
|
|
*/
|
|
|
|
|
2011-06-23 12:23:44 +00:00
|
|
|
package com.android.inputmethod.keyboard;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2012-04-19 21:24:51 +00:00
|
|
|
import static com.android.inputmethod.keyboard.internal.KeyboardIconsSet.ICON_UNDEFINED;
|
2012-10-29 05:46:34 +00:00
|
|
|
import static com.android.inputmethod.latin.Constants.CODE_OUTPUT_TEXT;
|
|
|
|
import static com.android.inputmethod.latin.Constants.CODE_SHIFT;
|
|
|
|
import static com.android.inputmethod.latin.Constants.CODE_SWITCH_ALPHA_SYMBOL;
|
|
|
|
import static com.android.inputmethod.latin.Constants.CODE_UNSPECIFIED;
|
2012-04-19 21:24:51 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.content.res.TypedArray;
|
2011-09-28 09:51:57 +00:00
|
|
|
import android.graphics.Rect;
|
2011-06-25 10:38:55 +00:00
|
|
|
import android.graphics.Typeface;
|
2010-12-02 09:46:21 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.text.TextUtils;
|
2011-12-12 03:52:42 +00:00
|
|
|
import android.util.Log;
|
2010-12-02 09:46:21 +00:00
|
|
|
import android.util.Xml;
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyDrawParams;
|
2012-02-02 15:18:47 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeySpecParser;
|
2012-08-30 05:22:40 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyStyle;
|
2012-08-30 08:42:49 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyVisualAttributes;
|
2011-08-30 09:35:56 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
|
2012-08-30 05:22:40 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyboardParams;
|
|
|
|
import com.android.inputmethod.keyboard.internal.KeyboardRow;
|
|
|
|
import com.android.inputmethod.keyboard.internal.MoreKeySpec;
|
2012-10-29 05:46:34 +00:00
|
|
|
import com.android.inputmethod.latin.Constants;
|
2011-06-21 14:38:42 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
2012-08-29 13:02:00 +00:00
|
|
|
import com.android.inputmethod.latin.ResourceUtils;
|
2012-03-08 08:07:02 +00:00
|
|
|
import com.android.inputmethod.latin.StringUtils;
|
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
|
|
|
|
2011-12-16 06:32:24 +00:00
|
|
|
import java.util.Arrays;
|
2012-05-26 04:23:34 +00:00
|
|
|
import java.util.Locale;
|
2011-01-06 13:37:39 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
/**
|
|
|
|
* Class for describing the position and characteristics of a single key in the keyboard.
|
|
|
|
*/
|
2012-09-11 10:15:00 +00:00
|
|
|
public class Key implements Comparable<Key> {
|
2011-12-12 03:52:42 +00:00
|
|
|
private static final String TAG = Key.class.getSimpleName();
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
/**
|
2010-12-20 11:30:26 +00:00
|
|
|
* The key code (unicode or custom code) that this key generates.
|
2010-12-02 09:46:21 +00:00
|
|
|
*/
|
2010-12-20 11:30:26 +00:00
|
|
|
public final int mCode;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
|
|
|
/** Label to display */
|
2012-01-25 08:01:53 +00:00
|
|
|
public final String mLabel;
|
2011-06-25 10:38:55 +00:00
|
|
|
/** Hint label to display on the key in conjunction with the label */
|
2012-01-25 08:01:53 +00:00
|
|
|
public final String mHintLabel;
|
2011-11-22 03:55:38 +00:00
|
|
|
/** Flags of the label */
|
|
|
|
private final int mLabelFlags;
|
|
|
|
private static final int LABEL_FLAGS_ALIGN_LEFT = 0x01;
|
|
|
|
private static final int LABEL_FLAGS_ALIGN_RIGHT = 0x02;
|
|
|
|
private static final int LABEL_FLAGS_ALIGN_LEFT_OF_CENTER = 0x08;
|
2012-04-11 03:34:30 +00:00
|
|
|
private static final int LABEL_FLAGS_FONT_NORMAL = 0x10;
|
|
|
|
private static final int LABEL_FLAGS_FONT_MONO_SPACE = 0x20;
|
2012-05-17 10:30:38 +00:00
|
|
|
// Start of key text ratio enum values
|
|
|
|
private static final int LABEL_FLAGS_FOLLOW_KEY_TEXT_RATIO_MASK = 0x1C0;
|
2012-04-11 03:34:30 +00:00
|
|
|
private static final int LABEL_FLAGS_FOLLOW_KEY_LARGE_LETTER_RATIO = 0x40;
|
|
|
|
private static final int LABEL_FLAGS_FOLLOW_KEY_LETTER_RATIO = 0x80;
|
|
|
|
private static final int LABEL_FLAGS_FOLLOW_KEY_LABEL_RATIO = 0xC0;
|
2012-05-17 10:30:38 +00:00
|
|
|
private static final int LABEL_FLAGS_FOLLOW_KEY_LARGE_LABEL_RATIO = 0x100;
|
|
|
|
private static final int LABEL_FLAGS_FOLLOW_KEY_HINT_LABEL_RATIO = 0x140;
|
|
|
|
// End of key text ratio mask enum values
|
2011-11-22 03:55:38 +00:00
|
|
|
private static final int LABEL_FLAGS_HAS_POPUP_HINT = 0x200;
|
2012-01-26 09:03:30 +00:00
|
|
|
private static final int LABEL_FLAGS_HAS_SHIFTED_LETTER_HINT = 0x400;
|
2011-11-22 03:55:38 +00:00
|
|
|
private static final int LABEL_FLAGS_HAS_HINT_LABEL = 0x800;
|
|
|
|
private static final int LABEL_FLAGS_WITH_ICON_LEFT = 0x1000;
|
|
|
|
private static final int LABEL_FLAGS_WITH_ICON_RIGHT = 0x2000;
|
|
|
|
private static final int LABEL_FLAGS_AUTO_X_SCALE = 0x4000;
|
2012-01-25 10:43:13 +00:00
|
|
|
private static final int LABEL_FLAGS_PRESERVE_CASE = 0x8000;
|
2012-01-26 09:03:30 +00:00
|
|
|
private static final int LABEL_FLAGS_SHIFTED_LETTER_ACTIVATED = 0x10000;
|
2012-02-07 08:07:23 +00:00
|
|
|
private static final int LABEL_FLAGS_FROM_CUSTOM_ACTION_LABEL = 0x20000;
|
2012-03-12 07:30:59 +00:00
|
|
|
private static final int LABEL_FLAGS_DISABLE_HINT_LABEL = 0x40000000;
|
|
|
|
private static final int LABEL_FLAGS_DISABLE_ADDITIONAL_MORE_KEYS = 0x80000000;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
|
|
|
/** Icon to display instead of a label. Icon takes precedence over a label */
|
2012-02-02 15:18:47 +00:00
|
|
|
private final int mIconId;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
|
|
|
/** Width of the key, not including the gap */
|
2010-12-03 04:17:26 +00:00
|
|
|
public final int mWidth;
|
2010-12-02 09:46:21 +00:00
|
|
|
/** Height of the key, not including the gap */
|
2010-12-03 04:17:26 +00:00
|
|
|
public final int mHeight;
|
2010-12-02 09:46:21 +00:00
|
|
|
/** X coordinate of the key in the keyboard layout */
|
2010-12-03 04:17:26 +00:00
|
|
|
public final int mX;
|
2010-12-02 09:46:21 +00:00
|
|
|
/** Y coordinate of the key in the keyboard layout */
|
2010-12-03 04:17:26 +00:00
|
|
|
public final int mY;
|
2011-09-28 09:51:57 +00:00
|
|
|
/** Hit bounding box of the key */
|
|
|
|
public final Rect mHitBox = new Rect();
|
|
|
|
|
2011-08-31 06:26:32 +00:00
|
|
|
/** More keys */
|
2012-04-09 08:48:32 +00:00
|
|
|
public final MoreKeySpec[] mMoreKeys;
|
2012-03-01 04:09:39 +00:00
|
|
|
/** More keys column number and flags */
|
|
|
|
private final int mMoreKeysColumnAndFlags;
|
|
|
|
private static final int MORE_KEYS_COLUMN_MASK = 0x000000ff;
|
|
|
|
private static final int MORE_KEYS_FLAGS_FIXED_COLUMN_ORDER = 0x80000000;
|
2012-02-29 09:45:13 +00:00
|
|
|
private static final int MORE_KEYS_FLAGS_HAS_LABELS = 0x40000000;
|
2012-03-07 09:41:38 +00:00
|
|
|
private static final int MORE_KEYS_FLAGS_NEEDS_DIVIDERS = 0x20000000;
|
|
|
|
private static final int MORE_KEYS_FLAGS_EMBEDDED_MORE_KEY = 0x10000000;
|
2012-03-01 04:09:39 +00:00
|
|
|
private static final String MORE_KEYS_AUTO_COLUMN_ORDER = "!autoColumnOrder!";
|
|
|
|
private static final String MORE_KEYS_FIXED_COLUMN_ORDER = "!fixedColumnOrder!";
|
2012-02-29 09:45:13 +00:00
|
|
|
private static final String MORE_KEYS_HAS_LABELS = "!hasLabels!";
|
2012-02-29 13:21:57 +00:00
|
|
|
private static final String MORE_KEYS_NEEDS_DIVIDERS = "!needsDividers!";
|
2012-03-05 02:39:07 +00:00
|
|
|
private static final String MORE_KEYS_EMBEDDED_MORE_KEY = "!embeddedMoreKey!";
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2011-09-15 05:21:46 +00:00
|
|
|
/** Background type that represents different key background visual than normal one. */
|
|
|
|
public final int mBackgroundType;
|
|
|
|
public static final int BACKGROUND_TYPE_NORMAL = 0;
|
|
|
|
public static final int BACKGROUND_TYPE_FUNCTIONAL = 1;
|
2011-09-15 07:13:04 +00:00
|
|
|
public static final int BACKGROUND_TYPE_ACTION = 2;
|
2012-01-26 09:03:30 +00:00
|
|
|
public static final int BACKGROUND_TYPE_STICKY_OFF = 3;
|
|
|
|
public static final int BACKGROUND_TYPE_STICKY_ON = 4;
|
2011-09-15 05:21:46 +00:00
|
|
|
|
2011-11-22 03:55:38 +00:00
|
|
|
private final int mActionFlags;
|
|
|
|
private static final int ACTION_FLAGS_IS_REPEATABLE = 0x01;
|
|
|
|
private static final int ACTION_FLAGS_NO_KEY_PREVIEW = 0x02;
|
2011-11-30 08:54:58 +00:00
|
|
|
private static final int ACTION_FLAGS_ALT_CODE_WHILE_TYPING = 0x04;
|
2012-02-01 06:07:25 +00:00
|
|
|
private static final int ACTION_FLAGS_ENABLE_LONG_PRESS = 0x08;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final KeyVisualAttributes mKeyVisualAttributes;
|
|
|
|
|
2012-08-28 08:19:49 +00:00
|
|
|
private final OptionalAttributes mOptionalAttributes;
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
private static final class OptionalAttributes {
|
2012-08-28 08:19:49 +00:00
|
|
|
/** Text to output when pressed. This can be multiple characters, like ".com" */
|
|
|
|
public final String mOutputText;
|
|
|
|
public final int mAltCode;
|
|
|
|
/** Icon for disabled state */
|
|
|
|
public final int mDisabledIconId;
|
|
|
|
/** Preview version of the icon, for the preview popup */
|
|
|
|
public final int mPreviewIconId;
|
|
|
|
/** The visual insets */
|
|
|
|
public final int mVisualInsetsLeft;
|
|
|
|
public final int mVisualInsetsRight;
|
|
|
|
|
|
|
|
public OptionalAttributes(final String outputText, final int altCode,
|
|
|
|
final int disabledIconId, final int previewIconId,
|
|
|
|
final int visualInsetsLeft, final int visualInsetsRight) {
|
|
|
|
mOutputText = outputText;
|
|
|
|
mAltCode = altCode;
|
|
|
|
mDisabledIconId = disabledIconId;
|
|
|
|
mPreviewIconId = previewIconId;
|
|
|
|
mVisualInsetsLeft = visualInsetsLeft;
|
|
|
|
mVisualInsetsRight = visualInsetsRight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 06:32:24 +00:00
|
|
|
private final int mHashCode;
|
|
|
|
|
2010-12-03 04:17:26 +00:00
|
|
|
/** The current pressed state of this key */
|
2011-06-23 12:23:44 +00:00
|
|
|
private boolean mPressed;
|
2011-02-18 02:28:17 +00:00
|
|
|
/** Key is enabled and responds on press */
|
2011-06-23 12:23:44 +00:00
|
|
|
private boolean mEnabled = true;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
/**
|
2012-04-09 08:48:32 +00:00
|
|
|
* This constructor is being used only for keys in more keys keyboard.
|
2010-12-14 06:31:47 +00:00
|
|
|
*/
|
2012-08-30 05:22:40 +00:00
|
|
|
public Key(final KeyboardParams params, final MoreKeySpec moreKeySpec, final int x, final int y,
|
|
|
|
final int width, final int height, final int labelFlags) {
|
2012-04-09 08:48:32 +00:00
|
|
|
this(params, moreKeySpec.mLabel, null, moreKeySpec.mIconId, moreKeySpec.mCode,
|
|
|
|
moreKeySpec.mOutputText, x, y, width, height, labelFlags);
|
2011-08-30 09:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This constructor is being used only for key in popup suggestions pane.
|
|
|
|
*/
|
2012-08-30 05:22:40 +00:00
|
|
|
public Key(final KeyboardParams params, final String label, final String hintLabel,
|
|
|
|
final int iconId, final int code, final String outputText, final int x, final int y,
|
|
|
|
final int width, final int height, final int labelFlags) {
|
2011-07-29 00:05:40 +00:00
|
|
|
mHeight = height - params.mVerticalGap;
|
2012-08-28 08:19:49 +00:00
|
|
|
mWidth = width - params.mHorizontalGap;
|
2011-08-30 09:35:56 +00:00
|
|
|
mHintLabel = hintLabel;
|
2012-02-29 09:45:13 +00:00
|
|
|
mLabelFlags = labelFlags;
|
2011-09-15 05:21:46 +00:00
|
|
|
mBackgroundType = BACKGROUND_TYPE_NORMAL;
|
2011-11-22 03:55:38 +00:00
|
|
|
mActionFlags = 0;
|
2011-08-31 06:26:32 +00:00
|
|
|
mMoreKeys = null;
|
2012-03-01 04:09:39 +00:00
|
|
|
mMoreKeysColumnAndFlags = 0;
|
2011-08-30 12:41:52 +00:00
|
|
|
mLabel = label;
|
2012-08-28 08:19:49 +00:00
|
|
|
if (outputText == null) {
|
|
|
|
mOptionalAttributes = null;
|
|
|
|
} else {
|
|
|
|
mOptionalAttributes = new OptionalAttributes(outputText, CODE_UNSPECIFIED,
|
|
|
|
ICON_UNDEFINED, ICON_UNDEFINED, 0, 0);
|
|
|
|
}
|
2011-08-30 09:35:56 +00:00
|
|
|
mCode = code;
|
2012-04-19 21:24:51 +00:00
|
|
|
mEnabled = (code != CODE_UNSPECIFIED);
|
2012-02-02 15:18:47 +00:00
|
|
|
mIconId = iconId;
|
2010-12-03 04:17:26 +00:00
|
|
|
// Horizontal gap is divided equally to both sides of the key.
|
2012-08-28 08:19:49 +00:00
|
|
|
mX = x + params.mHorizontalGap / 2;
|
2010-12-03 04:17:26 +00:00
|
|
|
mY = y;
|
2011-09-28 09:51:57 +00:00
|
|
|
mHitBox.set(x, y, x + width + 1, y + height);
|
2012-08-30 08:42:49 +00:00
|
|
|
mKeyVisualAttributes = null;
|
2011-12-16 06:32:24 +00:00
|
|
|
|
2012-03-22 09:09:19 +00:00
|
|
|
mHashCode = computeHashCode(this);
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
/**
|
|
|
|
* Create a key with the given top-left coordinate and extract its attributes from the XML
|
|
|
|
* parser.
|
2010-12-02 09:46:21 +00:00
|
|
|
* @param res resources associated with the caller's context
|
2011-07-29 00:05:40 +00:00
|
|
|
* @param params the keyboard building parameters.
|
2011-09-02 04:48:27 +00:00
|
|
|
* @param row the row that this key belongs to. row's x-coordinate will be the right edge of
|
|
|
|
* this key.
|
2010-12-02 09:46:21 +00:00
|
|
|
* @param parser the XML parser containing the attributes for this key
|
2011-12-14 09:11:27 +00:00
|
|
|
* @throws XmlPullParserException
|
2010-12-02 09:46:21 +00:00
|
|
|
*/
|
2012-08-30 05:22:40 +00:00
|
|
|
public Key(final Resources res, final KeyboardParams params, final KeyboardRow row,
|
|
|
|
final XmlPullParser parser) throws XmlPullParserException {
|
2011-09-26 02:26:38 +00:00
|
|
|
final float horizontalGap = isSpacer() ? 0 : params.mHorizontalGap;
|
2011-09-28 09:51:57 +00:00
|
|
|
final int keyHeight = row.mRowHeight;
|
2012-08-28 08:19:49 +00:00
|
|
|
mHeight = keyHeight - params.mVerticalGap;
|
2010-12-02 09:46:21 +00:00
|
|
|
|
2010-12-14 06:31:47 +00:00
|
|
|
final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
|
|
|
|
R.styleable.Keyboard_Key);
|
2010-12-08 08:07:25 +00:00
|
|
|
|
2012-05-01 07:51:38 +00:00
|
|
|
final KeyStyle style = params.mKeyStyles.getKeyStyle(keyAttr, parser);
|
2011-09-26 02:26:38 +00:00
|
|
|
final float keyXPos = row.getKeyX(keyAttr);
|
|
|
|
final float keyWidth = row.getKeyWidth(keyAttr, keyXPos);
|
2011-09-28 09:51:57 +00:00
|
|
|
final int keyYPos = row.getKeyY();
|
2011-05-30 11:05:50 +00:00
|
|
|
|
2011-09-02 08:05:24 +00:00
|
|
|
// Horizontal gap is divided equally to both sides of the key.
|
2012-05-28 07:13:11 +00:00
|
|
|
mX = Math.round(keyXPos + horizontalGap / 2);
|
2011-09-28 09:51:57 +00:00
|
|
|
mY = keyYPos;
|
2012-05-28 07:13:11 +00:00
|
|
|
mWidth = Math.round(keyWidth - horizontalGap);
|
|
|
|
mHitBox.set(Math.round(keyXPos), keyYPos, Math.round(keyXPos + keyWidth) + 1,
|
|
|
|
keyYPos + keyHeight);
|
2011-09-02 08:05:24 +00:00
|
|
|
// Update row to have current x coordinate.
|
2011-09-26 02:26:38 +00:00
|
|
|
row.setXPos(keyXPos + keyWidth);
|
2011-09-02 08:05:24 +00:00
|
|
|
|
2011-09-26 02:26:38 +00:00
|
|
|
mBackgroundType = style.getInt(keyAttr,
|
2012-06-05 18:28:55 +00:00
|
|
|
R.styleable.Keyboard_Key_backgroundType, row.getDefaultBackgroundType());
|
2011-09-02 08:05:24 +00:00
|
|
|
|
2012-08-28 08:19:49 +00:00
|
|
|
final int visualInsetsLeft = Math.round(ResourceUtils.getDimensionOrFraction(keyAttr,
|
2012-05-28 07:13:11 +00:00
|
|
|
R.styleable.Keyboard_Key_visualInsetsLeft, params.mBaseWidth, 0));
|
2012-08-28 08:19:49 +00:00
|
|
|
final int visualInsetsRight = Math.round(ResourceUtils.getDimensionOrFraction(keyAttr,
|
2012-05-28 07:13:11 +00:00
|
|
|
R.styleable.Keyboard_Key_visualInsetsRight, params.mBaseWidth, 0));
|
2012-04-19 21:24:51 +00:00
|
|
|
mIconId = KeySpecParser.getIconId(style.getString(keyAttr,
|
|
|
|
R.styleable.Keyboard_Key_keyIcon));
|
2012-08-28 08:19:49 +00:00
|
|
|
final int disabledIconId = KeySpecParser.getIconId(style.getString(keyAttr,
|
2012-04-19 21:24:51 +00:00
|
|
|
R.styleable.Keyboard_Key_keyIconDisabled));
|
2012-08-28 08:19:49 +00:00
|
|
|
final int previewIconId = KeySpecParser.getIconId(style.getString(keyAttr,
|
2012-04-19 21:24:51 +00:00
|
|
|
R.styleable.Keyboard_Key_keyIconPreview));
|
2011-09-02 08:05:24 +00:00
|
|
|
|
2012-03-12 07:30:59 +00:00
|
|
|
mLabelFlags = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelFlags)
|
|
|
|
| row.getDefaultKeyLabelFlags();
|
2012-05-26 04:23:34 +00:00
|
|
|
final boolean needsToUpperCase = needsToUpperCase(mLabelFlags, params.mId.mElementId);
|
|
|
|
final Locale locale = params.mId.mLocale;
|
2012-02-07 08:30:54 +00:00
|
|
|
int actionFlags = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyActionFlags);
|
2012-02-13 22:34:09 +00:00
|
|
|
String[] moreKeys = style.getStringArray(keyAttr, R.styleable.Keyboard_Key_moreKeys);
|
2012-03-01 04:09:39 +00:00
|
|
|
|
|
|
|
int moreKeysColumn = style.getInt(keyAttr,
|
2012-02-29 09:45:13 +00:00
|
|
|
R.styleable.Keyboard_Key_maxMoreKeysColumn, params.mMaxMoreKeysKeyboardColumn);
|
2012-03-01 04:09:39 +00:00
|
|
|
int value;
|
|
|
|
if ((value = KeySpecParser.getIntValue(moreKeys, MORE_KEYS_AUTO_COLUMN_ORDER, -1)) > 0) {
|
|
|
|
moreKeysColumn = value & MORE_KEYS_COLUMN_MASK;
|
|
|
|
}
|
|
|
|
if ((value = KeySpecParser.getIntValue(moreKeys, MORE_KEYS_FIXED_COLUMN_ORDER, -1)) > 0) {
|
|
|
|
moreKeysColumn = MORE_KEYS_FLAGS_FIXED_COLUMN_ORDER | (value & MORE_KEYS_COLUMN_MASK);
|
2012-02-13 22:34:09 +00:00
|
|
|
}
|
2012-02-29 09:45:13 +00:00
|
|
|
if (KeySpecParser.getBooleanValue(moreKeys, MORE_KEYS_HAS_LABELS)) {
|
|
|
|
moreKeysColumn |= MORE_KEYS_FLAGS_HAS_LABELS;
|
|
|
|
}
|
2012-02-29 13:21:57 +00:00
|
|
|
if (KeySpecParser.getBooleanValue(moreKeys, MORE_KEYS_NEEDS_DIVIDERS)) {
|
|
|
|
moreKeysColumn |= MORE_KEYS_FLAGS_NEEDS_DIVIDERS;
|
|
|
|
}
|
2012-03-05 02:39:07 +00:00
|
|
|
if (KeySpecParser.getBooleanValue(moreKeys, MORE_KEYS_EMBEDDED_MORE_KEY)) {
|
|
|
|
moreKeysColumn |= MORE_KEYS_FLAGS_EMBEDDED_MORE_KEY;
|
|
|
|
}
|
2012-03-01 04:09:39 +00:00
|
|
|
mMoreKeysColumnAndFlags = moreKeysColumn;
|
|
|
|
|
2012-03-12 07:30:59 +00:00
|
|
|
final String[] additionalMoreKeys;
|
|
|
|
if ((mLabelFlags & LABEL_FLAGS_DISABLE_ADDITIONAL_MORE_KEYS) != 0) {
|
|
|
|
additionalMoreKeys = null;
|
|
|
|
} else {
|
2012-04-19 21:24:51 +00:00
|
|
|
additionalMoreKeys = style.getStringArray(keyAttr,
|
|
|
|
R.styleable.Keyboard_Key_additionalMoreKeys);
|
2012-03-12 07:30:59 +00:00
|
|
|
}
|
2012-04-18 06:44:34 +00:00
|
|
|
moreKeys = KeySpecParser.insertAdditionalMoreKeys(moreKeys, additionalMoreKeys);
|
2012-01-25 10:43:13 +00:00
|
|
|
if (moreKeys != null) {
|
2012-02-01 06:07:25 +00:00
|
|
|
actionFlags |= ACTION_FLAGS_ENABLE_LONG_PRESS;
|
2012-04-09 08:48:32 +00:00
|
|
|
mMoreKeys = new MoreKeySpec[moreKeys.length];
|
2012-01-25 10:43:13 +00:00
|
|
|
for (int i = 0; i < moreKeys.length; i++) {
|
2012-05-26 04:23:34 +00:00
|
|
|
mMoreKeys[i] = new MoreKeySpec(
|
|
|
|
moreKeys[i], needsToUpperCase, locale, params.mCodesSet);
|
2012-01-25 10:43:13 +00:00
|
|
|
}
|
2012-04-09 08:48:32 +00:00
|
|
|
} else {
|
|
|
|
mMoreKeys = null;
|
2012-01-25 10:43:13 +00:00
|
|
|
}
|
2012-02-01 06:07:25 +00:00
|
|
|
mActionFlags = actionFlags;
|
2012-01-25 10:43:13 +00:00
|
|
|
|
2012-02-07 08:07:23 +00:00
|
|
|
if ((mLabelFlags & LABEL_FLAGS_FROM_CUSTOM_ACTION_LABEL) != 0) {
|
|
|
|
mLabel = params.mId.mCustomActionLabel;
|
|
|
|
} else {
|
2012-05-26 04:23:34 +00:00
|
|
|
mLabel = KeySpecParser.toUpperCaseOfStringForLocale(style.getString(keyAttr,
|
|
|
|
R.styleable.Keyboard_Key_keyLabel), needsToUpperCase, locale);
|
2012-02-07 08:07:23 +00:00
|
|
|
}
|
2012-03-12 07:30:59 +00:00
|
|
|
if ((mLabelFlags & LABEL_FLAGS_DISABLE_HINT_LABEL) != 0) {
|
|
|
|
mHintLabel = null;
|
|
|
|
} else {
|
2012-05-26 04:23:34 +00:00
|
|
|
mHintLabel = KeySpecParser.toUpperCaseOfStringForLocale(style.getString(keyAttr,
|
|
|
|
R.styleable.Keyboard_Key_keyHintLabel), needsToUpperCase, locale);
|
2012-03-12 07:30:59 +00:00
|
|
|
}
|
2012-05-26 04:23:34 +00:00
|
|
|
String outputText = KeySpecParser.toUpperCaseOfStringForLocale(style.getString(keyAttr,
|
|
|
|
R.styleable.Keyboard_Key_keyOutputText), needsToUpperCase, locale);
|
2012-04-19 21:24:51 +00:00
|
|
|
final int code = KeySpecParser.parseCode(style.getString(keyAttr,
|
|
|
|
R.styleable.Keyboard_Key_code), params.mCodesSet, CODE_UNSPECIFIED);
|
2012-01-26 09:03:30 +00:00
|
|
|
// Choose the first letter of the label as primary code if not specified.
|
2012-04-19 21:24:51 +00:00
|
|
|
if (code == CODE_UNSPECIFIED && TextUtils.isEmpty(outputText)
|
2011-12-12 03:52:42 +00:00
|
|
|
&& !TextUtils.isEmpty(mLabel)) {
|
2012-03-08 08:07:02 +00:00
|
|
|
if (StringUtils.codePointCount(mLabel) == 1) {
|
2012-01-26 09:03:30 +00:00
|
|
|
// Use the first letter of the hint label if shiftedLetterActivated flag is
|
|
|
|
// specified.
|
|
|
|
if (hasShiftedLetterHint() && isShiftedLetterActivated()
|
|
|
|
&& !TextUtils.isEmpty(mHintLabel)) {
|
2012-02-01 10:43:58 +00:00
|
|
|
mCode = mHintLabel.codePointAt(0);
|
2012-01-26 09:03:30 +00:00
|
|
|
} else {
|
2012-02-01 10:43:58 +00:00
|
|
|
mCode = mLabel.codePointAt(0);
|
2012-01-26 09:03:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In some locale and case, the character might be represented by multiple code
|
|
|
|
// points, such as upper case Eszett of German alphabet.
|
|
|
|
outputText = mLabel;
|
2012-04-19 21:24:51 +00:00
|
|
|
mCode = CODE_OUTPUT_TEXT;
|
2011-12-12 03:52:42 +00:00
|
|
|
}
|
2012-04-19 21:24:51 +00:00
|
|
|
} else if (code == CODE_UNSPECIFIED && outputText != null) {
|
2012-03-08 08:07:02 +00:00
|
|
|
if (StringUtils.codePointCount(outputText) == 1) {
|
2012-02-07 11:42:07 +00:00
|
|
|
mCode = outputText.codePointAt(0);
|
|
|
|
outputText = null;
|
|
|
|
} else {
|
2012-04-19 21:24:51 +00:00
|
|
|
mCode = CODE_OUTPUT_TEXT;
|
2012-02-07 11:42:07 +00:00
|
|
|
}
|
2011-09-02 08:05:24 +00:00
|
|
|
} else {
|
2012-05-26 04:23:34 +00:00
|
|
|
mCode = KeySpecParser.toUpperCaseOfCodeForLocale(code, needsToUpperCase, locale);
|
2011-09-02 08:05:24 +00:00
|
|
|
}
|
2012-08-28 08:19:49 +00:00
|
|
|
final int altCode = KeySpecParser.toUpperCaseOfCodeForLocale(
|
2012-05-26 04:23:34 +00:00
|
|
|
KeySpecParser.parseCode(style.getString(keyAttr,
|
2012-04-19 21:24:51 +00:00
|
|
|
R.styleable.Keyboard_Key_altCode), params.mCodesSet, CODE_UNSPECIFIED),
|
2012-05-26 04:23:34 +00:00
|
|
|
needsToUpperCase, locale);
|
2012-08-28 08:19:49 +00:00
|
|
|
if (outputText == null && altCode == CODE_UNSPECIFIED
|
|
|
|
&& disabledIconId == ICON_UNDEFINED && previewIconId == ICON_UNDEFINED
|
|
|
|
&& visualInsetsLeft == 0 && visualInsetsRight == 0) {
|
|
|
|
mOptionalAttributes = null;
|
|
|
|
} else {
|
|
|
|
mOptionalAttributes = new OptionalAttributes(outputText, altCode,
|
|
|
|
disabledIconId, previewIconId,
|
|
|
|
visualInsetsLeft, visualInsetsRight);
|
|
|
|
}
|
2012-08-30 08:42:49 +00:00
|
|
|
mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr);
|
2011-09-02 08:05:24 +00:00
|
|
|
keyAttr.recycle();
|
2012-08-30 08:42:49 +00:00
|
|
|
mHashCode = computeHashCode(this);
|
2012-01-26 09:03:30 +00:00
|
|
|
if (hasShiftedLetterHint() && TextUtils.isEmpty(mHintLabel)) {
|
|
|
|
Log.w(TAG, "hasShiftedLetterHint specified without keyHintLabel: " + this);
|
|
|
|
}
|
2011-12-16 06:32:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
private static boolean needsToUpperCase(final int labelFlags, final int keyboardElementId) {
|
2012-05-26 04:23:34 +00:00
|
|
|
if ((labelFlags & LABEL_FLAGS_PRESERVE_CASE) != 0) return false;
|
|
|
|
switch (keyboardElementId) {
|
2012-01-25 10:43:13 +00:00
|
|
|
case KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED:
|
|
|
|
case KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED:
|
|
|
|
case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED:
|
|
|
|
case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED:
|
2012-05-26 04:23:34 +00:00
|
|
|
return true;
|
2012-01-25 10:43:13 +00:00
|
|
|
default:
|
2012-05-26 04:23:34 +00:00
|
|
|
return false;
|
2012-01-25 10:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
private static int computeHashCode(final Key key) {
|
2011-12-16 06:32:24 +00:00
|
|
|
return Arrays.hashCode(new Object[] {
|
|
|
|
key.mX,
|
|
|
|
key.mY,
|
|
|
|
key.mWidth,
|
|
|
|
key.mHeight,
|
|
|
|
key.mCode,
|
|
|
|
key.mLabel,
|
|
|
|
key.mHintLabel,
|
2012-02-02 15:18:47 +00:00
|
|
|
key.mIconId,
|
2012-01-26 09:03:30 +00:00
|
|
|
key.mBackgroundType,
|
2012-02-09 18:17:39 +00:00
|
|
|
Arrays.hashCode(key.mMoreKeys),
|
2012-08-28 08:19:49 +00:00
|
|
|
key.getOutputText(),
|
2012-02-09 18:17:39 +00:00
|
|
|
key.mActionFlags,
|
|
|
|
key.mLabelFlags,
|
2011-12-16 06:32:24 +00:00
|
|
|
// Key can be distinguishable without the following members.
|
2012-08-28 08:19:49 +00:00
|
|
|
// key.mOptionalAttributes.mAltCode,
|
|
|
|
// key.mOptionalAttributes.mDisabledIconId,
|
|
|
|
// key.mOptionalAttributes.mPreviewIconId,
|
2011-12-16 06:32:24 +00:00
|
|
|
// key.mHorizontalGap,
|
|
|
|
// key.mVerticalGap,
|
2012-08-28 08:19:49 +00:00
|
|
|
// key.mOptionalAttributes.mVisualInsetLeft,
|
|
|
|
// key.mOptionalAttributes.mVisualInsetRight,
|
2011-12-16 06:32:24 +00:00
|
|
|
// key.mMaxMoreKeysColumn,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-09-11 10:15:00 +00:00
|
|
|
private boolean equalsInternal(final Key o) {
|
2011-12-16 06:32:24 +00:00
|
|
|
if (this == o) return true;
|
|
|
|
return o.mX == mX
|
|
|
|
&& o.mY == mY
|
|
|
|
&& o.mWidth == mWidth
|
|
|
|
&& o.mHeight == mHeight
|
|
|
|
&& o.mCode == mCode
|
|
|
|
&& TextUtils.equals(o.mLabel, mLabel)
|
2012-01-26 09:03:30 +00:00
|
|
|
&& TextUtils.equals(o.mHintLabel, mHintLabel)
|
2012-02-02 15:18:47 +00:00
|
|
|
&& o.mIconId == mIconId
|
2012-02-09 18:17:39 +00:00
|
|
|
&& o.mBackgroundType == mBackgroundType
|
|
|
|
&& Arrays.equals(o.mMoreKeys, mMoreKeys)
|
2012-08-28 08:19:49 +00:00
|
|
|
&& TextUtils.equals(o.getOutputText(), getOutputText())
|
2012-02-09 18:17:39 +00:00
|
|
|
&& o.mActionFlags == mActionFlags
|
|
|
|
&& o.mLabelFlags == mLabelFlags;
|
2011-12-16 06:32:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-11 10:15:00 +00:00
|
|
|
@Override
|
|
|
|
public int compareTo(Key o) {
|
|
|
|
if (equalsInternal(o)) return 0;
|
|
|
|
if (mHashCode > o.mHashCode) return 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-16 06:32:24 +00:00
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return mHashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-08-30 05:22:40 +00:00
|
|
|
public boolean equals(final Object o) {
|
2012-09-11 10:15:00 +00:00
|
|
|
return o instanceof Key && equalsInternal((Key)o);
|
2010-12-03 04:17:26 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 09:03:30 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2012-07-30 06:51:41 +00:00
|
|
|
final String label;
|
|
|
|
if (StringUtils.codePointCount(mLabel) == 1 && mLabel.codePointAt(0) == mCode) {
|
|
|
|
label = "";
|
|
|
|
} else {
|
|
|
|
label = "/" + mLabel;
|
|
|
|
}
|
|
|
|
return String.format("%s%s %d,%d %dx%d %s/%s/%s",
|
2012-10-29 05:46:34 +00:00
|
|
|
Constants.printableCode(mCode), label, mX, mY, mWidth, mHeight, mHintLabel,
|
2012-02-02 15:18:47 +00:00
|
|
|
KeyboardIconsSet.getIconName(mIconId), backgroundName(mBackgroundType));
|
2012-02-02 12:24:09 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
private static String backgroundName(final int backgroundType) {
|
2012-02-02 12:24:09 +00:00
|
|
|
switch (backgroundType) {
|
|
|
|
case BACKGROUND_TYPE_NORMAL: return "normal";
|
|
|
|
case BACKGROUND_TYPE_FUNCTIONAL: return "functional";
|
|
|
|
case BACKGROUND_TYPE_ACTION: return "action";
|
|
|
|
case BACKGROUND_TYPE_STICKY_OFF: return "stickyOff";
|
|
|
|
case BACKGROUND_TYPE_STICKY_ON: return "stickyOn";
|
|
|
|
default: return null;
|
2012-01-26 09:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public void markAsLeftEdge(final KeyboardParams params) {
|
2011-09-28 09:51:57 +00:00
|
|
|
mHitBox.left = params.mHorizontalEdgesPadding;
|
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public void markAsRightEdge(final KeyboardParams params) {
|
2011-09-28 09:51:57 +00:00
|
|
|
mHitBox.right = params.mOccupiedWidth - params.mHorizontalEdgesPadding;
|
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public void markAsTopEdge(final KeyboardParams params) {
|
2011-09-28 09:51:57 +00:00
|
|
|
mHitBox.top = params.mTopPadding;
|
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public void markAsBottomEdge(final KeyboardParams params) {
|
2011-09-28 09:51:57 +00:00
|
|
|
mHitBox.bottom = params.mOccupiedHeight + params.mBottomPadding;
|
2011-07-19 00:14:50 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 15:18:47 +00:00
|
|
|
public final boolean isSpacer() {
|
|
|
|
return this instanceof Spacer;
|
2011-09-08 08:19:23 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean isShift() {
|
2012-04-19 21:24:51 +00:00
|
|
|
return mCode == CODE_SHIFT;
|
2011-11-29 07:56:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean isModifier() {
|
2012-04-19 21:24:51 +00:00
|
|
|
return mCode == CODE_SHIFT || mCode == CODE_SWITCH_ALPHA_SYMBOL;
|
2011-11-29 07:56:27 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean isRepeatable() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mActionFlags & ACTION_FLAGS_IS_REPEATABLE) != 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean noKeyPreview() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mActionFlags & ACTION_FLAGS_NO_KEY_PREVIEW) != 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean altCodeWhileTyping() {
|
2011-11-30 08:54:58 +00:00
|
|
|
return (mActionFlags & ACTION_FLAGS_ALT_CODE_WHILE_TYPING) != 0;
|
2011-11-23 02:07:43 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean isLongPressEnabled() {
|
2012-02-01 06:07:25 +00:00
|
|
|
// We need not start long press timer on the key which has activated shifted letter.
|
|
|
|
return (mActionFlags & ACTION_FLAGS_ENABLE_LONG_PRESS) != 0
|
|
|
|
&& (mLabelFlags & LABEL_FLAGS_SHIFTED_LETTER_ACTIVATED) == 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final Typeface selectTypeface(final KeyDrawParams params) {
|
2011-06-25 10:38:55 +00:00
|
|
|
// TODO: Handle "bold" here too?
|
2011-11-22 03:55:38 +00:00
|
|
|
if ((mLabelFlags & LABEL_FLAGS_FONT_NORMAL) != 0) {
|
2011-06-25 10:38:55 +00:00
|
|
|
return Typeface.DEFAULT;
|
2011-11-22 03:55:38 +00:00
|
|
|
} else if ((mLabelFlags & LABEL_FLAGS_FONT_MONO_SPACE) != 0) {
|
2011-06-25 10:38:55 +00:00
|
|
|
return Typeface.MONOSPACE;
|
|
|
|
} else {
|
2012-08-30 08:42:49 +00:00
|
|
|
return params.mTypeface;
|
2011-06-25 10:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final int selectTextSize(final KeyDrawParams params) {
|
2012-05-17 10:30:38 +00:00
|
|
|
switch (mLabelFlags & LABEL_FLAGS_FOLLOW_KEY_TEXT_RATIO_MASK) {
|
2012-04-11 03:34:30 +00:00
|
|
|
case LABEL_FLAGS_FOLLOW_KEY_LETTER_RATIO:
|
2012-08-30 08:42:49 +00:00
|
|
|
return params.mLetterSize;
|
2012-05-18 08:07:14 +00:00
|
|
|
case LABEL_FLAGS_FOLLOW_KEY_LARGE_LETTER_RATIO:
|
2012-08-30 08:42:49 +00:00
|
|
|
return params.mLargeLetterSize;
|
2012-04-11 03:34:30 +00:00
|
|
|
case LABEL_FLAGS_FOLLOW_KEY_LABEL_RATIO:
|
2012-08-30 08:42:49 +00:00
|
|
|
return params.mLabelSize;
|
2012-05-17 10:30:38 +00:00
|
|
|
case LABEL_FLAGS_FOLLOW_KEY_LARGE_LABEL_RATIO:
|
2012-08-30 08:42:49 +00:00
|
|
|
return params.mLargeLabelSize;
|
2012-04-11 03:34:30 +00:00
|
|
|
case LABEL_FLAGS_FOLLOW_KEY_HINT_LABEL_RATIO:
|
2012-08-30 08:42:49 +00:00
|
|
|
return params.mHintLabelSize;
|
2012-04-11 03:34:30 +00:00
|
|
|
default: // No follow key ratio flag specified.
|
2012-08-30 08:42:49 +00:00
|
|
|
return StringUtils.codePointCount(mLabel) == 1 ? params.mLetterSize : params.mLabelSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int selectTextColor(final KeyDrawParams params) {
|
|
|
|
return isShiftedLetterActivated() ? params.mTextInactivatedColor : params.mTextColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int selectHintTextSize(final KeyDrawParams params) {
|
|
|
|
if (hasHintLabel()) {
|
|
|
|
return params.mHintLabelSize;
|
|
|
|
} else if (hasShiftedLetterHint()) {
|
|
|
|
return params.mShiftedLetterHintSize;
|
|
|
|
} else {
|
|
|
|
return params.mHintLetterSize;
|
2011-06-25 10:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final int selectHintTextColor(final KeyDrawParams params) {
|
|
|
|
if (hasHintLabel()) {
|
|
|
|
return params.mHintLabelColor;
|
|
|
|
} else if (hasShiftedLetterHint()) {
|
|
|
|
return isShiftedLetterActivated() ? params.mShiftedLetterHintActivatedColor
|
|
|
|
: params.mShiftedLetterHintInactivatedColor;
|
|
|
|
} else {
|
|
|
|
return params.mHintLetterColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int selectMoreKeyTextSize(final KeyDrawParams params) {
|
|
|
|
return hasLabelsInMoreKeys() ? params.mLabelSize : params.mLetterSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final boolean isAlignLeft() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_ALIGN_LEFT) != 0;
|
2011-08-03 04:29:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean isAlignRight() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_ALIGN_RIGHT) != 0;
|
2011-08-03 04:29:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean isAlignLeftOfCenter() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_ALIGN_LEFT_OF_CENTER) != 0;
|
2011-08-03 04:29:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean hasPopupHint() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_HAS_POPUP_HINT) != 0;
|
2011-06-15 03:36:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean hasShiftedLetterHint() {
|
2012-01-26 09:03:30 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_HAS_SHIFTED_LETTER_HINT) != 0;
|
2011-06-15 03:36:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean hasHintLabel() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_HAS_HINT_LABEL) != 0;
|
2011-06-25 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean hasLabelWithIconLeft() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_WITH_ICON_LEFT) != 0;
|
2011-08-03 04:29:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean hasLabelWithIconRight() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_WITH_ICON_RIGHT) != 0;
|
2011-08-03 04:29:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean needsXScale() {
|
2011-11-22 03:55:38 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_AUTO_X_SCALE) != 0;
|
2011-11-18 21:47:48 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean isShiftedLetterActivated() {
|
2012-01-26 09:03:30 +00:00
|
|
|
return (mLabelFlags & LABEL_FLAGS_SHIFTED_LETTER_ACTIVATED) != 0;
|
2012-01-27 09:10:19 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final int getMoreKeysColumn() {
|
2012-03-01 04:09:39 +00:00
|
|
|
return mMoreKeysColumnAndFlags & MORE_KEYS_COLUMN_MASK;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean isFixedColumnOrderMoreKeys() {
|
2012-03-01 04:09:39 +00:00
|
|
|
return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_FIXED_COLUMN_ORDER) != 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean hasLabelsInMoreKeys() {
|
2012-02-29 09:45:13 +00:00
|
|
|
return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_HAS_LABELS) != 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final int getMoreKeyLabelFlags() {
|
2012-04-11 03:34:30 +00:00
|
|
|
return hasLabelsInMoreKeys()
|
|
|
|
? LABEL_FLAGS_FOLLOW_KEY_LABEL_RATIO
|
|
|
|
: LABEL_FLAGS_FOLLOW_KEY_LETTER_RATIO;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean needsDividersInMoreKeys() {
|
2012-02-29 13:21:57 +00:00
|
|
|
return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_NEEDS_DIVIDERS) != 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean hasEmbeddedMoreKey() {
|
2012-03-05 02:39:07 +00:00
|
|
|
return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_EMBEDDED_MORE_KEY) != 0;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final String getOutputText() {
|
2012-08-28 08:19:49 +00:00
|
|
|
final OptionalAttributes attrs = mOptionalAttributes;
|
|
|
|
return (attrs != null) ? attrs.mOutputText : null;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final int getAltCode() {
|
2012-08-28 08:19:49 +00:00
|
|
|
final OptionalAttributes attrs = mOptionalAttributes;
|
|
|
|
return (attrs != null) ? attrs.mAltCode : CODE_UNSPECIFIED;
|
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public Drawable getIcon(final KeyboardIconsSet iconSet, final int alpha) {
|
2012-08-28 08:19:49 +00:00
|
|
|
final OptionalAttributes attrs = mOptionalAttributes;
|
|
|
|
final int disabledIconId = (attrs != null) ? attrs.mDisabledIconId : ICON_UNDEFINED;
|
|
|
|
final int iconId = mEnabled ? mIconId : disabledIconId;
|
2012-05-16 08:41:56 +00:00
|
|
|
final Drawable icon = iconSet.getIconDrawable(iconId);
|
|
|
|
if (icon != null) {
|
|
|
|
icon.setAlpha(alpha);
|
|
|
|
}
|
|
|
|
return icon;
|
2010-12-03 04:17:26 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public Drawable getPreviewIcon(final KeyboardIconsSet iconSet) {
|
2012-08-28 08:19:49 +00:00
|
|
|
final OptionalAttributes attrs = mOptionalAttributes;
|
|
|
|
final int previewIconId = (attrs != null) ? attrs.mPreviewIconId : ICON_UNDEFINED;
|
|
|
|
return previewIconId != ICON_UNDEFINED
|
|
|
|
? iconSet.getIconDrawable(previewIconId) : iconSet.getIconDrawable(mIconId);
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final int getDrawX() {
|
2012-08-28 08:19:49 +00:00
|
|
|
final OptionalAttributes attrs = mOptionalAttributes;
|
|
|
|
return (attrs == null) ? mX : mX + attrs.mVisualInsetsLeft;
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final int getDrawWidth() {
|
2012-08-28 08:19:49 +00:00
|
|
|
final OptionalAttributes attrs = mOptionalAttributes;
|
|
|
|
return (attrs == null) ? mWidth
|
|
|
|
: mWidth - attrs.mVisualInsetsLeft - attrs.mVisualInsetsRight;
|
2010-12-03 04:17:26 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
/**
|
|
|
|
* Informs the key that it has been pressed, in case it needs to change its appearance or
|
|
|
|
* state.
|
2011-04-07 07:12:00 +00:00
|
|
|
* @see #onReleased()
|
2010-12-02 09:46:21 +00:00
|
|
|
*/
|
|
|
|
public void onPressed() {
|
2011-04-07 07:12:00 +00:00
|
|
|
mPressed = true;
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-07 07:12:00 +00:00
|
|
|
* Informs the key that it has been released, in case it needs to change its appearance or
|
|
|
|
* state.
|
2010-12-02 09:46:21 +00:00
|
|
|
* @see #onPressed()
|
|
|
|
*/
|
2011-04-07 07:12:00 +00:00
|
|
|
public void onReleased() {
|
|
|
|
mPressed = false;
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public final boolean isEnabled() {
|
2011-06-23 12:23:44 +00:00
|
|
|
return mEnabled;
|
|
|
|
}
|
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
public void setEnabled(final boolean enabled) {
|
2011-06-23 12:23:44 +00:00
|
|
|
mEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
/**
|
2010-12-03 00:39:42 +00:00
|
|
|
* Detects if a point falls on this key.
|
2010-12-02 09:46:21 +00:00
|
|
|
* @param x the x-coordinate of the point
|
|
|
|
* @param y the y-coordinate of the point
|
2011-12-14 09:11:27 +00:00
|
|
|
* @return whether or not the point falls on the key. If the key is attached to an edge, it
|
|
|
|
* will assume that all points between the key and the edge are considered to be on the key.
|
2012-08-30 05:22:40 +00:00
|
|
|
* @see #markAsLeftEdge(KeyboardParams) etc.
|
2010-12-02 09:46:21 +00:00
|
|
|
*/
|
2012-08-30 05:22:40 +00:00
|
|
|
public boolean isOnKey(final int x, final int y) {
|
2011-09-28 09:51:57 +00:00
|
|
|
return mHitBox.contains(x, y);
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the square of the distance to the nearest edge of the key and the given point.
|
|
|
|
* @param x the x-coordinate of the point
|
|
|
|
* @param y the y-coordinate of the point
|
|
|
|
* @return the square of the distance of the point from the nearest edge of the key
|
|
|
|
*/
|
2012-08-30 05:22:40 +00:00
|
|
|
public int squaredDistanceToEdge(final int x, final int y) {
|
2011-04-28 14:39:49 +00:00
|
|
|
final int left = mX;
|
|
|
|
final int right = left + mWidth;
|
|
|
|
final int top = mY;
|
|
|
|
final int bottom = top + mHeight;
|
2010-12-02 09:46:21 +00:00
|
|
|
final int edgeX = x < left ? left : (x > right ? right : x);
|
|
|
|
final int edgeY = y < top ? top : (y > bottom ? bottom : y);
|
|
|
|
final int dx = x - edgeX;
|
|
|
|
final int dy = y - edgeY;
|
|
|
|
return dx * dx + dy * dy;
|
|
|
|
}
|
|
|
|
|
2011-09-15 08:54:21 +00:00
|
|
|
private final static int[] KEY_STATE_NORMAL_HIGHLIGHT_ON = {
|
|
|
|
android.R.attr.state_checkable,
|
|
|
|
android.R.attr.state_checked
|
|
|
|
};
|
|
|
|
|
|
|
|
private final static int[] KEY_STATE_PRESSED_HIGHLIGHT_ON = {
|
|
|
|
android.R.attr.state_pressed,
|
|
|
|
android.R.attr.state_checkable,
|
|
|
|
android.R.attr.state_checked
|
|
|
|
};
|
|
|
|
|
|
|
|
private final static int[] KEY_STATE_NORMAL_HIGHLIGHT_OFF = {
|
|
|
|
android.R.attr.state_checkable
|
|
|
|
};
|
|
|
|
|
|
|
|
private final static int[] KEY_STATE_PRESSED_HIGHLIGHT_OFF = {
|
|
|
|
android.R.attr.state_pressed,
|
|
|
|
android.R.attr.state_checkable
|
|
|
|
};
|
|
|
|
|
|
|
|
private final static int[] KEY_STATE_NORMAL = {
|
|
|
|
};
|
|
|
|
|
|
|
|
private final static int[] KEY_STATE_PRESSED = {
|
|
|
|
android.R.attr.state_pressed
|
|
|
|
};
|
|
|
|
|
|
|
|
// functional normal state (with properties)
|
|
|
|
private static final int[] KEY_STATE_FUNCTIONAL_NORMAL = {
|
|
|
|
android.R.attr.state_single
|
|
|
|
};
|
|
|
|
|
|
|
|
// functional pressed state (with properties)
|
|
|
|
private static final int[] KEY_STATE_FUNCTIONAL_PRESSED = {
|
|
|
|
android.R.attr.state_single,
|
|
|
|
android.R.attr.state_pressed
|
|
|
|
};
|
|
|
|
|
|
|
|
// action normal state (with properties)
|
|
|
|
private static final int[] KEY_STATE_ACTIVE_NORMAL = {
|
|
|
|
android.R.attr.state_active
|
|
|
|
};
|
|
|
|
|
|
|
|
// action pressed state (with properties)
|
|
|
|
private static final int[] KEY_STATE_ACTIVE_PRESSED = {
|
|
|
|
android.R.attr.state_active,
|
|
|
|
android.R.attr.state_pressed
|
|
|
|
};
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
/**
|
|
|
|
* Returns the drawable state for the key, based on the current state and type of the key.
|
|
|
|
* @return the drawable state of the key.
|
|
|
|
* @see android.graphics.drawable.StateListDrawable#setState(int[])
|
|
|
|
*/
|
2012-08-30 08:42:49 +00:00
|
|
|
public final int[] getCurrentDrawableState() {
|
2011-09-15 07:13:04 +00:00
|
|
|
switch (mBackgroundType) {
|
|
|
|
case BACKGROUND_TYPE_FUNCTIONAL:
|
2012-01-26 09:03:30 +00:00
|
|
|
return mPressed ? KEY_STATE_FUNCTIONAL_PRESSED : KEY_STATE_FUNCTIONAL_NORMAL;
|
2011-09-15 07:13:04 +00:00
|
|
|
case BACKGROUND_TYPE_ACTION:
|
2012-01-26 09:03:30 +00:00
|
|
|
return mPressed ? KEY_STATE_ACTIVE_PRESSED : KEY_STATE_ACTIVE_NORMAL;
|
|
|
|
case BACKGROUND_TYPE_STICKY_OFF:
|
|
|
|
return mPressed ? KEY_STATE_PRESSED_HIGHLIGHT_OFF : KEY_STATE_NORMAL_HIGHLIGHT_OFF;
|
|
|
|
case BACKGROUND_TYPE_STICKY_ON:
|
|
|
|
return mPressed ? KEY_STATE_PRESSED_HIGHLIGHT_ON : KEY_STATE_NORMAL_HIGHLIGHT_ON;
|
2011-09-15 07:13:04 +00:00
|
|
|
default: /* BACKGROUND_TYPE_NORMAL */
|
2012-01-26 09:03:30 +00:00
|
|
|
return mPressed ? KEY_STATE_PRESSED : KEY_STATE_NORMAL;
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-08 08:19:23 +00:00
|
|
|
|
|
|
|
public static class Spacer extends Key {
|
2012-08-30 05:22:40 +00:00
|
|
|
public Spacer(final Resources res, final KeyboardParams params, final KeyboardRow row,
|
|
|
|
final XmlPullParser parser) throws XmlPullParserException {
|
2012-04-05 05:59:55 +00:00
|
|
|
super(res, params, row, parser);
|
2011-09-08 08:19:23 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 09:57:44 +00:00
|
|
|
/**
|
|
|
|
* This constructor is being used only for divider in more keys keyboard.
|
|
|
|
*/
|
2012-08-30 05:22:40 +00:00
|
|
|
protected Spacer(final KeyboardParams params, final int x, final int y, final int width,
|
|
|
|
final int height) {
|
2012-04-19 21:24:51 +00:00
|
|
|
super(params, null, null, ICON_UNDEFINED, CODE_UNSPECIFIED,
|
2012-02-29 09:45:13 +00:00
|
|
|
null, x, y, width, height, 0);
|
2011-09-08 08:19:23 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-02 09:46:21 +00:00
|
|
|
}
|