Merge "Add Mongolian keyboard"
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java
index e08536b..2030369 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.java
@@ -1086,6 +1086,9 @@
         /* 101 */ null,
         // U+00BF: "¿" INVERTED QUESTION MARK
         /* 102 */ "?,\u00BF",
+        /* 103 */ "\"",
+        /* 104 */ "\'",
+        /* 105 */ "\'",
     };
 
     /* Language et: Estonian */
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
index bed31a7..5eab292 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java
@@ -160,6 +160,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;
@@ -179,18 +182,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);
@@ -201,8 +205,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);
@@ -234,8 +239,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/DebugSettings.java b/java/src/com/android/inputmethod/latin/DebugSettings.java
index 3af3cab..731b9ba 100644
--- a/java/src/com/android/inputmethod/latin/DebugSettings.java
+++ b/java/src/com/android/inputmethod/latin/DebugSettings.java
@@ -37,6 +37,8 @@
     private static final String DEBUG_MODE_KEY = "debug_mode";
     public static final String FORCE_NON_DISTINCT_MULTITOUCH_KEY = "force_non_distinct_multitouch";
     public static final String PREF_USABILITY_STUDY_MODE = "usability_study_mode";
+    private static final String PREF_STATISTICS_LOGGING_KEY = "enable_logging";
+    private static final boolean SHOW_STATISTICS_LOGGING = false;
 
     private boolean mServiceNeedsRestart = false;
     private CheckBoxPreference mDebugMode;
@@ -55,6 +57,12 @@
                     ResearchLogger.DEFAULT_USABILITY_STUDY_MODE));
             checkbox.setSummary(R.string.settings_warning_researcher_mode);
         }
+        final Preference statisticsLoggingPref = findPreference(PREF_STATISTICS_LOGGING_KEY);
+        if (statisticsLoggingPref instanceof CheckBoxPreference) {
+            if (!SHOW_STATISTICS_LOGGING) {
+                getPreferenceScreen().removePreference(statisticsLoggingPref);
+            }
+        }
 
         mServiceNeedsRestart = false;
         mDebugMode = (CheckBoxPreference) findPreference(DEBUG_MODE_KEY);
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 3dfffc4..5f79073 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -56,7 +56,6 @@
 import android.view.inputmethod.CompletionInfo;
 import android.view.inputmethod.CorrectionInfo;
 import android.view.inputmethod.EditorInfo;
-import android.view.inputmethod.InputMethodManager;
 import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.inputmethod.accessibility.AccessibilityUtils;
@@ -142,7 +141,7 @@
     private SharedPreferences mPrefs;
     @UsedForTesting final KeyboardSwitcher mKeyboardSwitcher;
     private final SubtypeSwitcher mSubtypeSwitcher;
-    private boolean mShouldSwitchToLastSubtype = true;
+    private final SubtypeState mSubtypeState = new SubtypeState();
 
     private boolean mIsMainDictionaryAvailable;
     private UserBinaryDictionary mUserDictionary;
@@ -366,6 +365,33 @@
         }
     }
 
+    static final class SubtypeState {
+        private InputMethodSubtype mLastActiveSubtype;
+        private boolean mCurrentSubtypeUsed;
+
+        public void currentSubtypeUsed() {
+            mCurrentSubtypeUsed = true;
+        }
+
+        public void switchSubtype(final IBinder token, final RichInputMethodManager richImm) {
+            final InputMethodSubtype currentSubtype = richImm.getInputMethodManager()
+                    .getCurrentInputMethodSubtype();
+            final InputMethodSubtype lastActiveSubtype = mLastActiveSubtype;
+            final boolean currentSubtypeUsed = mCurrentSubtypeUsed;
+            if (currentSubtypeUsed) {
+                mLastActiveSubtype = currentSubtype;
+                mCurrentSubtypeUsed = false;
+            }
+            if (currentSubtypeUsed
+                    && richImm.checkIfSubtypeBelongsToThisImeAndEnabled(lastActiveSubtype)
+                    && !currentSubtype.equals(lastActiveSubtype)) {
+                richImm.setInputMethodAndSubtype(token, lastActiveSubtype);
+                return;
+            }
+            richImm.switchToNextInputMethod(token, true /* onlyCurrentIme */);
+        }
+    }
+
     public LatinIME() {
         super();
         mSubtypeSwitcher = SubtypeSwitcher.getInstance();
@@ -710,21 +736,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);
@@ -897,6 +919,7 @@
         // Make a note of the cursor position
         mLastSelectionStart = newSelStart;
         mLastSelectionEnd = newSelEnd;
+        mSubtypeState.currentSubtypeUsed();
     }
 
     /**
@@ -1244,20 +1267,7 @@
             mRichImm.switchToNextInputMethod(token, false /* onlyCurrentIme */);
             return;
         }
-        if (mShouldSwitchToLastSubtype) {
-            final InputMethodManager imm = mRichImm.getInputMethodManager();
-            final InputMethodSubtype lastSubtype = imm.getLastInputMethodSubtype();
-            final boolean lastSubtypeBelongsToThisIme =
-                    mRichImm.checkIfSubtypeBelongsToThisImeAndEnabled(lastSubtype);
-            if (lastSubtypeBelongsToThisIme && imm.switchToLastInputMethod(token)) {
-                mShouldSwitchToLastSubtype = false;
-            } else {
-                mRichImm.switchToNextInputMethod(token, true /* onlyCurrentIme */);
-                mShouldSwitchToLastSubtype = true;
-            }
-        } else {
-            mRichImm.switchToNextInputMethod(token, true /* onlyCurrentIme */);
-        }
+        mSubtypeState.switchSubtype(token, mRichImm);
     }
 
     private void sendDownUpKeyEventForBackwardCompatibility(final int code) {
@@ -1326,7 +1336,6 @@
             handleBackspace(spaceState);
             mDeleteCount++;
             mExpectingUpdateSelection = true;
-            mShouldSwitchToLastSubtype = true;
             LatinImeLogger.logOnDelete(x, y);
             break;
         case Constants.CODE_SHIFT:
@@ -1382,7 +1391,6 @@
                 handleCharacter(primaryCode, keyX, keyY, spaceState);
             }
             mExpectingUpdateSelection = true;
-            mShouldSwitchToLastSubtype = true;
             break;
         }
         switcher.onCodeInput(primaryCode);
@@ -1411,7 +1419,7 @@
         mHandler.postUpdateSuggestionStrip();
         final String text = specificTldProcessingOnTextInput(rawText);
         if (SPACE_STATE_PHANTOM == mSpaceState) {
-            sendKeyCodePoint(Constants.CODE_SPACE);
+            promotePhantomSpace();
         }
         mConnection.commitText(text, 1);
         mConnection.endBatchEdit();
@@ -1574,7 +1582,7 @@
         mWordComposer.setBatchInputWord(batchInputText);
         mConnection.beginBatchEdit();
         if (SPACE_STATE_PHANTOM == mSpaceState) {
-            sendKeyCodePoint(Constants.CODE_SPACE);
+            promotePhantomSpace();
         }
         mConnection.setComposingText(batchInputText, 1);
         mExpectingUpdateSelection = true;
@@ -1729,7 +1737,7 @@
                 // Sanity check
                 throw new RuntimeException("Should not be composing here");
             }
-            sendKeyCodePoint(Constants.CODE_SPACE);
+            promotePhantomSpace();
         }
 
         // NOTE: isCursorTouchingWord() is a blocking IPC call, so it often takes several
@@ -1806,7 +1814,7 @@
 
         if (SPACE_STATE_PHANTOM == spaceState &&
                 mCurrentSettings.isPhantomSpacePromotingSymbol(primaryCode)) {
-            sendKeyCodePoint(Constants.CODE_SPACE);
+            promotePhantomSpace();
         }
         sendKeyCodePoint(primaryCode);
 
@@ -2070,7 +2078,7 @@
             int firstChar = Character.codePointAt(suggestion, 0);
             if ((!mCurrentSettings.isWeakSpaceStripper(firstChar))
                     && (!mCurrentSettings.isWeakSpaceSwapper(firstChar))) {
-                sendKeyCodePoint(Constants.CODE_SPACE);
+                promotePhantomSpace();
             }
         }
 
@@ -2247,6 +2255,11 @@
         mHandler.postUpdateSuggestionStrip();
     }
 
+    // This essentially inserts a space, and that's it.
+    public void promotePhantomSpace() {
+        sendKeyCodePoint(Constants.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/RichInputMethodManager.java b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
index d7055f7..b383230 100644
--- a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
+++ b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
@@ -206,6 +206,11 @@
         return null;
     }
 
+    public void setInputMethodAndSubtype(final IBinder token, final InputMethodSubtype subtype) {
+        mImmWrapper.mImm.setInputMethodAndSubtype(
+                token, mInputMethodInfoOfThisIme.getId(), subtype);
+    }
+
     public void setAdditionalInputMethodSubtypes(final InputMethodSubtype[] subtypes) {
         mImmWrapper.mImm.setAdditionalInputMethodSubtypes(
                 mInputMethodInfoOfThisIme.getId(), subtypes);
diff --git a/native/jni/src/char_utils.cpp b/native/jni/src/char_utils.cpp
index ff05f68..857be98 100644
--- a/native/jni/src/char_utils.cpp
+++ b/native/jni/src/char_utils.cpp
@@ -1045,11 +1045,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/tools/maketext/res/values-es/donottranslate-more-keys.xml b/tools/maketext/res/values-es/donottranslate-more-keys.xml
index 586dbad..961193b 100644
--- a/tools/maketext/res/values-es/donottranslate-more-keys.xml
+++ b/tools/maketext/res/values-es/donottranslate-more-keys.xml
@@ -74,4 +74,7 @@
     <string name="more_keys_for_tablet_comma">"!,&#x00A1;"</string>
     <!-- U+00BF: "¿" INVERTED QUESTION MARK -->
     <string name="more_keys_for_tablet_period">"\?,&#x00BF;"</string>
+    <string name="keylabel_for_apostrophe">\"</string>
+    <string name="keyhintlabel_for_apostrophe">\'</string>
+    <string name="more_keys_for_apostrophe">\'</string>
 </resources>