| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.inputmethodservice; |
| 18 |
|
| 19 |
import android.annotation.XmlRes; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.res.Resources; |
| 23 |
import android.content.res.TypedArray; |
| 24 |
import android.content.res.XmlResourceParser; |
| 25 |
import android.graphics.drawable.Drawable; |
| 26 |
import android.os.Build; |
| 27 |
import android.text.TextUtils; |
| 28 |
import android.util.DisplayMetrics; |
| 29 |
import android.util.Log; |
| 30 |
import android.util.TypedValue; |
| 31 |
import android.util.Xml; |
| 32 |
|
| 33 |
import org.xmlpull.v1.XmlPullParserException; |
| 34 |
|
| 35 |
import java.io.IOException; |
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.List; |
| 38 |
import java.util.StringTokenizer; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
@Deprecated |
| 68 |
public class Keyboard { |
| 69 |
|
| 70 |
static final String TAG = "Keyboard"; |
| 71 |
|
| 72 |
|
| 73 |
private static final String TAG_KEYBOARD = "Keyboard"; |
| 74 |
private static final String TAG_ROW = "Row"; |
| 75 |
private static final String TAG_KEY = "Key"; |
| 76 |
|
| 77 |
public static final int EDGE_LEFT = 0x01; |
| 78 |
public static final int EDGE_RIGHT = 0x02; |
| 79 |
public static final int EDGE_TOP = 0x04; |
| 80 |
public static final int EDGE_BOTTOM = 0x08; |
| 81 |
|
| 82 |
public static final int KEYCODE_SHIFT = -1; |
| 83 |
public static final int KEYCODE_MODE_CHANGE = -2; |
| 84 |
public static final int KEYCODE_CANCEL = -3; |
| 85 |
public static final int KEYCODE_DONE = -4; |
| 86 |
public static final int KEYCODE_DELETE = -5; |
| 87 |
public static final int KEYCODE_ALT = -6; |
| 88 |
|
| 89 |
|
| 90 |
private CharSequence mLabel; |
| 91 |
|
| 92 |
|
| 93 |
private int mDefaultHorizontalGap; |
| 94 |
|
| 95 |
|
| 96 |
private int mDefaultWidth; |
| 97 |
|
| 98 |
|
| 99 |
private int mDefaultHeight; |
| 100 |
|
| 101 |
|
| 102 |
private int mDefaultVerticalGap; |
| 103 |
|
| 104 |
|
| 105 |
private boolean mShifted; |
| 106 |
|
| 107 |
|
| 108 |
private Key[] mShiftKeys = { null, null }; |
| 109 |
|
| 110 |
|
| 111 |
private int[] mShiftKeyIndices = {-1, -1}; |
| 112 |
|
| 113 |
|
| 114 |
private int mKeyWidth; |
| 115 |
|
| 116 |
|
| 117 |
private int mKeyHeight; |
| 118 |
|
| 119 |
|
| 120 |
@UnsupportedAppUsage |
| 121 |
private int mTotalHeight; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
@UnsupportedAppUsage |
| 128 |
private int mTotalWidth; |
| 129 |
|
| 130 |
|
| 131 |
private List<Key> mKeys; |
| 132 |
|
| 133 |
|
| 134 |
@UnsupportedAppUsage |
| 135 |
private List<Key> mModifierKeys; |
| 136 |
|
| 137 |
|
| 138 |
private int mDisplayWidth; |
| 139 |
|
| 140 |
|
| 141 |
private int mDisplayHeight; |
| 142 |
|
| 143 |
|
| 144 |
private int mKeyboardMode; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private static final int GRID_WIDTH = 10; |
| 149 |
private static final int GRID_HEIGHT = 5; |
| 150 |
private static final int GRID_SIZE = GRID_WIDTH * GRID_HEIGHT; |
| 151 |
private int mCellWidth; |
| 152 |
private int mCellHeight; |
| 153 |
private int[][] mGridNeighbors; |
| 154 |
private int mProximityThreshold; |
| 155 |
|
| 156 |
private static float SEARCH_DISTANCE = 1.8f; |
| 157 |
|
| 158 |
private ArrayList<Row> rows = new ArrayList<Row>(); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public static class Row { |
| 172 |
|
| 173 |
public int defaultWidth; |
| 174 |
|
| 175 |
public int defaultHeight; |
| 176 |
|
| 177 |
public int defaultHorizontalGap; |
| 178 |
|
| 179 |
public int verticalGap; |
| 180 |
|
| 181 |
ArrayList<Key> mKeys = new ArrayList<Key>(); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public int rowEdgeFlags; |
| 188 |
|
| 189 |
|
| 190 |
public int mode; |
| 191 |
|
| 192 |
private Keyboard parent; |
| 193 |
|
| 194 |
public Row(Keyboard parent) { |
| 195 |
this.parent = parent; |
| 196 |
} |
| 197 |
|
| 198 |
public Row(Resources res, Keyboard parent, XmlResourceParser parser) { |
| 199 |
this.parent = parent; |
| 200 |
TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), |
| 201 |
com.android.internal.R.styleable.Keyboard); |
| 202 |
defaultWidth = getDimensionOrFraction(a, |
| 203 |
com.android.internal.R.styleable.Keyboard_keyWidth, |
| 204 |
parent.mDisplayWidth, parent.mDefaultWidth); |
| 205 |
defaultHeight = getDimensionOrFraction(a, |
| 206 |
com.android.internal.R.styleable.Keyboard_keyHeight, |
| 207 |
parent.mDisplayHeight, parent.mDefaultHeight); |
| 208 |
defaultHorizontalGap = getDimensionOrFraction(a, |
| 209 |
com.android.internal.R.styleable.Keyboard_horizontalGap, |
| 210 |
parent.mDisplayWidth, parent.mDefaultHorizontalGap); |
| 211 |
verticalGap = getDimensionOrFraction(a, |
| 212 |
com.android.internal.R.styleable.Keyboard_verticalGap, |
| 213 |
parent.mDisplayHeight, parent.mDefaultVerticalGap); |
| 214 |
a.recycle(); |
| 215 |
a = res.obtainAttributes(Xml.asAttributeSet(parser), |
| 216 |
com.android.internal.R.styleable.Keyboard_Row); |
| 217 |
rowEdgeFlags = a.getInt(com.android.internal.R.styleable.Keyboard_Row_rowEdgeFlags, 0); |
| 218 |
mode = a.getResourceId(com.android.internal.R.styleable.Keyboard_Row_keyboardMode, |
| 219 |
0); |
| 220 |
a.recycle(); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public static class Key { |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public int[] codes; |
| 248 |
|
| 249 |
|
| 250 |
public CharSequence label; |
| 251 |
|
| 252 |
|
| 253 |
public Drawable icon; |
| 254 |
|
| 255 |
public Drawable iconPreview; |
| 256 |
|
| 257 |
public int width; |
| 258 |
|
| 259 |
public int height; |
| 260 |
|
| 261 |
public int gap; |
| 262 |
|
| 263 |
public boolean sticky; |
| 264 |
|
| 265 |
public int x; |
| 266 |
|
| 267 |
public int y; |
| 268 |
|
| 269 |
public boolean pressed; |
| 270 |
|
| 271 |
public boolean on; |
| 272 |
|
| 273 |
public CharSequence text; |
| 274 |
|
| 275 |
public CharSequence popupCharacters; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public int edgeFlags; |
| 284 |
|
| 285 |
public boolean modifier; |
| 286 |
|
| 287 |
private Keyboard keyboard; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public int popupResId; |
| 293 |
|
| 294 |
public boolean repeatable; |
| 295 |
|
| 296 |
|
| 297 |
private final static int[] KEY_STATE_NORMAL_ON = { |
| 298 |
android.R.attr.state_checkable, |
| 299 |
android.R.attr.state_checked |
| 300 |
}; |
| 301 |
|
| 302 |
private final static int[] KEY_STATE_PRESSED_ON = { |
| 303 |
android.R.attr.state_pressed, |
| 304 |
android.R.attr.state_checkable, |
| 305 |
android.R.attr.state_checked |
| 306 |
}; |
| 307 |
|
| 308 |
private final static int[] KEY_STATE_NORMAL_OFF = { |
| 309 |
android.R.attr.state_checkable |
| 310 |
}; |
| 311 |
|
| 312 |
private final static int[] KEY_STATE_PRESSED_OFF = { |
| 313 |
android.R.attr.state_pressed, |
| 314 |
android.R.attr.state_checkable |
| 315 |
}; |
| 316 |
|
| 317 |
private final static int[] KEY_STATE_NORMAL = { |
| 318 |
}; |
| 319 |
|
| 320 |
private final static int[] KEY_STATE_PRESSED = { |
| 321 |
android.R.attr.state_pressed |
| 322 |
}; |
| 323 |
|
| 324 |
|
| 325 |
public Key(Row parent) { |
| 326 |
keyboard = parent.parent; |
| 327 |
height = parent.defaultHeight; |
| 328 |
width = parent.defaultWidth; |
| 329 |
gap = parent.defaultHorizontalGap; |
| 330 |
edgeFlags = parent.rowEdgeFlags; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public Key(Resources res, Row parent, int x, int y, XmlResourceParser parser) { |
| 343 |
this(parent); |
| 344 |
|
| 345 |
this.x = x; |
| 346 |
this.y = y; |
| 347 |
|
| 348 |
TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), |
| 349 |
com.android.internal.R.styleable.Keyboard); |
| 350 |
|
| 351 |
width = getDimensionOrFraction(a, |
| 352 |
com.android.internal.R.styleable.Keyboard_keyWidth, |
| 353 |
keyboard.mDisplayWidth, parent.defaultWidth); |
| 354 |
height = getDimensionOrFraction(a, |
| 355 |
com.android.internal.R.styleable.Keyboard_keyHeight, |
| 356 |
keyboard.mDisplayHeight, parent.defaultHeight); |
| 357 |
gap = getDimensionOrFraction(a, |
| 358 |
com.android.internal.R.styleable.Keyboard_horizontalGap, |
| 359 |
keyboard.mDisplayWidth, parent.defaultHorizontalGap); |
| 360 |
a.recycle(); |
| 361 |
a = res.obtainAttributes(Xml.asAttributeSet(parser), |
| 362 |
com.android.internal.R.styleable.Keyboard_Key); |
| 363 |
this.x += gap; |
| 364 |
TypedValue codesValue = new TypedValue(); |
| 365 |
a.getValue(com.android.internal.R.styleable.Keyboard_Key_codes, |
| 366 |
codesValue); |
| 367 |
if (codesValue.type == TypedValue.TYPE_INT_DEC |
| 368 |
|| codesValue.type == TypedValue.TYPE_INT_HEX) { |
| 369 |
codes = new int[] { codesValue.data }; |
| 370 |
} else if (codesValue.type == TypedValue.TYPE_STRING) { |
| 371 |
codes = parseCSV(codesValue.string.toString()); |
| 372 |
} |
| 373 |
|
| 374 |
iconPreview = a.getDrawable(com.android.internal.R.styleable.Keyboard_Key_iconPreview); |
| 375 |
if (iconPreview != null) { |
| 376 |
iconPreview.setBounds(0, 0, iconPreview.getIntrinsicWidth(), |
| 377 |
iconPreview.getIntrinsicHeight()); |
| 378 |
} |
| 379 |
popupCharacters = a.getText( |
| 380 |
com.android.internal.R.styleable.Keyboard_Key_popupCharacters); |
| 381 |
popupResId = a.getResourceId( |
| 382 |
com.android.internal.R.styleable.Keyboard_Key_popupKeyboard, 0); |
| 383 |
repeatable = a.getBoolean( |
| 384 |
com.android.internal.R.styleable.Keyboard_Key_isRepeatable, false); |
| 385 |
modifier = a.getBoolean( |
| 386 |
com.android.internal.R.styleable.Keyboard_Key_isModifier, false); |
| 387 |
sticky = a.getBoolean( |
| 388 |
com.android.internal.R.styleable.Keyboard_Key_isSticky, false); |
| 389 |
edgeFlags = a.getInt(com.android.internal.R.styleable.Keyboard_Key_keyEdgeFlags, 0); |
| 390 |
edgeFlags |= parent.rowEdgeFlags; |
| 391 |
|
| 392 |
icon = a.getDrawable( |
| 393 |
com.android.internal.R.styleable.Keyboard_Key_keyIcon); |
| 394 |
if (icon != null) { |
| 395 |
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); |
| 396 |
} |
| 397 |
label = a.getText(com.android.internal.R.styleable.Keyboard_Key_keyLabel); |
| 398 |
text = a.getText(com.android.internal.R.styleable.Keyboard_Key_keyOutputText); |
| 399 |
|
| 400 |
if (codes == null && !TextUtils.isEmpty(label)) { |
| 401 |
codes = new int[] { label.charAt(0) }; |
| 402 |
} |
| 403 |
a.recycle(); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public void onPressed() { |
| 412 |
pressed = !pressed; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public void onReleased(boolean inside) { |
| 433 |
pressed = !pressed; |
| 434 |
if (sticky && inside) { |
| 435 |
on = !on; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
int[] parseCSV(String value) { |
| 440 |
int count = 0; |
| 441 |
int lastIndex = 0; |
| 442 |
if (value.length() > 0) { |
| 443 |
count++; |
| 444 |
while ((lastIndex = value.indexOf(",", lastIndex + 1)) > 0) { |
| 445 |
count++; |
| 446 |
} |
| 447 |
} |
| 448 |
int[] values = new int[count]; |
| 449 |
count = 0; |
| 450 |
StringTokenizer st = new StringTokenizer(value, ","); |
| 451 |
while (st.hasMoreTokens()) { |
| 452 |
try { |
| 453 |
values[count++] = Integer.parseInt(st.nextToken()); |
| 454 |
} catch (NumberFormatException nfe) { |
| 455 |
Log.e(TAG, "Error parsing keycodes " + value); |
| 456 |
} |
| 457 |
} |
| 458 |
return values; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public boolean isInside(int x, int y) { |
| 470 |
boolean leftEdge = (edgeFlags & EDGE_LEFT) > 0; |
| 471 |
boolean rightEdge = (edgeFlags & EDGE_RIGHT) > 0; |
| 472 |
boolean topEdge = (edgeFlags & EDGE_TOP) > 0; |
| 473 |
boolean bottomEdge = (edgeFlags & EDGE_BOTTOM) > 0; |
| 474 |
if ((x >= this.x || (leftEdge && x <= this.x + this.width)) |
| 475 |
&& (x < this.x + this.width || (rightEdge && x >= this.x)) |
| 476 |
&& (y >= this.y || (topEdge && y <= this.y + this.height)) |
| 477 |
&& (y < this.y + this.height || (bottomEdge && y >= this.y))) { |
| 478 |
return true; |
| 479 |
} else { |
| 480 |
return false; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public int squaredDistanceFrom(int x, int y) { |
| 491 |
int xDist = this.x + width / 2 - x; |
| 492 |
int yDist = this.y + height / 2 - y; |
| 493 |
return xDist * xDist + yDist * yDist; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public int[] getCurrentDrawableState() { |
| 502 |
int[] states = KEY_STATE_NORMAL; |
| 503 |
|
| 504 |
if (on) { |
| 505 |
if (pressed) { |
| 506 |
states = KEY_STATE_PRESSED_ON; |
| 507 |
} else { |
| 508 |
states = KEY_STATE_NORMAL_ON; |
| 509 |
} |
| 510 |
} else { |
| 511 |
if (sticky) { |
| 512 |
if (pressed) { |
| 513 |
states = KEY_STATE_PRESSED_OFF; |
| 514 |
} else { |
| 515 |
states = KEY_STATE_NORMAL_OFF; |
| 516 |
} |
| 517 |
} else { |
| 518 |
if (pressed) { |
| 519 |
states = KEY_STATE_PRESSED; |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
return states; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
public Keyboard(Context context, int xmlLayoutResId) { |
| 533 |
this(context, xmlLayoutResId, 0); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public Keyboard(Context context, @XmlRes int xmlLayoutResId, int modeId, int width, |
| 546 |
int height) { |
| 547 |
mDisplayWidth = width; |
| 548 |
mDisplayHeight = height; |
| 549 |
|
| 550 |
mDefaultHorizontalGap = 0; |
| 551 |
mDefaultWidth = mDisplayWidth / 10; |
| 552 |
mDefaultVerticalGap = 0; |
| 553 |
mDefaultHeight = mDefaultWidth; |
| 554 |
mKeys = new ArrayList<Key>(); |
| 555 |
mModifierKeys = new ArrayList<Key>(); |
| 556 |
mKeyboardMode = modeId; |
| 557 |
loadKeyboard(context, context.getResources().getXml(xmlLayoutResId)); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
public Keyboard(Context context, @XmlRes int xmlLayoutResId, int modeId) { |
| 568 |
DisplayMetrics dm = context.getResources().getDisplayMetrics(); |
| 569 |
mDisplayWidth = dm.widthPixels; |
| 570 |
mDisplayHeight = dm.heightPixels; |
| 571 |
|
| 572 |
|
| 573 |
mDefaultHorizontalGap = 0; |
| 574 |
mDefaultWidth = mDisplayWidth / 10; |
| 575 |
mDefaultVerticalGap = 0; |
| 576 |
mDefaultHeight = mDefaultWidth; |
| 577 |
mKeys = new ArrayList<Key>(); |
| 578 |
mModifierKeys = new ArrayList<Key>(); |
| 579 |
mKeyboardMode = modeId; |
| 580 |
loadKeyboard(context, context.getResources().getXml(xmlLayoutResId)); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
public Keyboard(Context context, int layoutTemplateResId, |
| 598 |
CharSequence characters, int columns, int horizontalPadding) { |
| 599 |
this(context, layoutTemplateResId); |
| 600 |
int x = 0; |
| 601 |
int y = 0; |
| 602 |
int column = 0; |
| 603 |
mTotalWidth = 0; |
| 604 |
|
| 605 |
Row row = new Row(this); |
| 606 |
row.defaultHeight = mDefaultHeight; |
| 607 |
row.defaultWidth = mDefaultWidth; |
| 608 |
row.defaultHorizontalGap = mDefaultHorizontalGap; |
| 609 |
row.verticalGap = mDefaultVerticalGap; |
| 610 |
row.rowEdgeFlags = EDGE_TOP | EDGE_BOTTOM; |
| 611 |
final int maxColumns = columns == -1 ? Integer.MAX_VALUE : columns; |
| 612 |
for (int i = 0; i < characters.length(); i++) { |
| 613 |
char c = characters.charAt(i); |
| 614 |
if (column >= maxColumns |
| 615 |
|| x + mDefaultWidth + horizontalPadding > mDisplayWidth) { |
| 616 |
x = 0; |
| 617 |
y += mDefaultVerticalGap + mDefaultHeight; |
| 618 |
column = 0; |
| 619 |
} |
| 620 |
final Key key = new Key(row); |
| 621 |
key.x = x; |
| 622 |
key.y = y; |
| 623 |
key.label = String.valueOf(c); |
| 624 |
key.codes = new int[] { c }; |
| 625 |
column++; |
| 626 |
x += key.width + key.gap; |
| 627 |
mKeys.add(key); |
| 628 |
row.mKeys.add(key); |
| 629 |
if (x > mTotalWidth) { |
| 630 |
mTotalWidth = x; |
| 631 |
} |
| 632 |
} |
| 633 |
mTotalHeight = y + mDefaultHeight; |
| 634 |
rows.add(row); |
| 635 |
} |
| 636 |
|
| 637 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 638 |
final void resize(int newWidth, int newHeight) { |
| 639 |
int numRows = rows.size(); |
| 640 |
for (int rowIndex = 0; rowIndex < numRows; ++rowIndex) { |
| 641 |
Row row = rows.get(rowIndex); |
| 642 |
int numKeys = row.mKeys.size(); |
| 643 |
int totalGap = 0; |
| 644 |
int totalWidth = 0; |
| 645 |
for (int keyIndex = 0; keyIndex < numKeys; ++keyIndex) { |
| 646 |
Key key = row.mKeys.get(keyIndex); |
| 647 |
if (keyIndex > 0) { |
| 648 |
totalGap += key.gap; |
| 649 |
} |
| 650 |
totalWidth += key.width; |
| 651 |
} |
| 652 |
if (totalGap + totalWidth > newWidth) { |
| 653 |
int x = 0; |
| 654 |
float scaleFactor = (float)(newWidth - totalGap) / totalWidth; |
| 655 |
for (int keyIndex = 0; keyIndex < numKeys; ++keyIndex) { |
| 656 |
Key key = row.mKeys.get(keyIndex); |
| 657 |
key.width *= scaleFactor; |
| 658 |
key.x = x; |
| 659 |
x += key.width + key.gap; |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
mTotalWidth = newWidth; |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
} |
| 668 |
|
| 669 |
public List<Key> getKeys() { |
| 670 |
return mKeys; |
| 671 |
} |
| 672 |
|
| 673 |
public List<Key> getModifierKeys() { |
| 674 |
return mModifierKeys; |
| 675 |
} |
| 676 |
|
| 677 |
protected int getHorizontalGap() { |
| 678 |
return mDefaultHorizontalGap; |
| 679 |
} |
| 680 |
|
| 681 |
protected void setHorizontalGap(int gap) { |
| 682 |
mDefaultHorizontalGap = gap; |
| 683 |
} |
| 684 |
|
| 685 |
protected int getVerticalGap() { |
| 686 |
return mDefaultVerticalGap; |
| 687 |
} |
| 688 |
|
| 689 |
protected void setVerticalGap(int gap) { |
| 690 |
mDefaultVerticalGap = gap; |
| 691 |
} |
| 692 |
|
| 693 |
protected int getKeyHeight() { |
| 694 |
return mDefaultHeight; |
| 695 |
} |
| 696 |
|
| 697 |
protected void setKeyHeight(int height) { |
| 698 |
mDefaultHeight = height; |
| 699 |
} |
| 700 |
|
| 701 |
protected int getKeyWidth() { |
| 702 |
return mDefaultWidth; |
| 703 |
} |
| 704 |
|
| 705 |
protected void setKeyWidth(int width) { |
| 706 |
mDefaultWidth = width; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
public int getHeight() { |
| 714 |
return mTotalHeight; |
| 715 |
} |
| 716 |
|
| 717 |
public int getMinWidth() { |
| 718 |
return mTotalWidth; |
| 719 |
} |
| 720 |
|
| 721 |
public boolean setShifted(boolean shiftState) { |
| 722 |
for (Key shiftKey : mShiftKeys) { |
| 723 |
if (shiftKey != null) { |
| 724 |
shiftKey.on = shiftState; |
| 725 |
} |
| 726 |
} |
| 727 |
if (mShifted != shiftState) { |
| 728 |
mShifted = shiftState; |
| 729 |
return true; |
| 730 |
} |
| 731 |
return false; |
| 732 |
} |
| 733 |
|
| 734 |
public boolean isShifted() { |
| 735 |
return mShifted; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
public int[] getShiftKeyIndices() { |
| 742 |
return mShiftKeyIndices; |
| 743 |
} |
| 744 |
|
| 745 |
public int getShiftKeyIndex() { |
| 746 |
return mShiftKeyIndices[0]; |
| 747 |
} |
| 748 |
|
| 749 |
private void computeNearestNeighbors() { |
| 750 |
|
| 751 |
mCellWidth = (getMinWidth() + GRID_WIDTH - 1) / GRID_WIDTH; |
| 752 |
mCellHeight = (getHeight() + GRID_HEIGHT - 1) / GRID_HEIGHT; |
| 753 |
mGridNeighbors = new int[GRID_SIZE][]; |
| 754 |
int[] indices = new int[mKeys.size()]; |
| 755 |
final int gridWidth = GRID_WIDTH * mCellWidth; |
| 756 |
final int gridHeight = GRID_HEIGHT * mCellHeight; |
| 757 |
for (int x = 0; x < gridWidth; x += mCellWidth) { |
| 758 |
for (int y = 0; y < gridHeight; y += mCellHeight) { |
| 759 |
int count = 0; |
| 760 |
for (int i = 0; i < mKeys.size(); i++) { |
| 761 |
final Key key = mKeys.get(i); |
| 762 |
if (key.squaredDistanceFrom(x, y) < mProximityThreshold || |
| 763 |
key.squaredDistanceFrom(x + mCellWidth - 1, y) < mProximityThreshold || |
| 764 |
key.squaredDistanceFrom(x + mCellWidth - 1, y + mCellHeight - 1) |
| 765 |
< mProximityThreshold || |
| 766 |
key.squaredDistanceFrom(x, y + mCellHeight - 1) < mProximityThreshold) { |
| 767 |
indices[count++] = i; |
| 768 |
} |
| 769 |
} |
| 770 |
int [] cell = new int[count]; |
| 771 |
System.arraycopy(indices, 0, cell, 0, count); |
| 772 |
mGridNeighbors[(y / mCellHeight) * GRID_WIDTH + (x / mCellWidth)] = cell; |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
public int[] getNearestKeys(int x, int y) { |
| 785 |
if (mGridNeighbors == null) computeNearestNeighbors(); |
| 786 |
if (x >= 0 && x < getMinWidth() && y >= 0 && y < getHeight()) { |
| 787 |
int index = (y / mCellHeight) * GRID_WIDTH + (x / mCellWidth); |
| 788 |
if (index < GRID_SIZE) { |
| 789 |
return mGridNeighbors[index]; |
| 790 |
} |
| 791 |
} |
| 792 |
return new int[0]; |
| 793 |
} |
| 794 |
|
| 795 |
protected Row createRowFromXml(Resources res, XmlResourceParser parser) { |
| 796 |
return new Row(res, this, parser); |
| 797 |
} |
| 798 |
|
| 799 |
protected Key createKeyFromXml(Resources res, Row parent, int x, int y, |
| 800 |
XmlResourceParser parser) { |
| 801 |
return new Key(res, parent, x, y, parser); |
| 802 |
} |
| 803 |
|
| 804 |
private void loadKeyboard(Context context, XmlResourceParser parser) { |
| 805 |
boolean inKey = false; |
| 806 |
boolean inRow = false; |
| 807 |
boolean leftMostKey = false; |
| 808 |
int row = 0; |
| 809 |
int x = 0; |
| 810 |
int y = 0; |
| 811 |
Key key = null; |
| 812 |
Row currentRow = null; |
| 813 |
Resources res = context.getResources(); |
| 814 |
boolean skipRow = false; |
| 815 |
|
| 816 |
try { |
| 817 |
int event; |
| 818 |
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) { |
| 819 |
if (event == XmlResourceParser.START_TAG) { |
| 820 |
String tag = parser.getName(); |
| 821 |
if (TAG_ROW.equals(tag)) { |
| 822 |
inRow = true; |
| 823 |
x = 0; |
| 824 |
currentRow = createRowFromXml(res, parser); |
| 825 |
rows.add(currentRow); |
| 826 |
skipRow = currentRow.mode != 0 && currentRow.mode != mKeyboardMode; |
| 827 |
if (skipRow) { |
| 828 |
skipToEndOfRow(parser); |
| 829 |
inRow = false; |
| 830 |
} |
| 831 |
} else if (TAG_KEY.equals(tag)) { |
| 832 |
inKey = true; |
| 833 |
key = createKeyFromXml(res, currentRow, x, y, parser); |
| 834 |
mKeys.add(key); |
| 835 |
if (key.codes[0] == KEYCODE_SHIFT) { |
| 836 |
|
| 837 |
for (int i = 0; i < mShiftKeys.length; i++) { |
| 838 |
if (mShiftKeys[i] == null) { |
| 839 |
mShiftKeys[i] = key; |
| 840 |
mShiftKeyIndices[i] = mKeys.size()-1; |
| 841 |
break; |
| 842 |
} |
| 843 |
} |
| 844 |
mModifierKeys.add(key); |
| 845 |
} else if (key.codes[0] == KEYCODE_ALT) { |
| 846 |
mModifierKeys.add(key); |
| 847 |
} |
| 848 |
currentRow.mKeys.add(key); |
| 849 |
} else if (TAG_KEYBOARD.equals(tag)) { |
| 850 |
parseKeyboardAttributes(res, parser); |
| 851 |
} |
| 852 |
} else if (event == XmlResourceParser.END_TAG) { |
| 853 |
if (inKey) { |
| 854 |
inKey = false; |
| 855 |
x += key.gap + key.width; |
| 856 |
if (x > mTotalWidth) { |
| 857 |
mTotalWidth = x; |
| 858 |
} |
| 859 |
} else if (inRow) { |
| 860 |
inRow = false; |
| 861 |
y += currentRow.verticalGap; |
| 862 |
y += currentRow.defaultHeight; |
| 863 |
row++; |
| 864 |
} else { |
| 865 |
|
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
} catch (Exception e) { |
| 870 |
Log.e(TAG, "Parse error:" + e); |
| 871 |
e.printStackTrace(); |
| 872 |
} |
| 873 |
mTotalHeight = y - mDefaultVerticalGap; |
| 874 |
} |
| 875 |
|
| 876 |
private void skipToEndOfRow(XmlResourceParser parser) |
| 877 |
throws XmlPullParserException, IOException { |
| 878 |
int event; |
| 879 |
while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) { |
| 880 |
if (event == XmlResourceParser.END_TAG |
| 881 |
&& parser.getName().equals(TAG_ROW)) { |
| 882 |
break; |
| 883 |
} |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
private void parseKeyboardAttributes(Resources res, XmlResourceParser parser) { |
| 888 |
TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), |
| 889 |
com.android.internal.R.styleable.Keyboard); |
| 890 |
|
| 891 |
mDefaultWidth = getDimensionOrFraction(a, |
| 892 |
com.android.internal.R.styleable.Keyboard_keyWidth, |
| 893 |
mDisplayWidth, mDisplayWidth / 10); |
| 894 |
mDefaultHeight = getDimensionOrFraction(a, |
| 895 |
com.android.internal.R.styleable.Keyboard_keyHeight, |
| 896 |
mDisplayHeight, 50); |
| 897 |
mDefaultHorizontalGap = getDimensionOrFraction(a, |
| 898 |
com.android.internal.R.styleable.Keyboard_horizontalGap, |
| 899 |
mDisplayWidth, 0); |
| 900 |
mDefaultVerticalGap = getDimensionOrFraction(a, |
| 901 |
com.android.internal.R.styleable.Keyboard_verticalGap, |
| 902 |
mDisplayHeight, 0); |
| 903 |
mProximityThreshold = (int) (mDefaultWidth * SEARCH_DISTANCE); |
| 904 |
mProximityThreshold = mProximityThreshold * mProximityThreshold; |
| 905 |
a.recycle(); |
| 906 |
} |
| 907 |
|
| 908 |
static int getDimensionOrFraction(TypedArray a, int index, int base, int defValue) { |
| 909 |
TypedValue value = a.peekValue(index); |
| 910 |
if (value == null) return defValue; |
| 911 |
if (value.type == TypedValue.TYPE_DIMENSION) { |
| 912 |
return a.getDimensionPixelOffset(index, defValue); |
| 913 |
} else if (value.type == TypedValue.TYPE_FRACTION) { |
| 914 |
|
| 915 |
return Math.round(a.getFraction(index, base, base, defValue)); |
| 916 |
} |
| 917 |
return defValue; |
| 918 |
} |
| 919 |
} |