1 /* 2 * Copyright (C) 2009 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.settings.inputmethod; 18 19 import android.annotation.Nullable; 20 import android.app.ActionBar; 21 import android.app.settings.SettingsEnums; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.database.Cursor; 26 import android.os.Bundle; 27 import android.provider.UserDictionary; 28 import android.text.TextUtils; 29 import android.view.LayoutInflater; 30 import android.view.Menu; 31 import android.view.MenuInflater; 32 import android.view.MenuItem; 33 import android.view.View; 34 import android.view.ViewGroup; 35 import android.widget.AlphabetIndexer; 36 import android.widget.ListAdapter; 37 import android.widget.ListView; 38 import android.widget.SectionIndexer; 39 import android.widget.SimpleCursorAdapter; 40 import android.widget.TextView; 41 42 import androidx.fragment.app.ListFragment; 43 import androidx.loader.app.LoaderManager; 44 import androidx.loader.content.Loader; 45 46 import com.android.settings.R; 47 import com.android.settings.core.SubSettingLauncher; 48 import com.android.settings.overlay.FeatureFactory; 49 import com.android.settingslib.core.instrumentation.Instrumentable; 50 import com.android.settingslib.core.instrumentation.VisibilityLoggerMixin; 51 52 public class UserDictionarySettings extends ListFragment implements Instrumentable, 53 LoaderManager.LoaderCallbacks<Cursor> { 54 55 private static final String DELETE_SELECTION_WITH_SHORTCUT = UserDictionary.Words.WORD 56 + "=? AND " + UserDictionary.Words.SHORTCUT + "=?"; 57 private static final String DELETE_SELECTION_WITHOUT_SHORTCUT = UserDictionary.Words.WORD 58 + "=? AND " + UserDictionary.Words.SHORTCUT + " is null OR " 59 + UserDictionary.Words.SHORTCUT + "=''"; 60 61 private static final int OPTIONS_MENU_ADD = Menu.FIRST; 62 private static final int LOADER_ID = 1; 63 64 private VisibilityLoggerMixin mVisibilityLoggerMixin; 65 66 private Cursor mCursor; 67 private String mLocale; 68 69 @Override getMetricsCategory()70 public int getMetricsCategory() { 71 return SettingsEnums.USER_DICTIONARY_SETTINGS; 72 } 73 74 @Override onCreate(@ullable Bundle savedInstanceState)75 public void onCreate(@Nullable Bundle savedInstanceState) { 76 super.onCreate(savedInstanceState); 77 78 mVisibilityLoggerMixin = new VisibilityLoggerMixin(getMetricsCategory(), 79 FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider()); 80 81 final Intent intent = getActivity().getIntent(); 82 final String localeFromIntent = 83 null == intent ? null : intent.getStringExtra("locale"); 84 85 final Bundle arguments = getArguments(); 86 final String localeFromArguments = 87 null == arguments ? null : arguments.getString("locale"); 88 89 final String locale; 90 if (null != localeFromArguments) { 91 locale = localeFromArguments; 92 } else if (null != localeFromIntent) { 93 locale = localeFromIntent; 94 } else { 95 locale = null; 96 } 97 98 mLocale = locale; 99 100 setHasOptionsMenu(true); 101 getLoaderManager().initLoader(LOADER_ID, null, this /* callback */); 102 } 103 104 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)105 public View onCreateView( 106 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 107 // Show the language as a subtitle of the action bar 108 final ActionBar actionBar = getActivity().getActionBar(); 109 if (actionBar != null) { 110 actionBar.setTitle(R.string.user_dict_settings_title); 111 actionBar.setSubtitle( 112 UserDictionarySettingsUtils.getLocaleDisplayName(getActivity(), mLocale)); 113 } 114 115 return inflater.inflate( 116 com.android.internal.R.layout.preference_list_fragment, container, false); 117 } 118 119 @Override onViewCreated(View view, Bundle savedInstanceState)120 public void onViewCreated(View view, Bundle savedInstanceState) { 121 super.onViewCreated(view, savedInstanceState); 122 TextView emptyView = getView().findViewById(android.R.id.empty); 123 emptyView.setText(R.string.user_dict_settings_empty_text); 124 125 final ListView listView = getListView(); 126 listView.setFastScrollEnabled(true); 127 listView.setEmptyView(emptyView); 128 } 129 130 @Override onResume()131 public void onResume() { 132 super.onResume(); 133 mVisibilityLoggerMixin.onResume(); 134 getLoaderManager().restartLoader(LOADER_ID, null, this /* callback */); 135 } 136 createAdapter()137 private ListAdapter createAdapter() { 138 return new MyAdapter(getActivity(), 139 R.layout.user_dictionary_item, mCursor, 140 new String[]{UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT}, 141 new int[]{android.R.id.text1, android.R.id.text2}); 142 } 143 144 @Override onListItemClick(ListView l, View v, int position, long id)145 public void onListItemClick(ListView l, View v, int position, long id) { 146 final String word = getWord(position); 147 final String shortcut = getShortcut(position); 148 if (word != null) { 149 showAddOrEditDialog(word, shortcut); 150 } 151 } 152 153 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)154 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 155 MenuItem actionItem = 156 menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title) 157 .setIcon(R.drawable.ic_add_24dp); 158 actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | 159 MenuItem.SHOW_AS_ACTION_WITH_TEXT); 160 } 161 162 @Override onOptionsItemSelected(MenuItem item)163 public boolean onOptionsItemSelected(MenuItem item) { 164 if (item.getItemId() == OPTIONS_MENU_ADD) { 165 showAddOrEditDialog(null, null); 166 return true; 167 } 168 return false; 169 } 170 171 @Override onPause()172 public void onPause() { 173 super.onPause(); 174 mVisibilityLoggerMixin.onPause(); 175 } 176 177 /** 178 * Add or edit a word. If editingWord is null, it's an add; otherwise, it's an edit. 179 * 180 * @param editingWord the word to edit, or null if it's an add. 181 * @param editingShortcut the shortcut for this entry, or null if none. 182 */ showAddOrEditDialog(final String editingWord, final String editingShortcut)183 private void showAddOrEditDialog(final String editingWord, final String editingShortcut) { 184 final Bundle args = new Bundle(); 185 args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, null == editingWord 186 ? UserDictionaryAddWordContents.MODE_INSERT 187 : UserDictionaryAddWordContents.MODE_EDIT); 188 args.putString(UserDictionaryAddWordContents.EXTRA_WORD, editingWord); 189 args.putString(UserDictionaryAddWordContents.EXTRA_SHORTCUT, editingShortcut); 190 args.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mLocale); 191 192 new SubSettingLauncher(getContext()) 193 .setDestination(UserDictionaryAddWordFragment.class.getName()) 194 .setArguments(args) 195 .setTitleRes(R.string.user_dict_settings_add_dialog_title) 196 .setSourceMetricsCategory(getMetricsCategory()) 197 .launch(); 198 199 } 200 getWord(final int position)201 private String getWord(final int position) { 202 if (null == mCursor) return null; 203 mCursor.moveToPosition(position); 204 // Handle a possible race-condition 205 if (mCursor.isAfterLast()) return null; 206 207 return mCursor.getString( 208 mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD)); 209 } 210 getShortcut(final int position)211 private String getShortcut(final int position) { 212 if (null == mCursor) return null; 213 mCursor.moveToPosition(position); 214 // Handle a possible race-condition 215 if (mCursor.isAfterLast()) return null; 216 217 return mCursor.getString( 218 mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT)); 219 } 220 deleteWord(final String word, final String shortcut, final ContentResolver resolver)221 public static void deleteWord(final String word, final String shortcut, 222 final ContentResolver resolver) { 223 if (TextUtils.isEmpty(shortcut)) { 224 resolver.delete( 225 UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT, 226 new String[]{word}); 227 } else { 228 resolver.delete( 229 UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT, 230 new String[]{word, shortcut}); 231 } 232 } 233 234 @Override onCreateLoader(int id, Bundle args)235 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 236 return new UserDictionaryCursorLoader(getContext(), mLocale); 237 } 238 239 @Override onLoadFinished(Loader<Cursor> loader, Cursor data)240 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 241 mCursor = data; 242 getListView().setAdapter(createAdapter()); 243 } 244 245 @Override onLoaderReset(Loader<Cursor> loader)246 public void onLoaderReset(Loader<Cursor> loader) { 247 248 } 249 250 private static class MyAdapter extends SimpleCursorAdapter implements SectionIndexer { 251 252 private AlphabetIndexer mIndexer; 253 254 private final ViewBinder mViewBinder = new ViewBinder() { 255 256 @Override 257 public boolean setViewValue(View v, Cursor c, int columnIndex) { 258 if (columnIndex == UserDictionaryCursorLoader.INDEX_SHORTCUT) { 259 final String shortcut = c.getString(UserDictionaryCursorLoader.INDEX_SHORTCUT); 260 if (TextUtils.isEmpty(shortcut)) { 261 v.setVisibility(View.GONE); 262 } else { 263 ((TextView) v).setText(shortcut); 264 v.setVisibility(View.VISIBLE); 265 } 266 v.invalidate(); 267 return true; 268 } 269 270 return false; 271 } 272 }; 273 MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to)274 public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 275 super(context, layout, c, from, to); 276 277 if (null != c) { 278 final String alphabet = context.getString( 279 com.android.internal.R.string.fast_scroll_alphabet); 280 final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD); 281 mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet); 282 } 283 setViewBinder(mViewBinder); 284 } 285 286 @Override getPositionForSection(int section)287 public int getPositionForSection(int section) { 288 return null == mIndexer ? 0 : mIndexer.getPositionForSection(section); 289 } 290 291 @Override getSectionForPosition(int position)292 public int getSectionForPosition(int position) { 293 return null == mIndexer ? 0 : mIndexer.getSectionForPosition(position); 294 } 295 296 @Override getSections()297 public Object[] getSections() { 298 return null == mIndexer ? null : mIndexer.getSections(); 299 } 300 } 301 } 302