ProximityInfo.java revision d55baf06d96aff0b67979419c37ac3ebdc44768c
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.keyboard;
18
19import android.graphics.Rect;
20import android.text.TextUtils;
21import android.util.Log;
22
23import com.android.inputmethod.keyboard.internal.TouchPositionCorrection;
24import com.android.inputmethod.latin.Constants;
25import com.android.inputmethod.latin.JniUtils;
26
27import java.util.Arrays;
28
29public class ProximityInfo {
30    private static final String TAG = ProximityInfo.class.getSimpleName();
31    private static final boolean DEBUG = false;
32
33    // Must be equal to MAX_PROXIMITY_CHARS_SIZE in native/jni/src/defines.h
34    public static final int MAX_PROXIMITY_CHARS_SIZE = 16;
35    /** Number of key widths from current touch point to search for nearest keys. */
36    private static final float SEARCH_DISTANCE = 1.2f;
37    private static final Key[] EMPTY_KEY_ARRAY = new Key[0];
38    private static final float DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS = 0.15f;
39
40    private final int mGridWidth;
41    private final int mGridHeight;
42    private final int mGridSize;
43    private final int mCellWidth;
44    private final int mCellHeight;
45    // TODO: Find a proper name for mKeyboardMinWidth
46    private final int mKeyboardMinWidth;
47    private final int mKeyboardHeight;
48    private final int mMostCommonKeyWidth;
49    private final int mMostCommonKeyHeight;
50    private final Key[] mKeys;
51    private final Key[][] mGridNeighbors;
52    private final String mLocaleStr;
53
54    ProximityInfo(final String localeStr, final int gridWidth, final int gridHeight,
55            final int minWidth, final int height, final int mostCommonKeyWidth,
56            final int mostCommonKeyHeight, final Key[] keys,
57            final TouchPositionCorrection touchPositionCorrection) {
58        if (TextUtils.isEmpty(localeStr)) {
59            mLocaleStr = "";
60        } else {
61            mLocaleStr = localeStr;
62        }
63        mGridWidth = gridWidth;
64        mGridHeight = gridHeight;
65        mGridSize = mGridWidth * mGridHeight;
66        mCellWidth = (minWidth + mGridWidth - 1) / mGridWidth;
67        mCellHeight = (height + mGridHeight - 1) / mGridHeight;
68        mKeyboardMinWidth = minWidth;
69        mKeyboardHeight = height;
70        mMostCommonKeyHeight = mostCommonKeyHeight;
71        mMostCommonKeyWidth = mostCommonKeyWidth;
72        mKeys = keys;
73        mGridNeighbors = new Key[mGridSize][];
74        if (minWidth == 0 || height == 0) {
75            // No proximity required. Keyboard might be more keys keyboard.
76            return;
77        }
78        computeNearestNeighbors();
79        mNativeProximityInfo = createNativeProximityInfo(touchPositionCorrection);
80    }
81
82    private long mNativeProximityInfo;
83    static {
84        JniUtils.loadNativeLibrary();
85    }
86
87    // TODO: Stop passing proximityCharsArray
88    private static native long setProximityInfoNative(String locale,
89            int displayWidth, int displayHeight, int gridWidth, int gridHeight,
90            int mostCommonKeyWidth, int mostCommonKeyHeight, int[] proximityCharsArray,
91            int keyCount, int[] keyXCoordinates, int[] keyYCoordinates, int[] keyWidths,
92            int[] keyHeights, int[] keyCharCodes, float[] sweetSpotCenterXs,
93            float[] sweetSpotCenterYs, float[] sweetSpotRadii);
94
95    private static native void releaseProximityInfoNative(long nativeProximityInfo);
96
97    private static boolean needsProximityInfo(final Key key) {
98        // Don't include special keys into ProximityInfo.
99        return key.mCode >= Constants.CODE_SPACE;
100    }
101
102    private static int getProximityInfoKeysCount(final Key[] keys) {
103        int count = 0;
104        for (final Key key : keys) {
105            if (needsProximityInfo(key)) {
106                count++;
107            }
108        }
109        return count;
110    }
111
112    private long createNativeProximityInfo(final TouchPositionCorrection touchPositionCorrection) {
113        final Key[][] gridNeighborKeys = mGridNeighbors;
114        final int[] proximityCharsArray = new int[mGridSize * MAX_PROXIMITY_CHARS_SIZE];
115        Arrays.fill(proximityCharsArray, Constants.NOT_A_CODE);
116        for (int i = 0; i < mGridSize; ++i) {
117            final int proximityCharsLength = gridNeighborKeys[i].length;
118            int infoIndex = i * MAX_PROXIMITY_CHARS_SIZE;
119            for (int j = 0; j < proximityCharsLength; ++j) {
120                final Key neighborKey = gridNeighborKeys[i][j];
121                // Excluding from proximityCharsArray
122                if (!needsProximityInfo(neighborKey)) {
123                    continue;
124                }
125                proximityCharsArray[infoIndex] = neighborKey.mCode;
126                infoIndex++;
127            }
128        }
129        if (DEBUG) {
130            final StringBuilder sb = new StringBuilder();
131            for (int i = 0; i < mGridSize; i++) {
132                sb.setLength(0);
133                for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE; j++) {
134                    final int code = proximityCharsArray[i * MAX_PROXIMITY_CHARS_SIZE + j];
135                    if (code == Constants.NOT_A_CODE) {
136                        break;
137                    }
138                    if (sb.length() > 0) sb.append(" ");
139                    sb.append(Constants.printableCode(code));
140                }
141                Log.d(TAG, "proxmityChars["+i+"]: " + sb);
142            }
143        }
144
145        final Key[] keys = mKeys;
146        final int keyCount = getProximityInfoKeysCount(keys);
147        final int[] keyXCoordinates = new int[keyCount];
148        final int[] keyYCoordinates = new int[keyCount];
149        final int[] keyWidths = new int[keyCount];
150        final int[] keyHeights = new int[keyCount];
151        final int[] keyCharCodes = new int[keyCount];
152        final float[] sweetSpotCenterXs;
153        final float[] sweetSpotCenterYs;
154        final float[] sweetSpotRadii;
155
156        for (int infoIndex = 0, keyIndex = 0; keyIndex < keys.length; keyIndex++) {
157            final Key key = keys[keyIndex];
158            // Excluding from key coordinate arrays
159            if (!needsProximityInfo(key)) {
160                continue;
161            }
162            keyXCoordinates[infoIndex] = key.mX;
163            keyYCoordinates[infoIndex] = key.mY;
164            keyWidths[infoIndex] = key.mWidth;
165            keyHeights[infoIndex] = key.mHeight;
166            keyCharCodes[infoIndex] = key.mCode;
167            infoIndex++;
168        }
169
170        if (touchPositionCorrection != null && touchPositionCorrection.isValid()) {
171            if (DEBUG) {
172                Log.d(TAG, "touchPositionCorrection: ON");
173            }
174            sweetSpotCenterXs = new float[keyCount];
175            sweetSpotCenterYs = new float[keyCount];
176            sweetSpotRadii = new float[keyCount];
177            final int rows = touchPositionCorrection.getRows();
178            final float defaultRadius = DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS
179                    * (float)Math.hypot(mMostCommonKeyWidth, mMostCommonKeyHeight);
180            for (int infoIndex = 0, keyIndex = 0; keyIndex < keys.length; keyIndex++) {
181                final Key key = keys[keyIndex];
182                // Excluding from touch position correction arrays
183                if (!needsProximityInfo(key)) {
184                    continue;
185                }
186                final Rect hitBox = key.mHitBox;
187                sweetSpotCenterXs[infoIndex] = hitBox.exactCenterX();
188                sweetSpotCenterYs[infoIndex] = hitBox.exactCenterY();
189                sweetSpotRadii[infoIndex] = defaultRadius;
190                final int row = hitBox.top / mMostCommonKeyHeight;
191                if (row < rows) {
192                    final int hitBoxWidth = hitBox.width();
193                    final int hitBoxHeight = hitBox.height();
194                    final float hitBoxDiagonal = (float)Math.hypot(hitBoxWidth, hitBoxHeight);
195                    sweetSpotCenterXs[infoIndex] +=
196                            touchPositionCorrection.getX(row) * hitBoxWidth;
197                    sweetSpotCenterYs[infoIndex] +=
198                            touchPositionCorrection.getY(row) * hitBoxHeight;
199                    sweetSpotRadii[infoIndex] =
200                            touchPositionCorrection.getRadius(row) * hitBoxDiagonal;
201                }
202                if (DEBUG) {
203                    Log.d(TAG, String.format(
204                            "  [%2d] row=%d x/y/r=%7.2f/%7.2f/%5.2f %s code=%s", infoIndex, row,
205                            sweetSpotCenterXs[infoIndex], sweetSpotCenterYs[infoIndex],
206                            sweetSpotRadii[infoIndex], (row < rows ? "correct" : "default"),
207                            Constants.printableCode(key.mCode)));
208                }
209                infoIndex++;
210            }
211        } else {
212            sweetSpotCenterXs = sweetSpotCenterYs = sweetSpotRadii = null;
213            if (DEBUG) {
214                Log.d(TAG, "touchPositionCorrection: OFF");
215            }
216        }
217
218        // TODO: Stop passing proximityCharsArray
219        return setProximityInfoNative(mLocaleStr, mKeyboardMinWidth, mKeyboardHeight,
220                mGridWidth, mGridHeight, mMostCommonKeyWidth, mMostCommonKeyHeight,
221                proximityCharsArray, keyCount, keyXCoordinates, keyYCoordinates, keyWidths,
222                keyHeights, keyCharCodes, sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii);
223    }
224
225    public long getNativeProximityInfo() {
226        return mNativeProximityInfo;
227    }
228
229    @Override
230    protected void finalize() throws Throwable {
231        try {
232            if (mNativeProximityInfo != 0) {
233                releaseProximityInfoNative(mNativeProximityInfo);
234                mNativeProximityInfo = 0;
235            }
236        } finally {
237            super.finalize();
238        }
239    }
240
241    private void computeNearestNeighbors() {
242        final int defaultWidth = mMostCommonKeyWidth;
243        final int keyCount = mKeys.length;
244        final int gridSize = mGridNeighbors.length;
245        final int threshold = (int) (defaultWidth * SEARCH_DISTANCE);
246        final int thresholdSquared = threshold * threshold;
247        // Round-up so we don't have any pixels outside the grid
248        final int fullGridWidth = mGridWidth * mCellWidth;
249        final int fullGridHeight = mGridHeight * mCellHeight;
250
251        // For large layouts, 'neighborsFlatBuffer' is about 80k of memory: gridSize is usually 512,
252        // keycount is about 40 and a pointer to a Key is 4 bytes. This contains, for each cell,
253        // enough space for as many keys as there are on the keyboard. Hence, every
254        // keycount'th element is the start of a new cell, and each of these virtual subarrays
255        // start empty with keycount spaces available. This fills up gradually in the loop below.
256        // Since in the practice each cell does not have a lot of neighbors, most of this space is
257        // actually just empty padding in this fixed-size buffer.
258        final Key[] neighborsFlatBuffer = new Key[gridSize * keyCount];
259        final int[] neighborCountPerCell = new int[gridSize];
260        final int halfCellWidth = mCellWidth / 2;
261        final int halfCellHeight = mCellHeight / 2;
262        for (final Key key : mKeys) {
263            if (key.isSpacer()) continue;
264
265/* HOW WE PRE-SELECT THE CELLS (iterate over only the relevant cells, instead of all of them)
266
267  We want to compute the distance for keys that are in the cells that are close enough to the
268  key border, as this method is performance-critical. These keys are represented with 'star'
269  background on the diagram below. Let's consider the Y case first.
270
271  We want to select the cells which center falls between the top of the key minus the threshold,
272  and the bottom of the key plus the threshold.
273  topPixelWithinThreshold is key.mY - threshold, and bottomPixelWithinThreshold is
274  key.mY + key.mHeight + threshold.
275
276  Then we need to compute the center of the top row that we need to evaluate, as we'll iterate
277  from there.
278
279(0,0)----> x
280| .-------------------------------------------.
281| |   |   |   |   |   |   |   |   |   |   |   |
282| |---+---+---+---+---+---+---+---+---+---+---|   .- top of top cell (aligned on the grid)
283| |   |   |   |   |   |   |   |   |   |   |   |   |
284| |-----------+---+---+---+---+---+---+---+---|---'                          v
285| |   |   |   |***|***|*_________________________ topPixelWithinThreshold    | yDeltaToGrid
286| |---+---+---+-----^-+-|-+---+---+---+---+---|                              ^
287| |   |   |   |***|*|*|*|*|***|***|   |   |   |           ______________________________________
288v |---+---+--threshold--|-+---+---+---+---+---|          |
289  |   |   |   |***|*|*|*|*|***|***|   |   |   |          | Starting from key.mY, we substract
290y |---+---+---+---+-v-+-|-+---+---+---+---+---|          | thresholdBase and get the top pixel
291  |   |   |   |***|**########------------------- key.mY  | within the threshold. We align that on
292  |---+---+---+---+--#+---+-#-+---+---+---+---|          | the grid by computing the delta to the
293  |   |   |   |***|**#|***|*#*|***|   |   |   |          | grid, and get the top of the top cell.
294  |---+---+---+---+--#+---+-#-+---+---+---+---|          |
295  |   |   |   |***|**########*|***|   |   |   |          | Adding half the cell height to the top
296  |---+---+---+---+---+-|-+---+---+---+---+---|          | of the top cell, we get the middle of
297  |   |   |   |***|***|*|*|***|***|   |   |   |          | the top cell (yMiddleOfTopCell).
298  |---+---+---+---+---+-|-+---+---+---+---+---|          |
299  |   |   |   |***|***|*|*|***|***|   |   |   |          |
300  |---+---+---+---+---+-|________________________ yEnd   | Since we only want to add the key to
301  |   |   |   |   |   |   | (bottomPixelWithinThreshold) | the proximity if it's close enough to
302  |---+---+---+---+---+---+---+---+---+---+---|          | the center of the cell, we only need
303  |   |   |   |   |   |   |   |   |   |   |   |          | to compute for these cells where
304  '---'---'---'---'---'---'---'---'---'---'---'          | topPixelWithinThreshold is above the
305                                        (positive x,y)   | center of the cell. This is the case
306                                                         | when yDeltaToGrid is less than half
307  [Zoomed in diagram]                                    | the height of the cell.
308  +-------+-------+-------+-------+-------+              |
309  |       |       |       |       |       |              | On the zoomed in diagram, on the right
310  |       |       |       |       |       |              | the topPixelWithinThreshold (represented
311  |       |       |       |       |       |      top of  | with an = sign) is below and we can skip
312  +-------+-------+-------+--v----+-------+ .. top cell  | this cell, while on the left it's above
313  |       | = topPixelWT  |  |  yDeltaToGrid             | and we need to compute for this cell.
314  |..yStart.|.....|.......|..|....|.......|... y middle  | Thus, if yDeltaToGrid is more than half
315  |   (left)|     |       |  ^ =  |       | of top cell  | the height of the cell, we start the
316  +-------+-|-----+-------+----|--+-------+              | iteration one cell below the top cell,
317  |       | |     |       |    |  |       |              | else we start it on the top cell. This
318  |.......|.|.....|.......|....|..|.....yStart (right)   | is stored in yStart.
319
320  Since we only want to go up to bottomPixelWithinThreshold, and we only iterate on the center
321  of the keys, we can stop as soon as the y value exceeds bottomPixelThreshold, so we don't
322  have to align this on the center of the key. Hence, we don't need a separate value for
323  bottomPixelWithinThreshold and call this yEnd right away.
324*/
325            final int topPixelWithinThreshold = key.mY - threshold;
326            final int yDeltaToGrid = topPixelWithinThreshold % mCellHeight;
327            final int yMiddleOfTopCell = topPixelWithinThreshold - yDeltaToGrid + halfCellHeight;
328            final int yStart = Math.max(halfCellHeight,
329                    yMiddleOfTopCell + (yDeltaToGrid <= halfCellHeight ? 0 : mCellHeight));
330            final int yEnd = Math.min(fullGridHeight, key.mY + key.mHeight + threshold);
331
332            final int leftPixelWithinThreshold = key.mX - threshold;
333            final int xDeltaToGrid = leftPixelWithinThreshold % mCellWidth;
334            final int xMiddleOfLeftCell = leftPixelWithinThreshold - xDeltaToGrid + halfCellWidth;
335            final int xStart = Math.max(halfCellWidth,
336                    xMiddleOfLeftCell + (xDeltaToGrid <= halfCellWidth ? 0 : mCellWidth));
337            final int xEnd = Math.min(fullGridWidth, key.mX + key.mWidth + threshold);
338
339            int baseIndexOfCurrentRow = (yStart / mCellHeight) * mGridWidth + (xStart / mCellWidth);
340            for (int centerY = yStart; centerY <= yEnd; centerY += mCellHeight) {
341                int index = baseIndexOfCurrentRow;
342                for (int centerX = xStart; centerX <= xEnd; centerX += mCellWidth) {
343                    if (key.squaredDistanceToEdge(centerX, centerY) < thresholdSquared) {
344                        neighborsFlatBuffer[index * keyCount + neighborCountPerCell[index]] = key;
345                        ++neighborCountPerCell[index];
346                    }
347                    ++index;
348                }
349                baseIndexOfCurrentRow += mGridWidth;
350            }
351        }
352
353        for (int i = 0; i < gridSize; ++i) {
354            final int base = i * keyCount;
355            mGridNeighbors[i] =
356                    Arrays.copyOfRange(neighborsFlatBuffer, base, base + neighborCountPerCell[i]);
357        }
358    }
359
360    public void fillArrayWithNearestKeyCodes(final int x, final int y, final int primaryKeyCode,
361            final int[] dest) {
362        final int destLength = dest.length;
363        if (destLength < 1) {
364            return;
365        }
366        int index = 0;
367        if (primaryKeyCode > Constants.CODE_SPACE) {
368            dest[index++] = primaryKeyCode;
369        }
370        final Key[] nearestKeys = getNearestKeys(x, y);
371        for (Key key : nearestKeys) {
372            if (index >= destLength) {
373                break;
374            }
375            final int code = key.mCode;
376            if (code <= Constants.CODE_SPACE) {
377                break;
378            }
379            dest[index++] = code;
380        }
381        if (index < destLength) {
382            dest[index] = Constants.NOT_A_CODE;
383        }
384    }
385
386    public Key[] getNearestKeys(final int x, final int y) {
387        if (mGridNeighbors == null) {
388            return EMPTY_KEY_ARRAY;
389        }
390        if (x >= 0 && x < mKeyboardMinWidth && y >= 0 && y < mKeyboardHeight) {
391            int index = (y / mCellHeight) * mGridWidth + (x / mCellWidth);
392            if (index < mGridSize) {
393                return mGridNeighbors[index];
394            }
395        }
396        return EMPTY_KEY_ARRAY;
397    }
398}
399