Skip to content

Commit 19dc038

Browse files
committed
Sentences capitalization full refactoring and google-keyboard-like implementation
1 parent 4745129 commit 19dc038

File tree

4 files changed

+22
-147
lines changed

4 files changed

+22
-147
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 & 82 deletions
This file was deleted.

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

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -72,52 +72,20 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
7272
}
7373

7474
private fun updateShiftKeyState(code: Int?) {
75-
if (keyboardMode != KEYBOARD_LETTERS || ShiftState.isInputTypeAllowedCapitalizing(inputTypeClassVariation)) {
75+
if (code == MyKeyboard.KEYCODE_SHIFT) {
7676
return
7777
}
7878

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)
79+
val editorInfo = currentInputEditorInfo
80+
if (config.enableSentencesCapitalization && editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL) {
81+
if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
82+
keyboard?.setShifted(ShiftState.ON_ONE_CHAR)
9783
keyboardView?.invalidateAllKeys()
9884
return
9985
}
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) {
111-
if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
112-
keyboard?.setShifted(ShiftState.ON_ONE_CHAR)
113-
keyboardView?.invalidateAllKeys()
114-
return
115-
}
116-
}
117-
}
11886
}
11987

120-
// In other cases reset shift to OFF
88+
// in other cases reset shift to OFF
12189
keyboard?.setShifted(ShiftState.OFF)
12290
keyboardView?.invalidateAllKeys()
12391
}
@@ -128,8 +96,6 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
12896
return
12997
}
13098

131-
// this.keyboardView.setEditorInfo(EditorInfo)
132-
13399
if (code != MyKeyboard.KEYCODE_SHIFT) {
134100
lastShiftPressTS = 0
135101
}
@@ -139,18 +105,10 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
139105

140106
val selectedText = inputConnection.getSelectedText(0)
141107
if (TextUtils.isEmpty(selectedText)) {
142-
val text = inputConnection.getTextBeforeCursor(3, 0)?.dropLast(1)
143-
144-
if (keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
145-
keyboard?.setShifted(ShiftState.getShiftStateForText(this, inputTypeClassVariation, text?.toString()))
146-
keyboardView?.invalidateAllKeys()
147-
}
148-
149108
inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
150109
inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL))
151110
} else {
152111

153-
154112
inputConnection.commitText("", 1)
155113
}
156114
}
@@ -216,7 +174,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
216174
// If the keyboard is set to symbols and the user presses space, we usually should switch back to the letters keyboard.
217175
// However, avoid doing that in cases when the EditText for example requires numbers as the input.
218176
// We can detect that by the text not changing on pressing Space.
219-
if (keyboardMode != KEYBOARD_LETTERS && code == MyKeyboard.KEYCODE_SPACE) {
177+
if (keyboardMode != KEYBOARD_LETTERS && inputTypeClass == InputType.TYPE_CLASS_TEXT && code == MyKeyboard.KEYCODE_SPACE) {
220178
inputConnection.commitText(codeChar.toString(), 1)
221179
val newText = inputConnection.getExtractedText(ExtractedTextRequest(), 0)?.text
222180
if (originalText != newText) {
@@ -227,6 +185,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
227185
}
228186
}
229187
}
188+
230189
if (keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
231190
updateShiftKeyState(code)
232191
}
@@ -237,11 +196,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
237196
// TODO: Change keyboardMode to enum class
238197
keyboardMode = KEYBOARD_LETTERS
239198

240-
//Check if capitalization is needed after switching to letters layout
241-
val text = currentInputConnection?.getTextBeforeCursor(2, 0)
242-
val newShiftState = ShiftState.getShiftStateForText(this, inputTypeClassVariation, text?.toString())
243-
244-
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState)
199+
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType)
245200

246201
val editorInfo = currentInputEditorInfo
247202
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.mShiftState != ShiftState.ON_PERMANENT) {
@@ -296,7 +251,9 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
296251
}
297252
}
298253
return MyKeyboard(
299-
context = this, xmlLayoutResId = keyboardXml, enterKeyType = enterKeyType, shiftState = ShiftState.getDefaultShiftState(this, inputTypeClassVariation)
254+
context = this,
255+
xmlLayoutResId = keyboardXml,
256+
enterKeyType = enterKeyType,
300257
)
301258
}
302259

0 commit comments

Comments
 (0)