am d61f9981: Merge "Add -ldl for new stlport"

* commit 'd61f9981da7ece48b669ae67cad29a770bf61308':
  Add -ldl for new stlport
diff --git a/dictionaries/de_wordlist.xml.gz b/dictionaries/de_wordlist.xml.gz
index a4267b3..f193ed2 100644
--- a/dictionaries/de_wordlist.xml.gz
+++ b/dictionaries/de_wordlist.xml.gz
Binary files differ
diff --git a/dictionaries/en_gb_wordlist.xml.gz b/dictionaries/en_gb_wordlist.xml.gz
index 274424c..f955186 100644
--- a/dictionaries/en_gb_wordlist.xml.gz
+++ b/dictionaries/en_gb_wordlist.xml.gz
Binary files differ
diff --git a/dictionaries/en_wordlist.xml.gz b/dictionaries/en_wordlist.xml.gz
index 6e57f42..d5c5b8a 100644
--- a/dictionaries/en_wordlist.xml.gz
+++ b/dictionaries/en_wordlist.xml.gz
Binary files differ
diff --git a/dictionaries/fr_wordlist.xml.gz b/dictionaries/fr_wordlist.xml.gz
index 3134a04..5f8a5eb 100644
--- a/dictionaries/fr_wordlist.xml.gz
+++ b/dictionaries/fr_wordlist.xml.gz
Binary files differ
diff --git a/dictionaries/ru_wordlist.xml.gz b/dictionaries/ru_wordlist.xml.gz
index 877f060..020fe68 100644
--- a/dictionaries/ru_wordlist.xml.gz
+++ b/dictionaries/ru_wordlist.xml.gz
Binary files differ
diff --git a/java/res/mipmap-hdpi/ic_ime_settings.png b/java/res/mipmap-hdpi/ic_ime_settings.png
index 2de998b..486c70d 100644
--- a/java/res/mipmap-hdpi/ic_ime_settings.png
+++ b/java/res/mipmap-hdpi/ic_ime_settings.png
Binary files differ
diff --git a/java/res/mipmap-mdpi/ic_ime_settings.png b/java/res/mipmap-mdpi/ic_ime_settings.png
index 209bab4..75f4afb 100644
--- a/java/res/mipmap-mdpi/ic_ime_settings.png
+++ b/java/res/mipmap-mdpi/ic_ime_settings.png
Binary files differ
diff --git a/java/res/mipmap-xhdpi/ic_ime_settings.png b/java/res/mipmap-xhdpi/ic_ime_settings.png
index 371ac1f..bbf1919 100644
--- a/java/res/mipmap-xhdpi/ic_ime_settings.png
+++ b/java/res/mipmap-xhdpi/ic_ime_settings.png
Binary files differ
diff --git a/java/res/mipmap-xxhdpi/ic_ime_settings.png b/java/res/mipmap-xxhdpi/ic_ime_settings.png
index 17c52dd..16fc693 100644
--- a/java/res/mipmap-xxhdpi/ic_ime_settings.png
+++ b/java/res/mipmap-xxhdpi/ic_ime_settings.png
Binary files differ
diff --git a/java/res/raw/main_de.dict b/java/res/raw/main_de.dict
index 6122cd3..59df8b8 100644
--- a/java/res/raw/main_de.dict
+++ b/java/res/raw/main_de.dict
Binary files differ
diff --git a/java/res/raw/main_en.dict b/java/res/raw/main_en.dict
index de1170a..85ee809 100644
--- a/java/res/raw/main_en.dict
+++ b/java/res/raw/main_en.dict
Binary files differ
diff --git a/java/res/raw/main_es.dict b/java/res/raw/main_es.dict
index 7a4daf1..cb17926 100644
--- a/java/res/raw/main_es.dict
+++ b/java/res/raw/main_es.dict
Binary files differ
diff --git a/java/res/raw/main_fr.dict b/java/res/raw/main_fr.dict
index c607d0e..53fd8d1 100644
--- a/java/res/raw/main_fr.dict
+++ b/java/res/raw/main_fr.dict
Binary files differ
diff --git a/java/res/raw/main_it.dict b/java/res/raw/main_it.dict
index b93a55c..59b78d6 100644
--- a/java/res/raw/main_it.dict
+++ b/java/res/raw/main_it.dict
Binary files differ
diff --git a/java/res/raw/main_pt_br.dict b/java/res/raw/main_pt_br.dict
index 66ac3f9..2a8af6f 100644
--- a/java/res/raw/main_pt_br.dict
+++ b/java/res/raw/main_pt_br.dict
Binary files differ
diff --git a/java/res/raw/main_ru.dict b/java/res/raw/main_ru.dict
index 050b0b8..1cddb05 100644
--- a/java/res/raw/main_ru.dict
+++ b/java/res/raw/main_ru.dict
Binary files differ
diff --git a/java/res/values-fr/donottranslate.xml b/java/res/values-fr/donottranslate.xml
index 5288bd7..e11e65c 100644
--- a/java/res/values-fr/donottranslate.xml
+++ b/java/res/values-fr/donottranslate.xml
@@ -19,9 +19,10 @@
 -->
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <!-- Symbols that should be swapped with a magic space -->
-    <string name="weak_space_swapping_symbols">.,\")]}</string>
+    <string name="weak_space_swapping_symbols">.,)]}</string>
     <!-- Symbols that should strip a magic space -->
