Sentences capitalization full refactoring and google-keyboard-like implementation

This commit is contained in:
merkost 2023-05-31 23:29:58 +10:00
parent 474512955c
commit 19dc038372
4 changed files with 22 additions and 147 deletions

View File

@ -1,5 +1,12 @@
package com.simplemobiletools.keyboard.helpers package com.simplemobiletools.keyboard.helpers
enum class ShiftState {
OFF,
ON_ONE_CHAR,
ON_PERMANENT;
}
// limit the count of alternative characters that show up at long pressing a key // limit the count of alternative characters that show up at long pressing a key
const val MAX_KEYS_PER_MINI_ROW = 9 const val MAX_KEYS_PER_MINI_ROW = 9

View File

@ -242,12 +242,7 @@ class MyKeyboard {
fun isInside(x: Int, y: Int): Boolean { fun isInside(x: Int, y: Int): Boolean {
val leftEdge = edgeFlags and EDGE_LEFT > 0 val leftEdge = edgeFlags and EDGE_LEFT > 0
val rightEdge = edgeFlags and EDGE_RIGHT > 0 val rightEdge = edgeFlags and EDGE_RIGHT > 0
return ( return ((x >= this.x || leftEdge && x <= this.x + width) && (x < this.x + width || rightEdge && x >= this.x) && (y >= this.y && y <= this.y + height) && (y < this.y + height && y >= this.y))
(x >= this.x || leftEdge && x <= this.x + width) &&
(x < this.x + width || rightEdge && x >= this.x) &&
(y >= this.y && y <= this.y + height) &&
(y < this.y + height && y >= this.y)
)
} }
} }
@ -258,14 +253,13 @@ class MyKeyboard {
* @param enterKeyType determines what icon should we show on Enter key * @param enterKeyType determines what icon should we show on Enter key
*/ */
@JvmOverloads @JvmOverloads
constructor(context: Context, @XmlRes xmlLayoutResId: Int, enterKeyType: Int, shiftState: ShiftState = ShiftState.OFF) { constructor(context: Context, @XmlRes xmlLayoutResId: Int, enterKeyType: Int) {
mDisplayWidth = context.resources.displayMetrics.widthPixels mDisplayWidth = context.resources.displayMetrics.widthPixels
mDefaultHorizontalGap = 0 mDefaultHorizontalGap = 0
mDefaultWidth = mDisplayWidth / 10 mDefaultWidth = mDisplayWidth / 10
mDefaultHeight = mDefaultWidth mDefaultHeight = mDefaultWidth
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier) mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier)
mKeys = ArrayList() mKeys = ArrayList()
mShiftState = shiftState
mEnterKeyType = enterKeyType mEnterKeyType = enterKeyType
loadKeyboard(context, context.resources.getXml(xmlLayoutResId)) loadKeyboard(context, context.resources.getXml(xmlLayoutResId))
} }
@ -278,8 +272,7 @@ class MyKeyboard {
* @param characters the list of characters to display on the keyboard. One key will be created for each character. * @param characters the list of characters to display on the keyboard. One key will be created for each character.
* @param keyWidth the width of the popup key, make sure it is the same as the key itself * @param keyWidth the width of the popup key, make sure it is the same as the key itself
*/ */
constructor(context: Context, layoutTemplateResId: Int, characters: CharSequence, keyWidth: Int) : constructor(context: Context, layoutTemplateResId: Int, characters: CharSequence, keyWidth: Int) : this(context, layoutTemplateResId, 0) {
this(context, layoutTemplateResId, 0) {
var x = 0 var x = 0
var y = 0 var y = 0
var column = 0 var column = 0

View File

@ -1,82 +0,0 @@
package com.simplemobiletools.keyboard.helpers
import android.content.Context
import android.text.InputType
import com.simplemobiletools.keyboard.extensions.config
enum class ShiftState {
OFF,
ON_ONE_CHAR,
ON_PERMANENT;
companion object {
private val endOfSentenceChars: List<Char> = listOf('.', '?', '!')
private const val MIN_TEXT_LENGTH = 2
/**
* Input Type exceptions for capitalizing.
*/
private val inputTypeExceptions = listOf(
InputType.TYPE_TEXT_VARIATION_PASSWORD,
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,
InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD,
InputType.TYPE_NUMBER_VARIATION_PASSWORD,
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS,
InputType.TYPE_TEXT_VARIATION_URI
)
fun getDefaultShiftState(context: Context, inputTypeClassVariation: Int): ShiftState {
if (isInputTypeAllowedCapitalizing(inputTypeClassVariation)) {
return OFF
}
return when (context.config.enableSentencesCapitalization) {
true -> ON_ONE_CHAR
else -> OFF
}
}
fun getShiftStateForText(context: Context, inputTypeClassVariation: Int, text: String?): ShiftState {
if (isInputTypeAllowedCapitalizing(inputTypeClassVariation)) {
return OFF
}
return when {
shouldCapitalize(context, text) -> {
ON_ONE_CHAR
}
else -> {
OFF
}
}
}
/**
* The function is checking whether there is a need in capitalizing based on the given text
* @param context Used for checking current sentences capitalization setting
* @param text Last text from the input
*/
fun shouldCapitalize(context: Context, text: String?): Boolean {
// check whether it is the first letter in textField
if (text.isNullOrEmpty()) {
return true
}
if (!context.config.enableSentencesCapitalization) {
return false
}
val twoLastSymbols = text.takeLast(2)
if (twoLastSymbols.length < MIN_TEXT_LENGTH) {
return false
}
return endOfSentenceChars.contains(twoLastSymbols.first()) && twoLastSymbols.last().code == MyKeyboard.KEYCODE_SPACE
}
fun isInputTypeAllowedCapitalizing(inputTypeVariation: Int): Boolean {
return inputTypeVariation in inputTypeExceptions
}
}
}

View File

@ -72,52 +72,20 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
} }
private fun updateShiftKeyState(code: Int?) { private fun updateShiftKeyState(code: Int?) {
if (keyboardMode != KEYBOARD_LETTERS || ShiftState.isInputTypeAllowedCapitalizing(inputTypeClassVariation)) { if (code == MyKeyboard.KEYCODE_SHIFT) {
return return
} }
if (code == MyKeyboard.KEYCODE_SHIFT || code == MyKeyboard.KEYCODE_DELETE) { val editorInfo = currentInputEditorInfo
return if (config.enableSentencesCapitalization && editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL) {
} if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
keyboard?.setShifted(ShiftState.ON_ONE_CHAR)
val text = currentInputConnection.getTextBeforeCursor(2, 0) ?: return
// capitalize first letter on startup or if text is empty
if (code == null || text.isEmpty()) {
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
keyboardView?.invalidateAllKeys()
return
}
// capitalize sentences if needed
if (config.enableSentencesCapitalization) {
// capitalize on Enter click
if (code == MyKeyboard.KEYCODE_ENTER) {
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
keyboardView?.invalidateAllKeys() keyboardView?.invalidateAllKeys()
return return
} }
if (ShiftState.shouldCapitalize(this, text.toString())) {
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
keyboardView?.invalidateAllKeys()
return
} else {
// try capitalizing based on the editor info like google keep or google messenger apps
val editorInfo = currentInputEditorInfo
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL) {
if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
keyboard?.setShifted(ShiftState.ON_ONE_CHAR)
keyboardView?.invalidateAllKeys()
return
}
}
}
} }
// In other cases reset shift to OFF // in other cases reset shift to OFF
keyboard?.setShifted(ShiftState.OFF) keyboard?.setShifted(ShiftState.OFF)
keyboardView?.invalidateAllKeys() keyboardView?.invalidateAllKeys()
} }
@ -128,8 +96,6 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
return return
} }
// this.keyboardView.setEditorInfo(EditorInfo)
if (code != MyKeyboard.KEYCODE_SHIFT) { if (code != MyKeyboard.KEYCODE_SHIFT) {
lastShiftPressTS = 0 lastShiftPressTS = 0
} }
@ -139,18 +105,10 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
val selectedText = inputConnection.getSelectedText(0) val selectedText = inputConnection.getSelectedText(0)
if (TextUtils.isEmpty(selectedText)) { if (TextUtils.isEmpty(selectedText)) {
val text = inputConnection.getTextBeforeCursor(3, 0)?.dropLast(1)
if (keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
keyboard?.setShifted(ShiftState.getShiftStateForText(this, inputTypeClassVariation, text?.toString()))
keyboardView?.invalidateAllKeys()
}
inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)) inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL))
} else { } else {
inputConnection.commitText("", 1) inputConnection.commitText("", 1)
} }
} }
@ -216,7 +174,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
// If the keyboard is set to symbols and the user presses space, we usually should switch back to the letters keyboard. // If the keyboard is set to symbols and the user presses space, we usually should switch back to the letters keyboard.
// However, avoid doing that in cases when the EditText for example requires numbers as the input. // However, avoid doing that in cases when the EditText for example requires numbers as the input.
// We can detect that by the text not changing on pressing Space. // We can detect that by the text not changing on pressing Space.
if (keyboardMode != KEYBOARD_LETTERS && code == MyKeyboard.KEYCODE_SPACE) { if (keyboardMode != KEYBOARD_LETTERS && inputTypeClass == InputType.TYPE_CLASS_TEXT && code == MyKeyboard.KEYCODE_SPACE) {
inputConnection.commitText(codeChar.toString(), 1) inputConnection.commitText(codeChar.toString(), 1)
val newText = inputConnection.getExtractedText(ExtractedTextRequest(), 0)?.text val newText = inputConnection.getExtractedText(ExtractedTextRequest(), 0)?.text
if (originalText != newText) { if (originalText != newText) {
@ -227,6 +185,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
} }
} }
} }
if (keyboard!!.mShiftState != ShiftState.ON_PERMANENT) { if (keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
updateShiftKeyState(code) updateShiftKeyState(code)
} }
@ -237,11 +196,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
// TODO: Change keyboardMode to enum class // TODO: Change keyboardMode to enum class
keyboardMode = KEYBOARD_LETTERS keyboardMode = KEYBOARD_LETTERS
//Check if capitalization is needed after switching to letters layout keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType)
val text = currentInputConnection?.getTextBeforeCursor(2, 0)
val newShiftState = ShiftState.getShiftStateForText(this, inputTypeClassVariation, text?.toString())
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState)
val editorInfo = currentInputEditorInfo val editorInfo = currentInputEditorInfo
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != ShiftState.ON_PERMANENT) { if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
@ -296,7 +251,9 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
} }
} }
return MyKeyboard( return MyKeyboard(
context = this, xmlLayoutResId = keyboardXml, enterKeyType = enterKeyType, shiftState = ShiftState.getDefaultShiftState(this, inputTypeClassVariation) context = this,
xmlLayoutResId = keyboardXml,
enterKeyType = enterKeyType,
) )
} }