Use the amended user dictionary word for insertion

When the user edits a word before adding it to the user
dictionary, the keyboard should replace whatever was
committed before with the amended version.

Bug: 7725834
Change-Id: I1a417be6c5a86d6a96bc2c76aca314ad8f1202a9
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index f416396..e2a76a4 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -149,6 +149,8 @@
     private boolean mIsUserDictionaryAvailable;
 
     private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
+    private PositionalInfoForUserDictPendingAddition
+            mPositionalInfoForUserDictPendingAddition = null;
     private final WordComposer mWordComposer = new WordComposer();
     private RichInputConnection mConnection = new RichInputConnection(this);
 
@@ -780,6 +782,19 @@
         mainKeyboardView.setGesturePreviewMode(mCurrentSettings.mGesturePreviewTrailEnabled,
                 mCurrentSettings.mGestureFloatingPreviewTextEnabled);
 
+        // If we have a user dictionary addition in progress, we should check now if we should
+        // replace the previously committed string with the word that has actually been added
+        // to the user dictionary.
+        if (null != mPositionalInfoForUserDictPendingAddition
+                && mPositionalInfoForUserDictPendingAddition.tryReplaceWithActualWord(
+                        mConnection, editorInfo, mLastSelectionEnd)) {
+            mPositionalInfoForUserDictPendingAddition = null;
+        }
+        // If tryReplaceWithActualWord returns false, we don't know what word was
+        // added to the user dictionary yet, so we keep the data and defer processing. The word will
+        // be replaced when the user dictionary reports back with the actual word, which ends
+        // up calling #onWordAddedToUserDictionary() in this class.
+
         if (TRACE) Debug.startMethodTracing("/data/trace/latinime");
     }
 
@@ -1208,9 +1223,31 @@
     // Callback for the {@link SuggestionStripView}, to call when the "add to dictionary" hint is
     // pressed.
     @Override