-    <string name="weak_space_stripping_symbols">"&#x0009;&#x0020;\'\n-/_"</string>
+    <!-- Don't remove the enclosing double quotes, they protect whitespace (not just U+0020) -->
+    <string name="weak_space_stripping_symbols">"&#x0009;&#x0020;\n"\'-/_\"</string>
     <!-- Symbols that should promote magic spaces into real space -->
     <string name="phantom_space_promoting_symbols">;:!?([*&amp;@{&lt;&gt;+=|</string>
     <!-- Symbols that do NOT separate words -->
diff --git a/java/res/values/donottranslate.xml b/java/res/values/donottranslate.xml
index 9e07b22..a9c5e5d 100644
--- a/java/res/values/donottranslate.xml
+++ b/java/res/values/donottranslate.xml
@@ -21,9 +21,10 @@
     <!-- Symbols that are suggested between words -->
     <string name="suggested_punctuations">!,?,\\,,:,;,\",(,),\',-,/,@,_</string>
     <!-- Symbols that should be swapped with a weak space -->
-    <string name="weak_space_swapping_symbols">.,;:!?)]}\"</string>
+    <string name="weak_space_swapping_symbols">.,;:!?)]}</string>
     <!-- Symbols that should strip a weak space -->
-    <string name="weak_space_stripping_symbols">"&#x0009;&#x0020;\n/_\'-"@</string>
+    <!-- Don't remove the enclosing double quotes, they protect whitespace (not just U+0020) -->
+    <string name="weak_space_stripping_symbols">"&#x0009;&#x0020;\n"/_\'-@\"</string>
     <!-- Symbols that should convert weak spaces into real space -->
     <string name="phantom_space_promoting_symbols">([*&amp;{&lt;&gt;+=|</string>
     <!-- Symbols that do NOT separate words -->
diff --git a/java/res/xml/method.xml b/java/res/xml/method.xml
index 3d360a8..0387071 100644
--- a/java/res/xml/method.xml
+++ b/java/res/xml/method.xml
@@ -36,6 +36,7 @@
     en_GB: English Great Britain/qwerty
     eo: Esperanto/spanish
     es: Spanish/spanish
+    es_US: Spanish United States/spanish
     et: Estonian/nordic
     fa: Persian/arabic
     fi: Finnish/nordic
@@ -184,6 +185,13 @@
     />
     <subtype android:icon="@drawable/ic_subtype_keyboard"
             android:label="@string/subtype_generic"
+            android:subtypeId="-2066550842"
+            android:imeSubtypeLocale="es_US"
+            android:imeSubtypeMode="keyboard"
+            android:imeSubtypeExtraValue="KeyboardLayoutSet=spanish,AsciiCapable"
+    />
+    <subtype android:icon="@drawable/ic_subtype_keyboard"
+            android:label="@string/subtype_generic"
             android:subtypeId="-332580523"
             android:imeSubtypeLocale="et"
             android:imeSubtypeMode="keyboard"
diff --git a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java
index a01c301..ab7bd49 100644
--- a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java
+++ b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java
@@ -52,6 +52,10 @@
         sInstance.mImm = ImfUtils.getInputMethodManager(context);
     }
 
+    public InputMethodSubtype getCurrentInputMethodSubtype() {
+        return mImm.getCurrentInputMethodSubtype();
+    }
+
     public InputMethodSubtype getLastInputMethodSubtype() {
         return mImm.getLastInputMethodSubtype();
     }
@@ -65,6 +69,10 @@
                 onlyCurrentIme);
     }
 
+    public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) {
+        mImm.setInputMethodAndSubtype(token, id, subtype);
+    }
+
     public void showInputMethodPicker() {
         mImm.showInputMethodPicker();
     }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java
index 5e111fb..631e647 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java
@@ -487,6 +487,11 @@
                     // After chording input while normal state.
                     setShifted(UNSHIFT);
                 }
