New API in InputConnection to signal IME's text correction.

Scafolding so that the IME team can start working on this feature.

The animation part in the TextView is missing.

Change-Id: I8225538564370fba1500e3539742a8ab79bdd199
diff --git a/core/java/android/view/inputmethod/BaseInputConnection.java b/core/java/android/view/inputmethod/BaseInputConnection.java
index b66fb2c..e644045 100644
--- a/core/java/android/view/inputmethod/BaseInputConnection.java
+++ b/core/java/android/view/inputmethod/BaseInputConnection.java
@@ -164,13 +164,20 @@
     }
 
     /**
-     * Default implementation does nothing.
+     * Default implementation does nothing and returns false.
      */
     public boolean commitCompletion(CompletionInfo text) {
         return false;
     }
 
     /**
+     * Default implementation does nothing and returns false.
+     */
+    public boolean commitCorrection(CorrectionInfo correctionInfo) {
+        return false;
+    }
+
+    /**
      * Default implementation replaces any existing composing text with
      * the given text.  In addition, only if dummy mode, a key event is
      * sent for the new text and the current editable buffer cleared.
diff --git a/core/java/android/view/inputmethod/CompletionInfo.java b/core/java/android/view/inputmethod/CompletionInfo.java
index 3a8fe72..3591cb1 100644
--- a/core/java/android/view/inputmethod/CompletionInfo.java
+++ b/core/java/android/view/inputmethod/CompletionInfo.java
@@ -25,13 +25,11 @@
  * an input method.
  */
 public final class CompletionInfo implements Parcelable {
-    static final String TAG = "CompletionInfo";
-    
-    final long mId;
-    final int mPosition;
-    final CharSequence mText;
-    final CharSequence mLabel;
-    
+    private final long mId;
+    private final int mPosition;
+    private final CharSequence mText;
+    private final CharSequence mLabel;
+
     /**
      * Create a simple completion with just text, no label.
      */
@@ -52,7 +50,7 @@
         mLabel = label;
     }
 
