Skip to content

Commit aef91d8

Browse files
committed
Moved capitalization logic in a separate function
Added inputTypeClassVariation for controlling whether keyboard is in password mode to turn off capitalization
1 parent de6e704 commit aef91d8

File tree

2 files changed

+93
-53
lines changed

2 files changed

+93
-53
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.simplemobiletools.keyboard.helpers
22

33
import android.content.Context
4+
import android.text.InputType
45
import com.simplemobiletools.keyboard.extensions.config
56
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SPACE
67

@@ -10,23 +11,25 @@ enum class ShiftState {
1011
ON_PERMANENT;
1112

1213
companion object {
14+
private val MIN_TEXT_LENGTH = 2
1315
private val endOfSentenceChars: List<Char> = listOf('.', '?', '!')
1416

15-
fun getDefaultShiftState(context: Context): ShiftState {
17+
fun getDefaultShiftState(context: Context, inputTypeClassVariation: Int): ShiftState {
18+
if (isInputTypePassword(inputTypeClassVariation)) {
19+
return OFF
20+
}
1621
return when (context.config.enableSentencesCapitalization) {
1722
true -> ON_ONE_CHAR
1823
else -> OFF
1924
}
2025
}
2126

22-
fun getShiftStateForText(context: Context, newText: String?): ShiftState {
23-
if (!context.config.enableSentencesCapitalization) {
27+
fun getShiftStateForText(context: Context, inputTypeClassVariation: Int, text: String?): ShiftState {
28+
if (isInputTypePassword(inputTypeClassVariation)) {
2429
return OFF
2530
}
26-
27-
val twoLastSymbols = newText?.takeLast(2)
2831
return when {
29-
shouldCapitalizeSentence(previousChar = twoLastSymbols?.getOrNull(0), currentChar = twoLastSymbols?.getOrNull(1)) -> {
32+
shouldCapitalize(context, text) -> {
3033
ON_ONE_CHAR
3134
}
3235
else -> {
@@ -35,24 +38,30 @@ enum class ShiftState {
3538
}
3639
}
3740

38-
fun getCapitalizationOnDelete(context: Context, text: CharSequence?): ShiftState {
39-
if (!context.config.enableSentencesCapitalization) {
40-
return OFF
41+
fun shouldCapitalize(context: Context, text: String?): Boolean {
42+
//To capitalize first letter in textField
43+
if (text.isNullOrEmpty()) {
44+
return true
4145
}
4246

43-
return if (text.isNullOrEmpty() || shouldCapitalizeSentence(currentChar = text.last(), previousChar = text.getOrNull(text.lastIndex - 1))) {
44-
ON_ONE_CHAR
45-
} else {
46-
OFF
47+
if (!context.config.enableSentencesCapitalization) {
48+
return false
4749
}
48-
}
4950

50-
private fun shouldCapitalizeSentence(previousChar: Char?, currentChar: Char?): Boolean {
51-
if (previousChar == null || currentChar == null) {
51+
val twoLastSymbols = text.takeLast(2)
52+
53+
if (twoLastSymbols.length < MIN_TEXT_LENGTH) {
5254
return false
5355
}
5456

55-
return currentChar.code == KEYCODE_SPACE && endOfSentenceChars.contains(previousChar)
57+
return endOfSentenceChars.contains(twoLastSymbols.first()) && twoLastSymbols.last().code == KEYCODE_SPACE
58+
}
59+
60+
fun isInputTypePassword(inputTypeVariation: Int): Boolean {
61+
return inputTypeVariation == InputType.TYPE_TEXT_VARIATION_PASSWORD
62+
|| inputTypeVariation == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
63+
|| inputTypeVariation == InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD
64+
|| inputTypeVariation == InputType.TYPE_NUMBER_VARIATION_PASSWORD
5665
}
5766
}
5867
}

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

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package com.simplemobiletools.keyboard.services
33
import android.content.SharedPreferences
44
import android.inputmethodservice.InputMethodService
55
import android.text.InputType
6-
import android.text.InputType.TYPE_CLASS_DATETIME
7-
import android.text.InputType.TYPE_CLASS_NUMBER
8-
import android.text.InputType.TYPE_CLASS_PHONE
9-
import android.text.InputType.TYPE_MASK_CLASS
6+
import android.text.InputType.*
107
import android.text.TextUtils
118
import android.view.KeyEvent
129
import android.view.View
@@ -37,6 +34,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
3734
private var lastShiftPressTS = 0L
3835
private var keyboardMode = KEYBOARD_LETTERS
3936
private var inputTypeClass = InputType.TYPE_CLASS_TEXT
37+
private var inputTypeClassVariation = InputType.TYPE_CLASS_TEXT
4038
private var enterKeyType = IME_ACTION_NONE
4139
private var switchToLetters = false
4240

@@ -64,23 +62,64 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
6462
override fun onStartInput(attribute: EditorInfo?, restarting: Boolean) {
6563
super.onStartInput(attribute, restarting)
6664
inputTypeClass = attribute!!.inputType and TYPE_MASK_CLASS
65+
inputTypeClassVariation = attribute!!.inputType and TYPE_MASK_VARIATION
66+
6767
enterKeyType = attribute.imeOptions and (IME_MASK_ACTION or IME_FLAG_NO_ENTER_ACTION)
6868
keyboard = createNewKeyboard()
6969
keyboardView?.setKeyboard(keyboard!!)
7070
keyboardView?.setEditorInfo(attribute)
71-
updateShiftKeyState()
71+
updateShiftKeyState(null)
7272
}
7373

74-
private fun updateShiftKeyState() {
75-
if (keyboardMode == KEYBOARD_LETTERS) {
76-
val editorInfo = currentInputEditorInfo
77-
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
78-
if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
79-
keyboard?.setShifted(ShiftState.ON_ONE_CHAR)
80-
keyboardView?.invalidateAllKeys()
74+
private fun updateShiftKeyState(code: Int?) {
75+
if (code == MyKeyboard.KEYCODE_SHIFT || code == MyKeyboard.KEYCODE_DELETE) {
76+
return
77+
}
78+
79+
if (keyboardMode != KEYBOARD_LETTERS || ShiftState.isInputTypePassword(inputTypeClassVariation)) {
80+
return
81+
}
82+
83+
val text = currentInputConnection.getTextBeforeCursor(2, 0) ?: return
84+
//Capitalize first letter on startup or if text is empty
85+
if (code == null || text.isEmpty()) {
86+
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
87+
keyboardView?.invalidateAllKeys()
88+
return
89+
}
90+
91+
//Capitalize sentences if needed
92+
if (config.enableSentencesCapitalization) {
93+
94+
//Capitalize on Enter click
95+
if (code == MyKeyboard.KEYCODE_ENTER) {
96+
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
97+
keyboardView?.invalidateAllKeys()
98+
return
99+
}
100+
101+
102+
if (ShiftState.shouldCapitalize(this, text.toString())) {
103+
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
104+
keyboardView?.invalidateAllKeys()
105+
return
106+
} else {
107+
//Try capitalizing based on the editor info like google keep or google messenger apps
108+
val editorInfo = currentInputEditorInfo
109+
110+
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
111+
if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
112+
keyboard?.setShifted(ShiftState.ON_ONE_CHAR)
113+
keyboardView?.invalidateAllKeys()
114+
return
115+
}
81116
}
82117
}
83118
}
119+
120+
//Else just reset shift to OFF
121+
keyboard?.setShifted(ShiftState.OFF)
122+
keyboardView?.invalidateAllKeys()
84123
}
85124

86125
override fun onKey(code: Int) {
@@ -96,19 +135,22 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
96135
when (code) {
97136
MyKeyboard.KEYCODE_DELETE -> {
98137

99-
if (keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
100-
val extractedText = inputConnection.getTextBeforeCursor(3, 0)?.dropLast(1)
101-
keyboard!!.setShifted(ShiftState.getCapitalizationOnDelete(context = this, text = extractedText))
102-
}
103-
104138
val selectedText = inputConnection.getSelectedText(0)
105139
if (TextUtils.isEmpty(selectedText)) {
140+
val text = inputConnection.getTextBeforeCursor(3, 0)?.dropLast(1)
141+
142+
if (keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
143+
keyboard?.setShifted(ShiftState.getShiftStateForText(this, inputTypeClassVariation, text?.toString()))
144+
keyboardView?.invalidateAllKeys()
145+
}
146+
106147
inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
107148
inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL))
108149
} else {
150+
151+
109152
inputConnection.commitText("", 1)
110153
}
111-
keyboardView!!.invalidateAllKeys()
112154
}
113155

114156
MyKeyboard.KEYCODE_SHIFT -> {
@@ -142,11 +184,6 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
142184
} else {
143185
inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER))
144186
inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER))
145-
146-
if (config.enableSentencesCapitalization) {
147-
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
148-
keyboardView!!.invalidateAllKeys()
149-
}
150187
}
151188
}
152189

@@ -186,26 +223,21 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
186223
} else {
187224
inputConnection.commitText(codeChar.toString(), 1)
188225
}
189-
190-
if (keyboardMode == KEYBOARD_LETTERS && keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
191-
keyboard!!.setShifted(ShiftState.getShiftStateForText(this, newText = "$originalText$codeChar"))
192-
keyboardView!!.invalidateAllKeys()
193-
}
194-
195226
}
196227
}
197-
198-
if (code != MyKeyboard.KEYCODE_SHIFT && config.enableSentencesCapitalization) {
199-
updateShiftKeyState()
228+
if (keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
229+
updateShiftKeyState(code)
200230
}
201231
}
202232

203233
override fun onActionUp() {
204234
if (switchToLetters) {
205235
// TODO: Change keyboardMode to enum class
206236
keyboardMode = KEYBOARD_LETTERS
207-
val text = currentInputConnection?.getExtractedText(ExtractedTextRequest(), 0)?.text
208-
val newShiftState = ShiftState.getShiftStateForText(this, text?.toString().orEmpty())
237+
238+
//Check if capitalization is needed after switching to letters layout
239+
val text = currentInputConnection?.getTextBeforeCursor(2, 0)
240+
val newShiftState = ShiftState.getShiftStateForText(this, inputTypeClassVariation, text?.toString())
209241

210242
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState)
211243

@@ -261,9 +293,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
261293
getKeyboardLayoutXML()
262294
}
263295
}
264-
265296
return MyKeyboard(
266-
context = this, xmlLayoutResId = keyboardXml, enterKeyType = enterKeyType, shiftState = ShiftState.getDefaultShiftState(this)
297+
context = this, xmlLayoutResId = keyboardXml, enterKeyType = enterKeyType, shiftState = ShiftState.getDefaultShiftState(this, inputTypeClassVariation)
267298
)
268299
}
269300

0 commit comments

Comments
 (0)