+                // After chording input, automatic shift state may have been changed depending on
+                // what characters were input.
+                mShiftKeyState.onRelease();
+                mSwitchActions.requestUpdatingShiftState();
+                return;
             } else if (mAlphabetShiftState.isShiftLockShifted() && withSliding) {
                 // In shift locked state, shift has been pressed and slid out to other key.
                 setShiftLocked(true);
diff --git a/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java b/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java
index d126077..9394518 100644
--- a/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java
+++ b/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java
@@ -85,6 +85,9 @@
     }
 
     static final class SubtypeLocaleAdapter extends ArrayAdapter<SubtypeLocaleItem> {
+        private static final String TAG = SubtypeLocaleAdapter.class.getSimpleName();
+        private static final boolean DEBUG_SUBTYPE_ID = false;
+
         public SubtypeLocaleAdapter(final Context context) {
             super(context, android.R.layout.simple_spinner_item);
             setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@@ -94,6 +97,11 @@
             final int count = imi.getSubtypeCount();
             for (int i = 0; i < count; i++) {
                 final InputMethodSubtype subtype = imi.getSubtypeAt(i);
+                if (DEBUG_SUBTYPE_ID) {
+                    android.util.Log.d(TAG, String.format("%-6s 0x%08x %11d %s",
+                            subtype.getLocale(), subtype.hashCode(), subtype.hashCode(),
+                            SubtypeLocale.getSubtypeDisplayName(subtype, context.getResources())));
+                }
                 if (subtype.containsExtraValueKey(ASCII_CAPABLE)) {
                     items.add(createItem(context, subtype.getLocale()));
                 }
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
index b0b65ed..9a3f88f 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
@@ -154,6 +154,9 @@
         for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) {
             InputStream originalSourceStream = null;
             InputStream inputStream = null;
+            InputStream uncompressedStream = null;
+            InputStream decryptedStream = null;
+            BufferedInputStream bufferedStream = null;
             File outputFile = null;
             FileOutputStream outputStream = null;
             AssetFileDescriptor afd = null;
@@ -173,18 +176,19 @@
                 // Get the appropriate decryption method for this try
                 switch (mode) {
                     case COMPRESSED_CRYPTED_COMPRESSED:
-                        inputStream = FileTransforms.getUncompressedStream(
-                                FileTransforms.getDecryptedStream(
-                                        FileTransforms.getUncompressedStream(
-                                                originalSourceStream)));
+                        uncompressedStream =
+                                FileTransforms.getUncompressedStream(originalSourceStream);
+                        decryptedStream = FileTransforms.getDecryptedStream(uncompressedStream);
+                        inputStream = FileTransforms.getUncompressedStream(decryptedStream);
                         break;
                     case CRYPTED_COMPRESSED:
-                        inputStream = FileTransforms.getUncompressedStream(
-                                FileTransforms.getDecryptedStream(originalSourceStream));
+                        decryptedStream = FileTransforms.getDecryptedStream(originalSourceStream);
+                        inputStream = FileTransforms.getUncompressedStream(decryptedStream);
                         break;
                     case COMPRESSED_CRYPTED:
-                        inputStream = FileTransforms.getDecryptedStream(
-                                FileTransforms.getUncompressedStream(originalSourceStream));
+                        uncompressedStream =
+                                FileTransforms.getUncompressedStream(originalSourceStream);
+                        inputStream = FileTransforms.getDecryptedStream(uncompressedStream);
                         break;
                     case COMPRESSED_ONLY:
                         inputStream = FileTransforms.getUncompressedStream(originalSourceStream);
@@ -195,8 +199,9 @@
                     case NONE:
                         inputStream = originalSourceStream;
                         break;
-                    }
-                checkMagicAndCopyFileTo(new BufferedInputStream(inputStream), outputStream);
+                }
+                bufferedStream = new BufferedInputStream(inputStream);
+                checkMagicAndCopyFileTo(bufferedStream, outputStream);
                 outputStream.flush();
                 outputStream.close();
                 final File finalFile = new File(finalFileName);
@@ -228,8 +233,11 @@
                 try {
                     // inputStream.close() will close afd, we should not call afd.close().
                     if (null != inputStream) inputStream.close();
+                    if (null != uncompressedStream) uncompressedStream.close();
+                    if (null != decryptedStream) decryptedStream.close();
+                    if (null != bufferedStream) bufferedStream.close();
                 } catch (Exception e) {
-                    Log.e(TAG, "Exception while closing a cross-process file descriptor : " + e);
+                    Log.e(TAG, "Exception while closing a file descriptor : " + e);
                 }
                 try {
                     if (null != outputStream) outputStream.close();
diff --git a/java/src/com/android/inputmethod/latin/InputAttributes.java b/java/src/com/android/inputmethod/latin/InputAttributes.java
index 2f7608a..422fc72 100644
--- a/java/src/com/android/inputmethod/latin/InputAttributes.java
+++ b/java/src/com/android/inputmethod/latin/InputAttributes.java
@@ -29,6 +29,7 @@
     final public boolean mInputTypeNoAutoCorrect;
     final public boolean mIsSettingsSuggestionStripOn;
     final public boolean mApplicationSpecifiedCompletionOn;
+    final public boolean mShouldInsertSpacesAutomatically;
     final private int mInputType;
 
     public InputAttributes(final EditorInfo editorInfo, final boolean isFullscreenMode) {
@@ -54,6 +55,7 @@
             mIsSettingsSuggestionStripOn = false;
             mInputTypeNoAutoCorrect = false;
             mApplicationSpecifiedCompletionOn = false;
+            mShouldInsertSpacesAutomatically = false;
         } else {
             final int variation = inputType & InputType.TYPE_MASK_VARIATION;
             final boolean flagNoSuggestions =
@@ -65,6 +67,7 @@
             final boolean flagAutoComplete =
                     0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
 
+            // TODO: Have a helper method in InputTypeUtils
             // Make sure that passwords are not displayed in {@link SuggestionStripView}.
             if (InputTypeUtils.isPasswordInputType(inputType)
                     || InputTypeUtils.isVisiblePasswordInputType(inputType)
@@ -78,6 +81,8 @@
                 mIsSettingsSuggestionStripOn = true;
             }
 
+            mShouldInsertSpacesAutomatically = InputTypeUtils.isAutoSpaceFriendlyType(inputType);
+
             // If it's a browser edit field and auto correct is not ON explicitly, then
             // disable auto correction, but keep suggestions on.
             // If NO_SUGGESTIONS is set, don't do prediction.
diff --git a/java/src/com/android/inputmethod/latin/InputTypeUtils.java b/java/src/com/android/inputmethod/latin/InputTypeUtils.java
index 500866a..9a4503b 100644
--- a/java/src/com/android/inputmethod/latin/InputTypeUtils.java
+++ b/java/src/com/android/inputmethod/latin/InputTypeUtils.java
@@ -29,31 +29,37 @@
             TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD;
     private static final int TEXT_VISIBLE_PASSWORD_INPUT_TYPE =
             TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
+    private static final int[] SUPPRESSING_AUTO_SPACES_FIELD_VARIATION = {
+        InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS,
+        InputType.TYPE_TEXT_VARIATION_PASSWORD,
+        InputType.TYPE_TEXT_VARIATION_URI,
+        InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,
+        InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD };
 
     private InputTypeUtils() {
         // This utility class is not publicly instantiable.
     }
 
-    private static boolean isWebEditTextInputType(int inputType) {
+    private static boolean isWebEditTextInputType(final int inputType) {
         return inputType == (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EDIT_TEXT);
     }
 
-    private static boolean isWebPasswordInputType(int inputType) {
+    private static boolean isWebPasswordInputType(final int inputType) {
         return WEB_TEXT_PASSWORD_INPUT_TYPE != 0
                 && inputType == WEB_TEXT_PASSWORD_INPUT_TYPE;
     }
 
-    private static boolean isWebEmailAddressInputType(int inputType) {
+    private static boolean isWebEmailAddressInputType(final int inputType) {
         return WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE != 0
                 && inputType == WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE;
     }
 
-    private static boolean isNumberPasswordInputType(int inputType) {
+    private static boolean isNumberPasswordInputType(final int inputType) {
         return NUMBER_PASSWORD_INPUT_TYPE != 0
                 && inputType == NUMBER_PASSWORD_INPUT_TYPE;
     }
 
-    private static boolean isTextPasswordInputType(int inputType) {
+    private static boolean isTextPasswordInputType(final int inputType) {
         return inputType == TEXT_PASSWORD_INPUT_TYPE;
     }
 
@@ -61,12 +67,12 @@
         return variation == TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS;
     }
 
-    public static boolean isEmailVariation(int variation) {
+    public static boolean isEmailVariation(final int variation) {
         return variation == TYPE_TEXT_VARIATION_EMAIL_ADDRESS
                 || isWebEmailAddressVariation(variation);
     }
 
-    public static boolean isWebInputType(int inputType) {
+    public static boolean isWebInputType(final int inputType) {
         final int maskedInputType =
                 inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION);
         return isWebEditTextInputType(maskedInputType) || isWebPasswordInputType(maskedInputType)
@@ -74,7 +80,7 @@
     }
 
     // Please refer to TextView.isPasswordInputType
-    public static boolean isPasswordInputType(int inputType) {
+    public static boolean isPasswordInputType(final int inputType) {
         final int maskedInputType =
                 inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION);
         return isTextPasswordInputType(maskedInputType) || isWebPasswordInputType(maskedInputType)
@@ -82,9 +88,18 @@
     }
 
     // Please refer to TextView.isVisiblePasswordInputType
-    public static boolean isVisiblePasswordInputType(int inputType) {
+    public static boolean isVisiblePasswordInputType(final int inputType) {
         final int maskedInputType =
                 inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION);
         return maskedInputType == TEXT_VISIBLE_PASSWORD_INPUT_TYPE;
     }
+
+    public static boolean isAutoSpaceFriendlyType(final int inputType) {
+        if (TYPE_CLASS_TEXT != (TYPE_MASK_CLASS & inputType)) return false;
+        final int variation = TYPE_MASK_VARIATION & inputType;
+        for (final int fieldVariation : SUPPRESSING_AUTO_SPACES_FIELD_VARIATION) {
+            if (variation == fieldVariation) return false;
+        }
+        return true;
+    }
 }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index cf7eea7..e2a76a4 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -141,7 +141,7 @@
     private SharedPreferences mPrefs;
     /* package for tests */ final KeyboardSwitcher mKeyboardSwitcher;
     private final SubtypeSwitcher mSubtypeSwitcher;
-    private boolean mShouldSwitchToLastSubtype = true;
+    private final SubtypeState mSubtypeState = new SubtypeState();
 
     private boolean mIsMainDictionaryAvailable;
     private UserBinaryDictionary mUserDictionary;
@@ -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);
 
@@ -365,6 +367,34 @@
         }
     }
 
+    static final class SubtypeState {
+        private InputMethodSubtype mLastActiveSubtype;
+        private boolean mCurrentSubtypeUsed;
+
+        public void currentSubtypeUsed() {
+            mCurrentSubtypeUsed = true;
+        }
+
+        public void switchSubtype(final IBinder token, final InputMethodManagerCompatWrapper imm,
+                final Context context) {
+            final InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype();
+            final InputMethodSubtype lastActiveSubtype = mLastActiveSubtype;
+            final boolean currentSubtypeUsed = mCurrentSubtypeUsed;
+            if (currentSubtypeUsed) {
+                mLastActiveSubtype = currentSubtype;
+                mCurrentSubtypeUsed = false;
+            }
+            if (currentSubtypeUsed
+                    && ImfUtils.checkIfSubtypeBelongsToThisImeAndEnabled(context, lastActiveSubtype)
+                    && !currentSubtype.equals(lastActiveSubtype)) {
+                final String id = ImfUtils.getInputMethodIdOfThisIme(context);
+                imm.setInputMethodAndSubtype(token, id, lastActiveSubtype);
+                return;
+            }
+            imm.switchToNextInputMethod(token, true /* onlyCurrentIme */);
+        }
+    }
+
     public LatinIME() {
         super();
         mSubtypeSwitcher = SubtypeSwitcher.getInstance();
@@ -683,8 +713,6 @@
             accessUtils.onStartInputViewInternal(mainKeyboardView, editorInfo, restarting);
         }
 
-        final boolean selectionChanged = mLastSelectionStart != editorInfo.initialSelStart
-                || mLastSelectionEnd != editorInfo.initialSelEnd;
         final boolean inputTypeChanged = !mCurrentSettings.isSameInputType(editorInfo);
         final boolean isDifferentTextField = !restarting || inputTypeChanged;
         if (isDifferentTextField) {
@@ -704,21 +732,17 @@
         updateFullscreenMode();
         mApplicationSpecifiedCompletions = null;
 
-        if (isDifferentTextField || selectionChanged) {
-            // If the selection changed, we reset the input state. Essentially, we come here with
-            // restarting == true when the app called setText() or similar. We should reset the
-            // state if the app set the text to something else, but keep it if it set a suggestion
-            // or something.
-            mEnteredText = null;
-            resetComposingState(true /* alsoResetLastComposedWord */);
-            mDeleteCount = 0;
-            mSpaceState = SPACE_STATE_NONE;
+        // The app calling setText() has the effect of clearing the composing
+        // span, so we should reset our state unconditionally, even if restarting is true.
+        mEnteredText = null;
+        resetComposingState(true /* alsoResetLastComposedWord */);
+        mDeleteCount = 0;
+        mSpaceState = SPACE_STATE_NONE;
 
-            if (mSuggestionStripView != null) {
-                // This will set the punctuation suggestions if next word suggestion is off;
-                // otherwise it will clear the suggestion strip.
-                setPunctuationSuggestions();
-            }
+        if (mSuggestionStripView != null) {
+            // This will set the punctuation suggestions if next word suggestion is off;
+            // otherwise it will clear the suggestion strip.
+            setPunctuationSuggestions();
         }
 
         mConnection.resetCachesUponCursorMove(editorInfo.initialSelStart);
@@ -736,20 +760,17 @@
             // TODO: Come up with a more comprehensive way to reset the keyboard layout when
             // a keyboard layout set doesn't get reloaded in this method.
             switcher.resetKeyboardStateToAlphabet();
+            // In apps like Talk, we come here when the text is sent and the field gets emptied and
+            // we need to re-evaluate the shift state, but not the whole layout which would be
+            // disruptive.
+            // Space state must be updated before calling updateShiftState
+            switcher.updateShiftState();
         }
         setSuggestionStripShownInternal(
                 isSuggestionsStripVisible(), /* needsInputViewShown */ false);
 
         mLastSelectionStart = editorInfo.initialSelStart;
         mLastSelectionEnd = editorInfo.initialSelEnd;
-        // If we come here something in the text state is very likely to have changed.
-        // We should update the shift state regardless of whether we are restarting or not, because
-        // this is not perceived as a layout change that may be disruptive like we may have with
-        // switcher.loadKeyboard; in apps like Talk, we come here when the text is sent and the
-        // field gets emptied and we need to re-evaluate the shift state, but not the whole layout
-        // which would be disruptive.
-        // Space state must be updated before calling updateShiftState
-        mKeyboardSwitcher.updateShiftState();
 
         mHandler.cancelUpdateSuggestionStrip();
         mHandler.cancelDoubleSpacesTimer();
@@ -761,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");
     }
 
@@ -891,6 +925,7 @@
         // Make a note of the cursor position
         mLastSelectionStart = newSelStart;
         mLastSelectionEnd = newSelEnd;
+        mSubtypeState.currentSubtypeUsed();
     }
 
     /**
@@ -1188,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) {
@@ -1239,19 +1296,7 @@
             mImm.switchToNextInputMethod(token, false /* onlyCurrentIme */);
             return;
         }
-        if (mShouldSwitchToLastSubtype) {
-            final InputMethodSubtype lastSubtype = mImm.getLastInputMethodSubtype();
-            final boolean lastSubtypeBelongsToThisIme =
-                    ImfUtils.checkIfSubtypeBelongsToThisImeAndEnabled(this, lastSubtype);
-            if (lastSubtypeBelongsToThisIme && mImm.switchToLastInputMethod(token)) {
-                mShouldSwitchToLastSubtype = false;
-            } else {
-                mImm.switchToNextInputMethod(token, true /* onlyCurrentIme */);
-                mShouldSwitchToLastSubtype = true;
-            }
-        } else {
-            mImm.switchToNextInputMethod(token, true /* onlyCurrentIme */);
-        }
+        mSubtypeState.switchSubtype(token, mImm, this);
     }
 
     private void sendDownUpKeyEventForBackwardCompatibility(final int code) {
@@ -1320,7 +1365,6 @@
             handleBackspace(spaceState);
             mDeleteCount++;
             mExpectingUpdateSelection = true;
-            mShouldSwitchToLastSubtype = true;
             LatinImeLogger.logOnDelete(x, y);
             break;
         case Keyboard.CODE_SHIFT:
@@ -1376,7 +1420,6 @@
                 handleCharacter(primaryCode, keyX, keyY, spaceState);
             }
             mExpectingUpdateSelection = true;
-            mShouldSwitchToLastSubtype = true;
             break;
         }
         switcher.onCodeInput(primaryCode);
