From 9c2db2a269015f1663597d67291531dd98af839e Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Sun, 15 Nov 2026 16:10:32 +0530 Subject: [PATCH] feat(backup): back up the personal dictionary and separate the text expander The personal dictionary lives in Android's user dictionary, so the backup, which only contained files and preferences of the app, did not include it. It is saved as personal_dictionary.txt now, and restoring only adds the words that are not in the user dictionary yet. The backup dialog has separate options for Dictionaries, Personal Dictionary and Text Expander (the text expander was part of Dictionaries without saying so). Backups made before restore as before with the default selection. The export and import of the personal dictionary share the code with the backup. The import also keeps locales such as en_US, which were read as language tags before and came back empty. --- .../latin/utils/PersonalDictionaryTransfer.kt | 130 ++++++++++++++++++ .../preferences/BackupRestorePreference.kt | 35 ++++- .../screens/PersonalDictionariesScreen.kt | 95 +------------ app/src/main/res/values-ar/strings.xml | 2 + app/src/main/res/values-be/strings.xml | 2 + app/src/main/res/values-bg/strings.xml | 2 + app/src/main/res/values-cs/strings.xml | 2 + app/src/main/res/values-de/strings.xml | 2 + app/src/main/res/values-el/strings.xml | 2 + app/src/main/res/values-es-rUS/strings.xml | 2 + app/src/main/res/values-es/strings.xml | 2 + app/src/main/res/values-et/strings.xml | 2 + app/src/main/res/values-fr/strings.xml | 2 + app/src/main/res/values-hi/strings.xml | 2 + app/src/main/res/values-hr/strings.xml | 2 + app/src/main/res/values-hu/strings.xml | 2 + app/src/main/res/values-in/strings.xml | 2 + app/src/main/res/values-it/strings.xml | 2 + app/src/main/res/values-iw/strings.xml | 2 + app/src/main/res/values-lt/strings.xml | 2 + app/src/main/res/values-ml/strings.xml | 2 + app/src/main/res/values-nl/strings.xml | 2 + app/src/main/res/values-pl/strings.xml | 2 + app/src/main/res/values-pt-rBR/strings.xml | 2 + app/src/main/res/values-pt-rPT/strings.xml | 2 + app/src/main/res/values-pt/strings.xml | 2 + app/src/main/res/values-ro/strings.xml | 2 + app/src/main/res/values-ru/strings.xml | 2 + app/src/main/res/values-tr/strings.xml | 2 + app/src/main/res/values-uk/strings.xml | 2 + app/src/main/res/values-ur/strings.xml | 2 + app/src/main/res/values-zh-rCN/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + .../utils/PersonalDictionaryTransferTest.kt | 47 +++++++ .../settings/BackupPreferencesTest.kt | 22 ++- docs/FEATURES.md | 7 +- 36 files changed, 297 insertions(+), 99 deletions(-) create mode 100644 app/src/main/java/helium314/keyboard/latin/utils/PersonalDictionaryTransfer.kt create mode 100644 app/src/test/java/helium314/keyboard/latin/utils/PersonalDictionaryTransferTest.kt diff --git a/app/src/main/java/helium314/keyboard/latin/utils/PersonalDictionaryTransfer.kt b/app/src/main/java/helium314/keyboard/latin/utils/PersonalDictionaryTransfer.kt new file mode 100644 index 000000000..67d60ff4c --- /dev/null +++ b/app/src/main/java/helium314/keyboard/latin/utils/PersonalDictionaryTransfer.kt @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-3.0-only +package helium314.keyboard.latin.utils + +import android.content.ContentValues +import android.content.Context +import android.provider.UserDictionary +import java.io.BufferedReader +import java.io.Writer +import java.util.Locale + +/** + * Reads and writes the personal dictionary, i.e. the words in Android's user dictionary, as a text file + * with one word per line (word, shortcut and locale separated by tabs, like the Gboard dictionary export). + * Used by the export and import of the personal dictionary screen and by the backup. + */ +object PersonalDictionaryTransfer { + /** Entry name of the personal dictionary in a backup */ + const val BACKUP_ENTRY_NAME = "personal_dictionary.txt" + + private const val HEADER_PREFIX = "# Gboard Dictionary format:" + + /** [locale] is the locale as stored in the user dictionary (e.g. "en_US"), null for all languages */ + data class Entry(val word: String, val shortcut: String?, val locale: String?) + + /** Writes the header and the [entries], returns the number of entries */ + fun write(entries: Sequence, writer: Writer): Int { + writer.write("$HEADER_PREFIX word\tshortcut\tlocale\n") + var count = 0 + for (entry in entries) { + writer.write("${entry.word}\t${entry.shortcut.orEmpty()}\t${entry.locale.orEmpty()}\n") + count++ + } + return count + } + + fun parse(reader: BufferedReader): List { + val result = mutableListOf() + var wordIndex = 0 + var shortcutIndex = 1 + var localeIndex = 2 + while (true) { + val line = reader.readLine() ?: break + if (line.startsWith("#")) { + if (line.startsWith(HEADER_PREFIX)) { + val columnsStr = line.substring(HEADER_PREFIX.length) + val columns = (if (columnsStr.contains("\t")) columnsStr.split("\t") else columnsStr.split(Regex("\\s+"))) + .map { it.trim() } + val word = columns.indexOf("word") + if (word != -1) { + wordIndex = word + shortcutIndex = columns.indexOf("shortcut") + localeIndex = columns.indexOf("language_tag").let { if (it == -1) columns.indexOf("locale") else it } + } + } + continue + } + val parts = line.split("\t") + val word = parts.getOrNull(wordIndex).orEmpty() + if (word.isBlank()) continue + val shortcut = if (shortcutIndex != -1) parts.getOrNull(shortcutIndex)?.ifBlank { null } else null + val locale = if (localeIndex != -1) normalizeLocale(parts.getOrNull(localeIndex)) else null + result.add(Entry(word, shortcut, locale)) + } + return result + } + + /** + * Locale as stored in the user dictionary: "en_US" is kept, a language tag like "en-US" is converted, + * and blank or "all" means all languages. + */ + fun normalizeLocale(raw: String?): String? { + val value = raw?.trim().orEmpty() + if (value.isEmpty() || value == "all") return null + if (value.contains('_')) return value // already in the form of Locale.toString + return try { Locale.forLanguageTag(value).toString().ifEmpty { null } } catch (_: Exception) { null } + } + + /** All words of the user dictionary; the cursor can be null if there is no user dictionary */ + fun readAll(context: Context): List { + val cursor = context.contentResolver.query( + UserDictionary.Words.CONTENT_URI, + arrayOf(UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT, UserDictionary.Words.LOCALE), + null, null, UserDictionary.Words.DEFAULT_SORT_ORDER + ) ?: return emptyList() + return cursor.use { + val wordIndex = it.getColumnIndexOrThrow(UserDictionary.Words.WORD) + val shortcutIndex = it.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT) + val localeIndex = it.getColumnIndexOrThrow(UserDictionary.Words.LOCALE) + buildList { + while (it.moveToNext()) { + val word = it.getString(wordIndex) ?: continue + add(Entry(word, it.getString(shortcutIndex)?.ifEmpty { null }, it.getString(localeIndex)?.ifEmpty { null })) + } + } + } + } + + /** Adds [entries] to the user dictionary, without the ones that are in there already if [skipExisting]; returns the number added */ + fun insert(context: Context, entries: List, skipExisting: Boolean): Int { + val toAdd = if (skipExisting) { + val existing = readAll(context).toSet() + entries.filter { it !in existing }.distinct() + } else entries + if (toAdd.isEmpty()) return 0 + val values = toAdd.map { + ContentValues(5).apply { + put(UserDictionary.Words.WORD, it.word) + put(UserDictionary.Words.FREQUENCY, 250) + put(UserDictionary.Words.LOCALE, it.locale) + put(UserDictionary.Words.APP_ID, 0) + put(UserDictionary.Words.SHORTCUT, it.shortcut) + } + } + return try { + context.contentResolver.bulkInsert(UserDictionary.Words.CONTENT_URI, values.toTypedArray()) + } catch (e: Exception) { + Log.e("PersonalDictionary", "Bulk insert failed, falling back to one-by-one insert", e) + var added = 0 + for (v in values) { + try { + context.contentResolver.insert(UserDictionary.Words.CONTENT_URI, v) + added++ + } catch (ex: Exception) { + Log.w("PersonalDictionary", "Failed to add a word", ex) + } + } + added + } + } +} diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/BackupRestorePreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/BackupRestorePreference.kt index d0094e92a..2d8c82261 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/BackupRestorePreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/BackupRestorePreference.kt @@ -45,6 +45,7 @@ import helium314.keyboard.latin.utils.Log import helium314.keyboard.latin.utils.ProofreadService import helium314.keyboard.latin.utils.SubtypeSettings import helium314.keyboard.latin.utils.getActivity +import helium314.keyboard.latin.utils.PersonalDictionaryTransfer import helium314.keyboard.latin.utils.prefs import helium314.keyboard.latin.utils.protectedPrefs import helium314.keyboard.settings.Setting @@ -76,6 +77,8 @@ fun BackupRestorePreference(setting: Setting) { BackupCategory.LAYOUTS, BackupCategory.THEME_APPEARANCE, BackupCategory.DICTIONARIES, + BackupCategory.PERSONAL_DICTIONARY, + BackupCategory.TEXT_EXPANDER, BackupCategory.TYPING_HISTORY, BackupCategory.CLIPBOARD, BackupCategory.GENERAL_SETTINGS @@ -99,6 +102,8 @@ fun BackupRestorePreference(setting: Setting) { BackupCategory.LAYOUTS to R.string.backup_category_layouts, BackupCategory.THEME_APPEARANCE to R.string.backup_category_theme, BackupCategory.DICTIONARIES to R.string.backup_category_dictionaries, + BackupCategory.PERSONAL_DICTIONARY to R.string.backup_category_personal_dictionary, + BackupCategory.TEXT_EXPANDER to R.string.backup_category_text_expander, BackupCategory.TYPING_HISTORY to R.string.backup_category_typing_history, BackupCategory.CLIPBOARD to R.string.backup_category_clipboard, BackupCategory.API_KEYS to R.string.backup_category_api_keys, @@ -254,6 +259,17 @@ private fun backupLauncher( } } } + if (selectedCategories.contains(BackupCategory.PERSONAL_DICTIONARY)) { + // the personal dictionary is in the system user dictionary, not in the files of the app + val words = PersonalDictionaryTransfer.readAll(ctx) + if (words.isNotEmpty()) { + zipStream.putNextEntry(ZipEntry(PersonalDictionaryTransfer.BACKUP_ENTRY_NAME)) + val writer = zipStream.writer(Charsets.UTF_8) // not closed, this would close the zip + PersonalDictionaryTransfer.write(words.asSequence(), writer) + writer.flush() + zipStream.closeEntry() + } + } if (selectedCategories.contains(BackupCategory.API_KEYS)) { val secureSettings = ProofreadService(ctx).getSecureSettings() if (secureSettings.isNotEmpty()) { @@ -325,6 +341,8 @@ private fun restoreLauncher( File(filesDir, "dicts").deleteRecursively() File(filesDir, "blacklists").deleteRecursively() File(deviceProtectedFilesDir, "blacklists").deleteRecursively() + } + if (selectedCategories.contains(BackupCategory.TEXT_EXPANDER)) { File(filesDir, "text_expander_images").deleteRecursively() File(deviceProtectedFilesDir, "text_expander_images").deleteRecursively() helium314.keyboard.latin.utils.TextExpanderImageUtils.clearCache() @@ -382,6 +400,12 @@ private fun restoreLauncher( val restoredMap = readJsonLinesToMap(apiKeyLines) ProofreadService(ctx).restoreSecureSettings(restoredMap) } + } else if (entry.name == PersonalDictionaryTransfer.BACKUP_ENTRY_NAME) { + if (selectedCategories.contains(BackupCategory.PERSONAL_DICTIONARY)) { + // adds the words that are not in the user dictionary yet, existing words are kept + val words = PersonalDictionaryTransfer.parse(zip.reader(Charsets.UTF_8).buffered()) + PersonalDictionaryTransfer.insert(ctx, words, skipExisting = true) + } } else if (entry.name == PREFS_FILE_NAME) { preferenceLines = String(zip.readBytes()).split("\n") } else if (entry.name == PROTECTED_PREFS_FILE_NAME) { @@ -588,13 +612,15 @@ enum class BackupCategory { LAYOUTS, THEME_APPEARANCE, DICTIONARIES, + PERSONAL_DICTIONARY, + TEXT_EXPANDER, TYPING_HISTORY, CLIPBOARD, API_KEYS, GENERAL_SETTINGS } -private fun getCategoryForPrefKey(key: String): BackupCategory { +internal fun getCategoryForPrefKey(key: String): BackupCategory { if (key.startsWith("layout_")) return BackupCategory.LAYOUTS val themeKeys = setOf( @@ -623,7 +649,8 @@ private fun getCategoryForPrefKey(key: String): BackupCategory { "add_to_personal_dictionary", "add_to_personal_dict_threshold", "auto_correct_aggressiveness" ) - if (dictKeys.contains(key) || key.startsWith("pref_text_expander_")) return BackupCategory.DICTIONARIES + if (key.startsWith("pref_text_expander_")) return BackupCategory.TEXT_EXPANDER + if (dictKeys.contains(key)) return BackupCategory.DICTIONARIES val clipboardKeys = setOf( "enable_clipboard_history", "suggest_screenshots", "compress_screenshots", @@ -644,10 +671,12 @@ internal fun getCategoryForFilePath(path: String): BackupCategory? { } if (path.startsWith("dicts${File.separator}") || path.startsWith("dicts/") || path.startsWith("blacklists${File.separator}") || path.startsWith("blacklists/") - || path.startsWith("text_expander_images${File.separator}") || path.startsWith("text_expander_images/") ) { return BackupCategory.DICTIONARIES } + if (path.startsWith("text_expander_images${File.separator}") || path.startsWith("text_expander_images/")) { + return BackupCategory.TEXT_EXPANDER + } if (path.startsWith("UserHistoryDictionary")) { return BackupCategory.TYPING_HISTORY } diff --git a/app/src/main/java/helium314/keyboard/settings/screens/PersonalDictionariesScreen.kt b/app/src/main/java/helium314/keyboard/settings/screens/PersonalDictionariesScreen.kt index 8125cd74b..cdbf3cbb4 100644 --- a/app/src/main/java/helium314/keyboard/settings/screens/PersonalDictionariesScreen.kt +++ b/app/src/main/java/helium314/keyboard/settings/screens/PersonalDictionariesScreen.kt @@ -45,6 +45,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.res.painterResource import helium314.keyboard.keyboard.KeyboardSwitcher import helium314.keyboard.latin.utils.Log +import helium314.keyboard.latin.utils.PersonalDictionaryTransfer import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -202,75 +203,8 @@ private suspend fun importGboardDictionary(context: android.content.Context, uri return addedCount } -private fun parseAndInsert(context: android.content.Context, reader: BufferedReader): Int { - val valuesList = mutableListOf() - var line: String? - var wordIndex = 0 - var shortcutIndex = 1 - var localeIndex = 2 - while (reader.readLine().also { line = it } != null) { - val currentLine = line ?: continue - if (currentLine.startsWith("#")) { - val formatPrefix = "# Gboard Dictionary format:" - if (currentLine.startsWith(formatPrefix)) { - val columnsStr = currentLine.substring(formatPrefix.length) - val columns = if (columnsStr.contains("\t")) { - columnsStr.split("\t") - } else { - columnsStr.split(Regex("\\s+")) - }.map { it.trim() } - val wIdx = columns.indexOf("word") - val sIdx = columns.indexOf("shortcut") - val lIdx = columns.indexOf("language_tag").let { if (it == -1) columns.indexOf("locale") else it } - if (wIdx != -1) { - wordIndex = wIdx - shortcutIndex = sIdx - localeIndex = lIdx - } - } - continue - } - val parts = currentLine.split("\t") - if (parts.isNotEmpty()) { - val word = if (wordIndex in parts.indices) parts[wordIndex] else "" - if (word.isNotBlank()) { - val shortcut = if (shortcutIndex != -1 && shortcutIndex in parts.indices) parts[shortcutIndex].ifBlank { null } else null - val localeStr = if (localeIndex != -1 && localeIndex in parts.indices) parts[localeIndex].ifBlank { null } else null - - val locale = if (localeStr != null && localeStr != "all") { - try { Locale.forLanguageTag(localeStr) } catch(_: Exception) { null } - } else null - - val values = ContentValues(5).apply { - put(UserDictionary.Words.WORD, word) - put(UserDictionary.Words.FREQUENCY, 250) - put(UserDictionary.Words.LOCALE, locale?.toString()) - put(UserDictionary.Words.APP_ID, 0) - put(UserDictionary.Words.SHORTCUT, shortcut) - } - valuesList.add(values) - } - } - } - - if (valuesList.isEmpty()) return 0 - - return try { - context.contentResolver.bulkInsert(UserDictionary.Words.CONTENT_URI, valuesList.toTypedArray()) - } catch (e: Exception) { - Log.e("ImportDict", "Bulk insert failed, falling back to one-by-one insert", e) - var successCount = 0 - for (values in valuesList) { - try { - context.contentResolver.insert(UserDictionary.Words.CONTENT_URI, values) - successCount++ - } catch (ex: Exception) { - Log.w("ImportDict", "Failed to add word ${values.getAsString(UserDictionary.Words.WORD)}", ex) - } - } - successCount - } -} +private fun parseAndInsert(context: android.content.Context, reader: BufferedReader): Int = + PersonalDictionaryTransfer.insert(context, PersonalDictionaryTransfer.parse(reader), skipExisting = false) fun getSortedDictionaryLocales(): TreeSet { val sortedLocales = sortedSetOf(compareBy { it.toLanguageTag().lowercase() }) @@ -293,27 +227,8 @@ fun getSortedDictionaryLocales(): TreeSet { } private suspend fun exportAllDictionaries(context: android.content.Context, uri: android.net.Uri): Int = withContext(Dispatchers.IO) { - val cursor = context.contentResolver.query( - UserDictionary.Words.CONTENT_URI, - arrayOf(UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT, UserDictionary.Words.LOCALE), - null, null, UserDictionary.Words.DEFAULT_SORT_ORDER - ) ?: return@withContext 0 - var count = 0 - context.contentResolver.openOutputStream(uri)?.bufferedWriter()?.use { writer -> - writer.write("# Gboard Dictionary format: word\tshortcut\tlocale\n") - val wordIndex = cursor.getColumnIndexOrThrow(UserDictionary.Words.WORD) - val shortcutIndex = cursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT) - val localeIndex = cursor.getColumnIndexOrThrow(UserDictionary.Words.LOCALE) - while (cursor.moveToNext()) { - val word = cursor.getString(wordIndex) ?: continue - val shortcut = cursor.getString(shortcutIndex).orEmpty() - val loc = cursor.getString(localeIndex).orEmpty() - writer.write("$word\t$shortcut\t$loc\n") - count++ - } - } - cursor.close() - count + val entries = PersonalDictionaryTransfer.readAll(context) + context.contentResolver.openOutputStream(uri)?.bufferedWriter()?.use { PersonalDictionaryTransfer.write(entries.asSequence(), it) } ?: 0 } @Preview diff --git a/app/src/main/res/values-ar/strings.xml b/app/src/main/res/values-ar/strings.xml index 65184438f..9e089f6fb 100644 --- a/app/src/main/res/values-ar/strings.xml +++ b/app/src/main/res/values-ar/strings.xml @@ -1771,4 +1771,6 @@ أنماط النص المخصصة نمط واحد لكل سطر. التنسيق: الاسم: بالنمط A-Z a-z 0-9 (52 أو 62 حرفًا). انتظار أقصر للإملاءات الطويلة + القاموس الشخصي + موسع النص diff --git a/app/src/main/res/values-be/strings.xml b/app/src/main/res/values-be/strings.xml index c560f2162..892615556 100644 --- a/app/src/main/res/values-be/strings.xml +++ b/app/src/main/res/values-be/strings.xml @@ -1748,4 +1748,6 @@ Карыстальніцкія стылі тэксту Адзін стыль на радок. Фармат: Імя: у стылі A-Z a-z 0-9 (52 або 62 сімвалы). Карацей чакаць доўгіх дыктовак + Асабісты слоўнік + Расшыральнік тэксту diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 8106aecf2..c182da874 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -1748,4 +1748,6 @@ Персонализирани текстови стилове Един стил на ред. Формат: Име: стилизирано A-Z a-z 0-9 (52 или 62 знака). По-кратко изчакване за дълги диктовки + Личен речник + Текстов разширител diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 5a97e0e99..09e35481c 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -1713,4 +1713,6 @@ Umožňuje lokální inferenci GGUF / llama.cpp na zařízení pro korektury a p Vlastní styly textu Jeden styl na řádek. Formát: Název: stylizovaný A-Z a-z 0-9 (52 nebo 62 znaků). Kratší čekání na dlouhé diktáty + Osobní slovník + Text Expander diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index fda3714be..656b9d5c9 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -1764,4 +1764,6 @@ Ermöglicht lokale GGUF / llama.cpp-Inferenz für Korrekturlesen und Umschreiben Benutzerdefinierte Textstile Ein Stil pro Zeile. Format: Name: Stil A-Z a-z 0-9 (52 oder 62 Zeichen). Kürzere Wartezeiten bei langen Diktaten + Persönliches Wörterbuch + Textexpander diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index 19e543227..9adaa56eb 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -1814,4 +1814,6 @@ Προσαρμοσμένα στυλ κειμένου Ένα στυλ ανά γραμμή. Μορφή: Όνομα: στυλ A-Z a-z 0-9 (52 ή 62 χαρακτήρες). Συντομότερη αναμονή για μεγάλες υπαγορεύσεις + Προσωπικό Λεξικό + Επέκταση κειμένου diff --git a/app/src/main/res/values-es-rUS/strings.xml b/app/src/main/res/values-es-rUS/strings.xml index 66abfb34b..f8368dead 100644 --- a/app/src/main/res/values-es-rUS/strings.xml +++ b/app/src/main/res/values-es-rUS/strings.xml @@ -1780,4 +1780,6 @@ Permite la inferencia GGUF / llama.cpp en el dispositivo para corrección y rees Estilos de texto personalizados Un estilo por línea. Formato: Nombre: estilo A-Z a-z 0-9 (52 o 62 caracteres). Espera más corta para dictados largos + Diccionario personal + Expansor de texto diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index b7dc23679..9181d74e9 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -1710,4 +1710,6 @@ Permite la inferencia GGUF / llama.cpp en el dispositivo para corrección y rees Estilos de texto personalizados Un estilo por línea. Formato: Nombre: estilo A-Z a-z 0-9 (52 o 62 caracteres). Espera más corta para dictados largos + Diccionario personal + Expansor de texto diff --git a/app/src/main/res/values-et/strings.xml b/app/src/main/res/values-et/strings.xml index 631a6ae0f..34d07d44b 100644 --- a/app/src/main/res/values-et/strings.xml +++ b/app/src/main/res/values-et/strings.xml @@ -1748,4 +1748,6 @@ Võimaldab seadmesisese GGUF / llama.cpp järelduse korrektuuri ja ümbersõnast Kohandatud tekstistiilid Üks stiil rea kohta. Vorming: Nimi: stiilis A-Z a-z 0-9 (52 või 62 tähemärki). Lühem ootamine pikkade diktaatide jaoks + Isiklik sõnastik + Tekstilaiend diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 7ed5277b1..28dcfc3b1 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -1769,4 +1769,6 @@ Permet l\'inférence GGUF / llama.cpp sur l\'appareil pour la correction et la r Styles de texte personnalisés Un style par ligne. Format : Nom : stylisé A-Z a-z 0-9 (52 ou 62 caractères). Attente plus courte pour les longues dictées + Dictionnaire personnel + Extenseur de texte diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml index 4670fef36..483ef7044 100644 --- a/app/src/main/res/values-hi/strings.xml +++ b/app/src/main/res/values-hi/strings.xml @@ -1740,4 +1740,6 @@ कस्टम पाठ शैलियाँ प्रति पंक्ति एक शैली. प्रारूप: नाम: शैली A-Z a-z 0-9 (52 या 62 अक्षर)। लंबे श्रुतलेखों के लिए कम प्रतीक्षा + व्यक्तिगत शब्दकोश + पाठ विस्तारक diff --git a/app/src/main/res/values-hr/strings.xml b/app/src/main/res/values-hr/strings.xml index aac4925ae..ced5914e4 100644 --- a/app/src/main/res/values-hr/strings.xml +++ b/app/src/main/res/values-hr/strings.xml @@ -1711,4 +1711,6 @@ Omogućuje lokalno GGUF / llama.cpp zaključivanje na uređaju za lekturu i prep Prilagođeni stilovi teksta Jedan stil po retku. Format: Ime: stilizirano A-Z a-z 0-9 (52 ili 62 znaka). Kraće čekanje za duge diktate + Osobni rječnik + Text Expander diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index 4cbd4b9ff..e3c6ee28f 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -1711,4 +1711,6 @@ Lehetővé teszi az eszközön futó GGUF / llama.cpp következtetést a javít Egyedi szövegstílusok Soronként egy stílus. Formátum: Név: A-Z a-z 0-9 stílusban (52 vagy 62 karakter). Rövidebb várakozás a hosszú diktálásokra + Személyes szótár + Szövegbővítő diff --git a/app/src/main/res/values-in/strings.xml b/app/src/main/res/values-in/strings.xml index 33e497975..4b1112315 100644 --- a/app/src/main/res/values-in/strings.xml +++ b/app/src/main/res/values-in/strings.xml @@ -1711,4 +1711,6 @@ Memungkinkan inferensi GGUF / llama.cpp di perangkat untuk koreksi teks dan penu Gaya teks khusus Satu gaya per baris. Format: Nama: diberi gaya A-Z a-z 0-9 (52 atau 62 karakter). Penantian yang lebih singkat untuk dikte yang panjang + Kamus Pribadi + Ekspander Teks diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 6edc373b3..30f4866fa 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -1780,4 +1780,6 @@ Abilita l\'inferenza GGUF / llama.cpp sul dispositivo per correzione e riscrittu Stili di testo personalizzati Uno stile per riga. Formato: Nome: in stile A-Z a-z 0-9 (52 o 62 caratteri). Attesa più breve per dettati lunghi + Dizionario personale + Espansore di testo diff --git a/app/src/main/res/values-iw/strings.xml b/app/src/main/res/values-iw/strings.xml index 7f0fe463d..b7a9fb63a 100644 --- a/app/src/main/res/values-iw/strings.xml +++ b/app/src/main/res/values-iw/strings.xml @@ -1719,4 +1719,6 @@ סגנונות טקסט מותאמים אישית סגנון אחד בכל שורה. פורמט: שם: בסגנון A-Z a-z 0-9 (52 או 62 תווים). המתנה קצרה יותר לתכתיבים ארוכים + מילון אישי + מרחיב טקסט diff --git a/app/src/main/res/values-lt/strings.xml b/app/src/main/res/values-lt/strings.xml index c33b2f9b3..d0b84c32e 100644 --- a/app/src/main/res/values-lt/strings.xml +++ b/app/src/main/res/values-lt/strings.xml @@ -1749,4 +1749,6 @@ Ar norite atnaujinti? Pasirinktiniai teksto stiliai Vienas stilius eilutėje. Formatas: pavadinimas: stilius A-Z a-z 0-9 (52 arba 62 simboliai). Trumpesnis laukimas ilgiems diktantams + Asmeninis žodynas + Teksto plėtiklis diff --git a/app/src/main/res/values-ml/strings.xml b/app/src/main/res/values-ml/strings.xml index d4d90130d..ebdf41563 100644 --- a/app/src/main/res/values-ml/strings.xml +++ b/app/src/main/res/values-ml/strings.xml @@ -1814,4 +1814,6 @@ ഇഷ്ടാനുസൃത ടെക്സ്റ്റ് ശൈലികൾ ഓരോ വരിയിലും ഒരു ശൈലി. ഫോർമാറ്റ്: പേര്: ശൈലിയിലുള്ള A-Z a-z 0-9 (52 അല്ലെങ്കിൽ 62 പ്രതീകങ്ങൾ). ദൈർഘ്യമേറിയ നിർദ്ദേശങ്ങൾക്കായി ഹ്രസ്വ കാത്തിരിപ്പ് + വ്യക്തിഗത നിഘണ്ടു + ടെക്സ്റ്റ് എക്സ്പാൻഡർ diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 6de67101e..f19ac0dd9 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -1755,4 +1755,6 @@ Maakt GGUF / llama.cpp-inferentie op het apparaat mogelijk voor proeflezen en he Aangepaste tekststijlen Eén stijl per regel. Formaat: Naam: opgemaakt A-Z a-z 0-9 (52 of 62 tekens). Kortere wachttijd voor lange dictaten + Persoonlijk woordenboek + Tekstuitbreider diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 0b7e7ff3a..f80ad3dce 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -1755,4 +1755,6 @@ Umożliwia lokalne wnioskowanie GGUF / llama.cpp na urządzeniu w celu korekty i Niestandardowe style tekstu Jeden styl na linię. Format: Nazwa: stylizowana na A-Z a-z 0-9 (52 lub 62 znaki). Krótsze oczekiwanie na długie dyktanda + Słownik osobisty + Ekspander tekstu diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index dcc847454..3c591438d 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -1718,4 +1718,6 @@ Habilita inferência GGUF / llama.cpp no dispositivo para revisão e reescrita.< Estilos de texto personalizados Um estilo por linha. Formato: Nome: estilo A-Z a-z 0-9 (52 ou 62 caracteres). Espera mais curta para ditados longos + Dicionário Pessoal + Expansor de texto diff --git a/app/src/main/res/values-pt-rPT/strings.xml b/app/src/main/res/values-pt-rPT/strings.xml index 835080767..ea27e69f8 100644 --- a/app/src/main/res/values-pt-rPT/strings.xml +++ b/app/src/main/res/values-pt-rPT/strings.xml @@ -1710,4 +1710,6 @@ Habilita inferência GGUF / llama.cpp no dispositivo para revisão e reescrita.< Estilos de texto personalizados Um estilo por linha. Formato: Nome: estilo A-Z a-z 0-9 (52 ou 62 caracteres). Espera mais curta para ditados longos + Dicionário Pessoal + Expansor de texto diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 9380223c1..a6c5ea8ee 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -1771,4 +1771,6 @@ Habilita inferência GGUF / llama.cpp no dispositivo para revisão e reescrita.< Estilos de texto personalizados Um estilo por linha. Formato: Nome: estilo A-Z a-z 0-9 (52 ou 62 caracteres). Espera mais curta para ditados longos + Dicionário Pessoal + Expansor de texto diff --git a/app/src/main/res/values-ro/strings.xml b/app/src/main/res/values-ro/strings.xml index 899ad980a..8602fe557 100644 --- a/app/src/main/res/values-ro/strings.xml +++ b/app/src/main/res/values-ro/strings.xml @@ -1748,4 +1748,6 @@ Activează inferența GGUF / llama.cpp pe dispozitiv pentru corectură și refor Stiluri de text personalizate Un stil pe linie. Format: Nume: stil A-Z a-z 0-9 (52 sau 62 de caractere). Așteptați mai scurt pentru dictate lungi + Dicţionar personal + Text Expander diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index a87697c59..d2734ccdc 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -1773,4 +1773,6 @@ Пользовательские стили текста Один стиль в строке. Формат: Имя: в стиле A–Z a–z 0–9 (52 или 62 символа). Короткое ожидание длинных диктантов + Личный словарь + Расширитель текста diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml index 85feeed5b..d262af428 100644 --- a/app/src/main/res/values-tr/strings.xml +++ b/app/src/main/res/values-tr/strings.xml @@ -1740,4 +1740,6 @@ Metin düzeltme ve yeniden yazma için cihaz üzerinde GGUF / llama.cpp çıkar Özel metin stilleri Her satıra bir stil. Biçim: Ad: A-Z a-z 0-9 stilinde (52 veya 62 karakter). Uzun dikteler için daha kısa bekleme süresi + Kişisel Sözlük + Metin Genişletici diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index 4835fb295..8b1437b01 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -1713,4 +1713,6 @@ Власні стилі тексту Один стиль на рядок. Формат: ім’я: стилі A-Z a-z 0-9 (52 або 62 символи). Коротше очікування для довгих диктантів + Особистий словник + Розширювач тексту diff --git a/app/src/main/res/values-ur/strings.xml b/app/src/main/res/values-ur/strings.xml index f1a95c622..af1916fed 100644 --- a/app/src/main/res/values-ur/strings.xml +++ b/app/src/main/res/values-ur/strings.xml @@ -1805,4 +1805,6 @@ حسب ضرورت ٹیکسٹ اسٹائل فی لائن ایک انداز۔ فارمیٹ: نام: اسٹائل شدہ A-Z a-z 0-9 (52 یا 62 حروف)۔ طویل ڈکٹیشن کے لیے مختصر انتظار + ذاتی لغت + ٹیکسٹ ایکسپینڈر diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index abf5bfd24..6439e0f94 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -1714,4 +1714,6 @@ 自定义文本样式 每行一种样式。格式:名称:样式为 A-Z a-z 0-9(52 或 62 个字符)。 缩短长时间听写的等待时间 + 个人词典 + 文本扩展器 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1489b4220..729e76a4b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -715,6 +715,8 @@ Layouts Theme & Custom Backgrounds Dictionaries + Personal Dictionary + Text Expander Typing History API Keys Clipboard History diff --git a/app/src/test/java/helium314/keyboard/latin/utils/PersonalDictionaryTransferTest.kt b/app/src/test/java/helium314/keyboard/latin/utils/PersonalDictionaryTransferTest.kt new file mode 100644 index 000000000..bbc248325 --- /dev/null +++ b/app/src/test/java/helium314/keyboard/latin/utils/PersonalDictionaryTransferTest.kt @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-3.0-only +package helium314.keyboard.latin.utils + +import helium314.keyboard.latin.utils.PersonalDictionaryTransfer.Entry +import java.io.StringWriter +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class PersonalDictionaryTransferTest { + private fun roundTrip(entries: List): List { + val out = StringWriter() + assertEquals(entries.size, PersonalDictionaryTransfer.write(entries.asSequence(), out)) + return PersonalDictionaryTransfer.parse(out.toString().reader().buffered()) + } + + @Test fun `entries survive a round trip`() { + val entries = listOf( + Entry("LeanType", null, null), + Entry("omw", "on my way", "en_US"), + Entry("grüß", null, "de_DE"), + ) + assertEquals(entries, roundTrip(entries)) + } + + @Test fun `locale as stored is kept, language tags are converted`() { + assertEquals("en_US", PersonalDictionaryTransfer.normalizeLocale("en_US")) + assertEquals("en_US", PersonalDictionaryTransfer.normalizeLocale("en-US")) + assertEquals("de", PersonalDictionaryTransfer.normalizeLocale("de")) + assertNull(PersonalDictionaryTransfer.normalizeLocale("")) + assertNull(PersonalDictionaryTransfer.normalizeLocale("all")) + assertNull(PersonalDictionaryTransfer.normalizeLocale(null)) + } + + @Test fun `gboard export with other column order is read`() { + val text = "# Gboard Dictionary format:shortcut\tword\tlanguage_tag\n" + + "brb\tbe right back\ten-US\n" + + "\tplain\t\n" + val entries = PersonalDictionaryTransfer.parse(text.reader().buffered()) + assertEquals(listOf(Entry("be right back", "brb", "en_US"), Entry("plain", null, null)), entries) + } + + @Test fun `blank lines and comments are skipped`() { + val text = "# comment\n\nword\t\t\n \t\t\n" + assertEquals(listOf(Entry("word", null, null)), PersonalDictionaryTransfer.parse(text.reader().buffered())) + } +} diff --git a/app/src/test/java/helium314/keyboard/settings/BackupPreferencesTest.kt b/app/src/test/java/helium314/keyboard/settings/BackupPreferencesTest.kt index a75a97bf6..4f0ae66a2 100644 --- a/app/src/test/java/helium314/keyboard/settings/BackupPreferencesTest.kt +++ b/app/src/test/java/helium314/keyboard/settings/BackupPreferencesTest.kt @@ -12,6 +12,7 @@ import helium314.keyboard.latin.utils.protectedPrefs import helium314.keyboard.settings.preferences.BackupCategory import helium314.keyboard.settings.preferences.backupFilePatterns import helium314.keyboard.settings.preferences.getCategoryForFilePath +import helium314.keyboard.settings.preferences.getCategoryForPrefKey import helium314.keyboard.settings.preferences.restoreMainPreferences import java.io.File import kotlinx.serialization.json.Json @@ -84,11 +85,26 @@ class BackupPreferencesTest { assertTrue(prefs.edit().putString("theme_style", "keep").putString("pref_text_expander_data", "old").commit()) restoreMainPreferences(context, lines(mapOf("pref_text_expander_data" to "{}")), lines(emptyMap()), - setOf(BackupCategory.DICTIONARIES)) + setOf(BackupCategory.TEXT_EXPANDER)) assertEquals("keep", prefs.getString("theme_style", null)) assertEquals("{}", prefs.getString(TextExpanderUtils.PREF_DATA, null)) } + @Test + fun dictionariesDoNotRestoreTextExpander() { + restoreMainPreferences(context, lines(mapOf(TextExpanderUtils.PREF_DATA to "{}")), + null, setOf(BackupCategory.DICTIONARIES, BackupCategory.PERSONAL_DICTIONARY)) + assertFalse(prefs.contains(TextExpanderUtils.PREF_DATA)) + } + + @Test + fun dictionaryFilesAndSettingsStayInDictionaries() { + assertEquals(BackupCategory.DICTIONARIES, getCategoryForPrefKey("block_potentially_offensive")) + assertEquals(BackupCategory.DICTIONARIES, getCategoryForFilePath("dicts${File.separator}en${File.separator}main_en.dict")) + assertEquals(BackupCategory.DICTIONARIES, getCategoryForFilePath("blacklists${File.separator}en.txt")) + assertEquals(BackupCategory.TEXT_EXPANDER, getCategoryForPrefKey(TextExpanderUtils.PREF_ENABLED)) + } + @Test fun generalSettingsDoNotRestoreTextExpander() { restoreMainPreferences(context, lines(mapOf(TextExpanderUtils.PREF_DATA to "{}")), @@ -151,11 +167,11 @@ class BackupPreferencesTest { } @Test - fun textExpanderImagesUseDictionariesCategoryAndMatchBackupPattern() { + fun textExpanderImagesUseTextExpanderCategoryAndMatchBackupPattern() { val samplePath = "text_expander_images${File.separator}img_1700000000_abcd1234.png" val tempPath = "text_expander_images${File.separator}pending_1234.tmp" - assertEquals(BackupCategory.DICTIONARIES, getCategoryForFilePath("text_expander_images/img_1700000000_abcd1234.png")) + assertEquals(BackupCategory.TEXT_EXPANDER, getCategoryForFilePath("text_expander_images/img_1700000000_abcd1234.png")) assertTrue(backupFilePatterns.any { it.matches(samplePath) }) assertFalse(backupFilePatterns.any { it.matches(tempPath) }) } diff --git a/docs/FEATURES.md b/docs/FEATURES.md index 45af076ba..53d82ec80 100644 --- a/docs/FEATURES.md +++ b/docs/FEATURES.md @@ -1052,9 +1052,10 @@ Safeguard your personalized keyboard configuration, custom layouts, and linguist - **Modular Data Selection**: Choose exactly which components to include in your backup archive: - Keyboard Settings & Custom Preferences. - Custom Keyboard Layouts (`.json` and `.txt` files). - - Custom Dictionaries & Wordlists (user-added words). - - Learned Typing Prediction History & Frequency Tables. - - Quick Text Expander Shortcuts & Dynamic Templates. + - **Dictionaries**: imported dictionary files and blocked words. + - **Personal Dictionary**: the words you added yourself or that were added automatically (stored in Android's user dictionary). On restore, words that are already on the device are kept and only the missing ones are added. + - **Text Expander**: shortcuts, templates and attached images. + - Learned Typing Prediction History & Frequency Tables (**Typing History**). - **Password-Protected Encrypted API Keys**: - Securely export third-party cloud AI and media API keys (Gemini, Groq, OpenAI, GIPHY, KLIPY) using robust AES-256 encryption. - Backups can be safely shared across devices or saved to cloud storage without risking credential leakage. Decryption occurs only when the correct user password is provided during restoration.