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

http://eyes-free.googlecode.com/ · Java · 59 lines · 36 code · 7 blank · 16 comment · 6 complexity · 681f06da6a59d0d4f95d37f260d939f7 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. class MiniKeyboardKeyDetector extends KeyDetector {
  19. private static final int MAX_NEARBY_KEYS = 1;
  20. private final int mSlideAllowanceSquare;
  21. private final int mSlideAllowanceSquareTop;
  22. public MiniKeyboardKeyDetector(float slideAllowance) {
  23. super();
  24. mSlideAllowanceSquare = (int)(slideAllowance * slideAllowance);
  25. // Top slide allowance is slightly longer (sqrt(2) times) than other edges.
  26. mSlideAllowanceSquareTop = mSlideAllowanceSquare * 2;
  27. }
  28. @Override
  29. protected int getMaxNearbyKeys() {
  30. return MAX_NEARBY_KEYS;
  31. }
  32. @Override
  33. public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys) {
  34. final Key[] keys = getKeys();
  35. final int touchX = getTouchX(x);
  36. final int touchY = getTouchY(y);
  37. int closestKeyIndex = LatinKeyboardBaseView.NOT_A_KEY;
  38. int closestKeyDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare;
  39. final int keyCount = keys.length;
  40. for (int i = 0; i < keyCount; i++) {
  41. final Key key = keys[i];
  42. int dist = key.squaredDistanceFrom(touchX, touchY);
  43. if (dist < closestKeyDist) {
  44. closestKeyIndex = i;
  45. closestKeyDist = dist;
  46. }
  47. }
  48. if (allKeys != null && closestKeyIndex != LatinKeyboardBaseView.NOT_A_KEY)
  49. allKeys[0] = keys[closestKeyIndex].codes[0];
  50. return closestKeyIndex;
  51. }
  52. }