@@ -3,10 +3,7 @@ package com.simplemobiletools.keyboard.services
33import android.content.SharedPreferences
44import android.inputmethodservice.InputMethodService
55import 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.*
107import android.text.TextUtils
118import android.view.KeyEvent
129import 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