remove unused extracted main dictionaries

This commit is contained in:
Helium314 2023-11-20 00:06:03 +01:00
parent e4bc6996a5
commit 7d2dad3862
6 changed files with 35 additions and 9 deletions

View file

@ -16,14 +16,10 @@ import android.util.Log;
import org.dslul.openboard.inputmethod.latin.common.FileUtils;
import org.dslul.openboard.inputmethod.latin.common.LocaleUtils;
import org.dslul.openboard.inputmethod.latin.define.DecoderSpecificConstants;
import org.dslul.openboard.inputmethod.latin.makedict.DictionaryHeader;
import org.dslul.openboard.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils;
import org.dslul.openboard.inputmethod.latin.utils.DictionaryInfoUtils;
import java.io.File;
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -278,8 +274,7 @@ final public class BinaryDictionaryGetter {
// we have a match, now copy contents of the dictionary to cached word lists folder
final String bestMatchLocale = extractLocaleFromAssetsDictionaryFile(bestMatchName);
if (bestMatchLocale == null) return null;
File dictFile = new File(DictionaryInfoUtils.getCacheDirectoryForLocale(locale, context) +
File.separator + DictionaryInfoUtils.getMainDictFilename(locale));
File dictFile = DictionaryInfoUtils.getMainDictFile(locale, context);
if (dictFile.exists())
return dictFile;
try {

View file

@ -26,7 +26,7 @@ import org.dslul.openboard.inputmethod.latin.utils.SubtypeLocaleUtils
import org.dslul.openboard.inputmethod.latin.utils.showMissingDictionaryDialog
import org.dslul.openboard.inputmethod.latin.utils.toLocale
class LanguageFilterListFakePreference(searchField: EditText, recyclerView: RecyclerView) {
class LanguageFilterList(searchField: EditText, recyclerView: RecyclerView) {
private val adapter = LanguageAdapter(emptyList(), recyclerView.context)
private val sortedSubtypes = mutableListOf<MutableList<SubtypeInfo>>()

View file

@ -31,7 +31,7 @@ class LanguageSettingsFragment : Fragment(R.layout.language_settings) {
private val sortedSubtypes = LinkedHashMap<String, MutableList<SubtypeInfo>>()
private val enabledSubtypes = mutableListOf<InputMethodSubtype>()
private val systemLocales = mutableListOf<Locale>()
private lateinit var languageFilterList: LanguageFilterListFakePreference
private lateinit var languageFilterList: LanguageFilterList
private lateinit var sharedPreferences: SharedPreferences
private lateinit var systemOnlySwitch: SwitchCompat
private val dictionaryLocales by lazy { getDictionaryLocales(requireContext()).mapTo(HashSet()) { it.languageConsideringZZ() } }
@ -66,7 +66,7 @@ class LanguageSettingsFragment : Fragment(R.layout.language_settings) {
enabledSubtypes.addAll(getEnabledSubtypes(sharedPreferences))
loadSubtypes(b)
}
languageFilterList = LanguageFilterListFakePreference(view.findViewById(R.id.search_field), view.findViewById(R.id.language_list))
languageFilterList = LanguageFilterList(view.findViewById(R.id.search_field), view.findViewById(R.id.language_list))
loadSubtypes(systemOnlySwitch.isChecked)
return view
}

View file

@ -33,6 +33,8 @@ import org.dslul.openboard.inputmethod.latin.R;
import org.dslul.openboard.inputmethod.latin.common.FileUtils;
import org.dslul.openboard.inputmethod.latin.utils.ApplicationUtils;
import org.dslul.openboard.inputmethod.latin.utils.DeviceProtectedUtils;
import org.dslul.openboard.inputmethod.latin.utils.DictionaryUtilsKt;
import org.dslul.openboard.inputmethod.latin.utils.ExecutorUtils;
import org.dslul.openboard.inputmethod.latin.utils.FeedbackUtils;
import org.dslul.openboard.inputmethod.latin.utils.JniUtils;
@ -71,6 +73,8 @@ public final class SettingsFragment extends PreferenceFragmentCompat {
final Preference gesturePreference = findPreference(Settings.SCREEN_GESTURE);
preferenceScreen.removePreference(gesturePreference);
}
ExecutorUtils.getBackgroundExecutor(ExecutorUtils.KEYBOARD)
.execute(() -> DictionaryUtilsKt.cleanUnusedMainDicts(requireContext()));
}
@Override

View file

@ -299,6 +299,11 @@ public class DictionaryInfoUtils {
return MAIN_DICT_PREFIX + locale.toLowerCase(Locale.ENGLISH) + ".dict";
}
public static File getMainDictFile(@NonNull final String locale, @NonNull final Context context) {
return new File(DictionaryInfoUtils.getCacheDirectoryForLocale(locale, context) +
File.separator + DictionaryInfoUtils.getMainDictFilename(locale));
}
@Nullable
public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file,
final long offset, final long length) {

View file

@ -11,6 +11,9 @@ import androidx.core.content.edit
import org.dslul.openboard.inputmethod.latin.BinaryDictionaryGetter
import org.dslul.openboard.inputmethod.latin.R
import org.dslul.openboard.inputmethod.latin.settings.Settings
import org.dslul.openboard.inputmethod.latin.settings.getEnabledSubtypes
import org.dslul.openboard.inputmethod.latin.settings.locale
import java.io.File
import java.util.*
import kotlin.collections.HashSet
@ -68,4 +71,23 @@ fun showMissingDictionaryDialog(context: Context, locale: Locale) {
(dialog.findViewById<View>(android.R.id.message) as? TextView)?.movementMethod = LinkMovementMethod.getInstance()
}
fun cleanUnusedMainDicts(context: Context) {
val dictionaryDir = File(DictionaryInfoUtils.getWordListCacheDirectory(context))
val dirs = dictionaryDir.listFiles() ?: return
val prefs = DeviceProtectedUtils.getSharedPreferences(context)
val usedLocales = hashSetOf<String>()
getEnabledSubtypes(prefs).forEach {
val locale = it.locale()
usedLocales.add(locale)
Settings.getSecondaryLocales(prefs, locale).forEach { usedLocales.add(it.toString().lowercase()) }
}
for (dir in dirs) {
if (!dir.isDirectory) continue
if (dir.name in usedLocales) continue
if (dir.listFiles()?.any { it.name != DictionaryInfoUtils.getMainDictFilename(dir.name) } != false)
continue
dir.deleteRecursively()
}
}
const val DICTIONARY_URL = "https://codeberg.org/Helium314/aosp-dictionaries"