-    public boolean addWordToUserDictionary(final String word) {
+    public void addWordToUserDictionary(final String word) {
+        if (TextUtils.isEmpty(word)) {
+            // Probably never supposed to happen, but just in case.
+            mPositionalInfoForUserDictPendingAddition = null;
+            return;
+        }
+        mPositionalInfoForUserDictPendingAddition =
+                new PositionalInfoForUserDictPendingAddition(
+                        word, mLastSelectionEnd, getCurrentInputEditorInfo());
         mUserDictionary.addWordToUserDictionary(word, 128);
-        return true;
+    }
+
+    public void onWordAddedToUserDictionary(final String newSpelling) {
+        // If word was added but not by us, bail out
+        if (null == mPositionalInfoForUserDictPendingAddition) return;
+        if (mWordComposer.isComposingWord()) {
+            // We are late... give up and return
+            mPositionalInfoForUserDictPendingAddition = null;
+            return;
+        }
+        mPositionalInfoForUserDictPendingAddition.setActualWordBeingAdded(newSpelling);
+        if (mPositionalInfoForUserDictPendingAddition.tryReplaceWithActualWord(
+                mConnection, getCurrentInputEditorInfo(), mLastSelectionEnd)) {
+            mPositionalInfoForUserDictPendingAddition = null;
+        }
     }
 
     private static boolean isAlphabet(final int code) {
diff --git a/java/src/com/android/inputmethod/latin/PositionalInfoForUserDictPendingAddition.java b/java/src/com/android/inputmethod/latin/PositionalInfoForUserDictPendingAddition.java
new file mode 100644
index 0000000..8a2d222
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/PositionalInfoForUserDictPendingAddition.java
@@ -0,0 +1,98 @@
+/*
+ * 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.inputmethod.latin;
+
+import android.view.inputmethod.EditorInfo;
+
+/**
+ * Holder class for data about a word already committed but that may still be edited.
+ *
+ * When the user chooses to add a word to the user dictionary by pressing the appropriate
+ * suggestion, a dialog is presented to give a chance to edit the word before it is actually
+ * registered as a user dictionary word. If the word is actually modified, the IME needs to
+ * go back and replace the word that was committed with the amended version.
+ * The word we need to replace with will only be known after it's actually committed, so
+ * the IME needs to take a note of what it has to replace and where it is.
+ * This class encapsulates this data.
+ */
+public class PositionalInfoForUserDictPendingAddition {
+    final private String mOriginalWord;
+    final private int mCursorPos; // Position of the cursor after the word
+    final private EditorInfo mEditorInfo; // On what binding this has been added
+    private String mActualWordBeingAdded;
+
+    public PositionalInfoForUserDictPendingAddition(final String word, final int cursorPos,
+            final EditorInfo editorInfo) {
+        mOriginalWord = word;
+        mCursorPos = cursorPos;
+        mEditorInfo = editorInfo;
+    }
+
+    public void setActualWordBeingAdded(final String actualWordBeingAdded) {
+        mActualWordBeingAdded = actualWordBeingAdded;
+    }
+
+    /**
+     * Try to replace the string at the remembered position with the actual word being added.
+     *
+     * After the user validated the word being added, the IME has to replace the old version
+     * (which has been committed in the text view) with the amended version if it's different.
+     * This method tries to do that, but may fail because the IME is not yet ready to do so -
+     * for example, it is still waiting for the new string, or it is waiting to return to the text
+     * view in which the amendment should be made. In these cases, we should keep the data
+     * and wait until all conditions are met.
+     * This method returns true if the replacement has been successfully made and this data
+     * can be forgotten; it returns false if the replacement can't be made yet and we need to
+     * keep this until a later time.
+     * The IME knows about the actual word being added through a callback called by the
+     * user dictionary facility of the device. When this callback comes, the keyboard may still
+     * be connected to the edition dialog, or it may have already returned to the original text
+     * field. Replacement has to work in both cases.
+     * Accordingly, this method is called at two different points in time : upon getting the
+     * event that a new word was added to the user dictionary, and upon starting up in a
+     * new text field.
+     * @param connection The RichInputConnection through which to contact the editor.
+     * @param editorInfo Information pertaining to the editor we are currently in.
+     * @param currentCursorPosition The current cursor position, for checking purposes.
+     * @return true if the edit has been successfully made, false if we need to try again later
+     */
+    public boolean tryReplaceWithActualWord(final RichInputConnection connection,
+            final EditorInfo editorInfo, final int currentCursorPosition) {
+        // If we still don't know the actual word being added, we need to try again later.
+        if (null == mActualWordBeingAdded) return false;
+        // The entered text and the registered text were the same anyway : we can
+        // return success right away even if focus has not returned yet to the text field we
+        // want to amend.
+        if (mActualWordBeingAdded.equals(mOriginalWord)) return true;
+        // Not the same text field : we need to try again later. This happens when the addition
+        // is reported by the user dictionary provider before the focus has moved back to the
+        // original text view, so the IME is still in the text view of the dialog and has no way to
+        // edit the original text view at this time.
+        if (!mEditorInfo.packageName.equals(editorInfo.packageName)
+                || mEditorInfo.fieldId != editorInfo.fieldId) {
+            return false;
+        }
+        // Same text field, but not the same cursor position : we give up, so we return success
+        // so that it won't be tried again
+        if (currentCursorPosition != mCursorPos) return true;
+        // We have made all the checks : do the replacement and report success
+        connection.setComposingRegion(currentCursorPosition - mOriginalWord.length(),
+                currentCursorPosition);
+        connection.commitText(mActualWordBeingAdded, mActualWordBeingAdded.length());
+        return true;
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java
index 75b67bf..3b73227 100644
--- a/java/src/com/android/inputmethod/latin/RichInputConnection.java
+++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java
@@ -339,6 +339,24 @@
         }
     }
 
+    public void setComposingRegion(final int start, final int end) {
+        if (DEBUG_BATCH_NESTING) checkBatchEdit();
+        if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
+        mCurrentCursorPosition = end;
+        final CharSequence textBeforeCursor =
+                getTextBeforeCursor(DEFAULT_TEXT_CACHE_SIZE + (end - start), 0);
+        final int indexOfStartOfComposingText =
+                Math.max(textBeforeCursor.length() - (end - start), 0);
+        mComposingText.append(textBeforeCursor.subSequence(indexOfStartOfComposingText,
+                textBeforeCursor.length()));
+        mCommittedTextBeforeComposingText.setLength(0);
+        mCommittedTextBeforeComposingText.append(
+                textBeforeCursor.subSequence(0, indexOfStartOfComposingText));
+        if (null != mIC) {
+            mIC.setComposingRegion(start, end);
+        }
+    }
+
     public void setComposingText(final CharSequence text, final int i) {
         if (DEBUG_BATCH_NESTING) checkBatchEdit();
         if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
diff --git a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java
index 60e6fa1..8570746 100644
--- a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java
@@ -18,10 +18,12 @@
 
 import android.content.ContentProviderClient;
 import android.content.ContentResolver;
+import android.content.ContentUris;
 import android.content.Context;
 import android.content.Intent;
 import android.database.ContentObserver;
 import android.database.Cursor;
+import android.net.Uri;
 import android.provider.UserDictionary.Words;
 import android.text.TextUtils;
 
@@ -87,8 +89,25 @@
 
         mObserver = new ContentObserver(null) {
             @Override
-            public void onChange(boolean self) {
+            public void onChange(final boolean self) {
+                // This hook is deprecated as of API level 16, but should still be supported for
+                // cases where the IME is running on an older version of the platform.
+                onChange(self, null);
+            }
+            // The following hook is only available as of API level 16, and as such it will only
+            // work on JellyBean+ devices. On older versions of the platform, the hook
+            // above will be called instead.
+            @Override
+            public void onChange(final boolean self, final Uri uri) {
                 setRequiresReload(true);
+                // We want to report back to Latin IME in case the user just entered the word.
+                // If the user changed the word in the dialog box, then we want to replace
+                // what was entered in the text field.
+                if (null == uri || !(context instanceof LatinIME)) return;
+                final long changedRowId = ContentUris.parseId(uri);
+                if (-1 == changedRowId) return; // Unknown content... Not sure why we're here
+                final String changedWord = getChangedWordForUri(uri);
+                ((LatinIME)context).onWordAddedToUserDictionary(changedWord);
             }
         };
         cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
@@ -96,6 +115,19 @@
         loadDictionary();
     }
 
+    private String getChangedWordForUri(final Uri uri) {
+        final Cursor cursor = mContext.getContentResolver().query(uri,
+                PROJECTION_QUERY, null, null, null);
+        if (cursor == null) return null;
+        try {
+            if (!cursor.moveToFirst()) return null;
+            final int indexWord = cursor.getColumnIndex(Words.WORD);
+            return cursor.getString(indexWord);
+        } finally {
+            cursor.close();
+        }
+    }
+
     @Override
     public synchronized void close() {
         if (mObserver != null) {
diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
index e926fa2..a1c95ad 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
@@ -73,7 +73,7 @@
 public final class SuggestionStripView extends RelativeLayout implements OnClickListener,
         OnLongClickListener {
     public interface Listener {
-        public boolean addWordToUserDictionary(String word);
+        public void addWordToUserDictionary(String word);
         public void pickSuggestionManually(int index, CharSequence word);
     }