2010-08-26 09:49:26 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Google Inc.
|
|
|
|
*
|
|
|
|
* 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-08-26 09:49:26 +01:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public class ProximityKeyDetector extends KeyDetector {
|
2010-08-26 09:49:26 +01:00
|
|
|
private static final int MAX_NEARBY_KEYS = 12;
|
|
|
|
|
|
|
|
// working area
|
|
|
|
private int[] mDistances = new int[MAX_NEARBY_KEYS];
|
|
|
|
|
2010-09-01 15:18:39 +01:00
|
|
|
@Override
|
2010-09-02 16:45:26 +01:00
|
|
|
protected int getMaxNearbyKeys() {
|
|
|
|
return MAX_NEARBY_KEYS;
|
2010-08-26 09:49:26 +01:00
|
|
|
}
|
|
|
|
|
2010-09-01 15:18:39 +01:00
|
|
|
@Override
|
2010-08-26 09:49:26 +01:00
|
|
|
public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys) {
|
2010-09-02 16:45:26 +01:00
|
|
|
final Key[] keys = getKeys();
|
2010-09-02 14:36:22 +01:00
|
|
|
final int touchX = getTouchX(x);
|
|
|
|
final int touchY = getTouchY(y);
|
2010-11-05 09:41:12 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
int primaryIndex = NOT_A_KEY;
|
|
|
|
int closestKeyIndex = NOT_A_KEY;
|
2010-08-26 09:49:26 +01:00
|
|
|
int closestKeyDist = mProximityThresholdSquare + 1;
|
2010-11-05 09:41:12 +00:00
|
|
|
final int[] distances = mDistances;
|
2010-08-26 09:49:26 +01:00
|
|
|
Arrays.fill(distances, Integer.MAX_VALUE);
|
2010-11-05 09:41:12 +00:00
|
|
|
for (final int index : mKeyboard.getNearestKeys(touchX, touchY)) {
|
|
|
|
final Key key = keys[index];
|
|
|
|
final boolean isInside = key.isInside(touchX, touchY);
|
|
|
|
if (isInside)
|
|
|
|
primaryIndex = index;
|
|
|
|
final int dist = key.squaredDistanceToEdge(touchX, touchY);
|
|
|
|
if (isInside || (mProximityCorrectOn && dist < mProximityThresholdSquare)) {
|
2010-08-26 09:49:26 +01:00
|
|
|
if (dist < closestKeyDist) {
|
|
|
|
closestKeyDist = dist;
|
2010-11-05 09:41:12 +00:00
|
|
|
closestKeyIndex = index;
|
2010-08-26 09:49:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (allKeys == null) continue;
|
2010-12-02 11:54:32 +00:00
|
|
|
final int nCodes = key.mCodes.length;
|
2010-11-05 09:41:12 +00:00
|
|
|
// Find insertion point
|
2010-08-26 09:49:26 +01:00
|
|
|
for (int j = 0; j < distances.length; j++) {
|
|
|
|
if (distances[j] > dist) {
|
|
|
|
// Make space for nCodes codes
|
|
|
|
System.arraycopy(distances, j, distances, j + nCodes,
|
2010-11-05 09:41:12 +00:00
|
|
|
distances.length - (j + nCodes));
|
2010-08-26 09:49:26 +01:00
|
|
|
System.arraycopy(allKeys, j, allKeys, j + nCodes,
|
2010-11-05 09:41:12 +00:00
|
|
|
allKeys.length - (j + nCodes));
|
2010-12-02 11:54:32 +00:00
|
|
|
System.arraycopy(key.mCodes, 0, allKeys, j, nCodes);
|
2010-08-26 09:49:26 +01:00
|
|
|
Arrays.fill(distances, j, j + nCodes, dist);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-05 09:41:12 +00:00
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
return primaryIndex == NOT_A_KEY ? closestKeyIndex : primaryIndex;
|
2010-08-26 09:49:26 +01:00
|
|
|
}
|
2010-10-29 10:59:37 +01:00
|
|
|
}
|