Skip to content

Commit 01aeabd

Browse files
chihung93Hung
authored andcommitted
feature(language): add vi string + config Vn
Signed-off-by: Hung <>
1 parent b57a046 commit 01aeabd

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

app/src/main/kotlin/com/simplemobiletools/keyboard/activities/SettingsActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.simplemobiletools.keyboard.extensions.getKeyboardLanguages
1414
import com.simplemobiletools.keyboard.helpers.KEYBOARD_HEIGHT_MULTIPLIER_LARGE
1515
import com.simplemobiletools.keyboard.helpers.KEYBOARD_HEIGHT_MULTIPLIER_MEDIUM
1616
import com.simplemobiletools.keyboard.helpers.KEYBOARD_HEIGHT_MULTIPLIER_SMALL
17+
import com.simplemobiletools.keyboard.helpers.LANGUAGE_VIETNAMESE_TELEX
1718
import kotlinx.android.synthetic.main.activity_settings.*
1819
import java.util.*
1920
import kotlin.system.exitProcess

app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ const val KEYBOARD_HEIGHT_MULTIPLIER_MEDIUM = 2
5151
const val KEYBOARD_HEIGHT_MULTIPLIER_LARGE = 3
5252

5353
const val EMOJI_SPEC_FILE_PATH = "media/emoji_spec.txt"
54+
const val LANGUAGE_VN_TELEX = "language/extension.json"

app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/EmojiHelper.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.simplemobiletools.keyboard.helpers
22

33
import android.content.Context
4+
import org.json.JSONObject
5+
import java.io.InputStream
46

57
private var cachedEmojiData: MutableList<String>? = null
8+
val cachedVNTelexData: HashMap<String,String> = HashMap()
69

710
/**
811
* 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<String>
5962
cachedEmojiData = emojis
6063
return emojis
6164
}
65+
66+
67+
fun parseRawJsonSpecsFile(context: Context, path: String): HashMap<String,String> {
68+
if (cachedVNTelexData.isNotEmpty()) {
69+
return cachedVNTelexData
70+
}
71+
72+
try {
73+
val inputStream: InputStream = context.assets.open(path)
74+
val jsonString = inputStream.bufferedReader().use{it.readText()}
75+
val jsonData = JSONObject(jsonString)
76+
val rulesObj = jsonData.getJSONObject("rules")
77+
val ruleKeys = rulesObj.keys()
78+
while (ruleKeys.hasNext()){
79+
val key = ruleKeys.next()
80+
val value = rulesObj.getString(key)
81+
cachedVNTelexData[key] = value
82+
}
83+
} catch (ex: Exception) {
84+
ex.printStackTrace()
85+
return HashMap()
86+
}
87+
return cachedVNTelexData
88+
}

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,38 @@ class SimpleKeyboardIME : InputMethodService(), OnKeyboardActionListener, Shared
178178
switchToLetters = true
179179
}
180180
} else {
181-
inputConnection.commitText(codeChar.toString(), 1)
182-
if (originalText == null) {
183-
updateShiftKeyState()
181+
when {
182+
originalText != null && originalText.isNotEmpty() && cachedVNTelexData.isNotEmpty() -> {
183+
val fullText = originalText.toString() + codeChar.toString()
184+
val lastIndexEmpty = if (fullText.contains(" ")) {
185+
fullText.lastIndexOf(" ")
186+
} else 0
187+
if (lastIndexEmpty >= 0) {
188+
val word = fullText.subSequence(lastIndexEmpty, fullText.length).trim().toString()
189+
val wordChars = word.toCharArray()
190+
val predictWord = StringBuilder()
191+
for (char in wordChars.size -1 downTo 0) {
192+
predictWord.append(wordChars[char])
193+
val shouldChangeText = predictWord.reverse().toString()
194+
if (cachedVNTelexData.containsKey(shouldChangeText)) {
195+
inputConnection.setComposingRegion(fullText.length - shouldChangeText.length, fullText.length)
196+
inputConnection.setComposingText(cachedVNTelexData[shouldChangeText], fullText.length)
197+
inputConnection.setComposingRegion(fullText.length, fullText.length)
198+
return
199+
}
200+
}
201+
inputConnection.commitText(codeChar.toString(), 1)
202+
}
203+
}
204+
else -> {
205+
inputConnection.commitText(codeChar.toString(), 1)
206+
if (originalText == null) {
207+
updateShiftKeyState()
208+
}
209+
}
184210
}
211+
212+
185213
}
186214
}
187215
}

app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
418418
}
419419

420420
setupEmojiPalette(toolbarColor = toolbarColor, backgroundColor = mBackgroundColor, textColor = mTextColor)
421+
if(context.config.keyboardLanguage == LANGUAGE_VIETNAMESE_TELEX){
422+
setupLanguageTelex()
423+
}else{
424+
cachedVNTelexData.clear()
425+
}
421426
setupStoredClips()
422427
}
423428

@@ -1506,6 +1511,13 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
15061511
}
15071512
}
15081513

1514+
// For Vietnamese - Telex
1515+
private fun setupLanguageTelex() {
1516+
ensureBackgroundThread {
1517+
parseRawJsonSpecsFile(context, LANGUAGE_VN_TELEX)
1518+
}
1519+
}
1520+
15091521
private fun setupEmojiAdapter(emojis: List<String>) {
15101522
mEmojiPaletteHolder?.emojis_list?.apply {
15111523
val emojiItemWidth = context.resources.getDimensionPixelSize(R.dimen.emoji_item_size)

0 commit comments

Comments
 (0)