Factor some code

Bug: 6026080
Change-Id: I38475a95b1d421162099390244dbda09658346f5
diff --git a/src/com/android/settings/UserDictionarySettings.java b/src/com/android/settings/UserDictionarySettings.java
index 756c592..8d4e8b4 100644
--- a/src/com/android/settings/UserDictionarySettings.java
+++ b/src/com/android/settings/UserDictionarySettings.java
@@ -45,7 +45,7 @@
 import android.widget.SimpleCursorAdapter;
 import android.widget.TextView;
 
-import com.android.settings.inputmethod.UserDictionaryAddWordActivity;
+import com.android.settings.inputmethod.UserDictionaryAddWordContents;
 
 import java.util.Locale;
 
@@ -184,11 +184,11 @@
      */
     private void showAddOrEditDialog(final String editingWord) {
         final Bundle args = new Bundle();
-        args.putString(UserDictionaryAddWordActivity.EXTRA_MODE, null == editingWord
-                ? UserDictionaryAddWordActivity.MODE_INSERT_ACTION
-                : UserDictionaryAddWordActivity.MODE_EDIT_ACTION);
-        args.putString(UserDictionaryAddWordActivity.EXTRA_WORD, editingWord);
-        args.putString(UserDictionaryAddWordActivity.EXTRA_LOCALE, mLocale);
+        args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, null == editingWord
+                ? UserDictionaryAddWordContents.MODE_INSERT
+                : UserDictionaryAddWordContents.MODE_EDIT);
+        args.putString(UserDictionaryAddWordContents.EXTRA_WORD, editingWord);
+        args.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mLocale);
         android.preference.PreferenceActivity pa =
                 (android.preference.PreferenceActivity)getActivity();
         pa.startPreferencePanel(
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java
index b58f258..e502b7a 100644
--- a/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java
+++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java
@@ -41,19 +41,12 @@
 
 public class UserDictionaryAddWordActivity extends Activity
         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 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_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 = {
         R.id.user_dictionary_add_word_label,
@@ -63,10 +56,8 @@
         R.id.user_dictionary_settings_add_dialog_locale,
     };
 
-    private EditText mEditText;
-    private int mMode; // Either MODE_EDIT or MODE_INSERT
+    private UserDictionaryAddWordContents mContents;
     private String mOldWord;
-    private String mLocale; // may not be null: will be converted to default locale if received null
 
     private boolean mIsShowingMoreOptions = false;
 
@@ -76,39 +67,32 @@
         setContentView(R.layout.user_dictionary_add_word);
         final Intent intent = getIntent();
         final String action = intent.getAction();
+        final int mode;
         if (MODE_EDIT_ACTION.equals(action)) {
-            mMode = MODE_EDIT;
+            mode = UserDictionaryAddWordContents.MODE_EDIT;
         } else if (MODE_INSERT_ACTION.equals(action)) {
-            mMode = MODE_INSERT;
+            mode = UserDictionaryAddWordContents.MODE_INSERT;
         } else {
             // Can never come here because we only support these two actions in the manifest
             throw new RuntimeException("Unsupported action: " + action);
         }
 
-        String savedWord = null;
-        String savedLocale = null;
+        // The following will get the EXTRA_WORD and EXTRA_LOCALE fields that are in the intent.
+        // 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) {
             mIsShowingMoreOptions =
                     savedInstanceState.getBoolean(STATE_KEY_IS_OPEN, mIsShowingMoreOptions);
-            savedWord = savedInstanceState.getString(STATE_KEY_WORD);
-            savedLocale = savedInstanceState.getString(STATE_KEY_LOCALE);
+            // Override options if we have a saved state.
+            args.putAll(savedInstanceState);
         }
 
-        mOldWord = intent.getStringExtra(EXTRA_WORD);
-        if (null != savedLocale) {
-            mLocale = savedLocale;
-        } 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());
-        }
+        mOldWord = intent.getStringExtra(UserDictionaryAddWordContents.EXTRA_WORD);
+
+        mContents = new UserDictionaryAddWordContents(getWindow().getDecorView(), args);
 
         if (mIsShowingMoreOptions) {
             onClickMoreOptions(findViewById(R.id.user_dictionary_settings_add_dialog_more_options));
@@ -126,8 +110,9 @@
     @Override
     public void onSaveInstanceState(final Bundle outState) {
         outState.putBoolean(STATE_KEY_IS_OPEN, mIsShowingMoreOptions);
-        outState.putString(STATE_KEY_WORD, mEditText.getText().toString());
-        outState.putString(STATE_KEY_LOCALE, mLocale);
+        outState.putString(
+                UserDictionaryAddWordContents.EXTRA_WORD, mContents.mEditText.getText().toString());
+        outState.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mContents.mLocale);
     }
 
     public void onClickCancel(final View v) {
@@ -135,10 +120,11 @@
     }
 
     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());
         }
-        final String newWord = mEditText.getText().toString();
+        final String newWord = mContents.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?
@@ -149,7 +135,7 @@
         // TODO: Redefine the logic when we support shortcuts.
         UserDictionarySettings.deleteWord(newWord, this.getContentResolver());
 
-        if (TextUtils.isEmpty(mLocale)) {
+        if (TextUtils.isEmpty(mContents.mLocale)) {
             // Empty string means insert for all languages.
             UserDictionary.Words.addWord(this, newWord.toString(),
                     FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_ALL);
@@ -157,7 +143,7 @@
             // 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));
+            Locale.setDefault(Utils.createLocaleFromString(mContents.mLocale));
             UserDictionary.Words.addWord(this, newWord.toString(),
                     FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_CURRENT);
             Locale.setDefault(prevLocale);
@@ -207,7 +193,7 @@
 
         final Set<String> locales = UserDictionaryList.getUserDictionaryLocalesList(this);
         // 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();
         // The system locale should be inside. We want it at the 2nd spot.
         locales.remove(systemLocale); // system locale may not be null
@@ -215,8 +201,8 @@
         final ArrayList<LocaleRenderer> localesList = new ArrayList<LocaleRenderer>();
         // 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.
-        addLocaleDisplayNameToList(this, localesList, mLocale);
-        if (!systemLocale.equals(mLocale)) {
+        addLocaleDisplayNameToList(this, localesList, mContents.mLocale);
+        if (!systemLocale.equals(mContents.mLocale)) {
             addLocaleDisplayNameToList(this, localesList, systemLocale);
         }
         for (final String l : locales) {
@@ -251,14 +237,13 @@
     public void onItemSelected(final AdapterView<?> parent, final View view, final int pos,
             final long id) {
         final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos);
-        mLocale = locale.getLocaleString();
+        mContents.updateLocale(locale.getLocaleString());
     }
 
     @Override
     public void onNothingSelected(AdapterView<?> parent) {
         // I'm not sure we can come here, but if we do, that's the right thing to do.
         final Intent intent = getIntent();
-        final String locale = intent.getStringExtra(EXTRA_LOCALE); // this may be null
-        mLocale = null == locale ? Locale.getDefault().toString() : locale;
+        mContents.updateLocale(intent.getStringExtra(UserDictionaryAddWordContents.EXTRA_LOCALE));
     }
 }
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java
new file mode 100644
index 0000000..b67e834
--- /dev/null
+++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java
@@ -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;
+    }
+}
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java
index 9f3bad9..d5f4e3f 100644
--- a/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java
+++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java
@@ -36,6 +36,8 @@
 
     private static final int OPTIONS_MENU_DELETE = Menu.FIRST;
 
+    private UserDictionaryAddWordContents mContents;
+
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
@@ -44,7 +46,9 @@
 
     @Override
     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