Merge "Resume suggestion when backspacing to the end of a word"
diff --git a/java/res/values-et/donottranslate-more-keys.xml b/java/res/values-et/donottranslate-more-keys.xml
new file mode 100644
index 0000000..d6b3099
--- /dev/null
+++ b/java/res/values-et/donottranslate-more-keys.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 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.
+*/
+-->
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <string name="more_keys_for_a">ä,ā,à,á,â,ã,å,æ,ą</string>
+    <string name="more_keys_for_e">3,ē,è,ė,é,ê,ë,ę,ě</string>
+    <string name="more_keys_for_i">8,ī,ì,į,í,î,ï,ı</string>
+    <string name="more_keys_for_o">9,ö,õ,ò,ó,ô,œ,ő,ø</string>
+    <string name="more_keys_for_u">7,ü,ū,ų,ù,ú,û,ů,ű</string>
+    <string name="more_keys_for_s">š,ß,ś,ş</string>
+    <string name="more_keys_for_n">ņ,ñ,ń,ń</string>
+    <string name="more_keys_for_c">č,ç,ć</string>
+    <string name="more_keys_for_y">6,ý,ÿ</string>
+    <string name="more_keys_for_d">ď</string>
+    <string name="more_keys_for_r">4,ŗ,ř,ŕ</string>
+    <string name="more_keys_for_t">5,ţ,ť</string>
+    <string name="more_keys_for_z">ž,ż,ź</string>
+    <string name="more_keys_for_k">ķ</string>
+    <string name="more_keys_for_l">ļ,ł,ĺ,ľ</string>
+    <string name="more_keys_for_g">ģ,ğ</string>
+</resources>
diff --git a/java/res/values-lt/donottranslate-more-keys.xml b/java/res/values-lt/donottranslate-more-keys.xml
index 6b81e45..e36ce6a 100644
--- a/java/res/values-lt/donottranslate-more-keys.xml
+++ b/java/res/values-lt/donottranslate-more-keys.xml
@@ -18,11 +18,20 @@
 */
 -->
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <string name="more_keys_for_a">ą,à,á,â,ä,æ,ã,å,ā</string>
-    <string name="more_keys_for_e">3,ė,ę,è,é,ê,ë,ē</string>
-    <string name="more_keys_for_i">8,į,î,ï,ì,í,ī</string>
-    <string name="more_keys_for_u">7,ų,ū,û,ü,ù,ú</string>
-    <string name="more_keys_for_s">š,ß,ś</string>
+    <string name="more_keys_for_a">ą,ä,ā,à,á,â,ã,å,æ</string>
+    <string name="more_keys_for_e">3,ė,ę,ē,è,é,ê,ë,ě</string>
+    <string name="more_keys_for_i">8,į,ī,ì,í,î,ï,ı</string>
+    <string name="more_keys_for_o">9,ö,õ,ò,ó,ô,œ,ő,ø</string>
+    <string name="more_keys_for_u">7,ū,ų,ü,ū,ù,ú,û,ů,ű</string>
+    <string name="more_keys_for_s">š,ß,ś,ş</string>
+    <string name="more_keys_for_n">ņ,ñ,ń,ń</string>
     <string name="more_keys_for_c">č,ç,ć</string>
-    <string name="more_keys_for_z">ž,ź,ż</string>
+    <string name="more_keys_for_y">6,ý,ÿ</string>
+    <string name="more_keys_for_d">ď</string>
+    <string name="more_keys_for_r">4,ŗ,ř,ŕ</string>
+    <string name="more_keys_for_t">5,ţ,ť</string>
+    <string name="more_keys_for_z">ž,ż,ź</string>
+    <string name="more_keys_for_k">ķ</string>
+    <string name="more_keys_for_l">ļ,ł,ĺ,ľ</string>
+    <string name="more_keys_for_g">ģ,ğ</string>
 </resources>
