Read the dictionary resource in a more sensical place.

We don't need to pass this down all the way from LatinIME any more.
It fetched be done exactly where it needs to be.

Change-Id: I9f277f9c4f9de70ae755a1334d86c67bbb24c988
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
index 3fbe70f..b0c2adc 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
@@ -255,13 +255,13 @@
      * - Uses a content provider to get a public dictionary set, as per the protocol described
      *   in BinaryDictionaryFileDumper.
      * If that fails:
-     * - Gets a file name from the fallback resource passed as an argument.
+     * - Gets a file name from the built-in dictionary for this locale, if any.
      * If that fails:
      * - Returns null.
      * @return The list of addresses of valid dictionary files, or null.
      */
     public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale,
-            final Context context, final int fallbackResId) {
+            final Context context) {
 
         // 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
@@ -290,6 +290,8 @@
         }
 
         if (!foundMainDict && dictPackSettings.isWordListActive(mainDictId)) {
+            final int fallbackResId =
+                    DictionaryFactory.getMainDictionaryResourceId(context.getResources(), locale);
             final AssetFileAddress fallbackAsset = loadFallbackResource(context, fallbackResId);
             if (null != fallbackAsset) {
                 fileList.add(fallbackAsset);
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java
index 490a327..bf05f3b 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java
@@ -36,24 +36,22 @@
      * Initializes a dictionary from a dictionary pack, with explicit flags.
      *
      * This searches for a content provider providing a dictionary pack for the specified
-     * locale. If none is found, it falls back to using the resource passed as fallBackResId
-     * as a dictionary.
+     * locale. If none is found, it falls back to the built-in dictionary - if any.
      * @param context application context for reading resources
      * @param locale the locale for which to create the dictionary
-     * @param fallbackResId the id of the resource to use as a fallback if no pack is found
      * @param useFullEditDistance whether to use the full edit distance in suggestions
      * @return an initialized instance of DictionaryCollection
      */
     public static DictionaryCollection createDictionaryFromManager(final Context context,
-            final Locale locale, final int fallbackResId, final boolean useFullEditDistance) {
+            final Locale locale, final boolean useFullEditDistance) {
         if (null == locale) {
             Log.e(TAG, "No locale defined for dictionary");
-            return new DictionaryCollection(createBinaryDictionary(context, fallbackResId, locale));
+            return new DictionaryCollection(createBinaryDictionary(context, locale));
         }
 
         final LinkedList<Dictionary> dictList = new LinkedList<Dictionary>();
         final ArrayList<AssetFileAddress> assetFileList =
-                BinaryDictionaryGetter.getDictionaryFiles(locale, context, fallbackResId);
+                BinaryDictionaryGetter.getDictionaryFiles(locale, context);
         if (null != assetFileList) {
             for (final AssetFileAddress f : assetFileList) {
                 final BinaryDictionary binaryDictionary =
@@ -75,17 +73,14 @@
      * Initializes a dictionary from a dictionary pack, with default flags.
      *
      * This searches for a content provider providing a dictionary pack for the specified
-     * locale. If none is found, it falls back to using the resource passed as fallBackResId
-     * as a dictionary.
+     * locale. If none is found, it falls back to the built-in dictionary, if any.
      * @param context application context for reading resources
      * @param locale the locale for which to create the dictionary
-     * @param fallbackResId the id of the resource to use as a fallback if no pack is found
      * @return an initialized instance of DictionaryCollection
      */
     public static DictionaryCollection createDictionaryFromManager(final Context context,
-            final Locale locale, final int fallbackResId) {
-        return createDictionaryFromManager(context, locale, fallbackResId,
-                false /* useFullEditDistance */);
+            final Locale locale) {
+        return createDictionaryFromManager(context, locale, false /* useFullEditDistance */);
     }
 
     /**
@@ -96,9 +91,10 @@
      * @return an initialized instance of BinaryDictionary
      */
     protected static BinaryDictionary createBinaryDictionary(final Context context,
-            final int resId, final Locale locale) {
+            final Locale locale) {
         AssetFileDescriptor afd = null;
         try {
+            final int resId = getMainDictionaryResourceId(context.getResources(), locale);
             afd = context.getResources().openRawResourceFd(resId);
             if (afd == null) {
                 Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
@@ -115,7 +111,7 @@
             return new BinaryDictionary(context, sourceDir, afd.getStartOffset(), afd.getLength(),
                     false /* useFullEditDistance */, locale);
         } catch (android.content.res.Resources.NotFoundException e) {
-            Log.e(TAG, "Could not find the resource. resId=" + resId);
+            Log.e(TAG, "Could not find the resource");
             return null;
         } finally {
             if (null != afd) {
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index e16be2c..7cdeef8 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -501,9 +501,7 @@
             oldContactsDictionary = null;
         }
 
-        final int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(
-                mResources, keyboardLocale);
-        mSuggest = new Suggest(this, mainDicResId, keyboardLocale);
+        mSuggest = new Suggest(this, keyboardLocale);
         if (mSettingsValues.mAutoCorrectEnabled) {
             mSuggest.setAutoCorrectionThreshold(mSettingsValues.mAutoCorrectionThreshold);
         }
@@ -552,10 +550,7 @@
     }
 
     /* package private */ void resetSuggestMainDict() {
-        final Locale keyboardLocale = mSubtypeSwitcher.getInputLocale();
-        int mainDicResId = DictionaryFactory.getMainDictionaryResourceId(
-                mResources, keyboardLocale);
-        mSuggest.resetMainDict(this, mainDicResId, keyboardLocale);
+        mSuggest.resetMainDict(this, mSubtypeSwitcher.getInputLocale());
     }
 
     @Override
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index fa6664b..c3f3bd5 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -104,8 +104,8 @@
 
     private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4;
 
-    public Suggest(final Context context, final int dictionaryResId, final Locale locale) {
-        initAsynchronously(context, dictionaryResId, locale);
+    public Suggest(final Context context, final Locale locale) {
+        initAsynchronously(context, locale);
     }
 
     /* package for test */ Suggest(final Context context, final File dictionary,
@@ -119,9 +119,8 @@
         addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary);
     }
 
-    private void initAsynchronously(final Context context, final int dictionaryResId,
-            final Locale locale) {
-        resetMainDict(context, dictionaryResId, locale);
+    private void initAsynchronously(final Context context, final Locale locale) {
+        resetMainDict(context, locale);
 
         // TODO: read the whitelist and init the pool asynchronously too.
         // initPool should be done asynchronously now that the pool is thread-safe.
@@ -146,14 +145,13 @@
         }
     }
 
-    public void resetMainDict(final Context context, final int dictionaryResId,
-            final Locale locale) {
+    public void resetMainDict(final Context context, final Locale locale) {
         mMainDict = null;
         new Thread("InitializeBinaryDictionary") {
             @Override
             public void run() {
                 final Dictionary newMainDict = DictionaryFactory.createDictionaryFromManager(
-                        context, locale, dictionaryResId);
+                        context, locale);
                 mMainDict = newMainDict;
                 addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_MAIN, newMainDict);
                 addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_MAIN, newMainDict);
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
index 6e4ee31..576fbe6 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
@@ -386,11 +386,8 @@
         final int script = getScriptFromLocale(locale);
         final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo(
                 SpellCheckerProximityInfo.getProximityForScript(script));
-        final Resources resources = getResources();
-        final int fallbackResourceId = DictionaryFactory.getMainDictionaryResourceId(
-                resources, locale);
         final DictionaryCollection dictionaryCollection =
-                DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId,
+                DictionaryFactory.createDictionaryFromManager(this, locale,
                         true /* useFullEditDistance */);
         final String localeStr = locale.toString();
         Dictionary userDictionary = mUserDictionaries.get(localeStr);