-    CompletionInfo(Parcel source) {
+    private CompletionInfo(Parcel source) {
         mId = source.readLong();
         mPosition = source.readInt();
         mText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
@@ -100,7 +98,7 @@
 
     /**
      * Used to package this object into a {@link Parcel}.
-     * 
+     *
      * @param dest The {@link Parcel} to be written.
      * @param flags The flags used for parceling.
      */
diff --git a/core/java/android/view/inputmethod/CorrectionInfo.aidl b/core/java/android/view/inputmethod/CorrectionInfo.aidl
new file mode 100644
index 0000000..82865fd
--- /dev/null
+++ b/core/java/android/view/inputmethod/CorrectionInfo.aidl
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2010 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 android.view.inputmethod;
+
+parcelable CorrectionInfo;
diff --git a/core/java/android/view/inputmethod/CorrectionInfo.java b/core/java/android/view/inputmethod/CorrectionInfo.java
new file mode 100644
index 0000000..1b04e49
--- /dev/null
+++ b/core/java/android/view/inputmethod/CorrectionInfo.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2007-2010 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 android.view.inputmethod;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.text.TextUtils;
+
+/**
+ * Information about a single text correction that an editor has reported to
+ * an input method.
+ */
+public final class CorrectionInfo implements Parcelable {
+    private final int mOffset;
+    private final CharSequence mOldText;
+    private final CharSequence mNewText;
+
+    /**
+     * @param offset The offset in the edited text where the old and new text start.
+     * @param oldText The old text that has been replaced.
+     * @param newText The replacement text.
+     */
+    public CorrectionInfo(int offset, CharSequence oldText, CharSequence newText) {
+        mOffset = offset;
+        mOldText = oldText;
+        mNewText = newText;
+    }
+
+    private CorrectionInfo(Parcel source) {
+        mOffset = source.readInt();
+        mOldText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
+        mNewText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
+    }
+
+    /**
+     * Return the offset position of this correction in the text. Both the {@link #getOldText()} and
+     * {@link #getNewText()} start at this offset.
+     */
+    public int getOffset() {
+        return mOffset;
+    }
+
+    /**
+     * Return the text that has actually been typed by the user, and which has been corrected.
+     */
+    public CharSequence getOldText() {
+        return mOldText;
+    }
+
+    /**
+     * Return the new text that corrects what was typed by the user.
+     */
+    public CharSequence getNewText() {
+        return mNewText;
+    }
+
+    @Override
+    public String toString() {
+        return "CorrectionInfo{#" + mOffset + " \"" + mOldText + "\" -> \"" + mNewText + "\"}";
+    }
+
+    /**
+     * Used to package this object into a {@link Parcel}.
+     *
+     * @param dest The {@link Parcel} to be written.
+     * @param flags The flags used for parceling.
+     */
+    public void writeToParcel(Parcel dest, int flags) {
+        dest.writeInt(mOffset);
+        TextUtils.writeToParcel(mOldText, dest, flags);
+        TextUtils.writeToParcel(mNewText, dest, flags);
+    }
+
+    /**
+     * Used to make this class parcelable.
+     */
+    public static final Parcelable.Creator<CorrectionInfo> CREATOR
+            = new Parcelable.Creator<CorrectionInfo>() {
+        public CorrectionInfo createFromParcel(Parcel source) {
+            return new CorrectionInfo(source);
+        }
+
+        public CorrectionInfo[] newArray(int size) {
+            return new CorrectionInfo[size];
+        }
+    };
+
+    public int describeContents() {
+        return 0;
+    }
+}
diff --git a/core/java/android/view/inputmethod/InputConnection.java b/core/java/android/view/inputmethod/InputConnection.java
index 3b8a364..ea9e402 100644
--- a/core/java/android/view/inputmethod/InputConnection.java
+++ b/core/java/android/view/inputmethod/InputConnection.java
@@ -232,6 +232,16 @@
     public boolean commitCompletion(CompletionInfo text);
 
     /**
+     * Commit a correction automatically performed on the raw user's input. A typical example would
+     * be to correct typos using a dictionary.
+     *
+     * @param correctionInfo Detailed information about the correction.
+     *
+     * @return True on success, false if the input connection is no longer valid.
+     */
+    public boolean commitCorrection(CorrectionInfo correctionInfo);
+
+    /**
      * Set the selection of the text editor.  To set the cursor position,
      * start and end should have the same value.
      * @return Returns true on success, false if the input connection is no longer
diff --git a/core/java/android/view/inputmethod/InputConnectionWrapper.java b/core/java/android/view/inputmethod/InputConnectionWrapper.java
index b73f9bb..4d9d51e 100644
--- a/core/java/android/view/inputmethod/InputConnectionWrapper.java
+++ b/core/java/android/view/inputmethod/InputConnectionWrapper.java
@@ -87,6 +87,10 @@
         return mTarget.commitCompletion(text);
     }
 
+    public boolean commitCorrection(CorrectionInfo correctionInfo) {
+        return mTarget.commitCorrection(correctionInfo);
+    }
+
     public boolean setSelection(int start, int end) {
         return mTarget.setSelection(start, end);
     }
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 3cf945d..e4d26f0 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -114,6 +114,7 @@
 import android.view.animation.AnimationUtils;
 import android.view.inputmethod.BaseInputConnection;
 import android.view.inputmethod.CompletionInfo;
+import android.view.inputmethod.CorrectionInfo;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.ExtractedText;
 import android.view.inputmethod.ExtractedTextRequest;
@@ -5057,6 +5058,18 @@
     public void onCommitCompletion(CompletionInfo text) {
     }
 
+    /**
+     * Called by the framework in response to a text auto-correction (such as fixing a typo using a
+     * a dictionnary) from the current input method, provided by it calling
+     * {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default
+     * implementation flashes the background of the corrected word to provide feedback to the user.
+     *
+     * @param info The auto correct info about the text that was corrected.
+     */
+    public void onCommitCorrection(CorrectionInfo info) {
+        // TODO
+    }
+
     public void beginBatchEdit() {
         mInBatchEditControllers = true;
         final InputMethodState ims = mInputMethodState;
@@ -6572,8 +6585,7 @@
      * Not private so it can be called from an inner class without going
      * through a thunk.
      */
-    void handleTextChanged(CharSequence buffer, int start,
-            int before, int after) {
+    void handleTextChanged(CharSequence buffer, int start, int before, int after) {
         final InputMethodState ims = mInputMethodState;
         if (ims == null || ims.mBatchEditNesting == 0) {
             updateAfterEdit();
@@ -6603,8 +6615,7 @@
      * Not private so it can be called from an inner class without going
      * through a thunk.
      */
-    void spanChange(Spanned buf, Object what, int oldStart, int newStart,
-            int oldEnd, int newEnd) {
+    void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd, int newEnd) {
         // XXX Make the start and end move together if this ends up
         // spending too much time invalidating.
 
diff --git a/core/java/com/android/internal/view/IInputConnectionWrapper.java b/core/java/com/android/internal/view/IInputConnectionWrapper.java
index 986ba38..b5df812 100644
--- a/core/java/com/android/internal/view/IInputConnectionWrapper.java
+++ b/core/java/com/android/internal/view/IInputConnectionWrapper.java
@@ -24,6 +24,7 @@
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.inputmethod.CompletionInfo;
+import android.view.inputmethod.CorrectionInfo;
 import android.view.inputmethod.ExtractedTextRequest;
 import android.view.inputmethod.InputConnection;
 
@@ -39,6 +40,7 @@
     private static final int DO_GET_EXTRACTED_TEXT = 40;
     private static final int DO_COMMIT_TEXT = 50;
     private static final int DO_COMMIT_COMPLETION = 55;
+    private static final int DO_COMMIT_CORRECTION = 56;
     private static final int DO_SET_SELECTION = 57;
     private static final int DO_PERFORM_EDITOR_ACTION = 58;
     private static final int DO_PERFORM_CONTEXT_MENU_ACTION = 59;
@@ -116,6 +118,10 @@
         dispatchMessage(obtainMessageO(DO_COMMIT_COMPLETION, text));
     }
 
+    public void commitCorrection(CorrectionInfo info) {
+        dispatchMessage(obtainMessageO(DO_COMMIT_CORRECTION, info));
+    }
+
     public void setSelection(int start, int end) {
         dispatchMessage(obtainMessageII(DO_SET_SELECTION, start, end));
     }
@@ -309,6 +315,15 @@
                 ic.commitCompletion((CompletionInfo)msg.obj);
                 return;
             }
+            case DO_COMMIT_CORRECTION: {
+                InputConnection ic = mInputConnection.get();
+                if (ic == null || !isActive()) {
+                    Log.w(TAG, "commitCorrection on inactive InputConnection");
+                    return;
+                }
+                ic.commitCorrection((CorrectionInfo)msg.obj);
+                return;
+            }
             case DO_SET_COMPOSING_TEXT: {
                 InputConnection ic = mInputConnection.get();
                 if (ic == null || !isActive()) {
diff --git a/core/java/com/android/internal/view/IInputContext.aidl b/core/java/com/android/internal/view/IInputContext.aidl
index 333fc82..e00dd4e 100644
--- a/core/java/com/android/internal/view/IInputContext.aidl
+++ b/core/java/com/android/internal/view/IInputContext.aidl
@@ -19,6 +19,7 @@
 import android.os.Bundle;
 import android.view.KeyEvent;
 import android.view.inputmethod.CompletionInfo;
+import android.view.inputmethod.CorrectionInfo;
 import android.view.inputmethod.ExtractedTextRequest;
 
 import com.android.internal.view.IInputContextCallback;
@@ -48,6 +49,8 @@
 
     void commitCompletion(in CompletionInfo completion);
 
+    void commitCorrection(in CorrectionInfo correction);
+
     void setSelection(int start, int end);
     
     void performEditorAction(int actionCode);
diff --git a/core/java/com/android/internal/view/InputConnectionWrapper.java b/core/java/com/android/internal/view/InputConnectionWrapper.java
index 08c3026..b13118a 100644
--- a/core/java/com/android/internal/view/InputConnectionWrapper.java
+++ b/core/java/com/android/internal/view/InputConnectionWrapper.java
@@ -22,6 +22,7 @@
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.inputmethod.CompletionInfo;
+import android.view.inputmethod.CorrectionInfo;
 import android.view.inputmethod.ExtractedText;
 import android.view.inputmethod.ExtractedTextRequest;
 import android.view.inputmethod.InputConnection;
@@ -287,6 +288,15 @@
         }
     }
 
+    public boolean commitCorrection(CorrectionInfo correctionInfo) {
+        try {
+            mIInputContext.commitCorrection(correctionInfo);
+            return true;
+        } catch (RemoteException e) {
+            return false;
+        }
+    }
+
     public boolean setSelection(int start, int end) {
         try {
             mIInputContext.setSelection(start, end);
diff --git a/core/java/com/android/internal/widget/EditableInputConnection.java b/core/java/com/android/internal/widget/EditableInputConnection.java
index ad329d1..6a1beb4 100644
--- a/core/java/com/android/internal/widget/EditableInputConnection.java
+++ b/core/java/com/android/internal/widget/EditableInputConnection.java
@@ -22,6 +22,7 @@
 import android.util.Log;
 import android.view.inputmethod.BaseInputConnection;
 import android.view.inputmethod.CompletionInfo;
+import android.view.inputmethod.CorrectionInfo;
 import android.view.inputmethod.ExtractedText;
 import android.view.inputmethod.ExtractedTextRequest;
 import android.widget.TextView;
@@ -37,6 +38,7 @@
         mTextView = textview;
     }
 
+    @Override
     public Editable getEditable() {
         TextView tv = mTextView;
         if (tv != null) {
@@ -45,16 +47,19 @@
         return null;
     }
     
+    @Override
     public boolean beginBatchEdit() {
         mTextView.beginBatchEdit();
         return true;
     }
     
+    @Override
     public boolean endBatchEdit() {
         mTextView.endBatchEdit();
         return true;
     }
     
+    @Override
     public boolean clearMetaKeyStates(int states) {
         final Editable content = getEditable();
         if (content == null) return false;
@@ -70,6 +75,7 @@
         return true;
     }
     
+    @Override
     public boolean commitCompletion(CompletionInfo text) {
         if (DEBUG) Log.v(TAG, "commitCompletion " + text);
         mTextView.beginBatchEdit();
@@ -78,12 +84,26 @@
         return true;
     }
 
+    /**
+     * Calls the {@link TextView#onCommitCorrection} method of the associated TextView.
+     */
+    @Override
+    public boolean commitCorrection(CorrectionInfo correctionInfo) {
+        if (DEBUG) Log.v(TAG, "commitCorrection" + correctionInfo);
+        mTextView.beginBatchEdit();
+        mTextView.onCommitCorrection(correctionInfo);
+        mTextView.endBatchEdit();
+        return true;
+    }
+
+    @Override
     public boolean performEditorAction(int actionCode) {
         if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
         mTextView.onEditorAction(actionCode);
         return true;
     }
     
+    @Override
     public boolean performContextMenuAction(int id) {
         if (DEBUG) Log.v(TAG, "performContextMenuAction " + id);
         mTextView.beginBatchEdit();
@@ -92,6 +112,7 @@
         return true;
     }
     
+    @Override
     public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
         if (mTextView != null) {
             ExtractedText et = new ExtractedText();
@@ -104,7 +125,8 @@
         }
         return null;
     }
-    
+
+    @Override
     public boolean performPrivateCommand(String action, Bundle data) {
         mTextView.onPrivateIMECommand(action, data);
         return true;