OSDN Git Service

Change the interface for user dictionary multiple locales.
authorJean Chalard <jchalard@google.com>
Thu, 12 May 2011 06:06:16 +0000 (15:06 +0900)
committerJean Chalard <jchalard@google.com>
Wed, 18 May 2011 03:54:20 +0000 (12:54 +0900)
The user dictionary interface now works the following way:
* Locale gathering
- The current locale is always considered as present, even if
there are no words for it in the user dictionary.
- Any locale for which at least one word is registered is
considered present.
- A null locale is considered a valid locale meaning "all
languages".
- If no words are present at all, since the current locale is
always considered present, the system will consider there is
exactly one locale present - and allow editing this user
dictionary.

* Options display
- If only one locale is present, the interface is the same as
for Honeycomb: display a "User dictionary" PreferenceScreen
that brings up the dictionary editor interface.
- If there are several locales present, then the user
dictionary option will jump to a screen that will display a
list of available locales.

* Word insertion
- Inserting a word will always use the locale of the currently
displayed dictionary. If it is the "all languages" null
pseudo-locale, that will still hold and the word will be added
to this pseudo-locale.

It is worthy to note that the "All languages" locale is only
accessible if for some reason there are already words with a
null locale in the database before this is installed. For
example, if an application has inserted some words that way,
or if the user inserted words with a previous version of the
settings application.
On a freshly flashed device, barring the use of third-party
application that would add such words, it is not possible to
access the "all languages" locale any more because there is
no interface to do it, though it works if such words are
inside.

Bug: 3479738
Change-Id: Iba323e5aeb3f4f575896903a4e8bef6ffb3ea306

res/values/strings.xml
res/xml/language_settings.xml
src/com/android/settings/UserDictionarySettings.java
src/com/android/settings/Utils.java
src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
src/com/android/settings/inputmethod/UserDictionaryList.java [new file with mode: 0644]

index e3250be..f03286e 100644 (file)
@@ -2393,8 +2393,10 @@ found in the list of installed applications.</string>
     <!-- User dictionary settings --><skip />
     <!-- User dictionary settings, The titlebar text of the User dictionary settings screen. -->
     <string name="user_dict_settings_titlebar">User dictionary</string>
-    <!-- User dictionary settings, The title of the list item to go into the User dictionary settings screen. -->
-    <string name="user_dict_settings_title">User dictionary</string>
+    <!-- User dictionary settings, The title of the list item to go into the User dictionary settings screen when there is only one user dictionary. -->
+    <string name="user_dict_single_settings_title">User dictionary</string>
+    <!-- User dictionary settings, The title of the list item to go into the User dictionary list when there are several user dictionaries. -->
+    <string name="user_dict_multiple_settings_title">Manage user dictionaries</string>
     <!-- User dictionary settings.  The summary of the listem item to go into the User dictionary settings screen. -->
     <string name="user_dict_settings_summary">""</string>
     <!-- User dictionary settings. The title of the menu item to add a new word to the user dictionary. -->
@@ -2411,6 +2413,8 @@ found in the list of installed applications.</string>
     <string name="user_dict_settings_empty_text" product="tablet">You do not have any words in the user dictionary. You can add a word by touching the Add ( + ) button.</string>
     <!-- User dictionary settings. The text to show when there are no user-defined words in the dictionary  [CHAR LIMIT=200] -->
     <string name="user_dict_settings_empty_text" product="default">You do not have any words in the user dictionary. You can add a word through the menu.</string>
+    <!-- User dictionary settings. The text to show to describe the dictionary common to all languages -->
+    <string name="user_dict_settings_all_languages">All languages</string>
 
     <!-- This is for diagnostics screen. The title of a screen with various items realted to launching screens that will giev the user info. For example, it contains "Phone information" and "Battery information" -->
     <string name="testing">Testing</string>
index c63bed4..f19fb09 100644 (file)
         <PreferenceScreen
                  android:fragment="com.android.settings.LocalePicker"
                  android:key="phone_language"
-                 android:order="0"
-                 android:title="@string/phone_language">
-        </PreferenceScreen>
+                 android:title="@string/phone_language"/>
 
