Skip to content

Commit 21a2209

Browse files
committed
Merge branch 'capitalizing_settings' into direct_boot_aware
# Conflicts: # app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/ShiftState.kt # app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt
2 parents 999c19c + 073ea38 commit 21a2209

File tree

4 files changed

+26
-158
lines changed

4 files changed

+26
-158
lines changed

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

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

3+
4+
enum class ShiftState {
5+
OFF,
6+
ON_ONE_CHAR,
7+
ON_PERMANENT;
8+
}
9+
310
// limit the count of alternative characters that show up at long pressing a key
411
const val MAX_KEYS_PER_MINI_ROW = 9
512

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,7 @@ class MyKeyboard {
242242
fun isInside(x: Int, y: Int): Boolean {
243243
val leftEdge = edgeFlags and EDGE_LEFT > 0
244244
val rightEdge = edgeFlags and EDGE_RIGHT > 0
245-
return (
246-
(x >= this.x || leftEdge && x <= this.x + width) &&
247-
(x < this.x + width || rightEdge && x >= this.x) &&
248-
(y >= this.y && y <= this.y + height) &&
249-
(y < this.y + height && y >= this.y)
250-
)
245+
return ((x >= this.x || leftEdge && x <= this.x + width) && (x < this.x + width || rightEdge && x >= this.x) && (y >= this.y && y <= this.y + height) && (y < this.y + height && y >= this.y))
251246
}
252247
}
253248

@@ -258,14 +253,13 @@ class MyKeyboard {
258253
* @param enterKeyType determines what icon should we show on Enter key
259254
*/
260255
@JvmOverloads
261-
constructor(context: Context, @XmlRes xmlLayoutResId: Int, enterKeyType: Int, shiftState: ShiftState = ShiftState.OFF) {
256+
constructor(context: Context, @XmlRes xmlLayoutResId: Int, enterKeyType: Int) {
262257
mDisplayWidth = context.resources.displayMetrics.widthPixels
263258
mDefaultHorizontalGap = 0
264259
mDefaultWidth = mDisplayWidth / 10
265260
mDefaultHeight = mDefaultWidth
266261
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier)
267262
mKeys = ArrayList()
268-
mShiftState = shiftState
269263
mEnterKeyType = enterKeyType
270264
loadKeyboard(context, context.resources.getXml(xmlLayoutResId))
271265
}
@@ -278,8 +272,7 @@ class MyKeyboard {
278272
* @param characters the list of characters to display on the keyboard. One key will be created for each character.
279273
* @param keyWidth the width of the popup key, make sure it is the same as the key itself
280274
*/
281-
constructor(context: Context, layoutTemplateResId: Int, characters: CharSequence, keyWidth: Int) :
282-
this(context, layoutTemplateResId, 0) {
275+
constructor(context: Context, layoutTemplateResId: Int, characters: CharSequence, keyWidth: Int) : this(context, layoutTemplateResId, 0) {
283276
var x = 0
284277
var y = 0
285278
var column = 0

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

Lines changed: 0 additions & 81 deletions
This file was deleted.

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

Lines changed: 16 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.simplemobiletools.keyboard.services
22

33
import android.content.SharedPreferences
44
import android.inputmethodservice.InputMethodService
5-
import android.text.InputType
65
import android.text.InputType.*
76
import android.text.TextUtils
87
import android.view.KeyEvent
@@ -18,8 +17,7 @@ import com.simplemobiletools.keyboard.extensions.config
1817
import com.simplemobiletools.keyboard.extensions.safeStorageContext
1918
import com.simplemobiletools.keyboard.helpers.*
2019
import 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/
2523
class 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

Comments
 (0)