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
|
|
|
public class KeyDetector {
|
|
|
|
public static final int NOT_A_CODE = -1;
|
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;
|
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-04 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-23 03:08:36 +00:00
|
|
|
public boolean alwaysAllowsSlidingInput() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-15 10:44:43 +00:00
|
|
|
/**
|
|
|
|
* Detect the key whose hitbox the touch point is in.
|
|
|
|
*
|
|
|
|
* @param x The x-coordinate of a touch point
|
|
|
|
* @param y The y-coordinate of a touch point
|
|
|
|
* @return the key that the touch point hits.
|
|
|
|
*/
|
|
|
|
public Key detectHitKey(int x, int y) {
|
|
|
|
final int touchX = getTouchX(x);
|
|
|
|
final int touchY = getTouchY(y);
|
2011-04-15 04:05:58 +00:00
|
|
|
|
2012-03-14 14:17:12 +00:00
|
|
|
int minDistance = Integer.MAX_VALUE;
|
|
|
|
Key primaryKey = null;
|
|
|
|
for (final Key key: mKeyboard.getNearestKeys(touchX, touchY)) {
|
|
|
|
final boolean isOnKey = key.isOnKey(touchX, touchY);
|
|
|
|
final int distance = key.squaredDistanceToEdge(touchX, touchY);
|
2012-03-19 03:00:00 +00:00
|
|
|
// To take care of hitbox overlaps, we compare mCode here too.
|
2012-03-14 14:17:12 +00:00
|
|
|
if (primaryKey == null || distance < minDistance
|
2012-03-19 03:00:00 +00:00
|
|
|
|| (distance == minDistance && isOnKey && key.mCode > primaryKey.mCode)) {
|
2012-03-14 14:17:12 +00:00
|
|
|
minDistance = distance;
|
|
|
|
primaryKey = key;
|
2012-03-15 10:44:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-14 14:17:12 +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-03-19 05:47:38 +00:00
|
|
|
if (addDelimiter) sb.append(", ");
|
|
|
|
sb.append(Keyboard.printableCode(code));
|
|
|
|
addDelimiter = true;
|
2011-12-01 07:34:23 +00:00
|
|
|
}
|
|
|
|
return "[" + sb + "]";
|
|
|
|
}
|
2010-09-03 05:46:14 +00:00
|
|
|
}
|