@@ -2,7 +2,6 @@ package com.simplemobiletools.keyboard.services
22
33import android.content.SharedPreferences
44import android.inputmethodservice.InputMethodService
5- import android.text.InputType
65import android.text.InputType.*
76import android.text.TextUtils
87import android.view.KeyEvent
@@ -18,8 +17,7 @@ import com.simplemobiletools.keyboard.extensions.config
1817import com.simplemobiletools.keyboard.extensions.safeStorageContext
1918import com.simplemobiletools.keyboard.helpers.*
2019import com.simplemobiletools.keyboard.views.MyKeyboardView
21- import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.keyboard_holder
22- import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.keyboard_view
20+ import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.*
2321
2422// based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
2523class SimpleKeyboardIME : InputMethodService (), MyKeyboardView.OnKeyboardActionListener, SharedPreferences.OnSharedPreferenceChangeListener {
@@ -34,8 +32,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
3432 private var keyboardView: MyKeyboardView ? = null
3533 private var lastShiftPressTS = 0L
3634 private var keyboardMode = KEYBOARD_LETTERS
37- private var inputTypeClass = InputType . TYPE_CLASS_TEXT
38- private var inputTypeClassVariation = InputType . TYPE_CLASS_TEXT
35+ private var inputTypeClass = TYPE_CLASS_TEXT
36+ private var inputTypeClassVariation = TYPE_CLASS_TEXT
3937 private var enterKeyType = IME_ACTION_NONE
4038 private var switchToLetters = false
4139
@@ -63,7 +61,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
6361 override fun onStartInput (attribute : EditorInfo ? , restarting : Boolean ) {
6462 super .onStartInput(attribute, restarting)
6563 inputTypeClass = attribute!! .inputType and TYPE_MASK_CLASS
66- inputTypeClassVariation = attribute!! .inputType and TYPE_MASK_VARIATION
64+ inputTypeClassVariation = attribute.inputType and TYPE_MASK_VARIATION
6765 enterKeyType = attribute.imeOptions and (IME_MASK_ACTION or IME_FLAG_NO_ENTER_ACTION )
6866 keyboard = createNewKeyboard()
6967 keyboardView?.setKeyboard(keyboard!! )
@@ -72,47 +70,17 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
7270 }
7371
7472 private fun updateShiftKeyState (code : Int? ) {
75- if (keyboardMode != KEYBOARD_LETTERS || ShiftState .isInputTypeAllowedCapitalizing(inputTypeClassVariation) ) {
73+ if (code == MyKeyboard . KEYCODE_SHIFT ) {
7674 return
7775 }
7876
79- if (code == MyKeyboard .KEYCODE_SHIFT || code == MyKeyboard .KEYCODE_DELETE ) {
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 )
77+ val editorInfo = currentInputEditorInfo
78+ if (config.enableSentencesCapitalization && editorInfo != null && editorInfo.inputType != TYPE_NULL ) {
79+ if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0 ) {
80+ keyboard?.setShifted(ShiftState .ON_ONE_CHAR )
9781 keyboardView?.invalidateAllKeys()
9882 return
9983 }
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- if (currentInputEditorInfo != null && currentInputEditorInfo.inputType != InputType .TYPE_NULL ) {
109- if (currentInputConnection.getCursorCapsMode(currentInputEditorInfo.inputType) != 0 ) {
110- keyboard?.setShifted(ShiftState .ON_ONE_CHAR )
111- keyboardView?.invalidateAllKeys()
112- return
113- }
114- }
115- }
11684 }
11785
11886 keyboard?.setShifted(ShiftState .OFF )
@@ -131,25 +99,14 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
13199
132100 when (code) {
133101 MyKeyboard .KEYCODE_DELETE -> {
134-
135102 val selectedText = inputConnection.getSelectedText(0 )
136103 if (TextUtils .isEmpty(selectedText)) {
137- val text = inputConnection.getTextBeforeCursor(3 , 0 )?.dropLast(1 )
138-
139- if (keyboard?.mShiftState != ShiftState .ON_PERMANENT ) {
140- keyboard?.setShifted(ShiftState .getShiftStateForText(this , inputTypeClassVariation, text?.toString()))
141- keyboardView?.invalidateAllKeys()
142- }
143-
144104 inputConnection.sendKeyEvent(KeyEvent (KeyEvent .ACTION_DOWN , KeyEvent .KEYCODE_DEL ))
145105 inputConnection.sendKeyEvent(KeyEvent (KeyEvent .ACTION_UP , KeyEvent .KEYCODE_DEL ))
146106 } else {
147-
148-
149107 inputConnection.commitText(" " , 1 )
150108 }
151109 }
152-
153110 MyKeyboard .KEYCODE_SHIFT -> {
154111 if (keyboardMode == KEYBOARD_LETTERS ) {
155112 when {
@@ -173,7 +130,6 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
173130 }
174131 keyboardView!! .invalidateAllKeys()
175132 }
176-
177133 MyKeyboard .KEYCODE_ENTER -> {
178134 val imeOptionsActionId = getImeOptionsActionId()
179135 if (imeOptionsActionId != IME_ACTION_NONE ) {
@@ -183,7 +139,6 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
183139 inputConnection.sendKeyEvent(KeyEvent (KeyEvent .ACTION_UP , KeyEvent .KEYCODE_ENTER ))
184140 }
185141 }
186-
187142 MyKeyboard .KEYCODE_MODE_CHANGE -> {
188143 val keyboardXml = if (keyboardMode == KEYBOARD_LETTERS ) {
189144 keyboardMode = KEYBOARD_SYMBOLS
@@ -195,11 +150,9 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
195150 keyboard = MyKeyboard (this , keyboardXml, enterKeyType)
196151 keyboardView!! .setKeyboard(keyboard!! )
197152 }
198-
199153 MyKeyboard .KEYCODE_EMOJI -> {
200154 keyboardView?.openEmojiPalette()
201155 }
202-
203156 else -> {
204157 var codeChar = code.toChar()
205158 val originalText = inputConnection.getExtractedText(ExtractedTextRequest (), 0 )?.text ? : return
@@ -211,7 +164,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
211164 // If the keyboard is set to symbols and the user presses space, we usually should switch back to the letters keyboard.
212165 // However, avoid doing that in cases when the EditText for example requires numbers as the input.
213166 // We can detect that by the text not changing on pressing Space.
214- if (keyboardMode != KEYBOARD_LETTERS && code == MyKeyboard .KEYCODE_SPACE ) {
167+ if (keyboardMode != KEYBOARD_LETTERS && inputTypeClass == TYPE_CLASS_TEXT && code == MyKeyboard .KEYCODE_SPACE ) {
215168 inputConnection.commitText(codeChar.toString(), 1 )
216169 val newText = inputConnection.getExtractedText(ExtractedTextRequest (), 0 )?.text
217170 if (originalText != newText) {
@@ -222,6 +175,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
222175 }
223176 }
224177 }
178+
225179 if (keyboard!! .mShiftState != ShiftState .ON_PERMANENT ) {
226180 updateShiftKeyState(code)
227181 }
@@ -232,14 +186,10 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
232186 // TODO: Change keyboardMode to enum class
233187 keyboardMode = KEYBOARD_LETTERS
234188
235- // Check if capitalization is needed after switching to letters layout
236- val text = currentInputConnection?.getTextBeforeCursor(2 , 0 )
237- val newShiftState = ShiftState .getShiftStateForText(this , inputTypeClassVariation, text?.toString())
238-
239- keyboard = MyKeyboard (this , getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState)
189+ keyboard = MyKeyboard (this , getKeyboardLayoutXML(), enterKeyType)
240190
241191 val editorInfo = currentInputEditorInfo
242- if (editorInfo != null && editorInfo.inputType != InputType . TYPE_NULL && keyboard?.mShiftState != ShiftState .ON_PERMANENT ) {
192+ if (editorInfo != null && editorInfo.inputType != TYPE_NULL && keyboard?.mShiftState != ShiftState .ON_PERMANENT ) {
243193 if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0 ) {
244194 keyboard?.setShifted(ShiftState .ON_ONE_CHAR )
245195 }
@@ -274,24 +224,23 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
274224 keyboardMode = KEYBOARD_NUMBERS
275225 R .xml.keys_numbers
276226 }
277-
278227 TYPE_CLASS_PHONE -> {
279228 keyboardMode = KEYBOARD_PHONE
280229 R .xml.keys_phone
281230 }
282-
283231 TYPE_CLASS_DATETIME -> {
284232 keyboardMode = KEYBOARD_SYMBOLS
285233 R .xml.keys_symbols
286234 }
287-
288235 else -> {
289236 keyboardMode = KEYBOARD_LETTERS
290237 getKeyboardLayoutXML()
291238 }
292239 }
293240 return MyKeyboard (
294- context = this , xmlLayoutResId = keyboardXml, enterKeyType = enterKeyType, shiftState = ShiftState .getDefaultShiftState(this , inputTypeClassVariation)
241+ context = this ,
242+ xmlLayoutResId = keyboardXml,
243+ enterKeyType = enterKeyType,
295244 )
296245 }
297246
0 commit comments