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
|
|
|
|
2014-02-14 02:40:00 +00:00
|
|
|
/**
|
|
|
|
* This class handles key detection.
|
|
|
|
*/
|
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
|
|
|
|
2014-02-14 02:40:00 +00:00
|
|
|
public KeyDetector() {
|
|
|
|
this(0.0f /* keyHysteresisDistance */, 0.0f /* keyHysteresisDistanceForSlidingModifier */);
|
2012-10-06 14:22:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-14 02:40:00 +00:00
|
|
|
* Key detection object constructor with key hysteresis distances.
|
2012-10-06 14:22:36 +00:00
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
2014-02-19 04:52:28 +00:00
|
|
|
public KeyDetector(final float keyHysteresisDistance,
|
|
|
|
final 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
|
|
|
}
|
|
|
|
|
2014-02-19 04:52:28 +00:00
|
|
|
public void setKeyboard(final Keyboard keyboard, final float correctionX,
|
|
|
|
final 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
|
|
|
}
|
|
|
|
|
2014-02-19 04:52:28 +00:00
|
|
|
public int getKeyHysteresisDistanceSquared(final boolean isSlidingFromModifier) {
|
2012-10-06 14:22:36 +00:00
|
|
|
return isSlidingFromModifier
|
|
|
|
? mKeyHysteresisDistanceForSlidingModifierSquared : mKeyHysteresisDistanceSquared;
|
2010-09-01 14:18:39 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 04:52:28 +00:00
|
|
|
public int getTouchX(final 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.
|
2014-02-19 04:52:28 +00:00
|
|
|
public int getTouchY(final int y) {
|
2010-09-02 12:54:26 +00:00
|
|
|
return y + mCorrectionY;
|
|
|
|
}
|
|
|
|
|
2011-07-08 05:31:29 +00:00
|
|
|
public Keyboard getKeyboard() {
|
|
|
|
return mKeyboard;
|
2010-09-02 15:45:26 +00:00
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
public boolean alwaysAllowsKeySelectionByDraggingFinger() {
|
2011-08-23 03:08:36 +00:00
|
|
|
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.
|
|
|
|
*/
|
2014-02-19 04:52:28 +00:00
|
|
|
public Key detectHitKey(final int x, final int y) {
|
2014-02-27 05:14:52 +00:00
|
|
|
if (mKeyboard == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-03-15 10:44:43 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-08-12 09:05:11 +00:00
|
|
|
// To take care of hitbox overlaps, we compare key's code here too.
|
|
|
|
if (primaryKey == null || distance < minDistance
|
|
|
|
|| key.getCode() > primaryKey.getCode()) {
|
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
|
|
|
}
|
2010-09-03 05:46:14 +00:00
|
|
|
}
|