Move adding word functionality to the new interface
Step 4 Bug: 5306641 Change-Id: I150fd93e9802e92b4cf084867f0a9d1bc382cdae
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user