diff --git a/java/res/values-lv/donottranslate-more-keys.xml b/java/res/values-lv/donottranslate-more-keys.xml
index 77e1c26..8514e73 100644
--- a/java/res/values-lv/donottranslate-more-keys.xml
+++ b/java/res/values-lv/donottranslate-more-keys.xml
@@ -18,16 +18,20 @@
 */
 -->
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <string name="more_keys_for_a">ā,à,á,â,ä,æ,ã,å</string>
-    <string name="more_keys_for_e">3,ē,è,é,ê,ë,ę,ė</string>
-    <string name="more_keys_for_i">8,ī,î,ï,ì,í,į</string>
-    <string name="more_keys_for_u">7,ū,û,ü,ù,ú</string>
-    <string name="more_keys_for_s">š,ß,ś</string>
-    <string name="more_keys_for_n">ņ,ñ,ń</string>
+    <string name="more_keys_for_a">ā,à,á,â,ã,ä,å,æ,ą</string>
+    <string name="more_keys_for_e">3,ē,ė,è,é,ê,ë,ę,ě</string>
+    <string name="more_keys_for_i">8,ī,į,ì,í,î,ï,ı</string>
+    <string name="more_keys_for_o">9,ò,ó,ô,õ,ö,œ,ő,ø</string>
+    <string name="more_keys_for_u">7,ū,ų,ù,ú,û,ü,ů,ű</string>
+    <string name="more_keys_for_s">š,ß,ś,ş</string>
+    <string name="more_keys_for_n">ņ,ñ,ń,ń</string>
     <string name="more_keys_for_c">č,ç,ć</string>
-    <string name="more_keys_for_r">4,ŗ</string>
-    <string name="more_keys_for_z">ž,ź,ż</string>
+    <string name="more_keys_for_y">6,ý,ÿ</string>
+    <string name="more_keys_for_d">ď</string>
+    <string name="more_keys_for_r">4,ŗ,ř,ŕ</string>
+    <string name="more_keys_for_t">5,ţ,ť</string>
+    <string name="more_keys_for_z">ž,ż,ź</string>
     <string name="more_keys_for_k">ķ</string>
-    <string name="more_keys_for_l">ļ,ł</string>
-    <string name="more_keys_for_g">ģ</string>
+    <string name="more_keys_for_l">ļ,ł,ĺ,ľ</string>
+    <string name="more_keys_for_g">ģ,ğ</string>
 </resources>
diff --git a/java/res/xml/method.xml b/java/res/xml/method.xml
index 6184add..2b2c00c 100644
--- a/java/res/xml/method.xml
+++ b/java/res/xml/method.xml
@@ -20,7 +20,7 @@
 <!-- The attributes in this XML file provide configuration information -->
 <!-- for the Input Method Manager. -->
 
-<!-- Keyboard: en_US, en_GB, ar, cs, da, de, de(QWERTY), es, es_US, fi, fr, fr_CA, fr_CH, hr, hu, it, iw, nb, nl, pl, pt, ru, sr, sv, tr -->
+<!-- Keyboard: en_US, en_GB, ar, cs, da, de, de(QWERTY), es, es_US, et, fi, fr, fr_CA, fr_CH, hr, hu, it, iw, lt, lv, nb, nl, pl, pt, ru, sr, sv, tr -->
 <!-- TODO: use <lang>_keyboard icon instead of a common keyboard icon. -->
 <!-- If IME doesn't have an applicable subtype, the first subtype will be used as a default
      subtype.-->
@@ -77,6 +77,12 @@
     />
     <subtype android:icon="@drawable/ic_subtype_keyboard"
             android:label="@string/subtype_generic"
+            android:imeSubtypeLocale="et"
+            android:imeSubtypeMode="keyboard"
+            android:imeSubtypeExtraValue="AsciiCapable,SupportTouchPositionCorrection"
+    />
+    <subtype android:icon="@drawable/ic_subtype_keyboard"
+            android:label="@string/subtype_generic"
             android:imeSubtypeLocale="fi"
             android:imeSubtypeMode="keyboard"
             android:imeSubtypeExtraValue="AsciiCapable,SupportTouchPositionCorrection"
@@ -126,6 +132,18 @@
     />
     <subtype android:icon="@drawable/ic_subtype_keyboard"
             android:label="@string/subtype_generic"
+            android:imeSubtypeLocale="lt"
+            android:imeSubtypeMode="keyboard"
+            android:imeSubtypeExtraValue="AsciiCapable,SupportTouchPositionCorrection"
+    />
+    <subtype android:icon="@drawable/ic_subtype_keyboard"
+            android:label="@string/subtype_generic"
+            android:imeSubtypeLocale="lv"
+            android:imeSubtypeMode="keyboard"
+            android:imeSubtypeExtraValue="AsciiCapable,SupportTouchPositionCorrection"
+    />
+    <subtype android:icon="@drawable/ic_subtype_keyboard"
+            android:label="@string/subtype_generic"
             android:imeSubtypeLocale="nb"
             android:imeSubtypeMode="keyboard"
             android:imeSubtypeExtraValue="AsciiCapable,SupportTouchPositionCorrection"
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 4f26a21..60b436f 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -403,7 +403,9 @@
             resetPendingImsCallback();
             mIsOrientationChanging = true;
             final LatinIME latinIme = getOuterInstance();
