diff options
Diffstat (limited to 'src/com/android/settings/UserDictionarySettings.java')
-rw-r--r-- | src/com/android/settings/UserDictionarySettings.java | 117 |
1 files changed, 88 insertions, 29 deletions
diff --git a/src/com/android/settings/UserDictionarySettings.java b/src/com/android/settings/UserDictionarySettings.java index f712cb6..fa4359c 100644 --- a/src/com/android/settings/UserDictionarySettings.java +++ b/src/com/android/settings/UserDictionarySettings.java @@ -63,23 +63,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; @@ -93,7 +99,7 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - mView = inflater.inflate(R.layout.list_content_with_empty_view, container, false); + mView = inflater.inflate(R.layout.custom_preference_list_fragment, container, false); return mView; } @@ -101,7 +107,25 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - mCursor = createCursor(); + final Intent intent = getActivity().getIntent(); + 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; + } + + mLocale = locale; + mCursor = createCursor(locale); TextView emptyView = (TextView)mView.findViewById(R.id.empty); emptyView.setText(R.string.user_dict_settings_empty_text); @@ -117,12 +141,12 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata 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,12 +163,28 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata outState.putBoolean(INSTANCE_KEY_ADDED_WORD, mAddedWordAlready); } - private Cursor createCursor() { - String currentLocale = Locale.getDefault().toString(); - // Case-insensitive sort - return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, - QUERY_SELECTION, new String[] { currentLocale }, - "UPPER(" + UserDictionary.Words.WORD + ")"); + private Cursor createCursor(final String locale) { + // 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() { @@ -153,7 +193,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); @@ -167,7 +207,8 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata MenuItem actionItem = menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title) .setIcon(R.drawable.ic_menu_add); - actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); + actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | + MenuItem.SHOW_AS_ACTION_WITH_TEXT); } @Override @@ -182,6 +223,7 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata } private String getWord(int position) { + if (null == mCursor) return null; mCursor.moveToPosition(position); // Handle a possible race-condition if (mCursor.isAfterLast()) return null; @@ -235,14 +277,29 @@ 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 (!mCursor.requery()) { + 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 (null != mCursor && !mCursor.requery()) { throw new IllegalStateException("can't requery on already-closed cursor."); } mAddedWordAlready = true; @@ -277,23 +334,25 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata super(context, layout, c, from, to); mSettings = settings; - int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD); - String alphabet = context.getString( - com.android.internal.R.string.fast_scroll_alphabet); - mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet); + if (null != c) { + final String alphabet = context.getString( + com.android.internal.R.string.fast_scroll_alphabet); + final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD); + mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet); + } setViewBinder(mViewBinder); } public int getPositionForSection(int section) { - return mIndexer.getPositionForSection(section); + return null == mIndexer ? 0 : mIndexer.getPositionForSection(section); } public int getSectionForPosition(int position) { - return mIndexer.getSectionForPosition(position); + return null == mIndexer ? 0 : mIndexer.getSectionForPosition(position); } public Object[] getSections() { - return mIndexer.getSections(); + return null == mIndexer ? null : mIndexer.getSections(); } public void onClick(View v) { |