Merge "Remove the input connection sidework from handleCharacter"
diff --git a/Android.mk b/Android.mk
index 8f1acc5..91b2fbb 100644
--- a/Android.mk
+++ b/Android.mk
@@ -12,6 +12,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-LOCAL_PATH := $(call my-dir)
-
-include $(call all-makefiles-under,$(LOCAL_PATH))
+include $(call all-subdir-makefiles)
diff --git a/java/Android.mk b/java/Android.mk
index 43168e5..36ff506 100644
--- a/java/Android.mk
+++ b/java/Android.mk
@@ -1,3 +1,17 @@
+# Copyright (C) 2011 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.
+
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index aa6ef06..2d7eed7 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -203,7 +203,6 @@
     private boolean mApplicationSpecifiedCompletionOn;
 
     private WordComposer mWordComposer = new WordComposer();
-    private CharSequence mBestWord;
     private boolean mHasUncommittedTypedChars;
 
     private int mCorrectionMode;
@@ -1019,7 +1018,7 @@
                     .setHasMinimalSuggestion(false);
             // When in fullscreen mode, show completions generated by the application
             setSuggestions(builder.build());
-            mBestWord = null;
+            mWordComposer.deleteAutoCorrection();
             setSuggestionStripShown(true);
         }
     }
@@ -1653,10 +1652,18 @@
         Utils.Stats.onSeparator((char)primaryCode, x, y);
 
         if (pickedDefault) {
-            CharSequence typedWord = mWordComposer.getTypedWord();
-            if (!TextUtils.isEmpty(typedWord) && !typedWord.equals(mBestWord)) {
+            final CharSequence autoCorrection = mWordComposer.getAutoCorrectionOrNull();
+            final String typedWord = mWordComposer.getTypedWord();
+            if (TextUtils.isEmpty(typedWord)) {
+                throw new RuntimeException("We have non-committed chars but the typed word "
+                        + "is empty? Impossible! I must commit suicide.");
+            }
+            if (!typedWord.equals(autoCorrection)) {
+                // TODO: if the commitCorrection method is not supported by the platform
+                // this will do nothing and the correction will not be committed at all. What
+                // happens on Froyo/Gingerbread, where this API is not present?
                 InputConnectionCompatUtils.commitCorrection(
-                        ic, mLastSelectionEnd - typedWord.length(), typedWord, mBestWord);
+                        ic, mLastSelectionEnd - typedWord.length(), typedWord, autoCorrection);
             }
         }
         mKeyboardSwitcher.updateShiftState();
@@ -1851,14 +1858,15 @@
         setSuggestions(suggestedWords);
         if (suggestedWords.size() > 0) {
             if (shouldBlockAutoCorrectionBySafetyNet) {
-                mBestWord = typedWord;
+                mWordComposer.setAutoCorrection(typedWord);
             } else if (suggestedWords.hasAutoCorrectionWord()) {
-                mBestWord = suggestedWords.getWord(1);
+                mWordComposer.setAutoCorrection(suggestedWords.getWord(1));
             } else {
-                mBestWord = typedWord;
+                mWordComposer.setAutoCorrection(typedWord);
             }
         } else {
-            mBestWord = null;
+            // TODO: replace with mWordComposer.deleteAutoCorrection()?
+            mWordComposer.setAutoCorrection(null);
         }
         setSuggestionStripShown(isSuggestionsStripVisible());
     }
@@ -1869,16 +1877,17 @@
             mHandler.cancelUpdateSuggestions();
             updateSuggestions();
         }
