1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin.userdictionary; 18 19 import com.android.inputmethod.latin.R; 20 21 import android.app.ListFragment; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.database.Cursor; 26 import android.os.Build; 27 import android.os.Bundle; 28 import android.provider.UserDictionary; 29 import android.text.TextUtils; 30 import android.view.LayoutInflater; 31 import android.view.Menu; 32 import android.view.MenuInflater; 33 import android.view.MenuItem; 34 import android.view.View; 35 import android.view.ViewGroup; 36 import android.widget.AlphabetIndexer; 37 import android.widget.ListAdapter; 38 import android.widget.ListView; 39 import android.widget.SectionIndexer; 40 import android.widget.SimpleCursorAdapter; 41 import android.widget.TextView; 42 43 import java.util.Locale; 44 45 // Caveat: This class is basically taken from 46 // packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionarySettings.java 47 // in order to deal with some devices that have issues with the user dictionary handling 48 49 public class UserDictionarySettings extends ListFragment { 50 51 public static final boolean IS_SHORTCUT_API_SUPPORTED = 52 Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; 53 54 private static final String[] QUERY_PROJECTION_SHORTCUT_UNSUPPORTED = 55 { UserDictionary.Words._ID, UserDictionary.Words.WORD}; 56 private static final String[] QUERY_PROJECTION_SHORTCUT_SUPPORTED = 57 { UserDictionary.Words._ID, UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT}; 58 private static final String[] QUERY_PROJECTION = 59 IS_SHORTCUT_API_SUPPORTED ? 60 QUERY_PROJECTION_SHORTCUT_SUPPORTED : QUERY_PROJECTION_SHORTCUT_UNSUPPORTED; 61 62 // The index of the shortcut in the above array. 63 private static final int INDEX_SHORTCUT = 2; 64 65 private static final String[] ADAPTER_FROM_SHORTCUT_UNSUPPORTED = { 66 UserDictionary.Words.WORD, 67 }; 68 69 private static final String[] ADAPTER_FROM_SHORTCUT_SUPPORTED = { 70 UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT 71 }; 72 73 private static final String[] ADAPTER_FROM = IS_SHORTCUT_API_SUPPORTED ? 74 ADAPTER_FROM_SHORTCUT_SUPPORTED : ADAPTER_FROM_SHORTCUT_UNSUPPORTED; 75 76 private static final int[] ADAPTER_TO_SHORTCUT_UNSUPPORTED = { 77 android.R.id.text1, 78 }; 79 80 private static final int[] ADAPTER_TO_SHORTCUT_SUPPORTED = { 81 android.R.id.text1, android.R.id.text2 82 }; 83 84 private static final int[] ADAPTER_TO = IS_SHORTCUT_API_SUPPORTED ? 85 ADAPTER_TO_SHORTCUT_SUPPORTED : ADAPTER_TO_SHORTCUT_UNSUPPORTED; 86 87 // Either the locale is empty (means the word is applicable to all locales) 88 // or the word equals our current locale 89 private static final String QUERY_SELECTION = 90 UserDictionary.Words.LOCALE + "=?"; 91 private static final String QUERY_SELECTION_ALL_LOCALES = 92 UserDictionary.Words.LOCALE + " is null"; 93 94 private static final String DELETE_SELECTION_WITH_SHORTCUT = UserDictionary.Words.WORD 95 + "=? AND " + UserDictionary.Words.SHORTCUT + "=?"; 96 private static final String DELETE_SELECTION_WITHOUT_SHORTCUT = UserDictionary.Words.WORD 97 + "=? AND " + UserDictionary.Words.SHORTCUT + " is null OR " 98 + UserDictionary.Words.SHORTCUT + "=''"; 99 private static final String DELETE_SELECTION_SHORTCUT_UNSUPPORTED = 100 UserDictionary.Words.WORD + "=?"; 101 102 private static final int OPTIONS_MENU_ADD = Menu.FIRST; 103 104 private Cursor mCursor; 105 106 protected String mLocale; 107 108 @Override 109 public void onCreate(Bundle savedInstanceState) { 110 super.onCreate(savedInstanceState); 111 getActivity().getActionBar().setTitle(R.string.edit_personal_dictionary); 112 } 113 114 @Override 115 public View onCreateView( 116 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 117 return inflater.inflate( 118 R.layout.user_dictionary_preference_list_fragment, container, false); 119 } 120 121 @Override 122 public void onActivityCreated(Bundle savedInstanceState) { 123 super.onActivityCreated(savedInstanceState); 124 125 final Intent intent = getActivity().getIntent(); 126 final String localeFromIntent = 127 null == intent ? null : intent.getStringExtra("locale"); 128 129 final Bundle arguments = getArguments(); 130 final String localeFromArguments = 131 null == arguments ? null : arguments.getString("locale"); 132 133 final String locale; 134 if (null != localeFromArguments) { 135 locale = localeFromArguments; 136 } else if (null != localeFromIntent) { 137 locale = localeFromIntent; 138 } else { 139 locale = null; 140 } 141 142 mLocale = locale; 143 mCursor = createCursor(locale); 144 TextView emptyView = (TextView) getView().findViewById(android.R.id.empty); 145 emptyView.setText(R.string.user_dict_settings_empty_text); 146 147 final ListView listView = getListView(); 148 listView.setAdapter(createAdapter()); 149 listView.setFastScrollEnabled(true); 150 listView.setEmptyView(emptyView); 151 152 setHasOptionsMenu(true); 153 // Show the language as a subtitle of the action bar 154 getActivity().getActionBar().setSubtitle( 155 UserDictionarySettingsUtils.getLocaleDisplayName(getActivity(), mLocale)); 156 } 157 158 @SuppressWarnings("deprecation") 159 private Cursor createCursor(final String locale) { 160 // Locale can be any of: 161 // - The string representation of a locale, as returned by Locale#toString() 162 // - The empty string. This means we want a cursor returning words valid for all locales. 163 // - null. This means we want a cursor for the current locale, whatever this is. 164 // Note that this contrasts with the data inside the database, where NULL means "all 165 // locales" and there should never be an empty string. The confusion is called by the 166 // historical use of null for "all locales". 167 // TODO: it should be easy to make this more readable by making the special values 168 // human-readable, like "all_locales" and "current_locales" strings, provided they 169 // can be guaranteed not to match locales that may exist. 170 if ("".equals(locale)) { 171 // Case-insensitive sort 172 return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, 173 QUERY_SELECTION_ALL_LOCALES, null, 174 "UPPER(" + UserDictionary.Words.WORD + ")"); 175 } else { 176 final String queryLocale = null != locale ? locale : Locale.getDefault().toString(); 177 return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, 178 QUERY_SELECTION, new String[] { queryLocale }, 179 "UPPER(" + UserDictionary.Words.WORD + ")"); 180 } 181 } 182 183 private ListAdapter createAdapter() { 184 return new MyAdapter(getActivity(), R.layout.user_dictionary_item, mCursor, 185 ADAPTER_FROM, ADAPTER_TO, this); 186 } 187 188 @Override 189 public void onListItemClick(ListView l, View v, int position, long id) { 190 final String word = getWord(position); 191 final String shortcut = getShortcut(position); 192 if (word != null) { 193 showAddOrEditDialog(word, shortcut); 194 } 195 } 196 197 @Override 198 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 199 if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { 200 final Locale systemLocale = getResources().getConfiguration().locale; 201 if (!TextUtils.isEmpty(mLocale) && !mLocale.equals(systemLocale.toString())) { 202 // Hide the add button for ICS because it doesn't support specifying a locale 203 // for an entry. This new "locale"-aware API has been added in conjunction 204 // with the shortcut API. 205 return; 206 } 207 } 208 MenuItem actionItem = 209 menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title) 210 .setIcon(R.drawable.ic_menu_add); 211 actionItem.setShowAsAction( 212 MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); 213 } 214 215 @Override 216 public boolean onOptionsItemSelected(MenuItem item) { 217 if (item.getItemId() == OPTIONS_MENU_ADD) { 218 showAddOrEditDialog(null, null); 219 return true; 220 } 221 return false; 222 } 223 224 /** 225 * Add or edit a word. If editingWord is null, it's an add; otherwise, it's an edit. 226 * @param editingWord the word to edit, or null if it's an add. 227 * @param editingShortcut the shortcut for this entry, or null if none. 228 */ 229 private void showAddOrEditDialog(final String editingWord, final String editingShortcut) { 230 final Bundle args = new Bundle(); 231 args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, null == editingWord 232 ? UserDictionaryAddWordContents.MODE_INSERT 233 : UserDictionaryAddWordContents.MODE_EDIT); 234 args.putString(UserDictionaryAddWordContents.EXTRA_WORD, editingWord); 235 args.putString(UserDictionaryAddWordContents.EXTRA_SHORTCUT, editingShortcut); 236 args.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mLocale); 237 android.preference.PreferenceActivity pa = 238 (android.preference.PreferenceActivity)getActivity(); 239 pa.startPreferencePanel(UserDictionaryAddWordFragment.class.getName(), 240 args, R.string.user_dict_settings_add_dialog_title, null, null, 0); 241 } 242 243 private String getWord(final int position) { 244 if (null == mCursor) return null; 245 mCursor.moveToPosition(position); 246 // Handle a possible race-condition 247 if (mCursor.isAfterLast()) return null; 248 249 return mCursor.getString( 250 mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD)); 251 } 252 253 private String getShortcut(final int position) { 254 if (!IS_SHORTCUT_API_SUPPORTED) return null; 255 if (null == mCursor) return null; 256 mCursor.moveToPosition(position); 257 // Handle a possible race-condition 258 if (mCursor.isAfterLast()) return null; 259 260 return mCursor.getString( 261 mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT)); 262 } 263 264 public static void deleteWord(final String word, final String shortcut, 265 final ContentResolver resolver) { 266 if (!IS_SHORTCUT_API_SUPPORTED) { 267 resolver.delete(UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_SHORTCUT_UNSUPPORTED, 268 new String[] { word }); 269 } else if (TextUtils.isEmpty(shortcut)) { 270 resolver.delete( 271 UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT, 272 new String[] { word }); 273 } else { 274 resolver.delete( 275 UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT, 276 new String[] { word, shortcut }); 277 } 278 } 279 280 private static class MyAdapter extends SimpleCursorAdapter implements SectionIndexer { 281 282 private AlphabetIndexer mIndexer; 283 284 private ViewBinder mViewBinder = new ViewBinder() { 285 286 @Override 287 public boolean setViewValue(View v, Cursor c, int columnIndex) { 288 if (!IS_SHORTCUT_API_SUPPORTED) { 289 // just let SimpleCursorAdapter set the view values 290 return false; 291 } 292 if (columnIndex == INDEX_SHORTCUT) { 293 final String shortcut = c.getString(INDEX_SHORTCUT); 294 if (TextUtils.isEmpty(shortcut)) { 295 v.setVisibility(View.GONE); 296 } else { 297 ((TextView)v).setText(shortcut); 298 v.setVisibility(View.VISIBLE); 299 } 300 v.invalidate(); 301 return true; 302 } 303 304 return false; 305 } 306 }; 307 308 @SuppressWarnings("deprecation") 309 public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, 310 UserDictionarySettings settings) { 311 super(context, layout, c, from, to); 312 313 if (null != c) { 314 final String alphabet = context.getString(R.string.user_dict_fast_scroll_alphabet); 315 final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD); 316 mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet); 317 } 318 setViewBinder(mViewBinder); 319 } 320 321 @Override 322 public int getPositionForSection(int section) { 323 return null == mIndexer ? 0 : mIndexer.getPositionForSection(section); 324 } 325 326 @Override 327 public int getSectionForPosition(int position) { 328 return null == mIndexer ? 0 : mIndexer.getSectionForPosition(position); 329 } 330 331 @Override 332 public Object[] getSections() { 333 return null == mIndexer ? null : mIndexer.getSections(); 334 } 335 } 336 } 337