Key.java revision 8da9a13760896cd78235b60d0ea680ea13620532
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.inputmethod.keyboard; 18 19import android.content.res.Resources; 20import android.content.res.TypedArray; 21import android.content.res.XmlResourceParser; 22import android.graphics.Typeface; 23import android.graphics.drawable.Drawable; 24import android.text.TextUtils; 25import android.util.Xml; 26 27import com.android.inputmethod.keyboard.internal.KeyStyles; 28import com.android.inputmethod.keyboard.internal.KeyStyles.KeyStyle; 29import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; 30import com.android.inputmethod.keyboard.internal.KeyboardParams; 31import com.android.inputmethod.keyboard.internal.KeyboardParser; 32import com.android.inputmethod.keyboard.internal.KeyboardParser.ParseException; 33import com.android.inputmethod.keyboard.internal.PopupCharactersParser; 34import com.android.inputmethod.keyboard.internal.Row; 35import com.android.inputmethod.latin.R; 36 37import java.util.HashMap; 38import java.util.Map; 39 40/** 41 * Class for describing the position and characteristics of a single key in the keyboard. 42 */ 43public class Key { 44 /** 45 * The key code (unicode or custom code) that this key generates. 46 */ 47 public final int mCode; 48 49 /** Label to display */ 50 public final CharSequence mLabel; 51 /** Hint label to display on the key in conjunction with the label */ 52 public final CharSequence mHintLabel; 53 /** Option of the label */ 54 public final int mLabelOption; 55 public static final int LABEL_OPTION_ALIGN_LEFT = 0x01; 56 public static final int LABEL_OPTION_ALIGN_RIGHT = 0x02; 57 public static final int LABEL_OPTION_ALIGN_LEFT_OF_CENTER = 0x08; 58 private static final int LABEL_OPTION_LARGE_LETTER = 0x10; 59 private static final int LABEL_OPTION_FONT_NORMAL = 0x20; 60 private static final int LABEL_OPTION_FONT_MONO_SPACE = 0x40; 61 private static final int LABEL_OPTION_FOLLOW_KEY_LETTER_RATIO = 0x80; 62 private static final int LABEL_OPTION_FOLLOW_KEY_HINT_LABEL_RATIO = 0x100; 63 private static final int LABEL_OPTION_HAS_POPUP_HINT = 0x200; 64 private static final int LABEL_OPTION_HAS_UPPERCASE_LETTER = 0x400; 65 private static final int LABEL_OPTION_HAS_HINT_LABEL = 0x800; 66 67 /** Icon to display instead of a label. Icon takes precedence over a label */ 68 private Drawable mIcon; 69 /** Preview version of the icon, for the preview popup */ 70 private Drawable mPreviewIcon; 71 72 /** Width of the key, not including the gap */ 73 public final int mWidth; 74 /** Height of the key, not including the gap */ 75 public final int mHeight; 76 /** The horizontal gap around this key */ 77 public final int mHorizontalGap; 78 /** The vertical gap below this key */ 79 public final int mVerticalGap; 80 /** The visual insets */ 81 public final int mVisualInsetsLeft; 82 public final int mVisualInsetsRight; 83 /** Whether this key is sticky, i.e., a toggle key */ 84 public final boolean mSticky; 85 /** X coordinate of the key in the keyboard layout */ 86 public final int mX; 87 /** Y coordinate of the key in the keyboard layout */ 88 public final int mY; 89 /** Text to output when pressed. This can be multiple characters, like ".com" */ 90 public final CharSequence mOutputText; 91 /** Popup characters */ 92 public final CharSequence[] mPopupCharacters; 93 /** Popup keyboard maximum column number */ 94 public final int mMaxPopupColumn; 95 96 /** 97 * Flags that specify the anchoring to edges of the keyboard for detecting touch events 98 * that are just out of the boundary of the key. This is a bit mask of 99 * {@link Keyboard#EDGE_LEFT}, {@link Keyboard#EDGE_RIGHT}, 100 * {@link Keyboard#EDGE_TOP} and {@link Keyboard#EDGE_BOTTOM}. 101 */ 102 private int mEdgeFlags; 103 /** Whether this is a functional key which has different key top than normal key */ 104 public final boolean mFunctional; 105 /** Whether this key repeats itself when held down */ 106 public final boolean mRepeatable; 107 108 /** The current pressed state of this key */ 109 private boolean mPressed; 110 /** If this is a sticky key, is its highlight on? */ 111 private boolean mHighlightOn; 112 /** Key is enabled and responds on press */ 113 private boolean mEnabled = true; 114 115 // keyWidth constants 116 private static final int KEYWIDTH_FILL_RIGHT = 0; 117 private static final int KEYWIDTH_FILL_BOTH = -1; 118 119 private final static int[] KEY_STATE_NORMAL_ON = { 120 android.R.attr.state_checkable, 121 android.R.attr.state_checked 122 }; 123 124 private final static int[] KEY_STATE_PRESSED_ON = { 125 android.R.attr.state_pressed, 126 android.R.attr.state_checkable, 127 android.R.attr.state_checked 128 }; 129 130 private final static int[] KEY_STATE_NORMAL_OFF = { 131 android.R.attr.state_checkable 132 }; 133 134 private final static int[] KEY_STATE_PRESSED_OFF = { 135 android.R.attr.state_pressed, 136 android.R.attr.state_checkable 137 }; 138 139 private final static int[] KEY_STATE_NORMAL = { 140 }; 141 142 private final static int[] KEY_STATE_PRESSED = { 143 android.R.attr.state_pressed 144 }; 145 146 // functional normal state (with properties) 147 private static final int[] KEY_STATE_FUNCTIONAL_NORMAL = { 148 android.R.attr.state_single 149 }; 150 151 // functional pressed state (with properties) 152 private static final int[] KEY_STATE_FUNCTIONAL_PRESSED = { 153 android.R.attr.state_single, 154 android.R.attr.state_pressed 155 }; 156 157 // RTL parenthesis character swapping map. 158 private static final Map<Integer, Integer> sRtlParenthesisMap = new HashMap<Integer, Integer>(); 159 160 static { 161 // The all letters need to be mirrored are found at 162 // http://www.unicode.org/Public/6.0.0/ucd/extracted/DerivedBinaryProperties.txt 163 addRtlParenthesisPair('(', ')'); 164 addRtlParenthesisPair('[', ']'); 165 addRtlParenthesisPair('{', '}'); 166 addRtlParenthesisPair('<', '>'); 167 // \u00ab: LEFT-POINTING DOUBLE ANGLE QUOTATION MARK 168 // \u00bb: RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK 169 addRtlParenthesisPair('\u00ab', '\u00bb'); 170 // \u2039: SINGLE LEFT-POINTING ANGLE QUOTATION MARK 171 // \u203a: SINGLE RIGHT-POINTING ANGLE QUOTATION MARK 172 addRtlParenthesisPair('\u2039', '\u203a'); 173 // \u2264: LESS-THAN OR EQUAL TO 174 // \u2265: GREATER-THAN OR EQUAL TO 175 addRtlParenthesisPair('\u2264', '\u2265'); 176 } 177 178 private static void addRtlParenthesisPair(int left, int right) { 179 sRtlParenthesisMap.put(left, right); 180 sRtlParenthesisMap.put(right, left); 181 } 182 183 public static int getRtlParenthesisCode(int code) { 184 if (sRtlParenthesisMap.containsKey(code)) { 185 return sRtlParenthesisMap.get(code); 186 } else { 187 return code; 188 } 189 } 190 191 /** 192 * This constructor is being used only for key in popup mini keyboard. 193 */ 194 public Key(Resources res, KeyboardParams params, CharSequence popupCharacter, int x, int y, 195 int width, int height, int edgeFlags) { 196 mHeight = height - params.mVerticalGap; 197 mHorizontalGap = params.mHorizontalGap; 198 mVerticalGap = params.mVerticalGap; 199 mVisualInsetsLeft = mVisualInsetsRight = 0; 200 mWidth = width - mHorizontalGap; 201 mEdgeFlags = edgeFlags; 202 mHintLabel = null; 203 mLabelOption = 0; 204 mFunctional = false; 205 mSticky = false; 206 mRepeatable = false; 207 mPopupCharacters = null; 208 mMaxPopupColumn = 0; 209 final String popupSpecification = popupCharacter.toString(); 210 mLabel = PopupCharactersParser.getLabel(popupSpecification); 211 mOutputText = PopupCharactersParser.getOutputText(popupSpecification); 212 final int code = PopupCharactersParser.getCode(res, popupSpecification); 213 mCode = params.mIsRtlKeyboard ? getRtlParenthesisCode(code) : code; 214 mIcon = params.mIconsSet.getIcon(PopupCharactersParser.getIconId(popupSpecification)); 215 // Horizontal gap is divided equally to both sides of the key. 216 mX = x + mHorizontalGap / 2; 217 mY = y; 218 } 219 220 /** 221 * Create a key with the given top-left coordinate and extract its attributes from the XML 222 * parser. 223 * @param res resources associated with the caller's context 224 * @param params the keyboard building parameters. 225 * @param row the row that this key belongs to. 226 * @param x the x coordinate of the top-left 227 * @param y the y coordinate of the top-left 228 * @param parser the XML parser containing the attributes for this key 229 * @param keyStyles active key styles set 230 */ 231 public Key(Resources res, KeyboardParams params, Row row, int x, int y, 232 XmlResourceParser parser, KeyStyles keyStyles) { 233 234 final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser), 235 R.styleable.Keyboard); 236 int keyWidth; 237 try { 238 mHeight = KeyboardParser.getDimensionOrFraction(keyboardAttr, 239 R.styleable.Keyboard_rowHeight, 240 params.mHeight, row.mRowHeight) - params.mVerticalGap; 241 mHorizontalGap = KeyboardParser.getDimensionOrFraction(keyboardAttr, 242 R.styleable.Keyboard_horizontalGap, 243 params.mWidth, params.mHorizontalGap); 244 mVerticalGap = params.mVerticalGap; 245 keyWidth = KeyboardParser.getDimensionOrFraction(keyboardAttr, 246 R.styleable.Keyboard_keyWidth, 247 params.mWidth, row.mDefaultKeyWidth); 248 } finally { 249 keyboardAttr.recycle(); 250 } 251 252 final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser), 253 R.styleable.Keyboard_Key); 254 try { 255 final KeyStyle style; 256 if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyStyle)) { 257 String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle); 258 style = keyStyles.getKeyStyle(styleName); 259 if (style == null) 260 throw new ParseException("Unknown key style: " + styleName, parser); 261 } else { 262 style = keyStyles.getEmptyKeyStyle(); 263 } 264 265 final int keyboardWidth = params.mOccupiedWidth; 266 int keyXPos = KeyboardParser.getDimensionOrFraction(keyAttr, 267 R.styleable.Keyboard_Key_keyXPos, keyboardWidth, x); 268 if (keyXPos < 0) { 269 // If keyXPos is negative, the actual x-coordinate will be k + keyXPos. 270 keyXPos += keyboardWidth; 271 if (keyXPos < x) { 272 // keyXPos shouldn't be less than x because drawable area for this key starts 273 // at x. Or, this key will overlaps the adjacent key on its left hand side. 274 keyXPos = x; 275 } 276 } 277 if (keyWidth == KEYWIDTH_FILL_RIGHT) { 278 // If keyWidth is zero, the actual key width will be determined to fill out the 279 // area up to the right edge of the keyboard. 280 keyWidth = keyboardWidth - keyXPos; 281 } else if (keyWidth <= KEYWIDTH_FILL_BOTH) { 282 // If keyWidth is negative, the actual key width will be determined to fill out the 283 // area between the nearest key on the left hand side and the right edge of the 284 // keyboard. 285 keyXPos = x; 286 keyWidth = keyboardWidth - keyXPos; 287 } 288 289 // Horizontal gap is divided equally to both sides of the key. 290 mX = keyXPos + mHorizontalGap / 2; 291 mY = y; 292 mWidth = keyWidth - mHorizontalGap; 293 294 CharSequence[] popupCharacters = style.getTextArray( 295 keyAttr, R.styleable.Keyboard_Key_popupCharacters); 296 if (params.mId.mPasswordInput) { 297 popupCharacters = PopupCharactersParser.filterOut( 298 res, popupCharacters, PopupCharactersParser.NON_ASCII_FILTER); 299 } 300 // In Arabic symbol layouts, we'd like to keep digits in popup characters regardless of 301 // config_digit_popup_characters_enabled. 302 if (params.mId.isAlphabetKeyboard() && !res.getBoolean( 303 R.bool.config_digit_popup_characters_enabled)) { 304 mPopupCharacters = PopupCharactersParser.filterOut( 305 res, popupCharacters, PopupCharactersParser.DIGIT_FILTER); 306 } else { 307 mPopupCharacters = popupCharacters; 308 } 309 mMaxPopupColumn = style.getInt(keyboardAttr, 310 R.styleable.Keyboard_Key_maxPopupKeyboardColumn, 311 params.mMaxPopupColumn); 312 313 mRepeatable = style.getBoolean(keyAttr, R.styleable.Keyboard_Key_isRepeatable, false); 314 mFunctional = style.getBoolean(keyAttr, R.styleable.Keyboard_Key_isFunctional, false); 315 mSticky = style.getBoolean(keyAttr, R.styleable.Keyboard_Key_isSticky, false); 316 mEnabled = style.getBoolean(keyAttr, R.styleable.Keyboard_Key_enabled, true); 317 mEdgeFlags = 0; 318 319 final KeyboardIconsSet iconsSet = params.mIconsSet; 320 mVisualInsetsLeft = KeyboardParser.getDimensionOrFraction(keyAttr, 321 R.styleable.Keyboard_Key_visualInsetsLeft, keyboardWidth, 0); 322 mVisualInsetsRight = KeyboardParser.getDimensionOrFraction(keyAttr, 323 R.styleable.Keyboard_Key_visualInsetsRight, keyboardWidth, 0); 324 mPreviewIcon = iconsSet.getIcon(style.getInt( 325 keyAttr, R.styleable.Keyboard_Key_keyIconPreview, 326 KeyboardIconsSet.ICON_UNDEFINED)); 327 mIcon = iconsSet.getIcon(style.getInt( 328 keyAttr, R.styleable.Keyboard_Key_keyIcon, 329 KeyboardIconsSet.ICON_UNDEFINED)); 330 final int shiftedIconId = style.getInt(keyAttr, R.styleable.Keyboard_Key_keyIconShifted, 331 KeyboardIconsSet.ICON_UNDEFINED); 332 if (shiftedIconId != KeyboardIconsSet.ICON_UNDEFINED) { 333 final Drawable shiftedIcon = iconsSet.getIcon(shiftedIconId); 334 params.addShiftedIcon(this, shiftedIcon); 335 } 336 mHintLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyHintLabel); 337 338 mLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyLabel); 339 mLabelOption = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelOption, 0); 340 mOutputText = style.getText(keyAttr, R.styleable.Keyboard_Key_keyOutputText); 341 // Choose the first letter of the label as primary code if not 342 // specified. 343 final int code = style.getInt(keyAttr, R.styleable.Keyboard_Key_code, 344 Keyboard.CODE_UNSPECIFIED); 345 if (code == Keyboard.CODE_UNSPECIFIED && !TextUtils.isEmpty(mLabel)) { 346 final int firstChar = mLabel.charAt(0); 347 mCode = params.mIsRtlKeyboard ? getRtlParenthesisCode(firstChar) : firstChar; 348 } else if (code != Keyboard.CODE_UNSPECIFIED) { 349 mCode = code; 350 } else { 351 mCode = Keyboard.CODE_DUMMY; 352 } 353 } finally { 354 keyAttr.recycle(); 355 } 356 } 357 358 public void addEdgeFlags(int flags) { 359 mEdgeFlags |= flags; 360 } 361 362 public Typeface selectTypeface(Typeface defaultTypeface) { 363 // TODO: Handle "bold" here too? 364 if ((mLabelOption & LABEL_OPTION_FONT_NORMAL) != 0) { 365 return Typeface.DEFAULT; 366 } else if ((mLabelOption & LABEL_OPTION_FONT_MONO_SPACE) != 0) { 367 return Typeface.MONOSPACE; 368 } else { 369 return defaultTypeface; 370 } 371 } 372 373 public int selectTextSize(int letter, int largeLetter, int label, int hintLabel) { 374 if (mLabel.length() > 1 375 && (mLabelOption & (LABEL_OPTION_FOLLOW_KEY_LETTER_RATIO 376 | LABEL_OPTION_FOLLOW_KEY_HINT_LABEL_RATIO)) == 0) { 377 return label; 378 } else if ((mLabelOption & LABEL_OPTION_FOLLOW_KEY_HINT_LABEL_RATIO) != 0) { 379 return hintLabel; 380 } else if ((mLabelOption & LABEL_OPTION_LARGE_LETTER) != 0) { 381 return largeLetter; 382 } else { 383 return letter; 384 } 385 } 386 387 public boolean hasPopupHint() { 388 return (mLabelOption & LABEL_OPTION_HAS_POPUP_HINT) != 0; 389 } 390 391 public boolean hasUppercaseLetter() { 392 return (mLabelOption & LABEL_OPTION_HAS_UPPERCASE_LETTER) != 0; 393 } 394 395 public boolean hasHintLabel() { 396 return (mLabelOption & LABEL_OPTION_HAS_HINT_LABEL) != 0; 397 } 398 399 public Drawable getIcon() { 400 return mIcon; 401 } 402 403 public Drawable getPreviewIcon() { 404 return mPreviewIcon; 405 } 406 407 public void setIcon(Drawable icon) { 408 mIcon = icon; 409 } 410 411 public void setPreviewIcon(Drawable icon) { 412 mPreviewIcon = icon; 413 } 414 415 /** 416 * Informs the key that it has been pressed, in case it needs to change its appearance or 417 * state. 418 * @see #onReleased() 419 */ 420 public void onPressed() { 421 mPressed = true; 422 } 423 424 /** 425 * Informs the key that it has been released, in case it needs to change its appearance or 426 * state. 427 * @see #onPressed() 428 */ 429 public void onReleased() { 430 mPressed = false; 431 } 432 433 public void setHighlightOn(boolean highlightOn) { 434 mHighlightOn = highlightOn; 435 } 436 437 public boolean isEnabled() { 438 return mEnabled; 439 } 440 441 public void setEnabled(boolean enabled) { 442 mEnabled = enabled; 443 } 444 445 /** 446 * Detects if a point falls on this key. 447 * @param x the x-coordinate of the point 448 * @param y the y-coordinate of the point 449 * @return whether or not the point falls on the key. If the key is attached to an edge, it will 450 * assume that all points between the key and the edge are considered to be on the key. 451 */ 452 public boolean isOnKey(int x, int y) { 453 final int left = mX - mHorizontalGap / 2; 454 final int right = left + mWidth + mHorizontalGap; 455 final int top = mY; 456 final int bottom = top + mHeight + mVerticalGap; 457 final int flags = mEdgeFlags; 458 if (flags == 0) { 459 return x >= left && x <= right && y >= top && y <= bottom; 460 } 461 final boolean leftEdge = (flags & Keyboard.EDGE_LEFT) != 0; 462 final boolean rightEdge = (flags & Keyboard.EDGE_RIGHT) != 0; 463 final boolean topEdge = (flags & Keyboard.EDGE_TOP) != 0; 464 final boolean bottomEdge = (flags & Keyboard.EDGE_BOTTOM) != 0; 465 // In order to mitigate rounding errors, we use (left <= x <= right) here. 466 return (x >= left || leftEdge) && (x <= right || rightEdge) 467 && (y >= top || topEdge) && (y <= bottom || bottomEdge); 468 } 469 470 /** 471 * Returns the square of the distance to the nearest edge of the key and the given point. 472 * @param x the x-coordinate of the point 473 * @param y the y-coordinate of the point 474 * @return the square of the distance of the point from the nearest edge of the key 475 */ 476 public int squaredDistanceToEdge(int x, int y) { 477 final int left = mX; 478 final int right = left + mWidth; 479 final int top = mY; 480 final int bottom = top + mHeight; 481 final int edgeX = x < left ? left : (x > right ? right : x); 482 final int edgeY = y < top ? top : (y > bottom ? bottom : y); 483 final int dx = x - edgeX; 484 final int dy = y - edgeY; 485 return dx * dx + dy * dy; 486 } 487 488 /** 489 * Returns the drawable state for the key, based on the current state and type of the key. 490 * @return the drawable state of the key. 491 * @see android.graphics.drawable.StateListDrawable#setState(int[]) 492 */ 493 public int[] getCurrentDrawableState() { 494 final boolean pressed = mPressed; 495 if (!mSticky && mFunctional) { 496 if (pressed) { 497 return KEY_STATE_FUNCTIONAL_PRESSED; 498 } else { 499 return KEY_STATE_FUNCTIONAL_NORMAL; 500 } 501 } 502 503 int[] states = KEY_STATE_NORMAL; 504 505 if (mHighlightOn) { 506 if (pressed) { 507 states = KEY_STATE_PRESSED_ON; 508 } else { 509 states = KEY_STATE_NORMAL_ON; 510 } 511 } else { 512 if (mSticky) { 513 if (pressed) { 514 states = KEY_STATE_PRESSED_OFF; 515 } else { 516 states = KEY_STATE_NORMAL_OFF; 517 } 518 } else { 519 if (pressed) { 520 states = KEY_STATE_PRESSED; 521 } 522 } 523 } 524 return states; 525 } 526} 527