simplify the codes attribute, we always use 1 int only

This commit is contained in:
tibbi 2022-01-21 23:17:10 +01:00
parent af9d017fed
commit 7b3b1ecee6
2 changed files with 27 additions and 52 deletions

View File

@ -205,18 +205,11 @@ class MyKeyboard {
width = getDimensionOrFraction(a, R.styleable.MyKeyboard_keyWidth, keyboard.mDisplayWidth, parent.defaultWidth) width = getDimensionOrFraction(a, R.styleable.MyKeyboard_keyWidth, keyboard.mDisplayWidth, parent.defaultWidth)
height = parent.defaultHeight height = parent.defaultHeight
gap = getDimensionOrFraction(a, R.styleable.MyKeyboard_horizontalGap, keyboard.mDisplayWidth, parent.defaultHorizontalGap) gap = getDimensionOrFraction(a, R.styleable.MyKeyboard_horizontalGap, keyboard.mDisplayWidth, parent.defaultHorizontalGap)
this.x += gap
a.recycle() a.recycle()
a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.MyKeyboard_Key) a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.MyKeyboard_Key)
this.x += gap codes = arrayListOf(a.getInt(R.styleable.MyKeyboard_Key_codes, 0))
val codesValue = TypedValue()
a.getValue(R.styleable.MyKeyboard_Key_codes, codesValue)
if (codesValue.type == TypedValue.TYPE_INT_DEC || codesValue.type == TypedValue.TYPE_INT_HEX) {
codes = arrayListOf(codesValue.data)
} else if (codesValue.type == TypedValue.TYPE_STRING) {
codes = parseCSV(codesValue.string.toString())
}
popupCharacters = a.getText(R.styleable.MyKeyboard_Key_popupCharacters) popupCharacters = a.getText(R.styleable.MyKeyboard_Key_popupCharacters)
popupResId = a.getResourceId(R.styleable.MyKeyboard_Key_popupKeyboard, 0) popupResId = a.getResourceId(R.styleable.MyKeyboard_Key_popupKeyboard, 0)
@ -249,28 +242,6 @@ class MyKeyboard {
pressed = false pressed = false
} }
fun parseCSV(value: String): ArrayList<Int> {
var count = 0
var lastIndex = 0
if (value.isNotEmpty()) {
count++
while (value.indexOf(",", lastIndex + 1).also { lastIndex = it } > 0) {
count++
}
}
val values = ArrayList<Int>(count)
count = 0
val st = StringTokenizer(value, ",")
while (st.hasMoreTokens()) {
try {
values[count++] = st.nextToken().toInt()
} catch (nfe: NumberFormatException) {
}
}
return values
}
/** /**
* Detects if a point falls inside this key. * Detects if a point falls inside this key.
* @param x the x-coordinate of the point * @param x the x-coordinate of the point
@ -463,12 +434,14 @@ class MyKeyboard {
while (parser.next().also { event = it } != XmlResourceParser.END_DOCUMENT) { while (parser.next().also { event = it } != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.START_TAG) { if (event == XmlResourceParser.START_TAG) {
val tag = parser.name val tag = parser.name
if (TAG_ROW == tag) { when {
TAG_ROW == tag -> {
inRow = true inRow = true
x = 0 x = 0
currentRow = createRowFromXml(res, parser) currentRow = createRowFromXml(res, parser)
mRows.add(currentRow) mRows.add(currentRow)
} else if (TAG_KEY == tag) { }
TAG_KEY == tag -> {
inKey = true inKey = true
key = createKeyFromXml(res, currentRow!!, x, y, parser) key = createKeyFromXml(res, currentRow!!, x, y, parser)
mKeys!!.add(key) mKeys!!.add(key)
@ -482,9 +455,11 @@ class MyKeyboard {
key.icon = context.resources.getDrawable(enterResourceId, context.theme) key.icon = context.resources.getDrawable(enterResourceId, context.theme)
} }
currentRow.mKeys.add(key) currentRow.mKeys.add(key)
} else if (TAG_KEYBOARD == tag) { }
TAG_KEYBOARD == tag -> {
parseKeyboardAttributes(res, parser) parseKeyboardAttributes(res, parser)
} }
}
} else if (event == XmlResourceParser.END_TAG) { } else if (event == XmlResourceParser.END_TAG) {
if (inKey) { if (inKey) {
inKey = false inKey = false

View File

@ -15,7 +15,7 @@
<declare-styleable name="MyKeyboard_Key"> <declare-styleable name="MyKeyboard_Key">
<!-- The unicode value or comma-separated values that this key outputs. --> <!-- The unicode value or comma-separated values that this key outputs. -->
<attr name="codes" format="integer|string" /> <attr name="codes" format="integer" />
<!-- The XML keyboard layout of any popup keyboard. --> <!-- The XML keyboard layout of any popup keyboard. -->
<attr name="popupKeyboard" format="reference" /> <attr name="popupKeyboard" format="reference" />
<!-- The characters to display in the popup keyboard. --> <!-- The characters to display in the popup keyboard. -->