Move adding word functionality to the new interface

Step 4

Bug: 5306641
Change-Id: I150fd93e9802e92b4cf084867f0a9d1bc382cdae
This commit is contained in:
Jean Chalard
2011-12-02 19:15:47 +09:00
parent 1800130ff1
commit 6b8e6585c6
4 changed files with 89 additions and 129 deletions

View File

@@ -470,6 +470,7 @@
android:excludeFromRecents="true">
<intent-filter>
<action android:name="com.android.settings.USER_DICTIONARY_INSERT" />
<action android:name="com.android.settings.USER_DICTIONARY_EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE_LAUNCH" />
</intent-filter>

View File

@@ -84,8 +84,8 @@
style="?android:attr/buttonBarButtonStyle"
android:textSize="14sp"
android:text="@string/user_dict_settings_add_dialog_confirm"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:onClick="onClickConfirm" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

View File

@@ -20,6 +20,7 @@ import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListFragment;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@@ -44,16 +45,13 @@ import android.widget.SectionIndexer;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment;
import com.android.settings.inputmethod.UserDictionaryAddWordActivity;
import java.util.Locale;
public class UserDictionarySettings extends ListFragment implements DialogCreatable {
public class UserDictionarySettings extends ListFragment {
private static final String TAG = "UserDictionarySettings";
private static final String INSTANCE_KEY_DIALOG_EDITING_WORD = "DIALOG_EDITING_WORD";
private static final String INSTANCE_KEY_ADDED_WORD = "DIALOG_ADDED_WORD";
private static final String[] QUERY_PROJECTION = {
UserDictionary.Words._ID, UserDictionary.Words.WORD
};
@@ -70,26 +68,12 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
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 Cursor mCursor;
protected String mLocale;
private boolean mAddedWordAlready;
private boolean mAutoReturn;
private SettingsDialogFragment mDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -135,31 +119,6 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
setHasOptionsMenu(true);
if (savedInstanceState != null) {
mDialogEditingWord = savedInstanceState.getString(INSTANCE_KEY_DIALOG_EDITING_WORD);
mAddedWordAlready = savedInstanceState.getBoolean(INSTANCE_KEY_ADDED_WORD, false);
}
}
@Override
public void onResume() {
super.onResume();
final Intent intent = getActivity().getIntent();
if (!mAddedWordAlready
&& intent.getAction().equals("com.android.settings.USER_DICTIONARY_INSERT")) {
final String word = intent.getStringExtra(EXTRA_WORD);
mAutoReturn = true;
if (word != null) {
showAddOrEditDialog(word);
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(INSTANCE_KEY_DIALOG_EDITING_WORD, mDialogEditingWord);
outState.putBoolean(INSTANCE_KEY_ADDED_WORD, mAddedWordAlready);
}
private Cursor createCursor(final String locale) {
@@ -216,9 +175,18 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
return true;
}
private void showAddOrEditDialog(String editingWord) {
mDialogEditingWord = editingWord;
showDialog(DIALOG_ADD_OR_EDIT);
/**
* Add or edit a word. If editingWord is null, it's an add; otherwise, it's an edit.
* @param editingWord the word to edit, or null if it's an add.
*/
private void showAddOrEditDialog(final String editingWord) {
final Intent intent = new Intent(null == editingWord
? UserDictionaryAddWordActivity.MODE_INSERT_ACTION
: UserDictionaryAddWordActivity.MODE_EDIT_ACTION);
// The following are fine if they are null
intent.putExtra(UserDictionaryAddWordActivity.EXTRA_WORD, editingWord);
intent.putExtra(UserDictionaryAddWordActivity.EXTRA_LOCALE, mLocale);
startActivity(intent);
}
private String getWord(int position) {
@@ -231,81 +199,8 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD));
}
@Override
public Dialog onCreateDialog(int id) {
final Activity activity = getActivity();
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
final LayoutInflater inflater = LayoutInflater.from(dialogBuilder.getContext());
final View content = inflater.inflate(R.layout.dialog_edittext, null);
final EditText editText = (EditText) content.findViewById(R.id.edittext);
editText.setText(mDialogEditingWord);
// No prediction in soft keyboard mode. TODO: Create a better way to disable prediction
editText.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
AlertDialog dialog = dialogBuilder
.setTitle(mDialogEditingWord != null
? R.string.user_dict_settings_edit_dialog_title
: R.string.user_dict_settings_add_dialog_title)
.setView(content)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
onAddOrEditFinished(editText.getText().toString());
if (mAutoReturn) activity.onBackPressed();
}})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (mAutoReturn) activity.onBackPressed();
}})
.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN |
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return dialog;
}
private void showDialog(int dialogId) {
if (mDialogFragment != null) {
Log.e(TAG, "Old dialog fragment not null!");
}
mDialogFragment = new SettingsDialogFragment(this, dialogId);
mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
}
private void onAddOrEditFinished(String word) {
if (mDialogEditingWord != null) {
// 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.
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;
}
private void deleteWord(String word) {
getActivity().getContentResolver().delete(
public static void deleteWord(final String word, final ContentResolver resolver) {
resolver.delete(
UserDictionary.Words.CONTENT_URI, DELETE_SELECTION, new String[] { word });
}
@@ -355,7 +250,8 @@ public class UserDictionarySettings extends ListFragment implements DialogCreata
}
public void onClick(View v) {
mSettings.deleteWord((String) v.getTag());
UserDictionarySettings.deleteWord((String) v.getTag(),
mSettings.getActivity().getContentResolver());
}
}
}

