Enable showing the user dictionary in several locales.

Bug: 3479738
Change-Id: Ic57a5675c44c36ad255f9927da151ad6a2a8c44c
diff --git a/res/xml/language_settings.xml b/res/xml/language_settings.xml
index b7934e6..c63bed4 100644
--- a/res/xml/language_settings.xml
+++ b/res/xml/language_settings.xml
@@ -19,19 +19,18 @@
         android:title="@string/language_keyboard_settings_title">
 
     <PreferenceCategory android:key="language_settings_category"
-            android:title="@string/language_settings_category" />
+            android:title="@string/language_settings_category">
 
-    <PreferenceScreen
-       android:fragment="com.android.settings.LocalePicker"
-       android:key="phone_language"
-       android:title="@string/phone_language">
-    </PreferenceScreen>
+        <PreferenceScreen
+                 android:fragment="com.android.settings.LocalePicker"
+                 android:key="phone_language"
+                 android:order="0"
+                 android:title="@string/phone_language">
+        </PreferenceScreen>
 
-    <PreferenceScreen
-        android:fragment="com.android.settings.UserDictionarySettings"
-        android:title="@string/user_dict_settings_title"
-        android:summary="@string/user_dict_settings_summary">
-    </PreferenceScreen>
+      <!-- User dictionaries entries will be populated programmatically. -->
+
+    </PreferenceCategory>
 
     <PreferenceCategory android:key="voice_input_category"
             android:title="@string/voice_input_category" >
diff --git a/src/com/android/settings/UserDictionarySettings.java b/src/com/android/settings/UserDictionarySettings.java
index b13126a..d0ab472 100644
--- a/src/com/android/settings/UserDictionarySettings.java
+++ b/src/com/android/settings/UserDictionarySettings.java
@@ -101,7 +101,10 @@
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
-        mCursor = createCursor();
+        final Intent intent = getActivity().getIntent();
+        final String locale = intent.getStringExtra("locale");
+
+        mCursor = createCursor(null != locale ? locale : Locale.getDefault().toString());
         TextView emptyView = (TextView)mView.findViewById(R.id.empty);
         emptyView.setText(R.string.user_dict_settings_empty_text);
 
@@ -117,12 +120,12 @@
             mAddedWordAlready = savedInstanceState.getBoolean(INSTANCE_KEY_ADDED_WORD, false);
         }
     }
-    
+
     @Override
     public void onResume() {
         super.onResume();
         final Intent intent = getActivity().getIntent();
-        if (!mAddedWordAlready 
+        if (!mAddedWordAlready
                 && intent.getAction().equals("com.android.settings.USER_DICTIONARY_INSERT")) {
             final String word = intent.getStringExtra(EXTRA_WORD);
             mAutoReturn = true;
@@ -139,11 +142,10 @@
         outState.putBoolean(INSTANCE_KEY_ADDED_WORD, mAddedWordAlready);
     }
 
-    private Cursor createCursor() {
-        String currentLocale = Locale.getDefault().toString();
+    private Cursor createCursor(final String locale) {
         // Case-insensitive sort
         return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
-                QUERY_SELECTION, new String[] { currentLocale },
+                QUERY_SELECTION, new String[] { locale },
                 "UPPER(" + UserDictionary.Words.WORD + ")");
     }
 
diff --git a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
index a4808b0..611da4f 100644
--- a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
+++ b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
@@ -21,14 +21,22 @@
 import com.android.settings.Utils;
 import com.android.settings.VoiceInputOutputSettings;
 
+import android.app.Activity;
 import android.content.Context;
+import android.content.Intent;
 import android.content.res.Configuration;
+import android.database.Cursor;
 import android.os.Bundle;
 import android.preference.ListPreference;
 import android.preference.Preference;
+import android.preference.PreferenceGroup;
 import android.preference.PreferenceScreen;
 import android.provider.Settings;
 import android.view.inputmethod.InputMethodManager;
+import android.provider.UserDictionary;
+
+import java.util.Locale;
+import java.util.TreeMap;
 
 public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment
         implements Preference.OnPreferenceChangeListener{
@@ -36,6 +44,9 @@
     private static final String KEY_PHONE_LANGUAGE = "phone_language";
     private static final String KEY_CURRENT_INPUT_METHOD = "current_input_method";
     private static final String KEY_INPUT_METHOD_SELECTOR = "input_method_selector";
+    private static final String KEY_LANGUAGE_SETTINGS_CATEGORY = "language_settings_category";
+    private static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION =
+            "android.settings.USER_DICTIONARY_SETTINGS";
 
     private int mDefaultInputMethodSelectorVisibility = 0;
     private ListPreference mShowInputMethodSelectorPref;
@@ -59,6 +70,9 @@
         } else {
             mLanguagePref = findPreference(KEY_PHONE_LANGUAGE);
         }
+
+        createUserDictSettings((PreferenceGroup) findPreference(KEY_LANGUAGE_SETTINGS_CATEGORY));
+
         mShowInputMethodSelectorPref = (ListPreference)findPreference(
                 KEY_INPUT_METHOD_SELECTOR);
         mShowInputMethodSelectorPref.setOnPreferenceChangeListener(this);
@@ -77,6 +91,51 @@
         }
     }
 
+    /**
+     * Creates the entries that allow the user to go into the user dictionary for each locale.
+     * @param userDictGroup The group to put the settings in.
+     */
+    protected void createUserDictSettings(PreferenceGroup userDictGroup) {
+        final Activity activity = getActivity();
+        final Cursor locales = activity.managedQuery(UserDictionary.Words.CONTENT_URI,
+                new String[] { UserDictionary.Words.LOCALE },
+                null, null, null);
+        final TreeMap<String, Preference> prefs = new TreeMap<String, Preference>();
+        int order = findPreference(KEY_PHONE_LANGUAGE).getOrder();
+        if (!locales.moveToFirst()) {
+            prefs.put("", createUserDictionaryPreference(null, activity, ++order));
+        } else {
+            final int columnIndex = locales.getColumnIndex(UserDictionary.Words.LOCALE);
+            do {
+                final String locale = locales.getString(columnIndex);
+                if (!prefs.containsKey(locale))
+                    prefs.put(locale, createUserDictionaryPreference(locale, activity, ++order));
+            } while (locales.moveToNext());
+        }
+        for (final Preference p : prefs.values()) {
+            userDictGroup.addPreference(p);
+        }
+    }
+
+    /**
+     * Create a single User Dictionary Preference object, with its parameters set.
+     * @param locale The locale for which this user dictionary is for.
+     * @return The corresponding preference.
+     */
+    protected Preference createUserDictionaryPreference(String locale, Activity activity,
+            int order) {
+        final Preference newPref = new Preference(getActivity());
+        newPref.setOrder(order);
+        newPref.setTitle(activity.getString(R.string.user_dict_settings_title));
+        final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION);
+        if (null != locale) {
+            newPref.setSummary(new Locale(locale).getDisplayName());
+            intent.putExtra("locale", locale);
+        }
+        newPref.setIntent(intent);
+        return newPref;
+    }
+
     @Override
     public void onResume() {
         super.onResume();