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
|
|
|
*
|
2013-01-21 12:52:57 +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
|
2010-09-01 14:18:39 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2010-09-01 14:18:39 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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-09-01 14:18:39 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
package com.android.inputmethod.keyboard;
|
2010-09-01 14:18:39 +00:00
|
|
|
|
2012-08-21 05:05:57 +00:00
|
|
|
import com.android.inputmethod.latin.Constants;
|
2010-09-01 14:18:39 +00:00
|
|
|
|
2010-09-03 05:46:14 +00:00
|
|
|
|
2012-08-21 05:05:57 +00:00
|
|
|
public class KeyDetector {
|
2011-07-04 10:59:57 +00:00
|
|
|
private final int mKeyHysteresisDistanceSquared;
|
2012-10-06 14:22:36 +00:00
|
|
|
private final int mKeyHysteresisDistanceForSlidingModifierSquared;
|
2011-07-04 10:59:57 +00:00
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
private Keyboard mKeyboard;
|
|
|
|
private int mCorrectionX;
|
|
|
|
private int mCorrectionY;
|
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
|
2012-10-06 14:22:36 +00:00
|
|
|
* movement will not be handled as meaningful movement. The unit is pixel.
|
2011-07-04 10:59:57 +00:00
|
|
|
*/
|
|
|
|
public KeyDetector(float keyHysteresisDistance) {
|
2012-10-06 14:22:36 +00:00
|
|
|
this(keyHysteresisDistance, keyHysteresisDistance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class handles key detection.
|
|
|
|
*
|
|
|
|
* @param keyHysteresisDistance if the pointer movement distance is smaller than this, the
|
|
|
|
* movement will not be handled as meaningful movement. The unit is pixel.
|
|
|
|
* @param keyHysteresisDistanceForSlidingModifier the same parameter for sliding input that
|
|
|
|
* starts from a modifier key such as shift and symbols key.
|
|
|
|
*/
|
|
|
|
public KeyDetector(float keyHysteresisDistance, float keyHysteresisDistanceForSlidingModifier) {
|
2011-07-04 10:59:57 +00:00
|
|
|
mKeyHysteresisDistanceSquared = (int)(keyHysteresisDistance * keyHysteresisDistance);
|
2012-10-06 14:22:36 +00:00
|
|
|
mKeyHysteresisDistanceForSlidingModifierSquared = (int)(
|
|
|
|
keyHysteresisDistanceForSlidingModifier * keyHysteresisDistanceForSlidingModifier);
|
2011-07-04 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
public void setKeyboard(Keyboard keyboard, float correctionX, float correctionY) {
|
2012-08-06 01:50:19 +00:00
|
|
|
if (keyboard == null) {
|
2010-09-01 14:18:39 +00:00
|
|
|
throw new NullPointerException();
|
2012-08-06 01:50:19 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2012-10-06 14:22:36 +00:00
|
|
|
public int getKeyHysteresisDistanceSquared(boolean isSlidingFromModifier) {
|
|
|
|
return isSlidingFromModifier
|
|
|
|
? mKeyHysteresisDistanceForSlidingModifierSquared : 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-07-04 07:55:51 +00:00
|
|
|
// TODO: Remove vertical correction.
|
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() {
|
2012-08-08 02:41:58 +00:00
|
|
|
if (mKeyboard == null) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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)) {
|
2012-09-04 05:50:56 +00:00
|
|
|
// An edge key always has its enlarged hitbox to respond to an event that occurred in
|
|
|
|
// the empty area around the key. (@see Key#markAsLeftEdge(KeyboardParams)} etc.)
|
|
|
|
if (!key.isOnKey(touchX, touchY)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-14 14:17:12 +00:00
|
|
|
final int distance = key.squaredDistanceToEdge(touchX, touchY);
|
2012-09-04 05:50:56 +00:00
|
|
|
if (distance > minDistance) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-19 03:00:00 +00:00
|
|
|
// To take care of hitbox overlaps, we compare mCode here too.
|
2012-09-04 05:50:56 +00:00
|
|
|
if (primaryKey == null || distance < minDistance || 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) {
|
2012-10-29 05:46:34 +00:00
|
|
|
return key != null ? Constants.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) {
|
2012-08-21 05:05:57 +00:00
|
|
|
if (code == Constants.NOT_A_CODE) break;
|
2012-03-19 05:47:38 +00:00
|
|
|
if (addDelimiter) sb.append(", ");
|
2012-10-29 05:46:34 +00:00
|
|
|
sb.append(Constants.printableCode(code));
|
2012-03-19 05:47:38 +00:00
|
|
|
addDelimiter = true;
|
2011-12-01 07:34:23 +00:00
|
|
|
}
|
|
|
|
return "[" + sb + "]";
|
|
|
|
}
|
2010-09-03 05:46:14 +00:00
|
|
|
}
|