@@ -1405,7 +1448,7 @@
         mHandler.postUpdateSuggestionStrip();
         final CharSequence text = specificTldProcessingOnTextInput(rawText);
         if (SPACE_STATE_PHANTOM == mSpaceState) {
-            sendKeyCodePoint(Keyboard.CODE_SPACE);
+            promotePhantomSpace();
         }
         mConnection.commitText(text, 1);
         mConnection.endBatchEdit();
@@ -1568,7 +1611,7 @@
         mWordComposer.setBatchInputWord(batchInputText);
         mConnection.beginBatchEdit();
         if (SPACE_STATE_PHANTOM == mSpaceState) {
-            sendKeyCodePoint(Keyboard.CODE_SPACE);
+            promotePhantomSpace();
         }
         mConnection.setComposingText(batchInputText, 1);
         mExpectingUpdateSelection = true;
@@ -1724,7 +1767,7 @@
                 // Sanity check
                 throw new RuntimeException("Should not be composing here");
             }
-            sendKeyCodePoint(Keyboard.CODE_SPACE);
+            promotePhantomSpace();
         }
 
         // NOTE: isCursorTouchingWord() is a blocking IPC call, so it often takes several
@@ -1802,7 +1845,7 @@
 
         if (SPACE_STATE_PHANTOM == spaceState &&
                 mCurrentSettings.isPhantomSpacePromotingSymbol(primaryCode)) {
-            sendKeyCodePoint(Keyboard.CODE_SPACE);
+            promotePhantomSpace();
         }
         sendKeyCodePoint(primaryCode);
 
