• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.inputmethod.latin;
18 
19 import android.inputmethodservice.Keyboard;
20 import android.inputmethodservice.Keyboard.Key;
21 
22 import java.util.Arrays;
23 import java.util.List;
24 
25 abstract class KeyDetector {
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 
setKeyboard(Keyboard keyboard, float correctionX, float correctionY)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 
getTouchX(int x)50     protected int getTouchX(int x) {
51         return x + mCorrectionX;
52     }
53 
getTouchY(int y)54     protected int getTouchY(int y) {
55         return y + mCorrectionY;
56     }
57 
getKeys()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 
setProximityCorrectionEnabled(boolean enabled)65     public void setProximityCorrectionEnabled(boolean enabled) {
66         mProximityCorrectOn = enabled;
67     }
68 
isProximityCorrectionEnabled()69     public boolean isProximityCorrectionEnabled() {
70         return mProximityCorrectOn;
71     }
72 
setProximityThreshold(int threshold)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 com.android.inputmethod.latin.LatinKeyboardView.NOT_A_KEY}
84      *         value.
85      */
newCodeArray()86     public int[] newCodeArray() {
87         int[] codes = new int[getMaxNearbyKeys()];
88         Arrays.fill(codes, LatinKeyboardBaseView.NOT_A_KEY);
89         return codes;
90     }
91 
92     /**
93      * Computes maximum size of the array that can contain all nearby key indices returned by
94      * {@link #getKeyIndexAndNearbyCodes}.
95      *
96      * @return Returns maximum size of the array that can contain all nearby key indices returned
97      *         by {@link #getKeyIndexAndNearbyCodes}.
98      */
getMaxNearbyKeys()99     abstract protected int getMaxNearbyKeys();
100 
101     /**
102      * Finds all possible nearby key indices around a touch event point and returns the nearest key
103      * index. The algorithm to determine the nearby keys depends on the threshold set by
104      * {@link #setProximityThreshold(int)} and the mode set by
105      * {@link #setProximityCorrectionEnabled(boolean)}.
106      *
107      * @param x The x-coordinate of a touch point
108      * @param y The y-coordinate of a touch point
109      * @param allKeys All nearby key indices are returned in this array
110      * @return The nearest key index
111      */
getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys)112     abstract public int getKeyIndexAndNearbyCodes(int x, int y, int[] allKeys);
113 }
114