some code style updates

This commit is contained in:
tibbi 2022-01-08 20:21:22 +01:00
parent 1e4906c48d
commit 850878cfe8
3 changed files with 88 additions and 172 deletions

View File

@ -20,7 +20,6 @@ import android.content.res.Resources
import android.content.res.TypedArray import android.content.res.TypedArray
import android.content.res.XmlResourceParser import android.content.res.XmlResourceParser
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.inputmethodservice.Keyboard
import android.text.TextUtils import android.text.TextUtils
import android.util.TypedValue import android.util.TypedValue
import android.util.Xml import android.util.Xml
@ -71,7 +70,6 @@ class MyKeyboard {
/** Is the keyboard in the shifted state */ /** Is the keyboard in the shifted state */
var isShifted = false var isShifted = false
private set
/** Key instance for the shift key, if present */ /** Key instance for the shift key, if present */
private val mShiftKeys = arrayOf<Key?>(null, null) private val mShiftKeys = arrayOf<Key?>(null, null)
@ -92,17 +90,15 @@ class MyKeyboard {
*/ */
/** Total height of the keyboard, including the padding and keys */ /** Total height of the keyboard, including the padding and keys */
var height = 0 var height = 0
private set
/** /**
* Total width of the keyboard, including left side gaps and keys, but not any gaps on the * Total width of the keyboard, including left side gaps and keys, but not any gaps on the
* right side. * right side.
*/ */
var minWidth = 0 var minWidth = 0
private set
/** List of keys in this keyboard */ /** List of keys in this keyboard */
private var mKeys: MutableList<Key?>? = null var mKeys: MutableList<Key?>? = null
/** List of modifier keys such as Shift & Alt, if any */ /** List of modifier keys such as Shift & Alt, if any */
private var mModifierKeys: MutableList<Key?>? = null private var mModifierKeys: MutableList<Key?>? = null
@ -122,8 +118,6 @@ class MyKeyboard {
private val rows = ArrayList<Row?>() private val rows = ArrayList<Row?>()
companion object { companion object {
const val TAG = "Keyboard"
// Keyboard XML Tags // Keyboard XML Tags
private const val TAG_KEYBOARD = "Keyboard" private const val TAG_KEYBOARD = "Keyboard"
private const val TAG_ROW = "Row" private const val TAG_ROW = "Row"
@ -160,7 +154,7 @@ class MyKeyboard {
/** /**
* Container for keys in the keyboard. All keys in a row are at the same Y-coordinate. * Container for keys in the keyboard. All keys in a row are at the same Y-coordinate.
* Some of the key size defaults can be overridden per row from what the [Keyboard] * Some of the key size defaults can be overridden per row from what the [MyKeyboard]
* defines. * defines.
* @attr ref android.R.styleable#Keyboard_keyWidth * @attr ref android.R.styleable#Keyboard_keyWidth
* @attr ref android.R.styleable#Keyboard_keyHeight * @attr ref android.R.styleable#Keyboard_keyHeight
@ -185,7 +179,7 @@ class MyKeyboard {
/** /**
* Edge flags for this row of keys. Possible values that can be assigned are * Edge flags for this row of keys. Possible values that can be assigned are
* [EDGE_TOP][Keyboard.EDGE_TOP] and [EDGE_BOTTOM][Keyboard.EDGE_BOTTOM] * [EDGE_TOP][MyKeyboard.EDGE_TOP] and [EDGE_BOTTOM][MyKeyboard.EDGE_BOTTOM]
*/ */
var rowEdgeFlags = 0 var rowEdgeFlags = 0
@ -242,7 +236,7 @@ class MyKeyboard {
var codes = ArrayList<Int>() var codes = ArrayList<Int>()
/** Label to display */ /** Label to display */
var label: CharSequence? = null var label: CharSequence = ""
/** Icon to display instead of a label. Icon takes precedence over a label */ /** Icon to display instead of a label. Icon takes precedence over a label */
var icon: Drawable? = null var icon: Drawable? = null
@ -283,8 +277,8 @@ class MyKeyboard {
/** /**
* Flags that specify the anchoring to edges of the keyboard for detecting touch events * Flags that specify the anchoring to edges of the keyboard for detecting touch events
* that are just out of the boundary of the key. This is a bit mask of * that are just out of the boundary of the key. This is a bit mask of
* [Keyboard.EDGE_LEFT], [Keyboard.EDGE_RIGHT], [Keyboard.EDGE_TOP] and * [MyKeyboard.EDGE_LEFT], [MyKeyboard.EDGE_RIGHT], [MyKeyboard.EDGE_TOP] and
* [Keyboard.EDGE_BOTTOM]. * [MyKeyboard.EDGE_BOTTOM].
*/ */
var edgeFlags: Int var edgeFlags: Int
@ -292,7 +286,7 @@ class MyKeyboard {
var modifier = false var modifier = false
/** The keyboard that this key belongs to */ /** The keyboard that this key belongs to */
private val keyboard: MyKeyboard private val keyboard: MyKeyboard = parent.parent
/** /**
* If this key pops up a mini keyboard, this is the resource id for the XML layout for that * If this key pops up a mini keyboard, this is the resource id for the XML layout for that
@ -332,46 +326,29 @@ class MyKeyboard {
} }
iconPreview = a.getDrawable(R.styleable.Keyboard_Key_iconPreview) iconPreview = a.getDrawable(R.styleable.Keyboard_Key_iconPreview)
if (iconPreview != null) { iconPreview?.setBounds(0, 0, iconPreview!!.intrinsicWidth, iconPreview!!.intrinsicHeight)
iconPreview!!.setBounds(
0, 0, iconPreview!!.intrinsicWidth, popupCharacters = a.getText(R.styleable.Keyboard_Key_popupCharacters)
iconPreview!!.intrinsicHeight popupResId = a.getResourceId(R.styleable.Keyboard_Key_popupKeyboard, 0)
) repeatable = a.getBoolean(R.styleable.Keyboard_Key_isRepeatable, false)
} modifier = a.getBoolean(R.styleable.Keyboard_Key_isModifier, false)
popupCharacters = a.getText( sticky = a.getBoolean(R.styleable.Keyboard_Key_isSticky, false)
R.styleable.Keyboard_Key_popupCharacters
)
popupResId = a.getResourceId(
R.styleable.Keyboard_Key_popupKeyboard, 0
)
repeatable = a.getBoolean(
R.styleable.Keyboard_Key_isRepeatable, false
)
modifier = a.getBoolean(
R.styleable.Keyboard_Key_isModifier, false
)
sticky = a.getBoolean(
R.styleable.Keyboard_Key_isSticky, false
)
edgeFlags = a.getInt(R.styleable.Keyboard_Key_keyEdgeFlags, 0) edgeFlags = a.getInt(R.styleable.Keyboard_Key_keyEdgeFlags, 0)
edgeFlags = edgeFlags or parent.rowEdgeFlags edgeFlags = edgeFlags or parent.rowEdgeFlags
icon = a.getDrawable( icon = a.getDrawable(R.styleable.Keyboard_Key_keyIcon)
R.styleable.Keyboard_Key_keyIcon
) icon?.setBounds(0, 0, icon!!.intrinsicWidth, icon!!.intrinsicHeight)
if (icon != null) {
icon!!.setBounds(0, 0, icon!!.intrinsicWidth, icon!!.intrinsicHeight)
}
label = a.getText(R.styleable.Keyboard_Key_keyLabel) label = a.getText(R.styleable.Keyboard_Key_keyLabel)
text = a.getText(R.styleable.Keyboard_Key_keyOutputText) text = a.getText(R.styleable.Keyboard_Key_keyOutputText)
if (!TextUtils.isEmpty(label)) { if (!TextUtils.isEmpty(label)) {
codes = arrayListOf(label!![0].toInt()) codes = arrayListOf(label[0].toInt())
} }
a.recycle() a.recycle()
} }
/** Create an empty key with no attributes. */ /** Create an empty key with no attributes. */
init { init {
keyboard = parent.parent
height = parent.defaultHeight height = parent.defaultHeight
width = parent.defaultWidth width = parent.defaultWidth
gap = parent.defaultHorizontalGap gap = parent.defaultHorizontalGap
@ -415,7 +392,7 @@ class MyKeyboard {
fun parseCSV(value: String): ArrayList<Int> { fun parseCSV(value: String): ArrayList<Int> {
var count = 0 var count = 0
var lastIndex = 0 var lastIndex = 0
if (value.length > 0) { if (value.isNotEmpty()) {
count++ count++
while (value.indexOf(",", lastIndex + 1).also { lastIndex = it } > 0) { while (value.indexOf(",", lastIndex + 1).also { lastIndex = it } > 0) {
count++ count++
@ -496,26 +473,12 @@ class MyKeyboard {
} }
companion object { companion object {
private val KEY_STATE_NORMAL_ON = intArrayOf( private val KEY_STATE_NORMAL_ON = intArrayOf(android.R.attr.state_checkable, android.R.attr.state_checked)
android.R.attr.state_checkable, private val KEY_STATE_PRESSED_ON = intArrayOf(android.R.attr.state_pressed, android.R.attr.state_checkable, android.R.attr.state_checked)
android.R.attr.state_checked private val KEY_STATE_NORMAL_OFF = intArrayOf(android.R.attr.state_checkable)
) private val KEY_STATE_PRESSED_OFF = intArrayOf(android.R.attr.state_pressed, android.R.attr.state_checkable)
private val KEY_STATE_PRESSED_ON = intArrayOf(
android.R.attr.state_pressed,
android.R.attr.state_checkable,
android.R.attr.state_checked
)
private val KEY_STATE_NORMAL_OFF = intArrayOf(
android.R.attr.state_checkable
)
private val KEY_STATE_PRESSED_OFF = intArrayOf(
android.R.attr.state_pressed,
android.R.attr.state_checkable
)
private val KEY_STATE_NORMAL = intArrayOf() private val KEY_STATE_NORMAL = intArrayOf()
private val KEY_STATE_PRESSED = intArrayOf( private val KEY_STATE_PRESSED = intArrayOf(android.R.attr.state_pressed)
android.R.attr.state_pressed
)
} }
} }
@ -539,7 +502,6 @@ class MyKeyboard {
mDefaultVerticalGap = 0 mDefaultVerticalGap = 0
mDefaultHeight = mDefaultWidth mDefaultHeight = mDefaultWidth
mKeys = ArrayList() mKeys = ArrayList()
mModifierKeys = ArrayList()
mKeyboardMode = modeId mKeyboardMode = modeId
loadKeyboard(context, context.resources.getXml(xmlLayoutResId)) loadKeyboard(context, context.resources.getXml(xmlLayoutResId))
} }
@ -566,7 +528,6 @@ class MyKeyboard {
mDefaultVerticalGap = 0 mDefaultVerticalGap = 0
mDefaultHeight = mDefaultWidth mDefaultHeight = mDefaultWidth
mKeys = ArrayList() mKeys = ArrayList()
mModifierKeys = ArrayList()
mKeyboardMode = modeId mKeyboardMode = modeId
loadKeyboard(context, context.resources.getXml(xmlLayoutResId)) loadKeyboard(context, context.resources.getXml(xmlLayoutResId))
} }
@ -587,10 +548,8 @@ class MyKeyboard {
* number of keys that can fit in a row, it will be ignored. If this number is -1, the * number of keys that can fit in a row, it will be ignored. If this number is -1, the
* keyboard will fit as many keys as possible in each row. * keyboard will fit as many keys as possible in each row.
*/ */
constructor( constructor(context: Context, layoutTemplateResId: Int, characters: CharSequence, columns: Int, horizontalPadding: Int) :
context: Context, layoutTemplateResId: Int, this(context, layoutTemplateResId) {
characters: CharSequence, columns: Int, horizontalPadding: Int
) : this(context, layoutTemplateResId) {
var x = 0 var x = 0
var y = 0 var y = 0
var column = 0 var column = 0
@ -602,15 +561,15 @@ class MyKeyboard {
row.verticalGap = mDefaultVerticalGap row.verticalGap = mDefaultVerticalGap
row.rowEdgeFlags = EDGE_TOP or EDGE_BOTTOM row.rowEdgeFlags = EDGE_TOP or EDGE_BOTTOM
val maxColumns = if (columns == -1) Int.MAX_VALUE else columns val maxColumns = if (columns == -1) Int.MAX_VALUE else columns
for (i in 0 until characters.length) { for (element in characters) {
val c = characters[i] val c = element
if (column >= maxColumns if (column >= maxColumns || x + mDefaultWidth + horizontalPadding > mDisplayWidth
|| x + mDefaultWidth + horizontalPadding > mDisplayWidth
) { ) {
x = 0 x = 0
y += mDefaultVerticalGap + mDefaultHeight y += mDefaultVerticalGap + mDefaultHeight
column = 0 column = 0
} }
val key = Key(row) val key = Key(row)
key.x = x key.x = x
key.y = y key.y = y
@ -654,23 +613,18 @@ class MyKeyboard {
} }
} }
} }
minWidth = newWidth minWidth = newWidth
// TODO: This does not adjust the vertical placement according to the new size. // TODO: This does not adjust the vertical placement according to the new size.
// The main problem in the previous code was horizontal placement/size, but we should // The main problem in the previous code was horizontal placement/size, but we should
// also recalculate the vertical sizes/positions when we get this resize call. // also recalculate the vertical sizes/positions when we get this resize call.
} }
val keys: List<Key?>?
get() = mKeys
val modifierKeys: List<Key?>?
get() = mModifierKeys
fun setShifted(shiftState: Boolean): Boolean { fun setShifted(shiftState: Boolean): Boolean {
for (shiftKey in mShiftKeys) { for (shiftKey in mShiftKeys) {
if (shiftKey != null) { shiftKey?.on = shiftState
shiftKey.on = shiftState
}
} }
if (isShifted != shiftState) { if (isShifted != shiftState) {
isShifted = shiftState isShifted = shiftState
return true return true
@ -678,9 +632,6 @@ class MyKeyboard {
return false return false
} }
val shiftKeyIndex: Int
get() = shiftKeyIndices[0]
private fun computeNearestNeighbors() { private fun computeNearestNeighbors() {
// Round-up so we don't have any pixels outside the grid // Round-up so we don't have any pixels outside the grid
mCellWidth = (minWidth + GRID_WIDTH - 1) / GRID_WIDTH mCellWidth = (minWidth + GRID_WIDTH - 1) / GRID_WIDTH
@ -720,11 +671,11 @@ class MyKeyboard {
* @return the array of integer indices for the nearest keys to the given point. If the given * @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. * point is out of range, then an array of size zero is returned.
*/ */
fun getNearestKeys(x: Int, y: Int): IntArray? { fun getNearestKeys(x: Int, y: Int): IntArray {
if (x >= 0 && x < minWidth && y >= 0 && y < height) { if (x in 0 until minWidth && y >= 0 && y < height) {
val index: Int = y / mCellHeight * GRID_WIDTH + x / mCellWidth val index: Int = y / mCellHeight * GRID_WIDTH + x / mCellWidth
if (index < GRID_SIZE) { if (index < GRID_SIZE) {
return mGridNeighbors[index] return mGridNeighbors[index]!!
} }
} }
return IntArray(0) return IntArray(0)
@ -809,41 +760,20 @@ class MyKeyboard {
private fun skipToEndOfRow(parser: XmlResourceParser) { private fun skipToEndOfRow(parser: XmlResourceParser) {
var event: Int var event: Int
while (parser.next().also { event = it } != XmlResourceParser.END_DOCUMENT) { while (parser.next().also { event = it } != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.END_TAG if (event == XmlResourceParser.END_TAG && parser.name == TAG_ROW) {
&& parser.name == TAG_ROW
) {
break break
} }
} }
} }
private fun parseKeyboardAttributes(res: Resources, parser: XmlResourceParser) { private fun parseKeyboardAttributes(res: Resources, parser: XmlResourceParser) {
val a = res.obtainAttributes( val a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard)
Xml.asAttributeSet(parser), mDefaultWidth = getDimensionOrFraction(a, R.styleable.Keyboard_keyWidth, mDisplayWidth, mDisplayWidth / 10)
R.styleable.Keyboard mDefaultHeight = getDimensionOrFraction(a, R.styleable.Keyboard_keyHeight, mDisplayHeight, 50)
) mDefaultHorizontalGap = getDimensionOrFraction(a, R.styleable.Keyboard_horizontalGap, mDisplayWidth, 0)
mDefaultWidth = getDimensionOrFraction( mDefaultVerticalGap = getDimensionOrFraction(a, R.styleable.Keyboard_verticalGap, mDisplayHeight, 0)
a,
R.styleable.Keyboard_keyWidth,
mDisplayWidth, mDisplayWidth / 10
)
mDefaultHeight = getDimensionOrFraction(
a,
R.styleable.Keyboard_keyHeight,
mDisplayHeight, 50
)
mDefaultHorizontalGap = getDimensionOrFraction(
a,
R.styleable.Keyboard_horizontalGap,
mDisplayWidth, 0
)
mDefaultVerticalGap = getDimensionOrFraction(
a,
R.styleable.Keyboard_verticalGap,
mDisplayHeight, 0
)
mProximityThreshold = (mDefaultWidth * SEARCH_DISTANCE) as Int mProximityThreshold = (mDefaultWidth * SEARCH_DISTANCE) as Int
mProximityThreshold = mProximityThreshold * mProximityThreshold // Square it for comparison mProximityThreshold *= mProximityThreshold // Square it for comparison
a.recycle() a.recycle()
} }
} }

View File

@ -7,17 +7,18 @@ import android.view.KeyEvent
import android.view.View import android.view.View
import com.simplemobiletools.commons.extensions.performHapticFeedback import com.simplemobiletools.commons.extensions.performHapticFeedback
import com.simplemobiletools.keyboard.R import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.helpers.MyKeyboard
import com.simplemobiletools.keyboard.views.MyKeyboardView import com.simplemobiletools.keyboard.views.MyKeyboardView
// based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/ // based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionListener { class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionListener {
private var keyboard: Keyboard? = null private var keyboard: MyKeyboard? = null
private var keyboardView: MyKeyboardView? = null private var keyboardView: MyKeyboardView? = null
private var caps = false private var caps = false
override fun onCreateInputView(): View { override fun onCreateInputView(): View {
keyboardView = layoutInflater.inflate(R.layout.keyboard_view_keyboard, null) as MyKeyboardView keyboardView = layoutInflater.inflate(R.layout.keyboard_view_keyboard, null) as MyKeyboardView
keyboard = Keyboard(this, R.xml.keys_layout) keyboard = MyKeyboard(this, R.xml.keys_layout)
keyboardView!!.setKeyboard(keyboard!!) keyboardView!!.setKeyboard(keyboard!!)
keyboardView!!.onKeyboardActionListener = this keyboardView!!.onKeyboardActionListener = this
return keyboardView!! return keyboardView!!
@ -31,7 +32,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
val inputConnection = currentInputConnection val inputConnection = currentInputConnection
if (inputConnection != null) { if (inputConnection != null) {
when (primaryCode) { when (primaryCode) {
Keyboard.KEYCODE_DELETE -> { MyKeyboard.KEYCODE_DELETE -> {
val selectedText = inputConnection.getSelectedText(0) val selectedText = inputConnection.getSelectedText(0)
if (TextUtils.isEmpty(selectedText)) { if (TextUtils.isEmpty(selectedText)) {
inputConnection.deleteSurroundingText(1, 0) inputConnection.deleteSurroundingText(1, 0)
@ -41,12 +42,12 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
keyboard!!.isShifted = caps keyboard!!.isShifted = caps
keyboardView!!.invalidateAllKeys() keyboardView!!.invalidateAllKeys()
} }
Keyboard.KEYCODE_SHIFT -> { MyKeyboard.KEYCODE_SHIFT -> {
caps = !caps caps = !caps
keyboard!!.isShifted = caps keyboard!!.isShifted = caps
keyboardView!!.invalidateAllKeys() keyboardView!!.invalidateAllKeys()
} }
Keyboard.KEYCODE_DONE -> inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)) MyKeyboard.KEYCODE_DONE -> inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER))
else -> { else -> {
var code = primaryCode.toChar() var code = primaryCode.toChar()
if (Character.isLetter(code) && caps) { if (Character.isLetter(code) && caps) {

View File

@ -18,6 +18,7 @@ import android.view.accessibility.AccessibilityManager
import android.widget.PopupWindow import android.widget.PopupWindow
import android.widget.TextView import android.widget.TextView
import com.simplemobiletools.keyboard.R import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.helpers.MyKeyboard
import java.util.* import java.util.*
/** /**
@ -96,7 +97,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
fun swipeUp() fun swipeUp()
} }
private var mKeyboard: Keyboard? = null private var mKeyboard: MyKeyboard? = null
private var mCurrentKeyIndex: Int = NOT_A_KEY private var mCurrentKeyIndex: Int = NOT_A_KEY
private var mLabelTextSize = 0 private var mLabelTextSize = 0
@ -121,8 +122,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private var mPopupParent: View private var mPopupParent: View
private var mMiniKeyboardOffsetX = 0 private var mMiniKeyboardOffsetX = 0
private var mMiniKeyboardOffsetY = 0 private var mMiniKeyboardOffsetY = 0
private val mMiniKeyboardCache: MutableMap<Keyboard.Key, View?> private val mMiniKeyboardCache: MutableMap<MyKeyboard.Key, View?>
private var mKeys = ArrayList<Keyboard.Key>() private var mKeys = ArrayList<MyKeyboard.Key>()
/** /**
* Returns the [OnKeyboardActionListener] object. * Returns the [OnKeyboardActionListener] object.
* @return the listener attached to this keyboard * @return the listener attached to this keyboard
@ -178,7 +179,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private var mRepeatKeyIndex: Int = NOT_A_KEY private var mRepeatKeyIndex: Int = NOT_A_KEY
private var mPopupLayout = 0 private var mPopupLayout = 0
private var mAbortKey = false private var mAbortKey = false
private var mInvalidatedKey: Keyboard.Key? = null private var mInvalidatedKey: MyKeyboard.Key? = null
private val mClipRegion = Rect(0, 0, 0, 0) private val mClipRegion = Rect(0, 0, 0, 0)
private var mPossiblePoly = false private var mPossiblePoly = false
private val mSwipeTracker: SwipeTracker = SwipeTracker() private val mSwipeTracker: SwipeTracker = SwipeTracker()
@ -225,7 +226,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
companion object { companion object {
private const val NOT_A_KEY = -1 private const val NOT_A_KEY = -1
private val KEY_DELETE = intArrayOf(Keyboard.KEYCODE_DELETE) private val KEY_DELETE = intArrayOf(MyKeyboard.KEYCODE_DELETE)
private val LONG_PRESSABLE_STATE_SET = intArrayOf(R.attr.state_long_pressable) private val LONG_PRESSABLE_STATE_SET = intArrayOf(R.attr.state_long_pressable)
private const val MSG_SHOW_PREVIEW = 1 private const val MSG_SHOW_PREVIEW = 1
private const val MSG_REMOVE_PREVIEW = 2 private const val MSG_REMOVE_PREVIEW = 2
@ -387,7 +388,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
* @see .getKeyboard * @see .getKeyboard
* @param keyboard the keyboard to display in this view * @param keyboard the keyboard to display in this view
*/ */
fun setKeyboard(keyboard: Keyboard) { fun setKeyboard(keyboard: MyKeyboard) {
if (mKeyboard != null) { if (mKeyboard != null) {
showPreview(NOT_A_KEY) showPreview(NOT_A_KEY)
} }
@ -395,8 +396,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
// Remove any pending messages // Remove any pending messages
removeMessages() removeMessages()
mKeyboard = keyboard mKeyboard = keyboard
val keys = mKeyboard!!.keys val keys = mKeyboard!!.mKeys
mKeys = keys.toMutableList() as ArrayList<Keyboard.Key> mKeys = keys!!.toMutableList() as ArrayList<MyKeyboard.Key>
requestLayout() requestLayout()
// Hint to reallocate the buffer if the size changed // Hint to reallocate the buffer if the size changed
mKeyboardChanged = true mKeyboardChanged = true
@ -416,13 +417,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
* @see KeyboardView.isShifted * @see KeyboardView.isShifted
*/ */
fun setShifted(shifted: Boolean): Boolean { fun setShifted(shifted: Boolean): Boolean {
if (mKeyboard != null) { if (mKeyboard?.setShifted(shifted) == true) {
if (mKeyboard!!.setShifted(shifted)) {
// The whole keyboard probably needs to be redrawn // The whole keyboard probably needs to be redrawn
invalidateAllKeys() invalidateAllKeys()
return true return true
} }
}
return false return false
} }
@ -433,11 +432,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
* @see KeyboardView.setShifted * @see KeyboardView.setShifted
*/ */
fun isShifted(): Boolean { fun isShifted(): Boolean {
return if (mKeyboard != null) { return mKeyboard?.isShifted ?: false
mKeyboard!!.isShifted
} else {
false
}
} }
fun setPopupParent(v: View) { fun setPopupParent(v: View) {
@ -487,7 +482,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
* the touch distance from a key's center to avoid taking a square root. * the touch distance from a key's center to avoid taking a square root.
* @param keyboard * @param keyboard
*/ */
private fun computeProximityThreshold(keyboard: Keyboard?) { private fun computeProximityThreshold(keyboard: MyKeyboard?) {
if (keyboard == null) { if (keyboard == null) {
return return
} }
@ -510,9 +505,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
public override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { public override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh) super.onSizeChanged(w, h, oldw, oldh)
if (mKeyboard != null) { mKeyboard?.resize(w, h)
//mKeyboard.resize(w, h)
}
// Release the buffer, if any and it will be reallocated on the next draw // Release the buffer, if any and it will be reallocated on the next draw
mBuffer = null mBuffer = null
} }
@ -578,11 +571,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
keyBackground!!.state = drawableState keyBackground!!.state = drawableState
// Switch the character to uppercase if shift is pressed // Switch the character to uppercase if shift is pressed
val label = if (key.label == null) { val label = adjustCase(key.label)?.toString()
null
} else {
adjustCase(key.label).toString()
}
val bounds = keyBackground.bounds val bounds = keyBackground.bounds
if (key.width != bounds.right || key.height != bounds.bottom) { if (key.width != bounds.right || key.height != bounds.bottom) {
@ -611,11 +600,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
// Turn off drop shadow // Turn off drop shadow
paint.setShadowLayer(0f, 0f, 0f, 0) paint.setShadowLayer(0f, 0f, 0f, 0)
} else if (key.icon != null) { } else if (key.icon != null) {
val drawableX = (key.width - padding.left - padding.right - key.icon.intrinsicWidth) / 2 + padding.left val drawableX = (key.width - padding.left - padding.right - key.icon!!.intrinsicWidth) / 2 + padding.left
val drawableY = (key.height - key.icon.intrinsicHeight) / 2 val drawableY = (key.height - key.icon!!.intrinsicHeight) / 2
canvas.translate(drawableX.toFloat(), drawableY.toFloat()) canvas.translate(drawableX.toFloat(), drawableY.toFloat())
key.icon.setBounds(0, 0, key.icon.intrinsicWidth, key.icon.intrinsicHeight) key.icon!!.setBounds(0, 0, key.icon!!.intrinsicWidth, key.icon!!.intrinsicHeight)
key.icon.draw(canvas) key.icon!!.draw(canvas)
canvas.translate(-drawableX.toFloat(), -drawableY.toFloat()) canvas.translate(-drawableX.toFloat(), -drawableY.toFloat())
} }
canvas.translate((-key.x - kbdPaddingLeft).toFloat(), (-key.y - kbdPaddingTop).toFloat()) canvas.translate((-key.x - kbdPaddingLeft).toFloat(), (-key.y - kbdPaddingTop).toFloat())
@ -708,7 +697,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
// Multi-tap // Multi-tap
if (mInMultiTap) { if (mInMultiTap) {
if (mTapCount != -1) { if (mTapCount != -1) {
onKeyboardActionListener!!.onKey(Keyboard.KEYCODE_DELETE, KEY_DELETE) onKeyboardActionListener!!.onKey(MyKeyboard.KEYCODE_DELETE, KEY_DELETE)
} else { } else {
mTapCount = 0 mTapCount = 0
} }
@ -725,7 +714,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
/** /**
* Handle multi-tap keys by producing the key label for the current multi-tap state. * Handle multi-tap keys by producing the key label for the current multi-tap state.
*/ */
private fun getPreviewText(key: Keyboard.Key): CharSequence? { private fun getPreviewText(key: MyKeyboard.Key): CharSequence? {
return if (mInMultiTap) { return if (mInMultiTap) {
// Multi-tap // Multi-tap
mPreviewLabel.setLength(0) mPreviewLabel.setLength(0)
@ -835,10 +824,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
val popupWidth = Math.max(mPreviewText!!.measuredWidth, key.width + mPreviewText!!.paddingLeft + mPreviewText!!.paddingRight) val popupWidth = Math.max(mPreviewText!!.measuredWidth, key.width + mPreviewText!!.paddingLeft + mPreviewText!!.paddingRight)
val popupHeight = mPreviewHeight val popupHeight = mPreviewHeight
val lp = mPreviewText!!.layoutParams val lp = mPreviewText!!.layoutParams
if (lp != null) { lp?.width = popupWidth
lp.width = popupWidth lp?.height = popupHeight
lp.height = popupHeight
}
if (!mPreviewCentered) { if (!mPreviewCentered) {
mPopupPreviewX = key.x - mPreviewText!!.paddingLeft + paddingLeft mPopupPreviewX = key.x - mPreviewText!!.paddingLeft + paddingLeft
@ -895,12 +882,12 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
val event = AccessibilityEvent.obtain(eventType) val event = AccessibilityEvent.obtain(eventType)
onInitializeAccessibilityEvent(event) onInitializeAccessibilityEvent(event)
val text: String = when (code) { val text: String = when (code) {
Keyboard.KEYCODE_ALT -> context.getString(R.string.keyboardview_keycode_alt) MyKeyboard.KEYCODE_ALT -> context.getString(R.string.keyboardview_keycode_alt)
Keyboard.KEYCODE_CANCEL -> context.getString(R.string.keyboardview_keycode_cancel) MyKeyboard.KEYCODE_CANCEL -> context.getString(R.string.keyboardview_keycode_cancel)
Keyboard.KEYCODE_DELETE -> context.getString(R.string.keyboardview_keycode_delete) MyKeyboard.KEYCODE_DELETE -> context.getString(R.string.keyboardview_keycode_delete)
Keyboard.KEYCODE_DONE -> context.getString(R.string.keyboardview_keycode_done) MyKeyboard.KEYCODE_DONE -> context.getString(R.string.keyboardview_keycode_done)
Keyboard.KEYCODE_MODE_CHANGE -> context.getString(R.string.keyboardview_keycode_mode_change) MyKeyboard.KEYCODE_MODE_CHANGE -> context.getString(R.string.keyboardview_keycode_mode_change)
Keyboard.KEYCODE_SHIFT -> context.getString(R.string.keyboardview_keycode_shift) MyKeyboard.KEYCODE_SHIFT -> context.getString(R.string.keyboardview_keycode_shift)
'\n'.toInt() -> context.getString(R.string.keyboardview_keycode_enter) '\n'.toInt() -> context.getString(R.string.keyboardview_keycode_enter)
else -> code.toChar().toString() else -> code.toChar().toString()
} }
@ -973,7 +960,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
* @return true if the long press is handled, false otherwise. Subclasses should call the * @return true if the long press is handled, false otherwise. Subclasses should call the
* method on the base class if the subclass doesn't wish to handle the call. * method on the base class if the subclass doesn't wish to handle the call.
*/ */
protected fun onLongPress(popupKey: Keyboard.Key): Boolean { protected fun onLongPress(popupKey: MyKeyboard.Key): Boolean {
val popupKeyboardId = popupKey.popupResId val popupKeyboardId = popupKey.popupResId
if (popupKeyboardId != 0) { if (popupKeyboardId != 0) {
mMiniKeyboardContainer = mMiniKeyboardCache[popupKey] mMiniKeyboardContainer = mMiniKeyboardCache[popupKey]
@ -1009,10 +996,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} }
//mInputView.setSuggest(mSuggest); //mInputView.setSuggest(mSuggest);
val keyboard: Keyboard = if (popupKey.popupCharacters != null) { val keyboard = if (popupKey.popupCharacters != null) {
Keyboard(context, popupKeyboardId, popupKey.popupCharacters, -1, paddingLeft + paddingRight) MyKeyboard(context, popupKeyboardId, popupKey.popupCharacters!!, -1, paddingLeft + paddingRight)
} else { } else {
Keyboard(context, popupKeyboardId) MyKeyboard(context, popupKeyboardId)
} }
mMiniKeyboard!!.setKeyboard(keyboard) mMiniKeyboard!!.setKeyboard(keyboard)
@ -1282,11 +1269,9 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} }
private fun removeMessages() { private fun removeMessages() {
if (mHandler != null) { mHandler?.removeMessages(MSG_REPEAT)
mHandler!!.removeMessages(MSG_REPEAT) mHandler?.removeMessages(MSG_LONGPRESS)
mHandler!!.removeMessages(MSG_LONGPRESS) mHandler?.removeMessages(MSG_SHOW_PREVIEW)
mHandler!!.removeMessages(MSG_SHOW_PREVIEW)
}
} }
public override fun onDetachedFromWindow() { public override fun onDetachedFromWindow() {