@@ -2071,7 +2114,7 @@
             int firstChar = Character.codePointAt(suggestion, 0);
             if ((!mCurrentSettings.isWeakSpaceStripper(firstChar))
                     && (!mCurrentSettings.isWeakSpaceSwapper(firstChar))) {
-                sendKeyCodePoint(Keyboard.CODE_SPACE);
+                promotePhantomSpace();
             }
         }
 
@@ -2249,6 +2292,13 @@
         mHandler.postUpdateSuggestionStrip();
     }
 
+    // This essentially inserts a space, and that's it.
+    public void promotePhantomSpace() {
+        if (mCurrentSettings.shouldInsertSpacesAutomatically()) {
+            sendKeyCodePoint(Keyboard.CODE_SPACE);
+        }
+    }
+
     // Used by the RingCharBuffer
     public boolean isWordSeparator(final int code) {
         return mCurrentSettings.isWordSeparator(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 2144136..3b73227 100644
--- a/java/src/com/android/inputmethod/latin/RichInputConnection.java
+++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java
@@ -145,7 +145,8 @@
         mCurrentCursorPosition = newCursorPosition;
         mComposingText.setLength(0);
         mCommittedTextBeforeComposingText.setLength(0);
-        mCommittedTextBeforeComposingText.append(getTextBeforeCursor(DEFAULT_TEXT_CACHE_SIZE, 0));
+        final CharSequence textBeforeCursor = getTextBeforeCursor(DEFAULT_TEXT_CACHE_SIZE, 0);
+        if (null != textBeforeCursor) mCommittedTextBeforeComposingText.append(textBeforeCursor);
         mCharAfterTheCursor = getTextAfterCursor(1, 0);
         if (null != mIC) {
             mIC.finishComposingText();
@@ -338,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/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java
index 2a778aa..2f49fe9 100644
--- a/java/src/com/android/inputmethod/latin/SettingsValues.java
+++ b/java/src/com/android/inputmethod/latin/SettingsValues.java
@@ -271,6 +271,10 @@
         return mPhantomSpacePromotingSymbols.contains(String.valueOf((char)code));
     }
 
+    public boolean shouldInsertSpacesAutomatically() {
+        return mInputAttributes.mShouldInsertSpacesAutomatically;
+    }
+
     private static boolean isAutoCorrectEnabled(final Resources res,
             final String currentAutoCorrectionSetting) {
         final String autoCorrectionOff = res.getString(
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);
     }
 
diff --git a/native/jni/src/basechars.cpp b/native/jni/src/basechars.cpp
index 379cb62..d97311e 100644
--- a/native/jni/src/basechars.cpp
+++ b/native/jni/src/basechars.cpp
@@ -68,7 +68,7 @@
     0x0049, 0x0069, 0x0049, 0x0069, 0x0049, 0x0069, 0x0049, 0x0069,
     0x0049, 0x0131, 0x0049, 0x0069, 0x004a, 0x006a, 0x004b, 0x006b,
     0x0138, 0x004c, 0x006c, 0x004c, 0x006c, 0x004c, 0x006c, 0x004c,
-    0x006c, 0x0141, 0x0142, 0x004e, 0x006e, 0x004e, 0x006e, 0x004e,
+    0x006c, 0x004c, 0x006c, 0x004e, 0x006e, 0x004e, 0x006e, 0x004e,
     0x006e, 0x02bc, 0x014a, 0x014b, 0x004f, 0x006f, 0x004f, 0x006f,
     0x004f, 0x006f, 0x0152, 0x0153, 0x0052, 0x0072, 0x0052, 0x0072,
     0x0052, 0x0072, 0x0053, 0x0073, 0x0053, 0x0073, 0x0053, 0x0073,
@@ -159,11 +159,11 @@
     0x0415, 0x0415, 0x0402, 0x0413, 0x0404, 0x0405, 0x0406, 0x0406,
     0x0408, 0x0409, 0x040a, 0x040b, 0x041a, 0x0418, 0x0423, 0x040f,
     0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
-    0x0418, 0x0418, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f,
+    0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f,
     0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
     0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f,
     0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
-    0x0438, 0x0438, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f,
+    0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f,
     0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
     0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f,
     0x0435, 0x0435, 0x0452, 0x0433, 0x0454, 0x0455, 0x0456, 0x0456,
diff --git a/native/jni/src/defines.h b/native/jni/src/defines.h
index ea0f0ef..e06ee42 100644
--- a/native/jni/src/defines.h
+++ b/native/jni/src/defines.h
@@ -250,6 +250,12 @@
 #ifndef S_INT_MAX
 #define S_INT_MAX 2147483647 // ((1 << 31) - 1)
 #endif
+#ifndef S_INT_MIN
+// The literal constant -2147483648 does not work in C prior C90, because
+// the compiler tries to fit the positive number into an int and then negate it.
+// GCC warns about this.
+#define S_INT_MIN (-2147483647 - 1) // -(1 << 31)
+#endif
 
 // Define this to use mmap() for dictionary loading.  Undefine to use malloc() instead of mmap().
 // We measured and compared performance of both, and found mmap() is fairly good in terms of
diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateMultiTouchTests.java b/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateMultiTouchTests.java
index f5ad723..053bcb5 100644
--- a/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateMultiTouchTests.java
+++ b/tests/src/com/android/inputmethod/keyboard/internal/KeyboardStateMultiTouchTests.java
@@ -50,7 +50,7 @@
         pressKey(CODE_SYMBOL, SYMBOLS_UNSHIFTED);
         // Press/release symbol letter key.
         chordingPressAndReleaseKey('1', SYMBOLS_UNSHIFTED, SYMBOLS_UNSHIFTED);
-        // Release "123?" key, switch back to alphabet shift unshifted.
+        // Release "123?" key, switch back to alphabet unshifted.
         releaseKey(CODE_SYMBOL, ALPHABET_UNSHIFTED);
     }
 
@@ -330,7 +330,7 @@
         releaseKey('X', ALPHABET_MANUAL_SHIFTED);
         // Release 'Z' key
         releaseKey('Z', ALPHABET_MANUAL_SHIFTED);
-        // Release shift key.
+        // Release shift key, switch back to alphabet shifted.
         releaseKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED);
     }
 