-      <!-- User dictionaries entries will be populated programmatically. -->
+        <!-- User dictionary preference title and fragment will be set programmatically. -->
+        <PreferenceScreen
+                 android:key="key_user_dictionary_settings" />
 
     </PreferenceCategory>
 
index d0ab472..22112d7 100644 (file)
@@ -46,6 +46,7 @@ import android.widget.TextView;
 
 import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment;
 
+import java.util.Arrays;
 import java.util.Locale;
 
 public class UserDictionarySettings extends ListFragment implements DialogCreatable {
@@ -63,23 +64,29 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
 
     // Either the locale is empty (means the word is applicable to all locales)
     // or the word equals our current locale
-    private static final String QUERY_SELECTION = UserDictionary.Words.LOCALE + "=? OR "
-            + UserDictionary.Words.LOCALE + " is null";
+    private static final String QUERY_SELECTION =
+            UserDictionary.Words.LOCALE + "=?";
+    private static final String QUERY_SELECTION_ALL_LOCALES =
+            UserDictionary.Words.LOCALE + " is null";
 
     private static final String DELETE_SELECTION = UserDictionary.Words.WORD + "=?";
 
     private static final String EXTRA_WORD = "word";
-    
+
     private static final int OPTIONS_MENU_ADD = Menu.FIRST;
 
     private static final int DIALOG_ADD_OR_EDIT = 0;
-    
+
+    private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250;
+
     /** The word being edited in the dialog (null means the user is adding a word). */
     private String mDialogEditingWord;
 
     private View mView;
     private Cursor mCursor;
-    
+
+    protected String mLocale;
+
     private boolean mAddedWordAlready;
     private boolean mAutoReturn;
 
@@ -102,9 +109,24 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
         super.onActivityCreated(savedInstanceState);
 
         final Intent intent = getActivity().getIntent();
-        final String locale = intent.getStringExtra("locale");
+        final String localeFromIntent =
+                null == intent ? null : intent.getStringExtra("locale");
+
+        final Bundle arguments = getArguments();
+        final String localeFromArguments =
+                null == arguments ? null : arguments.getString("locale");
+
+        final String locale;
+        if (null != localeFromArguments) {
+            locale = localeFromArguments;
+        } else if (null != localeFromIntent) {
+            locale = localeFromIntent;
+        } else {
+            locale = null;
+        }
 
-        mCursor = createCursor(null != locale ? locale : Locale.getDefault().toString());
+        mLocale = locale;
+        mCursor = createCursor(locale);
         TextView emptyView = (TextView)mView.findViewById(R.id.empty);
         emptyView.setText(R.string.user_dict_settings_empty_text);
 
@@ -143,10 +165,27 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
     }
 
     private Cursor createCursor(final String locale) {
-        // Case-insensitive sort
-        return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
-                QUERY_SELECTION, new String[] { locale },
-                "UPPER(" + UserDictionary.Words.WORD + ")");
+        // Locale can be any of:
+        // - The string representation of a locale, as returned by Locale#toString()
+        // - The empty string. This means we want a cursor returning words valid for all locales.
+        // - null. This means we want a cursor for the current locale, whatever this is.
+        // Note that this contrasts with the data inside the database, where NULL means "all
+        // locales" and there should never be an empty string. The confusion is called by the
+        // historical use of null for "all locales".
+        // TODO: it should be easy to make this more readable by making the special values
+        // human-readable, like "all_locales" and "current_locales" strings, provided they
+        // can be guaranteed not to match locales that may exist.
+        if ("".equals(locale)) {
+            // Case-insensitive sort
+            return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
+                    QUERY_SELECTION_ALL_LOCALES, null,
+                    "UPPER(" + UserDictionary.Words.WORD + ")");
+        } else {
+            final String queryLocale = null != locale ? locale : Locale.getDefault().toString();
+            return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
+                    QUERY_SELECTION, new String[] { queryLocale },
+                    "UPPER(" + UserDictionary.Words.WORD + ")");
+        }
     }
 
     private ListAdapter createAdapter() {
@@ -155,7 +194,7 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
                 new String[] { UserDictionary.Words.WORD, UserDictionary.Words._ID },
                 new int[] { android.R.id.text1, R.id.delete_button }, this);
     }
