2011-02-22 08:28:55 +00:00
|
|
|
/*
|
2011-05-20 03:09:57 +00:00
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
2011-02-22 08:28:55 +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
|
2011-02-22 08:28:55 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-02-22 08:28:55 +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.
|
2011-02-22 08:28:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.keyboard;
|
|
|
|
|
2011-09-29 07:44:54 +00:00
|
|
|
import android.graphics.Rect;
|
2012-03-13 10:16:33 +00:00
|
|
|
import android.text.TextUtils;
|
2012-10-03 11:49:57 +00:00
|
|
|
import android.util.Log;
|
2011-09-29 07:44:54 +00:00
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.TouchPositionCorrection;
|
2012-08-21 05:05:57 +00:00
|
|
|
import com.android.inputmethod.latin.Constants;
|
2014-04-21 20:47:23 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.JniUtils;
|
2011-02-25 09:21:02 +00:00
|
|
|
|
2014-04-21 20:47:23 +00:00
|
|
|
import java.util.ArrayList;
|
2011-03-04 14:06:45 +00:00
|
|
|
import java.util.Arrays;
|
2014-04-21 20:47:23 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2011-03-04 14:06:45 +00:00
|
|
|
|
2013-03-21 04:22:27 +00:00
|
|
|
public class ProximityInfo {
|
2012-10-03 11:49:57 +00:00
|
|
|
private static final String TAG = ProximityInfo.class.getSimpleName();
|
|
|
|
private static final boolean DEBUG = false;
|
|
|
|
|
2013-01-22 04:14:53 +00:00
|
|
|
// Must be equal to MAX_PROXIMITY_CHARS_SIZE in native/jni/src/defines.h
|
2011-02-22 08:28:55 +00:00
|
|
|
public static final int MAX_PROXIMITY_CHARS_SIZE = 16;
|
2011-07-12 05:58:46 +00:00
|
|
|
/** Number of key widths from current touch point to search for nearest keys. */
|
2012-10-03 11:49:57 +00:00
|
|
|
private static final float SEARCH_DISTANCE = 1.2f;
|
2014-04-21 20:47:23 +00:00
|
|
|
private static final List<Key> EMPTY_KEY_LIST = Collections.emptyList();
|
2012-09-27 06:27:51 +00:00
|
|
|
private static final float DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS = 0.15f;
|
2011-02-22 08:28:55 +00:00
|
|
|
|
|
|
|
private final int mGridWidth;
|
|
|
|
private final int mGridHeight;
|
|
|
|
private final int mGridSize;
|
2011-07-12 05:58:46 +00:00
|
|
|
private final int mCellWidth;
|
|
|
|
private final int mCellHeight;
|
|
|
|
// TODO: Find a proper name for mKeyboardMinWidth
|
|
|
|
private final int mKeyboardMinWidth;
|
|
|
|
private final int mKeyboardHeight;
|
2012-03-07 06:12:22 +00:00
|
|
|
private final int mMostCommonKeyWidth;
|
2012-09-27 06:27:51 +00:00
|
|
|
private final int mMostCommonKeyHeight;
|
2014-04-21 21:41:57 +00:00
|
|
|
private final List<Key> mSortedKeys;
|
2014-04-21 20:47:23 +00:00
|
|
|
private final List<Key>[] mGridNeighbors;
|
2012-03-13 10:16:33 +00:00
|
|
|
private final String mLocaleStr;
|
2011-02-22 08:28:55 +00:00
|
|
|
|
2012-08-30 05:22:40 +00:00
|
|
|
ProximityInfo(final String localeStr, final int gridWidth, final int gridHeight,
|
|
|
|
final int minWidth, final int height, final int mostCommonKeyWidth,
|
2014-04-21 21:41:57 +00:00
|
|
|
final int mostCommonKeyHeight, final List<Key> sortedKeys,
|
2012-08-30 05:22:40 +00:00
|
|
|
final TouchPositionCorrection touchPositionCorrection) {
|
2012-03-13 10:16:33 +00:00
|
|
|
if (TextUtils.isEmpty(localeStr)) {
|
|
|
|
mLocaleStr = "";
|
|
|
|
} else {
|
|
|
|
mLocaleStr = localeStr;
|
|
|
|
}
|
2011-02-22 08:28:55 +00:00
|
|
|
mGridWidth = gridWidth;
|
|
|
|
mGridHeight = gridHeight;
|
|
|
|
mGridSize = mGridWidth * mGridHeight;
|
2011-07-12 05:58:46 +00:00
|
|
|
mCellWidth = (minWidth + mGridWidth - 1) / mGridWidth;
|
|
|
|
mCellHeight = (height + mGridHeight - 1) / mGridHeight;
|
|
|
|
mKeyboardMinWidth = minWidth;
|
|
|
|
mKeyboardHeight = height;
|
2012-09-27 06:27:51 +00:00
|
|
|
mMostCommonKeyHeight = mostCommonKeyHeight;
|
2012-03-07 06:12:22 +00:00
|
|
|
mMostCommonKeyWidth = mostCommonKeyWidth;
|
2014-04-21 21:41:57 +00:00
|
|
|
mSortedKeys = sortedKeys;
|
2014-04-21 20:47:23 +00:00
|
|
|
mGridNeighbors = new List[mGridSize];
|
2011-07-12 05:58:46 +00:00
|
|
|
if (minWidth == 0 || height == 0) {
|
2012-02-08 07:12:11 +00:00
|
|
|
// No proximity required. Keyboard might be more keys keyboard.
|
2011-07-12 05:58:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-23 08:37:20 +00:00
|
|
|
computeNearestNeighbors();
|
2012-09-25 02:58:36 +00:00
|
|
|
mNativeProximityInfo = createNativeProximityInfo(touchPositionCorrection);
|
2012-05-23 08:37:20 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 11:44:01 +00:00
|
|
|
private long mNativeProximityInfo;
|
2011-02-25 09:21:02 +00:00
|
|
|
static {
|
2012-03-08 08:07:02 +00:00
|
|
|
JniUtils.loadNativeLibrary();
|
2011-02-25 09:21:02 +00:00
|
|
|
}
|
2012-01-31 08:15:43 +00:00
|
|
|
|
2012-12-03 07:35:56 +00:00
|
|
|
// TODO: Stop passing proximityCharsArray
|
2013-01-22 04:14:53 +00:00
|
|
|
private static native long setProximityInfoNative(String locale,
|
2013-01-11 16:18:00 +00:00
|
|
|
int displayWidth, int displayHeight, int gridWidth, int gridHeight,
|
2013-04-10 02:40:53 +00:00
|
|
|
int mostCommonKeyWidth, int mostCommonKeyHeight, int[] proximityCharsArray,
|
|
|
|
int keyCount, int[] keyXCoordinates, int[] keyYCoordinates, int[] keyWidths,
|
|
|
|
int[] keyHeights, int[] keyCharCodes, float[] sweetSpotCenterXs,
|
|
|
|
float[] sweetSpotCenterYs, float[] sweetSpotRadii);
|
2012-01-31 08:15:43 +00:00
|
|
|
|
2013-01-11 16:18:00 +00:00
|
|
|
private static native void releaseProximityInfoNative(long nativeProximityInfo);
|
2011-02-22 08:28:55 +00:00
|
|
|
|
2012-12-03 07:35:56 +00:00
|
|
|
private static boolean needsProximityInfo(final Key key) {
|
|
|
|
// Don't include special keys into ProximityInfo.
|
2013-08-12 09:05:11 +00:00
|
|
|
return key.getCode() >= Constants.CODE_SPACE;
|
2012-12-03 07:35:56 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 21:13:51 +00:00
|
|
|
private static int getProximityInfoKeysCount(final List<Key> keys) {
|
2012-12-03 07:35:56 +00:00
|
|
|
int count = 0;
|
|
|
|
for (final Key key : keys) {
|
|
|
|
if (needsProximityInfo(key)) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private long createNativeProximityInfo(final TouchPositionCorrection touchPositionCorrection) {
|
2014-04-21 20:47:23 +00:00
|
|
|
final List<Key>[] gridNeighborKeys = mGridNeighbors;
|
2011-12-16 06:32:24 +00:00
|
|
|
final int[] proximityCharsArray = new int[mGridSize * MAX_PROXIMITY_CHARS_SIZE];
|
2012-08-21 05:05:57 +00:00
|
|
|
Arrays.fill(proximityCharsArray, Constants.NOT_A_CODE);
|
2011-02-22 08:28:55 +00:00
|
|
|
for (int i = 0; i < mGridSize; ++i) {
|
2014-04-21 20:47:23 +00:00
|
|
|
final int proximityCharsLength = gridNeighborKeys[i].size();
|
2012-12-03 07:35:56 +00:00
|
|
|
int infoIndex = i * MAX_PROXIMITY_CHARS_SIZE;
|
2011-03-04 14:06:45 +00:00
|
|
|
for (int j = 0; j < proximityCharsLength; ++j) {
|
2014-04-21 20:47:23 +00:00
|
|
|
final Key neighborKey = gridNeighborKeys[i].get(j);
|
2012-12-03 07:35:56 +00:00
|
|
|
// Excluding from proximityCharsArray
|
|
|
|
if (!needsProximityInfo(neighborKey)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-12 09:05:11 +00:00
|
|
|
proximityCharsArray[infoIndex] = neighborKey.getCode();
|
2012-12-03 07:35:56 +00:00
|
|
|
infoIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DEBUG) {
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
for (int i = 0; i < mGridSize; i++) {
|
|
|
|
sb.setLength(0);
|
|
|
|
for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE; j++) {
|
|
|
|
final int code = proximityCharsArray[i * MAX_PROXIMITY_CHARS_SIZE + j];
|
|
|
|
if (code == Constants.NOT_A_CODE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sb.length() > 0) sb.append(" ");
|
|
|
|
sb.append(Constants.printableCode(code));
|
|
|
|
}
|
|
|
|
Log.d(TAG, "proxmityChars["+i+"]: " + sb);
|
2011-02-22 08:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-03 07:35:56 +00:00
|
|
|
|
2014-04-21 21:41:57 +00:00
|
|
|
final List<Key> sortedKeys = mSortedKeys;
|
|
|
|
final int keyCount = getProximityInfoKeysCount(sortedKeys);
|
2011-09-29 07:44:54 +00:00
|
|
|
final int[] keyXCoordinates = new int[keyCount];
|
|
|
|
final int[] keyYCoordinates = new int[keyCount];
|
|
|
|
final int[] keyWidths = new int[keyCount];
|
|
|
|
final int[] keyHeights = new int[keyCount];
|
|
|
|
final int[] keyCharCodes = new int[keyCount];
|
2011-12-16 04:53:34 +00:00
|
|
|
final float[] sweetSpotCenterXs;
|
|
|
|
final float[] sweetSpotCenterYs;
|
|
|
|
final float[] sweetSpotRadii;
|
2012-03-14 09:40:19 +00:00
|
|
|
|
2014-04-21 21:41:57 +00:00
|
|
|
for (int infoIndex = 0, keyIndex = 0; keyIndex < sortedKeys.size(); keyIndex++) {
|
|
|
|
final Key key = sortedKeys.get(keyIndex);
|
2012-12-03 07:35:56 +00:00
|
|
|
// Excluding from key coordinate arrays
|
|
|
|
if (!needsProximityInfo(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-12 09:05:11 +00:00
|
|
|
keyXCoordinates[infoIndex] = key.getX();
|
|
|
|
keyYCoordinates[infoIndex] = key.getY();
|
|
|
|
keyWidths[infoIndex] = key.getWidth();
|
|
|
|
keyHeights[infoIndex] = key.getHeight();
|
|
|
|
keyCharCodes[infoIndex] = key.getCode();
|
2012-12-03 07:35:56 +00:00
|
|
|
infoIndex++;
|
2012-03-14 09:40:19 +00:00
|
|
|
}
|
|
|
|
|
2011-12-16 04:53:34 +00:00
|
|
|
if (touchPositionCorrection != null && touchPositionCorrection.isValid()) {
|
2012-10-03 11:49:57 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "touchPositionCorrection: ON");
|
|
|
|
}
|
2011-12-16 04:53:34 +00:00
|
|
|
sweetSpotCenterXs = new float[keyCount];
|
|
|
|
sweetSpotCenterYs = new float[keyCount];
|
|
|
|
sweetSpotRadii = new float[keyCount];
|
2012-10-03 11:49:57 +00:00
|
|
|
final int rows = touchPositionCorrection.getRows();
|
2012-09-27 06:27:51 +00:00
|
|
|
final float defaultRadius = DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS
|
|
|
|
* (float)Math.hypot(mMostCommonKeyWidth, mMostCommonKeyHeight);
|
2014-04-21 21:41:57 +00:00
|
|
|
for (int infoIndex = 0, keyIndex = 0; keyIndex < sortedKeys.size(); keyIndex++) {
|
|
|
|
final Key key = sortedKeys.get(keyIndex);
|
2012-12-03 07:35:56 +00:00
|
|
|
// Excluding from touch position correction arrays
|
|
|
|
if (!needsProximityInfo(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-12 09:05:11 +00:00
|
|
|
final Rect hitBox = key.getHitBox();
|
2012-12-03 07:35:56 +00:00
|
|
|
sweetSpotCenterXs[infoIndex] = hitBox.exactCenterX();
|
|
|
|
sweetSpotCenterYs[infoIndex] = hitBox.exactCenterY();
|
|
|
|
sweetSpotRadii[infoIndex] = defaultRadius;
|
2012-09-27 06:27:51 +00:00
|
|
|
final int row = hitBox.top / mMostCommonKeyHeight;
|
2012-10-03 11:49:57 +00:00
|
|
|
if (row < rows) {
|
2012-05-31 02:17:51 +00:00
|
|
|
final int hitBoxWidth = hitBox.width();
|
|
|
|
final int hitBoxHeight = hitBox.height();
|
2012-09-27 06:27:51 +00:00
|
|
|
final float hitBoxDiagonal = (float)Math.hypot(hitBoxWidth, hitBoxHeight);
|
2012-12-03 07:35:56 +00:00
|
|
|
sweetSpotCenterXs[infoIndex] +=
|
|
|
|
touchPositionCorrection.getX(row) * hitBoxWidth;
|
|
|
|
sweetSpotCenterYs[infoIndex] +=
|
|
|
|
touchPositionCorrection.getY(row) * hitBoxHeight;
|
|
|
|
sweetSpotRadii[infoIndex] =
|
|
|
|
touchPositionCorrection.getRadius(row) * hitBoxDiagonal;
|
2012-03-05 09:15:29 +00:00
|
|
|
}
|
2012-10-03 11:49:57 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, String.format(
|
2012-12-03 07:35:56 +00:00
|
|
|
" [%2d] row=%d x/y/r=%7.2f/%7.2f/%5.2f %s code=%s", infoIndex, row,
|
|
|
|
sweetSpotCenterXs[infoIndex], sweetSpotCenterYs[infoIndex],
|
|
|
|
sweetSpotRadii[infoIndex], (row < rows ? "correct" : "default"),
|
2013-08-12 09:05:11 +00:00
|
|
|
Constants.printableCode(key.getCode())));
|
2012-10-03 11:49:57 +00:00
|
|
|
}
|
2012-12-03 07:35:56 +00:00
|
|
|
infoIndex++;
|
2012-03-05 09:15:29 +00:00
|
|
|
}
|
2011-12-16 04:53:34 +00:00
|
|
|
} else {
|
|
|
|
sweetSpotCenterXs = sweetSpotCenterYs = sweetSpotRadii = null;
|
2012-10-03 11:49:57 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "touchPositionCorrection: OFF");
|
|
|
|
}
|
2011-12-16 04:53:34 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 07:35:56 +00:00
|
|
|
// TODO: Stop passing proximityCharsArray
|
2013-01-22 04:14:53 +00:00
|
|
|
return setProximityInfoNative(mLocaleStr, mKeyboardMinWidth, mKeyboardHeight,
|
2013-04-10 02:40:53 +00:00
|
|
|
mGridWidth, mGridHeight, mMostCommonKeyWidth, mMostCommonKeyHeight,
|
|
|
|
proximityCharsArray, keyCount, keyXCoordinates, keyYCoordinates, keyWidths,
|
|
|
|
keyHeights, keyCharCodes, sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii);
|
2011-09-29 07:44:54 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 11:44:01 +00:00
|
|
|
public long getNativeProximityInfo() {
|
2011-02-22 08:28:55 +00:00
|
|
|
return mNativeProximityInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void finalize() throws Throwable {
|
|
|
|
try {
|
|
|
|
if (mNativeProximityInfo != 0) {
|
|
|
|
releaseProximityInfoNative(mNativeProximityInfo);
|
|
|
|
mNativeProximityInfo = 0;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
super.finalize();
|
|
|
|
}
|
|
|
|
}
|
2011-07-12 05:58:46 +00:00
|
|
|
|
2012-05-23 08:37:20 +00:00
|
|
|
private void computeNearestNeighbors() {
|
|
|
|
final int defaultWidth = mMostCommonKeyWidth;
|
2014-04-21 21:41:57 +00:00
|
|
|
final int keyCount = mSortedKeys.size();
|
2013-06-03 09:42:55 +00:00
|
|
|
final int gridSize = mGridNeighbors.length;
|
|
|
|
final int threshold = (int) (defaultWidth * SEARCH_DISTANCE);
|
|
|
|
final int thresholdSquared = threshold * threshold;
|
2011-07-12 05:58:46 +00:00
|
|
|
// Round-up so we don't have any pixels outside the grid
|
2013-09-19 04:43:09 +00:00
|
|
|
final int lastPixelXCoordinate = mGridWidth * mCellWidth - 1;
|
|
|
|
final int lastPixelYCoordinate = mGridHeight * mCellHeight - 1;
|
2013-06-03 09:42:55 +00:00
|
|
|
|
|
|
|
// For large layouts, 'neighborsFlatBuffer' is about 80k of memory: gridSize is usually 512,
|
|
|
|
// keycount is about 40 and a pointer to a Key is 4 bytes. This contains, for each cell,
|
|
|
|
// enough space for as many keys as there are on the keyboard. Hence, every
|
|
|
|
// keycount'th element is the start of a new cell, and each of these virtual subarrays
|
|
|
|
// start empty with keycount spaces available. This fills up gradually in the loop below.
|
|
|
|
// Since in the practice each cell does not have a lot of neighbors, most of this space is
|
|
|
|
// actually just empty padding in this fixed-size buffer.
|
|
|
|
final Key[] neighborsFlatBuffer = new Key[gridSize * keyCount];
|
|
|
|
final int[] neighborCountPerCell = new int[gridSize];
|
|
|
|
final int halfCellWidth = mCellWidth / 2;
|
|
|
|
final int halfCellHeight = mCellHeight / 2;
|
2014-04-21 21:41:57 +00:00
|
|
|
for (final Key key : mSortedKeys) {
|
2013-06-03 09:42:55 +00:00
|
|
|
if (key.isSpacer()) continue;
|
|
|
|
|
|
|
|
/* HOW WE PRE-SELECT THE CELLS (iterate over only the relevant cells, instead of all of them)
|
|
|
|
|
|
|
|
We want to compute the distance for keys that are in the cells that are close enough to the
|
|
|
|
key border, as this method is performance-critical. These keys are represented with 'star'
|
|
|
|
background on the diagram below. Let's consider the Y case first.
|
|
|
|
|
|
|
|
We want to select the cells which center falls between the top of the key minus the threshold,
|
|
|
|
and the bottom of the key plus the threshold.
|
|
|
|
topPixelWithinThreshold is key.mY - threshold, and bottomPixelWithinThreshold is
|
|
|
|
key.mY + key.mHeight + threshold.
|
|
|
|
|
|
|
|
Then we need to compute the center of the top row that we need to evaluate, as we'll iterate
|
|
|
|
from there.
|
|
|
|
|
|
|
|
(0,0)----> x
|
|
|
|
| .-------------------------------------------.
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| |---+---+---+---+---+---+---+---+---+---+---| .- top of top cell (aligned on the grid)
|
|
|
|
| | | | | | | | | | | | | |
|
|
|
|
| |-----------+---+---+---+---+---+---+---+---|---' v
|
|
|
|
| | | | |***|***|*_________________________ topPixelWithinThreshold | yDeltaToGrid
|
|
|
|
| |---+---+---+-----^-+-|-+---+---+---+---+---| ^
|
|
|
|
| | | | |***|*|*|*|*|***|***| | | | ______________________________________
|
|
|
|
v |---+---+--threshold--|-+---+---+---+---+---| |
|
|
|
|
| | | |***|*|*|*|*|***|***| | | | | Starting from key.mY, we substract
|
|
|
|
y |---+---+---+---+-v-+-|-+---+---+---+---+---| | thresholdBase and get the top pixel
|
|
|
|
| | | |***|**########------------------- key.mY | within the threshold. We align that on
|
|
|
|
|---+---+---+---+--#+---+-#-+---+---+---+---| | the grid by computing the delta to the
|
|
|
|
| | | |***|**#|***|*#*|***| | | | | grid, and get the top of the top cell.
|
|
|
|
|---+---+---+---+--#+---+-#-+---+---+---+---| |
|
|
|
|
| | | |***|**########*|***| | | | | Adding half the cell height to the top
|
|
|
|
|---+---+---+---+---+-|-+---+---+---+---+---| | of the top cell, we get the middle of
|
|
|
|
| | | |***|***|*|*|***|***| | | | | the top cell (yMiddleOfTopCell).
|
|
|
|
|---+---+---+---+---+-|-+---+---+---+---+---| |
|
|
|
|
| | | |***|***|*|*|***|***| | | | |
|
|
|
|
|---+---+---+---+---+-|________________________ yEnd | Since we only want to add the key to
|
|
|
|
| | | | | | | (bottomPixelWithinThreshold) | the proximity if it's close enough to
|
|
|
|
|---+---+---+---+---+---+---+---+---+---+---| | the center of the cell, we only need
|
|
|
|
| | | | | | | | | | | | | to compute for these cells where
|
|
|
|
'---'---'---'---'---'---'---'---'---'---'---' | topPixelWithinThreshold is above the
|
|
|
|
(positive x,y) | center of the cell. This is the case
|
|
|
|
| when yDeltaToGrid is less than half
|
|
|
|
[Zoomed in diagram] | the height of the cell.
|
|
|
|
+-------+-------+-------+-------+-------+ |
|
|
|
|
| | | | | | | On the zoomed in diagram, on the right
|
|
|
|
| | | | | | | the topPixelWithinThreshold (represented
|
|
|
|
| | | | | | top of | with an = sign) is below and we can skip
|
|
|
|
+-------+-------+-------+--v----+-------+ .. top cell | this cell, while on the left it's above
|
|
|
|
| | = topPixelWT | | yDeltaToGrid | and we need to compute for this cell.
|
|
|
|
|..yStart.|.....|.......|..|....|.......|... y middle | Thus, if yDeltaToGrid is more than half
|
|
|
|
| (left)| | | ^ = | | of top cell | the height of the cell, we start the
|
|
|
|
+-------+-|-----+-------+----|--+-------+ | iteration one cell below the top cell,
|
|
|
|
| | | | | | | | | else we start it on the top cell. This
|
|
|
|
|.......|.|.....|.......|....|..|.....yStart (right) | is stored in yStart.
|
|
|
|
|
|
|
|
Since we only want to go up to bottomPixelWithinThreshold, and we only iterate on the center
|
|
|
|
of the keys, we can stop as soon as the y value exceeds bottomPixelThreshold, so we don't
|
|
|
|
have to align this on the center of the key. Hence, we don't need a separate value for
|
|
|
|
bottomPixelWithinThreshold and call this yEnd right away.
|
|
|
|
*/
|
2013-08-12 09:05:11 +00:00
|
|
|
final int keyX = key.getX();
|
|
|
|
final int keyY = key.getY();
|
|
|
|
final int topPixelWithinThreshold = keyY - threshold;
|
2013-06-03 09:42:55 +00:00
|
|
|
final int yDeltaToGrid = topPixelWithinThreshold % mCellHeight;
|
|
|
|
final int yMiddleOfTopCell = topPixelWithinThreshold - yDeltaToGrid + halfCellHeight;
|
|
|
|
final int yStart = Math.max(halfCellHeight,
|
|
|
|
yMiddleOfTopCell + (yDeltaToGrid <= halfCellHeight ? 0 : mCellHeight));
|
2013-09-19 04:43:09 +00:00
|
|
|
final int yEnd = Math.min(lastPixelYCoordinate, keyY + key.getHeight() + threshold);
|
2013-06-03 09:42:55 +00:00
|
|
|
|
2013-08-12 09:05:11 +00:00
|
|
|
final int leftPixelWithinThreshold = keyX - threshold;
|
2013-06-03 09:42:55 +00:00
|
|
|
final int xDeltaToGrid = leftPixelWithinThreshold % mCellWidth;
|
|
|
|
final int xMiddleOfLeftCell = leftPixelWithinThreshold - xDeltaToGrid + halfCellWidth;
|
|
|
|
final int xStart = Math.max(halfCellWidth,
|
|
|
|
xMiddleOfLeftCell + (xDeltaToGrid <= halfCellWidth ? 0 : mCellWidth));
|
2013-09-19 04:43:09 +00:00
|
|
|
final int xEnd = Math.min(lastPixelXCoordinate, keyX + key.getWidth() + threshold);
|
2013-06-03 09:42:55 +00:00
|
|
|
|
|
|
|
int baseIndexOfCurrentRow = (yStart / mCellHeight) * mGridWidth + (xStart / mCellWidth);
|
|
|
|
for (int centerY = yStart; centerY <= yEnd; centerY += mCellHeight) {
|
|
|
|
int index = baseIndexOfCurrentRow;
|
|
|
|
for (int centerX = xStart; centerX <= xEnd; centerX += mCellWidth) {
|
2013-09-19 04:43:09 +00:00
|
|
|
if (key.squaredDistanceToEdge(centerX, centerY) < thresholdSquared) {
|
2013-06-03 09:42:55 +00:00
|
|
|
neighborsFlatBuffer[index * keyCount + neighborCountPerCell[index]] = key;
|
|
|
|
++neighborCountPerCell[index];
|
2011-12-16 05:20:11 +00:00
|
|
|
}
|
2013-06-03 09:42:55 +00:00
|
|
|
++index;
|
2011-07-12 05:58:46 +00:00
|
|
|
}
|
2013-06-03 09:42:55 +00:00
|
|
|
baseIndexOfCurrentRow += mGridWidth;
|
2011-07-12 05:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-03 09:42:55 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < gridSize; ++i) {
|
2014-04-21 20:47:23 +00:00
|
|
|
final int indexStart = i * keyCount;
|
|
|
|
final int indexEnd = indexStart + neighborCountPerCell[i];
|
|
|
|
final ArrayList<Key> neighbords = CollectionUtils.newArrayList(indexEnd - indexStart);
|
|
|
|
for (int index = indexStart; index < indexEnd; index++) {
|
|
|
|
neighbords.add(neighborsFlatBuffer[index]);
|
|
|
|
}
|
|
|
|
mGridNeighbors[i] = Collections.unmodifiableList(neighbords);
|
2013-06-03 09:42:55 +00:00
|
|
|
}
|
2011-07-12 05:58:46 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 06:27:51 +00:00
|
|
|
public void fillArrayWithNearestKeyCodes(final int x, final int y, final int primaryKeyCode,
|
|
|
|
final int[] dest) {
|
2012-04-02 05:26:36 +00:00
|
|
|
final int destLength = dest.length;
|
|
|
|
if (destLength < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int index = 0;
|
2012-10-29 05:46:34 +00:00
|
|
|
if (primaryKeyCode > Constants.CODE_SPACE) {
|
2012-04-02 05:26:36 +00:00
|
|
|
dest[index++] = primaryKeyCode;
|
|
|
|
}
|
2014-04-21 20:47:23 +00:00
|
|
|
final List<Key> nearestKeys = getNearestKeys(x, y);
|
2012-04-02 05:26:36 +00:00
|
|
|
for (Key key : nearestKeys) {
|
|
|
|
if (index >= destLength) {
|
|
|
|
break;
|
|
|
|
}
|
2013-08-12 09:05:11 +00:00
|
|
|
final int code = key.getCode();
|
2012-10-29 05:46:34 +00:00
|
|
|
if (code <= Constants.CODE_SPACE) {
|
2012-04-02 05:26:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
dest[index++] = code;
|
|
|
|
}
|
|
|
|
if (index < destLength) {
|
2012-08-21 05:05:57 +00:00
|
|
|
dest[index] = Constants.NOT_A_CODE;
|
2012-04-02 05:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:47:23 +00:00
|
|
|
public List<Key> getNearestKeys(final int x, final int y) {
|
2011-07-12 05:58:46 +00:00
|
|
|
if (mGridNeighbors == null) {
|
2014-04-21 20:47:23 +00:00
|
|
|
return EMPTY_KEY_LIST;
|
2011-07-12 05:58:46 +00:00
|
|
|
}
|
|
|
|
if (x >= 0 && x < mKeyboardMinWidth && y >= 0 && y < mKeyboardHeight) {
|
2012-01-31 08:15:43 +00:00
|
|
|
int index = (y / mCellHeight) * mGridWidth + (x / mCellWidth);
|
2011-07-12 05:58:46 +00:00
|
|
|
if (index < mGridSize) {
|
|
|
|
return mGridNeighbors[index];
|
|
|
|
}
|
|
|
|
}
|
2014-04-21 20:47:23 +00:00
|
|
|
return EMPTY_KEY_LIST;
|
2011-07-12 05:58:46 +00:00
|
|
|
}
|
2011-02-22 08:28:55 +00:00
|
|
|
}
|