From 01aeabdf9a8d514928ad4b4979ec83a1d4b51e94 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 26 Jun 2023 16:25:45 +0700 Subject: [PATCH] feature(language): add vi string + config Vn Signed-off-by: Hung <> --- .../keyboard/activities/SettingsActivity.kt | 1 + .../keyboard/helpers/Constants.kt | 1 + .../keyboard/helpers/EmojiHelper.kt | 27 +++++++++++++++ .../keyboard/services/SimpleKeyboardIME.kt | 34 +++++++++++++++++-- .../keyboard/views/MyKeyboardView.kt | 12 +++++++ 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/activities/SettingsActivity.kt index 8b93961..8681413 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/activities/SettingsActivity.kt @@ -14,6 +14,7 @@ import com.simplemobiletools.keyboard.extensions.getKeyboardLanguages import com.simplemobiletools.keyboard.helpers.KEYBOARD_HEIGHT_MULTIPLIER_LARGE import com.simplemobiletools.keyboard.helpers.KEYBOARD_HEIGHT_MULTIPLIER_MEDIUM import com.simplemobiletools.keyboard.helpers.KEYBOARD_HEIGHT_MULTIPLIER_SMALL +import com.simplemobiletools.keyboard.helpers.LANGUAGE_VIETNAMESE_TELEX import kotlinx.android.synthetic.main.activity_settings.* import java.util.* import kotlin.system.exitProcess diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt index 468de4f..6ad63db 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt @@ -51,3 +51,4 @@ const val KEYBOARD_HEIGHT_MULTIPLIER_MEDIUM = 2 const val KEYBOARD_HEIGHT_MULTIPLIER_LARGE = 3 const val EMOJI_SPEC_FILE_PATH = "media/emoji_spec.txt" +const val LANGUAGE_VN_TELEX = "language/extension.json" diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/EmojiHelper.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/EmojiHelper.kt index 1545450..9805c9d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/EmojiHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/EmojiHelper.kt @@ -1,8 +1,11 @@ package com.simplemobiletools.keyboard.helpers import android.content.Context +import org.json.JSONObject +import java.io.InputStream private var cachedEmojiData: MutableList? = null +val cachedVNTelexData: HashMap = HashMap() /** * Reads the emoji list at the given [path] and returns an parsed [MutableList]. If the @@ -59,3 +62,27 @@ fun parseRawEmojiSpecsFile(context: Context, path: String): MutableList cachedEmojiData = emojis return emojis } + + +fun parseRawJsonSpecsFile(context: Context, path: String): HashMap { + if (cachedVNTelexData.isNotEmpty()) { + return cachedVNTelexData + } + + try { + val inputStream: InputStream = context.assets.open(path) + val jsonString = inputStream.bufferedReader().use{it.readText()} + val jsonData = JSONObject(jsonString) + val rulesObj = jsonData.getJSONObject("rules") + val ruleKeys = rulesObj.keys() + while (ruleKeys.hasNext()){ + val key = ruleKeys.next() + val value = rulesObj.getString(key) + cachedVNTelexData[key] = value + } + } catch (ex: Exception) { + ex.printStackTrace() + return HashMap() + } + return cachedVNTelexData +} diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt index 8140f15..7018947 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt @@ -178,10 +178,38 @@ class SimpleKeyboardIME : InputMethodService(), OnKeyboardActionListener, Shared switchToLetters = true } } else { - inputConnection.commitText(codeChar.toString(), 1) - if (originalText == null) { - updateShiftKeyState() + when { + originalText != null && originalText.isNotEmpty() && cachedVNTelexData.isNotEmpty() -> { + val fullText = originalText.toString() + codeChar.toString() + val lastIndexEmpty = if (fullText.contains(" ")) { + fullText.lastIndexOf(" ") + } else 0 + if (lastIndexEmpty >= 0) { + val word = fullText.subSequence(lastIndexEmpty, fullText.length).trim().toString() + val wordChars = word.toCharArray() + val predictWord = StringBuilder() + for (char in wordChars.size -1 downTo 0) { + predictWord.append(wordChars[char]) + val shouldChangeText = predictWord.reverse().toString() + if (cachedVNTelexData.containsKey(shouldChangeText)) { + inputConnection.setComposingRegion(fullText.length - shouldChangeText.length, fullText.length) + inputConnection.setComposingText(cachedVNTelexData[shouldChangeText], fullText.length) + inputConnection.setComposingRegion(fullText.length, fullText.length) + return + } + } + inputConnection.commitText(codeChar.toString(), 1) + } + } + else -> { + inputConnection.commitText(codeChar.toString(), 1) + if (originalText == null) { + updateShiftKeyState() + } + } } + + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt index 8653f04..c344779 100644 --- a/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt @@ -418,6 +418,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut } setupEmojiPalette(toolbarColor = toolbarColor, backgroundColor = mBackgroundColor, textColor = mTextColor) + if(context.config.keyboardLanguage == LANGUAGE_VIETNAMESE_TELEX){ + setupLanguageTelex() + }else{ + cachedVNTelexData.clear() + } setupStoredClips() } @@ -1506,6 +1511,13 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut } } + // For Vietnamese - Telex + private fun setupLanguageTelex() { + ensureBackgroundThread { + parseRawJsonSpecsFile(context, LANGUAGE_VN_TELEX) + } + } + private fun setupEmojiAdapter(emojis: List) { mEmojiPaletteHolder?.emojis_list?.apply { val emojiItemWidth = context.resources.getDimensionPixelSize(R.dimen.emoji_item_size)