From 80e0bf04292867ddc769aca75ebaee817b95a941 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 16 Aug 2011 21:35:52 +0900 Subject: [PATCH] Exception refactoring Now that the dictionary pack can return several files, it's better to handle IO exceptions for each file rather than globally. This also will help with next implementation steps. Bug: 5095140 Change-Id: I5ed135ad2ad4f55f61f9b3f92c48a35d5c24bdb2 --- .../latin/BinaryDictionaryFileDumper.java | 31 ++++++++++++------- .../latin/BinaryDictionaryGetter.java | 25 ++++++--------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index 3da670e2e..ed5f83b3b 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -98,23 +98,32 @@ public class BinaryDictionaryFileDumper { * @throw IOException if the provider-returned data could not be read. */ public static List cacheDictionariesFromContentProvider(final Locale locale, - final Context context) throws FileNotFoundException, IOException { + final Context context) { final ContentResolver resolver = context.getContentResolver(); final List idList = getDictIdList(locale, context); final List fileAddressList = new ArrayList(); for (String id : idList) { final Uri wordListUri = getProviderUri(id); - final AssetFileDescriptor afd = - resolver.openAssetFileDescriptor(wordListUri, "r"); - if (null == afd) continue; - 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 here out of security concerns - Log.e(TAG, "Could not have the dictionary pack delete a word list"); + 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); } - fileAddressList.add(AssetFileAddress.makeFromFileName(fileName)); } return fileAddressList; } diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 360cf21ca..9b89b9acf 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -214,23 +214,16 @@ class BinaryDictionaryGetter { */ public static List getDictionaryFiles(final Locale locale, final Context context, final int fallbackResId) { - try { - // cacheDictionariesFromContentProvider 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); - List cachedDictionaryList = getCachedDictionaryList(locale, context); - if (null != cachedDictionaryList) { - return cachedDictionaryList; - } - // If the list is null, fall through and return the fallback - } catch (FileNotFoundException e) { - Log.e(TAG, "Unable to create dictionary file from provider for locale " - + locale.toString() + ": falling back to internal dictionary"); - } catch (IOException e) { - Log.e(TAG, "Unable to read source data for locale " - + locale.toString() + ": falling back to internal dictionary"); + + // cacheDictionariesFromContentProvider 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); + List cachedDictionaryList = getCachedDictionaryList(locale, context); + if (null != cachedDictionaryList) { + return cachedDictionaryList; } + final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId, locale); if (null == fallbackAsset) return null;