-    
+
     @Override
     public void onListItemClick(ListView l, View v, int position, long id) {
         String word = getWord(position);
@@ -237,13 +276,28 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
             // The user was editing a word, so do a delete/add
             deleteWord(mDialogEditingWord);
         }
-        
+
         // Disallow duplicates
         deleteWord(word);
-        
+
         // TODO: present UI for picking whether to add word to all locales, or current.
-        UserDictionary.Words.addWord(getActivity(), word.toString(),
-                250, UserDictionary.Words.LOCALE_TYPE_ALL);
+        if (null == mLocale) {
+            // Null means insert with the default system locale.
+            UserDictionary.Words.addWord(getActivity(), word.toString(),
+                    FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_CURRENT);
+        } else if ("".equals(mLocale)) {
+            // Empty string means insert for all languages.
+            UserDictionary.Words.addWord(getActivity(), word.toString(),
+                    FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_ALL);
+        } else {
+            // TODO: fix the framework so that it can accept a locale when we add a word
+            // to the user dictionary instead of querying the system locale.
+            final Locale prevLocale = Locale.getDefault();
+            Locale.setDefault(Utils.createLocaleFromString(mLocale));
+            UserDictionary.Words.addWord(getActivity(), word.toString(),
+                    FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_CURRENT);
+            Locale.setDefault(prevLocale);
+        }
         if (!mCursor.requery()) {
             throw new IllegalStateException("can't requery on already-closed cursor.");
         }
index 18c6159..422ae90 100644 (file)
@@ -36,8 +36,10 @@ import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 
 import java.net.InetAddress;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 
 public class Utils {
 
@@ -308,4 +310,24 @@ public class Utils {
         }
         return addresses;
     }
+
+    public static Locale createLocaleFromString(String localeStr) {
+        // TODO: is there a better way to actually construct a locale that will match?
+        // The main problem is, on top of Java specs, locale.toString() and
+        // new Locale(locale.toString()).toString() do not return equal() strings in
+        // many cases, because the constructor takes the only string as the language
+        // code. So : new Locale("en", "US").toString() => "en_US"
+        // And : new Locale("en_US").toString() => "en_us"
+        if (null == localeStr)
+            return Locale.getDefault();
+        String[] brokenDownLocale = localeStr.split("_", 3);
+        // split may not return a 0-length array.
+        if (1 == brokenDownLocale.length) {
+            return new Locale(brokenDownLocale[0]);
+        } else if (2 == brokenDownLocale.length) {
+            return new Locale(brokenDownLocale[0], brokenDownLocale[1]);
+        } else {
+            return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]);
+        }
+    }
 }
index 31c6879..c72f0ba 100644 (file)
@@ -18,25 +18,21 @@ package com.android.settings.inputmethod;
 
 import com.android.settings.R;
 import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.UserDictionarySettings;
 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;
+import java.util.Set;
 
 public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment
         implements Preference.OnPreferenceChangeListener{
@@ -45,8 +41,8 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment
     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 static final String KEY_USER_DICTIONARY_SETTINGS = "key_user_dictionary_settings";
+
 
     private int mDefaultInputMethodSelectorVisibility = 0;
     private ListPreference mShowInputMethodSelectorPref;
@@ -70,9 +66,6 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment
         } else {
             mLanguagePref = findPreference(KEY_PHONE_LANGUAGE);
         }
-
-        createUserDictSettings((PreferenceGroup) findPreference(KEY_LANGUAGE_SETTINGS_CATEGORY));
-
         mShowInputMethodSelectorPref = (ListPreference)findPreference(
                 KEY_INPUT_METHOD_SELECTOR);
         mShowInputMethodSelectorPref.setOnPreferenceChangeListener(this);
@@ -91,49 +84,26 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment
         }
     }
 
