Correct a shaky processing and move it to a better place

The old code would remove caps from the first letter if
auto-capsed but that makes very little sense when the word
is camel-cased. Also, it would not correctly handle
surrogate pairs, and would require a direct reference to
LatinIME and a specific method to do the processing in an
unexpected place.

Change-Id: I416d6a805242788a2473f007ca7452c9fe3f5205
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 2ee5a38..e07bba0 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -528,7 +528,7 @@
         resetContactsDictionary(oldContactsDictionary);
 
         mUserHistoryDictionary
-                = new UserHistoryDictionary(this, this, localeStr, Suggest.DIC_USER_HISTORY);
+                = new UserHistoryDictionary(this, localeStr, Suggest.DIC_USER_HISTORY);
         mSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
 
         LocaleUtils.setSystemLocale(res, savedLocale);
@@ -2012,8 +2012,14 @@
             } else {
                 prevWord = null;
             }
+            final String secondWord;
+            if (mWordComposer.isAutoCapitalized() && !mWordComposer.isMostlyCaps()) {
+                secondWord = suggestion.toString().toLowerCase(mSubtypeSwitcher.getInputLocale());
+            } else {
+                secondWord = suggestion.toString();
+            }
             mUserHistoryDictionary.addToUserHistory(null == prevWord ? null : prevWord.toString(),
-                    suggestion.toString());
+                    secondWord);
         }
     }
 
@@ -2254,10 +2260,6 @@
         mFeedbackManager.vibrate(mKeyboardSwitcher.getKeyboardView());
     }
 
-    public boolean isAutoCapitalized() {
-        return mWordComposer.isAutoCapitalized();
-    }
-
     private void updateCorrectionMode() {
         // TODO: cleanup messy flags
         final boolean shouldAutoCorrect = mSettingsValues.mAutoCorrectEnabled
diff --git a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java
index 4e79846..db2cdf9 100644
--- a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java
@@ -75,8 +75,6 @@
     private static final String FREQ_COLUMN_PAIR_ID = "pair_id";
     private static final String FREQ_COLUMN_FREQUENCY = "freq";
 
-    private final LatinIME mIme;
-
     /** Locale for which this auto dictionary is storing words */
     private String mLocale;
 
@@ -139,9 +137,8 @@
         sDeleteHistoryBigrams = deleteHistoryBigram;
     }
 
-    public UserHistoryDictionary(Context context, LatinIME ime, String locale, int dicTypeId) {
+    public UserHistoryDictionary(final Context context, final String locale, final int dicTypeId) {
         super(context, dicTypeId);
-        mIme = ime;
         mLocale = locale;
         if (sOpenHelper == null) {
             sOpenHelper = new DatabaseHelper(getContext());
@@ -179,10 +176,6 @@
      * The second word may not be null (a NullPointerException would be thrown).
      */
     public int addToUserHistory(final String word1, String word2) {
-        // remove caps if second word is autocapitalized
-        if (mIme != null && mIme.isAutoCapitalized()) {
-            word2 = Character.toLowerCase(word2.charAt(0)) + word2.substring(1);
-        }
         super.addWord(word2, FREQUENCY_FOR_TYPED);
         // Do not insert a word as a bigram of itself
         if (word2.equals(word1)) {