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

http://eyes-free.googlecode.com/ · Java · 112 lines · 51 code · 19 blank · 42 comment · 4 complexity · c3d0e8170cef556480e42318187e1c20 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;
  18. import android.inputmethodservice.Keyboard.Key;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. abstract class KeyDetector {
  22. protected Keyboard mKeyboard;
  23. private Key[] mKeys;
  24. protected int mCorrectionX;
  25. protected int mCorrectionY;
  26. protected boolean mProximityCorrectOn;
  27. protected int mProximityThresholdSquare;
  28. public Key[] setKeyboard(Keyboard keyboard, float correctionX, float correctionY) {
  29. if (keyboard == null)
  30. throw new NullPointerException();
  31. mCorrectionX = (int)correctionX;
  32. mCorrectionY = (int)correctionY;
  33. mKeyboard = keyboard;
  34. List<Key> keys = mKeyboard.getKeys();
  35. Key[] array = keys.toArray(new Key[keys.size()]);
  36. mKeys = array;
  37. return array;
  38. }
  39. protected int getTouchX(int x) {
  40. return x + mCorrectionX;
  41. }
  42. protected int getTouchY(int y) {
  43. return y + mCorrectionY;
  44. }
  45. protected Key[] getKeys() {
  46. if (mKeys == null)
  47. throw new IllegalStateException("keyboard isn't set");
  48. // mKeyboard is guaranteed not to be null at setKeybaord() method if mKeys is not null
  49. return mKeys;
  50. }
  51. public void setProximityCorrectionEnabled(boolean enabled) {
  52. mProximityCorrectOn = enabled;
  53. }
  54. public boolean isProximityCorrectionEnabled() {
  55. return mProximityCorrectOn;
  56. }
  57. public void setProximityThreshold(int threshold) {
  58. mProximityThresholdSquare = threshold * threshold;
  59. }
  60. /**
  61. * Allocates array that can hold all key indices returned by {@link #getKeyIndexAndNearbyCodes}
  62. * method. The maximum size of the array should be computed by {@link #getMaxNearbyKeys}.
  63. *
  64. * @return Allocates and returns an array that can hold all key indices returned by
  65. * {@link #getKeyIndexAndNearbyCodes} method. All elements in the returned array are
  66. * initialized by {@link LatinKeyboardBaseView#NOT_A_KEY} value.
  67. */
  68. public int[] newCodeArray() {
  69. int[] codes = new int[getMaxNearbyKeys()];
  70. Arrays.fill(codes, LatinKeyboardBaseView.NOT_A_KEY);
  71. return codes;
  72. }
  73. /**
  74. * Computes maximum size of the array that can contain all nearby key indices returned by
  75. * {@link #getKeyIndexAndNearbyCodes}.
  76. *
  77. * @return Returns maximum size of the array that can contain all nearby key indices returned
  78. * by {@link #getKeyIndexAndNearbyCodes}.
  79. */
  80. abstract protected int getMaxNearbyKeys();
  81. /**
  82. * Finds all possible nearby key indices around a touch event point and returns the nearest key
  83. * index. The algorithm to determine the nearby keys depends on the threshold set by
  84. * {@link #setProximityThreshold(int)} and the mode set by
  85. * {@link #setProximityCorrectionEnabled(boolean)}.
  86. *
  87. * @param x The x-coordinate of a touch point
  88. * @param y The y-coordinate of a touch point
  89. * @param allKeys All nearby key indices are returned in this array
  90. * @return The nearest key index
  91. */
  92. abstract public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys);
  93. }