KeyDetector.java revision 5d542c24922ae531a581c122d685c3d86047b468
1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.keyboard;
18
19import java.util.Arrays;
20import java.util.List;
21
22public abstract class KeyDetector {
23    public static final int NOT_A_KEY = -1;
24    public static final int NOT_A_CODE = -1;
25
26    protected Keyboard mKeyboard;
27
28    private Key[] mKeys;
29
30    protected int mCorrectionX;
31
32    protected int mCorrectionY;
33
34    protected boolean mProximityCorrectOn;
35
36    protected int mProximityThresholdSquare;
37
38    public Key[] setKeyboard(Keyboard keyboard, float correctionX, float correctionY) {
39        if (keyboard == null)
40            throw new NullPointerException();
41        mCorrectionX = (int)correctionX;
42        mCorrectionY = (int)correctionY;
43        mKeyboard = keyboard;
44        List<Key> keys = mKeyboard.getKeys();
45        Key[] array = keys.toArray(new Key[keys.size()]);
46        mKeys = array;
47        return array;
48    }
49
50    protected int getTouchX(int x) {
51        return x + mCorrectionX;
52    }
53
54    protected int getTouchY(int y) {
55        return y + mCorrectionY;
56    }
57
58    protected Key[] getKeys() {
59        if (mKeys == null)
60            throw new IllegalStateException("keyboard isn't set");
61        // mKeyboard is guaranteed not to be null at setKeybaord() method if mKeys is not null
62        return mKeys;
63    }
64
65    public void setProximityCorrectionEnabled(boolean enabled) {
66        mProximityCorrectOn = enabled;
67    }
68
69    public boolean isProximityCorrectionEnabled() {
70        return mProximityCorrectOn;
71    }
72
73    public void setProximityThreshold(int threshold) {
74        mProximityThresholdSquare = threshold * threshold;
75    }
76
77    /**
78     * Allocates array that can hold all key indices returned by {@link #getKeyIndexAndNearbyCodes}
79     * method. The maximum size of the array should be computed by {@link #getMaxNearbyKeys}.
80     *
81     * @return Allocates and returns an array that can hold all key indices returned by
82     *         {@link #getKeyIndexAndNearbyCodes} method. All elements in the returned array are
83     *         initialized by {@link #NOT_A_KEY} value.
84     */
85    public int[] newCodeArray() {
86        int[] codes = new int[getMaxNearbyKeys()];
87        Arrays.fill(codes, NOT_A_KEY);
88        return codes;
89    }
90
91    /**
92     * Computes maximum size of the array that can contain all nearby key indices returned by
93     * {@link #getKeyIndexAndNearbyCodes}.
94     *
95     * @return Returns maximum size of the array that can contain all nearby key indices returned
96     *         by {@link #getKeyIndexAndNearbyCodes}.
97     */
98    abstract protected int getMaxNearbyKeys();
99
100    /**
101     * Finds all possible nearby key indices around a touch event point and returns the nearest key
102     * index. The algorithm to determine the nearby keys depends on the threshold set by
103     * {@link #setProximityThreshold(int)} and the mode set by
104     * {@link #setProximityCorrectionEnabled(boolean)}.
105     *
106     * @param x The x-coordinate of a touch point
107     * @param y The y-coordinate of a touch point
108     * @param allCodes All nearby key code except functional key are returned in this array
109     * @return The nearest key index
110     */
111    abstract public int getKeyIndexAndNearbyCodes(int x, int y, final int[] allCodes);
112}
113