Sentences capitalization full refactoring and google-keyboard-like implementation
This commit is contained in:
parent
474512955c
commit
19dc038372
|
@ -1,5 +1,12 @@
|
|||
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
|
||||
const val MAX_KEYS_PER_MINI_ROW = 9
|
||||
|
||||
|
|
|
@ -242,12 +242,7 @@ class MyKeyboard {
|
|||
fun isInside(x: Int, y: Int): Boolean {
|
||||
val leftEdge = edgeFlags and EDGE_LEFT > 0
|
||||
val rightEdge = edgeFlags and EDGE_RIGHT > 0
|
||||
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)
|
||||
)
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -258,14 +253,13 @@ class MyKeyboard {
|
|||
* @param enterKeyType determines what icon should we show on Enter key
|
||||
*/
|
||||
@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
|
||||
mDefaultHorizontalGap = 0
|
||||
mDefaultWidth = mDisplayWidth / 10
|
||||
mDefaultHeight = mDefaultWidth
|
||||
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier)
|
||||
mKeys = ArrayList()
|
||||
mShiftState = shiftState
|
||||
mEnterKeyType = enterKeyType
|
||||
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 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) :
|
||||
this(context, layoutTemplateResId, 0) {
|
||||
constructor(context: Context, layoutTemplateResId: Int, characters: CharSequence, keyWidth: Int) : this(context, layoutTemplateResId, 0) {
|
||||
var x = 0
|
||||
var y = 0
|
||||
var column = 0
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -72,52 +72,20 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
|
|||
}
|
||||
|
||||
private fun updateShiftKeyState(code: Int?) {
|
||||
if (keyboardMode != KEYBOARD_LETTERS || ShiftState.isInputTypeAllowedCapitalizing(inputTypeClassVariation)) {
|
||||
if (code == MyKeyboard.KEYCODE_SHIFT) {
|
||||
return
|
||||
}
|
||||
|
||||
if (code == MyKeyboard.KEYCODE_SHIFT || code == MyKeyboard.KEYCODE_DELETE) {
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
val editorInfo = currentInputEditorInfo
|
||||
if (config.enableSentencesCapitalization && editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL) {
|
||||
if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
|
||||
keyboard?.setShifted(ShiftState.ON_ONE_CHAR)
|
||||
keyboardView?.invalidateAllKeys()
|
||||
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)
|
||||
keyboardView?.invalidateAllKeys()
|
||||
}
|
||||
|
@ -128,8 +96,6 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
|
|||
return
|
||||
}
|
||||
|
||||
// this.keyboardView.setEditorInfo(EditorInfo)
|
||||
|
||||
if (code != MyKeyboard.KEYCODE_SHIFT) {
|
||||
lastShiftPressTS = 0
|
||||
}
|
||||
|
@ -139,18 +105,10 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
|
|||
|
||||
val selectedText = inputConnection.getSelectedText(0)
|
||||
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_UP, KeyEvent.KEYCODE_DEL))
|
||||
} else {
|
||||
|
||||
|
||||
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.
|
||||
// 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.
|
||||
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)
|
||||
val newText = inputConnection.getExtractedText(ExtractedTextRequest(), 0)?.text
|
||||
if (originalText != newText) {
|
||||
|
@ -227,6 +185,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
|
||||
updateShiftKeyState(code)
|
||||
}
|
||||
|
@ -237,11 +196,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
|
|||
// TODO: Change keyboardMode to enum class
|
||||
keyboardMode = KEYBOARD_LETTERS
|
||||
|
||||
//Check if capitalization is needed after switching to letters layout
|
||||
val text = currentInputConnection?.getTextBeforeCursor(2, 0)
|
||||
val newShiftState = ShiftState.getShiftStateForText(this, inputTypeClassVariation, text?.toString())
|
||||
|
||||
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState)
|
||||
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType)
|
||||
|
||||
val editorInfo = currentInputEditorInfo
|
||||
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
|
||||
|
@ -296,7 +251,9 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
|
|||
}
|
||||
}
|
||||
return MyKeyboard(
|
||||
context = this, xmlLayoutResId = keyboardXml, enterKeyType = enterKeyType, shiftState = ShiftState.getDefaultShiftState(this, inputTypeClassVariation)
|
||||
context = this,
|
||||
xmlLayoutResId = keyboardXml,
|
||||
enterKeyType = enterKeyType,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue