Merge "Revert "Only add spaces automatically when appropriate."" into jb-mr1.1-dev
diff --git a/java/res/values-fr/donottranslate.xml b/java/res/values-fr/donottranslate.xml
index 5288bd7..10feb71 100644
--- a/java/res/values-fr/donottranslate.xml
+++ b/java/res/values-fr/donottranslate.xml
@@ -21,7 +21,7 @@
     <!-- Symbols that should be swapped with a magic space -->
     <string name="weak_space_swapping_symbols">.,\")]}</string>
     <!-- Symbols that should strip a magic space -->
-    <string name="weak_space_stripping_symbols">"&#x0009;&#x0020;\'\n-/_"</string>
+    <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..f7802a8 100644
--- a/java/res/values/donottranslate.xml
+++ b/java/res/values/donottranslate.xml
@@ -23,7 +23,7 @@
     <!-- Symbols that should be swapped with a weak space -->
     <string name="weak_space_swapping_symbols">.,;:!?)]}\"</string>
     <!-- Symbols that should strip a weak space -->
-    <string name="weak_space_stripping_symbols">"&#x0009;&#x0020;\n/_\'-"@</string>
+    <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/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/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/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 023619d..ddfc273 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;
@@ -365,6 +365,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 +711,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 +730,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 +758,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();
@@ -891,6 +910,7 @@
         // Make a note of the cursor position
         mLastSelectionStart = newSelStart;
         mLastSelectionEnd = newSelEnd;
+        mSubtypeState.currentSubtypeUsed();
     }
 
     /**
@@ -1239,19 +1259,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 +1328,6 @@
             handleBackspace(spaceState);
             mDeleteCount++;
             mExpectingUpdateSelection = true;
-            mShouldSwitchToLastSubtype = true;
             LatinImeLogger.logOnDelete(x, y);
             break;
         case Keyboard.CODE_SHIFT:
@@ -1376,7 +1383,6 @@
                 handleCharacter(primaryCode, keyX, keyY, spaceState);
             }
             mExpectingUpdateSelection = true;
-            mShouldSwitchToLastSubtype = true;
             break;
         }
         switcher.onCodeInput(primaryCode);
diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java
index 2144136..75b67bf 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();
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,