Refactored ShiftState and added URI exception

This commit is contained in:
merkost 2023-05-25 13:20:04 +10:00
parent b97fa7b0e8
commit 474512955c
3 changed files with 84 additions and 79 deletions

View File

@ -1,82 +1,5 @@
package com.simplemobiletools.keyboard.helpers
import android.content.Context
import android.text.InputType
import com.simplemobiletools.keyboard.extensions.config
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SPACE
enum class ShiftState {
OFF,
ON_ONE_CHAR,
ON_PERMANENT;
companion object {
private const val MIN_TEXT_LENGTH = 2
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
)
private val endOfSentenceChars: List<Char> = listOf('.', '?', '!')
fun getDefaultShiftState(context: Context, inputTypeClassVariation: Int): ShiftState {
if (isInputTypePasswordOrEmail(inputTypeClassVariation)) {
return OFF
}
return when (context.config.enableSentencesCapitalization) {
true -> ON_ONE_CHAR
else -> OFF
}
}
fun getShiftStateForText(context: Context, inputTypeClassVariation: Int, text: String?): ShiftState {
if (isInputTypePasswordOrEmail(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 == KEYCODE_SPACE
}
fun isInputTypePasswordOrEmail(inputTypeVariation: Int): Boolean {
return inputTypeVariation in inputTypeExceptions
}
}
}
// limit the count of alternative characters that show up at long pressing a key
const val MAX_KEYS_PER_MINI_ROW = 9

View File

@ -0,0 +1,82 @@
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,11 +72,11 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
}
private fun updateShiftKeyState(code: Int?) {
if (code == MyKeyboard.KEYCODE_SHIFT || code == MyKeyboard.KEYCODE_DELETE) {
if (keyboardMode != KEYBOARD_LETTERS || ShiftState.isInputTypeAllowedCapitalizing(inputTypeClassVariation)) {
return
}
if (keyboardMode != KEYBOARD_LETTERS || ShiftState.isInputTypePasswordOrEmail(inputTypeClassVariation)) {
if (code == MyKeyboard.KEYCODE_SHIFT || code == MyKeyboard.KEYCODE_DELETE) {
return
}