From 7b1f74bb9ddae952f4da6c8d9bbb0057984b0988 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 24 Aug 2011 12:45:52 +0900 Subject: [PATCH] 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 --- .../latin/BinaryDictionaryFileDumper.java | 66 +++++++++++-------- .../latin/BinaryDictionaryGetter.java | 12 ++-- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index ed5f83b3b..1f756eafb 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -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. */ - private static List getDictIdList(final Locale locale, final Context context) { + private static List getWordListIds(final Locale locale, final Context context) { final ContentResolver resolver = context.getContentResolver(); 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 - * files locally so that they can be mmap'ed. This may overwrite previously cached dictionaries + * 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 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 IOException if the provider-returned data could not be read. */ - public static List cacheDictionariesFromContentProvider(final Locale locale, + public static List cacheWordListsFromContentProvider(final Locale locale, final Context context) { final ContentResolver resolver = context.getContentResolver(); - final List idList = getDictIdList(locale, context); + final List idList = getWordListIds(locale, context); final List fileAddressList = new ArrayList(); for (String id : idList) { - 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 { - 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); + final AssetFileAddress afd = cacheWordList(id, locale, resolver, context); + if (null != afd) { + fileAddressList.add(afd); } } return fileAddressList; diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 5d2dab0a9..38344300c 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -205,7 +205,7 @@ class BinaryDictionaryGetter { * @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. */ - private static File[] getCachedDictionaryList(final Locale locale, + private static File[] getCachedWordLists(final Locale locale, final Context context) { final String directoryName = getCacheDirectoryForLocale(locale, context); final File[] cacheFiles = new File(directoryName).listFiles(); @@ -235,11 +235,11 @@ class BinaryDictionaryGetter { public static List getDictionaryFiles(final Locale locale, 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 // list of everything we ever cached, so we ignore the return value. - BinaryDictionaryFileDumper.cacheDictionariesFromContentProvider(locale, context); - final File[] cachedDictionaryList = getCachedDictionaryList(locale, context); + BinaryDictionaryFileDumper.cacheWordListsFromContentProvider(locale, context); + final File[] cachedWordLists = getCachedWordLists(locale, context); final String mainDictId = getMainDictId(locale); @@ -247,8 +247,8 @@ class BinaryDictionaryGetter { boolean foundMainDict = false; final ArrayList fileList = new ArrayList(); - // cachedDictionaryList may not be null, see doc for getCachedDictionaryList - for (final File f : cachedDictionaryList) { + // cachedWordLists may not be null, see doc for getCachedDictionaryList + for (final File f : cachedWordLists) { final String wordListId = getWordListIdFromFileName(f.getName()); if (wordListId.equals(mainDictId)) { foundMainDict = true;