/ime/latinime/src/com/googlecode/eyesfree/inputmethod/latin/ProximityKeyDetector.java

http://eyes-free.googlecode.com/ · Java · 86 lines · 58 code · 10 blank · 18 comment · 13 complexity · 62e39930e913544ec7b4bffe96779be9 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc.
  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. package com.googlecode.eyesfree.inputmethod.latin;
  17. import android.inputmethodservice.Keyboard.Key;
  18. import java.util.Arrays;
  19. class ProximityKeyDetector extends KeyDetector {
  20. private static final int MAX_NEARBY_KEYS = 12;
  21. // working area
  22. private int[] mDistances = new int[MAX_NEARBY_KEYS];
  23. @Override
  24. protected int getMaxNearbyKeys() {
  25. return MAX_NEARBY_KEYS;
  26. }
  27. @Override
  28. public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys) {
  29. final Key[] keys = getKeys();
  30. final int touchX = getTouchX(x);
  31. final int touchY = getTouchY(y);
  32. int primaryIndex = LatinKeyboardBaseView.NOT_A_KEY;
  33. int closestKey = LatinKeyboardBaseView.NOT_A_KEY;
  34. int closestKeyDist = mProximityThresholdSquare + 1;
  35. int[] distances = mDistances;
  36. Arrays.fill(distances, Integer.MAX_VALUE);
  37. int [] nearestKeyIndices = mKeyboard.getNearestKeys(touchX, touchY);
  38. final int keyCount = nearestKeyIndices.length;
  39. for (int i = 0; i < keyCount; i++) {
  40. final Key key = keys[nearestKeyIndices[i]];
  41. int dist = 0;
  42. boolean isInside = key.isInside(touchX, touchY);
  43. if (isInside) {
  44. primaryIndex = nearestKeyIndices[i];
  45. }
  46. if (((mProximityCorrectOn
  47. && (dist = key.squaredDistanceFrom(touchX, touchY)) < mProximityThresholdSquare)
  48. || isInside)
  49. && key.codes[0] > 32) {
  50. // Find insertion point
  51. final int nCodes = key.codes.length;
  52. if (dist < closestKeyDist) {
  53. closestKeyDist = dist;
  54. closestKey = nearestKeyIndices[i];
  55. }
  56. if (allKeys == null) continue;
  57. for (int j = 0; j < distances.length; j++) {
  58. if (distances[j] > dist) {
  59. // Make space for nCodes codes
  60. System.arraycopy(distances, j, distances, j + nCodes,
  61. distances.length - j - nCodes);
  62. System.arraycopy(allKeys, j, allKeys, j + nCodes,
  63. allKeys.length - j - nCodes);
  64. System.arraycopy(key.codes, 0, allKeys, j, nCodes);
  65. Arrays.fill(distances, j, j + nCodes, dist);
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. if (primaryIndex == LatinKeyboardBaseView.NOT_A_KEY) {
  72. primaryIndex = closestKey;
  73. }
  74. return primaryIndex;
  75. }
  76. }