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
|
|
|
*
|
|
|
|
* 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-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.Keyboard.CODE_OUTPUT_TEXT;
|
|
|
|
import static com.android.inputmethod.keyboard.Keyboard.CODE_SHIFT;
|
|
|
|
import static com.android.inputmethod.keyboard.Keyboard.CODE_SWITCH_ALPHA_SYMBOL;
|
|
|
|
import static com.android.inputmethod.keyboard.Keyboard.CODE_UNSPECIFIED;
|
|
|
|
import static com.android.inputmethod.keyboard.internal.KeyboardIconsSet.ICON_UNDEFINED;
|
|
|
|
|
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-02-02 15:18:47 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeySpecParser;
|
2012-04-09 08:48:32 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeySpecParser.MoreKeySpec;
|
2011-06-25 10:38:55 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle;
|
2011-08-30 09:35:56 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
|
2011-06-21 14:38:42 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
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.
|
|
|
|
*/
|
|
|
|
public class 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;
|
2011-11-30 08:54:58 +00:00
|
|
|
public final int mAltCode;
|
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;
|
2011-12-18 16:11:09 +00:00
|
|
|
/** Icon for disabled state */
|
2012-02-02 15:18:47 +00:00
|
|
|
private final int mDisabledIconId;
|
2010-12-02 09:46:21 +00:00
|
|
|
/** Preview version of the icon, for the preview popup */
|
2012-02-02 15:18:47 +00:00
|
|
|
private final int mPreviewIconId;
|
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;
|
2011-04-11 02:30:15 +00:00
|
|
|
/** The horizontal gap around this key */
|
2011-08-01 23:50:08 +00:00
|
|
|
public final int mHorizontalGap;
|
|
|
|
/** The vertical gap below this key */
|
|
|
|
public final int mVerticalGap;
|
2011-04-11 02:30:15 +00:00
|
|
|
/** The visual insets */
|
|
|
|
public final int mVisualInsetsLeft;
|
|
|
|
public final int mVisualInsetsRight;
|
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();
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
/** Text to output when pressed. This can be multiple characters, like ".com" */
|
2010-12-03 04:17:26 +00:00
|
|
|
public final CharSequence mOutputText;
|
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
|
|
|
|
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-04-09 08:48:32 +00:00
|
|
|
public Key(Keyboard.Params params, MoreKeySpec moreKeySpec, int x, int y, int width, int height,
|
|
|
|
int labelFlags) {
|
|
|
|
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-02-02 15:18:47 +00:00
|
|
|
public Key(Keyboard.Params params, String label, String hintLabel, int iconId,
|
2012-02-29 09:45:13 +00:00
|
|
|
int code, String outputText, int x, int y, int width, int height, int labelFlags) {
|
2011-07-29 00:05:40 +00:00
|
|
|
mHeight = height - params.mVerticalGap;
|
|
|
|
mHorizontalGap = params.mHorizontalGap;
|
|
|
|
mVerticalGap = params.mVerticalGap;
|
2011-04-11 02:30:15 +00:00
|
|
|
mVisualInsetsLeft = mVisualInsetsRight = 0;
|
2011-08-01 23:50:08 +00:00
|
|
|
mWidth = width - 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;
|
|
|
|
mOutputText = outputText;
|
2011-08-30 09:35:56 +00:00
|
|
|
mCode = code;
|
2012-04-19 21:24:51 +00:00
|
|
|
mEnabled = (code != CODE_UNSPECIFIED);
|
|
|
|
mAltCode = CODE_UNSPECIFIED;
|
2012-02-02 15:18:47 +00:00
|
|
|
mIconId = iconId;
|
2012-04-19 21:24:51 +00:00
|
|
|
mDisabledIconId = ICON_UNDEFINED;
|
|
|
|
mPreviewIconId = ICON_UNDEFINED;
|
2010-12-03 04:17:26 +00:00
|
|
|
// Horizontal gap is divided equally to both sides of the key.
|
2011-08-01 23:50:08 +00:00
|
|
|
mX = x + 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);
|
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
|
|
|
*/
|
2011-12-18 10:54:08 +00:00
|
|
|
public Key(Resources res, Keyboard.Params params, Keyboard.Builder.Row row,
|
2012-04-05 05:59:55 +00:00
|
|
|
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;
|
2011-09-02 08:05:24 +00:00
|
|
|
mVerticalGap = params.mVerticalGap;
|
2011-09-28 09:51:57 +00:00
|
|
|
mHeight = keyHeight - 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);
|
|
|
|
mHorizontalGap = Math.round(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,
|
|
|
|
R.styleable.Keyboard_Key_backgroundType, BACKGROUND_TYPE_NORMAL);
|
2011-09-02 08:05:24 +00:00
|
|
|
|
2012-05-28 07:13:11 +00:00
|
|
|
mVisualInsetsLeft = Math.round(Keyboard.Builder.getDimensionOrFraction(keyAttr,
|
|
|
|
R.styleable.Keyboard_Key_visualInsetsLeft, params.mBaseWidth, 0));
|
|
|
|
mVisualInsetsRight = Math.round(Keyboard.Builder.getDimensionOrFraction(keyAttr,
|
|
|
|
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));
|
|
|
|
mDisabledIconId = KeySpecParser.getIconId(style.getString(keyAttr,
|
|
|
|
R.styleable.Keyboard_Key_keyIconDisabled));
|
|
|
|
mPreviewIconId = KeySpecParser.getIconId(style.getString(keyAttr,
|
|
|
|
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-01-26 09:03:30 +00:00
|
|
|
mOutputText = outputText;
|
2012-05-26 04:23:34 +00:00
|
|
|
mAltCode = KeySpecParser.toUpperCaseOfCodeForLocale(
|
|
|
|
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-03-22 09:09:19 +00:00
|
|
|
mHashCode = computeHashCode(this);
|
2011-09-02 08:05:24 +00:00
|
|
|
|
|
|
|
keyAttr.recycle();
|
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-05-26 04:23:34 +00:00
|
|
|
private static boolean needsToUpperCase(int labelFlags, int keyboardElementId) {
|
|
|
|
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-03-22 09:09:19 +00:00
|
|
|
private static int computeHashCode(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),
|
|
|
|
key.mOutputText,
|
|
|
|
key.mActionFlags,
|
|
|
|
key.mLabelFlags,
|
2011-12-16 06:32:24 +00:00
|
|
|
// Key can be distinguishable without the following members.
|
|
|
|
// key.mAltCode,
|
2012-02-02 15:18:47 +00:00
|
|
|
// key.mDisabledIconId,
|
|
|
|
// key.mPreviewIconId,
|
2011-12-16 06:32:24 +00:00
|
|
|
// key.mHorizontalGap,
|
|
|
|
// key.mVerticalGap,
|
|
|
|
// key.mVisualInsetLeft,
|
|
|
|
// key.mVisualInsetRight,
|
|
|
|
// key.mMaxMoreKeysColumn,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean equals(Key o) {
|
|
|
|
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)
|
|
|
|
&& TextUtils.equals(o.mOutputText, mOutputText)
|
|
|
|
&& o.mActionFlags == mActionFlags
|
|
|
|
&& o.mLabelFlags == mLabelFlags;
|
2011-12-16 06:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return mHashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
return o instanceof Key && equals((Key)o);
|
2010-12-03 04:17:26 +00:00
|
|
|
}
|
|
|
|
|
2012-01-26 09:03:30 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2012-02-02 12:24:09 +00:00
|
|
|
return String.format("%s/%s %d,%d %dx%d %s/%s/%s",
|
|
|
|
Keyboard.printableCode(mCode), mLabel, 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
|
|
|
}
|
|
|
|
|
|
|
|
private static String backgroundName(int backgroundType) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-18 10:54:08 +00:00
|
|
|
public void markAsLeftEdge(Keyboard.Params params) {
|
2011-09-28 09:51:57 +00:00
|
|
|
mHitBox.left = params.mHorizontalEdgesPadding;
|
|
|
|
}
|
|
|
|
|
2011-12-18 10:54:08 +00:00
|
|
|
public void markAsRightEdge(Keyboard.Params params) {
|
2011-09-28 09:51:57 +00:00
|
|
|
mHitBox.right = params.mOccupiedWidth - params.mHorizontalEdgesPadding;
|
|
|
|
}
|
|
|
|
|
2011-12-18 10:54:08 +00:00
|
|
|
public void markAsTopEdge(Keyboard.Params params) {
|
2011-09-28 09:51:57 +00:00
|
|
|
mHitBox.top = params.mTopPadding;
|
|
|
|
}
|
|
|
|
|
2011-12-18 10:54:08 +00:00
|
|
|
public void markAsBottomEdge(Keyboard.Params 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
|
|
|
}
|
|
|
|
|
2011-11-29 07:56:27 +00:00
|
|
|
public boolean isShift() {
|
2012-04-19 21:24:51 +00:00
|
|
|
return mCode == CODE_SHIFT;
|
2011-11-29 07:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2011-11-22 03:55:38 +00:00
|
|
|
public boolean isRepeatable() {
|
|
|
|
return (mActionFlags & ACTION_FLAGS_IS_REPEATABLE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean noKeyPreview() {
|
|
|
|
return (mActionFlags & ACTION_FLAGS_NO_KEY_PREVIEW) != 0;
|
|
|
|
}
|
|
|
|
|
2011-11-30 08:54:58 +00:00
|
|
|
public boolean altCodeWhileTyping() {
|
|
|
|
return (mActionFlags & ACTION_FLAGS_ALT_CODE_WHILE_TYPING) != 0;
|
2011-11-23 02:07:43 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 06:07:25 +00:00
|
|
|
public boolean isLongPressEnabled() {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2011-06-25 10:38:55 +00:00
|
|
|
public Typeface selectTypeface(Typeface defaultTypeface) {
|
|
|
|
// 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 {
|
|
|
|
return defaultTypeface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-18 08:07:14 +00:00
|
|
|
public int selectTextSize(int letterSize, int largeLetterSize, int labelSize,
|
|
|
|
int largeLabelSize, int hintLabelSize) {
|
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-05-18 08:07:14 +00:00
|
|
|
return letterSize;
|
|
|
|
case LABEL_FLAGS_FOLLOW_KEY_LARGE_LETTER_RATIO:
|
|
|
|
return largeLetterSize;
|
2012-04-11 03:34:30 +00:00
|
|
|
case LABEL_FLAGS_FOLLOW_KEY_LABEL_RATIO:
|
2012-05-18 08:07:14 +00:00
|
|
|
return labelSize;
|
2012-05-17 10:30:38 +00:00
|
|
|
case LABEL_FLAGS_FOLLOW_KEY_LARGE_LABEL_RATIO:
|
2012-05-18 08:07:14 +00:00
|
|
|
return largeLabelSize;
|
2012-04-11 03:34:30 +00:00
|
|
|
case LABEL_FLAGS_FOLLOW_KEY_HINT_LABEL_RATIO:
|
2012-05-18 08:07:14 +00:00
|
|
|
return hintLabelSize;
|
2012-04-11 03:34:30 +00:00
|
|
|
default: // No follow key ratio flag specified.
|
2012-05-18 08:07:14 +00:00
|
|
|
return StringUtils.codePointCount(mLabel) == 1 ? letterSize : labelSize;
|
2011-06-25 10:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-03 04:29:24 +00:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
|
|
|
public 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
|
|
|
}
|
|
|
|
|
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2011-06-15 03:36:53 +00:00
|
|
|
public 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-01-26 09:03:30 +00:00
|
|
|
public boolean hasShiftedLetterHint() {
|
|
|
|
return (mLabelFlags & LABEL_FLAGS_HAS_SHIFTED_LETTER_HINT) != 0;
|
2011-06-15 03:36:53 +00:00
|
|
|
}
|
|
|
|
|
2011-06-25 10:38:55 +00:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2011-08-03 04:29:24 +00:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2011-09-12 07:29:24 +00:00
|
|
|
public 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-01-26 09:03:30 +00:00
|
|
|
public boolean isShiftedLetterActivated() {
|
|
|
|
return (mLabelFlags & LABEL_FLAGS_SHIFTED_LETTER_ACTIVATED) != 0;
|
2012-01-27 09:10:19 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 04:09:39 +00:00
|
|
|
public int getMoreKeysColumn() {
|
|
|
|
return mMoreKeysColumnAndFlags & MORE_KEYS_COLUMN_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFixedColumnOrderMoreKeys() {
|
|
|
|
return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_FIXED_COLUMN_ORDER) != 0;
|
|
|
|
}
|
|
|
|
|
2012-02-29 09:45:13 +00:00
|
|
|
public boolean hasLabelsInMoreKeys() {
|
|
|
|
return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_HAS_LABELS) != 0;
|
|
|
|
}
|
|
|
|
|
2012-04-11 03:34:30 +00:00
|
|
|
public int getMoreKeyLabelFlags() {
|
|
|
|
return hasLabelsInMoreKeys()
|
|
|
|
? LABEL_FLAGS_FOLLOW_KEY_LABEL_RATIO
|
|
|
|
: LABEL_FLAGS_FOLLOW_KEY_LETTER_RATIO;
|
|
|
|
}
|
|
|
|
|
2012-02-29 13:21:57 +00:00
|
|
|
public boolean needsDividersInMoreKeys() {
|
|
|
|
return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_NEEDS_DIVIDERS) != 0;
|
|
|
|
}
|
|
|
|
|
2012-03-05 02:39:07 +00:00
|
|
|
public boolean hasEmbeddedMoreKey() {
|
|
|
|
return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_EMBEDDED_MORE_KEY) != 0;
|
|
|
|
}
|
|
|
|
|
2012-05-16 08:41:56 +00:00
|
|
|
public Drawable getIcon(KeyboardIconsSet iconSet, int alpha) {
|
2012-03-05 09:01:33 +00:00
|
|
|
final int iconId = mEnabled ? mIconId : mDisabledIconId;
|
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-02-02 15:18:47 +00:00
|
|
|
public Drawable getPreviewIcon(KeyboardIconsSet iconSet) {
|
2012-04-19 21:24:51 +00:00
|
|
|
return mPreviewIconId != ICON_UNDEFINED
|
2012-02-02 15:18:47 +00:00
|
|
|
? iconSet.getIconDrawable(mPreviewIconId)
|
|
|
|
: iconSet.getIconDrawable(mIconId);
|
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
|
|
|
}
|
|
|
|
|
2011-06-23 12:23:44 +00:00
|
|
|
public boolean isEnabled() {
|
|
|
|
return mEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
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-01-10 08:24:00 +00:00
|
|
|
* @see #markAsLeftEdge(Keyboard.Params) etc.
|
2010-12-02 09:46:21 +00:00
|
|
|
*/
|
2010-12-03 00:39:42 +00:00
|
|
|
public boolean isOnKey(int x, 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
|
|
|
|
*/
|
|
|
|
public int squaredDistanceToEdge(int x, 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[])
|
|
|
|
*/
|
|
|
|
public 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 {
|
2011-12-18 10:54:08 +00:00
|
|
|
public Spacer(Resources res, Keyboard.Params params, Keyboard.Builder.Row row,
|
2012-04-05 05:59:55 +00:00
|
|
|
XmlPullParser parser) throws XmlPullParserException {
|
|
|
|
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-02-02 15:18:47 +00:00
|
|
|
protected Spacer(Keyboard.Params params, int x, int y, int width, 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
|
|
|
}
|