2010-09-01 14:18:39 +00:00
|
|
|
/*
|
2011-05-20 03:09:57 +00:00
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2010-09-01 14:18:39 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
package com.android.inputmethod.keyboard;
|
2010-09-01 14:18:39 +00:00
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2010-09-02 15:45:26 +00:00
|
|
|
import java.util.Arrays;
|
2012-01-31 08:15:43 +00:00
|
|
|
import java.util.List;
|
2010-09-01 14:18:39 +00:00
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
public class KeyDetector {
|
|
|
|
private static final String TAG = KeyDetector.class.getSimpleName();
|
|
|
|
private static final boolean DEBUG = false;
|
2010-09-03 05:46:14 +00:00
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
public static final int NOT_A_CODE = -1;
|
2012-01-31 08:15:43 +00:00
|
|
|
private static final int ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE = 2;
|
2010-09-03 05:46:14 +00:00
|
|
|
|
2011-07-04 10:59:57 +00:00
|
|
|
private final int mKeyHysteresisDistanceSquared;
|
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
private Keyboard mKeyboard;
|
|
|
|
private int mCorrectionX;
|
|
|
|
private int mCorrectionY;
|
|
|
|
private boolean mProximityCorrectOn;
|
|
|
|
private int mProximityThresholdSquare;
|
2010-09-03 05:46:14 +00:00
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
// working area
|
|
|
|
private static final int MAX_NEARBY_KEYS = 12;
|
|
|
|
private final int[] mDistances = new int[MAX_NEARBY_KEYS];
|
2011-12-16 05:20:11 +00:00
|
|
|
private final Key[] mNeighborKeys = new Key[MAX_NEARBY_KEYS];
|
2010-09-01 14:18:39 +00:00
|
|
|
|
2011-07-04 10:59:57 +00:00
|
|
|
/**
|
|
|
|
* This class handles key detection.
|
|
|
|
*
|
|
|
|
* @param keyHysteresisDistance if the pointer movement distance is smaller than this, the
|
|
|
|
* movement will not been handled as meaningful movement. The unit is pixel.
|
|
|
|
*/
|
|
|
|
public KeyDetector(float keyHysteresisDistance) {
|
|
|
|
mKeyHysteresisDistanceSquared = (int)(keyHysteresisDistance * keyHysteresisDistance);
|
|
|
|
}
|
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
public void setKeyboard(Keyboard keyboard, float correctionX, float correctionY) {
|
2010-09-01 14:18:39 +00:00
|
|
|
if (keyboard == null)
|
|
|
|
throw new NullPointerException();
|
2010-09-02 12:54:26 +00:00
|
|
|
mCorrectionX = (int)correctionX;
|
|
|
|
mCorrectionY = (int)correctionY;
|
2010-09-01 14:18:39 +00:00
|
|
|
mKeyboard = keyboard;
|
2011-07-29 00:05:40 +00:00
|
|
|
final int threshold = keyboard.mMostCommonKeyWidth;
|
2011-07-04 10:59:57 +00:00
|
|
|
mProximityThresholdSquare = threshold * threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyHysteresisDistanceSquared() {
|
|
|
|
return mKeyHysteresisDistanceSquared;
|
2010-09-01 14:18:39 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 07:38:00 +00:00
|
|
|
public int getTouchX(int x) {
|
2010-09-02 12:54:26 +00:00
|
|
|
return x + mCorrectionX;
|
|
|
|
}
|
|
|
|
|
2012-03-06 07:38:00 +00:00
|
|
|
public int getTouchY(int y) {
|
2010-09-02 12:54:26 +00:00
|
|
|
return y + mCorrectionY;
|
|
|
|
}
|
|
|
|
|
2011-07-08 05:31:29 +00:00
|
|
|
public Keyboard getKeyboard() {
|
2011-04-15 04:05:58 +00:00
|
|
|
if (mKeyboard == null)
|
2010-09-02 15:45:26 +00:00
|
|
|
throw new IllegalStateException("keyboard isn't set");
|
2011-07-08 05:31:29 +00:00
|
|
|
return mKeyboard;
|
2010-09-02 15:45:26 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 14:18:39 +00:00
|
|
|
public void setProximityCorrectionEnabled(boolean enabled) {
|
|
|
|
mProximityCorrectOn = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isProximityCorrectionEnabled() {
|
|
|
|
return mProximityCorrectOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setProximityThreshold(int threshold) {
|
|
|
|
mProximityThresholdSquare = threshold * threshold;
|
|
|
|
}
|
|
|
|
|
2011-08-23 03:08:36 +00:00
|
|
|
public boolean alwaysAllowsSlidingInput() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
/**
|
2011-11-29 07:56:27 +00:00
|
|
|
* Computes maximum size of the array that can contain all nearby key codes returned by
|
|
|
|
* {@link #getKeyAndNearbyCodes}.
|
2011-04-15 04:05:58 +00:00
|
|
|
*
|
2011-11-29 07:56:27 +00:00
|
|
|
* @return Returns maximum size of the array that can contain all nearby key codes returned
|
|
|
|
* by {@link #getKeyAndNearbyCodes}.
|
2011-04-15 04:05:58 +00:00
|
|
|
*/
|
|
|
|
protected int getMaxNearbyKeys() {
|
|
|
|
return MAX_NEARBY_KEYS;
|
|
|
|
}
|
|
|
|
|
2010-09-03 05:46:14 +00:00
|
|
|
/**
|
2011-11-29 07:56:27 +00:00
|
|
|
* Allocates array that can hold all key codes returned by {@link #getKeyAndNearbyCodes}
|
2010-09-03 05:46:14 +00:00
|
|
|
* method. The maximum size of the array should be computed by {@link #getMaxNearbyKeys}.
|
|
|
|
*
|
2011-11-29 07:56:27 +00:00
|
|
|
* @return Allocates and returns an array that can hold all key codes returned by
|
|
|
|
* {@link #getKeyAndNearbyCodes} method. All elements in the returned array are
|
2011-04-28 14:39:49 +00:00
|
|
|
* initialized by {@link #NOT_A_CODE} value.
|
2010-09-03 05:46:14 +00:00
|
|
|
*/
|
2010-09-02 15:45:26 +00:00
|
|
|
public int[] newCodeArray() {
|
|
|
|
int[] codes = new int[getMaxNearbyKeys()];
|
2011-04-28 14:39:49 +00:00
|
|
|
Arrays.fill(codes, NOT_A_CODE);
|
2010-09-02 15:45:26 +00:00
|
|
|
return codes;
|
|
|
|
}
|
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
private void initializeNearbyKeys() {
|
|
|
|
Arrays.fill(mDistances, Integer.MAX_VALUE);
|
2011-12-16 05:20:11 +00:00
|
|
|
Arrays.fill(mNeighborKeys, null);
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
|
2010-09-03 05:46:14 +00:00
|
|
|
/**
|
2011-04-15 04:05:58 +00:00
|
|
|
* Insert the key into nearby keys buffer and sort nearby keys by ascending order of distance.
|
2011-04-28 14:39:49 +00:00
|
|
|
* If the distance of two keys are the same, the key which the point is on should be considered
|
|
|
|
* as a closer one.
|
2010-09-03 05:46:14 +00:00
|
|
|
*
|
2011-12-16 05:20:11 +00:00
|
|
|
* @param key the key to be inserted into the nearby keys buffer.
|
2011-04-15 04:05:58 +00:00
|
|
|
* @param distance distance between the key's edge and user touched point.
|
2011-04-28 14:39:49 +00:00
|
|
|
* @param isOnKey true if the point is on the key.
|
2011-04-15 04:05:58 +00:00
|
|
|
* @return order of the key in the nearby buffer, 0 if it is the nearest key.
|
2010-09-03 05:46:14 +00:00
|
|
|
*/
|
2011-12-16 05:20:11 +00:00
|
|
|
private int sortNearbyKeys(Key key, int distance, boolean isOnKey) {
|
2011-04-15 04:05:58 +00:00
|
|
|
final int[] distances = mDistances;
|
2011-12-16 05:20:11 +00:00
|
|
|
final Key[] neighborKeys = mNeighborKeys;
|
2011-04-15 04:05:58 +00:00
|
|
|
for (int insertPos = 0; insertPos < distances.length; insertPos++) {
|
2011-04-28 14:39:49 +00:00
|
|
|
final int comparingDistance = distances[insertPos];
|
|
|
|
if (distance < comparingDistance || (distance == comparingDistance && isOnKey)) {
|
2011-04-15 04:05:58 +00:00
|
|
|
final int nextPos = insertPos + 1;
|
|
|
|
if (nextPos < distances.length) {
|
|
|
|
System.arraycopy(distances, insertPos, distances, nextPos,
|
|
|
|
distances.length - nextPos);
|
2011-12-16 05:20:11 +00:00
|
|
|
System.arraycopy(neighborKeys, insertPos, neighborKeys, nextPos,
|
|
|
|
neighborKeys.length - nextPos);
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
distances[insertPos] = distance;
|
2011-12-16 05:20:11 +00:00
|
|
|
neighborKeys[insertPos] = key;
|
2011-04-15 04:05:58 +00:00
|
|
|
return insertPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return distances.length;
|
|
|
|
}
|
|
|
|
|
2012-01-31 08:15:43 +00:00
|
|
|
private void getNearbyKeyCodes(final int primaryCode, final int[] allCodes) {
|
2011-12-16 05:20:11 +00:00
|
|
|
final Key[] neighborKeys = mNeighborKeys;
|
2012-01-31 08:15:43 +00:00
|
|
|
final int maxCodesSize = allCodes.length;
|
2011-04-15 04:05:58 +00:00
|
|
|
|
|
|
|
// allCodes[0] should always have the key code even if it is a non-letter key.
|
2011-12-16 05:20:11 +00:00
|
|
|
if (neighborKeys[0] == null) {
|
2011-04-15 04:05:58 +00:00
|
|
|
allCodes[0] = NOT_A_CODE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int numCodes = 0;
|
2012-01-31 08:15:43 +00:00
|
|
|
for (int j = 0; j < neighborKeys.length && numCodes < maxCodesSize; j++) {
|
2011-12-16 05:20:11 +00:00
|
|
|
final Key key = neighborKeys[j];
|
|
|
|
if (key == null)
|
2011-04-15 04:05:58 +00:00
|
|
|
break;
|
2011-12-16 05:20:11 +00:00
|
|
|
final int code = key.mCode;
|
2011-04-15 04:05:58 +00:00
|
|
|
// filter out a non-letter key from nearby keys
|
|
|
|
if (code < Keyboard.CODE_SPACE)
|
|
|
|
continue;
|
|
|
|
allCodes[numCodes++] = code;
|
|
|
|
}
|
2012-01-31 08:15:43 +00:00
|
|
|
if (maxCodesSize <= numCodes) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-17 02:28:22 +00:00
|
|
|
|
|
|
|
final int code = (primaryCode == NOT_A_CODE) ? allCodes[0] : primaryCode;
|
|
|
|
if (code == NOT_A_CODE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final List<Integer> additionalChars = mKeyboard.getAdditionalProximityChars().get(code);
|
|
|
|
if (additionalChars == null || additionalChars.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int currentCodesSize = numCodes;
|
|
|
|
allCodes[numCodes++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
|
|
|
|
if (maxCodesSize <= numCodes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO: This is O(N^2). Assuming additionalChars.size() is up to 4 or 5.
|
|
|
|
for (int i = 0; i < additionalChars.size(); ++i) {
|
|
|
|
final int additionalChar = additionalChars.get(i);
|
|
|
|
boolean contains = false;
|
|
|
|
for (int j = 0; j < currentCodesSize; ++j) {
|
|
|
|
if (additionalChar == allCodes[j]) {
|
|
|
|
contains = true;
|
|
|
|
break;
|
2012-01-31 08:15:43 +00:00
|
|
|
}
|
2012-02-17 02:28:22 +00:00
|
|
|
}
|
|
|
|
if (!contains) {
|
|
|
|
allCodes[numCodes++] = additionalChar;
|
|
|
|
if (maxCodesSize <= numCodes) {
|
|
|
|
return;
|
2012-01-31 08:15:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
2010-09-01 14:18:39 +00:00
|
|
|
|
2010-09-03 05:46:14 +00:00
|
|
|
/**
|
2011-11-29 07:56:27 +00:00
|
|
|
* Finds all possible nearby key codes around a touch event point and returns the nearest key.
|
|
|
|
* The algorithm to determine the nearby keys depends on the threshold set by
|
2010-09-03 05:46:14 +00:00
|
|
|
* {@link #setProximityThreshold(int)} and the mode set by
|
|
|
|
* {@link #setProximityCorrectionEnabled(boolean)}.
|
|
|
|
*
|
|
|
|
* @param x The x-coordinate of a touch point
|
|
|
|
* @param y The y-coordinate of a touch point
|
2011-11-29 07:56:27 +00:00
|
|
|
* @param allCodes All nearby key codes except functional key are returned in this array
|
|
|
|
* @return The nearest key
|
2010-09-03 05:46:14 +00:00
|
|
|
*/
|
2011-11-29 07:56:27 +00:00
|
|
|
public Key getKeyAndNearbyCodes(int x, int y, final int[] allCodes) {
|
2011-04-15 04:05:58 +00:00
|
|
|
final int touchX = getTouchX(x);
|
|
|
|
final int touchY = getTouchY(y);
|
|
|
|
|
|
|
|
initializeNearbyKeys();
|
2011-11-29 07:56:27 +00:00
|
|
|
Key primaryKey = null;
|
2011-12-16 05:20:11 +00:00
|
|
|
for (final Key key: mKeyboard.getNearestKeys(touchX, touchY)) {
|
2011-04-28 14:39:49 +00:00
|
|
|
final boolean isOnKey = key.isOnKey(touchX, touchY);
|
2011-04-15 04:05:58 +00:00
|
|
|
final int distance = key.squaredDistanceToEdge(touchX, touchY);
|
2011-04-28 14:39:49 +00:00
|
|
|
if (isOnKey || (mProximityCorrectOn && distance < mProximityThresholdSquare)) {
|
2011-12-16 05:20:11 +00:00
|
|
|
final int insertedPosition = sortNearbyKeys(key, distance, isOnKey);
|
2011-11-29 07:56:27 +00:00
|
|
|
if (insertedPosition == 0 && isOnKey) {
|
|
|
|
primaryKey = key;
|
|
|
|
}
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allCodes != null && allCodes.length > 0) {
|
2012-01-31 08:15:43 +00:00
|
|
|
getNearbyKeyCodes(primaryKey != null ? primaryKey.mCode : NOT_A_CODE, allCodes);
|
2011-04-15 04:05:58 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "x=" + x + " y=" + y
|
2011-12-01 07:34:23 +00:00
|
|
|
+ " primary=" + printableCode(primaryKey)
|
|
|
|
+ " codes=" + printableCodes(allCodes));
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-29 07:56:27 +00:00
|
|
|
return primaryKey;
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
2011-12-01 07:34:23 +00:00
|
|
|
|
|
|
|
public static String printableCode(Key key) {
|
2011-12-12 04:08:41 +00:00
|
|
|
return key != null ? Keyboard.printableCode(key.mCode) : "none";
|
2011-12-01 07:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String printableCodes(int[] codes) {
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
2012-02-17 02:28:22 +00:00
|
|
|
boolean addDelimiter = false;
|
2011-12-01 07:34:23 +00:00
|
|
|
for (final int code : codes) {
|
|
|
|
if (code == NOT_A_CODE) break;
|
2012-02-17 02:28:22 +00:00
|
|
|
if (code == ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
|
|
|
|
sb.append(" | ");
|
|
|
|
addDelimiter = false;
|
|
|
|
} else {
|
|
|
|
if (addDelimiter) sb.append(", ");
|
2012-03-02 05:48:10 +00:00
|
|
|
sb.append(Keyboard.printableCode(code));
|
2012-02-17 02:28:22 +00:00
|
|
|
addDelimiter = true;
|
|
|
|
}
|
2011-12-01 07:34:23 +00:00
|
|
|
}
|
|
|
|
return "[" + sb + "]";
|
|
|
|
}
|
2010-09-03 05:46:14 +00:00
|
|
|
}
|