Skip to content
Snippets Groups Projects
Commit 83207fb4 authored by Jean Chalard's avatar Jean Chalard
Browse files

Move the settings test to a more appropriate place.

This change refactors the dictionary selection code so that the
cached dictionary files list and the settings tests are more
cleanly separated.
This will also help with future refactorings that will test for
the presence of the main dictionary and insert the fall back if
it's not supplied by the dictionary pack.

Bug: 5095140
Change-Id: I8d7caad7c054031df71fe78b043801a774d50f65
parent 5ba5ff9b
No related branches found
No related tags found
Loading
...@@ -41,6 +41,11 @@ class BinaryDictionaryGetter { ...@@ -41,6 +41,11 @@ class BinaryDictionaryGetter {
*/ */
private static final String TAG = BinaryDictionaryGetter.class.getSimpleName(); private static final String TAG = BinaryDictionaryGetter.class.getSimpleName();
/**
* Used to return empty lists
*/
private static final File[] EMPTY_FILE_ARRAY = new File[0];
/** /**
* Name of the common preferences name to know which word list are on and which are off. * Name of the common preferences name to know which word list are on and which are off.
*/ */
...@@ -198,29 +203,14 @@ class BinaryDictionaryGetter { ...@@ -198,29 +203,14 @@ class BinaryDictionaryGetter {
* *
* @param locale the locale to find the dictionary files for. * @param locale the locale to find the dictionary files for.
* @param context the context on which to open the files upon. * @param context the context on which to open the files upon.
* @return a list of binary dictionary files, which may be null but may not be empty. * @return an array of binary dictionary files, which may be empty but may not be null.
*/ */
private static List<AssetFileAddress> getCachedDictionaryList(final Locale locale, private static File[] getCachedDictionaryList(final Locale locale,
final Context context) { final Context context) {
final String directoryName = getCacheDirectoryForLocale(locale, context); final String directoryName = getCacheDirectoryForLocale(locale, context);
final File[] cacheFiles = new File(directoryName).listFiles(); final File[] cacheFiles = new File(directoryName).listFiles();
// TODO: Never return null. Fallback on the built-in dictionary, and if that's if (null == cacheFiles) return EMPTY_FILE_ARRAY;
// not present or disabled, then return an empty list. return cacheFiles;
if (null == cacheFiles) return null;
final DictPackSettings dictPackSettings = new DictPackSettings(context);
final ArrayList<AssetFileAddress> fileList = new ArrayList<AssetFileAddress>();
for (File f : cacheFiles) {
final String wordListId = getWordListIdFromFileName(f.getName());
if (!dictPackSettings.isWordListActive(wordListId)) continue;
if (f.canRead()) {
fileList.add(AssetFileAddress.makeFromFileName(f.getPath()));
} else {
Log.e(TAG, "Found a cached dictionary file but cannot read it");
}
}
return fileList.size() > 0 ? fileList : null;
} }
/** /**
...@@ -242,10 +232,26 @@ class BinaryDictionaryGetter { ...@@ -242,10 +232,26 @@ class BinaryDictionaryGetter {
// storage, but we don't really care about what was copied NOW: what we want is the // storage, but we don't really care about what was copied NOW: what we want is the
// list of everything we ever cached, so we ignore the return value. // list of everything we ever cached, so we ignore the return value.
BinaryDictionaryFileDumper.cacheDictionariesFromContentProvider(locale, context); BinaryDictionaryFileDumper.cacheDictionariesFromContentProvider(locale, context);
List<AssetFileAddress> cachedDictionaryList = getCachedDictionaryList(locale, context); final File[] cachedDictionaryList = getCachedDictionaryList(locale, context);
if (null != cachedDictionaryList) {
return cachedDictionaryList; final DictPackSettings dictPackSettings = new DictPackSettings(context);
final ArrayList<AssetFileAddress> fileList = new ArrayList<AssetFileAddress>();
// cachedDictionaryList may not be null, see doc for getCachedDictionaryList
for (final File f : cachedDictionaryList) {
final String wordListId = getWordListIdFromFileName(f.getName());
if (!dictPackSettings.isWordListActive(wordListId)) continue;
if (f.canRead()) {
fileList.add(AssetFileAddress.makeFromFileName(f.getPath()));
} else {
Log.e(TAG, "Found a cached dictionary file but cannot read it");
}
}
if (!fileList.isEmpty()) {
return fileList;
} }
// If the list is empty, fall through and return the fallback
final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId, final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId,
locale); locale);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment