Move the word commit code to UserDictionaryAddWordContent.

...and call it from the fragment too.

Bug: 6026080
Change-Id: Icb83fd60ed33dfa593124ed0af6b9fff4b63f1f5
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java
index e502b7a..f741d19 100644
--- a/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java
+++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java
@@ -41,7 +41,6 @@
 
 public class UserDictionaryAddWordActivity extends Activity
         implements AdapterView.OnItemSelectedListener {
-    private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250;
 
     private static final String STATE_KEY_IS_OPEN = "isOpen";
 
@@ -57,7 +56,6 @@
     };
 
     private UserDictionaryAddWordContents mContents;
-    private String mOldWord;
 
     private boolean mIsShowingMoreOptions = false;
 
@@ -90,8 +88,6 @@
             args.putAll(savedInstanceState);
         }
 
-        mOldWord = intent.getStringExtra(UserDictionaryAddWordContents.EXTRA_WORD);
-
         mContents = new UserDictionaryAddWordContents(getWindow().getDecorView(), args);
 
         if (mIsShowingMoreOptions) {
@@ -120,34 +116,7 @@
     }
 
     public void onClickConfirm(final View v) {
-        if (UserDictionaryAddWordContents.MODE_EDIT == mContents.mMode
-                && !TextUtils.isEmpty(mOldWord)) {
-            UserDictionarySettings.deleteWord(mOldWord, this.getContentResolver());
-        }
-        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?
-            finish();
-            return;
-        }
-        // Disallow duplicates.
-        // TODO: Redefine the logic when we support shortcuts.
-        UserDictionarySettings.deleteWord(newWord, this.getContentResolver());
-
-        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);
-        } 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(mContents.mLocale));
-            UserDictionary.Words.addWord(this, newWord.toString(),
-                    FREQUENCY_FOR_USER_DICTIONARY_ADDS, UserDictionary.Words.LOCALE_TYPE_CURRENT);
-            Locale.setDefault(prevLocale);
-        }
+        mContents.apply(this);
         finish();
     }
 
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java
index b67e834..5de6f03 100644
--- a/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java
+++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java
@@ -16,11 +16,17 @@
 
 package com.android.settings.inputmethod;
 
+import android.content.ContentResolver;
+import android.content.Context;
 import android.os.Bundle;
+import android.provider.UserDictionary;
+import android.text.TextUtils;
 import android.view.View;
 import android.widget.EditText;
 
 import com.android.settings.R;
+import com.android.settings.UserDictionarySettings;
+import com.android.settings.Utils;
 
 import java.util.Locale;
 
@@ -36,9 +42,12 @@
     public static final int MODE_EDIT = 0;
     public static final int MODE_INSERT = 1;
 
+    private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250;
+
     /* package */ final int mMode; // Either MODE_EDIT or MODE_INSERT
     /* package */ final EditText mEditText;
     /* package */ String mLocale;
+    /* package */ String mOldWord;
 
     /* package */ UserDictionaryAddWordContents(final View view, final Bundle args) {
         mEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text);
@@ -48,6 +57,7 @@
             mEditText.setSelection(word.length());
         }
         mMode = args.getInt(EXTRA_MODE); // default return value for #getInt() is 0 = MODE_EDIT
+        mOldWord = args.getString(EXTRA_WORD);
         updateLocale(args.getString(EXTRA_LOCALE));
     }
 
@@ -56,4 +66,25 @@
     /* package */ void updateLocale(final String locale) {
         mLocale = null == locale ? Locale.getDefault().toString() : locale;
     }
+
+    /* package */ void apply(final Context context) {
+        final ContentResolver resolver = context.getContentResolver();
+        if (UserDictionaryAddWordContents.MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) {
+            UserDictionarySettings.deleteWord(mOldWord, resolver);
+        }
+        final String newWord = mEditText.getText().toString();
+        if (TextUtils.isEmpty(newWord)) {
+            // If the word is somehow empty, don't insert it.
+            return;
+        }
+        // Disallow duplicates.
+        // TODO: Redefine the logic when we support shortcuts.
+        UserDictionarySettings.deleteWord(newWord, resolver);
+
+        // In this class we use the empty string to represent 'all locales' and mLocale cannot
+        // be null. However the addWord method takes null to mean 'all locales'.
+        UserDictionary.Words.addWord(context, newWord.toString(),
+                FREQUENCY_FOR_USER_DICTIONARY_ADDS, null /* shortcut */,
+                TextUtils.isEmpty(mLocale) ? null : Utils.createLocaleFromString(mLocale));
+    }
 }
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java
index d5f4e3f..f4a39a2 100644
--- a/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java
+++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java
@@ -68,6 +68,7 @@
     @Override
     public void onPause() {
         super.onPause();
+        mContents.apply(getActivity());
         // We are being hidden: commit changes to the user dictionary
     }
 }