Distinguish adding an already present word / cancelling

Bug: 7725834
Change-Id: Iab3d1818f008a553868fb30e8460ea3f77c2de50
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java
index 627e7c6..1bf68ce 100644
--- a/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java
+++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordActivity.java
@@ -33,8 +33,9 @@
     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 CODE_WORD_ADDED = 0;
-    private static final int CODE_CANCEL = 1;
+    /* package */ static final int CODE_WORD_ADDED = 0;
+    /* package */ static final int CODE_CANCEL = 1;
+    /* package */ static final int CODE_ALREADY_PRESENT = 2;
 
     private UserDictionaryAddWordContents mContents;
 
@@ -73,7 +74,7 @@
         mContents.saveStateIntoBundle(outState);
     }
 
-    private void reportBackToCaller(final Bundle result) {
+    private void reportBackToCaller(final int resultCode, final Bundle result) {
         final Intent senderIntent = getIntent();
         final Object listener = senderIntent.getExtras().get("listener");
         if (!(listener instanceof Messenger)) return; // This will work if listener is null too.
@@ -81,7 +82,7 @@
 
         final Message m = Message.obtain();
         m.obj = result;
-        m.what = (null != result) ? CODE_WORD_ADDED : CODE_CANCEL;
+        m.what = resultCode;
         try {
             messenger.send(m);
         } catch (RemoteException e) {
@@ -90,12 +91,14 @@
     }
 
     public void onClickCancel(final View v) {
-        reportBackToCaller(null);
+        reportBackToCaller(CODE_CANCEL, null);
         finish();
     }
 
     public void onClickConfirm(final View v) {
-        reportBackToCaller(mContents.apply(this));
+        final Bundle parameters = new Bundle();
+        final int resultCode = mContents.apply(this, parameters);
+        reportBackToCaller(resultCode, parameters);
         finish();
     }
 }
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java
index f28b4e5..3251216 100644
--- a/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java
+++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java
@@ -103,7 +103,8 @@
         // If we are in add mode, nothing was added, so we don't need to do anything.
     }
 
-    /* package */ Bundle apply(final Context context) {
+    /* package */ int apply(final Context context, final Bundle outParameters) {
+        if (null != outParameters) saveStateIntoBundle(outParameters);
         final ContentResolver resolver = context.getContentResolver();
         if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) {
             // Mode edit: remove the old entry.
@@ -123,13 +124,13 @@
         }
         if (TextUtils.isEmpty(newWord)) {
             // If the word is somehow empty, don't insert it.
-            return null;
+            return UserDictionaryAddWordActivity.CODE_CANCEL;
         }
         // If there is no shortcut, and the word already exists in the database, then we
         // should not insert, because either A. the word exists with no shortcut, in which
         // case the exact same thing we want to insert is already there, or B. the word
         // exists with at least one shortcut, in which case it has priority on our word.
-        if (hasWord(newWord, context)) return null;
+        if (hasWord(newWord, context)) return UserDictionaryAddWordActivity.CODE_ALREADY_PRESENT;
 
         // Disallow duplicates. If the same word with no shortcut is defined, remove it; if
         // the same word with the same shortcut is defined, remove it; but we don't mind if
@@ -146,9 +147,7 @@
                 FREQUENCY_FOR_USER_DICTIONARY_ADDS, newShortcut,
                 TextUtils.isEmpty(mLocale) ? null : Utils.createLocaleFromString(mLocale));
 
-        final Bundle returnValues = new Bundle();
-        saveStateIntoBundle(returnValues);
-        return returnValues;
+        return UserDictionaryAddWordActivity.CODE_WORD_ADDED;
     }
 
     private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD };
diff --git a/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java b/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java
index 97ffa19..e33333b 100644
--- a/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java
+++ b/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java
@@ -116,7 +116,7 @@
         super.onPause();
         // We are being hidden: commit changes to the user dictionary, unless we were deleting it
         if (!mIsDeleting) {
-            mContents.apply(getActivity());
+            mContents.apply(getActivity(), null);
         }
     }