blob: 5d3c8ac37dfd8cd9bdb93824429b0bef1edd03c3 [file] [log] [blame]
The Android Open Source Project590c0a92009-01-22 00:13:44 -08001/**
2 * Copyright (C) 2007 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17package com.android.settings;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.ListActivity;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.database.Cursor;
25import android.os.Bundle;
26import android.provider.UserDictionary;
27import android.view.ContextMenu;
28import android.view.Menu;
29import android.view.MenuItem;
30import android.view.View;
31import android.view.ContextMenu.ContextMenuInfo;
32import android.widget.AlphabetIndexer;
33import android.widget.EditText;
34import android.widget.ListAdapter;
35import android.widget.ListView;
36import android.widget.SectionIndexer;
37import android.widget.SimpleCursorAdapter;
38import android.widget.TextView;
39import android.widget.AdapterView.AdapterContextMenuInfo;
40
41import java.util.Locale;
42
43public class UserDictionarySettings extends ListActivity {
44
45 private static final String INSTANCE_KEY_DIALOG_EDITING_WORD = "DIALOG_EDITING_WORD";
46
47 private static final String[] QUERY_PROJECTION = {
48 UserDictionary.Words._ID, UserDictionary.Words.WORD
49 };
50
51 // Either the locale is empty (means the word is applicable to all locales)
52 // or the word equals our current locale
53 private static final String QUERY_SELECTION = UserDictionary.Words.LOCALE + "=? OR "
54 + UserDictionary.Words.LOCALE + " is null";
55
56 private static final String DELETE_SELECTION = UserDictionary.Words.WORD + "=?";
57
58 private static final int CONTEXT_MENU_EDIT = Menu.FIRST;
59 private static final int CONTEXT_MENU_DELETE = Menu.FIRST + 1;
60
61 private static final int OPTIONS_MENU_ADD = Menu.FIRST;
62
63 private static final int DIALOG_ADD_OR_EDIT = 0;
64
65 /** The word being edited in the dialog (null means the user is adding a word). */
66 private String mDialogEditingWord;
67
68 private Cursor mCursor;
69
70 @Override
71 protected void onCreate(Bundle savedInstanceState) {
72 super.onCreate(savedInstanceState);
73
74 setContentView(R.layout.list_content_with_empty_view);
75
76 mCursor = createCursor();
77 setListAdapter(createAdapter());
78
79 TextView emptyView = (TextView) findViewById(R.id.empty);
80 emptyView.setText(R.string.user_dict_settings_empty_text);
81
82 ListView listView = getListView();
83 listView.setFastScrollEnabled(true);
84 listView.setEmptyView(emptyView);
85
86 registerForContextMenu(listView);
87 }
88
89 @Override
90 protected void onRestoreInstanceState(Bundle state) {
91 super.onRestoreInstanceState(state);
92 mDialogEditingWord = state.getString(INSTANCE_KEY_DIALOG_EDITING_WORD);
93 }
94
95 @Override
96 protected void onSaveInstanceState(Bundle outState) {
97 super.onSaveInstanceState(outState);
98 outState.putString(INSTANCE_KEY_DIALOG_EDITING_WORD, mDialogEditingWord);
99 }
100
101 private Cursor createCursor() {
102 String currentLocale = Locale.getDefault().toString();
103 // Case-insensitive sort
104 return managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION,
105 QUERY_SELECTION, new String[] { currentLocale },
106 "UPPER(" + UserDictionary.Words.WORD + ")");
107 }
108
109 private ListAdapter createAdapter() {
110 return new MyAdapter(this,
111 android.R.layout.simple_list_item_1, mCursor,
112 new String[] { UserDictionary.Words.WORD },
113 new int[] { android.R.id.text1 });
114 }
115
116 @Override
117 protected void onListItemClick(ListView l, View v, int position, long id) {
118 showAddOrEditDialog(getWord(position));
119 }
120
121 @Override
122 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
123 if (!(menuInfo instanceof AdapterContextMenuInfo)) return;
124
125 AdapterContextMenuInfo adapterMenuInfo = (AdapterContextMenuInfo) menuInfo;
126 menu.setHeaderTitle(getWord(adapterMenuInfo.position));
127 menu.add(0, CONTEXT_MENU_EDIT, 0, R.string.user_dict_settings_context_menu_edit_title);
128 menu.add(0, CONTEXT_MENU_DELETE, 0, R.string.user_dict_settings_context_menu_delete_title);
129 }
130
131 @Override
132 public boolean onContextItemSelected(MenuItem item) {
133 ContextMenuInfo menuInfo = item.getMenuInfo();
134 if (!(menuInfo instanceof AdapterContextMenuInfo)) return false;
135
136 AdapterContextMenuInfo adapterMenuInfo = (AdapterContextMenuInfo) menuInfo;
137 String word = getWord(adapterMenuInfo.position);
138
139 switch (item.getItemId()) {
140 case CONTEXT_MENU_DELETE:
141 deleteWord(word);
142 return true;
143
144 case CONTEXT_MENU_EDIT:
145 showAddOrEditDialog(word);
146 return true;
147 }
148
149 return false;
150 }
151
152 @Override
153 public boolean onCreateOptionsMenu(Menu menu) {
154 menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title)
155 .setIcon(R.drawable.ic_menu_add);
156 return true;
157 }
158
159 @Override
160 public boolean onOptionsItemSelected(MenuItem item) {
161 showAddOrEditDialog(null);
162 return true;
163 }
164
165 private void showAddOrEditDialog(String editingWord) {
166 mDialogEditingWord = editingWord;
167 showDialog(DIALOG_ADD_OR_EDIT);
168 }
169
170 private String getWord(int position) {
171 mCursor.moveToPosition(position);
172 return mCursor.getString(
173 mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD));
174 }
175
176 @Override
177 protected Dialog onCreateDialog(int id) {
178 View content = getLayoutInflater().inflate(R.layout.dialog_edittext, null);
179 final EditText editText = (EditText) content.findViewById(R.id.edittext);
180
181 return new AlertDialog.Builder(this)
182 .setTitle(R.string.user_dict_settings_add_dialog_title)
183 .setView(content)
184 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
185 public void onClick(DialogInterface dialog, int which) {
186 onAddOrEditFinished(editText.getText().toString());
187 }})
188 .setNegativeButton(android.R.string.cancel, null)
189 .create();
190 }
191
192 @Override
193 protected void onPrepareDialog(int id, Dialog d) {
194 AlertDialog dialog = (AlertDialog) d;
195 EditText editText = (EditText) dialog.findViewById(R.id.edittext);
196 editText.setText(mDialogEditingWord);
197 }
198
199 private void onAddOrEditFinished(String word) {
200 if (mDialogEditingWord != null) {
201 // The user was editing a word, so do a delete/add
202 deleteWord(mDialogEditingWord);
203 }
204
205 // Disallow duplicates
206 deleteWord(word);
207
208 // TODO: present UI for picking whether to add word to all locales, or current.
209 UserDictionary.Words.addWord(this, word.toString(),
210 1, UserDictionary.Words.LOCALE_TYPE_ALL);
211 mCursor.requery();
212 }
213
214 private void deleteWord(String word) {
215 getContentResolver().delete(UserDictionary.Words.CONTENT_URI, DELETE_SELECTION,
216 new String[] { word });
217 }
218
219 private static class MyAdapter extends SimpleCursorAdapter implements SectionIndexer {
220 private AlphabetIndexer mIndexer;
221
222 public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
223 super(context, layout, c, from, to);
224
225 int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD);
226 String alphabet = context.getString(com.android.internal.R.string.fast_scroll_alphabet);
227 mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet);
228 }
229
230 public int getPositionForSection(int section) {
231 return mIndexer.getPositionForSection(section);
232 }
233
234 public int getSectionForPosition(int position) {
235 return mIndexer.getSectionForPosition(position);
236 }
237
238 public Object[] getSections() {
239 return mIndexer.getSections();
240 }
241 }
242}