Factor some code

Bug: 6026080
Change-Id: I38475a95b1d421162099390244dbda09658346f5
This commit is contained in:
Jean Chalard
2012-05-08 19:11:49 +09:00
parent be07e49a92
commit be3f0faf52
4 changed files with 98 additions and 50 deletions

View File

@@ -45,7 +45,7 @@ import android.widget.SectionIndexer;
import android.widget.SimpleCursorAdapter; import android.widget.SimpleCursorAdapter;
import android.widget.TextView; import android.widget.TextView;
import com.android.settings.inputmethod.UserDictionaryAddWordActivity; import com.android.settings.inputmethod.UserDictionaryAddWordContents;
import java.util.Locale; import java.util.Locale;
@@ -184,11 +184,11 @@ public class UserDictionarySettings extends ListFragment {
*/ */
private void showAddOrEditDialog(final String editingWord) { private void showAddOrEditDialog(final String editingWord) {
final Bundle args = new Bundle(); final Bundle args = new Bundle();
args.putString(UserDictionaryAddWordActivity.EXTRA_MODE, null == editingWord args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, null == editingWord
? UserDictionaryAddWordActivity.MODE_INSERT_ACTION ? UserDictionaryAddWordContents.MODE_INSERT
: UserDictionaryAddWordActivity.MODE_EDIT_ACTION); : UserDictionaryAddWordContents.MODE_EDIT);
args.putString(UserDictionaryAddWordActivity.EXTRA_WORD, editingWord); args.putString(UserDictionaryAddWordContents.EXTRA_WORD, editingWord);
args.putString(UserDictionaryAddWordActivity.EXTRA_LOCALE, mLocale); args.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mLocale);
android.preference.PreferenceActivity pa = android.preference.PreferenceActivity pa =
(android.preference.PreferenceActivity)getActivity(); (android.preference.PreferenceActivity)getActivity();
pa.startPreferencePanel( pa.startPreferencePanel(

View File

@@ -41,19 +41,12 @@ import android.widget.Spinner;
public class UserDictionaryAddWordActivity extends Activity public class UserDictionaryAddWordActivity extends Activity
implements AdapterView.OnItemSelectedListener { implements AdapterView.OnItemSelectedListener {
public static final String EXTRA_MODE = "mode";
public static final String EXTRA_WORD = "word";
public static final String EXTRA_LOCALE = "locale";
private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250; private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250;
private static final String STATE_KEY_IS_OPEN = "isOpen"; private static final String STATE_KEY_IS_OPEN = "isOpen";
private static final String STATE_KEY_WORD = "word";
private static final String STATE_KEY_LOCALE = "locale";
public static final String MODE_EDIT_ACTION = "com.android.settings.USER_DICTIONARY_EDIT"; 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"; 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 static final int[] IDS_SHOWN_ONLY_IN_MORE_OPTIONS_MODE = { private static final int[] IDS_SHOWN_ONLY_IN_MORE_OPTIONS_MODE = {
R.id.user_dictionary_add_word_label, R.id.user_dictionary_add_word_label,
@@ -63,10 +56,8 @@ public class UserDictionaryAddWordActivity extends Activity
R.id.user_dictionary_settings_add_dialog_locale, R.id.user_dictionary_settings_add_dialog_locale,
}; };
private EditText mEditText; private UserDictionaryAddWordContents mContents;
private int mMode; // Either MODE_EDIT or MODE_INSERT
private String mOldWord; private String mOldWord;
private String mLocale; // may not be null: will be converted to default locale if received null
private boolean mIsShowingMoreOptions = false; private boolean mIsShowingMoreOptions = false;
@@ -76,39 +67,32 @@ public class UserDictionaryAddWordActivity extends Activity
setContentView(R.layout.user_dictionary_add_word); setContentView(R.layout.user_dictionary_add_word);
final Intent intent = getIntent(); final Intent intent = getIntent();
final String action = intent.getAction(); final String action = intent.getAction();
final int mode;
if (MODE_EDIT_ACTION.equals(action)) { if (MODE_EDIT_ACTION.equals(action)) {
mMode = MODE_EDIT; mode = UserDictionaryAddWordContents.MODE_EDIT;
} else if (MODE_INSERT_ACTION.equals(action)) { } else if (MODE_INSERT_ACTION.equals(action)) {
mMode = MODE_INSERT; mode = UserDictionaryAddWordContents.MODE_INSERT;
} else { } else {
// Can never come here because we only support these two actions in the manifest // Can never come here because we only support these two actions in the manifest
throw new RuntimeException("Unsupported action: " + action); throw new RuntimeException("Unsupported action: " + action);
} }
String savedWord = null; // The following will get the EXTRA_WORD and EXTRA_LOCALE fields that are in the intent.
String savedLocale = null; // We do need to add the action by hand, because UserDictionaryAddWordContents expects
// it to be in the bundle, in the EXTRA_MODE key.
final Bundle args = intent.getExtras();
args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, mode);
if (null != savedInstanceState) { if (null != savedInstanceState) {
mIsShowingMoreOptions = mIsShowingMoreOptions =
savedInstanceState.getBoolean(STATE_KEY_IS_OPEN, mIsShowingMoreOptions); savedInstanceState.getBoolean(STATE_KEY_IS_OPEN, mIsShowingMoreOptions);
savedWord = savedInstanceState.getString(STATE_KEY_WORD); // Override options if we have a saved state.
savedLocale = savedInstanceState.getString(STATE_KEY_LOCALE); args.putAll(savedInstanceState);
} }
mOldWord = intent.getStringExtra(EXTRA_WORD); mOldWord = intent.getStringExtra(UserDictionaryAddWordContents.EXTRA_WORD);
if (null != savedLocale) {
mLocale = savedLocale; mContents = new UserDictionaryAddWordContents(getWindow().getDecorView(), args);
} else {
final String locale = intent.getStringExtra(EXTRA_LOCALE); // this may be null
mLocale = null == locale ? Locale.getDefault().toString() : locale;
}
mEditText = (EditText)findViewById(R.id.user_dictionary_add_word_text);
if (null != savedWord) {
mEditText.setText(savedWord);
mEditText.setSelection(savedWord.length());
} else if (null != mOldWord) {
mEditText.setText(mOldWord);
mEditText.setSelection(mOldWord.length());
}
if (mIsShowingMoreOptions) { if (mIsShowingMoreOptions) {
onClickMoreOptions(findViewById(R.id.user_dictionary_settings_add_dialog_more_options)); onClickMoreOptions(findViewById(R.id.user_dictionary_settings_add_dialog_more_options));
@@ -126,8 +110,9 @@ public class UserDictionaryAddWordActivity extends Activity
@Override @Override
public void onSaveInstanceState(final Bundle outState) { public void onSaveInstanceState(final Bundle outState) {
outState.putBoolean(STATE_KEY_IS_OPEN, mIsShowingMoreOptions); outState.putBoolean(STATE_KEY_IS_OPEN, mIsShowingMoreOptions);
outState.putString(STATE_KEY_WORD, mEditText.getText().toString()); outState.putString(
outState.putString(STATE_KEY_LOCALE, mLocale); UserDictionaryAddWordContents.EXTRA_WORD, mContents.mEditText.getText().toString());
outState.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mContents.mLocale);
} }
public void onClickCancel(final View v) { public void onClickCancel(final View v) {
@@ -135,10 +120,11 @@ public class UserDictionaryAddWordActivity extends Activity
} }
public void onClickConfirm(final View v) { public void onClickConfirm(final View v) {
if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { if (UserDictionaryAddWordContents.MODE_EDIT == mContents.mMode
&& !TextUtils.isEmpty(mOldWord)) {
UserDictionarySettings.deleteWord(mOldWord, this.getContentResolver()); UserDictionarySettings.deleteWord(mOldWord, this.getContentResolver());
} }
final String newWord = mEditText.getText().toString(); final String newWord = mContents.mEditText.getText().toString();
if (TextUtils.isEmpty(newWord)) { if (TextUtils.isEmpty(newWord)) {
// If the word is somehow empty, don't insert it. // If the word is somehow empty, don't insert it.
// TODO: grey out the Ok button when the text is empty? // TODO: grey out the Ok button when the text is empty?
@@ -149,7 +135,7 @@ public class UserDictionaryAddWordActivity extends Activity
// TODO: Redefine the logic when we support shortcuts. // TODO: Redefine the logic when we support shortcuts.
UserDictionarySettings.deleteWord(newWord, this.getContentResolver()); UserDictionarySettings.deleteWord(newWord, this.getContentResolver());
if (TextUtils.isEmpty(mLocale)) { if (TextUtils.isEmpty(mContents.mLocale)) {
// Empty string means insert for all languages. // Empty string means insert for all languages.
UserDictionary.Words.addWord(this, newWord.toString(), UserDictionary.Words.addWord(this, newWord.toString(),
FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_ALL); FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_ALL);
@@ -157,7 +143,7 @@ public class UserDictionaryAddWordActivity extends Activity
// TODO: fix the framework so that it can accept a locale when we add a word // 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. // to the user dictionary instead of querying the system locale.
final Locale prevLocale = Locale.getDefault(); final Locale prevLocale = Locale.getDefault();
Locale.setDefault(Utils.createLocaleFromString(mLocale)); Locale.setDefault(Utils.createLocaleFromString(mContents.mLocale));
UserDictionary.Words.addWord(this, newWord.toString(), UserDictionary.Words.addWord(this, newWord.toString(),
FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_CURRENT); FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_CURRENT);
Locale.setDefault(prevLocale); Locale.setDefault(prevLocale);
@@ -207,7 +193,7 @@ public class UserDictionaryAddWordActivity extends Activity
final Set<String> locales = UserDictionaryList.getUserDictionaryLocalesList(this); final Set<String> locales = UserDictionaryList.getUserDictionaryLocalesList(this);
// Remove our locale if it's in, because we're always gonna put it at the top // Remove our locale if it's in, because we're always gonna put it at the top
locales.remove(mLocale); // mLocale may not be null locales.remove(mContents.mLocale); // mLocale may not be null
final String systemLocale = Locale.getDefault().toString(); final String systemLocale = Locale.getDefault().toString();
// The system locale should be inside. We want it at the 2nd spot. // The system locale should be inside. We want it at the 2nd spot.
locales.remove(systemLocale); // system locale may not be null locales.remove(systemLocale); // system locale may not be null
@@ -215,8 +201,8 @@ public class UserDictionaryAddWordActivity extends Activity
final ArrayList<LocaleRenderer> localesList = new ArrayList<LocaleRenderer>(); final ArrayList<LocaleRenderer> localesList = new ArrayList<LocaleRenderer>();
// Add the passed locale, then the system locale at the top of the list. Add an // Add the passed locale, then the system locale at the top of the list. Add an
// "all languages" entry at the bottom of the list. // "all languages" entry at the bottom of the list.
addLocaleDisplayNameToList(this, localesList, mLocale); addLocaleDisplayNameToList(this, localesList, mContents.mLocale);
if (!systemLocale.equals(mLocale)) { if (!systemLocale.equals(mContents.mLocale)) {
addLocaleDisplayNameToList(this, localesList, systemLocale); addLocaleDisplayNameToList(this, localesList, systemLocale);
} }
for (final String l : locales) { for (final String l : locales) {
@@ -251,14 +237,13 @@ public class UserDictionaryAddWordActivity extends Activity
public void onItemSelected(final AdapterView<?> parent, final View view, final int pos, public void onItemSelected(final AdapterView<?> parent, final View view, final int pos,
final long id) { final long id) {
final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos); final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos);
mLocale = locale.getLocaleString(); mContents.updateLocale(locale.getLocaleString());
} }
@Override @Override
public void onNothingSelected(AdapterView<?> parent) { public void onNothingSelected(AdapterView<?> parent) {
// I'm not sure we can come here, but if we do, that's the right thing to do. // I'm not sure we can come here, but if we do, that's the right thing to do.
final Intent intent = getIntent(); final Intent intent = getIntent();
final String locale = intent.getStringExtra(EXTRA_LOCALE); // this may be null mContents.updateLocale(intent.getStringExtra(UserDictionaryAddWordContents.EXTRA_LOCALE));
mLocale = null == locale ? Locale.getDefault().toString() : locale;
} }
} }

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.inputmethod;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.android.settings.R;
import java.util.Locale;
/**
* A container class to factor common code to UserDictionaryAddWordFragment
* and UserDictionaryAddWordActivity.
*/
public class UserDictionaryAddWordContents {
public static final String EXTRA_MODE = "mode";
public static final String EXTRA_WORD = "word";
public static final String EXTRA_LOCALE = "locale";
public static final int MODE_EDIT = 0;
public static final int MODE_INSERT = 1;
/* package */ final int mMode; // Either MODE_EDIT or MODE_INSERT
/* package */ final EditText mEditText;
/* package */ String mLocale;
/* package */ UserDictionaryAddWordContents(final View view, final Bundle args) {
mEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text);
final String word = args.getString(EXTRA_WORD);
if (null != word) {
mEditText.setText(word);
mEditText.setSelection(word.length());
}
mMode = args.getInt(EXTRA_MODE); // default return value for #getInt() is 0 = MODE_EDIT
updateLocale(args.getString(EXTRA_LOCALE));
}
// locale may be null, this means default locale
// It may also be the empty string, which means "all locales"
/* package */ void updateLocale(final String locale) {
mLocale = null == locale ? Locale.getDefault().toString() : locale;
}
}

View File

@@ -36,6 +36,8 @@ public class UserDictionaryAddWordFragment extends Fragment {
private static final int OPTIONS_MENU_DELETE = Menu.FIRST; private static final int OPTIONS_MENU_DELETE = Menu.FIRST;
private UserDictionaryAddWordContents mContents;
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
@@ -44,7 +46,9 @@ public class UserDictionaryAddWordFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
return inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null); final View view = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null);
mContents = new UserDictionaryAddWordContents(view, getArguments());
return view;
} }
@Override @Override