@@ -351,8 +351,24 @@
         releaseKey('X', ALPHABET_MANUAL_SHIFTED);
         // Release 'Z' key
         releaseKey('Z', ALPHABET_MANUAL_SHIFTED);
-        // Release shift key.
+        // Release shift key, updated to alphabet unshifted.
         releaseKey(CODE_SHIFT, ALPHABET_UNSHIFTED);
+
+        // Update shift state with auto caps enabled.
+        pressAndReleaseKey(CODE_AUTO_CAPS_TRIGGER, ALPHABET_UNSHIFTED, ALPHABET_AUTOMATIC_SHIFTED);
+
+        // Press shift key and hold, switch to alphabet shifted.
+        pressKey(CODE_SHIFT, ALPHABET_MANUAL_SHIFTED);
+        // Press 'X' key and hold
+        chordingPressKey('X', ALPHABET_MANUAL_SHIFTED);
+        // Release 'X' key
+        releaseKey('X', ALPHABET_MANUAL_SHIFTED);
+        // Press  key and hold, stays in alphabet shifted.
+        chordingPressKey(CODE_AUTO_CAPS_TRIGGER, ALPHABET_MANUAL_SHIFTED);
+        // Release 'Z' key
+        releaseKey(CODE_AUTO_CAPS_TRIGGER, ALPHABET_MANUAL_SHIFTED);
+        // Release shift key, updated to alphabet automatic shifted.
+        releaseKey(CODE_SHIFT, ALPHABET_AUTOMATIC_SHIFTED);
     }
 
     // Multi touch shift chording input in capitalize character mode.
