Skip to content

Commit 4745129

Browse files
committed
Refactored ShiftState and added URI exception
1 parent b97fa7b commit 4745129

File tree

3 files changed

+84
-79
lines changed

3 files changed

+84
-79
lines changed

app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,5 @@
11
package com.simplemobiletools.keyboard.helpers
22

3-
import android.content.Context
4-
import android.text.InputType
5-
import com.simplemobiletools.keyboard.extensions.config
6-
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SPACE
7-
8-
enum class ShiftState {
9-
OFF,
10-
ON_ONE_CHAR,
11-
ON_PERMANENT;
12-
13-
companion object {
14-
private const val MIN_TEXT_LENGTH = 2
15-
private val inputTypeExceptions = listOf(
16-
InputType.TYPE_TEXT_VARIATION_PASSWORD,
17-
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,
18-
InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD,
19-
InputType.TYPE_NUMBER_VARIATION_PASSWORD,
20-
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
21-
InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS
22-
)
23-
private val endOfSentenceChars: List<Char> = listOf('.', '?', '!')
24-
25-
fun getDefaultShiftState(context: Context, inputTypeClassVariation: Int): ShiftState {
26-
if (isInputTypePasswordOrEmail(inputTypeClassVariation)) {
27-
return OFF
28-
}
29-
return when (context.config.enableSentencesCapitalization) {
30-
true -> ON_ONE_CHAR
31-
else -> OFF
32-
}
33-
}
34-
35-
fun getShiftStateForText(context: Context, inputTypeClassVariation: Int, text: String?): ShiftState {
36-
if (isInputTypePasswordOrEmail(inputTypeClassVariation)) {
37-
return OFF
38-
}
39-
return when {
40-
shouldCapitalize(context, text) -> {
41-
ON_ONE_CHAR
42-
}
43-
44-
else -> {
45-
OFF
46-
}
47-
}
48-
}
49-
50-
/**
51-
* The function is checking whether there is a need in capitalizing based on the given text
52-
* @param context Used for checking current sentences capitalization setting
53-
* @param text Last text from the input
54-
*/
55-
fun shouldCapitalize(context: Context, text: String?): Boolean {
56-
// check whether it is the first letter in textField
57-
if (text.isNullOrEmpty()) {
58-
return true
59-
}
60-
61-
if (!context.config.enableSentencesCapitalization) {
62-
return false
63-
}
64-
65-
val twoLastSymbols = text.takeLast(2)
66-
67-
if (twoLastSymbols.length < MIN_TEXT_LENGTH) {
68-
return false
69-
}
70-
71-
return endOfSentenceChars.contains(twoLastSymbols.first()) && twoLastSymbols.last().code == KEYCODE_SPACE
72-
}
73-
74-
fun isInputTypePasswordOrEmail(inputTypeVariation: Int): Boolean {
75-
return inputTypeVariation in inputTypeExceptions
76-
}
77-
}
78-
}
79-
803
// limit the count of alternative characters that show up at long pressing a key
814
const val MAX_KEYS_PER_MINI_ROW = 9
825

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.simplemobiletools.keyboard.helpers
2+
3+
import android.content.Context
4+
import android.text.InputType
5+
import com.simplemobiletools.keyboard.extensions.config
6+
7+
enum class ShiftState {
8+
OFF,
9+
ON_ONE_CHAR,
10+
ON_PERMANENT;
11+
12+
companion object {
13+
private val endOfSentenceChars: List<Char> = listOf('.', '?', '!')
14+
private const val MIN_TEXT_LENGTH = 2
15+
16+
/**
17+
* Input Type exceptions for capitalizing.
18+
*/
19+
private val inputTypeExceptions = listOf(
20+
InputType.TYPE_TEXT_VARIATION_PASSWORD,
21+
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,
22+
InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD,
23+
InputType.TYPE_NUMBER_VARIATION_PASSWORD,
24+
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
25+
InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS,
26+
InputType.TYPE_TEXT_VARIATION_URI
27+
)
28+
29+
fun getDefaultShiftState(context: Context, inputTypeClassVariation: Int): ShiftState {
30+
if (isInputTypeAllowedCapitalizing(inputTypeClassVariation)) {
31+
return OFF
32+
}
33+
return when (context.config.enableSentencesCapitalization) {
34+
true -> ON_ONE_CHAR
35+
else -> OFF
36+
}
37+
}
38+
39+
fun getShiftStateForText(context: Context, inputTypeClassVariation: Int, text: String?): ShiftState {
40+
if (isInputTypeAllowedCapitalizing(inputTypeClassVariation)) {
41+
return OFF
42+
}
43+
return when {
44+
shouldCapitalize(context, text) -> {
45+
ON_ONE_CHAR
46+
}
47+
48+
else -> {
49+
OFF
50+
}
51+
}
52+
}
53+
54+
/**
55+
* The function is checking whether there is a need in capitalizing based on the given text
56+
* @param context Used for checking current sentences capitalization setting
57+
* @param text Last text from the input
58+
*/
59+
fun shouldCapitalize(context: Context, text: String?): Boolean {
60+
// check whether it is the first letter in textField
61+
if (text.isNullOrEmpty()) {
62+
return true
63+
}
64+
65+
if (!context.config.enableSentencesCapitalization) {
66+
return false
67+
}
68+
69+
val twoLastSymbols = text.takeLast(2)
70+
71+
if (twoLastSymbols.length < MIN_TEXT_LENGTH) {
72+
return false
73+
}
74+
75+
return endOfSentenceChars.contains(twoLastSymbols.first()) && twoLastSymbols.last().code == MyKeyboard.KEYCODE_SPACE
76+
}
77+
78+
fun isInputTypeAllowedCapitalizing(inputTypeVariation: Int): Boolean {
79+
return inputTypeVariation in inputTypeExceptions
80+
}
81+
}
82+
}

app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
7272
}
7373

7474
private fun updateShiftKeyState(code: Int?) {
75-
if (code == MyKeyboard.KEYCODE_SHIFT || code == MyKeyboard.KEYCODE_DELETE) {
75+
if (keyboardMode != KEYBOARD_LETTERS || ShiftState.isInputTypeAllowedCapitalizing(inputTypeClassVariation)) {
7676
return
7777
}
7878

79-
if (keyboardMode != KEYBOARD_LETTERS || ShiftState.isInputTypePasswordOrEmail(inputTypeClassVariation)) {
79+
if (code == MyKeyboard.KEYCODE_SHIFT || code == MyKeyboard.KEYCODE_DELETE) {
8080
return
8181
}
8282

0 commit comments

Comments
 (0)