From 474512955cc283d34f22d24b475fbed101951201 Mon Sep 17 00:00:00 2001 From: merkost Date: Thu, 25 May 2023 13:20:04 +1000 Subject: [PATCH] Refactored ShiftState and added URI exception --- .../keyboard/helpers/Constants.kt | 77 ----------------- .../keyboard/helpers/ShiftState.kt | 82 +++++++++++++++++++ .../keyboard/services/SimpleKeyboardIME.kt | 4 +- 3 files changed, 84 insertions(+), 79 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/ShiftState.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt index efe786b..383b120 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt @@ -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 = 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 diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/ShiftState.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/ShiftState.kt new file mode 100644 index 0000000..8b29258 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/ShiftState.kt @@ -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 = 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 + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt index 2a92e90..8a08441 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt @@ -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 }