Merge "Suggest words with excessive chars out of proximity chars Bug: 3273807"
diff --git a/java/res/values-xlarge/bools.xml b/java/res/values-xlarge/bools.xml
index abacfa1..9fb670c 100644
--- a/java/res/values-xlarge/bools.xml
+++ b/java/res/values-xlarge/bools.xml
@@ -21,6 +21,7 @@
     <!-- Whether or not Popup on key press is enabled by default -->
     <bool name="default_popup_preview">false</bool>
     <bool name="config_enable_show_settings_key_option">false</bool>
+    <bool name="config_enable_show_subtype_settings">false</bool>
     <bool name="config_enable_show_voice_key_option">false</bool>
     <bool name="config_candidate_highlight_font_color_enabled">false</bool>
 </resources>
diff --git a/java/res/values/bools.xml b/java/res/values/bools.xml
index 84b0fe1..8742676 100644
--- a/java/res/values/bools.xml
+++ b/java/res/values/bools.xml
@@ -31,6 +31,7 @@
     <bool name="default_recorrection_enabled">true</bool>
     <bool name="config_long_press_comma_for_settings_enabled">true</bool>
     <bool name="config_enable_show_settings_key_option">true</bool>
+    <bool name="config_enable_show_subtype_settings">true</bool>
     <bool name="config_enable_show_voice_key_option">true</bool>
     <bool name="config_candidate_highlight_font_color_enabled">true</bool>
 </resources>
diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml
index 0eee060..47b3b45 100644
--- a/java/res/xml/prefs.xml
+++ b/java/res/xml/prefs.xml
@@ -73,6 +73,7 @@
     <!-- TODO: Filter subtypes by IME in SubtypeEnabler -->
     <!-- TODO: Maybe use this only for phone? -->
     <PreferenceScreen
+            android:key="subtype_settings"
             android:title="@string/language_selection_title"
             android:summary="@string/language_selection_summary">
         <intent
diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java
index b8590a7..3f604a3 100644
--- a/java/src/com/android/inputmethod/latin/Settings.java
+++ b/java/src/com/android/inputmethod/latin/Settings.java
@@ -53,6 +53,7 @@
     public static final String PREF_VOICE_SETTINGS_KEY = "voice_mode";
     public static final String PREF_INPUT_LANGUAGE = "input_language";
     public static final String PREF_SELECTED_LANGUAGES = "selected_languages";
+    public static final String PREF_SUBTYPES = "subtype_settings";
 
     public static final String PREF_PREDICTION_SETTINGS_KEY = "prediction_settings";
     public static final String PREF_QUICK_FIXES = "quick_fixes";
@@ -119,6 +120,12 @@
             getPreferenceScreen().removePreference(
                     getPreferenceScreen().findPreference(PREF_VIBRATE_ON));
         }
+
+        final boolean showSubtypeSettings = getResources().getBoolean(
+                R.bool.config_enable_show_subtype_settings);
+        if (!showSubtypeSettings) {
+            getPreferenceScreen().removePreference(findPreference(PREF_SUBTYPES));
+        }
     }
 
     @Override
diff --git a/java/src/com/android/inputmethod/latin/UserDictionary.java b/java/src/com/android/inputmethod/latin/UserDictionary.java
index 7a94ae4..e03f564 100644
--- a/java/src/com/android/inputmethod/latin/UserDictionary.java
+++ b/java/src/com/android/inputmethod/latin/UserDictionary.java
@@ -21,6 +21,7 @@
 import android.content.Context;
 import android.database.ContentObserver;
 import android.database.Cursor;
+import android.net.Uri;
 import android.provider.UserDictionary.Words;
 
 public class UserDictionary extends ExpandableDictionary {
@@ -80,7 +81,7 @@
      * @TODO use a higher or float range for frequency
      */
     @Override
-    public synchronized void addWord(String word, int frequency) {
+    public synchronized void addWord(final String word, final int frequency) {
         // Force load the dictionary here synchronously
         if (getRequiresReload()) loadDictionaryAsync();
         // Safeguard against adding long words. Can cause stack overflow.
@@ -99,7 +100,22 @@
         new Thread("addWord") {
             @Override
             public void run() {
-                contentResolver.insert(Words.CONTENT_URI, values);
+                Cursor cursor = contentResolver.query(Words.CONTENT_URI, PROJECTION,
+                        "word=? and ((locale IS NULL) or (locale=?))",
+                        new String[] { word, mLocale }, null);
+                if (cursor != null && cursor.moveToFirst()) {
+                    String locale = cursor.getString(cursor.getColumnIndex(Words.LOCALE));
+                    // If locale is null, we will not override the entry.
+                    if (locale != null && locale.equals(mLocale.toString())) {
+                        long id = cursor.getLong(cursor.getColumnIndex(Words._ID));
+                        Uri uri = Uri.withAppendedPath(Words.CONTENT_URI, Long.toString(id));
+                        // Update the entry with new frequency value.
+                        contentResolver.update(uri, values, null, null);
+                    }
+                } else {
+                    // Insert new entry.
+                    contentResolver.insert(Words.CONTENT_URI, values);
+                }
             }
         }.start();