@@ -20,7 +20,8 @@ import com.simplemobiletools.keyboard.R
2020import com.simplemobiletools.keyboard.extensions.config
2121import com.simplemobiletools.keyboard.helpers.*
2222import com.simplemobiletools.keyboard.views.MyKeyboardView
23- import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.*
23+ import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.keyboard_holder
24+ import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.keyboard_view
2425
2526// based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
2627class SimpleKeyboardIME : InputMethodService (), MyKeyboardView.OnKeyboardActionListener, SharedPreferences.OnSharedPreferenceChangeListener {
@@ -41,7 +42,6 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
4142
4243 override fun onInitializeInterface () {
4344 super .onInitializeInterface()
44- keyboard = MyKeyboard (this , getKeyboardLayoutXML(), enterKeyType)
4545 getSharedPrefs().registerOnSharedPreferenceChangeListener(this )
4646 }
4747
@@ -65,8 +65,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
6565 super .onStartInput(attribute, restarting)
6666 inputTypeClass = attribute!! .inputType and TYPE_MASK_CLASS
6767 enterKeyType = attribute.imeOptions and (IME_MASK_ACTION or IME_FLAG_NO_ENTER_ACTION )
68-
69- keyboard = getKeyBoard()
68+ keyboard = createNewKeyboard()
7069 keyboardView?.setKeyboard(keyboard!! )
7170 keyboardView?.setEditorInfo(attribute)
7271 updateShiftKeyState()
@@ -75,9 +74,9 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
7574 private fun updateShiftKeyState () {
7675 if (keyboardMode == KEYBOARD_LETTERS ) {
7776 val editorInfo = currentInputEditorInfo
78- if (editorInfo != null && editorInfo.inputType != InputType .TYPE_NULL && keyboard?.mShiftState != SHIFT_ON_PERMANENT ) {
77+ if (editorInfo != null && editorInfo.inputType != InputType .TYPE_NULL && keyboard?.mShiftState != ShiftState . ON_PERMANENT ) {
7978 if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0 ) {
80- keyboard?.setShifted(SHIFT_ON_ONE_CHAR )
79+ keyboard?.setShifted(ShiftState . ON_ONE_CHAR )
8180 keyboardView?.invalidateAllKeys()
8281 }
8382 }
@@ -96,8 +95,10 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
9695
9796 when (code) {
9897 MyKeyboard .KEYCODE_DELETE -> {
99- if (keyboard!! .mShiftState == SHIFT_ON_ONE_CHAR ) {
100- keyboard!! .mShiftState = SHIFT_OFF
98+
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))
101102 }
102103
103104 val selectedText = inputConnection.getSelectedText(0 )
@@ -109,13 +110,14 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
109110 }
110111 keyboardView!! .invalidateAllKeys()
111112 }
113+
112114 MyKeyboard .KEYCODE_SHIFT -> {
113115 if (keyboardMode == KEYBOARD_LETTERS ) {
114116 when {
115- keyboard!! .mShiftState == SHIFT_ON_PERMANENT -> keyboard!! .mShiftState = SHIFT_OFF
116- System .currentTimeMillis() - lastShiftPressTS < SHIFT_PERM_TOGGLE_SPEED -> keyboard!! .mShiftState = SHIFT_ON_PERMANENT
117- keyboard!! .mShiftState == SHIFT_ON_ONE_CHAR -> keyboard!! .mShiftState = SHIFT_OFF
118- keyboard!! .mShiftState == SHIFT_OFF -> keyboard!! .mShiftState = SHIFT_ON_ONE_CHAR
117+ keyboard!! .mShiftState == ShiftState . ON_PERMANENT -> keyboard!! .mShiftState = ShiftState . OFF
118+ System .currentTimeMillis() - lastShiftPressTS < SHIFT_PERM_TOGGLE_SPEED -> keyboard!! .mShiftState = ShiftState . ON_PERMANENT
119+ keyboard!! .mShiftState == ShiftState . ON_ONE_CHAR -> keyboard!! .mShiftState = ShiftState . OFF
120+ keyboard!! .mShiftState == ShiftState . OFF -> keyboard!! .mShiftState = ShiftState . ON_ONE_CHAR
119121 }
120122
121123 lastShiftPressTS = System .currentTimeMillis()
@@ -132,15 +134,22 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
132134 }
133135 keyboardView!! .invalidateAllKeys()
134136 }
137+
135138 MyKeyboard .KEYCODE_ENTER -> {
136139 val imeOptionsActionId = getImeOptionsActionId()
137140 if (imeOptionsActionId != IME_ACTION_NONE ) {
138141 inputConnection.performEditorAction(imeOptionsActionId)
139142 } else {
140143 inputConnection.sendKeyEvent(KeyEvent (KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_ENTER ))
141144 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+ }
142150 }
143151 }
152+
144153 MyKeyboard .KEYCODE_MODE_CHANGE -> {
145154 val keyboardXml = if (keyboardMode == KEYBOARD_LETTERS ) {
146155 keyboardMode = KEYBOARD_SYMBOLS
@@ -152,48 +161,58 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
152161 keyboard = MyKeyboard (this , keyboardXml, enterKeyType)
153162 keyboardView!! .setKeyboard(keyboard!! )
154163 }
164+
155165 MyKeyboard .KEYCODE_EMOJI -> {
156166 keyboardView?.openEmojiPalette()
157167 }
168+
158169 else -> {
159170 var codeChar = code.toChar()
160- if (Character .isLetter(codeChar) && keyboard!! .mShiftState > SHIFT_OFF ) {
171+ val originalText = inputConnection.getExtractedText(ExtractedTextRequest (), 0 )?.text ? : return
172+
173+ if (Character .isLetter(codeChar) && keyboard!! .mShiftState > ShiftState .OFF ) {
161174 codeChar = Character .toUpperCase(codeChar)
162175 }
163176
164177 // If the keyboard is set to symbols and the user presses space, we usually should switch back to the letters keyboard.
165178 // However, avoid doing that in cases when the EditText for example requires numbers as the input.
166179 // We can detect that by the text not changing on pressing Space.
167180 if (keyboardMode != KEYBOARD_LETTERS && code == MyKeyboard .KEYCODE_SPACE ) {
168- val originalText = inputConnection.getExtractedText(ExtractedTextRequest (), 0 )?.text ? : return
169181 inputConnection.commitText(codeChar.toString(), 1 )
170182 val newText = inputConnection.getExtractedText(ExtractedTextRequest (), 0 )?.text
171- switchToLetters = originalText != newText
183+ if (originalText != newText) {
184+ switchToLetters = true
185+ }
172186 } else {
173187 inputConnection.commitText(codeChar.toString(), 1 )
174188 }
175189
176- if (keyboard !! .mShiftState == SHIFT_ON_ONE_CHAR && keyboardMode == KEYBOARD_LETTERS ) {
177- keyboard!! .mShiftState = SHIFT_OFF
190+ if (keyboardMode == KEYBOARD_LETTERS && keyboard !! .mShiftState != ShiftState . ON_PERMANENT ) {
191+ keyboard!! .setShifted( ShiftState .getShiftStateForText( this , newText = " $originalText$codeChar " ))
178192 keyboardView!! .invalidateAllKeys()
179193 }
194+
180195 }
181196 }
182197
183- if (code != MyKeyboard .KEYCODE_SHIFT ) {
198+ if (code != MyKeyboard .KEYCODE_SHIFT && config.enableSentencesCapitalization ) {
184199 updateShiftKeyState()
185200 }
186201 }
187202
188203 override fun onActionUp () {
189204 if (switchToLetters) {
205+ // TODO: Change keyboardMode to enum class
190206 keyboardMode = KEYBOARD_LETTERS
191- keyboard = MyKeyboard (this , getKeyboardLayoutXML(), enterKeyType)
207+ val text = currentInputConnection?.getExtractedText(ExtractedTextRequest (), 0 )?.text
208+ val newShiftState = ShiftState .getShiftStateForText(this , text?.toString().orEmpty())
209+
210+ keyboard = MyKeyboard (this , getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState)
192211
193212 val editorInfo = currentInputEditorInfo
194- if (editorInfo != null && editorInfo.inputType != InputType .TYPE_NULL && keyboard?.mShiftState != SHIFT_ON_PERMANENT ) {
213+ if (editorInfo != null && editorInfo.inputType != InputType .TYPE_NULL && keyboard?.mShiftState != ShiftState . ON_PERMANENT ) {
195214 if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0 ) {
196- keyboard?.setShifted(SHIFT_ON_ONE_CHAR )
215+ keyboard?.setShifted(ShiftState . ON_ONE_CHAR )
197216 }
198217 }
199218
@@ -215,32 +234,37 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
215234 }
216235
217236 override fun reloadKeyboard () {
218- val keyboard = getKeyBoard ()
237+ val keyboard = createNewKeyboard ()
219238 this .keyboard = keyboard
220239 keyboardView?.setKeyboard(keyboard)
221240 }
222241
223- private fun getKeyBoard (): MyKeyboard {
242+ private fun createNewKeyboard (): MyKeyboard {
224243 val keyboardXml = when (inputTypeClass) {
225244 TYPE_CLASS_NUMBER -> {
226245 keyboardMode = KEYBOARD_NUMBERS
227246 R .xml.keys_numbers
228247 }
248+
229249 TYPE_CLASS_PHONE -> {
230250 keyboardMode = KEYBOARD_PHONE
231251 R .xml.keys_phone
232252 }
253+
233254 TYPE_CLASS_DATETIME -> {
234255 keyboardMode = KEYBOARD_SYMBOLS
235256 R .xml.keys_symbols
236257 }
258+
237259 else -> {
238260 keyboardMode = KEYBOARD_LETTERS
239261 getKeyboardLayoutXML()
240262 }
241263 }
242264
243- return MyKeyboard (this , keyboardXml, enterKeyType)
265+ return MyKeyboard (
266+ context = this , xmlLayoutResId = keyboardXml, enterKeyType = enterKeyType, shiftState = ShiftState .getDefaultShiftState(this )
267+ )
244268 }
245269
246270 override fun onUpdateSelection (oldSelStart : Int , oldSelEnd : Int , newSelStart : Int , newSelEnd : Int , candidatesStart : Int , candidatesEnd : Int ) {
0 commit comments