Merge "Refactoring: cut out a method for caching a word list"
This commit is contained in:
commit
8b21eb2507
2 changed files with 44 additions and 34 deletions
|
@ -63,10 +63,10 @@ public class BinaryDictionaryFileDumper {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries a content provider for the list of dictionaries for a specific locale
|
* Queries a content provider for the list of word lists for a specific locale
|
||||||
* available to copy into Latin IME.
|
* available to copy into Latin IME.
|
||||||
*/
|
*/
|
||||||
private static List<String> getDictIdList(final Locale locale, final Context context) {
|
private static List<String> getWordListIds(final Locale locale, final Context context) {
|
||||||
final ContentResolver resolver = context.getContentResolver();
|
final ContentResolver resolver = context.getContentResolver();
|
||||||
final Uri dictionaryPackUri = getProviderUri(locale.toString());
|
final Uri dictionaryPackUri = getProviderUri(locale.toString());
|
||||||
|
|
||||||
|
@ -88,41 +88,51 @@ public class BinaryDictionaryFileDumper {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries a content provider for dictionary data for some locale and cache the returned files
|
* Caches a word list the id of which is passed as an argument.
|
||||||
|
*/
|
||||||
|
private static AssetFileAddress cacheWordList(final String id, final Locale locale,
|
||||||
|
final ContentResolver resolver, final Context context) {
|
||||||
|
final Uri wordListUri = getProviderUri(id);
|
||||||
|
try {
|
||||||
|
final AssetFileDescriptor afd = resolver.openAssetFileDescriptor(wordListUri, "r");
|
||||||
|
if (null == afd) return null;
|
||||||
|
final String fileName = copyFileTo(afd.createInputStream(),
|
||||||
|
BinaryDictionaryGetter.getCacheFileName(id, locale, context));
|
||||||
|
afd.close();
|
||||||
|
if (0 >= resolver.delete(wordListUri, null, null)) {
|
||||||
|
// I'd rather not print the word list ID to the log out of security concerns
|
||||||
|
Log.e(TAG, "Could not have the dictionary pack delete a word list");
|
||||||
|
}
|
||||||
|
return AssetFileAddress.makeFromFileName(fileName);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// This may only come from openAssetFileDescriptor
|
||||||
|
return null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Can't read the file for some reason.
|
||||||
|
Log.e(TAG, "Cannot read a word list from the dictionary pack : " + e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queries a content provider for word list data for some locale and cache the returned files
|
||||||
*
|
*
|
||||||
* This will query a content provider for dictionary data for a given locale, and copy the
|
* This will query a content provider for word list data for a given locale, and copy the
|
||||||
* files locally so that they can be mmap'ed. This may overwrite previously cached dictionaries
|
* files locally so that they can be mmap'ed. This may overwrite previously cached word lists
|
||||||
* with newer versions if a newer version is made available by the content provider.
|
* with newer versions if a newer version is made available by the content provider.
|
||||||
* @returns the addresses of the files, or null if no data could be obtained.
|
* @returns the addresses of the word list files, or null if no data could be obtained.
|
||||||
* @throw FileNotFoundException if the provider returns non-existent data.
|
* @throw FileNotFoundException if the provider returns non-existent data.
|
||||||
* @throw IOException if the provider-returned data could not be read.
|
* @throw IOException if the provider-returned data could not be read.
|
||||||
*/
|
*/
|
||||||
public static List<AssetFileAddress> cacheDictionariesFromContentProvider(final Locale locale,
|
public static List<AssetFileAddress> cacheWordListsFromContentProvider(final Locale locale,
|
||||||
final Context context) {
|
final Context context) {
|
||||||
final ContentResolver resolver = context.getContentResolver();
|
final ContentResolver resolver = context.getContentResolver();
|
||||||
final List<String> idList = getDictIdList(locale, context);
|
final List<String> idList = getWordListIds(locale, context);
|
||||||
final List<AssetFileAddress> fileAddressList = new ArrayList<AssetFileAddress>();
|
final List<AssetFileAddress> fileAddressList = new ArrayList<AssetFileAddress>();
|
||||||
for (String id : idList) {
|
for (String id : idList) {
|
||||||
final Uri wordListUri = getProviderUri(id);
|
final AssetFileAddress afd = cacheWordList(id, locale, resolver, context);
|
||||||
AssetFileDescriptor afd = null;
|
if (null != afd) {
|
||||||
try {
|
fileAddressList.add(afd);
|
||||||
afd = resolver.openAssetFileDescriptor(wordListUri, "r");
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
// leave null inside afd and continue
|
|
||||||
}
|
|
||||||
if (null == afd) continue;
|
|
||||||
try {
|
|
||||||
final String fileName = copyFileTo(afd.createInputStream(),
|
|
||||||
BinaryDictionaryGetter.getCacheFileName(id, locale, context));
|
|
||||||
afd.close();
|
|
||||||
if (0 >= resolver.delete(wordListUri, null, null)) {
|
|
||||||
// I'd rather not print the word list ID to the log out of security concerns
|
|
||||||
Log.e(TAG, "Could not have the dictionary pack delete a word list");
|
|
||||||
}
|
|
||||||
fileAddressList.add(AssetFileAddress.makeFromFileName(fileName));
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Can't read the file for some reason. Continue onto the next file.
|
|
||||||
Log.e(TAG, "Cannot read a word list from the dictionary pack : " + e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fileAddressList;
|
return fileAddressList;
|
||||||
|
|
|
@ -205,7 +205,7 @@ class BinaryDictionaryGetter {
|
||||||
* @param context the context on which to open the files upon.
|
* @param context the context on which to open the files upon.
|
||||||
* @return an array of binary dictionary files, which may be empty but may not be null.
|
* @return an array of binary dictionary files, which may be empty but may not be null.
|
||||||
*/
|
*/
|
||||||
private static File[] getCachedDictionaryList(final Locale locale,
|
private static File[] getCachedWordLists(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();
|
||||||
|
@ -235,11 +235,11 @@ class BinaryDictionaryGetter {
|
||||||
public static List<AssetFileAddress> getDictionaryFiles(final Locale locale,
|
public static List<AssetFileAddress> getDictionaryFiles(final Locale locale,
|
||||||
final Context context, final int fallbackResId) {
|
final Context context, final int fallbackResId) {
|
||||||
|
|
||||||
// cacheDictionariesFromContentProvider returns the list of files it copied to local
|
// cacheWordListsFromContentProvider returns the list of files it copied to local
|
||||||
// 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.cacheWordListsFromContentProvider(locale, context);
|
||||||
final File[] cachedDictionaryList = getCachedDictionaryList(locale, context);
|
final File[] cachedWordLists = getCachedWordLists(locale, context);
|
||||||
|
|
||||||
final String mainDictId = getMainDictId(locale);
|
final String mainDictId = getMainDictId(locale);
|
||||||
|
|
||||||
|
@ -247,8 +247,8 @@ class BinaryDictionaryGetter {
|
||||||
|
|
||||||
boolean foundMainDict = false;
|
boolean foundMainDict = false;
|
||||||
final ArrayList<AssetFileAddress> fileList = new ArrayList<AssetFileAddress>();
|
final ArrayList<AssetFileAddress> fileList = new ArrayList<AssetFileAddress>();
|
||||||
// cachedDictionaryList may not be null, see doc for getCachedDictionaryList
|
// cachedWordLists may not be null, see doc for getCachedDictionaryList
|
||||||
for (final File f : cachedDictionaryList) {
|
for (final File f : cachedWordLists) {
|
||||||
final String wordListId = getWordListIdFromFileName(f.getName());
|
final String wordListId = getWordListIdFromFileName(f.getName());
|
||||||
if (wordListId.equals(mainDictId)) {
|
if (wordListId.equals(mainDictId)) {
|
||||||
foundMainDict = true;
|
foundMainDict = true;
|
||||||
|
|
Loading…
Reference in a new issue