Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add emoji autocompletions #220

Merged
merged 7 commits into from
Oct 26, 2024
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
64 changes: 64 additions & 0 deletions app/src/main/java/be/scri/helpers/DatabaseHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package be.scri.helpers

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream

class DatabaseHelper(
private val context: Context,
) : SQLiteOpenHelper(context, null, null, DATABASE_VERSION) {
companion object {
private const val DATABASE_VERSION = 1
}

override fun onCreate(db: SQLiteDatabase) {
}

override fun onUpgrade(
db: SQLiteDatabase,
oldVersion: Int,
newVersion: Int,
) {
}

fun loadDatabase(language: String) {
val databaseName = "${language}LanguageData.sqlite"
val dbFile = context.getDatabasePath(databaseName)
if (!dbFile.exists()) {
val inputStream: InputStream = context.assets.open("data/$databaseName")
val outputStream: OutputStream = FileOutputStream(dbFile)

inputStream.copyTo(outputStream)

outputStream.flush()
outputStream.close()
inputStream.close()
}
}

fun getEmojiKeywords(language: String): HashMap<String, MutableList<String>> {
val hashMap = HashMap<String, MutableList<String>>()
val dbFile = context.getDatabasePath("${language}LanguageData.sqlite")
val db = SQLiteDatabase.openDatabase(dbFile.path, null, SQLiteDatabase.OPEN_READONLY)
val cursor = db.rawQuery("SELECT * FROM emoji_keywords", null)

cursor.use {
if (cursor.moveToFirst()) {
do {
val key = cursor.getString(0)
val values = mutableListOf<String>()

for (i in 1 until cursor.columnCount) {
values.add(cursor.getString(i))
}

hashMap[key] = values
} while (cursor.moveToNext())
}
}
return hashMap
}
}
10 changes: 8 additions & 2 deletions app/src/main/java/be/scri/services/EnglishKeyboardIME.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package be.scri.services

import android.text.InputType
import android.util.Log
import android.view.View
import android.view.inputmethod.EditorInfo.IME_ACTION_NONE
import be.scri.R
import be.scri.databinding.KeyboardViewCommandOptionsBinding
import be.scri.helpers.MyKeyboard
import be.scri.helpers.MyKeyboard.Companion.KEYCODE_ENTER
import be.scri.views.MyKeyboardView

class EnglishKeyboardIME : SimpleKeyboardIME("English") {
Expand Down Expand Up @@ -67,7 +67,7 @@ class EnglishKeyboardIME : SimpleKeyboardIME("English") {
super.handleKeyboardLetters(keyboardMode, keyboardView)
keyboardView!!.invalidateAllKeys()
}
KEYCODE_ENTER -> {
MyKeyboard.KEYCODE_ENTER -> {
if (currentState == ScribeState.IDLE || currentState == ScribeState.SELECT_COMMAND) {
handleKeycodeEnter(binding = null, false)
} else {
Expand All @@ -89,6 +89,12 @@ class EnglishKeyboardIME : SimpleKeyboardIME("English") {
}
}

lastWord = getLastWordBeforeCursor()
Log.d("Debug", "$lastWord")
autosuggestEmojis = findEmojisForLastWord(emojiKeywords, lastWord)
Log.d("Debug", "$autosuggestEmojis")
updateButtonText(isAutoSuggestEnabled, autosuggestEmojis)

if (code != MyKeyboard.KEYCODE_SHIFT) {
super.updateShiftKeyState()
}
Expand Down
34 changes: 20 additions & 14 deletions app/src/main/java/be/scri/services/FrenchKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ class FrenchKeyboardIME : SimpleKeyboardIME("French") {
override var switchToLetters = false
override var hasTextBeforeCursor = false

override fun onCreateInputView(): View {
binding = KeyboardViewCommandOptionsBinding.inflate(layoutInflater)
val keyboardHolder = binding.root
Log.i("MY-TAG", "From French Keyboard IME")
keyboardView = binding.keyboardView
keyboardView!!.setKeyboard(keyboard!!)
setupCommandBarTheme(binding)
keyboardView!!.setKeyboardHolder()
keyboardView!!.mOnKeyboardActionListener = this
initializeEmojiButtons()
updateUI()
return keyboardHolder
}

override fun onKey(code: Int) {
val inputConnection = currentInputConnection
if (keyboard == null || inputConnection == null) {
Expand Down Expand Up @@ -71,25 +85,17 @@ class FrenchKeyboardIME : SimpleKeyboardIME("French") {
}
}

lastWord = getLastWordBeforeCursor()
Log.d("Debug", "$lastWord")
autosuggestEmojis = findEmojisForLastWord(emojiKeywords, lastWord)
Log.d("Debug", "$autosuggestEmojis")
updateButtonText(isAutoSuggestEnabled, autosuggestEmojis)

if (code != MyKeyboard.KEYCODE_SHIFT) {
super.updateShiftKeyState()
}
}

override fun onCreateInputView(): View {
binding = KeyboardViewCommandOptionsBinding.inflate(layoutInflater)
val keyboardHolder = binding.root
Log.i("MY-TAG", "From French Keyboard IME")
keyboardView = binding.keyboardView
keyboardView!!.setKeyboard(keyboard!!)
setupCommandBarTheme(binding)
keyboardView!!.setKeyboardHolder()
keyboardView!!.mOnKeyboardActionListener = this
initializeEmojiButtons()
updateUI()
return keyboardHolder
}

override fun onCreate() {
super.onCreate()
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType)
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/be/scri/services/GermanKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class GermanKeyboardIME : SimpleKeyboardIME("German") {
}
}

lastWord = getLastWordBeforeCursor()
Log.d("Debug", "$lastWord")
autosuggestEmojis = findEmojisForLastWord(emojiKeywords, lastWord)
Log.d("Debug", "$autosuggestEmojis")
updateButtonText(isAutoSuggestEnabled, autosuggestEmojis)

if (code != MyKeyboard.KEYCODE_SHIFT) {
super.updateShiftKeyState()
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/be/scri/services/ItalianKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class ItalianKeyboardIME : SimpleKeyboardIME("Italian") {
}
}

lastWord = getLastWordBeforeCursor()
Log.d("Debug", "$lastWord")
autosuggestEmojis = findEmojisForLastWord(emojiKeywords, lastWord)
Log.d("Debug", "$autosuggestEmojis")
updateButtonText(isAutoSuggestEnabled, autosuggestEmojis)

if (code != MyKeyboard.KEYCODE_SHIFT) {
super.updateShiftKeyState()
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/be/scri/services/PortugueseKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class PortugueseKeyboardIME : SimpleKeyboardIME("Portuguese") {
}
}

lastWord = getLastWordBeforeCursor()
Log.d("Debug", "$lastWord")
autosuggestEmojis = findEmojisForLastWord(emojiKeywords, lastWord)
Log.d("Debug", "$autosuggestEmojis")
updateButtonText(isAutoSuggestEnabled, autosuggestEmojis)

if (code != MyKeyboard.KEYCODE_SHIFT) {
super.updateShiftKeyState()
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/be/scri/services/RussianKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class RussianKeyboardIME : SimpleKeyboardIME("Russian") {
}
}

lastWord = getLastWordBeforeCursor()
Log.d("Debug", "$lastWord")
autosuggestEmojis = findEmojisForLastWord(emojiKeywords, lastWord)
Log.d("Debug", "$autosuggestEmojis")
updateButtonText(isAutoSuggestEnabled, autosuggestEmojis)

if (code != MyKeyboard.KEYCODE_SHIFT) {
super.updateShiftKeyState()
}
Expand Down
81 changes: 80 additions & 1 deletion app/src/main/java/be/scri/services/SimpleKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.widget.Button
import be.scri.R
import be.scri.databinding.KeyboardViewCommandOptionsBinding
import be.scri.databinding.KeyboardViewKeyboardBinding
import be.scri.helpers.DatabaseHelper
import be.scri.helpers.MyKeyboard
import be.scri.helpers.SHIFT_OFF
import be.scri.helpers.SHIFT_ON_ONE_CHAR
Expand Down Expand Up @@ -60,11 +61,15 @@ abstract class SimpleKeyboardIME(
private var emojiBtnTablet2: Button? = null
private var emojiSpaceTablet2: View? = null
private var emojiBtnTablet3: Button? = null
private lateinit var dbHelper: DatabaseHelper
lateinit var emojiKeywords: HashMap<String, MutableList<String>>
var isAutoSuggestEnabled: Boolean = false
var lastWord: String? = null
var autosuggestEmojis: MutableList<String>? = null
// abstract var keyboardViewKeyboardBinding : KeyboardViewKeyboardBinding

protected var currentState: ScribeState = ScribeState.IDLE
protected lateinit var keyboardBinding: KeyboardViewKeyboardBinding
private var isAutoSuggestEnabled: Boolean = false

enum class ScribeState {
IDLE,
Expand Down Expand Up @@ -137,6 +142,7 @@ abstract class SimpleKeyboardIME(
setupIdleView()
initializeEmojiButtons()
updateButtonVisibility(isAutoSuggestEnabled)
updateButtonText(isAutoSuggestEnabled, autosuggestEmojis)
}

ScribeState.SELECT_COMMAND -> setupSelectCommandView()
Expand Down Expand Up @@ -314,6 +320,61 @@ abstract class SimpleKeyboardIME(
}
}

fun getLastWordBeforeCursor(): String? {
val inputConnection = currentInputConnection ?: return null

val textBeforeCursor = inputConnection.getTextBeforeCursor(50, 0) ?: return null

val trimmedText = textBeforeCursor.trim().toString()

val lastWord = trimmedText.split("\\s+".toRegex()).lastOrNull()

return lastWord
}

fun findEmojisForLastWord(
emojiKeywords: HashMap<String, MutableList<String>>,
lastWord: String?,
): MutableList<String>? {
lastWord?.let { word ->
val lowerCaseWord = word.lowercase()
val emojis = emojiKeywords[lowerCaseWord]
if (emojis != null) {
Log.d("Debug", "Emojis for '$word': $emojis")
return emojis
} else {
Log.d("Debug", "No emojis found for '$word'")
}
}
return null
}

fun updateButtonText(
isAutoSuggestEnabled: Boolean,
autosuggestEmojis: MutableList<String>?,
) {
if (isAutoSuggestEnabled) {
emojiBtnTablet1?.text = autosuggestEmojis?.get(0)
emojiBtnTablet2?.text = autosuggestEmojis?.get(1)
emojiBtnTablet3?.text = autosuggestEmojis?.get(2)

emojiBtnPhone1?.text = autosuggestEmojis?.get(0)
emojiBtnPhone2?.text = autosuggestEmojis?.get(1)

binding.emojiBtnTablet1.setOnClickListener { insertEmoji(emojiBtnTablet1?.text.toString()) }
binding.emojiBtnTablet2.setOnClickListener { insertEmoji(emojiBtnTablet2?.text.toString()) }
binding.emojiBtnTablet3.setOnClickListener { insertEmoji(emojiBtnTablet3?.text.toString()) }

binding.emojiBtnPhone1.setOnClickListener { insertEmoji(emojiBtnPhone1?.text.toString()) }
binding.emojiBtnPhone2.setOnClickListener { insertEmoji(emojiBtnPhone2?.text.toString()) }
}
}

private fun insertEmoji(emoji: String) {
val inputConnection = currentInputConnection ?: return
inputConnection.commitText(emoji, 1)
}

override fun onPress(primaryCode: Int) {
if (primaryCode != 0) {
keyboardView?.vibrateIfNeeded()
Expand Down Expand Up @@ -344,10 +405,28 @@ abstract class SimpleKeyboardIME(
}
}

val languageAlias = getLanguageAlias(language)
dbHelper = DatabaseHelper(this)
dbHelper.loadDatabase(languageAlias)
emojiKeywords = dbHelper.getEmojiKeywords(languageAlias)

keyboard = MyKeyboard(this, keyboardXml, enterKeyType)
keyboardView?.setKeyboard(keyboard!!)
}

private fun getLanguageAlias(language: String): String =
when (language) {
"English" -> "EN"
"French" -> "FR"
"German" -> "DE"
"Italian" -> "IT"
"Portuguese" -> "PT"
"Russian" -> "RU"
"Spanish" -> "ES"
"Swedish" -> "SV"
else -> ""
}

fun updateShiftKeyState() {
if (keyboardMode == keyboardLetters) {
val editorInfo = currentInputEditorInfo
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/be/scri/services/SpanishKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class SpanishKeyboardIME : SimpleKeyboardIME(language = "Spanish") {
}
}

lastWord = getLastWordBeforeCursor()
Log.d("Debug", "$lastWord")
autosuggestEmojis = findEmojisForLastWord(emojiKeywords, lastWord)
Log.d("Debug", "$autosuggestEmojis")
updateButtonText(isAutoSuggestEnabled, autosuggestEmojis)

if (code != MyKeyboard.KEYCODE_SHIFT) {
super.updateShiftKeyState()
}
Expand Down
34 changes: 20 additions & 14 deletions app/src/main/java/be/scri/services/SwedishKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ class SwedishKeyboardIME : SimpleKeyboardIME("Swedish") {
override var switchToLetters = false
override var hasTextBeforeCursor = false

override fun onCreateInputView(): View {
binding = KeyboardViewCommandOptionsBinding.inflate(layoutInflater)
val keyboardHolder = binding.root
Log.i("MY-TAG", "From Swedish Keyboard IME")
keyboardView = binding.keyboardView
keyboardView!!.setKeyboard(keyboard!!)
setupCommandBarTheme(binding)
keyboardView!!.setKeyboardHolder()
keyboardView!!.mOnKeyboardActionListener = this
initializeEmojiButtons()
updateUI()
return keyboardHolder
}

override fun onKey(code: Int) {
val inputConnection = currentInputConnection
if (keyboard == null || inputConnection == null) {
Expand Down Expand Up @@ -76,25 +90,17 @@ class SwedishKeyboardIME : SimpleKeyboardIME("Swedish") {
}
}

lastWord = getLastWordBeforeCursor()
Log.d("Debug", "$lastWord")
autosuggestEmojis = findEmojisForLastWord(emojiKeywords, lastWord)
Log.d("Debug", "$autosuggestEmojis")
updateButtonText(isAutoSuggestEnabled, autosuggestEmojis)

if (code != MyKeyboard.KEYCODE_SHIFT) {
super.updateShiftKeyState()
}
}

override fun onCreateInputView(): View {
binding = KeyboardViewCommandOptionsBinding.inflate(layoutInflater)
val keyboardHolder = binding.root
Log.i("MY-TAG", "From Swedish Keyboard IME")
keyboardView = binding.keyboardView
keyboardView!!.setKeyboard(keyboard!!)
setupCommandBarTheme(binding)
keyboardView!!.setKeyboardHolder()
keyboardView!!.mOnKeyboardActionListener = this
initializeEmojiButtons()
updateUI()
return keyboardHolder
}

override fun onCreate() {
super.onCreate()
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType)
Expand Down
Loading