[ML15] First step in removing DictionaryFacilitator.getLocale

Bug: 11230254
Change-Id: Ied010ec6f1024080e8a499f682d2fc1780e82ef7
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java
index 4a218d5..5683e3d 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java
@@ -214,6 +214,7 @@
         mPersonalizationHelper.updateEnabledSubtypes(enabledSubtypes);
     }
 
+    // TODO: remove this, it's confusing with seamless multiple language switching
     public void setIsMonolingualUser(final boolean isMonolingualUser) {
         mPersonalizationHelper.setIsMonolingualUser(isMonolingualUser);
     }
@@ -223,16 +224,25 @@
         return mDictionaryGroups[0].mLocale;
     }
 
+    public boolean isActive() {
+        return null != mDictionaryGroups[0].mLocale;
+    }
+
     /**
-     * Returns the primary locale among all currently active locales. BE CAREFUL using this.
+     * Returns the most probable locale among all currently active locales. BE CAREFUL using this.
      *
      * DO NOT USE THIS just because it's convenient. Use it when it's correct, for example when
      * choosing what dictionary to put a word in, or when changing the capitalization of a typed
      * string.
-     * @return the primary active locale
+     * @return the most probable locale
      */
-    public Locale getPrimaryLocale() {
-        return mDictionaryGroups[0].mLocale;
+    public Locale getMostProbableLocale() {
+        return getDictionaryGroupForMostProbableLanguage().mLocale;
+    }
+
+    private DictionaryGroup getDictionaryGroupForMostProbableLanguage() {
+        // TODO: implement this
+        return mDictionaryGroups[0];
     }
 
     private static ExpandableBinaryDictionary getSubDict(final String dictType,
@@ -274,11 +284,6 @@
         return null;
     }
 
-    private DictionaryGroup getDictionaryGroupForActiveLanguage() {
-        // TODO: implement this
-        return mDictionaryGroups[0];
-    }
-
     public void resetDictionariesWithDictNamePrefix(final Context context,
             final Locale newLocaleToUse,
             final boolean useContactsDict, final boolean usePersonalizedDicts,
@@ -542,17 +547,18 @@
     }
 
     public void addWordToUserDictionary(final Context context, final String word) {
-        final Locale locale = getLocale();
+        final Locale locale = getMostProbableLocale();
         if (locale == null) {
             return;
         }
+        // TODO: add a toast telling what language this is being added to?
         UserBinaryDictionary.addWordToUserDictionary(context, locale, word);
     }
 
     public void addToUserHistory(final String suggestion, final boolean wasAutoCapitalized,
             final NgramContext ngramContext, final int timeStampInSeconds,
             final boolean blockPotentiallyOffensive) {
-        final DictionaryGroup dictionaryGroup = getDictionaryGroupForActiveLanguage();
+        final DictionaryGroup dictionaryGroup = getDictionaryGroupForMostProbableLanguage();
         final String[] words = suggestion.split(Constants.WORD_SEPARATOR);
         NgramContext ngramContextForCurrentWord = ngramContext;
         for (int i = 0; i < words.length; i++) {
@@ -620,7 +626,7 @@
 
     private void removeWord(final String dictName, final String word) {
         final ExpandableBinaryDictionary dictionary =
-                getDictionaryGroupForActiveLanguage().getSubDict(dictName);
+                getDictionaryGroupForMostProbableLanguage().getSubDict(dictName);
         if (dictionary != null) {
             dictionary.removeUnigramEntryDynamically(word);
         }
@@ -747,7 +753,8 @@
             final SpacingAndPunctuations spacingAndPunctuations,
             final AddMultipleDictionaryEntriesCallback callback) {
         mPersonalizationHelper.addEntriesToPersonalizationDictionariesToUpdate(
-                getLocale(), personalizationDataChunk, spacingAndPunctuations, callback);
+                getMostProbableLocale(), personalizationDataChunk, spacingAndPunctuations,
+                callback);
     }
 
     @UsedForTesting
@@ -756,7 +763,7 @@
         // TODO: we're inserting the phrase into the dictionary for the active language. Rethink
         // this a bit from a theoretical point of view.
         final ExpandableBinaryDictionary contextualDict =
-                getDictionaryGroupForActiveLanguage().getSubDict(Dictionary.TYPE_CONTEXTUAL);
+                getDictionaryGroupForMostProbableLanguage().getSubDict(Dictionary.TYPE_CONTEXTUAL);
         if (contextualDict == null) {
             return;
         }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 64e65ef..6a583ae 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1627,7 +1627,7 @@
         }
         final String wordToShow;
         if (CapsModeUtils.isAutoCapsMode(mInputLogic.mLastComposedWord.mCapitalizedMode)) {
-            wordToShow = word.toLowerCase(mDictionaryFacilitator.getPrimaryLocale());
+            wordToShow = word.toLowerCase(mDictionaryFacilitator.getMostProbableLocale());
         } else {
             wordToShow = word;
         }
@@ -1912,7 +1912,7 @@
     }
 
     public void dumpDictionaryForDebug(final String dictName) {
-        if (mDictionaryFacilitator.getLocale() == null) {
+        if (!mDictionaryFacilitator.isActive()) {
             resetDictionaryFacilitatorIfNecessary();
         }
         mDictionaryFacilitator.dumpDictionaryForDebug(dictName);
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index d2d9b9b..e181237 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -55,10 +55,6 @@
         mDictionaryFacilitator = dictionaryFacilitator;
     }
 
-    public Locale getLocale() {
-        return mDictionaryFacilitator.getLocale();
-    }
-
     public void setAutoCorrectionThreshold(final float threshold) {
         mAutoCorrectionThreshold = threshold;
     }
@@ -136,7 +132,10 @@
                 SESSION_ID_TYPING);
         final ArrayList<SuggestedWordInfo> suggestionsContainer =
                 getTransformedSuggestedWordInfoList(wordComposer, suggestionResults,
-                        trailingSingleQuotesCount, mDictionaryFacilitator.getLocale());
+                        trailingSingleQuotesCount,
+                        // For transforming suggestions that don't come for any dictionary, we
+                        // use the currently most probable locale as it's our best bet.
+                        mDictionaryFacilitator.getMostProbableLocale());
         final boolean didRemoveTypedWord =
                 SuggestedWordInfo.removeDups(wordComposer.getTypedWord(), suggestionsContainer);
 
@@ -216,7 +215,8 @@
         final SuggestionResults suggestionResults = mDictionaryFacilitator.getSuggestionResults(
                 wordComposer, ngramContext, proximityInfo, settingsValuesForSuggestion,
                 SESSION_ID_GESTURE);
-        final Locale defaultLocale = mDictionaryFacilitator.getLocale();
+        // For transforming words that don't come from a dictionary, because it's our best bet
+        final Locale defaultLocale = mDictionaryFacilitator.getMostProbableLocale();
         final ArrayList<SuggestedWordInfo> suggestionsContainer =
                 new ArrayList<>(suggestionResults);
         final int suggestionsCount = suggestionsContainer.size();