avoid adding null dictionaries, see #293

This commit is contained in:
Helium314 2023-11-27 14:50:36 +01:00
parent 57bf742da0
commit 2b8c62cb02

View file

@ -190,7 +190,7 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
new ConcurrentHashMap<>(); new ConcurrentHashMap<>();
public DictionaryGroup() { public DictionaryGroup() {
this(new Locale(""), null /* mainDict */, null /* account */, Collections.emptyMap() /* subDicts */); this(new Locale(""), null, null, Collections.emptyMap());
} }
public DictionaryGroup(@NonNull final Locale locale, public DictionaryGroup(@NonNull final Locale locale,
@ -313,16 +313,15 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
private static ExpandableBinaryDictionary getSubDict(final String dictType, private static ExpandableBinaryDictionary getSubDict(final String dictType,
final Context context, final Locale locale, final File dictFile, final Context context, final Locale locale, final File dictFile,
final String dictNamePrefix, @Nullable final String account) { final String dictNamePrefix, @Nullable final String account) {
final Class<? extends ExpandableBinaryDictionary> dictClass = final Class<? extends ExpandableBinaryDictionary> dictClass = DICT_TYPE_TO_CLASS.get(dictType);
DICT_TYPE_TO_CLASS.get(dictType);
if (dictClass == null) { if (dictClass == null) {
Log.e(TAG, "Cannot create dictionary: no class for " + dictType);
return null; return null;
} }
try { try {
final Method factoryMethod = dictClass.getMethod(DICT_FACTORY_METHOD_NAME, final Method factoryMethod = dictClass.getMethod(DICT_FACTORY_METHOD_NAME,
DICT_FACTORY_METHOD_ARG_TYPES); DICT_FACTORY_METHOD_ARG_TYPES);
final Object dict = factoryMethod.invoke(null /* obj */, final Object dict = factoryMethod.invoke(null, context, locale, dictFile, dictNamePrefix, account);
context, locale, dictFile, dictNamePrefix, account);
return (ExpandableBinaryDictionary) dict; return (ExpandableBinaryDictionary) dict;
} catch (final NoSuchMethodException | SecurityException | IllegalAccessException } catch (final NoSuchMethodException | SecurityException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) { | IllegalArgumentException | InvocationTargetException e) {
@ -356,15 +355,14 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
// TODO: Make subDictTypesToUse configurable by resource or a static final list. // TODO: Make subDictTypesToUse configurable by resource or a static final list.
final HashSet<String> subDictTypesToUse = new HashSet<>(); final HashSet<String> subDictTypesToUse = new HashSet<>();
subDictTypesToUse.add(Dictionary.TYPE_USER); subDictTypesToUse.add(Dictionary.TYPE_USER);
final List<Locale> allLocales = new ArrayList<Locale>() {{ final List<Locale> allLocales = new ArrayList<>() {{
add(newLocale); add(newLocale);
addAll(Settings.getInstance().getCurrent().mSecondaryLocales); addAll(Settings.getInstance().getCurrent().mSecondaryLocales);
}}; }};
// Do not use contacts dictionary if we do not have permissions to read contacts. // Do not use contacts dictionary if we do not have permissions to read contacts.
final boolean contactsPermissionGranted = PermissionsUtil.checkAllPermissionsGranted( if (useContactsDict
context, Manifest.permission.READ_CONTACTS); && PermissionsUtil.checkAllPermissionsGranted(context, Manifest.permission.READ_CONTACTS)) {
if (useContactsDict && contactsPermissionGranted) {
subDictTypesToUse.add(Dictionary.TYPE_CONTACTS); subDictTypesToUse.add(Dictionary.TYPE_CONTACTS);
} }
if (usePersonalizedDicts) { if (usePersonalizedDicts) {
@ -411,7 +409,8 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
if (noExistingDictsForThisLocale || forceReloadMainDictionary if (noExistingDictsForThisLocale || forceReloadMainDictionary
|| !oldDictionaryGroupForLocale.hasDict(subDictType, account)) { || !oldDictionaryGroupForLocale.hasDict(subDictType, account)) {
// Create a new dictionary. // Create a new dictionary.
subDict = getSubDict(subDictType, context, locale, null /* dictFile */, dictNamePrefix, account); subDict = getSubDict(subDictType, context, locale, null, dictNamePrefix, account);
if (subDict == null) continue; // https://github.com/Helium314/openboard/issues/293
} else { } else {
// Reuse the existing dictionary, and don't close it at the end // Reuse the existing dictionary, and don't close it at the end
subDict = oldDictionaryGroupForLocale.getSubDict(subDictType); subDict = oldDictionaryGroupForLocale.getSubDict(subDictType);
@ -528,18 +527,16 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
for (final String dictType : dictionaryTypes) { for (final String dictType : dictionaryTypes) {
if (dictType.equals(Dictionary.TYPE_MAIN)) { if (dictType.equals(Dictionary.TYPE_MAIN)) {
mainDictionary = DictionaryFactory.createMainDictionaryFromManager(context, mainDictionary = DictionaryFactory.createMainDictionaryFromManager(context, locale);
locale);
} else { } else {
final File dictFile = dictionaryFiles.get(dictType); final File dictFile = dictionaryFiles.get(dictType);
final ExpandableBinaryDictionary dict = getSubDict( final ExpandableBinaryDictionary dict = getSubDict(
dictType, context, locale, dictFile, "" /* dictNamePrefix */, account); dictType, context, locale, dictFile, "", account);
if (dict == null) { if (dict == null) {
throw new RuntimeException("Unknown dictionary type: " + dictType); throw new RuntimeException("Unknown dictionary type: " + dictType);
} }
if (additionalDictAttributes.containsKey(dictType)) { if (additionalDictAttributes.containsKey(dictType)) {
dict.clearAndFlushDictionaryWithAdditionalAttributes( dict.clearAndFlushDictionaryWithAdditionalAttributes(additionalDictAttributes.get(dictType));
additionalDictAttributes.get(dictType));
} }
dict.reloadDictionaryIfRequired(); dict.reloadDictionaryIfRequired();
dict.waitAllTasksForTests(); dict.waitAllTasksForTests();