From 0b516ac1d4a497b80b62ccfe0d6638b3fd0fd0fb Mon Sep 17 00:00:00 2001 From: Owm Date: Mon, 10 Aug 2026 22:56:50 +0530 Subject: [PATCH] Fixed-emoji-autosuggestion Signed-off-by: Owm --- .../java/be/scri/helpers/SuggestionHandler.kt | 1 + .../be/scri/services/EnglishKeyboardIME.kt | 10 ----- .../be/scri/services/FrenchKeyboardIME.kt | 10 ----- .../be/scri/services/GeneralKeyboardIME.kt | 44 ++++--------------- .../be/scri/services/GermanKeyboardIME.kt | 7 --- .../be/scri/services/ItalianKeyboardIME.kt | 7 --- .../be/scri/services/PortugueseKeyboardIME.kt | 7 --- .../be/scri/services/RussianKeyboardIME.kt | 7 --- .../be/scri/services/SpanishKeyboardIME.kt | 7 --- .../be/scri/services/SwedishKeyboardIME.kt | 7 --- .../be/scri/helpers/data/EmojiDataManager.kt | 17 +++++-- .../be/scri/helpers/data/SQLiteExtensions.kt | 4 +- 12 files changed, 24 insertions(+), 104 deletions(-) diff --git a/app/src/keyboards/java/be/scri/helpers/SuggestionHandler.kt b/app/src/keyboards/java/be/scri/helpers/SuggestionHandler.kt index 3b836ac59..1c6e4be91 100644 --- a/app/src/keyboards/java/be/scri/helpers/SuggestionHandler.kt +++ b/app/src/keyboards/java/be/scri/helpers/SuggestionHandler.kt @@ -160,6 +160,7 @@ class SuggestionHandler( ime.updateEmojiSuggestion(true, emojis) ime.updateButtonVisibility(true) } else { + ime.autoSuggestEmojis = null ime.updateButtonVisibility(false) } } diff --git a/app/src/keyboards/java/be/scri/services/EnglishKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/EnglishKeyboardIME.kt index 8978d7232..2d14a2116 100644 --- a/app/src/keyboards/java/be/scri/services/EnglishKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/EnglishKeyboardIME.kt @@ -5,7 +5,6 @@ package be.scri.services import android.text.InputType import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import be.scri.R -import be.scri.helpers.KeyHandler /** * The EnglishKeyboardIME class provides the input method for the English language keyboard. @@ -32,13 +31,4 @@ class EnglishKeyboardIME : GeneralKeyboardIME("English") { override var inputTypeClass: Int = InputType.TYPE_CLASS_TEXT override var enterKeyType: Int = IME_ACTION_NONE override var switchToLetters: Boolean = false - - private val keyHandler by lazy { KeyHandler(this) } - - /** - * Handles key input from the keyboard and delegates it to [KeyHandler]. - */ - override fun onKey(code: Int) { - keyHandler.handleKey(code, language) - } } diff --git a/app/src/keyboards/java/be/scri/services/FrenchKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/FrenchKeyboardIME.kt index e389ad15b..30c3383fb 100644 --- a/app/src/keyboards/java/be/scri/services/FrenchKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/FrenchKeyboardIME.kt @@ -5,7 +5,6 @@ package be.scri.services import android.text.InputType import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import be.scri.R -import be.scri.helpers.KeyHandler /** * The FrenchKeyboardIME class provides the input method for the French language keyboard. @@ -38,13 +37,4 @@ class FrenchKeyboardIME : GeneralKeyboardIME("French") { // so we must remove the overrides here. They are now inherited directly. // override lateinit var binding: KeyboardViewCommandOptionsBinding // REMOVED // override var keyboardView: KeyboardView? = null // REMOVED - - private val keyHandler by lazy { KeyHandler(this) } - - /** - * Handles key press events on the keyboard. - */ - override fun onKey(code: Int) { - keyHandler.handleKey(code, language) - } } diff --git a/app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt index 1b8b6afbe..762536831 100644 --- a/app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt @@ -46,6 +46,7 @@ import be.scri.helpers.AutocompletionHandler import be.scri.helpers.BackspaceHandler import be.scri.helpers.DatabaseManagers import be.scri.helpers.EmojiUtils.insertEmoji +import be.scri.helpers.KeyHandler import be.scri.helpers.KeyboardBase import be.scri.helpers.KeyboardLanguageMappingConstants import be.scri.helpers.LanguageMappingConstants.getLanguageAlias @@ -137,6 +138,7 @@ abstract class GeneralKeyboardIME( internal lateinit var suggestionHandler: SuggestionHandler internal lateinit var autocompletionHandler: AutocompletionHandler private lateinit var autocompletionManager: AutocompletionDataManager + internal lateinit var keyHandler: KeyHandler private var dataContract: DataContract? = null var emojiKeywords: HashMap>? = null @@ -220,6 +222,7 @@ abstract class GeneralKeyboardIME( suggestionHandler = SuggestionHandler(this) autocompletionManager = dbManagers.autocompletionManager autocompletionHandler = AutocompletionHandler(this) + keyHandler = KeyHandler(this) clipboardMonitor = ClipboardMonitor(this) { text -> latestClipText = text @@ -522,42 +525,7 @@ abstract class GeneralKeyboardIME( * Handles key input from the keyboard. Delegates to specific handlers based on the key code. */ override fun onKey(code: Int) { - val inputConnection = currentInputConnection - if (inputConnection != null) { - when (code) { - KeyboardBase.KEYCODE_EMOJI -> openEmojiKeyboard() - KeyboardBase.KEYCODE_DELETE -> handleDelete() - KeyboardBase.KEYCODE_SHIFT -> { - if (keyboardMode == keyboardLetters) { - val shiftState = keyboardView?.mKeyboard?.mShiftState ?: SHIFT_OFF - when { - shiftState == SHIFT_ON_PERMANENT -> keyboardView?.setShifted(SHIFT_OFF) - System.currentTimeMillis() - lastShiftPressTS < shiftPermToggleSpeed -> keyboardView?.setShifted(SHIFT_ON_PERMANENT) - shiftState == SHIFT_ON_ONE_CHAR -> keyboardView?.setShifted(SHIFT_OFF) - shiftState == SHIFT_OFF -> keyboardView?.setShifted(SHIFT_ON_ONE_CHAR) - } - lastShiftPressTS = System.currentTimeMillis() - } else { - handleModeChange(keyboardMode, keyboardView, this) - } - } - - KeyboardBase.KEYCODE_ENTER -> handleKeycodeEnter() - KeyboardBase.KEYCODE_MODE_CHANGE -> handleModeChange(keyboardMode, keyboardView, this) - KeyboardBase.KEYCODE_CLIPBOARD -> openClipboardPanel() - else -> { - if (KeyboardBase.SCRIBE_VIEW_KEYS.contains(code)) { - val keyLabel = keyboardView?.getKeyLabel(code) - if (!keyLabel.isNullOrEmpty()) { - commitText("$keyLabel ") - } - } else { - val commandBarState = currentState != ScribeState.IDLE && currentState != ScribeState.SELECT_COMMAND - handleElseCondition(code, keyboardMode, commandBarState) - } - } - } - } + keyHandler.handleKey(code, language) } // MARK: Helper Methods @@ -1841,6 +1809,10 @@ abstract class GeneralKeyboardIME( } if (completions.isNullOrEmpty()) { uiManager.disableAutoSuggest(language) + if (!autoSuggestEmojis.isNullOrEmpty() && emojiAutoSuggestionEnabled) { + updateEmojiSuggestion(true, autoSuggestEmojis) + updateButtonVisibility(true) + } return } diff --git a/app/src/keyboards/java/be/scri/services/GermanKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/GermanKeyboardIME.kt index 618e79f1c..3ae4737ce 100644 --- a/app/src/keyboards/java/be/scri/services/GermanKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/GermanKeyboardIME.kt @@ -5,7 +5,6 @@ package be.scri.services import android.text.InputType import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import be.scri.R -import be.scri.helpers.KeyHandler import be.scri.helpers.PreferencesHelper.getIsAccentCharacterDisabled /** @@ -52,10 +51,4 @@ class GermanKeyboardIME : GeneralKeyboardIME("German") { // so we must remove the overrides here. They are now inherited directly. // override lateinit var binding: KeyboardViewCommandOptionsBinding // REMOVED // override var keyboardView: KeyboardView? = null // REMOVED - - private val keyHandler by lazy { KeyHandler(this) } - - override fun onKey(code: Int) { - keyHandler.handleKey(code, language) - } } diff --git a/app/src/keyboards/java/be/scri/services/ItalianKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/ItalianKeyboardIME.kt index 26826de08..c6b15e294 100644 --- a/app/src/keyboards/java/be/scri/services/ItalianKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/ItalianKeyboardIME.kt @@ -5,7 +5,6 @@ package be.scri.services import android.text.InputType import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import be.scri.R -import be.scri.helpers.KeyHandler /** * The ItalianKeyboardIME class provides the input method for the Italian language keyboard. @@ -32,10 +31,4 @@ class ItalianKeyboardIME : GeneralKeyboardIME("Italian") { override var inputTypeClass: Int = InputType.TYPE_CLASS_TEXT override var enterKeyType: Int = IME_ACTION_NONE override var switchToLetters: Boolean = false - - private val keyHandler by lazy { KeyHandler(this) } - - override fun onKey(code: Int) { - keyHandler.handleKey(code, language) - } } diff --git a/app/src/keyboards/java/be/scri/services/PortugueseKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/PortugueseKeyboardIME.kt index 05c5c7f0b..49d367646 100644 --- a/app/src/keyboards/java/be/scri/services/PortugueseKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/PortugueseKeyboardIME.kt @@ -5,7 +5,6 @@ package be.scri.services import android.text.InputType import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import be.scri.R -import be.scri.helpers.KeyHandler /** * The PortugueseKeyboardIME class provides the input method for the Portuguese language keyboard. @@ -33,10 +32,4 @@ class PortugueseKeyboardIME : GeneralKeyboardIME("Portuguese") { override var enterKeyType: Int = IME_ACTION_NONE override var switchToLetters: Boolean = false override var hasTextBeforeCursor: Boolean = false - - private val keyHandler by lazy { KeyHandler(this) } - - override fun onKey(code: Int) { - keyHandler.handleKey(code, language) - } } diff --git a/app/src/keyboards/java/be/scri/services/RussianKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/RussianKeyboardIME.kt index afb59dbfb..142602d01 100644 --- a/app/src/keyboards/java/be/scri/services/RussianKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/RussianKeyboardIME.kt @@ -5,7 +5,6 @@ package be.scri.services import android.text.InputType import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import be.scri.R -import be.scri.helpers.KeyHandler /** * The RussianKeyboardIME class provides the input method for the Russian language keyboard. @@ -32,10 +31,4 @@ class RussianKeyboardIME : GeneralKeyboardIME("Russian") { override var inputTypeClass: Int = InputType.TYPE_CLASS_TEXT override var enterKeyType: Int = IME_ACTION_NONE override var switchToLetters: Boolean = false - - private val keyHandler by lazy { KeyHandler(this) } - - override fun onKey(code: Int) { - keyHandler.handleKey(code, language) - } } diff --git a/app/src/keyboards/java/be/scri/services/SpanishKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/SpanishKeyboardIME.kt index 605844d65..76029ed63 100644 --- a/app/src/keyboards/java/be/scri/services/SpanishKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/SpanishKeyboardIME.kt @@ -5,7 +5,6 @@ package be.scri.services import android.text.InputType import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import be.scri.R -import be.scri.helpers.KeyHandler import be.scri.helpers.PreferencesHelper.getIsAccentCharacterDisabled /** @@ -43,10 +42,4 @@ class SpanishKeyboardIME : GeneralKeyboardIME("Spanish") { override var enterKeyType: Int = IME_ACTION_NONE override var switchToLetters: Boolean = false override var hasTextBeforeCursor: Boolean = false - - private val keyHandler by lazy { KeyHandler(this) } - - override fun onKey(code: Int) { - keyHandler.handleKey(code, language) - } } diff --git a/app/src/keyboards/java/be/scri/services/SwedishKeyboardIME.kt b/app/src/keyboards/java/be/scri/services/SwedishKeyboardIME.kt index f423aae6e..0ef2c640a 100644 --- a/app/src/keyboards/java/be/scri/services/SwedishKeyboardIME.kt +++ b/app/src/keyboards/java/be/scri/services/SwedishKeyboardIME.kt @@ -5,7 +5,6 @@ package be.scri.services import android.text.InputType import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import be.scri.R -import be.scri.helpers.KeyHandler import be.scri.helpers.PreferencesHelper.getIsAccentCharacterDisabled /** @@ -42,10 +41,4 @@ class SwedishKeyboardIME : GeneralKeyboardIME("Swedish") { override var inputTypeClass: Int = InputType.TYPE_CLASS_TEXT override var enterKeyType: Int = IME_ACTION_NONE override var switchToLetters: Boolean = false - - private val keyHandler by lazy { KeyHandler(this) } - - override fun onKey(code: Int) { - keyHandler.handleKey(code, language) - } } diff --git a/app/src/main/java/be/scri/helpers/data/EmojiDataManager.kt b/app/src/main/java/be/scri/helpers/data/EmojiDataManager.kt index f36c9701a..7a40714f9 100644 --- a/app/src/main/java/be/scri/helpers/data/EmojiDataManager.kt +++ b/app/src/main/java/be/scri/helpers/data/EmojiDataManager.kt @@ -29,14 +29,22 @@ class EmojiDataManager( val db = fileManager.getLanguageDatabase(language) ?: return emojiMap db.use { - if (!it.tableExists("emoji_keywords")) return emojiMap + // The server contract names the table "emojikeywords", but older local + // databases may use the snake_case form, so accept both. + val tableName = + when { + it.tableExists("emoji_keywords") -> "emoji_keywords" + it.tableExists("emojikeywords") -> "emojikeywords" + else -> return emojiMap + } - it.rawQuery("SELECT MAX(LENGTH(word)) FROM emoji_keywords", null).use { cursor -> + it.rawQuery("SELECT MAX(LENGTH(word)) FROM $tableName", null).use { cursor -> if (cursor.moveToFirst()) { maxKeywordLength = cursor.getInt(0) } } - it.rawQuery("SELECT * FROM emoji_keywords", null).use { cursor -> + + it.rawQuery("SELECT * FROM $tableName", null).use { cursor -> processEmojiCursor(cursor, emojiMap) } } @@ -61,7 +69,8 @@ class EmojiDataManager( .mapNotNull { name -> cursor.getColumnIndex(name).takeIf { it != -1 } } do { - val word = cursor.getString(wordIndex) + // Keys are lowercased so lookups via the user's (lowercased) input match. + val word = cursor.getString(wordIndex)?.lowercase() ?: continue val emojis = emojiIndices .mapNotNull { index -> cursor.getString(index)?.takeIf { it.isNotBlank() } } diff --git a/app/src/main/java/be/scri/helpers/data/SQLiteExtensions.kt b/app/src/main/java/be/scri/helpers/data/SQLiteExtensions.kt index 4d129bc29..587c97edd 100644 --- a/app/src/main/java/be/scri/helpers/data/SQLiteExtensions.kt +++ b/app/src/main/java/be/scri/helpers/data/SQLiteExtensions.kt @@ -6,8 +6,8 @@ import android.database.sqlite.SQLiteDatabase fun SQLiteDatabase.tableExists(tableName: String): Boolean = rawQuery( - "SELECT name FROM sqlite_master WHERE type='table' AND name='$tableName'", - null, + "SELECT name FROM sqlite_master WHERE type='table' AND LOWER(name) = LOWER(?)", + arrayOf(tableName), ).use { it.moveToFirst() } fun SQLiteDatabase.columnExists(