View File

@@ -17,30 +17,93 @@
package com.android.settings.inputmethod;
import com.android.settings.R;
import com.android.settings.UserDictionarySettings;
import com.android.settings.Utils;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.UserDictionary;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
public class UserDictionaryAddWordActivity extends Activity {
private static final String EXTRA_WORD = "word";
public static final String EXTRA_WORD = "word";
public static final String EXTRA_LOCALE = "locale";
private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250;
public static final String MODE_EDIT_ACTION = "com.android.settings.USER_DICTIONARY_EDIT";
public static final String MODE_INSERT_ACTION = "com.android.settings.USER_DICTIONARY_INSERT";
private static final int MODE_EDIT = 0;
private static final int MODE_INSERT = 1;
private EditText mEditText;
private int mMode; // Either MODE_EDIT or MODE_INSERT
private String mOldWord;
private String mLocale; // may be null
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_dictionary_add_word);
final Intent intent = getIntent();
final String word = intent.getStringExtra(EXTRA_WORD);
final String action = intent.getAction();
if (MODE_EDIT_ACTION.equals(action)) {
mMode = MODE_EDIT;
} else if (MODE_INSERT_ACTION.equals(action)) {
mMode = MODE_INSERT;
} else {
// Can never come here because we only support these two actions in the manifest
throw new RuntimeException("Unsupported action: " + action);
}
mOldWord = intent.getStringExtra(EXTRA_WORD);
mLocale = intent.getStringExtra(EXTRA_LOCALE); // this may be null
mEditText = (EditText)findViewById(R.id.user_dictionary_add_word_text);
mEditText.setText(word);
mEditText.setSelection(word.length());
if (null != mOldWord) {
mEditText.setText(mOldWord);
mEditText.setSelection(mOldWord.length());
}
}
public void onClickCancel(final View v) {
finish();
}
public void onClickConfirm(final View v) {
if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) {
UserDictionarySettings.deleteWord(mOldWord, this.getContentResolver());
}
final String newWord = mEditText.getText().toString();
if (TextUtils.isEmpty(newWord)) {
// If the word is somehow empty, don't insert it.
// TODO: grey out the Ok button when the text is empty?
finish();
return;
}
// Disallow duplicates.
// TODO: Redefine the logic when we support shortcuts.
UserDictionarySettings.deleteWord(newWord, this.getContentResolver());
if (null == mLocale) {
// Null means insert with the default system locale.
UserDictionary.Words.addWord(this, newWord.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(this, newWord.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(this, newWord.toString(),
FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_CURRENT);
Locale.setDefault(prevLocale);
}
finish();
}
}