@@ -6,7 +6,6 @@ import android.content.res.Resources
66import android.content.res.TypedArray
77import android.content.res.XmlResourceParser
88import android.graphics.drawable.Drawable
9- import android.util.SparseArray
109import android.util.TypedValue
1110import android.util.Xml
1211import android.view.inputmethod.EditorInfo
@@ -45,14 +44,10 @@ class MyKeyboard {
4544 /* * Width of the screen available to fit the keyboard */
4645 private var mDisplayWidth = 0
4746
48- /* * What icon should we show at Enter key */
47+ /* * What icon should we show at Enter key */
4948 private var mEnterKeyType = IME_ACTION_NONE
5049
51- /* * Keyboard mode, or zero, if none. */
52- private var mCellWidth = 0
53- private var mCellHeight = 0
54- private var mGridNeighbors: SparseArray <IntArray ?>? = null
55- private var mProximityThreshold = 0
50+ /* * Keyboard rows */
5651 private val mRows = ArrayList <Row ?>()
5752
5853 companion object {
@@ -61,22 +56,12 @@ class MyKeyboard {
6156 private const val TAG_KEY = " Key"
6257 private const val EDGE_LEFT = 0x01
6358 private const val EDGE_RIGHT = 0x02
64- private const val EDGE_TOP = 0x04
65- private const val EDGE_BOTTOM = 0x08
6659 const val KEYCODE_SHIFT = - 1
6760 const val KEYCODE_MODE_CHANGE = - 2
6861 const val KEYCODE_ENTER = - 4
6962 const val KEYCODE_DELETE = - 5
7063 const val KEYCODE_SPACE = 32
7164
72- // Variables for pre-computing nearest keys.
73- private const val GRID_WIDTH = 10
74- private const val GRID_HEIGHT = 5
75- private const val GRID_SIZE = GRID_WIDTH * GRID_HEIGHT
76-
77- /* * Number of key widths from current touch point to search for nearest keys. */
78- private const val SEARCH_DISTANCE = 1.8f
79-
8065 fun getDimensionOrFraction (a : TypedArray , index : Int , base : Int , defValue : Int ): Int {
8166 val value = a.peekValue(index) ? : return defValue
8267 return when (value.type) {
@@ -174,7 +159,7 @@ class MyKeyboard {
174159
175160 /* *
176161 * Flags that specify the anchoring to edges of the keyboard for detecting touch events that are just out of the boundary of the key.
177- * This is a bit mask of [MyKeyboard.EDGE_LEFT], [MyKeyboard.EDGE_RIGHT], [MyKeyboard.EDGE_TOP] and [MyKeyboard.EDGE_BOTTOM] .
162+ * This is a bit mask of [MyKeyboard.EDGE_LEFT], [MyKeyboard.EDGE_RIGHT].
178163 */
179164 private var edgeFlags = 0
180165
@@ -240,24 +225,10 @@ class MyKeyboard {
240225 fun isInside (x : Int , y : Int ): Boolean {
241226 val leftEdge = edgeFlags and EDGE_LEFT > 0
242227 val rightEdge = edgeFlags and EDGE_RIGHT > 0
243- val topEdge = edgeFlags and EDGE_TOP > 0
244- val bottomEdge = edgeFlags and EDGE_BOTTOM > 0
245228 return ((x >= this .x || leftEdge && x <= this .x + width)
246229 && (x < this .x + width || rightEdge && x >= this .x)
247- && (y >= this .y || topEdge && y <= this .y + height)
248- && (y < this .y + height || bottomEdge && y >= this .y))
249- }
250-
251- /* *
252- * Returns the square of the distance between the center of the key and the given point.
253- * @param x the x-coordinate of the point
254- * @param y the y-coordinate of the point
255- * @return the square of the distance of the point from the center of the key
256- */
257- fun squaredDistanceFrom (x : Int , y : Int ): Int {
258- val xDist = this .x + width / 2 - x
259- val yDist = this .y + height / 2 - y
260- return xDist * xDist + yDist * yDist
230+ && (y >= this .y && y <= this .y + height)
231+ && (y < this .y + height && y >= this .y))
261232 }
262233 }
263234
@@ -332,60 +303,6 @@ class MyKeyboard {
332303 return false
333304 }
334305
335- private fun computeNearestNeighbors () {
336- // Round-up so we don't have any pixels outside the grid
337- mCellWidth = (mMinWidth + GRID_WIDTH - 1 ) / GRID_WIDTH
338- mCellHeight = (mHeight + GRID_HEIGHT - 1 ) / GRID_HEIGHT
339- mGridNeighbors = SparseArray <IntArray ?>(GRID_SIZE )
340- val indices = IntArray (mKeys!! .size)
341- val gridWidth: Int = GRID_WIDTH * mCellWidth
342- val gridHeight: Int = GRID_HEIGHT * mCellHeight
343- var x = 0
344- while (x < gridWidth) {
345- var y = 0
346- while (y < gridHeight) {
347- var count = 0
348- for (i in mKeys!! .indices) {
349- val key = mKeys!! [i]
350- if (key!! .squaredDistanceFrom(x, y) < mProximityThreshold || key.squaredDistanceFrom(
351- x + mCellWidth - 1 , y
352- ) < mProximityThreshold || (key.squaredDistanceFrom(x + mCellWidth - 1 , y + mCellHeight - 1 )
353- < mProximityThreshold) || key.squaredDistanceFrom(x, y + mCellHeight - 1 ) < mProximityThreshold
354- ) {
355- indices[count++ ] = i
356- }
357- }
358-
359- val cell = IntArray (count)
360- System .arraycopy(indices, 0 , cell, 0 , count)
361- mGridNeighbors!! .put(y / mCellHeight * GRID_WIDTH + x / mCellWidth, cell)
362- y + = mCellHeight
363- }
364- x + = mCellWidth
365- }
366- }
367-
368- /* *
369- * Returns the indices of the keys that are closest to the given point.
370- * @param x the x-coordinate of the point
371- * @param y the y-coordinate of the point
372- * @return the array of integer indices for the nearest keys to the given point. If the given point is out of range, then an array of size zero is returned.
373- */
374- fun getNearestKeys (x : Int , y : Int ): IntArray {
375- if (mGridNeighbors == null ) {
376- computeNearestNeighbors()
377- }
378-
379- if (x in 0 until mMinWidth && y >= 0 && y < mHeight) {
380- val index = y / mCellHeight * GRID_WIDTH + x / mCellWidth
381- if (index < GRID_SIZE ) {
382- return mGridNeighbors!! [index]!!
383- }
384- }
385-
386- return IntArray (0 )
387- }
388-
389306 private fun createRowFromXml (res : Resources , parser : XmlResourceParser ? ): Row {
390307 return Row (res, this , parser)
391308 }
@@ -458,8 +375,6 @@ class MyKeyboard {
458375 mDefaultWidth = getDimensionOrFraction(a, R .styleable.MyKeyboard_keyWidth , mDisplayWidth, mDisplayWidth / 10 )
459376 mDefaultHeight = res.getDimension(R .dimen.key_height).toInt()
460377 mDefaultHorizontalGap = getDimensionOrFraction(a, R .styleable.MyKeyboard_horizontalGap , mDisplayWidth, 0 )
461- mProximityThreshold = (mDefaultWidth * SEARCH_DISTANCE ).toInt()
462- mProximityThreshold * = mProximityThreshold // Square it for comparison
463378 a.recycle()
464379 }
465380}
0 commit comments