-    /**
-     * 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) {
+    private void updateUserDictionaryPreference(Preference userDictionaryPreference) {
         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));
+        final Set<String> localeList = UserDictionaryList.getUserDictionaryLocalesList(activity);
+        if (localeList.size() <= 1) {
+            userDictionaryPreference.setTitle(R.string.user_dict_single_settings_title);
+            userDictionaryPreference.setFragment(UserDictionarySettings.class.getName());
+            // If the size of localeList is 0, we don't set the locale parameter in the
+            // extras. This will be interpreted by the UserDictionarySettings class as
+            // meaning "the current locale".
+            // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesList()
+            // the locale list always has at least one element, since it always includes the current
+            // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesList().
+            if (localeList.size() == 1) {
+                final String locale = (String)localeList.toArray()[0];
+                userDictionaryPreference.getExtras().putString("locale", locale);
+            }
         } else {
-            final int columnIndex = locales.getColumnIndex(UserDictionary.Words.LOCALE);
-            do {
-                final String locale = locales.getString(columnIndex);
-                if (locale != null && !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);
+            userDictionaryPreference.setTitle(R.string.user_dict_multiple_settings_title);
+            userDictionaryPreference.setFragment(UserDictionaryList.class.getName());
         }
-        newPref.setIntent(intent);
-        return newPref;
     }
 
     @Override
@@ -148,6 +118,7 @@ public class InputMethodAndLanguageSettings extends SettingsPreferenceFragment
             }
         }
 
+        updateUserDictionaryPreference(findPreference(KEY_USER_DICTIONARY_SETTINGS));
         mShowInputMethodSelectorPref.setOnPreferenceChangeListener(this);
     }
 
diff --git a/src/com/android/settings/inputmethod/UserDictionaryList.java b/src/com/android/settings/inputmethod/UserDictionaryList.java
new file mode 100644 (file)
index 0000000..5db2841
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.inputmethod;
+
+import com.android.settings.R;
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.UserDictionarySettings;
+import com.android.settings.Utils;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.database.Cursor;
+import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.PreferenceGroup;
+import android.provider.UserDictionary;
+
+import java.util.Locale;
+import java.util.Set;
+import java.util.TreeSet;
+
+public class UserDictionaryList extends SettingsPreferenceFragment {
+
+    private static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION =
+            "android.settings.USER_DICTIONARY_SETTINGS";
+
+    @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+        setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity()));
+    }
+
+    static Set<String> getUserDictionaryLocalesList(Activity activity) {
+        final Cursor cursor = activity.managedQuery(UserDictionary.Words.CONTENT_URI,
+                new String[] { UserDictionary.Words.LOCALE },
+                null, null, null);
+        final Set<String> localeList = new TreeSet<String>();
+        if (cursor.moveToFirst()) {
+            final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE);
+            do {
+                String locale = cursor.getString(columnIndex);
+                localeList.add(null != locale ? locale : "");
+            } while (cursor.moveToNext());
+        }
+        localeList.add(Locale.getDefault().toString());
+        return localeList;
+    }
+
+    /**
+     * 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();
+        userDictGroup.removeAll();
+        final Set<String> localeList = UserDictionaryList.getUserDictionaryLocalesList(activity);
+
+        if (localeList.isEmpty()) {
+            userDictGroup.addPreference(createUserDictionaryPreference(null, activity));
+        } else {
+            for (String locale : localeList) {
+                userDictGroup.addPreference(createUserDictionaryPreference(locale, activity));
+            }
+        }
+    }
+
+    /**
+     * 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) {
+        final Preference newPref = new Preference(getActivity());
+        final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION);
+        if (null == locale) {
+            newPref.setTitle(Locale.getDefault().getDisplayName());
+        } else {
+            if ("".equals(locale))
+                newPref.setTitle(getString(R.string.user_dict_settings_all_languages));
+            else
+                newPref.setTitle(Utils.createLocaleFromString(locale).getDisplayName());
+            intent.putExtra("locale", locale);
+            newPref.getExtras().putString("locale", locale);
+        }
+        newPref.setIntent(intent);
+        newPref.setFragment(UserDictionarySettings.class.getName());
+        return newPref;
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        createUserDictSettings(getPreferenceScreen());
+    }
+}