@@ -372,8 +388,8 @@
         releaseKey('X', ALPHABET_MANUAL_SHIFTED);
         // Release 'Z' key
         releaseKey('Z', ALPHABET_MANUAL_SHIFTED);
-        // Release shift key.
-        releaseKey(CODE_SHIFT, ALPHABET_UNSHIFTED);
+        // Release shift key, updated to alphabet automatic shifted.
+        releaseKey(CODE_SHIFT, ALPHABET_AUTOMATIC_SHIFTED);
     }
 
     public void testLongPressShiftAndChording() {
diff --git a/tests/src/com/android/inputmethod/latin/BlueUnderlineTests.java b/tests/src/com/android/inputmethod/latin/BlueUnderlineTests.java
index a9947c1..03310c8 100644
--- a/tests/src/com/android/inputmethod/latin/BlueUnderlineTests.java
+++ b/tests/src/com/android/inputmethod/latin/BlueUnderlineTests.java
@@ -104,4 +104,20 @@
         final SpanGetter span = new SpanGetter(mTextView.getText(), SuggestionSpan.class);
         assertNull("blue underline removed when cursor is moved", span.mSpan);
     }
+
+    public void testComposingStopsOnSpace() {
+        final String STRING_TO_TYPE = "this ";
+        type(STRING_TO_TYPE);
+        sleep(DELAY_TO_WAIT_FOR_UNDERLINE);
+        // Simulate the onUpdateSelection() event
+        mLatinIME.onUpdateSelection(0, 0, STRING_TO_TYPE.length(), STRING_TO_TYPE.length(), -1, -1);
+        runMessages();
+        // Here the blue underline has been set. testBlueUnderline() is testing for this already,
+        // so let's not test it here again.
+        // Now simulate the user moving the cursor.
+        SpanGetter span = new SpanGetter(mTextView.getText(), UnderlineSpan.class);
+        assertNull("should not be composing, so should not have an underline span", span.mSpan);
+        span = new SpanGetter(mTextView.getText(), SuggestionSpan.class);
+        assertNull("should not be composing, so should not have an underline span", span.mSpan);
+    }
 }