Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 58d4e610 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Use unmodifiable list to return the nearest keys

Change-Id: Ic9084b08fa9a1f25b7f4b98c627350583c401c12
parent 064af5c0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -211,10 +211,10 @@ public class Keyboard {
     * Returns the array of the keys that are closest to the given point.
     * @param x the x-coordinate of the point
     * @param y the y-coordinate of the point
     * @return the array of the nearest keys to the given point. If the given
     * @return the list of the nearest keys to the given point. If the given
     * point is out of range, then an array of size zero is returned.
     */
    public Key[] getNearestKeys(final int x, final int y) {
    public List<Key> getNearestKeys(final int x, final int y) {
        // Avoid dead pixels at edges of the keyboard
        final int adjustedX = Math.max(0, Math.min(x, mOccupiedWidth - 1));
        final int adjustedY = Math.max(0, Math.min(y, mOccupiedHeight - 1));
+21 −13
Original line number Diff line number Diff line
@@ -22,9 +22,13 @@ import android.util.Log;

import com.android.inputmethod.keyboard.internal.TouchPositionCorrection;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.JniUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ProximityInfo {
    private static final String TAG = ProximityInfo.class.getSimpleName();
@@ -34,7 +38,7 @@ public class ProximityInfo {
    public static final int MAX_PROXIMITY_CHARS_SIZE = 16;
    /** Number of key widths from current touch point to search for nearest keys. */
    private static final float SEARCH_DISTANCE = 1.2f;
    private static final Key[] EMPTY_KEY_ARRAY = new Key[0];
    private static final List<Key> EMPTY_KEY_LIST = Collections.emptyList();
    private static final float DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS = 0.15f;

    private final int mGridWidth;
@@ -48,7 +52,7 @@ public class ProximityInfo {
    private final int mMostCommonKeyWidth;
    private final int mMostCommonKeyHeight;
    private final Key[] mKeys;
    private final Key[][] mGridNeighbors;
    private final List<Key>[] mGridNeighbors;
    private final String mLocaleStr;

    ProximityInfo(final String localeStr, final int gridWidth, final int gridHeight,
@@ -70,7 +74,7 @@ public class ProximityInfo {
        mMostCommonKeyHeight = mostCommonKeyHeight;
        mMostCommonKeyWidth = mostCommonKeyWidth;
        mKeys = keys;
        mGridNeighbors = new Key[mGridSize][];
        mGridNeighbors = new List[mGridSize];
        if (minWidth == 0 || height == 0) {
            // No proximity required. Keyboard might be more keys keyboard.
            return;
@@ -110,14 +114,14 @@ public class ProximityInfo {
    }

    private long createNativeProximityInfo(final TouchPositionCorrection touchPositionCorrection) {
        final Key[][] gridNeighborKeys = mGridNeighbors;
        final List<Key>[] gridNeighborKeys = mGridNeighbors;
        final int[] proximityCharsArray = new int[mGridSize * MAX_PROXIMITY_CHARS_SIZE];
        Arrays.fill(proximityCharsArray, Constants.NOT_A_CODE);
        for (int i = 0; i < mGridSize; ++i) {
            final int proximityCharsLength = gridNeighborKeys[i].length;
            final int proximityCharsLength = gridNeighborKeys[i].size();
            int infoIndex = i * MAX_PROXIMITY_CHARS_SIZE;
            for (int j = 0; j < proximityCharsLength; ++j) {
                final Key neighborKey = gridNeighborKeys[i][j];
                final Key neighborKey = gridNeighborKeys[i].get(j);
                // Excluding from proximityCharsArray
                if (!needsProximityInfo(neighborKey)) {
                    continue;
@@ -353,9 +357,13 @@ y |---+---+---+---+-v-+-|-+---+---+---+---+---| | thresholdBase and get
        }

        for (int i = 0; i < gridSize; ++i) {
            final int base = i * keyCount;
            mGridNeighbors[i] =
                    Arrays.copyOfRange(neighborsFlatBuffer, base, base + neighborCountPerCell[i]);
            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);
        }
    }

@@ -369,7 +377,7 @@ y |---+---+---+---+-v-+-|-+---+---+---+---+---| | thresholdBase and get
        if (primaryKeyCode > Constants.CODE_SPACE) {
            dest[index++] = primaryKeyCode;
        }
        final Key[] nearestKeys = getNearestKeys(x, y);
        final List<Key> nearestKeys = getNearestKeys(x, y);
        for (Key key : nearestKeys) {
            if (index >= destLength) {
                break;
@@ -385,9 +393,9 @@ y |---+---+---+---+-v-+-|-+---+---+---+---+---| | thresholdBase and get
        }
    }

    public Key[] getNearestKeys(final int x, final int y) {
    public List<Key> getNearestKeys(final int x, final int y) {
        if (mGridNeighbors == null) {
            return EMPTY_KEY_ARRAY;
            return EMPTY_KEY_LIST;
        }
        if (x >= 0 && x < mKeyboardMinWidth && y >= 0 && y < mKeyboardHeight) {
            int index = (y / mCellHeight) * mGridWidth + (x / mCellWidth);
@@ -395,6 +403,6 @@ y |---+---+---+---+-v-+-|-+---+---+---+---+---| | thresholdBase and get
                return mGridNeighbors[index];
            }
        }
        return EMPTY_KEY_ARRAY;
        return EMPTY_KEY_LIST;
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.inputmethod.latin.utils.JsonUtils;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@@ -216,9 +217,9 @@ public class DynamicGridKeyboard extends Keyboard {
    }

    @Override
    public Key[] getNearestKeys(final int x, final int y) {
    public List<Key> getNearestKeys(final int x, final int y) {
        // TODO: Calculate the nearest key index in mGridKeys from x and y.
        return getKeys();
        return Arrays.asList(getKeys());
    }

    static final class GridKey extends Key {