Refactoring: cut out a method for caching a word list

This is preparation to have the decrypting/unzipping code
moved over to LatinIME.

Bug: 5095140
Change-Id: Ic3fdcc3de673b46cef2eb9ebe6a52cbdd614e50a
This commit is contained in:
Jean Chalard 2011-08-24 12:45:52 +09:00
parent f77009ac3a
commit 7b1f74bb9d
2 changed files with 44 additions and 34 deletions

View file

@ -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,30 +88,14 @@ 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.
*
* This will query a content provider for dictionary data for a given locale, and copy the
* files locally so that they can be mmap'ed. This may overwrite previously cached dictionaries
* 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.
* @throw FileNotFoundException if the provider returns non-existent data.
* @throw IOException if the provider-returned data could not be read.
*/ */
public static List<AssetFileAddress> cacheDictionariesFromContentProvider(final Locale locale, private static AssetFileAddress cacheWordList(final String id, final Locale locale,
final Context context) { final ContentResolver resolver, final Context context) {
final ContentResolver resolver = context.getContentResolver();
final List<String> idList = getDictIdList(locale, context);
final List<AssetFileAddress> fileAddressList = new ArrayList<AssetFileAddress>();
for (String id : idList) {
final Uri wordListUri = getProviderUri(id); final Uri wordListUri = getProviderUri(id);
AssetFileDescriptor afd = null;
try {
afd = resolver.openAssetFileDescriptor(wordListUri, "r");
} catch (FileNotFoundException e) {
// leave null inside afd and continue
}
if (null == afd) continue;
try { try {
final AssetFileDescriptor afd = resolver.openAssetFileDescriptor(wordListUri, "r");
if (null == afd) return null;
final String fileName = copyFileTo(afd.createInputStream(), final String fileName = copyFileTo(afd.createInputStream(),
BinaryDictionaryGetter.getCacheFileName(id, locale, context)); BinaryDictionaryGetter.getCacheFileName(id, locale, context));
afd.close(); afd.close();
@ -119,11 +103,37 @@ public class BinaryDictionaryFileDumper {
// I'd rather not print the word list ID to the log out of security concerns // 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"); Log.e(TAG, "Could not have the dictionary pack delete a word list");
} }
fileAddressList.add(AssetFileAddress.makeFromFileName(fileName)); return AssetFileAddress.makeFromFileName(fileName);
} catch (FileNotFoundException e) {
// This may only come from openAssetFileDescriptor
return null;
} catch (IOException e) { } catch (IOException e) {
// Can't read the file for some reason. Continue onto the next file. // Can't read the file for some reason.
Log.e(TAG, "Cannot read a word list from the dictionary pack : " + e); 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 word list data for a given locale, and copy the
* 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.
* @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 IOException if the provider-returned data could not be read.
*/
public static List<AssetFileAddress> cacheWordListsFromContentProvider(final Locale locale,
final Context context) {
final ContentResolver resolver = context.getContentResolver();
final List<String> idList = getWordListIds(locale, context);
final List<AssetFileAddress> fileAddressList = new ArrayList<AssetFileAddress>();
for (String id : idList) {
final AssetFileAddress afd = cacheWordList(id, locale, resolver, context);
if (null != afd) {
fileAddressList.add(afd);
}
} }
return fileAddressList; return fileAddressList;
} }

View file

@ -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;