Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/main/java/be/scri/helpers/DatabaseHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class DatabaseHelper(
getRequiredData(language),
)

fun getEmojiMaxKeywordLength(): Int = dbManagers.emojiManager.maxKeywordLength

fun findCaseAnnnotationForPreposition(language: String): HashMap<String, MutableList<String>> =
if (language != "DE" && language != "RU") {
hashMapOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@ import android.database.sqlite.SQLiteDatabase
class EmojiDataManager(
private val context: Context,
) {
// Track max keyword length.
var maxKeywordLength = 0

fun getEmojiKeywords(language: String): HashMap<String, MutableList<String>> {
val dbFile = context.getDatabasePath("${language}LanguageData.sqlite")
return processEmojiKeywords(dbFile.path)
}

private fun processEmojiKeywords(dbPath: String): HashMap<String, MutableList<String>> {
val hashMap = HashMap<String, MutableList<String>>()
val db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY)

db.use { database ->
database.rawQuery("SELECT * FROM emoji_keywords", null).use { cursor ->
SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY).use { db ->
// Get max keyword length.
db.rawQuery("SELECT MAX(LENGTH(word)) FROM emoji_keywords", null).use { cursor ->
if (cursor.moveToFirst()) {
maxKeywordLength = cursor.getInt(0)
}
}

// Keyword processing.
db.rawQuery("SELECT * FROM emoji_keywords", null).use { cursor ->
processEmojiCursor(cursor, hashMap)
}
}
Expand Down
44 changes: 43 additions & 1 deletion app/src/main/java/be/scri/services/GeneralKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ abstract class GeneralKeyboardIME(

private lateinit var dbHelper: DatabaseHelper
lateinit var emojiKeywords: HashMap<String, MutableList<String>>
var emojiMaxKeywordLength: Int = 0
lateinit var nounKeywords: HashMap<String, List<String>>
lateinit var pluralWords: List<String>
lateinit var caseAnnotation: HashMap<String, MutableList<String>>
Expand Down Expand Up @@ -295,6 +296,7 @@ abstract class GeneralKeyboardIME(
dbHelper = DatabaseHelper(this)
dbHelper.loadDatabase(languageAlias)
emojiKeywords = dbHelper.getEmojiKeywords(languageAlias)
emojiMaxKeywordLength = dbHelper.getEmojiMaxKeywordLength()
pluralWords = dbHelper.checkIfWordIsPlural(languageAlias)!!
nounKeywords = dbHelper.findGenderOfWord(languageAlias)

Expand Down Expand Up @@ -1229,12 +1231,52 @@ abstract class GeneralKeyboardIME(

/**
* Inserts the specified emoji into the current input field.
* Replaces the last word if there's no trailing space.
*
* @param emoji The emoji character to be inserted.
*/
private fun insertEmoji(emoji: String) {
val inputConnection = currentInputConnection ?: return
inputConnection.commitText(emoji, 1)
val maxLookBack = emojiMaxKeywordLength.coerceAtLeast(1)

inputConnection.beginBatchEdit()
try {
val previousText = inputConnection.getTextBeforeCursor(maxLookBack, 0)?.toString() ?: ""

// Find last word boundary efficiently
val lastSpaceIndex = previousText.lastIndexOf(' ')
val hasSpace = lastSpaceIndex != -1

when {
// Case 1: Ends with space or empty
previousText.isEmpty() || hasSpace && lastSpaceIndex == previousText.length - 1 -> {
inputConnection.commitText(emoji, 1)
}

// Case 2: Has previous word
hasSpace -> {
val lastWord = previousText.substring(lastSpaceIndex + 1)
if (emojiKeywords.containsKey(lastWord.lowercase())) {
inputConnection.deleteSurroundingText(lastWord.length, 0)
inputConnection.commitText(emoji, 1)
} else {
inputConnection.commitText(emoji, 1)
}
}

// Case 3: Entire text is the word
else -> {
if (emojiKeywords.containsKey(previousText.lowercase())) {
inputConnection.deleteSurroundingText(previousText.length, 0)
inputConnection.commitText(emoji, 1)
} else {
inputConnection.commitText(emoji, 1)
}
}
}
} finally {
inputConnection.endBatchEdit()
}
}

/**
Expand Down
Loading