-            latinIme.mKeyboardSwitcher.saveKeyboardState();
+            if (latinIme.isInputViewShown()) {
+                latinIme.mKeyboardSwitcher.saveKeyboardState();
+            }
         }
 
         private void resetPendingImsCallback() {
@@ -1798,6 +1800,7 @@
         // The whitelist should be case-insensitive, so it's not possible to be consistent with
         // a boolean flag. Right now this is handled with a slight hack in
         // WhitelistDictionary#shouldForciblyAutoCorrectFrom.
+        final int quotesCount = wordComposer.trailingSingleQuotesCount();
         final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected(
                 mSuggest.getUnigramDictionaries(),
                 // If the typed string ends with a single quote, for dictionary lookup purposes
@@ -1805,8 +1808,8 @@
                 // typed string in the dictionary (to avoid autocorrecting from an existing
                 // word, so for consistency this lookup should be made WITHOUT the trailing
                 // single quote.
-                wordComposer.isLastCharASingleQuote()
-                        ? typedWord.subSequence(0, typedWord.length() - 1) : typedWord,
+                quotesCount > 0
+                        ? typedWord.subSequence(0, typedWord.length() - quotesCount) : typedWord,
                 preferCapitalization());
         if (mCorrectionMode == Suggest.CORRECTION_FULL
                 || mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM) {
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 5a3c348..2a36f82 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -82,8 +82,6 @@
     public static final String DICT_KEY_USER_BIGRAM = "user_bigram";
     public static final String DICT_KEY_WHITELIST ="whitelist";
 
-    private static String SINGLE_QUOTE_AS_STRING = String.valueOf((char)Keyboard.CODE_SINGLE_QUOTE);
-
     private static final boolean DBG = LatinImeLogger.sDBG;
 
     private AutoCorrection mAutoCorrection;
@@ -109,7 +107,7 @@
     // TODO: Remove these member variables by passing more context to addWord() callback method
     private boolean mIsFirstCharCapitalized;
     private boolean mIsAllUpperCase;
-    private boolean mIsLastCharASingleQuote;
+    private int mTrailingSingleQuotesCount;
 
     private int mCorrectionMode = CORRECTION_BASIC;
 
@@ -299,13 +297,14 @@
         mAutoCorrection.init();
         mIsFirstCharCapitalized = wordComposer.isFirstCharCapitalized();
         mIsAllUpperCase = wordComposer.isAllUpperCase();
-        mIsLastCharASingleQuote = wordComposer.isLastCharASingleQuote();
+        mTrailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount();
         collectGarbage(mSuggestions, mPrefMaxSuggestions);
         Arrays.fill(mScores, 0);
 
         final String typedWord = wordComposer.getTypedWord();
-        final String consideredWord = mIsLastCharASingleQuote
-                ? typedWord.substring(0, typedWord.length() - 1) : typedWord;
+        final String consideredWord = mTrailingSingleQuotesCount > 0
+                ? typedWord.substring(0, typedWord.length() - mTrailingSingleQuotesCount)
+                : typedWord;
         if (typedWord != null) {
             // Treating USER_TYPED as UNIGRAM suggestion for logging now.
             LatinImeLogger.onAddSuggestedWord(typedWord, Suggest.DIC_USER_TYPED,
@@ -360,9 +359,11 @@
                 if (key.equals(DICT_KEY_USER_UNIGRAM) || key.equals(DICT_KEY_WHITELIST))
                     continue;
                 final Dictionary dictionary = mUnigramDictionaries.get(key);
-                if (mIsLastCharASingleQuote) {
+                if (mTrailingSingleQuotesCount > 0) {
                     final WordComposer tmpWordComposer = new WordComposer(wordComposer);
-                    tmpWordComposer.deleteLast();
+                    for (int i = mTrailingSingleQuotesCount - 1; i >= 0; --i) {
+                        tmpWordComposer.deleteLast();
+                    }
                     dictionary.getWords(tmpWordComposer, this, proximityInfo);
                 } else {
                     dictionary.getWords(wordComposer, this, proximityInfo);
@@ -380,8 +381,15 @@
                 whitelistedWord);
 
         if (whitelistedWord != null) {
-            mSuggestions.add(0, mIsLastCharASingleQuote
-                    ? whitelistedWord + SINGLE_QUOTE_AS_STRING : whitelistedWord);
+            if (mTrailingSingleQuotesCount > 0) {
+                final StringBuilder sb = new StringBuilder(whitelistedWord);
+                for (int i = mTrailingSingleQuotesCount - 1; i >= 0; --i) {
+                    sb.appendCodePoint(Keyboard.CODE_SINGLE_QUOTE);
+                }
+                mSuggestions.add(0, sb.toString());
+            } else {
+                mSuggestions.add(0, whitelistedWord);
+            }
         }
 
         if (typedWord != null) {
@@ -500,7 +508,7 @@
         } else {
             sb.append(word, offset, length);
         }
-        if (mIsLastCharASingleQuote) {
+        for (int i = mTrailingSingleQuotesCount - 1; i >= 0; --i) {
             sb.appendCodePoint(Keyboard.CODE_SINGLE_QUOTE);
         }
         suggestions.add(pos, sb);
diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java
index ef2e4d3..c9314ee 100644
--- a/java/src/com/android/inputmethod/latin/WordComposer.java
+++ b/java/src/com/android/inputmethod/latin/WordComposer.java
@@ -46,7 +46,7 @@
 
     private boolean mAutoCapitalized;
     // Cache this value for performance
-    private boolean mIsLastCharASingleQuote;
+    private int mTrailingSingleQuotesCount;
 
     /**
      * Whether the user chose to capitalize the first char of the word.
@@ -59,7 +59,7 @@
         mTypedWord = new StringBuilder(N);
         mXCoordinates = new int[N];
         mYCoordinates = new int[N];
-        mIsLastCharASingleQuote = false;
+        mTrailingSingleQuotesCount = 0;
     }
 
     public WordComposer(WordComposer source) {
@@ -74,7 +74,7 @@
         mCapsCount = source.mCapsCount;
         mIsFirstCharCapitalized = source.mIsFirstCharCapitalized;
         mAutoCapitalized = source.mAutoCapitalized;
-        mIsLastCharASingleQuote = source.mIsLastCharASingleQuote;
+        mTrailingSingleQuotesCount = source.mTrailingSingleQuotesCount;
     }
 
     /**
@@ -85,7 +85,7 @@
         mTypedWord.setLength(0);
         mCapsCount = 0;
         mIsFirstCharCapitalized = false;
-        mIsLastCharASingleQuote = false;
+        mTrailingSingleQuotesCount = 0;
     }
 
     /**
@@ -135,7 +135,11 @@
         mIsFirstCharCapitalized = isFirstCharCapitalized(
                 newIndex, primaryCode, mIsFirstCharCapitalized);
         if (Character.isUpperCase(primaryCode)) mCapsCount++;
-        mIsLastCharASingleQuote = Keyboard.CODE_SINGLE_QUOTE == primaryCode;
+        if (Keyboard.CODE_SINGLE_QUOTE == primaryCode) {
+            ++mTrailingSingleQuotesCount;
+        } else {
+            mTrailingSingleQuotesCount = 0;
+        }
     }
 
     /**
@@ -211,10 +215,14 @@
         }
         if (size() == 0) {
             mIsFirstCharCapitalized = false;
-            mIsLastCharASingleQuote = false;
+        }
+        if (mTrailingSingleQuotesCount > 0) {
+            --mTrailingSingleQuotesCount;
         } else {
-            mIsLastCharASingleQuote =
-                    Keyboard.CODE_SINGLE_QUOTE == mTypedWord.codePointAt(mTypedWord.length() - 1);
+            for (int i = mTypedWord.length() - 1; i >= 0; --i) {
+                if (Keyboard.CODE_SINGLE_QUOTE != mTypedWord.codePointAt(i)) break;
+                ++mTrailingSingleQuotesCount;
+            }
         }
     }
 
@@ -237,8 +245,8 @@
         return mIsFirstCharCapitalized;
     }
 
-    public boolean isLastCharASingleQuote() {
-        return mIsLastCharASingleQuote;
+    public int trailingSingleQuotesCount() {
+        return mTrailingSingleQuotesCount;
     }
 
     /**