-        if (mBestWord != null && mBestWord.length() > 0) {
-            Utils.Stats.onAutoCorrection(mWordComposer.getTypedWord(), mBestWord.toString(),
-                    separatorCode);
+        final CharSequence autoCorrection = mWordComposer.getAutoCorrectionOrNull();
+        if (autoCorrection != null) {
+            final String typedWord = mWordComposer.getTypedWord();
+            Utils.Stats.onAutoCorrection(typedWord, autoCorrection.toString(), separatorCode);
             mExpectingUpdateSelection = true;
-            commitBestWord(mBestWord);
-            if (!mBestWord.equals(mWordComposer.getTypedWord())) {
-                mWordSavedForAutoCorrectCancellation = mBestWord.toString();
+            commitBestWord(autoCorrection);
+            if (!autoCorrection.equals(typedWord)) {
+                mWordSavedForAutoCorrectCancellation = autoCorrection.toString();
             }
             // Add the word to the user unigram dictionary if it's not a known word
-            addToUserUnigramAndBigramDictionaries(mBestWord,
+            addToUserUnigramAndBigramDictionaries(autoCorrection,
                     UserUnigramDictionary.FREQUENCY_FOR_TYPED);
             return true;
         }
@@ -2157,8 +2166,6 @@
     private void restartSuggestionsOnWordBeforeCursor(final InputConnection ic,
             final CharSequence word) {
         mWordComposer.setComposingWord(word, mKeyboardSwitcher.getLatinKeyboard());
-        // mBestWord will be set appropriately by updateSuggestions() called by the handler
-        mBestWord = null;
         mHasUncommittedTypedChars = true;
         mComposingStateManager.onStartComposingText();
         ic.deleteSurroundingText(word.length(), 0);
diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java
index dfb00c8..fcaf81c 100644
--- a/java/src/com/android/inputmethod/latin/WordComposer.java
+++ b/java/src/com/android/inputmethod/latin/WordComposer.java
@@ -41,6 +41,8 @@
     private int[] mYCoordinates;
 
     private StringBuilder mTypedWord;
+    // An auto-correction for this word out of the dictionary.
+    private CharSequence mAutoCorrection;
 
     private int mCapsCount;
 
@@ -60,6 +62,7 @@
         mXCoordinates = new int[N];
         mYCoordinates = new int[N];
         mTrailingSingleQuotesCount = 0;
+        mAutoCorrection = null;
     }
 
     public WordComposer(WordComposer source) {
@@ -75,6 +78,7 @@
         mIsFirstCharCapitalized = source.mIsFirstCharCapitalized;
         mAutoCapitalized = source.mAutoCapitalized;
         mTrailingSingleQuotesCount = source.mTrailingSingleQuotesCount;
+        mAutoCorrection = null;
     }
 
     /**
@@ -86,6 +90,7 @@
         mCapsCount = 0;
         mIsFirstCharCapitalized = false;
         mTrailingSingleQuotesCount = 0;
+        mAutoCorrection = null;
     }
 
     /**
@@ -140,6 +145,7 @@
         } else {
             mTrailingSingleQuotesCount = 0;
         }
+        mAutoCorrection = null;
     }
 
     /**
@@ -173,6 +179,7 @@
             int codePoint = word.charAt(i);
             addKeyInfo(codePoint, keyboard, keyDetector);
         }
+        mAutoCorrection = null;
     }
 
     /**
@@ -224,11 +231,12 @@
                 ++mTrailingSingleQuotesCount;
             }
         }
+        mAutoCorrection = null;
     }
 
     /**
      * Returns the word as it was typed, without any correction applied.
-     * @return the word that was typed so far
+     * @return the word that was typed so far. Never returns null.
      */
     public String getTypedWord() {
         return mTypedWord.toString();
@@ -277,4 +285,25 @@
     public boolean isAutoCapitalized() {
         return mAutoCapitalized;
     }
+
+    /**
+     * Sets the auto-correction for this word.
+     */
+    public void setAutoCorrection(final CharSequence correction) {
+        mAutoCorrection = correction;
+    }
+
+    /**
+     * Remove any auto-correction that may have been set.
+     */
+    public void deleteAutoCorrection() {
+        mAutoCorrection = null;
+    }
+
+    /**
+     * @return the auto-correction for this world, or null if none.
+     */
+    public CharSequence getAutoCorrectionOrNull() {
+        return mAutoCorrection;
+    }
 }
diff --git a/native/Android.mk b/native/Android.mk
index 5dcc1e5..5053e7d 100644
--- a/native/Android.mk
+++ b/native/Android.mk
@@ -1,65 +1 @@
-LOCAL_PATH := $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_C_INCLUDES += $(LOCAL_PATH)/src
-
-LOCAL_CFLAGS += -Werror -Wall
-
-# To suppress compiler warnings for unused variables/functions used for debug features etc.
-LOCAL_CFLAGS += -Wno-unused-parameter -Wno-unused-function
-
-LOCAL_SRC_FILES := \
-    jni/com_android_inputmethod_keyboard_ProximityInfo.cpp \
-    jni/com_android_inputmethod_latin_BinaryDictionary.cpp \
-    jni/jni_common.cpp \
-    src/basechars.cpp \
-    src/bigram_dictionary.cpp \
-    src/char_utils.cpp \
-    src/correction.cpp \
-    src/dictionary.cpp \
-    src/proximity_info.cpp \
-    src/unigram_dictionary.cpp
-
-#FLAG_DBG := true
-#FLAG_DO_PROFILE := true
-
-TARGETING_UNBUNDLED_FROYO := true
-
-ifeq ($(TARGET_ARCH), x86)
-    TARGETING_UNBUNDLED_FROYO := false
-endif
-
-ifeq ($(FLAG_DBG), true)
-    TARGETING_UNBUNDLED_FROYO := false
-endif
-
-ifeq ($(FLAG_DO_PROFILE), true)
-    TARGETING_UNBUNDLED_FROYO := false
-endif
-
-ifeq ($(TARGETING_UNBUNDLED_FROYO), true)
-    LOCAL_NDK_VERSION := 4
-    LOCAL_SDK_VERSION := 8
-endif
-
-LOCAL_MODULE := libjni_latinime
-
-LOCAL_MODULE_TAGS := user
-
-# For STL
-LOCAL_C_INCLUDES += external/stlport/stlport bionic
-LOCAL_SHARED_LIBRARIES += libstlport
-
-ifeq ($(FLAG_DO_PROFILE), true)
-    $(warning Making profiling version of native library)
-    LOCAL_CFLAGS += -DFLAG_DO_PROFILE
-    LOCAL_SHARED_LIBRARIES += libcutils libutils
-else # FLAG_DO_PROFILE
-ifeq ($(FLAG_DBG), true)
-    $(warning Making debug version of native library)
-    LOCAL_CFLAGS += -DFLAG_DBG
-    LOCAL_SHARED_LIBRARIES += libcutils libutils
-endif # FLAG_DBG
-endif # FLAG_DO_PROFILE
-
-include $(BUILD_SHARED_LIBRARY)
+include $(call all-subdir-makefiles)
diff --git a/native/jni/Android.mk b/native/jni/Android.mk
new file mode 100644
index 0000000..c4adbfa
--- /dev/null
+++ b/native/jni/Android.mk
@@ -0,0 +1,87 @@
+# Copyright (C) 2011 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.
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LATIN_IME_SRC_DIR := ../src
+
+LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(LATIN_IME_SRC_DIR)
+
+LOCAL_CFLAGS += -Werror -Wall
+
+# To suppress compiler warnings for unused variables/functions used for debug features etc.
+LOCAL_CFLAGS += -Wno-unused-parameter -Wno-unused-function
+
+LATIN_IME_JNI_SRC_FILES := \
+    com_android_inputmethod_keyboard_ProximityInfo.cpp \
+    com_android_inputmethod_latin_BinaryDictionary.cpp \
+    jni_common.cpp
+
+LATIN_IME_CORE_SRC_FILES := \
+    basechars.cpp \
+    bigram_dictionary.cpp \
+    char_utils.cpp \
+    correction.cpp \
+    dictionary.cpp \
+    proximity_info.cpp \
+    unigram_dictionary.cpp
+
+LOCAL_SRC_FILES := \
+    $(LATIN_IME_JNI_SRC_FILES) \
+    $(addprefix $(LATIN_IME_SRC_DIR)/,$(LATIN_IME_CORE_SRC_FILES))
+
+#FLAG_DBG := true
+#FLAG_DO_PROFILE := true
+
+TARGETING_UNBUNDLED_FROYO := true
+
+ifeq ($(TARGET_ARCH), x86)
+    TARGETING_UNBUNDLED_FROYO := false
+endif
+
+ifeq ($(FLAG_DBG), true)
+    TARGETING_UNBUNDLED_FROYO := false
+endif
+
+ifeq ($(FLAG_DO_PROFILE), true)
+    TARGETING_UNBUNDLED_FROYO := false
+endif
+
+ifeq ($(TARGETING_UNBUNDLED_FROYO), true)
+    LOCAL_NDK_VERSION := 4
+    LOCAL_SDK_VERSION := 8
+endif
+
+LOCAL_MODULE := libjni_latinime
+
+LOCAL_MODULE_TAGS := user
+
+# For STL
+LOCAL_C_INCLUDES += external/stlport/stlport bionic
+LOCAL_SHARED_LIBRARIES += libstlport
+
+ifeq ($(FLAG_DO_PROFILE), true)
+    $(warning Making profiling version of native library)
+    LOCAL_CFLAGS += -DFLAG_DO_PROFILE
+    LOCAL_SHARED_LIBRARIES += libcutils libutils
+else # FLAG_DO_PROFILE
+ifeq ($(FLAG_DBG), true)
+    $(warning Making debug version of native library)
+    LOCAL_CFLAGS += -DFLAG_DBG
+    LOCAL_SHARED_LIBRARIES += libcutils libutils
+endif # FLAG_DBG
+endif # FLAG_DO_PROFILE
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/native/jni/Application.mk b/native/jni/Application.mk
new file mode 100644
index 0000000..caf3b26
--- /dev/null
+++ b/native/jni/Application.mk
@@ -0,0 +1 @@
+APP_STL := stlport_static
diff --git a/tests/Android.mk b/tests/Android.mk
index 658e8e2..6634070 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -1,3 +1,17 @@
+# Copyright (C) 2011 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.
+
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
diff --git a/tools/Android.mk b/tools/Android.mk
index 8f1acc5..91b2fbb 100644
--- a/tools/Android.mk
+++ b/tools/Android.mk
@@ -12,6 +12,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-LOCAL_PATH := $(call my-dir)
-
-include $(call all-makefiles-under,$(LOCAL_PATH))
+include $(call all-subdir-makefiles)