Merge "Hook for fetching sync content from UserHistoryDict"
diff --git a/common/src/com/android/inputmethod/latin/common/Constants.java b/common/src/com/android/inputmethod/latin/common/Constants.java
index abc377a..a860d35 100644
--- a/common/src/com/android/inputmethod/latin/common/Constants.java
+++ b/common/src/com/android/inputmethod/latin/common/Constants.java
@@ -179,7 +179,7 @@
 
     // (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram is supported in Java side. Needs to modify
     // MAX_PREV_WORD_COUNT_FOR_N_GRAM in native/jni/src/defines.h for suggestions.
-    public static final int MAX_PREV_WORD_COUNT_FOR_N_GRAM = 2;
+    public static final int MAX_PREV_WORD_COUNT_FOR_N_GRAM = 3;
 
     // Key events coming any faster than this are long-presses.
     public static final int LONG_PRESS_MILLISECONDS = 200;
diff --git a/common/src/com/android/inputmethod/latin/common/LocaleUtils.java b/common/src/com/android/inputmethod/latin/common/LocaleUtils.java
index 7f2333b..d5878c0 100644
--- a/common/src/com/android/inputmethod/latin/common/LocaleUtils.java
+++ b/common/src/com/android/inputmethod/latin/common/LocaleUtils.java
@@ -104,7 +104,8 @@
      * @param testedLocale the locale to test.
      * @return a constant that measures how well the tested locale matches the reference locale.
      */
-    public static int getMatchLevel(final String referenceLocale, final String testedLocale) {
+    public static int getMatchLevel(@Nullable final String referenceLocale,
+            @Nullable final String testedLocale) {
         if (StringUtils.isEmpty(referenceLocale)) {
             return StringUtils.isEmpty(testedLocale) ? LOCALE_FULL_MATCH : LOCALE_ANY_MATCH;
         }
@@ -167,11 +168,8 @@
      * @param localeString a string specification of a locale, in a format of "ll_cc_variant" where
      * "ll" is a language code, "cc" is a country code.
      */
-    @Nullable
-    public static Locale constructLocaleFromString(@Nullable final String localeString) {
-        if (localeString == null) {
-            return null;
-        }
+    @Nonnull
+    public static Locale constructLocaleFromString(@Nonnull final String localeString) {
         synchronized (sLocaleCache) {
             if (sLocaleCache.containsKey(localeString)) {
                 return sLocaleCache.get(localeString);
diff --git a/java/res/layout/additional_subtype_dialog.xml b/java/res/layout/additional_subtype_dialog.xml
index b7804f5..2de7d07 100644
--- a/java/res/layout/additional_subtype_dialog.xml
+++ b/java/res/layout/additional_subtype_dialog.xml
@@ -38,7 +38,6 @@
             android:text="@string/subtype_locale" />
         <Spinner
             android:id="@+id/subtype_locale_spinner"
-            android:spinnerMode="dialog"
             android:layout_width="0dp"
             android:layout_weight="70"
             android:layout_height="wrap_content"
@@ -47,7 +46,8 @@
             android:layout_marginTop="8dip"
             android:layout_gravity="fill_horizontal|center_vertical"
             android:gravity="start|left"
-            android:prompt="@string/subtype_locale" />
+            android:prompt="@string/subtype_locale"
+            style="@style/additionalSubtypeSpinnerStyle" />
         </LinearLayout>
     <LinearLayout
         android:orientation="horizontal"
@@ -63,7 +63,6 @@
             android:text="@string/keyboard_layout_set" />
         <Spinner
             android:id="@+id/keyboard_layout_set_spinner"
-            android:spinnerMode="dialog"
             android:layout_width="0dp"
             android:layout_weight="70"
             android:layout_height="wrap_content"
@@ -72,6 +71,7 @@
             android:layout_marginTop="8dip"
             android:layout_gravity="fill_horizontal|center_vertical"
             android:gravity="start|left"
-            android:prompt="@string/keyboard_layout_set" />
+            android:prompt="@string/keyboard_layout_set"
+            style="@style/additionalSubtypeSpinnerStyle" />
     </LinearLayout>
 </LinearLayout>
diff --git a/java/res/layout/input_view.xml b/java/res/layout/input_view.xml
index ae3c19d..98ef02b 100644
--- a/java/res/layout/input_view.xml
+++ b/java/res/layout/input_view.xml
@@ -23,10 +23,18 @@
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     style="?attr/inputViewStyle">
-    <include
-        android:id="@+id/main_keyboard_frame"
-        layout="@layout/main_keyboard_frame" />
-    <include
-        android:id="@+id/emoji_palettes_view"
-        layout="@layout/emoji_palettes_view" />
+    <LinearLayout
+        android:id="@+id/horizontal_keyboard_frame"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="bottom"
+        android:gravity="bottom"
+        android:orientation="horizontal">
+        <include
+            android:id="@+id/main_keyboard_frame"
+            layout="@layout/main_keyboard_frame" />
+        <include
+            android:id="@+id/emoji_palettes_view"
+            layout="@layout/emoji_palettes_view" />
+    </LinearLayout>
 </com.android.inputmethod.latin.InputView>
diff --git a/java/res/values-eu-rES/strings-emoji-descriptions.xml b/java/res/values-eu-rES/strings-emoji-descriptions.xml
index c774ae1..49cf216 100644
--- a/java/res/values-eu-rES/strings-emoji-descriptions.xml
+++ b/java/res/values-eu-rES/strings-emoji-descriptions.xml
@@ -288,7 +288,7 @@
     <string name="spoken_emoji_1F382" msgid="4720497171946687501">"Urtebetetze-pastela"</string>
     <string name="spoken_emoji_1F383" msgid="3536505941578757623">"Halloween-eko kuia-lanpara"</string>
     <string name="spoken_emoji_1F384" msgid="1797870204479059004">"Gabonetako zuhaitza"</string>
-    <string name="spoken_emoji_1F385" msgid="1754174063483626367">"Aita Noel"</string>
+    <string name="spoken_emoji_1F385" msgid="1754174063483626367">"Bizarzuri"</string>
     <string name="spoken_emoji_1F386" msgid="2130445450758114746">"Su artifizialak"</string>
     <string name="spoken_emoji_1F387" msgid="3403182563117999933">"Bengala"</string>
     <string name="spoken_emoji_1F388" msgid="2903047203723251804">"Puxika"</string>
@@ -602,7 +602,7 @@
     <string name="spoken_emoji_1F4F0" msgid="6330208624731662525">"Egunkaria"</string>
     <string name="spoken_emoji_1F4F1" msgid="3966503935581675695">"Telefono mugikorra"</string>
     <string name="spoken_emoji_1F4F2" msgid="1057540341746100087">"Telefono mugikorra eta geziak eskuinalderantz ezkerraldean"</string>
-    <string name="spoken_emoji_1F4F3" msgid="5003984447315754658">"Bibrazio-modua"</string>
+    <string name="spoken_emoji_1F4F3" msgid="5003984447315754658">"Dardara modua"</string>
     <string name="spoken_emoji_1F4F4" msgid="5549847566968306253">"Telefono mugikorra itzalita"</string>
     <string name="spoken_emoji_1F4F5" msgid="3660199448671699238">"Telefono mugikorrik ez"</string>
     <string name="spoken_emoji_1F4F6" msgid="2676974903233268860">"Antena barrekin"</string>
diff --git a/java/res/values-fi/strings.xml b/java/res/values-fi/strings.xml
index 0308d18..6ef4b4e 100644
--- a/java/res/values-fi/strings.xml
+++ b/java/res/values-fi/strings.xml
@@ -170,9 +170,9 @@
     <string name="install_dict" msgid="180852772562189365">"Asenna"</string>
     <string name="cancel_download_dict" msgid="7843340278507019303">"Peruuta"</string>
     <string name="delete_dict" msgid="756853268088330054">"Poista"</string>
-    <string name="should_download_over_metered_prompt" msgid="1583881200688185508">"Laitteesi käyttökielelle on saatavilla sanakirja.&lt;br/&gt; Suosittelemme <xliff:g id="LANGUAGE_NAME">%1$s</xliff:g>-sanakirjan &lt;b&gt;lataamista&lt;/b&gt;, sillä se helpottaa laitteella kirjoittamista.&lt;br/&gt; &lt;br/&gt; Lataus kestää useimmiten muutaman minuutin 3G-yhteydellä. Latauksesta saatetaan periä maksu, ellei käytössäsi ole &lt;b&gt;rajoittamatonta tiedonsiirtopakettia&lt;/b&gt;.&lt;br/&gt; Jos et ole varma tiedonsiirtosopimuksesi tyypistä, etsi käyttöösi wifi-yhteys, niin lataus alkaa automaattisesti.&lt;br/&gt; &lt;br/&gt; Vinkki: voit ladata ja poistaa sanakirjoja mobiililaitteesi &lt;b&gt;Asetukset&lt;/b&gt;-valikon &lt;b&gt;Kieli ja syöttötapa&lt;/b&gt; -osiossa."</string>
+    <string name="should_download_over_metered_prompt" msgid="1583881200688185508">"Laitteesi käyttökielelle on saatavilla sanakirja.&lt;br/&gt; Suosittelemme <xliff:g id="LANGUAGE_NAME">%1$s</xliff:g>-sanakirjan &lt;b&gt;lataamista&lt;/b&gt;, sillä se helpottaa laitteella kirjoittamista.&lt;br/&gt; &lt;br/&gt; Lataus kestää useimmiten muutaman minuutin 3G-yhteydellä. Latauksesta saatetaan periä maksu, ellei käytössäsi ole &lt;b&gt;rajoittamatonta tiedonsiirtopakettia&lt;/b&gt;.&lt;br/&gt; Jos et ole varma tiedonsiirtosopimuksesi tyypistä, etsi käyttöösi Wi-Fi-yhteys, niin lataus alkaa automaattisesti.&lt;br/&gt; &lt;br/&gt; Vinkki: voit ladata ja poistaa sanakirjoja mobiililaitteesi &lt;b&gt;Asetukset&lt;/b&gt;-valikon &lt;b&gt;Kieli ja syöttötapa&lt;/b&gt; -osiossa."</string>
     <string name="download_over_metered" msgid="1643065851159409546">"Lataa nyt (<xliff:g id="SIZE_IN_MEGABYTES">%1$.1f</xliff:g> Mt)"</string>
-    <string name="do_not_download_over_metered" msgid="2176209579313941583">"Lataa wifi-yhteydellä"</string>
+    <string name="do_not_download_over_metered" msgid="2176209579313941583">"Lataa Wi-Fi-yhteydellä"</string>
     <string name="dict_available_notification_title" msgid="4583842811218581658">"Sanakirja on saatavilla kielelle <xliff:g id="LANGUAGE_NAME">%1$s</xliff:g>"</string>
     <string name="dict_available_notification_description" msgid="1075194169443163487">"Paina tätä, jos haluat tarkastella kohdetta ja ladata sen"</string>
     <string name="toast_downloading_suggestions" msgid="6128155879830851739">"Ladataan: kielen <xliff:g id="LANGUAGE_NAME">%1$s</xliff:g> ehdotukset ovat pian käytettävissä."</string>
diff --git a/java/res/values-gl-rES/strings.xml b/java/res/values-gl-rES/strings.xml
index d72bcb8..9616a26 100644
--- a/java/res/values-gl-rES/strings.xml
+++ b/java/res/values-gl-rES/strings.xml
@@ -30,9 +30,9 @@
     <string name="settings_screen_accounts" msgid="7570397912370223287">"Contas e privacidade"</string>
     <string name="settings_screen_appearance" msgid="9153102634339912029">"Aparencia e deseños"</string>
     <string name="settings_screen_multilingual" msgid="1391301621464509659">"Opcións multilingües"</string>
-    <string name="settings_screen_gesture" msgid="8826372746901183556">"Escritura mediante xestos"</string>
+    <string name="settings_screen_gesture" msgid="8826372746901183556">"Escritura xestual"</string>
     <string name="settings_screen_correction" msgid="1616818407747682955">"Corrección de texto"</string>
-    <string name="settings_screen_advanced" msgid="7472408607625972994">"Avanzada"</string>
+    <string name="settings_screen_advanced" msgid="7472408607625972994">"Opcións avanzadas"</string>
     <string name="settings_screen_theme" msgid="2137262503543943871">"Tema"</string>
     <string name="enable_split_keyboard" msgid="4177264923999493614">"Activar teclado dividido"</string>
     <string name="include_other_imes_in_language_switch_list" msgid="4533689960308565519">"Outros métodos de entrada"</string>
diff --git a/java/res/values-hi/strings.xml b/java/res/values-hi/strings.xml
index 239b56f..a4f2dd3 100644
--- a/java/res/values-hi/strings.xml
+++ b/java/res/values-hi/strings.xml
@@ -94,12 +94,12 @@
     <string name="subtype_en_GB" msgid="88170601942311355">"अंग्रेज़ी (यूके)"</string>
     <string name="subtype_en_US" msgid="6160452336634534239">"अंग्रेज़ी (यूएस)"</string>
     <string name="subtype_es_US" msgid="5583145191430180200">"स्पेनिश (यूएस)"</string>
-    <string name="subtype_hi_ZZ" msgid="8860448146262798623">"Hinglish"</string>
+    <string name="subtype_hi_ZZ" msgid="8860448146262798623">"हिंग्लिश"</string>
     <string name="subtype_sr_ZZ" msgid="9059219552986034343">"सर्बियाई (लैटिन)"</string>
     <string name="subtype_with_layout_en_GB" msgid="1931018968641592304">"अंग्रेज़ी (यूके) (<xliff:g id="KEYBOARD_LAYOUT">%s</xliff:g>)"</string>
     <string name="subtype_with_layout_en_US" msgid="8809311287529805422">"अंग्रेज़ी (यूएस) (<xliff:g id="KEYBOARD_LAYOUT">%s</xliff:g>)"</string>
     <string name="subtype_with_layout_es_US" msgid="510930471167541338">"स्‍पेनिश (यूएस) (<xliff:g id="KEYBOARD_LAYOUT">%s</xliff:g>)"</string>
-    <string name="subtype_with_layout_hi_ZZ" msgid="6827402953860547044">"Hinglish (<xliff:g id="KEYBOARD_LAYOUT">%s</xliff:g>)"</string>
+    <string name="subtype_with_layout_hi_ZZ" msgid="6827402953860547044">"हिंग्लिश (<xliff:g id="KEYBOARD_LAYOUT">%s</xliff:g>)"</string>
     <string name="subtype_with_layout_sr_ZZ" msgid="2859024772719772407">"सर्बियाई (<xliff:g id="KEYBOARD_LAYOUT">%s</xliff:g>)"</string>
     <string name="subtype_generic_traditional" msgid="8584594350973800586">"<xliff:g id="LANGUAGE_NAME">%s</xliff:g> (पारंपरिक)"</string>
     <string name="subtype_generic_compact" msgid="3353673321203202922">"<xliff:g id="LANGUAGE_NAME">%s</xliff:g> (संक्षिप्त)"</string>
diff --git a/java/res/values-km-rKH/strings-emoji-descriptions.xml b/java/res/values-km-rKH/strings-emoji-descriptions.xml
index e9b8780..aca04fc 100644
--- a/java/res/values-km-rKH/strings-emoji-descriptions.xml
+++ b/java/res/values-km-rKH/strings-emoji-descriptions.xml
@@ -25,16 +25,16 @@
 
 <resources xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <string name="spoken_emoji_00A9" msgid="2859822817116803638">"សញ្ញា​រក្សា​សិទ្ធ​"</string>
+    <string name="spoken_emoji_00A9" msgid="2859822817116803638">"សញ្ញា​រក្សា​សិទ្ធ"</string>
     <string name="spoken_emoji_00AE" msgid="7708335454134589027">"សញ្ញា​​​ចុះ​បញ្ជី"</string>
     <string name="spoken_emoji_203C" msgid="153340916701508663">"សញ្ញា​ឧទាន​​ពីរ"</string>
-    <string name="spoken_emoji_2049" msgid="4877256448299555371">"សញ្ញា​​ឧទាន​សញ្ញា​សួរ​​"</string>
+    <string name="spoken_emoji_2049" msgid="4877256448299555371">"សញ្ញា​​ឧទាន​សញ្ញា​សួរ"</string>
     <string name="spoken_emoji_2122" msgid="9188440722954720429">"សញ្ញា​​​និក្ខិត្តសញ្ញា"</string>
     <string name="spoken_emoji_2139" msgid="9114342638917304327">"ប្រភព​ព័ត៌មាន"</string>
     <string name="spoken_emoji_2194" msgid="8055202727034946680">"ព្រួញ​ឆ្វេងស្ដាំ"</string>
     <string name="spoken_emoji_2195" msgid="8028122253301087407">"ព្រួញ​ឡើង​លើ​ចុះក្រោម"</string>
     <string name="spoken_emoji_2196" msgid="4019164898967854363">"ព្រួញ​ទិសពាយព្យ"</string>
-    <string name="spoken_emoji_2197" msgid="4255723717709017801">"ព្រួញ​​ទិស​ឥសាន្ត​ឦសាន្ត​"</string>
+    <string name="spoken_emoji_2197" msgid="4255723717709017801">"ព្រួញ​​ទិស​ឥសាន្ត​ឦសាន្ត"</string>
     <string name="spoken_emoji_2198" msgid="1452063451313622090">"ព្រួញ​​ទិស​អាគ្នេយ៍"</string>
     <string name="spoken_emoji_2199" msgid="6942722693368807849">"ព្រួញ​​ទិស​និរតី"</string>
     <string name="spoken_emoji_21A9" msgid="5204750172335111188">"ព្រួញ​ទៅ​ឆ្វេង​មាន​ទំពក់"</string>
@@ -45,7 +45,7 @@
     <string name="spoken_emoji_23EA" msgid="2251396938087774944">"ត្រីកោណ​ខ្មៅ​ពីរ​ចង្អុល​ទៅ​ឆ្វេង"</string>
     <string name="spoken_emoji_23EB" msgid="3746885195641491865">"ត្រីកោណ​ខ្មៅ​ពីរ​ចង្អុល​​ឡើង​លើ"</string>
     <string name="spoken_emoji_23EC" msgid="7852372752901163416">"ត្រីកោណ​ខ្មៅ​ពីរ​ចង្អុល​​ចុះក្រោម"</string>
-    <string name="spoken_emoji_23F0" msgid="8474219588750627870">"នាឡិកា​រោទ៍​"</string>
+    <string name="spoken_emoji_23F0" msgid="8474219588750627870">"នាឡិកា​រោទ៍"</string>
     <string name="spoken_emoji_23F3" msgid="166900119581024371">"កែវ​ពិសោធន៍​មាន​ខ្សាច់ហូរ"</string>
     <string name="spoken_emoji_24C2" msgid="3948348737566038470">"អក្សរ​អឹម​ធំ​ក្នុង​រង្វង់"</string>
     <string name="spoken_emoji_25AA" msgid="7865181015100227349">"ការ៉េ​តូច​​ពណ៌ខ្មៅ"</string>
@@ -195,7 +195,7 @@
     <string name="spoken_emoji_1F312" msgid="4458575672576125401">"ព្រះ​ចន្ទ​មួយ​ចំណិត​ស្ដាំ"</string>
     <string name="spoken_emoji_1F313" msgid="7599181787989497294">"ព្រះ​ចន្ទ​ពាក់​កណ្ដាល"</string>
     <string name="spoken_emoji_1F314" msgid="4898293184964365413">"ព្រះ​ចន្ទ​មួយ​ចំណិត​ឆ្វេង"</string>
-    <string name="spoken_emoji_1F315" msgid="3218117051779496309">"ព្រះចន្ទ​ពេញ​វង់​"</string>
+    <string name="spoken_emoji_1F315" msgid="3218117051779496309">"ព្រះចន្ទ​ពេញ​វង់"</string>
     <string name="spoken_emoji_1F316" msgid="2061317145777689569">"ព្រះ​ចន្ទ​ភ្លឺ​មួយ​ចំហៀង"</string>
     <string name="spoken_emoji_1F317" msgid="2721090687319539049">"ព្រះ​ចន្ទ​ភ្លឺ​ពាក់​កណ្ដាល"</string>
     <string name="spoken_emoji_1F318" msgid="3814091755648887570">"ព្រះ​ចន្ទ​ភ្លឺ​មួយ​ចំណិត​ឆ្វេង"</string>
@@ -262,7 +262,7 @@
     <string name="spoken_emoji_1F365" msgid="4963815540953316307">"នំ​ត្រី​រាង​មូល"</string>
     <string name="spoken_emoji_1F366" msgid="7862401745277049404">"ការ៉េម​​បំពង់"</string>
     <string name="spoken_emoji_1F367" msgid="7447972978281980414">"ការ៉េម​កែវ"</string>
-    <string name="spoken_emoji_1F368" msgid="7790003146142724913">"ការ៉េម​"</string>
+    <string name="spoken_emoji_1F368" msgid="7790003146142724913">"ការ៉េម"</string>
     <string name="spoken_emoji_1F369" msgid="7383712944084857350">"ដូណាត់"</string>
     <string name="spoken_emoji_1F36A" msgid="2726271795913042295">"ខូគី"</string>
     <string name="spoken_emoji_1F36B" msgid="6342163604299875931">"​​សូកូឡា"</string>
@@ -280,7 +280,7 @@
     <string name="spoken_emoji_1F377" msgid="1762398562314172075">"កែវ​ស្រា"</string>
     <string name="spoken_emoji_1F378" msgid="5528234560590117516">"កែវ​ស្រា​ក្រឡុក"</string>
     <string name="spoken_emoji_1F379" msgid="790581290787943325">"ភេសជ្ជៈ​​​ត្រូពិក"</string>
-    <string name="spoken_emoji_1F37A" msgid="391966822450619516">"កែវ​​ស្រាបៀ​"</string>
+    <string name="spoken_emoji_1F37A" msgid="391966822450619516">"កែវ​​ស្រាបៀ"</string>
     <string name="spoken_emoji_1F37B" msgid="9015043286465670662">"ជល់​កែវ​ស្រាបៀ"</string>
     <string name="spoken_emoji_1F37C" msgid="2532113819464508894">"ដប​ទឹកដោះ​គោ"</string>
     <string name="spoken_emoji_1F380" msgid="3487363857092458827">"ខ្សែ​បូ"</string>
@@ -313,7 +313,7 @@
     <string name="spoken_emoji_1F3A7" msgid="837856608794094105">"កាស"</string>
     <string name="spoken_emoji_1F3A8" msgid="2332260356509244587">"ក្ដារ​លាយ​ពណ៌​វិចិត្រករ"</string>
     <string name="spoken_emoji_1F3A9" msgid="9045869366525115256">"មួក​​​សម្ដែង​សិល្បៈ"</string>
-    <string name="spoken_emoji_1F3AA" msgid="5728760354237132">"តង់​សៀក​"</string>
+    <string name="spoken_emoji_1F3AA" msgid="5728760354237132">"តង់​សៀក"</string>
     <string name="spoken_emoji_1F3AB" msgid="1657997517193216284">"សំបុត្រ"</string>
     <string name="spoken_emoji_1F3AC" msgid="4317366554314492152">"បន្ទះកណ្ដឹង"</string>
     <string name="spoken_emoji_1F3AD" msgid="607157286336130470">"សម្ដែង​សិល្បៈ"</string>
@@ -334,10 +334,10 @@
     <string name="spoken_emoji_1F3BC" msgid="1608424748821446230">"និមិត្តសញ្ញា​តន្ត្រី"</string>
     <string name="spoken_emoji_1F3BD" msgid="5490786111375627777">"អាវ​កីឡា​​មាន​ខ្សែ​ឆៀង"</string>
     <string name="spoken_emoji_1F3BE" msgid="1851613105691627931">"រ៉ាកែត និង​បាល់"</string>
-    <string name="spoken_emoji_1F3BF" msgid="6862405997423247921">"ជិះ​ស្គី ​និង​ក្ដារ​​ស្គី​"</string>
+    <string name="spoken_emoji_1F3BF" msgid="6862405997423247921">"ជិះ​ស្គី ​និង​ក្ដារ​​ស្គី"</string>
     <string name="spoken_emoji_1F3C0" msgid="7421420756115104085">"បាល់​បោះ​ និង​វណ្ណ​មូល"</string>
     <string name="spoken_emoji_1F3C1" msgid="6926537251677319922">"ទង់ជាតិ​ប្រណាំង​ម៉ូតូ"</string>
-    <string name="spoken_emoji_1F3C2" msgid="5708596929237987082">"អ្នក​ជិះ​​ក្ដារ​រំអិល​លើ​ព្រិល​"</string>
+    <string name="spoken_emoji_1F3C2" msgid="5708596929237987082">"អ្នក​ជិះ​​ក្ដារ​រំអិល​លើ​ព្រិល"</string>
     <string name="spoken_emoji_1F3C3" msgid="5850982999510115824">"អ្នក​រត់"</string>
     <string name="spoken_emoji_1F3C4" msgid="8468355585994639838">"អ្នក​ជិះ​ទូក​រអិល​លើ​ទឹក"</string>
     <string name="spoken_emoji_1F3C6" msgid="9094474706847545409">"ពាន​រង្វាន់"</string>
@@ -354,7 +354,7 @@
     <string name="spoken_emoji_1F3E6" msgid="342132788513806214">"ធនាគារ"</string>
     <string name="spoken_emoji_1F3E7" msgid="6322352038284944265">"ម៉ាស៊ីន​​អេធីអឹម"</string>
     <string name="spoken_emoji_1F3E8" msgid="5864918444350599907">"សណ្ឋាគារ"</string>
-    <string name="spoken_emoji_1F3E9" msgid="7830416185375326938">"សណ្ឋាគារ​ក្ដី​ស្រឡាញ់​"</string>
+    <string name="spoken_emoji_1F3E9" msgid="7830416185375326938">"សណ្ឋាគារ​ក្ដី​ស្រឡាញ់"</string>
     <string name="spoken_emoji_1F3EA" msgid="5081084413084360479">"ហាង​​ទំនិញ ២៤​ម៉ោង"</string>
     <string name="spoken_emoji_1F3EB" msgid="7010966528205150525">"សាលារៀន"</string>
     <string name="spoken_emoji_1F3EC" msgid="4845978861878295154">"ហាង​​ទំនិញធំៗ"</string>
@@ -439,12 +439,12 @@
     <string name="spoken_emoji_1F44D" msgid="6182553970602667815">"​មេដៃ​ឡើង​លើ"</string>
     <string name="spoken_emoji_1F44E" msgid="8030851867365111809">"​មេដៃ​ចុះ​ក្រោម"</string>
     <string name="spoken_emoji_1F44F" msgid="5148753662268213389">"ទះ​ដៃ"</string>
-    <string name="spoken_emoji_1F450" msgid="1012021072085157054">"លា​ដៃ​"</string>
+    <string name="spoken_emoji_1F450" msgid="1012021072085157054">"លា​ដៃ"</string>
     <string name="spoken_emoji_1F451" msgid="8257466714629051320">"មកុដ"</string>
     <string name="spoken_emoji_1F452" msgid="4567394011149905466">"មួក​​ស្ត្រី"</string>
     <string name="spoken_emoji_1F453" msgid="5978410551173163010">"វ៉ែនតា"</string>
-    <string name="spoken_emoji_1F454" msgid="348469036193323252">"ក្រ​វ៉ាត់​ករ​"</string>
-    <string name="spoken_emoji_1F455" msgid="5665118831861433578">"អាវ​យឺត​​"</string>
+    <string name="spoken_emoji_1F454" msgid="348469036193323252">"ក្រ​វ៉ាត់​ករ"</string>
+    <string name="spoken_emoji_1F455" msgid="5665118831861433578">"អាវ​យឺត"</string>
     <string name="spoken_emoji_1F456" msgid="1890991330923356408">"ខោ​ខោវប៊យ"</string>
     <string name="spoken_emoji_1F457" msgid="3904310482655702620">"សំលៀក​បំពាក់"</string>
     <string name="spoken_emoji_1F458" msgid="5704243858031107692">"គី​ម៉ូណូ"</string>
@@ -463,8 +463,8 @@
     <string name="spoken_emoji_1F465" msgid="4461307702499679879">"គណនី"</string>
     <string name="spoken_emoji_1F466" msgid="1938873085514108889">"ក្មេង​​ប្រុស"</string>
     <string name="spoken_emoji_1F467" msgid="8237080594860144998">"ក្មេង​ស្រី"</string>
-    <string name="spoken_emoji_1F468" msgid="6081300722526675382">"បុរស​"</string>
-    <string name="spoken_emoji_1F469" msgid="1090140923076108158">"ស្ត្រី​"</string>
+    <string name="spoken_emoji_1F468" msgid="6081300722526675382">"បុរស"</string>
+    <string name="spoken_emoji_1F469" msgid="1090140923076108158">"ស្ត្រី"</string>
     <string name="spoken_emoji_1F46A" msgid="5063570981942606595">"គ្រួសារ"</string>
     <string name="spoken_emoji_1F46B" msgid="6795882374287327952">"បុរស​​ និង​ស្ត្រី​កាន់ដៃ​គ្នា"</string>
     <string name="spoken_emoji_1F46C" msgid="6844464165783964495">"បុរស​ពីរ​នាក់​កាន់​ដៃ​គ្នា"</string>
@@ -490,16 +490,16 @@
     <string name="spoken_emoji_1F480" msgid="3696253485164878739">"លលាដ៍​ក្បាល"</string>
     <string name="spoken_emoji_1F481" msgid="320408708521966893">"អ្នក​ផ្ដល់​ព័ត៌មាន"</string>
     <string name="spoken_emoji_1F482" msgid="3424354860245608949">"អ្នក​យាម"</string>
-    <string name="spoken_emoji_1F483" msgid="3221113594843849083">"អ្នក​រាំ​"</string>
+    <string name="spoken_emoji_1F483" msgid="3221113594843849083">"អ្នក​រាំ"</string>
     <string name="spoken_emoji_1F484" msgid="7348014979080444885">"ក្រេម​លាប​បបូរ​មាត់"</string>
     <string name="spoken_emoji_1F485" msgid="6133507975565116339">"ថ្នាំ​លាប​​​ក្រចក"</string>
     <string name="spoken_emoji_1F486" msgid="9085459968247394155">"ម៉ាស្សា​មុខ"</string>
     <string name="spoken_emoji_1F487" msgid="1479113637259592150">"កាត់សក់"</string>
     <string name="spoken_emoji_1F488" msgid="6922559285234100252">"ស្លាក​សញ្ញា​កាត់សក់"</string>
     <string name="spoken_emoji_1F489" msgid="8114863680950147305">"ស៊ីរ៉ាំង"</string>
-    <string name="spoken_emoji_1F48A" msgid="8526843630145963032">"ថ្នាំ​គ្រាប់​"</string>
+    <string name="spoken_emoji_1F48A" msgid="8526843630145963032">"ថ្នាំ​គ្រាប់"</string>
     <string name="spoken_emoji_1F48B" msgid="2538528967897640292">"ស្នាម​ថើប"</string>
-    <string name="spoken_emoji_1F48C" msgid="1681173271652890232">"លិខិត​ស្នេហា​"</string>
+    <string name="spoken_emoji_1F48C" msgid="1681173271652890232">"លិខិត​ស្នេហា"</string>
     <string name="spoken_emoji_1F48D" msgid="8259886164999042373">"រោទ៍"</string>
     <string name="spoken_emoji_1F48E" msgid="8777981696011111101">"ត្បូង​ថ្ម"</string>
     <string name="spoken_emoji_1F48F" msgid="741593675183677907">"ថើប"</string>
@@ -525,7 +525,7 @@
     <string name="spoken_emoji_1F4A3" msgid="6378351742957821735">"គ្រាប់បែក"</string>
     <string name="spoken_emoji_1F4A4" msgid="7217736258870346625">"និមិត្ត​សញ្ញា​​ដេក"</string>
     <string name="spoken_emoji_1F4A5" msgid="5401995723541239858">"និមិត្ត​សញ្ញា​​ប៉ះ​ទង្គិច​គ្នា"</string>
-    <string name="spoken_emoji_1F4A6" msgid="3837802182716483848">"និមិត្ត​សញ្ញា​​ស្រក់​ញើស​"</string>
+    <string name="spoken_emoji_1F4A6" msgid="3837802182716483848">"និមិត្ត​សញ្ញា​​ស្រក់​ញើស"</string>
     <string name="spoken_emoji_1F4A7" msgid="5718438987757885141">"ដំណក់​ទឹក"</string>
     <string name="spoken_emoji_1F4A8" msgid="4472108229720006377">"និមិត្ត​សញ្ញា​​ដកឃ្លា"</string>
     <string name="spoken_emoji_1F4A9" msgid="1240958472788430032">"គំនរ​ធូលី"</string>
@@ -539,7 +539,7 @@
     <string name="spoken_emoji_1F4B1" msgid="8339494003418572905">"ប្ដូរ​​រូបិយប័ណ្ណ"</string>
     <string name="spoken_emoji_1F4B2" msgid="3179159430187243132">"សញ្ញា​ដុល្លារ"</string>
     <string name="spoken_emoji_1F4B3" msgid="5375412518221759596">"កាត​​ឥណទាន"</string>
-    <string name="spoken_emoji_1F4B4" msgid="1068592463669453204">"ក្រដាស​ប្រាក់​​ធនាគារ​មាន​សញ្ញា​​យ៉េន​​"</string>
+    <string name="spoken_emoji_1F4B4" msgid="1068592463669453204">"ក្រដាស​ប្រាក់​​ធនាគារ​មាន​សញ្ញា​​យ៉េន"</string>
     <string name="spoken_emoji_1F4B5" msgid="1426708699891832564">"លុយដុល្លារ"</string>
     <string name="spoken_emoji_1F4B6" msgid="8289249930736444837">"ក្រដាស​ប្រាក់​​ធនាគារ​មាន​សញ្ញា​អឺរ៉ូ"</string>
     <string name="spoken_emoji_1F4B7" msgid="5245100496860739429">"ក្រដាស​ប្រាក់​​ធនាគារ​មាន​សញ្ញា​​​ផោន"</string>
@@ -547,7 +547,7 @@
     <string name="spoken_emoji_1F4B9" msgid="647509393536679903">"ក្រាហ្វិក​និន្នាការ​ឡើង​​មាន​​សញ្ញា​យ៉េន"</string>
     <string name="spoken_emoji_1F4BA" msgid="1269737854891046321">"កៅអី"</string>
     <string name="spoken_emoji_1F4BB" msgid="6252883563347816451">"កុំព្យូទ័រ​ផ្ទាល់ខ្លួន"</string>
-    <string name="spoken_emoji_1F4BC" msgid="6182597732218446206">"វ៉ា​លី​"</string>
+    <string name="spoken_emoji_1F4BC" msgid="6182597732218446206">"វ៉ា​លី"</string>
     <string name="spoken_emoji_1F4BD" msgid="5820961044768829176">"ឌីស​​តូច"</string>
     <string name="spoken_emoji_1F4BE" msgid="4754542485835379808">"ថា​ស​​ទន់"</string>
     <string name="spoken_emoji_1F4BF" msgid="2237481756984721795">"ថាស"</string>
@@ -557,7 +557,7 @@
     <string name="spoken_emoji_1F4C3" msgid="3727274466173970142">"ទំព័រ​​កោង"</string>
     <string name="spoken_emoji_1F4C4" msgid="4382570710795501612">"ទំព័រ​បញ្ឈរ"</string>
     <string name="spoken_emoji_1F4C5" msgid="8693944622627762487">"ប្រតិទិន"</string>
-    <string name="spoken_emoji_1F4C6" msgid="8469908708708424640">"ហែក​ប្រតិទិន​"</string>
+    <string name="spoken_emoji_1F4C6" msgid="8469908708708424640">"ហែក​ប្រតិទិន"</string>
     <string name="spoken_emoji_1F4C7" msgid="2665313547987324495">"​កាត​រៀប​តាម​អក្សរ"</string>
     <string name="spoken_emoji_1F4C8" msgid="8007686702282833600">"ក្រាហ្វិក​មាន​និន្នាការ​ឡើង"</string>
     <string name="spoken_emoji_1F4C9" msgid="2271951411192893684">"ក្រាហ្វិក​មាន​និន្នាការ​ចុះ"</string>
@@ -573,11 +573,11 @@
     <string name="spoken_emoji_1F4D3" msgid="5873386492793610808">"សៀវភៅ"</string>
     <string name="spoken_emoji_1F4D4" msgid="4754469936418776360">"សៀវភៅ​មាន​ក្រប​ពណ៌"</string>
     <string name="spoken_emoji_1F4D5" msgid="4642713351802778905">"សៀវភៅ​​បិទ"</string>
-    <string name="spoken_emoji_1F4D6" msgid="6987347918381807186">"សៀវភៅ​បើក​​"</string>
+    <string name="spoken_emoji_1F4D6" msgid="6987347918381807186">"សៀវភៅ​បើក"</string>
     <string name="spoken_emoji_1F4D7" msgid="7813394163241379223">"សៀវភៅ​​ពណ៌​បៃតង"</string>
     <string name="spoken_emoji_1F4D8" msgid="7189799718984979521">"សៀវភៅ​​ពណ៌​ខៀវ"</string>
     <string name="spoken_emoji_1F4D9" msgid="3874664073186440225">"សៀវភៅ​​ពណ៌​ទឹកក្រូច"</string>
-    <string name="spoken_emoji_1F4DA" msgid="872212072924287762">"សៀវភៅ​"</string>
+    <string name="spoken_emoji_1F4DA" msgid="872212072924287762">"សៀវភៅ"</string>
     <string name="spoken_emoji_1F4DB" msgid="2015183603583392969">"ស្លាកឈ្មោះ"</string>
     <string name="spoken_emoji_1F4DC" msgid="5075845110932456783">"ក្រដាស​រមូរ"</string>
     <string name="spoken_emoji_1F4DD" msgid="2494006707147586786">"កំណត់ចំណាំ"</string>
@@ -589,7 +589,7 @@
     <string name="spoken_emoji_1F4E3" msgid="5588916572878599224">"ឧបករណ៍​បំពង​សំឡេង"</string>
     <string name="spoken_emoji_1F4E4" msgid="2063561529097749707">"ថា​ស​​​ចេញ"</string>
     <string name="spoken_emoji_1F4E5" msgid="3232462702926143576">"ថា​ស​ចូល"</string>
-    <string name="spoken_emoji_1F4E6" msgid="3399454337197561635">"កញ្ចប់​"</string>
+    <string name="spoken_emoji_1F4E6" msgid="3399454337197561635">"កញ្ចប់"</string>
     <string name="spoken_emoji_1F4E7" msgid="5557136988503873238">"និមិត្ត​សញ្ញា​អ៊ីមែល"</string>
     <string name="spoken_emoji_1F4E8" msgid="30698793974124123">"ស្រោម​​សំបុត្រ​ចូល"</string>
     <string name="spoken_emoji_1F4E9" msgid="5947550337678643166">"ស្រោម​សំបុត្រ​​មាន​សញ្ញា​ព្រួញ​ពី​លើ"</string>
@@ -626,7 +626,7 @@
     <string name="spoken_emoji_1F50C" msgid="7793219132036431680">"ដុយ​អគ្គិសនី"</string>
     <string name="spoken_emoji_1F50D" msgid="8140244710637926780">"កែវ​ពង្រីក​ចង្អុល​ខាង​ឆ្វេង"</string>
     <string name="spoken_emoji_1F50E" msgid="4751821352839693365">"កែវ​ពង្រីក​ចង្អុល​ខាង​ស្ដាំ"</string>
-    <string name="spoken_emoji_1F50F" msgid="915079280472199605">"ចាក់សោ​​​ដោយ​ប្រើ​​ប៊ិច​"</string>
+    <string name="spoken_emoji_1F50F" msgid="915079280472199605">"ចាក់សោ​​​ដោយ​ប្រើ​​ប៊ិច"</string>
     <string name="spoken_emoji_1F510" msgid="7658381761691758318">"បិទ​​សោ​ដោយ​ប្រើ​​​​កូនសោ"</string>
     <string name="spoken_emoji_1F511" msgid="262319867774655688">"សោ"</string>
     <string name="spoken_emoji_1F512" msgid="5628688337255115175">"ចាក់សោ"</string>
@@ -645,15 +645,15 @@
     <string name="spoken_emoji_1F51F" msgid="8673370823728653973">"គ្រាប់​ចុច​ ១០"</string>
     <string name="spoken_emoji_1F520" msgid="7335109890337048900">"និមិត្ត​សញ្ញា​បញ្ចូល​សម្រាប់​អក្សរ​ឡាតាំង​ធំ"</string>
     <string name="spoken_emoji_1F521" msgid="2693185864450925778">"និមិត្ត​សញ្ញា​បញ្ចូល​សម្រាប់​អក្សរ​ឡាតាំង​តូច"</string>
-    <string name="spoken_emoji_1F522" msgid="8419130286280673347">"និមិត្ត​សញ្ញា​បញ្ចូល​សម្រាប់​​លេខ​"</string>
+    <string name="spoken_emoji_1F522" msgid="8419130286280673347">"និមិត្ត​សញ្ញា​បញ្ចូល​សម្រាប់​​លេខ"</string>
     <string name="spoken_emoji_1F523" msgid="3318053476401719421">"ការ​បញ្ចូល​និមិត្តសញ្ញា"</string>
     <string name="spoken_emoji_1F524" msgid="1625073997522316331">"និមិត្ត​សញ្ញា​បញ្ចូល​សម្រាប់​អក្សរ​ឡាតាំង"</string>
     <string name="spoken_emoji_1F525" msgid="4083884189172963790">"ភ្លើង"</string>
     <string name="spoken_emoji_1F526" msgid="2035494936742643580">"ពិល​​អគ្គិសនី"</string>
     <string name="spoken_emoji_1F527" msgid="134257142354034271">"ម៉ាឡេត"</string>
     <string name="spoken_emoji_1F528" msgid="700627429570609375">"ញញួរ"</string>
-    <string name="spoken_emoji_1F529" msgid="7480548235904988573">"ឡោ​ស៊ី​​"</string>
-    <string name="spoken_emoji_1F52A" msgid="7613580031502317893">"កាំបិត​​"</string>
+    <string name="spoken_emoji_1F529" msgid="7480548235904988573">"ឡោ​ស៊ី"</string>
+    <string name="spoken_emoji_1F52A" msgid="7613580031502317893">"កាំបិត"</string>
     <string name="spoken_emoji_1F52B" msgid="4554906608328118613">"កាំភ្លើង​ខ្លី"</string>
     <string name="spoken_emoji_1F52C" msgid="1330294501371770790">"មីក្រូទស្សន៍"</string>
     <string name="spoken_emoji_1F52D" msgid="7549551775445177140">"កែវ​យឹត"</string>
@@ -662,7 +662,7 @@
     <string name="spoken_emoji_1F530" msgid="3572898444281774023">"និមិត្តសញ្ញា​ជប៉ុន​សម្រាប់​អ្នក​ចាប់ផ្ដើម"</string>
     <string name="spoken_emoji_1F531" msgid="5225633376450025396">"លំពែង​មុខ​បី"</string>
     <string name="spoken_emoji_1F532" msgid="9169568490485180779">"ប៊ូតុង​ការេ​ពណ៌​ខ្មៅ"</string>
-    <string name="spoken_emoji_1F533" msgid="6554193837201918598">"ប៊ូតុង​ការ៉េ​ពណ៌​ស​"</string>
+    <string name="spoken_emoji_1F533" msgid="6554193837201918598">"ប៊ូតុង​ការ៉េ​ពណ៌​ស"</string>
     <string name="spoken_emoji_1F534" msgid="8339298801331865340">"រង្វង់​ពណ៌​ក្រហម​​​ធំ"</string>
     <string name="spoken_emoji_1F535" msgid="1227403104835533512">"រង្វង់​ពណ៌​ខៀវ​ធំ"</string>
     <string name="spoken_emoji_1F536" msgid="5477372445510469331">"ពេជ្រ​ពណ៌​ទឹកក្រូច​ធំ"</string>
@@ -745,8 +745,8 @@
     <string name="spoken_emoji_1F628" msgid="8875777401624904224">"មុខ​ភ័យ​ខ្លាច"</string>
     <string name="spoken_emoji_1F629" msgid="1411538490319190118">"មុខ​​នឿយហត់"</string>
     <string name="spoken_emoji_1F62A" msgid="4726686726690289969">"មុខ​​ងងុយ​គេង"</string>
-    <string name="spoken_emoji_1F62B" msgid="3221980473921623613">"មុខ​អស់កម្លាំង​​"</string>
-    <string name="spoken_emoji_1F62C" msgid="4616356691941225182">"មុខ​ក្រញេវក្រញូវ​"</string>
+    <string name="spoken_emoji_1F62B" msgid="3221980473921623613">"មុខ​អស់កម្លាំង"</string>
+    <string name="spoken_emoji_1F62C" msgid="4616356691941225182">"មុខ​ក្រញេវក្រញូវ"</string>
     <string name="spoken_emoji_1F62D" msgid="4283677508698812232">"មុខ​យំ​លឺៗ"</string>
     <string name="spoken_emoji_1F62E" msgid="726083405284353894">"មុខ​បើក​មាត់"</string>
     <string name="spoken_emoji_1F62F" msgid="7746620088234710962">"មុខ​ស្ងៀមស្ងាត់"</string>
@@ -784,7 +784,7 @@
     <string name="spoken_emoji_1F683" msgid="8772750354339223092">"ទូរ​​រថភ្លើង"</string>
     <string name="spoken_emoji_1F684" msgid="346396777356203608">"រថភ្លើង​ល្បឿន​លឿន"</string>
     <string name="spoken_emoji_1F685" msgid="1237059817190832730">"រថភ្លើង​ល្បឿន​លឿន​​មាន​ច្រមុះ"</string>
-    <string name="spoken_emoji_1F686" msgid="3525197227223620343">"រថភ្លើង​"</string>
+    <string name="spoken_emoji_1F686" msgid="3525197227223620343">"រថភ្លើង"</string>
     <string name="spoken_emoji_1F687" msgid="5110143437960392837">"មេត្រូ"</string>
     <string name="spoken_emoji_1F688" msgid="4702085029871797965">"រថភ្លើង​ប្រើ​​​ពន្លឺ"</string>
     <string name="spoken_emoji_1F689" msgid="2375851019798817094">"ស្ថានីយ"</string>
@@ -803,7 +803,7 @@
     <string name="spoken_emoji_1F696" msgid="6391604457418285404">"តាក់ស៊ី​ខាង​មុខ"</string>
     <string name="spoken_emoji_1F697" msgid="7978399334396733790">"រថយន្ត"</string>
     <string name="spoken_emoji_1F698" msgid="7006050861129732018">"រថយន្ត​ខាង​មុខ"</string>
-    <string name="spoken_emoji_1F699" msgid="630317052666590607">"រថយន្ត​​​សម្រាប់​កម្សាន្ត​"</string>
+    <string name="spoken_emoji_1F699" msgid="630317052666590607">"រថយន្ត​​​សម្រាប់​កម្សាន្ត"</string>
     <string name="spoken_emoji_1F69A" msgid="4739797891735823577">"រថយន្ត​​ចែក​ចាយ"</string>
     <string name="spoken_emoji_1F69B" msgid="4715997280786620649">"ឡាន​កាមីយ៉ុង"</string>
     <string name="spoken_emoji_1F69C" msgid="5557395610750818161">"ត្រាក់ទ័រ"</string>
@@ -819,7 +819,7 @@
     <string name="spoken_emoji_1F6A6" msgid="485575967773793454">"ភ្លើង​ចរាចរណ៍​បញ្ឈរ"</string>
     <string name="spoken_emoji_1F6A7" msgid="6411048933816976794">"សញ្ញា​​សំណង់"</string>
     <string name="spoken_emoji_1F6A8" msgid="6345717218374788364">"រថយន្ត​​ប៉ូលិស​បើក​សារ៉ែន​វិល"</string>
-    <string name="spoken_emoji_1F6A9" msgid="6586380356807600412">"បង្គោល​ទង់ជាតិ​រាង​ត្រីកោណ​"</string>
+    <string name="spoken_emoji_1F6A9" msgid="6586380356807600412">"បង្គោល​ទង់ជាតិ​រាង​ត្រីកោណ"</string>
     <string name="spoken_emoji_1F6AA" msgid="8954448167261738885">"ទ្វារ"</string>
     <string name="spoken_emoji_1F6AB" msgid="5313946262888343544">"សញ្ញា​ហាម​ចូល"</string>
     <string name="spoken_emoji_1F6AC" msgid="6946858177965948288">"​សញ្ញា​ជក់​បារី"</string>
diff --git a/java/res/values-km-rKH/strings-talkback-descriptions.xml b/java/res/values-km-rKH/strings-talkback-descriptions.xml
index f907832..29d3b95 100644
--- a/java/res/values-km-rKH/strings-talkback-descriptions.xml
+++ b/java/res/values-km-rKH/strings-talkback-descriptions.xml
@@ -78,7 +78,7 @@
     <string name="spoken_emoji_unknown" msgid="5981009928135394306">"មិន​ស្គាល់​សញ្ញា​អារម្មណ៍"</string>
     <string name="spoken_emoticon_3A_2D_21_20" msgid="2410905667389534573">"មុខ​អផ្សុក"</string>
     <string name="spoken_emoticon_3A_2D_24_20" msgid="2481260475945560438">"មុខ​ខ្មាស​​អៀន"</string>
-    <string name="spoken_emoticon_42_2D_29_20" msgid="1063205250387128068">"ពាក់​វ៉ែនតា​"</string>
+    <string name="spoken_emoticon_42_2D_29_20" msgid="1063205250387128068">"ពាក់​វ៉ែនតា"</string>
     <string name="spoken_emoticon_3A_4F_20" msgid="532695091593447238">"មុខ​ភ្ញាក់ផ្អើល"</string>
     <string name="spoken_emoticon_3A_2D_2A_20" msgid="5612342617244114291">"មុខ​ថើប"</string>
     <string name="spoken_emoticon_3A_2D_5B_20" msgid="2223507987759905920">"មុខ​ចង​ចិញ្ចើម"</string>
diff --git a/java/res/values-km-rKH/strings.xml b/java/res/values-km-rKH/strings.xml
index 8aea853..86fdf29 100644
--- a/java/res/values-km-rKH/strings.xml
+++ b/java/res/values-km-rKH/strings.xml
@@ -39,7 +39,7 @@
     <string name="include_other_imes_in_language_switch_list_summary" msgid="840637129103317635">"គ្រាប់ចុច​ប្ដូរ​ភាសា​តាម​វិធីសាស្ត្រ​បញ្ចូល​ផ្សេងទៀត"</string>
     <string name="show_language_switch_key" msgid="5915478828318774384">"គ្រាប់​ចុច​ប្ដូរ​​ភាសា"</string>
     <string name="show_language_switch_key_summary" msgid="7343403647474265713">"បង្ហាញ​នៅ​ពេល​ដែល​បើក​ភាសា​បញ្ចូល​ច្រើន"</string>
-    <string name="key_preview_popup_dismiss_delay" msgid="6213164897443068248">"​សោ​លេចឡើង​បោះបង់​ការ​​ពន្យារពេល​"</string>
+    <string name="key_preview_popup_dismiss_delay" msgid="6213164897443068248">"​សោ​លេចឡើង​បោះបង់​ការ​​ពន្យារពេល"</string>
     <string name="key_preview_popup_dismiss_no_delay" msgid="2096123151571458064">"គ្មាន​ការ​ពន្យារពេល"</string>
     <string name="key_preview_popup_dismiss_default_delay" msgid="2166964333903906734">"លំនាំដើម"</string>
     <string name="abbreviation_unit_milliseconds" msgid="8700286094028323363">"<xliff:g id="MILLISECONDS">%s</xliff:g> មិល្លី​វិនាទី"</string>
@@ -50,7 +50,7 @@
     <string name="enable_metrics_logging" msgid="5506372337118822837">"ធ្វើឲ្យ <xliff:g id="APPLICATION_NAME">%s</xliff:g> ប្រសើរ​ឡើង"</string>
     <string name="use_double_space_period" msgid="8781529969425082860">"រយៈ​ពេល​ចុច​ដកឃ្លា​ពីរដង"</string>
     <string name="use_double_space_period_summary" msgid="6532892187247952799">"ប៉ះ​ដកឃ្លា​ពីរ​​ដង​បញ្ចូល​​​រយៈ​ពេល​ដែល​អនុវត្ត​តាម​ដកឃ្លា"</string>
-    <string name="auto_cap" msgid="1719746674854628252">"ការ​សរសេរ​ជា​អក្សរ​ធំ​​ស្វ័យប្រវត្តិ​"</string>
+    <string name="auto_cap" msgid="1719746674854628252">"ការ​សរសេរ​ជា​អក្សរ​ធំ​​ស្វ័យប្រវត្តិ"</string>
     <string name="auto_cap_summary" msgid="7934452761022946874">"សរសេរ​ពាក្យ​ដំបូង​​​ជា​អក្សរ​ធំ​​នៃ​ប្រយោគ​នីមួយ​ៗ"</string>
     <string name="edit_personal_dictionary" msgid="3996910038952940420">"វចនានុក្រម​ផ្ទាល់ខ្លួន"</string>
     <string name="configure_dictionaries_title" msgid="4238652338556902049">"ផ្នែក​បន្ថែម​វចនានុក្រម"</string>
@@ -58,7 +58,7 @@
     <string name="prefs_show_suggestions" msgid="8026799663445531637">"បង្ហាញ​ការ​ស្នើ​​កែ"</string>
     <string name="prefs_show_suggestions_summary" msgid="1583132279498502825">"បង្ហាញ​ពាក្យ​​បាន​​ផ្ដល់​​ស្នើ​​ខណៈ​ពេល​​​វាយ​បញ្ចូល"</string>
     <string name="prefs_block_potentially_offensive_title" msgid="5078480071057408934">"ទប់ស្កាត់​​ពាក្យ​​បំពាន"</string>
-    <string name="prefs_block_potentially_offensive_summary" msgid="2371835479734991364">"កុំ​ស្នើ​ឲ្យ​ពាក្យ​បំពាន​មាន​សក្ដានុពល​"</string>
+    <string name="prefs_block_potentially_offensive_summary" msgid="2371835479734991364">"កុំ​ស្នើ​ឲ្យ​ពាក្យ​បំពាន​មាន​សក្ដានុពល"</string>
     <string name="auto_correction" msgid="7630720885194996950">"ការ​កែ​​​ស្វ័យប្រវត្តិ"</string>
     <string name="auto_correction_summary" msgid="5625751551134658006">"ចន្លោះ​មិន​ឃើញ ​និង​សញ្ញា​​វណ្ណយុត្ត​កែ​ពាក្យ​ដែល​បាន​វាយ​ខុស​ស្វ័យប្រវត្តិ"</string>
     <string name="auto_correction_threshold_mode_off" msgid="8470882665417944026">"បិទ"</string>
@@ -149,7 +149,7 @@
     <string name="dictionary_provider_name" msgid="3027315045397363079">"កម្មវិធី​ផ្ដល់​វចនានុក្រម"</string>
     <string name="dictionary_service_name" msgid="6237472350693511448">"សេវាកម្ម​​វចនានុក្រម"</string>
     <string name="download_description" msgid="6014835283119198591">"ព័ត៌មាន​បច្ចុប្បន្នភាព​វចនានុក្រម"</string>
-    <string name="dictionary_settings_title" msgid="8091417676045693313">"ផ្នែក​បន្ថែម​វចនានុក្រម​​"</string>
+    <string name="dictionary_settings_title" msgid="8091417676045693313">"ផ្នែក​បន្ថែម​វចនានុក្រម"</string>
     <string name="dictionary_install_over_metered_network_prompt" msgid="3587517870006332980">"វចនានុក្រម​​​​​អាច​ប្រើ​បាន"</string>
     <string name="dictionary_settings_summary" msgid="5305694987799824349">"ការ​កំណត់​សម្រាប់​វចនានុក្រម"</string>
     <string name="user_dictionaries" msgid="3582332055892252845">"វចនានុក្រម​​​អ្នក​ប្រើ"</string>
@@ -165,10 +165,10 @@
     <string name="message_updating" msgid="4457761393932375219">"ពិនិត្យមើល​បច្ចុប្បន្នភាព"</string>
     <string name="message_loading" msgid="5638680861387748936">"កំពុង​ផ្ទុក..."</string>
     <string name="main_dict_description" msgid="3072821352793492143">"វចនានុក្រម​ចម្បង"</string>
-    <string name="cancel" msgid="6830980399865683324">"បោះ​បង់​"</string>
+    <string name="cancel" msgid="6830980399865683324">"បោះ​បង់"</string>
     <string name="go_to_settings" msgid="3876892339342569259">"ការ​កំណត់"</string>
     <string name="install_dict" msgid="180852772562189365">"ដំឡើង"</string>
-    <string name="cancel_download_dict" msgid="7843340278507019303">"បោះ​បង់​"</string>
+    <string name="cancel_download_dict" msgid="7843340278507019303">"បោះ​បង់"</string>
     <string name="delete_dict" msgid="756853268088330054">"លុប"</string>
     <string name="should_download_over_metered_prompt" msgid="1583881200688185508">"ភាសា​ដែល​បាន​ជ្រើស​នៅ​លើ​ឧបករណ៍​ចល័ត​មាន​វចនានុក្រម​អាច​ប្រើ​បាន។&lt;br/&gt; យើង​ផ្ដល់​អនុសាសន៍​ឲ្យ &lt;b&gt;ទាញ​យក&lt;/b&gt; វចនានុក្រម​ភាសា <xliff:g id="LANGUAGE_NAME">%1$s</xliff:g> ដើម្បី​បង្កើន​បទពិសោធន៍​វាយ​បញ្ចូល​របស់​អ្នក។&lt;br/&gt; &lt;br/&gt; ការ​ទាញ​យក​អាច​ចំណាយ​ពេល​ប្រហែល​ពីរ​នាទី​នៅ​តាម 3G។ ការ​គិត​ថ្លៃ​អាច​អនុវត្ត​ប្រសិន​បើ​អ្នក​មិន​ប្រើ &lt;b&gt;ផែនការ​ទិន្នន័យ​គ្មាន​ដែន​កំណត់&lt;/b&gt;.&lt;br/&gt; បើ​អ្នក​មិន​ប្រាកដ​​ថា​ផែនការ​ណា​មួយ​ដែល​អ្នក​មាន យើង​ផ្ដល់​អនុសាសន៍​ឲ្យ​​ភ្ជាប់​វ៉ាយហ្វាយ ដើម្បី​ចាប់ផ្ដើម​ទាញ​យក​ដោយ​ស្វ័យ​ប្រវត្តិ។&lt;br/&gt; &lt;br/&gt; ជំនួយ៖ អ្នក​អាច​ទាញ​យក និង​លុប​វចនានុក្រម​ដោយ​ចូល​ទៅ​ &lt;b&gt;ភាសា &amp; ការ​បញ្ចូល&lt;/b&gt; នៅ​ក្នុង​ម៉ឺនុយ &lt;b&gt;ការ​កំណត់&lt;/b&gt; សម្រាប់​ឧបករណ៍​ចល័ត។"</string>
     <string name="download_over_metered" msgid="1643065851159409546">"ទាញ​យក​ឥឡូវ​នេះ (<xliff:g id="SIZE_IN_MEGABYTES">%1$.1f</xliff:g> មេកាបៃ)"</string>
@@ -186,7 +186,7 @@
     <string name="user_dict_settings_add_word_option_name" msgid="6665558053408962865">"ពាក្យ៖"</string>
     <string name="user_dict_settings_add_shortcut_option_name" msgid="3094731590655523777">"ផ្លូវកាត់​៖"</string>
     <string name="user_dict_settings_add_locale_option_name" msgid="4738643440987277705">"ភាសា៖"</string>
-    <string name="user_dict_settings_add_word_hint" msgid="4902434148985906707">"វាយ​បញ្ចូល​ពាក្យ​"</string>
+    <string name="user_dict_settings_add_word_hint" msgid="4902434148985906707">"វាយ​បញ្ចូល​ពាក្យ"</string>
     <string name="user_dict_settings_add_shortcut_hint" msgid="2265453012555060178">"ផ្លូវកាត់​ជា​ជម្រើស"</string>
     <string name="user_dict_settings_edit_dialog_title" msgid="3765774633869590352">"កែ​ពាក្យ"</string>
     <string name="user_dict_settings_context_menu_edit_title" msgid="6812255903472456302">"កែ"</string>
diff --git a/java/res/values-lo-rLA/strings-emoji-descriptions.xml b/java/res/values-lo-rLA/strings-emoji-descriptions.xml
index 335fc19..61845da 100644
--- a/java/res/values-lo-rLA/strings-emoji-descriptions.xml
+++ b/java/res/values-lo-rLA/strings-emoji-descriptions.xml
@@ -210,7 +210,7 @@
     <string name="spoken_emoji_1F330" msgid="3115760035618051575">"​ລູກ​ເກົາ​ລັດ"</string>
     <string name="spoken_emoji_1F331" msgid="5658888205290008691">"​ກ້າ​ໄມ້"</string>
     <string name="spoken_emoji_1F332" msgid="2935650450421165938">"ຕົ້ນ​ໄມ້​ບໍ່​ຜັດ​ໃບ"</string>
-    <string name="spoken_emoji_1F333" msgid="5898847427062482675">"​ຕົ້ນ​ໄມ້​ຜັດໃບ​"</string>
+    <string name="spoken_emoji_1F333" msgid="5898847427062482675">"​ຕົ້ນ​ໄມ້​ຜັດໃບ"</string>
     <string name="spoken_emoji_1F334" msgid="6183375224678417894">"​ຕົ້ນ​ປາມ"</string>
     <string name="spoken_emoji_1F335" msgid="5352418412103584941">"​ກະ​ບອງ​ເພັດ"</string>
     <string name="spoken_emoji_1F337" msgid="3839107352363566289">"​ທິວ​ລິບ"</string>
@@ -450,7 +450,7 @@
     <string name="spoken_emoji_1F458" msgid="5704243858031107692">"​ກິ​ໂມ​ໂນ"</string>
     <string name="spoken_emoji_1F459" msgid="3553148747050035251">"​ບິ​ກີ​ນີ"</string>
     <string name="spoken_emoji_1F45A" msgid="1389654639484716101">"​ເສື້ອ​ຜ້າ​ຜູ່​ຍິງ"</string>
-    <string name="spoken_emoji_1F45B" msgid="1113293170254222904">"ກະ​ເປົາ​"</string>
+    <string name="spoken_emoji_1F45B" msgid="1113293170254222904">"ກະ​ເປົາ"</string>
     <string name="spoken_emoji_1F45C" msgid="3410257778598006936">"ກະ​ເປົາ"</string>
     <string name="spoken_emoji_1F45D" msgid="812176504300064819">"​ກະ​ເປົາ"</string>
     <string name="spoken_emoji_1F45E" msgid="2901741399934723562">"​ເກີບ​ຜູ່​ຊາຍ"</string>
diff --git a/java/res/values-lo-rLA/strings-letter-descriptions.xml b/java/res/values-lo-rLA/strings-letter-descriptions.xml
index ecc0b7a..47f7cbc 100644
--- a/java/res/values-lo-rLA/strings-letter-descriptions.xml
+++ b/java/res/values-lo-rLA/strings-letter-descriptions.xml
@@ -186,7 +186,7 @@
     <string name="spoken_symbol_2019" msgid="8892530161598134083">"Right single quotation mark"</string>
     <string name="spoken_symbol_201A" msgid="2072987157683446644">"Single low-9 quotation mark"</string>
     <string name="spoken_symbol_201C" msgid="4588048378803665427">"​ເຄື່ອງ​ໝາຍ​ວົງ​ຢືມ​ຊ້າຍ"</string>
-    <string name="spoken_symbol_201D" msgid="1642776849495925895">"​ເຄື່ອງ​ໝາຍ​ວົງ​ຢືມ​ຂວາ​"</string>
+    <string name="spoken_symbol_201D" msgid="1642776849495925895">"​ເຄື່ອງ​ໝາຍ​ວົງ​ຢືມ​ຂວາ"</string>
     <string name="spoken_symbol_2020" msgid="9084628638189344431">"Dagger"</string>
     <string name="spoken_symbol_2021" msgid="5081396468559426475">"Double dagger"</string>
     <string name="spoken_symbol_2030" msgid="9068837172419431755">"​ເຄື່ອງ​ໝາຍ​ຕໍ່​ໄມລ໌"</string>
diff --git a/java/res/values-ml-rIN/strings-emoji-descriptions.xml b/java/res/values-ml-rIN/strings-emoji-descriptions.xml
index a846f31..2fad192 100644
--- a/java/res/values-ml-rIN/strings-emoji-descriptions.xml
+++ b/java/res/values-ml-rIN/strings-emoji-descriptions.xml
@@ -348,7 +348,7 @@
     <string name="spoken_emoji_1F3E0" msgid="6277213201655811842">"വീടുനിർമ്മാണം"</string>
     <string name="spoken_emoji_1F3E1" msgid="233476176077538885">"പൂന്തോട്ടനുള്ള വീട്"</string>
     <string name="spoken_emoji_1F3E2" msgid="919736380093964570">"ഓഫീസ് കെട്ടിടം"</string>
-    <string name="spoken_emoji_1F3E3" msgid="6177606081825094184">"ജപ്പാനീസ് പോസ്‌റ്റ് ഓഫീസ്"</string>
+    <string name="spoken_emoji_1F3E3" msgid="6177606081825094184">"ജാപ്പനീസ്‌ പോസ്‌റ്റ് ഓഫീസ്"</string>
     <string name="spoken_emoji_1F3E4" msgid="717377871070970293">"യൂറോപ്പ്യൻ പോസ്‌റ്റ് ഓഫീസ്"</string>
     <string name="spoken_emoji_1F3E5" msgid="1350532500431776780">"ആശുപത്രി"</string>
     <string name="spoken_emoji_1F3E6" msgid="342132788513806214">"ബാങ്ക്"</string>
@@ -659,7 +659,7 @@
     <string name="spoken_emoji_1F52D" msgid="7549551775445177140">"ടെലിസ്‌കോപ്പ്"</string>
     <string name="spoken_emoji_1F52E" msgid="4457099417872625141">"സ്ഥടിക ബോൾ"</string>
     <string name="spoken_emoji_1F52F" msgid="8899031001317442792">"മധ്യഭാഗത്ത് കറുത്ത ഡോട്ടുള്ള ആറ് പോയിന്റുള്ള നക്ഷത്രം"</string>
-    <string name="spoken_emoji_1F530" msgid="3572898444281774023">"തുടക്കക്കാർക്കുള്ള ജപ്പാനീസ് ചിഹ്നം"</string>
+    <string name="spoken_emoji_1F530" msgid="3572898444281774023">"തുടക്കക്കാർക്കുള്ള ജാപ്പനീസ്‌ ചിഹ്നം"</string>
     <string name="spoken_emoji_1F531" msgid="5225633376450025396">"ത്രിശൂലത്തിന്റെ ചിഹ്നം"</string>
     <string name="spoken_emoji_1F532" msgid="9169568490485180779">"കറുത്ത ചതുര ബട്ടൺ"</string>
     <string name="spoken_emoji_1F533" msgid="6554193837201918598">"വെള്ള ചതുര ബട്ടൺ"</string>
diff --git a/java/res/values-mn-rMN/strings-emoji-descriptions.xml b/java/res/values-mn-rMN/strings-emoji-descriptions.xml
index f09a51d..5014b2f 100644
--- a/java/res/values-mn-rMN/strings-emoji-descriptions.xml
+++ b/java/res/values-mn-rMN/strings-emoji-descriptions.xml
@@ -267,7 +267,7 @@
     <string name="spoken_emoji_1F36A" msgid="2726271795913042295">"Печень"</string>
     <string name="spoken_emoji_1F36B" msgid="6342163604299875931">"Шоколад"</string>
     <string name="spoken_emoji_1F36C" msgid="2168934753998218790">"Чихэр"</string>
-    <string name="spoken_emoji_1F36D" msgid="3671507903799975792">"Иштэй чихэр"</string>
+    <string name="spoken_emoji_1F36D" msgid="3671507903799975792">"Lollipop"</string>
     <string name="spoken_emoji_1F36E" msgid="4630541402785165902">"Шар тос"</string>
     <string name="spoken_emoji_1F36F" msgid="5577915387425169439">"Зөгийн бал"</string>
     <string name="spoken_emoji_1F370" msgid="7243244547866114951">"Үелсэн бялуу"</string>
diff --git a/java/res/values-my-rMM/strings-action-keys.xml b/java/res/values-my-rMM/strings-action-keys.xml
index d15c9e5..f7a2ca9 100644
--- a/java/res/values-my-rMM/strings-action-keys.xml
+++ b/java/res/values-my-rMM/strings-action-keys.xml
@@ -27,5 +27,5 @@
     <string name="label_send_key" msgid="482252074224462163">"ပို့ရန်"</string>
     <string name="label_search_key" msgid="7965186050435796642">"ရှာဖွေရန်"</string>
     <string name="label_pause_key" msgid="2225922926459730642">"ဆိုင်းငံ့ရန်"</string>
-    <string name="label_wait_key" msgid="5891247853595466039">"စောင့်ဆိုင်းရန်"</string>
+    <string name="label_wait_key" msgid="5891247853595466039">"စောင့်ဆိုင်းရန်"</string>
 </resources>
diff --git a/java/res/values-my-rMM/strings-letter-descriptions.xml b/java/res/values-my-rMM/strings-letter-descriptions.xml
index 2d5338b..d904f53 100644
--- a/java/res/values-my-rMM/strings-letter-descriptions.xml
+++ b/java/res/values-my-rMM/strings-letter-descriptions.xml
@@ -29,7 +29,7 @@
     <string name="spoken_accented_letter_00AA" msgid="4374325261868451570">"ဣထိလိင် အစဉ်ပြ အညွှန်း"</string>
     <string name="spoken_accented_letter_00B5" msgid="9031387673828823891">"မိုက်ခရို သင်္ကေတ"</string>
     <string name="spoken_accented_letter_00BA" msgid="5045198452071207437">"ပုလိင် အစဉ်ပြ အညွှန်း"</string>
-    <string name="spoken_accented_letter_00DF" msgid="2260098367028134281">"ပြတ်သားသည့် S"</string>
+    <string name="spoken_accented_letter_00DF" msgid="2260098367028134281">"ပြတ်သားသည့် S"</string>
     <string name="spoken_accented_letter_00E0" msgid="2234515772182387086">"A၊ တည်ငြိမ်သော"</string>
     <string name="spoken_accented_letter_00E1" msgid="7780174500802535063">"A၊ စူးရှသော"</string>
     <string name="spoken_accented_letter_00E2" msgid="7054108480488102631">"A၊ သရသံသင်္ကေတ"</string>
@@ -133,45 +133,45 @@
     <string name="spoken_accented_letter_0259" msgid="2464085263158415898">"Schwa"</string>
     <string name="spoken_accented_letter_1EA1" msgid="688124877202887630">"A၊ အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1EA3" msgid="327960130366386256">"A အပေါ်မှာ ချိတ်"</string>
-    <string name="spoken_accented_letter_1EA5" msgid="637406363453769610">"A၊ သရသံသင်္ကေတ နှင့် စူးရှသော"</string>
-    <string name="spoken_accented_letter_1EA7" msgid="1419591804181615409">"A၊ သရသံသင်္ကေတ နှင့် တည်ငြိမ်သော"</string>
-    <string name="spoken_accented_letter_1EA9" msgid="6068887382734896756">"A၊ သရသံသင်္ကေတ နှင့် အပေါ်မှာ ချိတ်"</string>
-    <string name="spoken_accented_letter_1EAB" msgid="7236523151662538333">"A၊ သရသံသင်္ကေတ နှင့် tilde"</string>
-    <string name="spoken_accented_letter_1EAD" msgid="2363364864106332076">"A,၊ သရသံသင်္ကေတ နှင့် အောက်မှာ အစက်"</string>
-    <string name="spoken_accented_letter_1EAF" msgid="1576329511464272935">"A၊ တည်ငြိမ်သော နှင့် စူးရှသော"</string>
-    <string name="spoken_accented_letter_1EB1" msgid="4634735072816076592">"A၊ breve နှင့် တည်ငြိမ်သော"</string>
-    <string name="spoken_accented_letter_1EB3" msgid="2325245849038771534">"A၊ breve နှင့် အပေါ်မှာ ချိတ်"</string>
-    <string name="spoken_accented_letter_1EB5" msgid="3720427596242746295">"A၊ breve နှင့် tilde"</string>
-    <string name="spoken_accented_letter_1EB7" msgid="700415535653646695">"A၊ breve နှင့် အောက်မှာ အစက်"</string>
+    <string name="spoken_accented_letter_1EA5" msgid="637406363453769610">"A၊ သရသံသင်္ကေတ နှင့် စူးရှသော"</string>
+    <string name="spoken_accented_letter_1EA7" msgid="1419591804181615409">"A၊ သရသံသင်္ကေတ နှင့် တည်ငြိမ်သော"</string>
+    <string name="spoken_accented_letter_1EA9" msgid="6068887382734896756">"A၊ သရသံသင်္ကေတ နှင့် အပေါ်မှာ ချိတ်"</string>
+    <string name="spoken_accented_letter_1EAB" msgid="7236523151662538333">"A၊ သရသံသင်္ကေတ နှင့် tilde"</string>
+    <string name="spoken_accented_letter_1EAD" msgid="2363364864106332076">"A,၊ သရသံသင်္ကေတ နှင့် အောက်မှာ အစက်"</string>
+    <string name="spoken_accented_letter_1EAF" msgid="1576329511464272935">"A၊ တည်ငြိမ်သော နှင့် စူးရှသော"</string>
+    <string name="spoken_accented_letter_1EB1" msgid="4634735072816076592">"A၊ breve နှင့် တည်ငြိမ်သော"</string>
+    <string name="spoken_accented_letter_1EB3" msgid="2325245849038771534">"A၊ breve နှင့် အပေါ်မှာ ချိတ်"</string>
+    <string name="spoken_accented_letter_1EB5" msgid="3720427596242746295">"A၊ breve နှင့် tilde"</string>
+    <string name="spoken_accented_letter_1EB7" msgid="700415535653646695">"A၊ breve နှင့် အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1EB9" msgid="3901338692305890487">"E၊ အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1EBB" msgid="4028688699415417302">"E၊ အပေါ်မှာ ချိတ်"</string>
     <string name="spoken_accented_letter_1EBD" msgid="181253633045931897">"E၊ tilde"</string>
-    <string name="spoken_accented_letter_1EBF" msgid="3309618845007944963">"E၊ သရသံသင်္ကေတ နှင့် စူးရှသော"</string>
-    <string name="spoken_accented_letter_1EC1" msgid="8139046749226332542">"E၊ သရသံသင်္ကေတ နှင့် တည်ငြိမ်သော"</string>
-    <string name="spoken_accented_letter_1EC3" msgid="3239674223053133383">"E၊ သရသံသင်္ကေတ နှင့် ချိတ် အပေါ်မှာ"</string>
-    <string name="spoken_accented_letter_1EC5" msgid="2216559244705714587">"E၊ သရသံသင်္ကေတ နှင့် tilde"</string>
-    <string name="spoken_accented_letter_1EC7" msgid="9012731468253986792">"E၊ သရသံသင်္ကေတ နှင့် အောက်မှာ အစက်"</string>
+    <string name="spoken_accented_letter_1EBF" msgid="3309618845007944963">"E၊ သရသံသင်္ကေတ နှင့် စူးရှသော"</string>
+    <string name="spoken_accented_letter_1EC1" msgid="8139046749226332542">"E၊ သရသံသင်္ကေတ နှင့် တည်ငြိမ်သော"</string>
+    <string name="spoken_accented_letter_1EC3" msgid="3239674223053133383">"E၊ သရသံသင်္ကေတ နှင့် ချိတ် အပေါ်မှာ"</string>
+    <string name="spoken_accented_letter_1EC5" msgid="2216559244705714587">"E၊ သရသံသင်္ကေတ နှင့် tilde"</string>
+    <string name="spoken_accented_letter_1EC7" msgid="9012731468253986792">"E၊ သရသံသင်္ကေတ နှင့် အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1EC9" msgid="2901917620195717034">"I၊ အပေါ်မှာ ချိတ်"</string>
     <string name="spoken_accented_letter_1ECB" msgid="5470387489820034621">"I၊ အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1ECD" msgid="1340122876914839806">"O၊ အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1ECF" msgid="2326921263882559755">"O၊ အပေါ်မှာ ချိတ်"</string>
-    <string name="spoken_accented_letter_1ED1" msgid="2885683296042774958">"O၊ သရသံသင်္ကေတ နှင့် စူးရှသော"</string>
-    <string name="spoken_accented_letter_1ED3" msgid="6857664926477376178">"O၊ သရသံသင်္ကေတ နှင့် တည်ငြိမ်သော"</string>
-    <string name="spoken_accented_letter_1ED5" msgid="2015209467290624062">"O၊ သရသံသင်္ကေတ နှင့် အပေါ်မှာ ချိတ်"</string>
-    <string name="spoken_accented_letter_1ED7" msgid="7924481354960306389">"O၊ သရသံသင်္ကေတ နှင့် tilde"</string>
-    <string name="spoken_accented_letter_1ED9" msgid="7023315590332365554">"O၊ သရသံသင်္ကေတ နှင့် အောက်မှာ အစက်"</string>
-    <string name="spoken_accented_letter_1EDB" msgid="2379438944917634496">"O၊ horn နှင့် စူးရှသော"</string>
-    <string name="spoken_accented_letter_1EDD" msgid="8107077534204404085">"O၊ horn နှင့် တည်ငြိမ်သော"</string>
-    <string name="spoken_accented_letter_1EDF" msgid="1846880105528347966">"O၊ horn နှင့် အပေါ်မှာ ချိတ်"</string>
-    <string name="spoken_accented_letter_1EE1" msgid="1520037313389776718">"O၊ horn နှင့် tilde"</string>
-    <string name="spoken_accented_letter_1EE3" msgid="907964027171008963">"O၊ horn နှင့် အောက်မှာ အစက်"</string>
+    <string name="spoken_accented_letter_1ED1" msgid="2885683296042774958">"O၊ သရသံသင်္ကေတ နှင့် စူးရှသော"</string>
+    <string name="spoken_accented_letter_1ED3" msgid="6857664926477376178">"O၊ သရသံသင်္ကေတ နှင့် တည်ငြိမ်သော"</string>
+    <string name="spoken_accented_letter_1ED5" msgid="2015209467290624062">"O၊ သရသံသင်္ကေတ နှင့် အပေါ်မှာ ချိတ်"</string>
+    <string name="spoken_accented_letter_1ED7" msgid="7924481354960306389">"O၊ သရသံသင်္ကေတ နှင့် tilde"</string>
+    <string name="spoken_accented_letter_1ED9" msgid="7023315590332365554">"O၊ သရသံသင်္ကေတ နှင့် အောက်မှာ အစက်"</string>
+    <string name="spoken_accented_letter_1EDB" msgid="2379438944917634496">"O၊ horn နှင့် စူးရှသော"</string>
+    <string name="spoken_accented_letter_1EDD" msgid="8107077534204404085">"O၊ horn နှင့် တည်ငြိမ်သော"</string>
+    <string name="spoken_accented_letter_1EDF" msgid="1846880105528347966">"O၊ horn နှင့် အပေါ်မှာ ချိတ်"</string>
+    <string name="spoken_accented_letter_1EE1" msgid="1520037313389776718">"O၊ horn နှင့် tilde"</string>
+    <string name="spoken_accented_letter_1EE3" msgid="907964027171008963">"O၊ horn နှင့် အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1EE5" msgid="1522024630360038700">"U၊ အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1EE7" msgid="7815412228302952637">"U၊ အပေါ်မှာ ချိတ်"</string>
-    <string name="spoken_accented_letter_1EE9" msgid="4219119671251485651">"U၊ horn နှင့် စူးရှသော"</string>
-    <string name="spoken_accented_letter_1EEB" msgid="4086009841269002231">"U၊ horn နှင့် တည်ငြိမ်သော"</string>
-    <string name="spoken_accented_letter_1EED" msgid="3528151733528719847">"U၊  horn နှင့် အပေါ်မှာ ချိတ်"</string>
-    <string name="spoken_accented_letter_1EEF" msgid="3508548229409072119">"U၊ horn နှင့် tilde"</string>
-    <string name="spoken_accented_letter_1EF1" msgid="1912816350401931115">"U၊ horn နှင့် အောက်မှာ အစက်"</string>
+    <string name="spoken_accented_letter_1EE9" msgid="4219119671251485651">"U၊ horn နှင့် စူးရှသော"</string>
+    <string name="spoken_accented_letter_1EEB" msgid="4086009841269002231">"U၊ horn နှင့် တည်ငြိမ်သော"</string>
+    <string name="spoken_accented_letter_1EED" msgid="3528151733528719847">"U၊  horn နှင့် အပေါ်မှာ ချိတ်"</string>
+    <string name="spoken_accented_letter_1EEF" msgid="3508548229409072119">"U၊ horn နှင့် tilde"</string>
+    <string name="spoken_accented_letter_1EF1" msgid="1912816350401931115">"U၊ horn နှင့် အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1EF3" msgid="7211760439435946494">"Y၊ တည်ငြိမ်သော"</string>
     <string name="spoken_accented_letter_1EF5" msgid="8998864482764007384">"Y၊ အောက်မှာ အစက်"</string>
     <string name="spoken_accented_letter_1EF7" msgid="922043627252869200">"Y၊ အပေါ်မှာ ချိတ်"</string>
diff --git a/java/res/values-my-rMM/strings-talkback-descriptions.xml b/java/res/values-my-rMM/strings-talkback-descriptions.xml
index 08fd190..a4f84a2 100644
--- a/java/res/values-my-rMM/strings-talkback-descriptions.xml
+++ b/java/res/values-my-rMM/strings-talkback-descriptions.xml
@@ -79,7 +79,7 @@
     <string name="spoken_emoticon_3A_2D_21_20" msgid="2410905667389534573">"စိတ်ကုန်နေသော မျက်နှာ"</string>
     <string name="spoken_emoticon_3A_2D_24_20" msgid="2481260475945560438">"ကသိကအောက် မျက်နှာ"</string>
     <string name="spoken_emoticon_42_2D_29_20" msgid="1063205250387128068">"နေကာမျက်မှန်တပ် မျက်နှာ"</string>
-    <string name="spoken_emoticon_3A_4F_20" msgid="532695091593447238">"အံ့အားသင့်နေသော မျက်နှာ"</string>
+    <string name="spoken_emoticon_3A_4F_20" msgid="532695091593447238">"အံ့အားသင့်နေသော မျက်နှာ"</string>
     <string name="spoken_emoticon_3A_2D_2A_20" msgid="5612342617244114291">"နမ်းနေသော မျက်နှာ"</string>
     <string name="spoken_emoticon_3A_2D_5B_20" msgid="2223507987759905920">"မှုန်ကုပ်ကုပ် မျက်နှာ"</string>
     <string name="spoken_open_more_keys_keyboard" msgid="6832897688371903747">"အစားထိုးစရာ စာလုံးများ ရှိနိုင်"</string>
diff --git a/java/res/values-my-rMM/strings.xml b/java/res/values-my-rMM/strings.xml
index ab40378..2810046 100644
--- a/java/res/values-my-rMM/strings.xml
+++ b/java/res/values-my-rMM/strings.xml
@@ -30,11 +30,11 @@
     <string name="settings_screen_accounts" msgid="7570397912370223287">"အကောင့်များ &amp; ကိုယ်ပိုင်ကိစ္စ"</string>
     <string name="settings_screen_appearance" msgid="9153102634339912029">"အပြင်ပန်း &amp; အပြင်အဆင်များ"</string>
     <string name="settings_screen_multilingual" msgid="1391301621464509659">"ဘာသာစကားစုံ ရွေးချယ်စရာများ"</string>
-    <string name="settings_screen_gesture" msgid="8826372746901183556">"လှုပ်ရှားမှုဖြင့်စာရိုက်ခြင်း"</string>
+    <string name="settings_screen_gesture" msgid="8826372746901183556">"လှုပ်ရှားမှုဖြင့်စာရိုက်ခြင်း"</string>
     <string name="settings_screen_correction" msgid="1616818407747682955">"စာအမှားပြပြင်ခြင်း"</string>
-    <string name="settings_screen_advanced" msgid="7472408607625972994">"အဆင့်မြင့်"</string>
+    <string name="settings_screen_advanced" msgid="7472408607625972994">"အဆင့်မြင့်"</string>
     <string name="settings_screen_theme" msgid="2137262503543943871">"အပြင်အဆင်"</string>
-    <string name="enable_split_keyboard" msgid="4177264923999493614">"ကီးဘုတ် ခွဲခြမ်းမှု ဖွင့်ထားရန်"</string>
+    <string name="enable_split_keyboard" msgid="4177264923999493614">"ကီးဘုတ် ခွဲခြမ်းမှု ဖွင့်ထားရန်"</string>
     <string name="include_other_imes_in_language_switch_list" msgid="4533689960308565519">"အခြားထည့်သွင်းမည့် နည်းလမ်းများသို့ ပြောင်းရန်"</string>
     <string name="include_other_imes_in_language_switch_list_summary" msgid="840637129103317635">"ဘာသာပြောင်းသည့် ကီးသည် အခြားထည့်သွင်းရန် နည်းလမ်းများလည်း ပါဝင်သည်"</string>
     <string name="show_language_switch_key" msgid="5915478828318774384">"ဘာသာစကား ပြောင်းခလုတ်"</string>
@@ -47,7 +47,7 @@
     <string name="use_contacts_dict" msgid="4435317977804180815">"အဆယ်ကသွယ်အမည်များ အကြံပြုမည်"</string>
     <string name="use_contacts_dict_summary" msgid="6599983334507879959">"အကြံပြုချက်များနှင့် အမှားပြင်ခြင်းများအတွက် အဆက်သွယ်မှ အမည်များ အသုံးပြုမည်"</string>
     <string name="use_personalized_dicts" msgid="5167396352105467626">"ကိုယ်ရေးကိုယ်တာ အကြံပြုချက်များ"</string>
-    <string name="enable_metrics_logging" msgid="5506372337118822837">"မြှင့်တင်ပါ <xliff:g id="APPLICATION_NAME">%s</xliff:g>"</string>
+    <string name="enable_metrics_logging" msgid="5506372337118822837">"မြှင့်တင်ပါ <xliff:g id="APPLICATION_NAME">%s</xliff:g>"</string>
     <string name="use_double_space_period" msgid="8781529969425082860">"နှစ်နေရာခြား အဆုံးသတ်"</string>
     <string name="use_double_space_period_summary" msgid="6532892187247952799">"အချိန်ကာလ"</string>
     <string name="auto_cap" msgid="1719746674854628252">"အော်တိုစာလုံးကြီးပြောင်း"</string>
@@ -84,7 +84,7 @@
     <string name="hint_add_to_dictionary_without_word" msgid="3040385779511255101">"သိမ်းရန် ဤနေရာကို ထိပါ"</string>
     <string name="has_dictionary" msgid="6071847973466625007">"အဘိဓါန်ရနိုင်"</string>
     <string name="keyboard_layout" msgid="8451164783510487501">"ကီးဘုတ်အရောင်"</string>
-    <string name="switch_accounts" msgid="3321216593719006162">"အကောင့်များကို ပြောင်းရန်"</string>
+    <string name="switch_accounts" msgid="3321216593719006162">"အကောင့်များကို ပြောင်းရန်"</string>
     <string name="no_accounts_selected" msgid="2073821619103904330">"အကောင့်များ မရွေးရသေးပါ"</string>
     <string name="account_selected" msgid="2846876462199625974">"<xliff:g id="EMAIL_ADDRESS">%1$s</xliff:g>အား လတ်တလော သုံးန​ေ၏"</string>
     <string name="account_select_ok" msgid="9141195141763227797">"အိုကေ"</string>
@@ -156,7 +156,7 @@
     <string name="default_user_dict_pref_name" msgid="1625055720489280530">"သုံးစွဲသူ၏ အဘိဓာန်"</string>
     <string name="dictionary_available" msgid="4728975345815214218">"အဘိဓါန်ရရှိနိုင်"</string>
     <string name="dictionary_downloading" msgid="2982650524622620983">"လက်ရှိ ဒေါင်းလုပ်လုပ်နေသည်"</string>
-    <string name="dictionary_installed" msgid="8081558343559342962">"ထည့်သွင်းပြီး"</string>
+    <string name="dictionary_installed" msgid="8081558343559342962">"ထည့်သွင်းပြီး"</string>
     <string name="dictionary_disabled" msgid="8950383219564621762">"ထည့်သွင်းထားပြီး၊ ပိတ်ထားသည်"</string>
     <string name="cannot_connect_to_dict_service" msgid="9216933695765732398">"အဘိဓါန်ဝန်ဆောင်မှုသို့ ချိတ်ဆက်ရန် ပြဿနာရှိနေသည်"</string>
     <string name="no_dictionaries_available" msgid="8039920716566132611">"အဘိဓါန်မရှိ"</string>
@@ -170,7 +170,7 @@
     <string name="install_dict" msgid="180852772562189365">"တပ်ဆင်ပါ"</string>
     <string name="cancel_download_dict" msgid="7843340278507019303">"ထားတော့"</string>
     <string name="delete_dict" msgid="756853268088330054">"ဖျက်ရန်"</string>
-    <string name="should_download_over_metered_prompt" msgid="1583881200688185508">"သင့်ဖုန်းရှိ ရွေးချယ်ထားသည့် ဘာသာအတွက် အဘိဓါန်ရှိပါသည်။ &lt;br/&gt; အဘိဓါန်အား &lt;b&gt;ဒေါင်းလုပ်လုပ်ကာ&lt;/b&gt; the <xliff:g id="LANGUAGE_NAME">%1$s</xliff:g>  သင့်စာရိုက် အတွေ့အကြုံတိုးတက်စေရန် ကျွန်ုပ်တို့အကြံပြုပါသည်။ &lt;br/&gt; &lt;br/&gt; ဒေါင်းလုပ်လုပ်ရန် 3G ပေါ်တွင် ၁ မှ ၂ မိနစ်ခန့်ကြာနိုင်သည်။ သင့်တွင် &lt;b&gt;အကန့်သတ်မှရိ အချက်လက် သုံးစွဲမှု&lt;/b&gt;မရှိလျှင် ငွေကျသင့်နိုင်ပါသည်။ &lt;br/&gt; သင့်တွင် မည်သည့်အချက်လက်သုံးစွဲမှု ရှိနေသည်ကိုမသိလျှင်၊ အလိုအလျောက် ဒေါင်းလုပ်လုပ်ရန် Wi-Fi ကွန်ရက်တစ်ခု ရှာဖွေရန် တိုက်တွန်းပါသည်။ &lt;br/&gt; &lt;br/&gt; နည်းလမ်း: သင့်ဖုန်းကိရိယာရှိ &lt;b&gt;ဆက်တင်ထဲတွင်&lt;/b&gt; &lt;b&gt;ဘာသာ &amp; စာရိုက်ထည့်မှု&lt;/b&gt; သို့သွားကာ အဘိဓါန်များကို ဒေါင်းလုပ်လုပ်နိုင် ဖယ်ရှားနိုင်ပါသည်။"</string>
+    <string name="should_download_over_metered_prompt" msgid="1583881200688185508">"သင့်ဖုန်းရှိ ရွေးချယ်ထားသည့် ဘာသာအတွက် အဘိဓါန်ရှိပါသည်။ &lt;br/&gt; အဘိဓါန်အား &lt;b&gt;ဒေါင်းလုပ်လုပ်ကာ&lt;/b&gt; the <xliff:g id="LANGUAGE_NAME">%1$s</xliff:g>  သင့်စာရိုက် အတွေ့အကြုံတိုးတက်စေရန် ကျွန်ုပ်တို့အကြံပြုပါသည်။ &lt;br/&gt; &lt;br/&gt; ဒေါင်းလုပ်လုပ်ရန် 3G ပေါ်တွင် ၁ မှ ၂ မိနစ်ခန့်ကြာနိုင်သည်။ သင့်တွင် &lt;b&gt;အကန့်သတ်မှရိ အချက်လက် သုံးစွဲမှု&lt;/b&gt;မရှိလျှင် ငွေကျသင့်နိုင်ပါသည်။ &lt;br/&gt; သင့်တွင် မည်သည့်အချက်လက်သုံးစွဲမှု ရှိနေသည်ကိုမသိလျှင်၊ အလိုအလျောက် ဒေါင်းလုပ်လုပ်ရန် Wi-Fi ကွန်ရက်တစ်ခု ရှာဖွေရန် တိုက်တွန်းပါသည်။ &lt;br/&gt; &lt;br/&gt; နည်းလမ်း: သင့်ဖုန်းကိရိယာရှိ &lt;b&gt;ဆက်တင်ထဲတွင်&lt;/b&gt; &lt;b&gt;ဘာသာ &amp; စာရိုက်ထည့်မှု&lt;/b&gt; သို့သွားကာ အဘိဓါန်များကို ဒေါင်းလုပ်လုပ်နိုင် ဖယ်ရှားနိုင်ပါသည်။"</string>
     <string name="download_over_metered" msgid="1643065851159409546">"ယခုဒေါင်းလုပ်လုပ်မည် (<xliff:g id="SIZE_IN_MEGABYTES">%1$.1f</xliff:g>MB)"</string>
     <string name="do_not_download_over_metered" msgid="2176209579313941583">"Wi-Fi အသုံးပြု၍ ဒေါင်းလုပ်လုပ်ရန်"</string>
     <string name="dict_available_notification_title" msgid="4583842811218581658">"<xliff:g id="LANGUAGE_NAME">%1$s</xliff:g> အတွက် အဘိဓါန် ရနိုင်ပါသည်"</string>
@@ -191,7 +191,7 @@
     <string name="user_dict_settings_edit_dialog_title" msgid="3765774633869590352">"စာလုံးကို ပြင်ဆင်မည်"</string>
     <string name="user_dict_settings_context_menu_edit_title" msgid="6812255903472456302">"တည်းဖြတ်ရန်"</string>
     <string name="user_dict_settings_context_menu_delete_title" msgid="8142932447689461181">"ဖျက်ရန်"</string>
-    <string name="user_dict_settings_empty_text" msgid="558499587532668203">"သင့်အဘိဓာန်ထဲတွင် မည်သည့်စာလုံးမှမရှိပါ။ ထပ်ထည့်ခြင်း(+)ခလုတ်ကို ထိ၍ စာလုံးထည့်ပါ။"</string>
+    <string name="user_dict_settings_empty_text" msgid="558499587532668203">"သင့်အဘိဓာန်ထဲတွင် မည်သည့်စာလုံးမှမရှိပါ။ ထပ်ထည့်ခြင်း(+)ခလုတ်ကို ထိ၍ စာလုံးထည့်ပါ။"</string>
     <string name="user_dict_settings_all_languages" msgid="8276126583216298886">"ဘာသာစကားအားလုံးအတွက်"</string>
     <string name="user_dict_settings_more_languages" msgid="7131268499685180461">"ဘာသာစကားပိုများများ…"</string>
     <string name="user_dict_settings_delete" msgid="110413335187193859">"ဖျက်သိမ်းရန်"</string>
diff --git a/java/res/values-si-rLK/strings-emoji-descriptions.xml b/java/res/values-si-rLK/strings-emoji-descriptions.xml
index 14187a6..ae341dc 100644
--- a/java/res/values-si-rLK/strings-emoji-descriptions.xml
+++ b/java/res/values-si-rLK/strings-emoji-descriptions.xml
@@ -267,7 +267,7 @@
     <string name="spoken_emoji_1F36A" msgid="2726271795913042295">"කුකීය"</string>
     <string name="spoken_emoji_1F36B" msgid="6342163604299875931">"චොකලට් බාරය"</string>
     <string name="spoken_emoji_1F36C" msgid="2168934753998218790">"පැණිරස"</string>
-    <string name="spoken_emoji_1F36D" msgid="3671507903799975792">"ලොලිපොප්"</string>
+    <string name="spoken_emoji_1F36D" msgid="3671507903799975792">"Lollipop"</string>
     <string name="spoken_emoji_1F36E" msgid="4630541402785165902">"කස්ටට්"</string>
     <string name="spoken_emoji_1F36F" msgid="5577915387425169439">"මී පැණි මුට්ටිය"</string>
     <string name="spoken_emoji_1F370" msgid="7243244547866114951">"ෂොට් කේක්"</string>
diff --git a/java/res/values-v19/spinner-style.xml b/java/res/values-v19/spinner-style.xml
new file mode 100644
index 0000000..a699905
--- /dev/null
+++ b/java/res/values-v19/spinner-style.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2014, 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:android="http://schemas.android.com/apk/res/android">
+    <!-- Until KitKat (API 19), {@link android.widget.Spinner} of dialog mode in a Dialog can't
+         handle orientation change correctly. Using dropdown mode avoids the issue.
+         This file overrides values/spinner-style.xml on KitKat and up. -->
+    <style name="additionalSubtypeSpinnerStyle">
+        <item name="android:spinnerMode">dialog</item>
+    </style>
+</resources>
diff --git a/java/res/values/config-auto-correction-thresholds.xml b/java/res/values/config-auto-correction-thresholds.xml
index 7d94a42..fc701c7 100644
--- a/java/res/values/config-auto-correction-thresholds.xml
+++ b/java/res/values/config-auto-correction-thresholds.xml
@@ -34,6 +34,12 @@
         <item>floatNegativeInfinity</item>
     </string-array>
 
+    <!-- Chosen to be slightly less than the "aggressive" threshold. This is the threshold for
+         a mildly plausible suggestion given the input; if no "plausible" suggestion is present
+         for a language, it's a strong indicator the user is not typing in this language, so we
+         may be more forgiving of whitelist entries in another language. -->
+    <string name="plausibility_threshold" translatable="false">0.065</string>
+
     <!-- The index of the auto correction threshold values array. -->
     <string name="auto_correction_threshold_mode_index_off" translatable="false">0</string>
     <string name="auto_correction_threshold_mode_index_modest" translatable="false">1</string>
diff --git a/java/res/values/donottranslate-debug-settings.xml b/java/res/values/donottranslate-debug-settings.xml
index 491043f..55ab933 100644
--- a/java/res/values/donottranslate-debug-settings.xml
+++ b/java/res/values/donottranslate-debug-settings.xml
@@ -47,6 +47,12 @@
     <string name="prefs_resize_keyboard">Enable keyboard resizing</string>
     <!-- Title of the settings for setting keyboard height -->
     <string name="prefs_keyboard_height_scale">Keyboard height scale</string>
+    <!-- Title of the settings for setting keyboard width -->
+    <string name="prefs_keyboard_bottom_margin">Keyboard bottom margin</string>
+    <!-- Title of the settings for setting keyboard width -->
+    <string name="prefs_keyboard_left_margin">Keyboard left margin</string>
+    <!-- Title of the settings for setting keyboard width -->
+    <string name="prefs_keyboard_right_margin">Keyboard right margin</string>
     <!-- Message to show when there are no files to install as an external dictionary [CHAR LIMIT=100] -->
     <string name="read_external_dictionary_no_files_message">No dictionary files in the Downloads folder</string>
     <!-- Title of the dialog that selects a file to install as an external dictionary [CHAR LIMIT=50] -->
diff --git a/java/res/values/spinner-style.xml b/java/res/values/spinner-style.xml
new file mode 100644
index 0000000..c0f32ab
--- /dev/null
+++ b/java/res/values/spinner-style.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2014, 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:android="http://schemas.android.com/apk/res/android">
+    <!-- Until KitKat (API 19), {@link android.widget.Spinner} of dialog mode in a Dialog can't
+         handle orientation change correctly. Using dropdown mode avoids the issue.
+         This file is overridden by values-v19/spinner-style.xml on KitKat and up. -->
+    <style name="additionalSubtypeSpinnerStyle">
+        <item name="android:spinnerMode">dropdown</item>
+    </style>
+</resources>
diff --git a/java/res/xml-sw600dp/key_styles_common.xml b/java/res/xml-sw600dp/key_styles_common.xml
index 006cda3..ea8f292 100644
--- a/java/res/xml-sw600dp/key_styles_common.xml
+++ b/java/res/xml-sw600dp/key_styles_common.xml
@@ -35,10 +35,6 @@
                 latin:keyLabelFlags="hasShiftedLetterHint" />
         </default>
     </switch>
-    <!-- Base key style for the key which may have settings key as more keys. -->
-    <key-style
-        latin:styleName="baseSettingsMoreKeysStyle"
-        latin:parentStyle="hasShiftedLetterHintStyle" />
     <include
         latin:keyboardLayout="@xml/key_styles_settings" />
     <!-- Functional key styles -->
diff --git a/java/res/xml/key_styles_common.xml b/java/res/xml/key_styles_common.xml
index b36ddf2..d85438d 100644
--- a/java/res/xml/key_styles_common.xml
+++ b/java/res/xml/key_styles_common.xml
@@ -35,9 +35,6 @@
                 latin:keyLabelFlags="hasShiftedLetterHint" />
         </default>
     </switch>
-    <!-- Base key style for the key which may have settings key as more keys. -->
-    <key-style
-        latin:styleName="baseSettingsMoreKeysStyle" />
     <include
         latin:keyboardLayout="@xml/key_styles_settings" />
     <!-- Functional key styles -->
diff --git a/java/res/xml/key_styles_settings.xml b/java/res/xml/key_styles_settings.xml
index a504bed..43ee601 100644
--- a/java/res/xml/key_styles_settings.xml
+++ b/java/res/xml/key_styles_settings.xml
@@ -21,16 +21,14 @@
 <merge
     xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
 >
-    <!-- Base key style for the key which may have settings key as more keys. -->
-    <!-- Kept as a separate file for cleaner overriding by an overlay.  -->
+    <!-- Key style for the key which may have settings key as more keys. -->
     <switch>
         <case
             latin:clobberSettingsKey="true"
         >
             <key-style
                 latin:styleName="settingsMoreKeysStyle"
-                latin:backgroundType="functional"
-                latin:parentStyle="baseSettingsMoreKeysStyle" />
+                latin:backgroundType="functional" />
         </case>
         <!-- clobberSettingsKey="false" -->
         <default>
@@ -38,8 +36,7 @@
                 latin:styleName="settingsMoreKeysStyle"
                 latin:keyLabelFlags="hasPopupHint"
                 latin:additionalMoreKeys="!text/keyspec_settings"
-                latin:backgroundType="functional"
-                latin:parentStyle="baseSettingsMoreKeysStyle" />
+                latin:backgroundType="functional" />
         </default>
     </switch>
 </merge>
diff --git a/java/res/xml/prefs_screen_debug.xml b/java/res/xml/prefs_screen_debug.xml
index 905bc04..3134727 100644
--- a/java/res/xml/prefs_screen_debug.xml
+++ b/java/res/xml/prefs_screen_debug.xml
@@ -87,6 +87,24 @@
         android:title="@string/prefs_keyboard_height_scale"
         latin:minValue="50"
         latin:maxValue="120" /> <!-- percentage -->
+    <com.android.inputmethod.latin.settings.SeekBarDialogPreference
+        android:dependency="pref_resize_keyboard"
+        android:key="pref_keyboard_bottom_margin"
+        android:title="@string/prefs_keyboard_bottom_margin"
+        latin:minValue="0"
+        latin:maxValue="50" /> <!-- percentage -->
+    <com.android.inputmethod.latin.settings.SeekBarDialogPreference
+        android:dependency="pref_resize_keyboard"
+        android:key="pref_keyboard_left_margin"
+        android:title="@string/prefs_keyboard_left_margin"
+        latin:minValue="0"
+        latin:maxValue="50" /> <!-- percentage -->
+    <com.android.inputmethod.latin.settings.SeekBarDialogPreference
+        android:dependency="pref_resize_keyboard"
+        android:key="pref_keyboard_right_margin"
+        android:title="@string/prefs_keyboard_right_margin"
+        latin:minValue="0"
+        latin:maxValue="50" /> <!-- percentage -->
     <PreferenceScreen
         android:key="read_external_dictionary"
         android:title="@string/prefs_read_external_dictionary" />
diff --git a/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java
index 94a1ee6..e80982f 100644
--- a/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java
+++ b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java
@@ -269,13 +269,9 @@
                 eventTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0 /* metaState */);
         // Inject a fake down event to {@link PointerTracker} to handle a long press correctly.
         tracker.processMotionEvent(downEvent, mKeyDetector);
-        // The above fake down event triggers an unnecessary long press timer that should be
-        // canceled.
-        tracker.cancelLongPressTimer();
         downEvent.recycle();
-        // Invoke {@link MainKeyboardView#onLongPress(PointerTracker)} as if a long press timeout
-        // has passed.
-        mKeyboardView.onLongPress(tracker);
+        // Invoke {@link PointerTracker#onLongPressed()} as if a long press timeout has passed.
+        tracker.onLongPressed();
         // If {@link Key#hasNoPanelAutoMoreKeys()} is true (such as "0 +" key on the phone layout)
         // or a key invokes IME switcher dialog, we should just ignore the next
         // {@link #onRegisterHoverKey(Key,MotionEvent)}. It can be determined by whether
diff --git a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
index be07443..c38ea00 100644
--- a/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
+++ b/java/src/com/android/inputmethod/compat/SuggestionSpanUtils.java
@@ -118,11 +118,7 @@
             if (TextUtils.isEmpty(localeString)) {
                 continue;
             }
-            final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
-            if (locale == null) {
-                continue;
-            }
-            return locale;
+            return LocaleUtils.constructLocaleFromString(localeString);
         }
         return null;
     }
diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java b/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java
index e6acb8f..c678f08 100644
--- a/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java
+++ b/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java
@@ -22,6 +22,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.os.IBinder;
+import android.util.Log;
 import android.widget.Toast;
 
 import com.android.inputmethod.latin.R;
@@ -33,6 +34,8 @@
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
+import javax.annotation.Nonnull;
+
 /**
  * Service that handles background tasks for the dictionary provider.
  *
@@ -51,6 +54,8 @@
  *     to access, and mark the current state as such.
  */
 public final class DictionaryService extends Service {
+    private static final String TAG = DictionaryService.class.getSimpleName();
+
     /**
      * The package name, to use in the intent actions.
      */
@@ -156,9 +161,14 @@
             final int startId) {
         final DictionaryService self = this;
         if (SHOW_DOWNLOAD_TOAST_INTENT_ACTION.equals(intent.getAction())) {
-            // This is a UI action, it can't be run in another thread
-            showStartDownloadingToast(this, LocaleUtils.constructLocaleFromString(
-                    intent.getStringExtra(LOCALE_INTENT_ARGUMENT)));
+            final String localeString = intent.getStringExtra(LOCALE_INTENT_ARGUMENT);
+            if (localeString == null) {
+                Log.e(TAG, "Received " + intent.getAction() + " without locale; skipped");
+            } else {
+                // This is a UI action, it can't be run in another thread
+                showStartDownloadingToast(
+                        this, LocaleUtils.constructLocaleFromString(localeString));
+            }
         } else {
             // If it's a command that does not require UI, arrange for the work to be done on a
             // separate thread, so that we can return right away. The executor will spawn a thread
@@ -245,7 +255,8 @@
     /**
      * Shows a toast informing the user that an automatic dictionary download is starting.
      */
-    private static void showStartDownloadingToast(final Context context, final Locale locale) {
+    private static void showStartDownloadingToast(final Context context,
+            @Nonnull final Locale locale) {
         final String toastText = String.format(
                 context.getString(R.string.toast_downloading_suggestions),
                 locale.getDisplayName());
diff --git a/java/src/com/android/inputmethod/dictionarypack/DownloadOverMeteredDialog.java b/java/src/com/android/inputmethod/dictionarypack/DownloadOverMeteredDialog.java
index 50b3c72..91ed673 100644
--- a/java/src/com/android/inputmethod/dictionarypack/DownloadOverMeteredDialog.java
+++ b/java/src/com/android/inputmethod/dictionarypack/DownloadOverMeteredDialog.java
@@ -28,7 +28,7 @@
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.common.LocaleUtils;
 
-import java.util.Locale;
+import javax.annotation.Nullable;
 
 /**
  * This implements the dialog for asking the user whether it's okay to download dictionaries over
@@ -54,11 +54,11 @@
         setTexts(localeString, size);
     }
 
-    private void setTexts(final String localeString, final long size) {
+    private void setTexts(@Nullable final String localeString, final long size) {
         final String promptFormat = getString(R.string.should_download_over_metered_prompt);
         final String allowButtonFormat = getString(R.string.download_over_metered);
-        final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
-        final String language = (null == locale ? "" : locale.getDisplayLanguage());
+        final String language = (null == localeString) ? ""
+                : LocaleUtils.constructLocaleFromString(localeString).getDisplayLanguage();
         final TextView prompt = (TextView)findViewById(R.id.download_over_metered_prompt);
         prompt.setText(Html.fromHtml(String.format(promptFormat, language)));
         final Button allowButton = (Button)findViewById(R.id.allow_button);
diff --git a/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java b/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
index aeb6667..f05db9d 100644
--- a/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
+++ b/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java
@@ -57,7 +57,6 @@
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Locale;
 import java.util.Set;
 import java.util.TreeSet;
 
@@ -880,8 +879,8 @@
         // None of those are expected to happen, but just in case...
         if (null == notificationIntent || null == notificationManager) return;
 
-        final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
-        final String language = (null == locale ? "" : locale.getDisplayLanguage());
+        final String language = (null == localeString) ? ""
+                : LocaleUtils.constructLocaleFromString(localeString).getDisplayLanguage();
         final String titleFormat = context.getString(R.string.dict_available_notification_title);
         final String notificationTitle = String.format(titleFormat, language);
         final Notification.Builder builder = new Notification.Builder(context)
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java
index 06e552e..d1db3d4 100644
--- a/java/src/com/android/inputmethod/keyboard/Key.java
+++ b/java/src/com/android/inputmethod/keyboard/Key.java
@@ -43,6 +43,9 @@
 import java.util.Arrays;
 import java.util.Locale;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 /**
  * Class for describing the position and characteristics of a single key in the keyboard.
  */
@@ -113,9 +116,11 @@
     /** Y coordinate of the top-left corner of the key in the keyboard layout, excluding the gap. */
     private final int mY;
     /** Hit bounding box of the key */
+    @Nonnull
     private final Rect mHitBox = new Rect();
 
     /** More keys. It is guaranteed that this is null or an array of one or more elements */
+    @Nullable
     private final MoreKeySpec[] mMoreKeys;
     /** More keys column number and flags */
     private final int mMoreKeysColumnAndFlags;
@@ -158,8 +163,9 @@
     private static final int ACTION_FLAGS_ALT_CODE_WHILE_TYPING = 0x04;
     private static final int ACTION_FLAGS_ENABLE_LONG_PRESS = 0x08;
 
+    @Nullable
     private final KeyVisualAttributes mKeyVisualAttributes;
-
+    @Nullable
     private final OptionalAttributes mOptionalAttributes;
 
     private static final class OptionalAttributes {
@@ -181,6 +187,7 @@
             mVisualInsetsRight = visualInsetsRight;
         }
 
+        @Nullable
         public static OptionalAttributes newInstance(final String outputText, final int altCode,
                 final int disabledIconId, final int visualInsetsLeft, final int visualInsetsRight) {
             if (outputText == null && altCode == CODE_UNSPECIFIED
@@ -204,10 +211,10 @@
      * Constructor for a key on <code>MoreKeyKeyboard</code>, on <code>MoreSuggestions</code>,
      * and in a <GridRows/>.
      */
-    public Key(final String label, final int iconId, final int code, final String outputText,
-            final String hintLabel, final int labelFlags, final int backgroundType, final int x,
-            final int y, final int width, final int height, final int horizontalGap,
-            final int verticalGap) {
+    public Key(@Nullable final String label, final int iconId, final int code,
+            @Nullable final String outputText, @Nullable final String hintLabel,
+            final int labelFlags, final int backgroundType, final int x, final int y,
+            final int width, final int height, final int horizontalGap, final int verticalGap) {
         mWidth = width - horizontalGap;
         mHeight = height - verticalGap;
         mHorizontalGap = horizontalGap;
@@ -245,8 +252,9 @@
      * @param row the row that this key belongs to. row's x-coordinate will be the right edge of
      *        this key.
      */
-    public Key(final String keySpec, final TypedArray keyAttr, final KeyStyle style,
-            final KeyboardParams params, final KeyboardRow row) {
+    public Key(@Nullable final String keySpec, @Nonnull final TypedArray keyAttr,
+            @Nonnull final KeyStyle style, @Nonnull final KeyboardParams params,
+            @Nonnull final KeyboardRow row) {
         mHorizontalGap = isSpacer() ? 0 : params.mHorizontalGap;
         mVerticalGap = params.mVerticalGap;
 
@@ -403,11 +411,11 @@
      *
      * @param key the original key.
      */
-    protected Key(final Key key) {
+    protected Key(@Nonnull final Key key) {
         this(key, key.mMoreKeys);
     }
 
-    private Key(final Key key, final MoreKeySpec[] moreKeys) {
+    private Key(@Nonnull final Key key, @Nullable final MoreKeySpec[] moreKeys) {
         // Final attributes.
         mCode = key.mCode;
         mLabel = key.mLabel;
@@ -433,8 +441,9 @@
         mEnabled = key.mEnabled;
     }
 
-    public static Key removeRedundantMoreKeys(final Key key,
-            final MoreKeySpec.LettersOnBaseLayout lettersOnBaseLayout) {
+    @Nonnull
+    public static Key removeRedundantMoreKeys(@Nonnull final Key key,
+            @Nonnull final MoreKeySpec.LettersOnBaseLayout lettersOnBaseLayout) {
         final MoreKeySpec[] moreKeys = key.getMoreKeys();
         final MoreKeySpec[] filteredMoreKeys = MoreKeySpec.removeRedundantMoreKeys(
                 moreKeys, lettersOnBaseLayout);
@@ -554,14 +563,17 @@
         return mCode;
     }
 
+    @Nullable
     public String getLabel() {
         return mLabel;
     }
 
+    @Nullable
     public String getHintLabel() {
         return mHintLabel;
     }
 
+    @Nullable
     public MoreKeySpec[] getMoreKeys() {
         return mMoreKeys;
     }
@@ -620,6 +632,7 @@
         return mKeyVisualAttributes;
     }
 
+    @Nonnull
     public final Typeface selectTypeface(final KeyDrawParams params) {
         switch (mLabelFlags & LABEL_FLAGS_FONT_MASK) {
         case LABEL_FLAGS_FONT_NORMAL:
@@ -696,6 +709,7 @@
         return params.mLetterSize;
     }
 
+    @Nonnull
     public Typeface selectPreviewTypeface(final KeyDrawParams params) {
         if (previewHasLetterSize()) {
             return selectTypeface(params);
@@ -780,6 +794,7 @@
         return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_NO_PANEL_AUTO_MORE_KEY) != 0;
     }
 
+    @Nullable
     public final String getOutputText() {
         final OptionalAttributes attrs = mOptionalAttributes;
         return (attrs != null) ? attrs.mOutputText : null;
@@ -794,6 +809,7 @@
         return mIconId;
     }
 
+    @Nullable
     public Drawable getIcon(final KeyboardIconsSet iconSet, final int alpha) {
         final OptionalAttributes attrs = mOptionalAttributes;
         final int disabledIconId = (attrs != null) ? attrs.mDisabledIconId : ICON_UNDEFINED;
@@ -805,6 +821,7 @@
         return icon;
     }
 
+    @Nullable
     public Drawable getPreviewIcon(final KeyboardIconsSet iconSet) {
         return iconSet.getIconDrawable(getIconId());
     }
@@ -897,6 +914,7 @@
         mEnabled = enabled;
     }
 
+    @Nonnull
     public Rect getHitBox() {
         return mHitBox;
     }
@@ -968,8 +986,10 @@
      * @return the background drawable of the key.
      * @see android.graphics.drawable.StateListDrawable#setState(int[])
      */
-    public final Drawable selectBackgroundDrawable(final Drawable keyBackground,
-            final Drawable functionalKeyBackground, final Drawable spacebarBackground) {
+    @Nonnull
+    public final Drawable selectBackgroundDrawable(@Nonnull final Drawable keyBackground,
+            @Nonnull final Drawable functionalKeyBackground,
+            @Nonnull final Drawable spacebarBackground) {
         final Drawable background;
         if (mBackgroundType == BACKGROUND_TYPE_FUNCTIONAL) {
             background = functionalKeyBackground;
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index 619b801..2055a59 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -28,6 +28,9 @@
 import java.util.Collections;
 import java.util.List;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 /**
  * Loads an XML description of a keyboard and stores the attributes of the keys. A keyboard
  * consists of rows of keys.
@@ -47,6 +50,7 @@
  * </pre>
  */
 public class Keyboard {
+    @Nonnull
     public final KeyboardId mId;
     public final int mThemeId;
 
@@ -78,17 +82,22 @@
     public final int mMaxMoreKeysKeyboardColumn;
 
     /** List of keys in this keyboard */
+    @Nonnull
     private final List<Key> mSortedKeys;
+    @Nonnull
     public final List<Key> mShiftKeys;
+    @Nonnull
     public final List<Key> mAltCodeKeysWhileTyping;
+    @Nonnull
     public final KeyboardIconsSet mIconsSet;
 
     private final SparseArray<Key> mKeyCache = new SparseArray<>();
 
+    @Nonnull
     private final ProximityInfo mProximityInfo;
     private final boolean mProximityCharsCorrectionEnabled;
 
-    public Keyboard(final KeyboardParams params) {
+    public Keyboard(@Nonnull final KeyboardParams params) {
         mId = params.mId;
         mThemeId = params.mThemeId;
         mOccupiedHeight = params.mOccupiedHeight;
@@ -114,7 +123,7 @@
         mProximityCharsCorrectionEnabled = params.mProximityCharsCorrectionEnabled;
     }
 
-    protected Keyboard(final Keyboard keyboard) {
+    protected Keyboard(@Nonnull final Keyboard keyboard) {
         mId = keyboard.mId;
         mThemeId = keyboard.mThemeId;
         mOccupiedHeight = keyboard.mOccupiedHeight;
@@ -150,6 +159,7 @@
         return canAssumeNativeHasProximityCharsInfoOfAllKeys || Character.isLetter(code);
     }
 
+    @Nonnull
     public ProximityInfo getProximityInfo() {
         return mProximityInfo;
     }
@@ -160,10 +170,12 @@
      * The list may contain {@link Key.Spacer} object as well.
      * @return the sorted unmodifiable list of {@link Key}s of this keyboard.
      */
+    @Nonnull
     public List<Key> getSortedKeys() {
         return mSortedKeys;
     }
 
+    @Nullable
     public Key getKey(final int code) {
         if (code == Constants.CODE_UNSPECIFIED) {
             return null;
@@ -185,7 +197,7 @@
         }
     }
 
-    public boolean hasKey(final Key aKey) {
+    public boolean hasKey(@Nonnull final Key aKey) {
         if (mKeyCache.indexOfValue(aKey) >= 0) {
             return true;
         }
@@ -211,6 +223,7 @@
      * @return the list of the nearest keys to the given point. If the given
      * point is out of range, then an array of size zero is returned.
      */
+    @Nonnull
     public List<Key> getNearestKeys(final int x, final int y) {
         // Avoid dead pixels at edges of the keyboard
         final int adjustedX = Math.max(0, Math.min(x, mOccupiedWidth - 1));
@@ -218,7 +231,8 @@
         return mProximityInfo.getNearestKeys(adjustedX, adjustedY);
     }
 
-    public int[] getCoordinates(final int[] codePoints) {
+    @Nonnull
+    public int[] getCoordinates(@Nonnull final int[] codePoints) {
         final int length = codePoints.length;
         final int[] coordinates = CoordinateUtils.newCoordinateArray(length);
         for (int i = 0; i < length; ++i) {
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java b/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
index b105138..d71dc59 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
@@ -34,7 +34,7 @@
 import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils;
 import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
 import com.android.inputmethod.keyboard.internal.KeyboardParams;
-import com.android.inputmethod.keyboard.internal.KeysCache;
+import com.android.inputmethod.keyboard.internal.UniqueKeysCache;
 import com.android.inputmethod.latin.InputAttributes;
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.RichInputMethodSubtype;
@@ -72,6 +72,7 @@
     private static final String KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX = "keyboard_layout_set_";
 
     private final Context mContext;
+    @Nonnull
     private final Params mParams;
 
     // How many layouts we forcibly keep in cache. This only includes ALPHABET (default) and
@@ -84,7 +85,8 @@
     private static final Keyboard[] sForcibleKeyboardCache = new Keyboard[FORCIBLE_CACHE_SIZE];
     private static final HashMap<KeyboardId, SoftReference<Keyboard>> sKeyboardCache =
             new HashMap<>();
-    private static final KeysCache sKeysCache = new KeysCache();
+    @Nonnull
+    private static final UniqueKeysCache sUniqueKeysCache = UniqueKeysCache.newInstance();
     private final static HashMap<InputMethodSubtype, Integer> sScriptIdsForSubtypes =
             new HashMap<>();
 
@@ -142,10 +144,11 @@
 
     private static void clearKeyboardCache() {
         sKeyboardCache.clear();
-        sKeysCache.clear();
+        sUniqueKeysCache.clear();
     }
 
-    public static int getScriptId(final Resources resources, final InputMethodSubtype subtype) {
+    public static int getScriptId(final Resources resources,
+            @Nonnull final InputMethodSubtype subtype) {
         final Integer value = sScriptIdsForSubtypes.get(subtype);
         if (null == value) {
             final int scriptId = Builder.readScriptId(resources, subtype);
@@ -155,11 +158,12 @@
         return value;
     }
 
-    KeyboardLayoutSet(final Context context, final Params params) {
+    KeyboardLayoutSet(final Context context, @Nonnull final Params params) {
         mContext = context;
         mParams = params;
     }
 
+    @Nonnull
     public Keyboard getKeyboard(final int baseKeyboardLayoutSetElementId) {
         final int keyboardLayoutSetElementId;
         switch (mParams.mMode) {
@@ -203,6 +207,7 @@
         }
     }
 
+    @Nonnull
     private Keyboard getKeyboard(final ElementParams elementParams, final KeyboardId id) {
         final SoftReference<Keyboard> ref = sKeyboardCache.get(id);
         final Keyboard cachedKeyboard = (ref == null) ? null : ref.get();
@@ -214,10 +219,8 @@
         }
 
         final KeyboardBuilder<KeyboardParams> builder =
-                new KeyboardBuilder<>(mContext, new KeyboardParams());
-        if (id.isAlphabetKeyboard()) {
-            builder.setAutoGenerate(sKeysCache);
-        }
+                new KeyboardBuilder<>(mContext, new KeyboardParams(sUniqueKeysCache));
+        sUniqueKeysCache.setEnabled(id.isAlphabetKeyboard());
         builder.setAllowRedundantMoreKes(elementParams.mAllowRedundantMoreKeys);
         final int keyboardXmlId = elementParams.mKeyboardXmlId;
         builder.load(keyboardXmlId, id);
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index 5e3a5f1..d19f506 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -22,7 +22,9 @@
 import android.view.ContextThemeWrapper;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.ViewGroup;
 import android.view.inputmethod.EditorInfo;
+import android.widget.LinearLayout;
 
 import com.android.inputmethod.compat.InputMethodServiceCompatUtils;
 import com.android.inputmethod.event.Event;
@@ -64,7 +66,7 @@
 
     private KeyboardTheme mKeyboardTheme;
     private Context mThemeContext;
-
+    private View mHorizontalKeyboardFrame;
     private static final KeyboardSwitcher sInstance = new KeyboardSwitcher();
 
     public static KeyboardSwitcher getInstance() {
@@ -111,8 +113,16 @@
         final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
                 mThemeContext, editorInfo);
         final Resources res = mThemeContext.getResources();
-        final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
+        final ViewGroup.MarginLayoutParams p =
+                (ViewGroup.MarginLayoutParams) mHorizontalKeyboardFrame.getLayoutParams();
+
+        final int keyboardLeftMargin = ResourceUtils.getKeyboardLeftMargin(res, settingsValues);
+        final int keyboardRightMargin = ResourceUtils.getKeyboardRightMargin(res, settingsValues);
+        final int keyboardBottomMargin = ResourceUtils.getKeyboardBottomMargin(res, settingsValues);
+        p.setMargins(keyboardLeftMargin, 0, keyboardRightMargin, keyboardBottomMargin);
+
         final int keyboardHeight = ResourceUtils.getKeyboardHeight(res, settingsValues);
+        final int keyboardWidth = ResourceUtils.getKeyboardWidth(res, settingsValues);
         builder.setKeyboardGeometry(keyboardWidth, keyboardHeight);
         builder.setSubtype(RichInputMethodManager.getInstance().getCurrentSubtype());
         builder.setVoiceInputKeyEnabled(settingsValues.mShowsVoiceInputKey);
@@ -411,6 +421,8 @@
         mEmojiPalettesView.setHardwareAcceleratedDrawingEnabled(
                 isHardwareAcceleratedDrawingEnabled);
         mEmojiPalettesView.setKeyboardActionListener(mLatinIME);
+        mHorizontalKeyboardFrame = (LinearLayout)mCurrentInputView.findViewById(
+                R.id.horizontal_keyboard_frame);
         return mCurrentInputView;
     }
 
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index 01980ea..27e538c 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -103,7 +103,8 @@
     // TODO: Consider having a dummy keyboard object to make this @Nonnull
     @Nullable
     private Keyboard mKeyboard;
-    protected final KeyDrawParams mKeyDrawParams = new KeyDrawParams();
+    @Nonnull
+    private final KeyDrawParams mKeyDrawParams = new KeyDrawParams();
 
     // Drawing
     /** True if all keys should be drawn */
@@ -120,6 +121,7 @@
     @Nonnull
     private final Paint mPaint = new Paint();
     private final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics();
+
     public KeyboardView(final Context context, final AttributeSet attrs) {
         this(context, attrs, R.attr.keyboardViewStyle);
     }
@@ -210,6 +212,11 @@
         return mVerticalCorrection;
     }
 
+    @Nonnull
+    protected KeyDrawParams getKeyDrawParams() {
+        return mKeyDrawParams;
+    }
+
     protected void updateKeyDrawParams(final int keyHeight) {
         mKeyDrawParams.updateParams(keyHeight, mKeyVisualAttributes);
     }
diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
index 06b87bd..eeac475 100644
--- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
@@ -461,12 +461,17 @@
         windowContentView.addView(mDrawingPreviewPlacerView);
     }
 
+    // Implements {@link DrawingProxy#onKeyPressed(Key,boolean)}.
     @Override
-    public void showKeyPreview(@Nonnull final Key key) {
-        // If the key is invalid or has no key preview, we must not show key preview.
-        if (key.noKeyPreview()) {
-            return;
+    public void onKeyPressed(@Nonnull final Key key, final boolean withPreview) {
+        key.onPressed();
+        invalidateKey(key);
+        if (withPreview && !key.noKeyPreview()) {
+            showKeyPreview(key);
         }
+    }
+
+    private void showKeyPreview(@Nonnull final Key key) {
         final Keyboard keyboard = getKeyboard();
         if (keyboard == null) {
             return;
@@ -479,19 +484,30 @@
 
         locatePreviewPlacerView();
         getLocationInWindow(mOriginCoords);
-        mKeyPreviewChoreographer.placeAndShowKeyPreview(key, keyboard.mIconsSet, mKeyDrawParams,
+        mKeyPreviewChoreographer.placeAndShowKeyPreview(key, keyboard.mIconsSet, getKeyDrawParams(),
                 getWidth(), mOriginCoords, mDrawingPreviewPlacerView, isHardwareAccelerated());
     }
 
-    // Implements {@link DrawingProxy#dismissKeyPreviewWithoutDelay(Key)}.
-    @Override
-    public void dismissKeyPreviewWithoutDelay(@Nonnull final Key key) {
+    private void dismissKeyPreviewWithoutDelay(@Nonnull final Key key) {
         mKeyPreviewChoreographer.dismissKeyPreview(key, false /* withAnimation */);
         invalidateKey(key);
     }
 
+    // Implements {@link DrawingProxy#onKeyReleased(Key,boolean)}.
     @Override
-    public void dismissKeyPreview(@Nonnull final Key key) {
+    public void onKeyReleased(@Nonnull final Key key, final boolean withAnimation) {
+        key.onReleased();
+        invalidateKey(key);
+        if (!key.noKeyPreview()) {
+            if (withAnimation) {
+                dismissKeyPreview(key);
+            } else {
+                dismissKeyPreviewWithoutDelay(key);
+            }
+        }
+    }
+
+    private void dismissKeyPreview(@Nonnull final Key key) {
         if (isHardwareAccelerated()) {
             mKeyPreviewChoreographer.dismissKeyPreview(key, true /* withAnimation */);
             return;
@@ -574,7 +590,11 @@
         mDrawingPreviewPlacerView.removeAllViews();
     }
 
-    private MoreKeysPanel onCreateMoreKeysPanel(final Key key, final Context context) {
+    // Implements {@link DrawingProxy@showMoreKeysKeyboard(Key,PointerTracker)}.
+    @Override
+    @Nullable
+    public MoreKeysPanel showMoreKeysKeyboard(@Nonnull final Key key,
+            @Nonnull final PointerTracker tracker) {
         final MoreKeySpec[] moreKeys = key.getMoreKeys();
         if (moreKeys == null) {
             return null;
@@ -590,7 +610,7 @@
                     && !key.noKeyPreview() && moreKeys.length == 1
                     && mKeyPreviewDrawParams.getVisibleWidth() > 0;
             final MoreKeysKeyboard.Builder builder = new MoreKeysKeyboard.Builder(
-                    context, key, getKeyboard(), isSingleMoreKeyWithPreview,
+                    getContext(), key, getKeyboard(), isSingleMoreKeyWithPreview,
                     mKeyPreviewDrawParams.getVisibleWidth(),
                     mKeyPreviewDrawParams.getVisibleHeight(), newLabelPaint(key));
             moreKeysKeyboard = builder.build();
@@ -603,50 +623,6 @@
                 (MoreKeysKeyboardView)container.findViewById(R.id.more_keys_keyboard_view);
         moreKeysKeyboardView.setKeyboard(moreKeysKeyboard);
         container.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
-        return moreKeysKeyboardView;
-    }
-
-    // Implements {@link DrawingProxy@onLongPress(PointerTracker)}.
-    /**
-     * Called when a key is long pressed.
-     * @param tracker the pointer tracker which pressed the parent key
-     */
-    @Override
-    public void onLongPress(@Nonnull final PointerTracker tracker) {
-        if (isShowingMoreKeysPanel()) {
-            return;
-        }
-        final Key key = tracker.getKey();
-        if (key == null) {
-            return;
-        }
-        final KeyboardActionListener listener = mKeyboardActionListener;
-        if (key.hasNoPanelAutoMoreKey()) {
-            final int moreKeyCode = key.getMoreKeys()[0].mCode;
-            tracker.onLongPressed();
-            listener.onPressKey(moreKeyCode, 0 /* repeatCount */, true /* isSinglePointer */);
-            listener.onCodeInput(moreKeyCode, Constants.NOT_A_COORDINATE,
-                    Constants.NOT_A_COORDINATE, false /* isKeyRepeat */);
-            listener.onReleaseKey(moreKeyCode, false /* withSliding */);
-            return;
-        }
-        final int code = key.getCode();
-        if (code == Constants.CODE_SPACE || code == Constants.CODE_LANGUAGE_SWITCH) {
-            // Long pressing the space key invokes IME switcher dialog.
-            if (listener.onCustomRequest(Constants.CUSTOM_CODE_SHOW_INPUT_METHOD_PICKER)) {
-                tracker.onLongPressed();
-                listener.onReleaseKey(code, false /* withSliding */);
-                return;
-            }
-        }
-        openMoreKeysPanel(key, tracker);
-    }
-
-    private void openMoreKeysPanel(final Key key, final PointerTracker tracker) {
-        final MoreKeysPanel moreKeysPanel = onCreateMoreKeysPanel(key, getContext());
-        if (moreKeysPanel == null) {
-            return;
-        }
 
         final int[] lastCoords = CoordinateUtils.newInstance();
         tracker.getLastCoordinates(lastCoords);
@@ -664,10 +640,8 @@
         // {@code mPreviewVisibleOffset} has been set appropriately in
         // {@link KeyboardView#showKeyPreview(PointerTracker)}.
         final int pointY = key.getY() + mKeyPreviewDrawParams.getVisibleOffset();
-        moreKeysPanel.showMoreKeysPanel(this, this, pointX, pointY, mKeyboardActionListener);
-        tracker.onShowMoreKeysPanel(moreKeysPanel);
-        // TODO: Implement zoom in animation of more keys panel.
-        mKeyPreviewChoreographer.dismissKeyPreview(key, false /* withAnimation */);
+        moreKeysKeyboardView.showMoreKeysPanel(this, this, pointX, pointY, mKeyboardActionListener);
+        return moreKeysKeyboardView;
     }
 
     public boolean isInDraggingFinger() {
@@ -895,13 +869,16 @@
     }
 
     private void drawLanguageOnSpacebar(final Key key, final Canvas canvas, final Paint paint) {
+        final Keyboard keyboard = getKeyboard();
+        if (keyboard == null) {
+            return;
+        }
         final int width = key.getWidth();
         final int height = key.getHeight();
         paint.setTextAlign(Align.CENTER);
         paint.setTypeface(Typeface.DEFAULT);
         paint.setTextSize(mLanguageOnSpacebarTextSize);
-        final RichInputMethodSubtype subtype = getKeyboard().mId.mSubtype;
-        final String language = layoutLanguageOnSpacebar(paint, subtype, width);
+        final String language = layoutLanguageOnSpacebar(paint, keyboard.mId.mSubtype, width);
         // Draw language text with shadow
         final float descent = paint.descent();
         final float textHeight = -paint.ascent() + descent;
diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboard.java b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboard.java
index f0de86f..a021e5e 100644
--- a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboard.java
@@ -27,6 +27,8 @@
 import com.android.inputmethod.latin.common.StringUtils;
 import com.android.inputmethod.latin.utils.TypefaceUtils;
 
+import javax.annotation.Nonnull;
+
 public final class MoreKeysKeyboard extends Keyboard {
     private final int mDefaultKeyCoordX;
 
@@ -328,6 +330,7 @@
         }
 
         @Override
+        @Nonnull
         public MoreKeysKeyboard build() {
             final MoreKeysKeyboardParams params = mParams;
             final int moreKeyFlags = mParentKey.getMoreKeyLabelFlags();
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index 7902ce8..9764cb3 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -222,7 +222,7 @@
         final int trackersSize = sTrackers.size();
         for (int i = 0; i < trackersSize; ++i) {
             final PointerTracker tracker = sTrackers.get(i);
-            tracker.setReleasedKeyGraphics(tracker.getKey());
+            tracker.setReleasedKeyGraphics(tracker.getKey(), true /* withAnimation */);
         }
     }
 
@@ -382,19 +382,17 @@
         return mKeyDetector.detectHitKey(x, y);
     }
 
-    private void setReleasedKeyGraphics(@Nullable final Key key) {
+    private void setReleasedKeyGraphics(@Nullable final Key key, final boolean withAnimation) {
         if (key == null) {
             return;
         }
 
-        sDrawingProxy.dismissKeyPreview(key);
-        // Even if the key is disabled, update the key release graphics just in case.
-        updateReleaseKeyGraphics(key);
+        sDrawingProxy.onKeyReleased(key, withAnimation);
 
         if (key.isShift()) {
             for (final Key shiftKey : mKeyboard.mShiftKeys) {
                 if (shiftKey != key) {
-                    updateReleaseKeyGraphics(shiftKey);
+                    sDrawingProxy.onKeyReleased(shiftKey, false /* withAnimation */);
                 }
             }
         }
@@ -403,11 +401,11 @@
             final int altCode = key.getAltCode();
             final Key altKey = mKeyboard.getKey(altCode);
             if (altKey != null) {
-                updateReleaseKeyGraphics(altKey);
+                sDrawingProxy.onKeyReleased(altKey, false /* withAnimation */);
             }
             for (final Key k : mKeyboard.mAltCodeKeysWhileTyping) {
                 if (k != key && k.getAltCode() == altCode) {
-                    updateReleaseKeyGraphics(k);
+                    sDrawingProxy.onKeyReleased(k, false /* withAnimation */);
                 }
             }
         }
@@ -418,7 +416,7 @@
         return sTypingTimeRecorder.needsToSuppressKeyPreviewPopup(eventTime);
     }
 
-    private void setPressedKeyGraphics(final Key key, final long eventTime) {
+    private void setPressedKeyGraphics(@Nullable final Key key, final long eventTime) {
         if (key == null) {
             return;
         }
@@ -430,15 +428,13 @@
             return;
         }
 
-        if (!key.noKeyPreview() && !sInGesture && !needsToSuppressKeyPreviewPopup(eventTime)) {
-            sDrawingProxy.showKeyPreview(key);
-        }
-        updatePressKeyGraphics(key);
+        final boolean noKeyPreview = sInGesture || needsToSuppressKeyPreviewPopup(eventTime);
+        sDrawingProxy.onKeyPressed(key, !noKeyPreview);
 
         if (key.isShift()) {
             for (final Key shiftKey : mKeyboard.mShiftKeys) {
                 if (shiftKey != key) {
-                    updatePressKeyGraphics(shiftKey);
+                    sDrawingProxy.onKeyPressed(shiftKey, false /* withPreview */);
                 }
             }
         }
@@ -447,26 +443,16 @@
             final int altCode = key.getAltCode();
             final Key altKey = mKeyboard.getKey(altCode);
             if (altKey != null) {
-                updatePressKeyGraphics(altKey);
+                sDrawingProxy.onKeyPressed(altKey, false /* withPreview */);
             }
             for (final Key k : mKeyboard.mAltCodeKeysWhileTyping) {
                 if (k != key && k.getAltCode() == altCode) {
-                    updatePressKeyGraphics(k);
+                    sDrawingProxy.onKeyPressed(k, false /* withPreview */);
                 }
             }
         }
     }
 
-    private static void updateReleaseKeyGraphics(final Key key) {
-        key.onReleased();
-        sDrawingProxy.invalidateKey(key);
-    }
-
-    private static void updatePressKeyGraphics(final Key key) {
-        key.onPressed();
-        sDrawingProxy.invalidateKey(key);
-    }
-
     public GestureStrokeDrawingPoints getGestureStrokeDrawingPoints() {
         return mGestureStrokeDrawingPoints;
     }
@@ -837,7 +823,7 @@
     }
 
     private void processDraggingFingerOutFromOldKey(final Key oldKey) {
-        setReleasedKeyGraphics(oldKey);
+        setReleasedKeyGraphics(oldKey, true /* withAnimation */);
         callListenerOnRelease(oldKey, oldKey.getCode(), true /* withSliding */);
         startKeySelectionByDraggingFinger(oldKey);
         sTimerProxy.cancelKeyTimersOf(this);
@@ -880,12 +866,12 @@
             }
             onUpEvent(x, y, eventTime);
             cancelTrackingForAction();
-            setReleasedKeyGraphics(oldKey);
+            setReleasedKeyGraphics(oldKey, true /* withAnimation */);
         } else {
             if (!mIsDetectingGesture) {
                 cancelTrackingForAction();
             }
-            setReleasedKeyGraphics(oldKey);
+            setReleasedKeyGraphics(oldKey, true /* withAnimation */);
         }
     }
 
@@ -913,7 +899,7 @@
             onGestureMoveEvent(x, y, eventTime, true /* isMajorEvent */, newKey);
             if (sInGesture) {
                 mCurrentKey = null;
-                setReleasedKeyGraphics(oldKey);
+                setReleasedKeyGraphics(oldKey, true /* withAnimation */);
                 return;
             }
         }
@@ -978,7 +964,7 @@
         final int currentRepeatingKeyCode = mCurrentRepeatingKeyCode;
         mCurrentRepeatingKeyCode = Constants.NOT_A_CODE;
         // Release the last pressed key.
-        setReleasedKeyGraphics(currentKey);
+        setReleasedKeyGraphics(currentKey, true /* withAnimation */);
 
         if (isShowingMoreKeysPanel()) {
             if (!mIsTrackingForActionDisabled) {
@@ -1015,14 +1001,6 @@
         }
     }
 
-    public void onShowMoreKeysPanel(final MoreKeysPanel panel) {
-        setReleasedKeyGraphics(mCurrentKey);
-        final int translatedX = panel.translateX(mLastX);
-        final int translatedY = panel.translateY(mLastY);
-        panel.onDownEvent(translatedX, translatedY, mPointerId, SystemClock.uptimeMillis());
-        mMoreKeysPanel = panel;
-    }
-
     @Override
     public void cancelTrackingForAction() {
         if (isShowingMoreKeysPanel()) {
@@ -1035,14 +1013,49 @@
         return !mIsTrackingForActionDisabled;
     }
 
-    public void cancelLongPressTimer() {
+    public void onLongPressed() {
         sTimerProxy.cancelLongPressTimersOf(this);
+        if (isShowingMoreKeysPanel()) {
+            return;
+        }
+        final Key key = getKey();
+        if (key == null) {
+            return;
+        }
+        if (key.hasNoPanelAutoMoreKey()) {
+            cancelKeyTracking();
+            final int moreKeyCode = key.getMoreKeys()[0].mCode;
+            sListener.onPressKey(moreKeyCode, 0 /* repeatCont */, true /* isSinglePointer */);
+            sListener.onCodeInput(moreKeyCode, Constants.NOT_A_COORDINATE,
+                    Constants.NOT_A_COORDINATE, false /* isKeyRepeat */);
+            sListener.onReleaseKey(moreKeyCode, false /* withSliding */);
+            return;
+        }
+        final int code = key.getCode();
+        if (code == Constants.CODE_SPACE || code == Constants.CODE_LANGUAGE_SWITCH) {
+            // Long pressing the space key invokes IME switcher dialog.
+            if (sListener.onCustomRequest(Constants.CUSTOM_CODE_SHOW_INPUT_METHOD_PICKER)) {
+                cancelKeyTracking();
+                sListener.onReleaseKey(code, false /* withSliding */);
+                return;
+            }
+        }
+
+        setReleasedKeyGraphics(key, false /* withAnimation */);
+        final MoreKeysPanel moreKeysPanel = sDrawingProxy.showMoreKeysKeyboard(key, this);
+        if (moreKeysPanel == null) {
+            return;
+        }
+        final int translatedX = moreKeysPanel.translateX(mLastX);
+        final int translatedY = moreKeysPanel.translateY(mLastY);
+        moreKeysPanel.onDownEvent(translatedX, translatedY, mPointerId, SystemClock.uptimeMillis());
+        mMoreKeysPanel = moreKeysPanel;
     }
 
-    public void onLongPressed() {
+    private void cancelKeyTracking() {
         resetKeySelectionByDraggingFinger();
         cancelTrackingForAction();
-        setReleasedKeyGraphics(mCurrentKey);
+        setReleasedKeyGraphics(mCurrentKey, true /* withAnimation */);
         sPointerTrackerQueue.remove(this);
     }
 
@@ -1059,7 +1072,7 @@
 
     private void onCancelEventInternal() {
         sTimerProxy.cancelKeyTimersOf(this);
-        setReleasedKeyGraphics(mCurrentKey);
+        setReleasedKeyGraphics(mCurrentKey, true /* withAnimation */);
         resetKeySelectionByDraggingFinger();
         dismissMoreKeysPanel();
     }
diff --git a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java
index ab2323b..228b964 100644
--- a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java
+++ b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java
@@ -28,6 +28,8 @@
 import java.util.Collections;
 import java.util.List;
 
+import javax.annotation.Nonnull;
+
 public class ProximityInfo {
     private static final String TAG = ProximityInfo.class.getSimpleName();
     private static final boolean DEBUG = false;
@@ -36,6 +38,7 @@
     public static final int MAX_PROXIMITY_CHARS_SIZE = 16;
     /** Number of key widths from current touch point to search for nearest keys. */
     private static final float SEARCH_DISTANCE = 1.2f;
+    @Nonnull
     private static final List<Key> EMPTY_KEY_LIST = Collections.emptyList();
     private static final float DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS = 0.15f;
 
@@ -49,13 +52,16 @@
     private final int mKeyboardHeight;
     private final int mMostCommonKeyWidth;
     private final int mMostCommonKeyHeight;
+    @Nonnull
     private final List<Key> mSortedKeys;
+    @Nonnull
     private final List<Key>[] mGridNeighbors;
 
     @SuppressWarnings("unchecked")
     ProximityInfo(final int gridWidth, final int gridHeight, final int minWidth, final int height,
-            final int mostCommonKeyWidth, final int mostCommonKeyHeight, final List<Key> sortedKeys,
-            final TouchPositionCorrection touchPositionCorrection) {
+            final int mostCommonKeyWidth, final int mostCommonKeyHeight,
+            @Nonnull final List<Key> sortedKeys,
+            @Nonnull final TouchPositionCorrection touchPositionCorrection) {
         mGridWidth = gridWidth;
         mGridHeight = gridHeight;
         mGridSize = mGridWidth * mGridHeight;
@@ -104,7 +110,8 @@
         return count;
     }
 
-    private long createNativeProximityInfo(final TouchPositionCorrection touchPositionCorrection) {
+    private long createNativeProximityInfo(
+            @Nonnull final TouchPositionCorrection touchPositionCorrection) {
         final List<Key>[] gridNeighborKeys = mGridNeighbors;
         final int[] proximityCharsArray = new int[mGridSize * MAX_PROXIMITY_CHARS_SIZE];
         Arrays.fill(proximityCharsArray, Constants.NOT_A_CODE);
@@ -163,7 +170,7 @@
             infoIndex++;
         }
 
-        if (touchPositionCorrection != null && touchPositionCorrection.isValid()) {
+        if (touchPositionCorrection.isValid()) {
             if (DEBUG) {
                 Log.d(TAG, "touchPositionCorrection: ON");
             }
@@ -385,10 +392,8 @@
         }
     }
 
+    @Nonnull
     public List<Key> getNearestKeys(final int x, final int y) {
-        if (mGridNeighbors == null) {
-            return EMPTY_KEY_LIST;
-        }
         if (x >= 0 && x < mKeyboardMinWidth && y >= 0 && y < mKeyboardHeight) {
             int index = (y / mCellHeight) * mGridWidth + (x / mCellWidth);
             if (index < mGridSize) {
diff --git a/java/src/com/android/inputmethod/keyboard/emoji/EmojiLayoutParams.java b/java/src/com/android/inputmethod/keyboard/emoji/EmojiLayoutParams.java
index 582e091..2ac7997 100644
--- a/java/src/com/android/inputmethod/keyboard/emoji/EmojiLayoutParams.java
+++ b/java/src/com/android/inputmethod/keyboard/emoji/EmojiLayoutParams.java
@@ -39,7 +39,7 @@
 
     public EmojiLayoutParams(final Resources res) {
         final int defaultKeyboardHeight = ResourceUtils.getDefaultKeyboardHeight(res);
-        final int defaultKeyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
+        final int defaultKeyboardWidth = ResourceUtils.getKeyboardFrameWidth(res);
         mKeyVerticalGap = (int) res.getFraction(R.fraction.config_key_vertical_gap_holo,
                 defaultKeyboardHeight, defaultKeyboardHeight);
         mBottomPadding = (int) res.getFraction(R.fraction.config_keyboard_bottom_padding_holo,
diff --git a/java/src/com/android/inputmethod/keyboard/emoji/EmojiPalettesView.java b/java/src/com/android/inputmethod/keyboard/emoji/EmojiPalettesView.java
index cf4dd3d..223947e 100644
--- a/java/src/com/android/inputmethod/keyboard/emoji/EmojiPalettesView.java
+++ b/java/src/com/android/inputmethod/keyboard/emoji/EmojiPalettesView.java
@@ -50,6 +50,8 @@
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.RichInputMethodSubtype;
 import com.android.inputmethod.latin.common.Constants;
+import com.android.inputmethod.latin.settings.Settings;
+import com.android.inputmethod.latin.settings.SettingsValues;
 import com.android.inputmethod.latin.utils.ResourceUtils;
 
 import java.util.concurrent.TimeUnit;
@@ -113,9 +115,10 @@
                 context, null /* editorInfo */);
         final Resources res = context.getResources();
         mEmojiLayoutParams = new EmojiLayoutParams(res);
+        final SettingsValues s = Settings.getInstance().getCurrent();
         builder.setSubtype(RichInputMethodSubtype.getEmojiSubtype());
-        builder.setKeyboardGeometry(ResourceUtils.getDefaultKeyboardWidth(res),
-                mEmojiLayoutParams.mEmojiKeyboardHeight);
+        builder.setKeyboardGeometry(ResourceUtils.getKeyboardWidth(res, s),
+                ResourceUtils.getKeyboardHeight(res, s));
         final KeyboardLayoutSet layoutSet = builder.build();
         final TypedArray emojiPalettesViewAttr = context.obtainStyledAttributes(attrs,
                 R.styleable.EmojiPalettesView, defStyle, R.style.EmojiPalettesView);
@@ -140,7 +143,7 @@
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         final Resources res = getContext().getResources();
         // The main keyboard expands to the entire this {@link KeyboardView}.
-        final int width = ResourceUtils.getDefaultKeyboardWidth(res)
+        final int width = ResourceUtils.getKeyboardFrameWidth(res)
                 + getPaddingLeft() + getPaddingRight();
         final int height = ResourceUtils.getDefaultKeyboardHeight(res)
                 + res.getDimensionPixelSize(R.dimen.config_suggestions_strip_height)
diff --git a/java/src/com/android/inputmethod/keyboard/internal/DrawingProxy.java b/java/src/com/android/inputmethod/keyboard/internal/DrawingProxy.java
index 7fc586a..06bdfc4 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/DrawingProxy.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/DrawingProxy.java
@@ -17,29 +17,36 @@
 package com.android.inputmethod.keyboard.internal;
 
 import com.android.inputmethod.keyboard.Key;
+import com.android.inputmethod.keyboard.MoreKeysPanel;
 import com.android.inputmethod.keyboard.PointerTracker;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 public interface DrawingProxy {
-    // TODO: Remove this method.
-    public void invalidateKey(@Nullable Key key);
-
-    // TODO: Rename this method to onKeyPressed.
-    public void showKeyPreview(@Nonnull Key key);
-
-    // TODO: Rename this method to onKeyReleased.
-    public void dismissKeyPreview(@Nonnull Key key);
+    /**
+     * Called when a key is being pressed.
+     * @param key the {@link Key} that is being pressed.
+     * @param withPreview true if key popup preview should be displayed.
+     */
+    public void onKeyPressed(@Nonnull Key key, boolean withPreview);
 
     /**
-     * Dismiss a key preview visual without delay.
-     * @param key the key whose preview visual should be dismissed.
+     * Called when a key is being released.
+     * @param key the {@link Key} that is being released.
+     * @param withAnimation when true, key popup preview should be dismissed with animation.
      */
-    public void dismissKeyPreviewWithoutDelay(@Nonnull Key key);
+    public void onKeyReleased(@Nonnull Key key, boolean withAnimation);
 
-    // TODO: Rename this method to onKeyLongPressed.
-    public void onLongPress(@Nonnull PointerTracker tracker);
+    /**
+     * Start showing more keys keyboard of a key that is being long pressed.
+     * @param key the {@link Key} that is being long pressed and showing more keys keyboard.
+     * @param tracker the {@link PointerTracker} that detects this long pressing.
+     * @return {@link MoreKeysPanel} that is being shown. null if there is no need to show more keys
+     *     keyboard.
+     */
+    @Nullable
+    public MoreKeysPanel showMoreKeysKeyboard(@Nonnull Key key, @Nonnull PointerTracker tracker);
 
     /**
      * Start a while-typing-animation.
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java b/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java
index df50efd..3ef9ea1 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java
@@ -20,8 +20,12 @@
 
 import com.android.inputmethod.latin.utils.ResourceUtils;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 public final class KeyDrawParams {
-    public Typeface mTypeface;
+    @Nonnull
+    public Typeface mTypeface = Typeface.DEFAULT;
 
     public int mLetterSize;
     public int mLabelSize;
@@ -49,7 +53,7 @@
 
     public KeyDrawParams() {}
 
-    private KeyDrawParams(final KeyDrawParams copyFrom) {
+    private KeyDrawParams(@Nonnull final KeyDrawParams copyFrom) {
         mTypeface = copyFrom.mTypeface;
 
         mLetterSize = copyFrom.mLetterSize;
@@ -77,7 +81,7 @@
         mAnimAlpha = copyFrom.mAnimAlpha;
     }
 
-    public void updateParams(final int keyHeight, final KeyVisualAttributes attr) {
+    public void updateParams(final int keyHeight, @Nullable final KeyVisualAttributes attr) {
         if (attr == null) {
             return;
         }
@@ -117,8 +121,9 @@
                 attr.mHintLabelOffCenterRatio, mHintLabelOffCenterRatio);
     }
 
+    @Nonnull
     public KeyDrawParams mayCloneAndUpdateParams(final int keyHeight,
-            final KeyVisualAttributes attr) {
+            @Nullable final KeyVisualAttributes attr) {
         if (attr == null) {
             return this;
         }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java
index 63aab96..3eb62e7 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java
@@ -22,6 +22,9 @@
 import com.android.inputmethod.latin.common.Constants;
 import com.android.inputmethod.latin.common.StringUtils;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 /**
  * The string parser of the key specification.
  *
@@ -53,11 +56,11 @@
         // Intentional empty constructor for utility class.
     }
 
-    private static boolean hasIcon(final String keySpec) {
+    private static boolean hasIcon(@Nonnull final String keySpec) {
         return keySpec.startsWith(KeyboardIconsSet.PREFIX_ICON);
     }
 
-    private static boolean hasCode(final String keySpec, final int labelEnd) {
+    private static boolean hasCode(@Nonnull final String keySpec, final int labelEnd) {
         if (labelEnd <= 0 || labelEnd + 1 >= keySpec.length()) {
             return false;
         }
@@ -72,7 +75,8 @@
         return false;
     }
 
-    private static String parseEscape(final String text) {
+    @Nonnull
+    private static String parseEscape(@Nonnull final String text) {
         if (text.indexOf(BACKSLASH) < 0) {
             return text;
         }
@@ -91,7 +95,7 @@
         return sb.toString();
     }
 
-    private static int indexOfLabelEnd(final String keySpec) {
+    private static int indexOfLabelEnd(@Nonnull final String keySpec) {
         final int length = keySpec.length();
         if (keySpec.indexOf(BACKSLASH) < 0) {
             final int labelEnd = keySpec.indexOf(VERTICAL_BAR);
@@ -116,22 +120,25 @@
         return -1;
     }
 
-    private static String getBeforeLabelEnd(final String keySpec, final int labelEnd) {
+    @Nonnull
+    private static String getBeforeLabelEnd(@Nonnull final String keySpec, final int labelEnd) {
         return (labelEnd < 0) ? keySpec : keySpec.substring(0, labelEnd);
     }
 
-    private static String getAfterLabelEnd(final String keySpec, final int labelEnd) {
+    @Nonnull
+    private static String getAfterLabelEnd(@Nonnull final String keySpec, final int labelEnd) {
         return keySpec.substring(labelEnd + /* VERTICAL_BAR */1);
     }
 
-    private static void checkDoubleLabelEnd(final String keySpec, final int labelEnd) {
+    private static void checkDoubleLabelEnd(@Nonnull final String keySpec, final int labelEnd) {
         if (indexOfLabelEnd(getAfterLabelEnd(keySpec, labelEnd)) < 0) {
             return;
         }
         throw new KeySpecParserError("Multiple " + VERTICAL_BAR + ": " + keySpec);
     }
 
-    public static String getLabel(final String keySpec) {
+    @Nullable
+    public static String getLabel(@Nullable final String keySpec) {
         if (keySpec == null) {
             // TODO: Throw {@link KeySpecParserError} once Key.keyLabel attribute becomes mandatory.
             return null;
@@ -147,7 +154,8 @@
         return label;
     }
 
-    private static String getOutputTextInternal(final String keySpec, final int labelEnd) {
+    @Nullable
+    private static String getOutputTextInternal(@Nonnull final String keySpec, final int labelEnd) {
         if (labelEnd <= 0) {
             return null;
         }
@@ -155,7 +163,8 @@
         return parseEscape(getAfterLabelEnd(keySpec, labelEnd));
     }
 
-    public static String getOutputText(final String keySpec) {
+    @Nullable
+    public static String getOutputText(@Nullable final String keySpec) {
         if (keySpec == null) {
             // TODO: Throw {@link KeySpecParserError} once Key.keyLabel attribute becomes mandatory.
             return null;
@@ -184,7 +193,7 @@
         return (StringUtils.codePointCount(label) == 1) ? null : label;
     }
 
-    public static int getCode(final String keySpec) {
+    public static int getCode(@Nullable final String keySpec) {
         if (keySpec == null) {
             // TODO: Throw {@link KeySpecParserError} once Key.keyLabel attribute becomes mandatory.
             return CODE_UNSPECIFIED;
@@ -211,7 +220,7 @@
         return (StringUtils.codePointCount(label) == 1) ? label.codePointAt(0) : CODE_OUTPUT_TEXT;
     }
 
-    public static int parseCode(final String text, final int defaultCode) {
+    public static int parseCode(@Nullable final String text, final int defaultCode) {
         if (text == null) {
             return defaultCode;
         }
@@ -226,7 +235,7 @@
         return defaultCode;
     }
 
-    public static int getIconId(final String keySpec) {
+    public static int getIconId(@Nullable final String keySpec) {
         if (keySpec == null) {
             // TODO: Throw {@link KeySpecParserError} once Key.keyLabel attribute becomes mandatory.
             return KeyboardIconsSet.ICON_UNDEFINED;
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyStyle.java b/java/src/com/android/inputmethod/keyboard/internal/KeyStyle.java
index 7941ddd..28aa22c 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyStyle.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyStyle.java
@@ -18,18 +18,22 @@
 
 import android.content.res.TypedArray;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 public abstract class KeyStyle {
     private final KeyboardTextsSet mTextsSet;
 
-    public abstract String[] getStringArray(TypedArray a, int index);
-    public abstract String getString(TypedArray a, int index);
+    public abstract @Nullable String[] getStringArray(TypedArray a, int index);
+    public abstract @Nullable String getString(TypedArray a, int index);
     public abstract int getInt(TypedArray a, int index, int defaultValue);
     public abstract int getFlags(TypedArray a, int index);
 
-    protected KeyStyle(final KeyboardTextsSet textsSet) {
+    protected KeyStyle(@Nonnull final KeyboardTextsSet textsSet) {
         mTextsSet = textsSet;
     }
 
+    @Nullable
     protected String parseString(final TypedArray a, final int index) {
         if (a.hasValue(index)) {
             return mTextsSet.resolveTextReference(a.getString(index));
@@ -37,6 +41,7 @@
         return null;
     }
 
+    @Nullable
     protected String[] parseStringArray(final TypedArray a, final int index) {
         if (a.hasValue(index)) {
             final String text = mTextsSet.resolveTextReference(a.getString(index));
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java
index 5cbb341..61f98c8 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyStylesSet.java
@@ -29,33 +29,42 @@
 import java.util.Arrays;
 import java.util.HashMap;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 public final class KeyStylesSet {
     private static final String TAG = KeyStylesSet.class.getSimpleName();
     private static final boolean DEBUG = false;
 
+    @Nonnull
     private final HashMap<String, KeyStyle> mStyles = new HashMap<>();
 
+    @Nonnull
     private final KeyboardTextsSet mTextsSet;
+    @Nonnull
     private final KeyStyle mEmptyKeyStyle;
+    @Nonnull
     private static final String EMPTY_STYLE_NAME = "<empty>";
 
-    public KeyStylesSet(final KeyboardTextsSet textsSet) {
+    public KeyStylesSet(@Nonnull final KeyboardTextsSet textsSet) {
         mTextsSet = textsSet;
         mEmptyKeyStyle = new EmptyKeyStyle(textsSet);
         mStyles.put(EMPTY_STYLE_NAME, mEmptyKeyStyle);
     }
 
     private static final class EmptyKeyStyle extends KeyStyle {
-        EmptyKeyStyle(final KeyboardTextsSet textsSet) {
+        EmptyKeyStyle(@Nonnull final KeyboardTextsSet textsSet) {
             super(textsSet);
         }
 
         @Override
+        @Nullable
         public String[] getStringArray(final TypedArray a, final int index) {
             return parseStringArray(a, index);
         }
 
         @Override
+        @Nullable
         public String getString(final TypedArray a, final int index) {
             return parseString(a, index);
         }
@@ -76,14 +85,16 @@
         private final String mParentStyleName;
         private final SparseArray<Object> mStyleAttributes = new SparseArray<>();
 
-        public DeclaredKeyStyle(final String parentStyleName, final KeyboardTextsSet textsSet,
-                final HashMap<String, KeyStyle> styles) {
+        public DeclaredKeyStyle(@Nonnull final String parentStyleName,
+                @Nonnull final KeyboardTextsSet textsSet,
+                @Nonnull final HashMap<String, KeyStyle> styles) {
             super(textsSet);
             mParentStyleName = parentStyleName;
             mStyles = styles;
         }
 
         @Override
+        @Nullable
         public String[] getStringArray(final TypedArray a, final int index) {
             if (a.hasValue(index)) {
                 return parseStringArray(a, index);
@@ -98,6 +109,7 @@
         }
 
         @Override
+        @Nullable
         public String getString(final TypedArray a, final int index) {
             if (a.hasValue(index)) {
                 return parseString(a, index);
@@ -176,37 +188,43 @@
     public void parseKeyStyleAttributes(final TypedArray keyStyleAttr, final TypedArray keyAttrs,
             final XmlPullParser parser) throws XmlPullParserException {
         final String styleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_styleName);
+        if (styleName == null) {
+            throw new XmlParseUtils.ParseException(
+                    KeyboardBuilder.TAG_KEY_STYLE + " has no styleName attribute", parser);
+        }
         if (DEBUG) {
             Log.d(TAG, String.format("<%s styleName=%s />",
                     KeyboardBuilder.TAG_KEY_STYLE, styleName));
             if (mStyles.containsKey(styleName)) {
-                Log.d(TAG, "key-style " + styleName + " is overridden at "
+                Log.d(TAG, KeyboardBuilder.TAG_KEY_STYLE + " " + styleName + " is overridden at "
                         + parser.getPositionDescription());
             }
         }
 
-        String parentStyleName = EMPTY_STYLE_NAME;
-        if (keyStyleAttr.hasValue(R.styleable.Keyboard_KeyStyle_parentStyle)) {
-            parentStyleName = keyStyleAttr.getString(R.styleable.Keyboard_KeyStyle_parentStyle);
-            if (!mStyles.containsKey(parentStyleName)) {
-                throw new XmlParseUtils.ParseException(
-                        "Unknown parentStyle " + parentStyleName, parser);
-            }
+        final String parentStyleInAttr = keyStyleAttr.getString(
+                R.styleable.Keyboard_KeyStyle_parentStyle);
+        if (parentStyleInAttr != null && !mStyles.containsKey(parentStyleInAttr)) {
+            throw new XmlParseUtils.ParseException(
+                    "Unknown parentStyle " + parentStyleInAttr, parser);
         }
+        final String parentStyleName = (parentStyleInAttr == null) ? EMPTY_STYLE_NAME
+                : parentStyleInAttr;
         final DeclaredKeyStyle style = new DeclaredKeyStyle(parentStyleName, mTextsSet, mStyles);
         style.readKeyAttributes(keyAttrs);
         mStyles.put(styleName, style);
     }
 
+    @Nonnull
     public KeyStyle getKeyStyle(final TypedArray keyAttr, final XmlPullParser parser)
             throws XmlParseUtils.ParseException {
-        if (!keyAttr.hasValue(R.styleable.Keyboard_Key_keyStyle)) {
+        final String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle);
+        if (styleName == null) {
             return mEmptyKeyStyle;
         }
-        final String styleName = keyAttr.getString(R.styleable.Keyboard_Key_keyStyle);
-        if (!mStyles.containsKey(styleName)) {
+        final KeyStyle style = mStyles.get(styleName);
+        if (style == null) {
             throw new XmlParseUtils.ParseException("Unknown key style: " + styleName, parser);
         }
-        return mStyles.get(styleName);
+        return style;
     }
 }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyVisualAttributes.java b/java/src/com/android/inputmethod/keyboard/internal/KeyVisualAttributes.java
index c60d587..6f000d2 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyVisualAttributes.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyVisualAttributes.java
@@ -23,7 +23,11 @@
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.utils.ResourceUtils;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 public final class KeyVisualAttributes {
+    @Nullable
     public final Typeface mTypeface;
 
     public final float mLetterRatio;
@@ -81,7 +85,8 @@
         }
     }
 
-    public static KeyVisualAttributes newInstance(final TypedArray keyAttr) {
+    @Nullable
+    public static KeyVisualAttributes newInstance(@Nonnull final TypedArray keyAttr) {
         final int indexCount = keyAttr.getIndexCount();
         for (int i = 0; i < indexCount; i++) {
             final int attrId = keyAttr.getIndex(i);
@@ -93,7 +98,7 @@
         return null;
     }
 
-    private KeyVisualAttributes(final TypedArray keyAttr) {
+    private KeyVisualAttributes(@Nonnull final TypedArray keyAttr) {
         if (keyAttr.hasValue(R.styleable.Keyboard_Key_keyTypeface)) {
             mTypeface = Typeface.defaultFromStyle(
                     keyAttr.getInt(R.styleable.Keyboard_Key_keyTypeface, Typeface.NORMAL));
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
index 51f89c1..2b07e1d 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
@@ -139,6 +139,7 @@
     private static final int DEFAULT_KEYBOARD_COLUMNS = 10;
     private static final int DEFAULT_KEYBOARD_ROWS = 4;
 
+    @Nonnull
     protected final KP mParams;
     protected final Context mContext;
     protected final Resources mResources;
@@ -149,7 +150,7 @@
     private boolean mTopEdge;
     private Key mRightEdgeKey = null;
 
-    public KeyboardBuilder(final Context context, final KP params) {
+    public KeyboardBuilder(final Context context, @Nonnull final KP params) {
         mContext = context;
         final Resources res = context.getResources();
         mResources = res;
@@ -160,10 +161,6 @@
         params.GRID_HEIGHT = res.getInteger(R.integer.config_keyboard_grid_height);
     }
 
-    public void setAutoGenerate(final KeysCache keysCache) {
-        mParams.mKeysCache = keysCache;
-    }
-
     public void setAllowRedundantMoreKes(final boolean enabled) {
         mParams.mAllowRedundantMoreKeys = enabled;
     }
@@ -194,6 +191,7 @@
         mParams.mProximityCharsCorrectionEnabled = enabled;
     }
 
+    @Nonnull
     public Keyboard build() {
         return new Keyboard(mParams);
     }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java
index e1f302c..15a5bd4 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java
@@ -26,6 +26,9 @@
 
 import java.util.HashMap;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 public final class KeyboardIconsSet {
     private static final String TAG = KeyboardIconsSet.class.getSimpleName();
 
@@ -127,6 +130,7 @@
         return iconId >= 0 && iconId < ICON_NAMES.length;
     }
 
+    @Nonnull
     public static String getIconName(final int iconId) {
         return isValidIconId(iconId) ? ICON_NAMES[iconId] : "unknown<" + iconId + ">";
     }
@@ -147,6 +151,7 @@
         throw new RuntimeException("unknown icon name: " + name);
     }
 
+    @Nullable
     public Drawable getIconDrawable(final int iconId) {
         if (isValidIconId(iconId)) {
             return mIcons[iconId];
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardParams.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParams.java
index fb5e977..738d6a4 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardParams.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParams.java
@@ -49,6 +49,7 @@
     public int mLeftPadding;
     public int mRightPadding;
 
+    @Nullable
     public KeyVisualAttributes mKeyVisualAttributes;
 
     public int mDefaultRowHeight;
@@ -63,14 +64,21 @@
     public int GRID_HEIGHT;
 
     // Keys are sorted from top-left to bottom-right order.
+    @Nonnull
     public final SortedSet<Key> mSortedKeys = new TreeSet<>(ROW_COLUMN_COMPARATOR);
+    @Nonnull
     public final ArrayList<Key> mShiftKeys = new ArrayList<>();
+    @Nonnull
     public final ArrayList<Key> mAltCodeKeysWhileTyping = new ArrayList<>();
+    @Nonnull
     public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet();
+    @Nonnull
     public final KeyboardTextsSet mTextsSet = new KeyboardTextsSet();
+    @Nonnull
     public final KeyStylesSet mKeyStyles = new KeyStylesSet(mTextsSet);
 
-    @Nullable public KeysCache mKeysCache;
+    @Nonnull
+    private final UniqueKeysCache mUniqueKeysCache;
     public boolean mAllowRedundantMoreKeys;
 
     public int mMostCommonKeyHeight = 0;
@@ -78,6 +86,7 @@
 
     public boolean mProximityCharsCorrectionEnabled;
 
+    @Nonnull
     public final TouchPositionCorrection mTouchPositionCorrection =
             new TouchPositionCorrection();
 
@@ -93,6 +102,14 @@
         }
     };
 
+    public KeyboardParams() {
+        this(UniqueKeysCache.NO_CACHE);
+    }
+
+    public KeyboardParams(@Nonnull final UniqueKeysCache keysCache) {
+        mUniqueKeysCache = keysCache;
+    }
+
     protected void clearKeys() {
         mSortedKeys.clear();
         mShiftKeys.clear();
@@ -100,7 +117,7 @@
     }
 
     public void onAddKey(@Nonnull final Key newKey) {
-        final Key key = (mKeysCache != null) ? mKeysCache.get(newKey) : newKey;
+        final Key key = mUniqueKeysCache.getUniqueKey(newKey);
         final boolean isSpacer = key.isSpacer();
         if (isSpacer && key.getWidth() == 0) {
             // Ignore zero width {@link Spacer}.
@@ -132,10 +149,7 @@
         mSortedKeys.clear();
         for (final Key key : allKeys) {
             final Key filteredKey = Key.removeRedundantMoreKeys(key, lettersOnBaseLayout);
-            if (mKeysCache != null) {
-                mKeysCache.replace(key, filteredKey);
-            }
-            mSortedKeys.add(filteredKey);
+            mSortedKeys.add(mUniqueKeysCache.getUniqueKey(filteredKey));
         }
     }
 
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeysCache.java b/java/src/com/android/inputmethod/keyboard/internal/KeysCache.java
deleted file mode 100644
index e867863..0000000
--- a/java/src/com/android/inputmethod/keyboard/internal/KeysCache.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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.keyboard.internal;
-
-import com.android.inputmethod.keyboard.Key;
-
-import java.util.HashMap;
-
-public final class KeysCache {
-    private final HashMap<Key, Key> mMap = new HashMap<>();
-
-    public void clear() {
-        mMap.clear();
-    }
-
-    public Key get(final Key key) {
-        final Key existingKey = mMap.get(key);
-        if (existingKey != null) {
-            // Reuse the existing element that equals to "key" without adding "key" to the map.
-            return existingKey;
-        }
-        mMap.put(key, key);
-        return key;
-    }
-
-    public Key replace(final Key oldKey, final Key newKey) {
-        if (oldKey.equals(newKey)) {
-            return oldKey;
-        }
-        mMap.remove(oldKey);
-        return get(newKey);
-    }
-}
diff --git a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java
index 87c96cc..0bd42fc 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java
@@ -24,14 +24,13 @@
 import com.android.inputmethod.latin.common.CollectionUtils;
 import com.android.inputmethod.latin.common.Constants;
 import com.android.inputmethod.latin.common.StringUtils;
-import com.android.inputmethod.latin.define.DebugFlags;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Locale;
 
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 
 /**
  * The more key specification object. The more keys are an array of {@link MoreKeySpec}.
@@ -47,12 +46,15 @@
 // TODO: Should extend the key specification object.
 public final class MoreKeySpec {
     public final int mCode;
+    @Nullable
     public final String mLabel;
+    @Nullable
     public final String mOutputText;
     public final int mIconId;
 
-    public MoreKeySpec(final String moreKeySpec, boolean needsToUpperCase, final Locale locale) {
-        if (TextUtils.isEmpty(moreKeySpec)) {
+    public MoreKeySpec(@Nonnull final String moreKeySpec, boolean needsToUpperCase,
+            @Nonnull final Locale locale) {
+        if (moreKeySpec.isEmpty()) {
             throw new KeySpecParser.KeySpecParserError("Empty more key spec");
         }
         final String label = KeySpecParser.getLabel(moreKeySpec);
@@ -76,7 +78,7 @@
 
     @Nonnull
     public Key buildKey(final int x, final int y, final int labelFlags,
-            final KeyboardParams params) {
+            @Nonnull final KeyboardParams params) {
         return new Key(mLabel, mIconId, mCode, mOutputText, null /* hintLabel */, labelFlags,
                 Key.BACKGROUND_TYPE_NORMAL, x, y, params.mDefaultKeyWidth, params.mDefaultRowHeight,
                 params.mHorizontalGap, params.mVerticalGap);
@@ -87,14 +89,18 @@
         int hashCode = 1;
         hashCode = 31 + mCode;
         hashCode = hashCode * 31 + mIconId;
-        hashCode = hashCode * 31 + (mLabel == null ? 0 : mLabel.hashCode());
-        hashCode = hashCode * 31 + (mOutputText == null ? 0 : mOutputText.hashCode());
+        final String label = mLabel;
+        hashCode = hashCode * 31 + (label == null ? 0 : label.hashCode());
+        final String outputText = mOutputText;
+        hashCode = hashCode * 31 + (outputText == null ? 0 : outputText.hashCode());
         return hashCode;
     }
 
     @Override
     public boolean equals(final Object o) {
-        if (this == o) return true;
+        if (this == o) {
+            return true;
+        }
         if (o instanceof MoreKeySpec) {
             final MoreKeySpec other = (MoreKeySpec)o;
             return mCode == other.mCode
@@ -121,7 +127,7 @@
         private final SparseIntArray mCodes = new SparseIntArray();
         private final HashSet<String> mTexts = new HashSet<>();
 
-        public void addLetter(final Key key) {
+        public void addLetter(@Nonnull final Key key) {
             final int code = key.getCode();
             if (CharacterCompat.isAlphabetic(code)) {
                 mCodes.put(code, 0);
@@ -130,7 +136,7 @@
             }
         }
 
-        public boolean contains(final MoreKeySpec moreKey) {
+        public boolean contains(@Nonnull final MoreKeySpec moreKey) {
             final int code = moreKey.mCode;
             if (CharacterCompat.isAlphabetic(code) && mCodes.indexOfKey(code) >= 0) {
                 return true;
@@ -141,8 +147,9 @@
         }
     }
 
-    public static MoreKeySpec[] removeRedundantMoreKeys(final MoreKeySpec[] moreKeys,
-            final LettersOnBaseLayout lettersOnBaseLayout) {
+    @Nullable
+    public static MoreKeySpec[] removeRedundantMoreKeys(@Nullable final MoreKeySpec[] moreKeys,
+            @Nonnull final LettersOnBaseLayout lettersOnBaseLayout) {
         if (moreKeys == null) {
             return null;
         }
@@ -162,7 +169,6 @@
         return filteredMoreKeys.toArray(new MoreKeySpec[size]);
     }
 
-    private static final boolean DEBUG = DebugFlags.DEBUG_ENABLED;
     // Constants for parsing.
     private static final char COMMA = Constants.CODE_COMMA;
     private static final char BACKSLASH = Constants.CODE_BACKSLASH;
@@ -180,7 +186,8 @@
      * @return an array of key specification text. Null if the specified <code>text</code> is empty
      * or has no key specifications.
      */
-    public static String[] splitKeySpecs(final String text) {
+    @Nullable
+    public static String[] splitKeySpecs(@Nullable final String text) {
         if (TextUtils.isEmpty(text)) {
             return null;
         }
@@ -222,9 +229,11 @@
         return list.toArray(new String[list.size()]);
     }
 
+    @Nonnull
     private static final String[] EMPTY_STRING_ARRAY = new String[0];
 
-    private static String[] filterOutEmptyString(final String[] array) {
+    @Nonnull
+    private static String[] filterOutEmptyString(@Nullable final String[] array) {
         if (array == null) {
             return EMPTY_STRING_ARRAY;
         }
@@ -245,8 +254,8 @@
         return out.toArray(new String[out.size()]);
     }
 
-    public static String[] insertAdditionalMoreKeys(final String[] moreKeySpecs,
-            final String[] additionalMoreKeySpecs) {
+    public static String[] insertAdditionalMoreKeys(@Nullable final String[] moreKeySpecs,
+            @Nullable final String[] additionalMoreKeySpecs) {
         final String[] moreKeys = filterOutEmptyString(moreKeySpecs);
         final String[] additionalMoreKeys = filterOutEmptyString(additionalMoreKeySpecs);
         final int moreKeysCount = moreKeys.length;
@@ -280,11 +289,6 @@
         if (additionalCount > 0 && additionalIndex == 0) {
             // No '%' marker is found in more keys.
             // Insert all additional more keys to the head of more keys.
-            if (DEBUG && out != null) {
-                throw new RuntimeException("Internal logic error:"
-                        + " moreKeys=" + Arrays.toString(moreKeys)
-                        + " additionalMoreKeys=" + Arrays.toString(additionalMoreKeys));
-            }
             out = CollectionUtils.arrayAsList(additionalMoreKeys, additionalIndex, additionalCount);
             for (int i = 0; i < moreKeysCount; i++) {
                 out.add(moreKeys[i]);
@@ -292,11 +296,6 @@
         } else if (additionalIndex < additionalCount) {
             // The number of '%' markers are less than additional more keys.
             // Append remained additional more keys to the tail of more keys.
-            if (DEBUG && out != null) {
-                throw new RuntimeException("Internal logic error:"
-                        + " moreKeys=" + Arrays.toString(moreKeys)
-                        + " additionalMoreKeys=" + Arrays.toString(additionalMoreKeys));
-            }
             out = CollectionUtils.arrayAsList(moreKeys, 0, moreKeysCount);
             for (int i = additionalIndex; i < additionalCount; i++) {
                 out.add(additionalMoreKeys[additionalIndex]);
@@ -311,7 +310,7 @@
         }
     }
 
-    public static int getIntValue(final String[] moreKeys, final String key,
+    public static int getIntValue(@Nullable final String[] moreKeys, final String key,
             final int defaultValue) {
         if (moreKeys == null) {
             return defaultValue;
@@ -338,7 +337,7 @@
         return value;
     }
 
-    public static boolean getBooleanValue(final String[] moreKeys, final String key) {
+    public static boolean getBooleanValue(@Nullable final String[] moreKeys, final String key) {
         if (moreKeys == null) {
             return false;
         }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/TimerHandler.java b/java/src/com/android/inputmethod/keyboard/internal/TimerHandler.java
index 8068427..91f3558 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/TimerHandler.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/TimerHandler.java
@@ -66,7 +66,7 @@
         case MSG_LONGPRESS_SHIFT_KEY:
             cancelLongPressTimers();
             final PointerTracker tracker2 = (PointerTracker) msg.obj;
-            drawingProxy.onLongPress(tracker2);
+            tracker2.onLongPressed();
             break;
         case MSG_UPDATE_BATCH_INPUT:
             final PointerTracker tracker3 = (PointerTracker) msg.obj;
@@ -74,8 +74,7 @@
             startUpdateBatchInputTimer(tracker3);
             break;
         case MSG_DISMISS_KEY_PREVIEW:
-            final Key key = (Key) msg.obj;
-            drawingProxy.dismissKeyPreviewWithoutDelay(key);
+            drawingProxy.onKeyReleased((Key) msg.obj, false /* withAnimation */);
             break;
         case MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT:
             drawingProxy.dismissGestureFloatingPreviewTextWithoutDelay();
diff --git a/java/src/com/android/inputmethod/keyboard/internal/UniqueKeysCache.java b/java/src/com/android/inputmethod/keyboard/internal/UniqueKeysCache.java
new file mode 100644
index 0000000..5b329dc
--- /dev/null
+++ b/java/src/com/android/inputmethod/keyboard/internal/UniqueKeysCache.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2014 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.keyboard.internal;
+
+import com.android.inputmethod.keyboard.Key;
+
+import java.util.HashMap;
+
+import javax.annotation.Nonnull;
+
+public abstract class UniqueKeysCache {
+    public abstract void setEnabled(boolean enabled);
+    public abstract void clear();
+    public abstract @Nonnull Key getUniqueKey(@Nonnull Key key);
+
+    @Nonnull
+    public static final UniqueKeysCache NO_CACHE = new UniqueKeysCache() {
+        @Override
+        public void setEnabled(boolean enabled) {}
+
+        @Override
+        public void clear() {}
+
+        @Override
+        public Key getUniqueKey(Key key) { return key; }
+    };
+
+    @Nonnull
+    public static UniqueKeysCache newInstance() {
+        return new UniqueKeysCacheImpl();
+    }
+
+    private static final class UniqueKeysCacheImpl extends UniqueKeysCache {
+        private final HashMap<Key, Key> mCache;
+
+        private boolean mEnabled;
+
+        UniqueKeysCacheImpl() {
+            mCache = new HashMap<>();
+        }
+
+        @Override
+        public void setEnabled(final boolean enabled) {
+            mEnabled = enabled;
+        }
+
+        @Override
+        public void clear() {
+            mCache.clear();
+        }
+
+        @Override
+        public Key getUniqueKey(final Key key) {
+            if (!mEnabled) {
+                return key;
+            }
+            final Key existingKey = mCache.get(key);
+            if (existingKey != null) {
+                // Reuse the existing object that equals to "key" without adding "key" to
+                // the cache.
+                return existingKey;
+            }
+            mCache.put(key, key);
+            return key;
+        }
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
index 5afb62b..9c70cad 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java
@@ -263,7 +263,8 @@
     public static ArrayList<AssetFileAddress> getDictionaryFiles(final Locale locale,
             final Context context) {
 
-        final boolean hasDefaultWordList = DictionaryFactory.isDictionaryAvailable(context, locale);
+        final boolean hasDefaultWordList = DictionaryInfoUtils.isDictionaryAvailable(
+                context, locale);
         BinaryDictionaryFileDumper.cacheWordListsFromContentProvider(locale, context,
                 hasDefaultWordList);
         final File[] cachedWordLists = getCachedWordLists(locale.toString(), context);
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java
index b24fdea..acf9cf1 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java
@@ -63,6 +63,9 @@
     // HACK: This threshold is being used when adding a capitalized entry in the User History
     // dictionary.
     private static final int CAPITALIZED_FORM_MAX_PROBABILITY_FOR_INSERT = 140;
+    // How many words we need to type in a row ({@see mConfidenceInMostProbableLanguage}) to
+    // declare we are confident the user is typing in the most probable language.
+    private static final int CONFIDENCE_THRESHOLD = 3;
 
     private DictionaryGroup[] mDictionaryGroups = new DictionaryGroup[] { new DictionaryGroup() };
     private DictionaryGroup mMostProbableDictionaryGroup = mDictionaryGroups[0];
@@ -138,6 +141,10 @@
 
         public final Locale mLocale;
         private Dictionary mMainDict;
+        // Confidence that the most probable language is actually the language the user is
+        // typing in. For now, this is simply the number of times a word from this language
+        // has been committed in a row.
+        private int mConfidence = 0;
         public float mWeightForTypingInLocale = WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
         public float mWeightForGesturingInLocale = WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
         public final ConcurrentHashMap<String, ExpandableBinaryDictionary> mSubDictMap =
@@ -257,11 +264,12 @@
         return mMostProbableDictionaryGroup;
     }
 
-    public void switchMostProbableLanguage(final Locale locale) {
+    public void switchMostProbableLanguage(@Nullable final Locale locale) {
         if (null == locale) {
             // In many cases, there is no locale to a committed word. For example, a typed word
-            // that does not auto-correct has no locale. In this case we simply do not change
-            // the most probable language.
+            // that is in none of the currently active dictionaries but still does not
+            // auto-correct to anything has no locale. In this case we simply do not change
+            // the most probable language and do not touch confidence.
             return;
         }
         final DictionaryGroup newMostProbableDictionaryGroup =
@@ -272,15 +280,31 @@
             // facilitator any more. In this case, just not changing things is fine.
             return;
         }
-        mMostProbableDictionaryGroup.mWeightForTypingInLocale =
-                DictionaryGroup.WEIGHT_FOR_TYPING_IN_NOT_MOST_PROBABLE_LANGUAGE;
-        mMostProbableDictionaryGroup.mWeightForGesturingInLocale =
-                DictionaryGroup.WEIGHT_FOR_GESTURING_IN_NOT_MOST_PROBABLE_LANGUAGE;
-        newMostProbableDictionaryGroup.mWeightForTypingInLocale =
-                DictionaryGroup.WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
-        newMostProbableDictionaryGroup.mWeightForGesturingInLocale =
-                DictionaryGroup.WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
-        mMostProbableDictionaryGroup = newMostProbableDictionaryGroup;
+        if (newMostProbableDictionaryGroup == mMostProbableDictionaryGroup) {
+            ++newMostProbableDictionaryGroup.mConfidence;
+        } else {
+            mMostProbableDictionaryGroup.mWeightForTypingInLocale =
+                    DictionaryGroup.WEIGHT_FOR_TYPING_IN_NOT_MOST_PROBABLE_LANGUAGE;
+            mMostProbableDictionaryGroup.mWeightForGesturingInLocale =
+                    DictionaryGroup.WEIGHT_FOR_GESTURING_IN_NOT_MOST_PROBABLE_LANGUAGE;
+            mMostProbableDictionaryGroup.mConfidence = 0;
+            newMostProbableDictionaryGroup.mWeightForTypingInLocale =
+                    DictionaryGroup.WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
+            newMostProbableDictionaryGroup.mWeightForGesturingInLocale =
+                    DictionaryGroup.WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
+            mMostProbableDictionaryGroup = newMostProbableDictionaryGroup;
+        }
+    }
+
+    public boolean isConfidentAboutCurrentLanguageBeing(final Locale mLocale) {
+        final DictionaryGroup mostProbableDictionaryGroup = mMostProbableDictionaryGroup;
+        if (!mostProbableDictionaryGroup.mLocale.equals(mLocale)) {
+            return false;
+        }
+        if (mDictionaryGroups.length <= 1) {
+            return true;
+        }
+        return mostProbableDictionaryGroup.mConfidence >= CONFIDENCE_THRESHOLD;
     }
 
     @Nullable
@@ -624,7 +648,8 @@
             final int timeStampInSeconds, final boolean blockPotentiallyOffensive) {
         final ExpandableBinaryDictionary userHistoryDictionary =
                 dictionaryGroup.getSubDict(Dictionary.TYPE_USER_HISTORY);
-        if (userHistoryDictionary == null) {
+        if (userHistoryDictionary == null
+                || !isConfidentAboutCurrentLanguageBeing(userHistoryDictionary.mLocale)) {
             return;
         }
         final int maxFreq = getFrequency(word);
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFactory.java b/java/src/com/android/inputmethod/latin/DictionaryFactory.java
index 3459b42..781ab06 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFactory.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFactory.java
@@ -19,10 +19,8 @@
 import android.content.ContentProviderClient;
 import android.content.Context;
 import android.content.res.AssetFileDescriptor;
-import android.content.res.Resources;
 import android.util.Log;
 
-import com.android.inputmethod.annotations.UsedForTesting;
 import com.android.inputmethod.latin.utils.DictionaryInfoUtils;
 
 import java.io.File;
@@ -43,11 +41,10 @@
      * locale. If none is found, it falls back to the built-in dictionary - if any.
      * @param context application context for reading resources
      * @param locale the locale for which to create the dictionary
-     * @param useFullEditDistance whether to use the full edit distance in suggestions
      * @return an initialized instance of DictionaryCollection
      */
     public static DictionaryCollection createMainDictionaryFromManager(final Context context,
-            final Locale locale, final boolean useFullEditDistance) {
+            final Locale locale) {
         if (null == locale) {
             Log.e(TAG, "No locale defined for dictionary");
             return new DictionaryCollection(Dictionary.TYPE_MAIN, locale,
@@ -61,7 +58,7 @@
             for (final AssetFileAddress f : assetFileList) {
                 final ReadOnlyBinaryDictionary readOnlyBinaryDictionary =
                         new ReadOnlyBinaryDictionary(f.mFilename, f.mOffset, f.mLength,
-                                useFullEditDistance, locale, Dictionary.TYPE_MAIN);
+                                false /* useFullEditDistance */, locale, Dictionary.TYPE_MAIN);
                 if (readOnlyBinaryDictionary.isValidDictionary()) {
                     dictList.add(readOnlyBinaryDictionary);
                 } else {
@@ -101,49 +98,33 @@
             }
             final String wordlistId =
                     DictionaryInfoUtils.getWordListIdFromFileName(new File(f.mFilename).getName());
-            if (null != wordlistId) {
-                // TODO: this is a reasonable last resort, but it is suboptimal.
-                // The following will remove the entry for this dictionary with the dictionary
-                // provider. When the metadata is downloaded again, we will try downloading it
-                // again.
-                // However, in the practice that will mean the user will find themselves without
-                // the new dictionary. That's fine for languages where it's included in the APK,
-                // but for other languages it will leave the user without a dictionary at all until
-                // the next update, which may be a few days away.
-                // Ideally, we would trigger a new download right away, and use increasing retry
-                // delays for this particular id/version combination.
-                // Then again, this is expected to only ever happen in case of human mistake. If
-                // the wrong file is on the server, the following is still doing the right thing.
-                // If it's a file left over from the last version however, it's not great.
-                BinaryDictionaryFileDumper.reportBrokenFileToDictionaryProvider(
-                        providerClient,
-                        context.getString(R.string.dictionary_pack_client_id),
-                        wordlistId);
-            }
+            // TODO: this is a reasonable last resort, but it is suboptimal.
+            // The following will remove the entry for this dictionary with the dictionary
+            // provider. When the metadata is downloaded again, we will try downloading it
+            // again.
+            // However, in the practice that will mean the user will find themselves without
+            // the new dictionary. That's fine for languages where it's included in the APK,
+            // but for other languages it will leave the user without a dictionary at all until
+            // the next update, which may be a few days away.
+            // Ideally, we would trigger a new download right away, and use increasing retry
+            // delays for this particular id/version combination.
+            // Then again, this is expected to only ever happen in case of human mistake. If
+            // the wrong file is on the server, the following is still doing the right thing.
+            // If it's a file left over from the last version however, it's not great.
+            BinaryDictionaryFileDumper.reportBrokenFileToDictionaryProvider(
+                    providerClient,
+                    context.getString(R.string.dictionary_pack_client_id),
+                    wordlistId);
         }
     }
 
     /**
-     * Initializes a main dictionary collection from a dictionary pack, with default flags.
-     *
-     * This searches for a content provider providing a dictionary pack for the specified
-     * locale. If none is found, it falls back to the built-in dictionary, if any.
-     * @param context application context for reading resources
-     * @param locale the locale for which to create the dictionary
-     * @return an initialized instance of DictionaryCollection
-     */
-    public static DictionaryCollection createMainDictionaryFromManager(final Context context,
-            final Locale locale) {
-        return createMainDictionaryFromManager(context, locale, false /* useFullEditDistance */);
-    }
-
-    /**
      * Initializes a read-only binary dictionary from a raw resource file
      * @param context application context for reading resources
      * @param locale the locale to use for the resource
      * @return an initialized instance of ReadOnlyBinaryDictionary
      */
-    protected static ReadOnlyBinaryDictionary createReadOnlyBinaryDictionary(final Context context,
+    private static ReadOnlyBinaryDictionary createReadOnlyBinaryDictionary(final Context context,
             final Locale locale) {
         AssetFileDescriptor afd = null;
         try {
@@ -177,36 +158,4 @@
             }
         }
     }
-
-    /**
-     * Create a dictionary from passed data. This is intended for unit tests only.
-     * @param dictionaryList the list of files to read, with their offsets and lengths
-     * @param useFullEditDistance whether to use the full edit distance in suggestions
-     * @return the created dictionary, or null.
-     */
-    @UsedForTesting
-    public static Dictionary createDictionaryForTest(final AssetFileAddress[] dictionaryList,
-            final boolean useFullEditDistance, Locale locale) {
-        final DictionaryCollection dictionaryCollection =
-                new DictionaryCollection(Dictionary.TYPE_MAIN, locale);
-        for (final AssetFileAddress address : dictionaryList) {
-            final ReadOnlyBinaryDictionary readOnlyBinaryDictionary = new ReadOnlyBinaryDictionary(
-                    address.mFilename, address.mOffset, address.mLength, useFullEditDistance,
-                    locale, Dictionary.TYPE_MAIN);
-            dictionaryCollection.addDictionary(readOnlyBinaryDictionary);
-        }
-        return dictionaryCollection;
-    }
-
-    /**
-     * Find out whether a dictionary is available for this locale.
-     * @param context the context on which to check resources.
-     * @param locale the locale to check for.
-     * @return whether a (non-placeholder) dictionary is available or not.
-     */
-    public static boolean isDictionaryAvailable(Context context, Locale locale) {
-        final Resources res = context.getResources();
-        return 0 != DictionaryInfoUtils.getMainDictionaryResourceIdIfAvailableForLocale(
-                res, locale);
-    }
 }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 7b7b6d3..bd660c5 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -100,6 +100,7 @@
 import com.android.inputmethod.latin.utils.IntentUtils;
 import com.android.inputmethod.latin.utils.JniUtils;
 import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper;
+import com.android.inputmethod.latin.utils.ResourceUtils;
 import com.android.inputmethod.latin.utils.StatsUtils;
 import com.android.inputmethod.latin.utils.StatsUtilsManager;
 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
@@ -590,7 +591,6 @@
         // TODO: Resolve mutual dependencies of {@link #loadSettings()} and
         // {@link #resetDictionaryFacilitatorIfNecessary()}.
         loadSettings();
-        mSubtypeSwitcher.onSubtypeChanged(mRichImm.getCurrentRawSubtype());
         resetDictionaryFacilitatorIfNecessary();
 
         // Register to receive ringer mode change and network state change.
@@ -708,6 +708,7 @@
             mInputLogic.mSuggest.setAutoCorrectionThreshold(
                     settingsValues.mAutoCorrectionThreshold);
         }
+        mInputLogic.mSuggest.setPlausibilityThreshold(settingsValues.mPlausibilityThreshold);
     }
 
     /**
@@ -730,6 +731,7 @@
         unregisterReceiver(mDictionaryPackInstallReceiver);
         unregisterReceiver(mDictionaryDumpBroadcastReceiver);
         mStatsUtilsManager.onDestroy();
+        DictionaryDecayBroadcastReciever.cancelIntervalAlarmForDictionaryDecaying(this);
         super.onDestroy();
     }
 
@@ -864,7 +866,8 @@
     public void onCurrentInputMethodSubtypeChanged(final InputMethodSubtype subtype) {
         // Note that the calling sequence of onCreate() and onCurrentInputMethodSubtypeChanged()
         // is not guaranteed. It may even be called at the same time on a different thread.
-        mSubtypeSwitcher.onSubtypeChanged(subtype);
+        mRichImm.onSubtypeChanged(subtype);
+        mSubtypeSwitcher.onSubtypeChanged(mRichImm.getCurrentSubtype());
         mInputLogic.onSubtypeChanged(SubtypeLocaleUtils.getCombiningRulesExtraValue(subtype),
                 mSettings.getCurrent());
         loadKeyboard();
@@ -880,8 +883,8 @@
         // Switch to the null consumer to handle cases leading to early exit below, for which we
         // also wouldn't be consuming gesture data.
         mGestureConsumer = GestureConsumer.NULL_GESTURE_CONSUMER;
-        mRichImm.clearSubtypeCaches();
-        mSubtypeSwitcher.onSubtypeChanged(mRichImm.getCurrentRawSubtype());
+        mRichImm.refreshSubtypeCaches();
+        mSubtypeSwitcher.onSubtypeChanged(mRichImm.getCurrentSubtype());
         final KeyboardSwitcher switcher = mKeyboardSwitcher;
         switcher.updateKeyboardTheme();
         final MainKeyboardView mainKeyboardView = switcher.getMainKeyboardView();
@@ -1007,6 +1010,7 @@
                 suggest.setAutoCorrectionThreshold(
                         currentSettingsValues.mAutoCorrectionThreshold);
             }
+            suggest.setPlausibilityThreshold(currentSettingsValues.mPlausibilityThreshold);
 
             switcher.loadKeyboard(editorInfo, currentSettingsValues, getCurrentAutoCapsState(),
                     getCurrentRecapitalizeState());
@@ -1208,6 +1212,7 @@
             return;
         }
         final SettingsValues settingsValues = mSettings.getCurrent();
+        final Resources res = getResources();
         final View visibleKeyboardView = mKeyboardSwitcher.getVisibleKeyboardView();
         if (visibleKeyboardView == null || !hasSuggestionStripView()) {
             return;
@@ -1225,13 +1230,15 @@
         final int suggestionsHeight = (!mKeyboardSwitcher.isShowingEmojiPalettes()
                 && mSuggestionStripView.getVisibility() == View.VISIBLE)
                 ? mSuggestionStripView.getHeight() : 0;
-        final int visibleTopY = inputHeight - visibleKeyboardView.getHeight() - suggestionsHeight;
+        final int visibleTopY = inputHeight
+                - ResourceUtils.getKeyboardFrameHeight(res, settingsValues)
+                - suggestionsHeight;
         mSuggestionStripView.setMoreSuggestionsHeight(visibleTopY);
         // Need to set expanded touchable region only if a keyboard view is being shown.
         if (visibleKeyboardView.isShown()) {
             final int touchLeft = 0;
             final int touchTop = mKeyboardSwitcher.isShowingMoreKeysPanel() ? 0 : visibleTopY;
-            final int touchRight = visibleKeyboardView.getWidth();
+            final int touchRight = ResourceUtils.getKeyboardFrameWidth(res);
             final int touchBottom = inputHeight
                     // Extend touchable region below the keyboard.
                     + EXTENDED_TOUCHABLE_REGION_HEIGHT;
@@ -1446,7 +1453,7 @@
     // completely replace #onCodeInput.
     public void onEvent(@Nonnull final Event event) {
         if (Constants.CODE_SHORTCUT == event.mKeyCode) {
-            mRichImm.switchToShortcutIME(this);
+            mRichImm.switchToShortcutIme(this);
         }
         final InputTransaction completeInputTransaction =
                 mInputLogic.onCodeInput(mSettings.getCurrent(), event,
diff --git a/java/src/com/android/inputmethod/latin/NgramContext.java b/java/src/com/android/inputmethod/latin/NgramContext.java
index 82a1327..b477312 100644
--- a/java/src/com/android/inputmethod/latin/NgramContext.java
+++ b/java/src/com/android/inputmethod/latin/NgramContext.java
@@ -133,7 +133,7 @@
 
     // n is 1-indexed.
     @UsedForTesting
-    public boolean isNthPrevWordBeginningOfSontence(final int n) {
+    public boolean isNthPrevWordBeginningOfSentence(final int n) {
         if (n <= 0 || n > mPrevWordsCount) {
             return false;
         }
diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
index a1ac55a..4621217 100644
--- a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
+++ b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
@@ -110,8 +110,12 @@
 
         // Initialize additional subtypes.
         SubtypeLocaleUtils.init(context);
-        final InputMethodSubtype[] additionalSubtypes = getAdditionalSubtypes(context);
-        setAdditionalInputMethodSubtypes(additionalSubtypes);
+        final InputMethodSubtype[] additionalSubtypes = getAdditionalSubtypes();
+        mImmWrapper.mImm.setAdditionalInputMethodSubtypes(
+                getInputMethodIdOfThisIme(), additionalSubtypes);
+
+        // Initialize the current input method subtype and the shortcut IME.
+        refreshSubtypeCaches();
 
         final ConnectivityManager connectivityManager =
                 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -119,11 +123,10 @@
         mIsNetworkConnected = (info != null && info.isConnected());
     }
 
-    public InputMethodSubtype[] getAdditionalSubtypes(final Context context) {
-        SubtypeLocaleUtils.init(context);
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+    public InputMethodSubtype[] getAdditionalSubtypes() {
+        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
         final String prefAdditionalSubtypes = Settings.readPrefAdditionalSubtypes(
-                prefs, context.getResources());
+                prefs, mContext.getResources());
         return AdditionalSubtypeUtils.createAdditionalSubtypesArray(prefAdditionalSubtypes);
     }
 
@@ -325,23 +328,22 @@
         return INDEX_NOT_FOUND;
     }
 
-    @Nonnull
-    public RichInputMethodSubtype onSubtypeChanged(@Nonnull final InputMethodSubtype newSubtype) {
-        final RichInputMethodSubtype richSubtype = createCurrentRichInputMethodSubtype(newSubtype);
+    public void onSubtypeChanged(@Nonnull final InputMethodSubtype newSubtype) {
+        updateCurrentSubtype(newSubtype);
+        updateShortcutIme();
         if (DEBUG) {
-            Log.w(TAG, "onSubtypeChanged: " + richSubtype.getNameForLogging());
+            Log.w(TAG, "onSubtypeChanged: " + mCurrentRichInputMethodSubtype.getNameForLogging());
         }
-        mCurrentRichInputMethodSubtype = richSubtype;
-        return richSubtype;
     }
 
     private static RichInputMethodSubtype sForcedSubtypeForTesting = null;
 
     @UsedForTesting
-    static void forceSubtype(final InputMethodSubtype subtype) {
+    static void forceSubtype(@Nonnull final InputMethodSubtype subtype) {
         sForcedSubtypeForTesting = new RichInputMethodSubtype(subtype);
     }
 
+    @Nonnull
     public Locale[] getCurrentSubtypeLocales() {
         if (null != sForcedSubtypeForTesting) {
             return sForcedSubtypeForTesting.getLocales();
@@ -349,6 +351,7 @@
         return getCurrentSubtype().getLocales();
     }
 
+    @Nonnull
     public RichInputMethodSubtype getCurrentSubtype() {
         if (null != sForcedSubtypeForTesting) {
             return sForcedSubtypeForTesting;
@@ -361,18 +364,6 @@
         return SubtypeLocaleUtils.getCombiningRulesExtraValue(getCurrentSubtype().getRawSubtype());
     }
 
-    @Nonnull
-    public InputMethodSubtype getCurrentRawSubtype() {
-        return mImmWrapper.mImm.getCurrentInputMethodSubtype();
-    }
-
-    @Nonnull
-    public RichInputMethodSubtype createCurrentRichInputMethodSubtype(
-            @Nonnull final InputMethodSubtype rawSubtype) {
-        return AdditionalFeaturesSettingUtils.createRichInputMethodSubtype(this, rawSubtype,
-                mContext);
-    }
-
     public boolean hasMultipleEnabledIMEsOrSubtypes(final boolean shouldIncludeAuxiliarySubtypes) {
         final List<InputMethodInfo> enabledImis = mImmWrapper.mImm.getEnabledInputMethodList();
         return hasMultipleEnabledSubtypes(shouldIncludeAuxiliarySubtypes, enabledImis);
@@ -458,7 +449,7 @@
                 getInputMethodIdOfThisIme(), subtypes);
         // Clear the cache so that we go read the {@link InputMethodInfo} of this IME and list of
         // subtypes again next time.
-        clearSubtypeCaches();
+        refreshSubtypeCaches();
     }
 
     private List<InputMethodSubtype> getEnabledInputMethodSubtypeList(final InputMethodInfo imi,
@@ -475,10 +466,12 @@
         return result;
     }
 
-    public void clearSubtypeCaches() {
+    public void refreshSubtypeCaches() {
         mSubtypeListCacheWithImplicitlySelectedSubtypes.clear();
         mSubtypeListCacheWithoutImplicitlySelectedSubtypes.clear();
         mInputMethodInfoCache.clear();
+        updateCurrentSubtype(mImmWrapper.mImm.getCurrentInputMethodSubtype());
+        updateShortcutIme();
     }
 
     public boolean shouldOfferSwitchingToNextInputMethod(final IBinder binder,
@@ -517,8 +510,13 @@
         return true;
     }
 
-    // TODO: Make this private
-    void updateShortcutIME() {
+    private void updateCurrentSubtype(@Nonnull final InputMethodSubtype subtype) {
+        final RichInputMethodSubtype richSubtype = AdditionalFeaturesSettingUtils
+                .createRichInputMethodSubtype(this, subtype, mContext);
+        mCurrentRichInputMethodSubtype = richSubtype;
+    }
+
+    private void updateShortcutIme() {
         if (DEBUG) {
             Log.d(TAG, "Update shortcut IME from : "
                     + (mShortcutInputMethodInfo == null
@@ -550,7 +548,7 @@
         }
     }
 
-    public void switchToShortcutIME(final InputMethodService context) {
+    public void switchToShortcutIme(final InputMethodService context) {
         if (mShortcutInputMethodInfo == null) {
             return;
         }
@@ -576,19 +574,16 @@
     }
 
     public boolean isShortcutImeEnabled() {
-        updateShortcutIME();
         if (mShortcutInputMethodInfo == null) {
             return false;
         }
         if (mShortcutSubtype == null) {
             return true;
         }
-        return checkIfSubtypeBelongsToImeAndEnabled(
-                mShortcutInputMethodInfo, mShortcutSubtype);
+        return checkIfSubtypeBelongsToImeAndEnabled(mShortcutInputMethodInfo, mShortcutSubtype);
     }
 
     public boolean isShortcutImeReady() {
-        updateShortcutIME();
         if (mShortcutInputMethodInfo == null) {
             return false;
         }
diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
index 23e348b..92ba6c2 100644
--- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
@@ -57,7 +57,7 @@
         mResources = context.getResources();
         mRichImm = RichInputMethodManager.getInstance();
 
-        onSubtypeChanged(mRichImm.getCurrentRawSubtype());
+        onSubtypeChanged(mRichImm.getCurrentSubtype());
         updateParametersOnStartInputView();
     }
 
@@ -69,17 +69,14 @@
         final List<InputMethodSubtype> enabledSubtypesOfThisIme =
                 mRichImm.getMyEnabledInputMethodSubtypeList(true);
         mLanguageOnSpacebarHelper.onUpdateEnabledSubtypes(enabledSubtypesOfThisIme);
-        mRichImm.updateShortcutIME();
     }
 
     // Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function.
-    public void onSubtypeChanged(@Nonnull final InputMethodSubtype newSubtype) {
-        final RichInputMethodSubtype richSubtype = mRichImm.onSubtypeChanged(newSubtype);
+    public void onSubtypeChanged(@Nonnull final RichInputMethodSubtype richSubtype) {
         final boolean implicitlyEnabledSubtype = mRichImm
-                .checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled(newSubtype);
+                .checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled(richSubtype.getRawSubtype());
         mLanguageOnSpacebarHelper.onSubtypeChanged(
                 richSubtype, implicitlyEnabledSubtype, mResources.getConfiguration().locale);
-        mRichImm.updateShortcutIME();
     }
 
     public int getLanguageOnSpacebarFormatType(final RichInputMethodSubtype subtype) {
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 2d0ec42..0bf0f68 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -32,6 +32,7 @@
 import java.util.HashMap;
 import java.util.Locale;
 
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
 /**
@@ -64,15 +65,30 @@
     }
 
     private float mAutoCorrectionThreshold;
+    private float mPlausibilityThreshold;
 
     public Suggest(final DictionaryFacilitator dictionaryFacilitator) {
         mDictionaryFacilitator = dictionaryFacilitator;
     }
 
+    /**
+     * Set the normalized-score threshold for a suggestion to be considered strong enough that we
+     * will auto-correct to this.
+     * @param threshold the threshold
+     */
     public void setAutoCorrectionThreshold(final float threshold) {
         mAutoCorrectionThreshold = threshold;
     }
 
+    /**
+     * Set the normalized-score threshold for what we consider a "plausible" suggestion, in
+     * the same dimension as the auto-correction threshold.
+     * @param threshold the threshold
+     */
+    public void setPlausibilityThreshold(final float threshold) {
+        mPlausibilityThreshold = threshold;
+    }
+
     public interface OnGetSuggestedWordsCallback {
         public void onGetSuggestedWords(final SuggestedWords suggestedWords);
     }
@@ -117,7 +133,8 @@
         return suggestionsContainer;
     }
 
-    private static String getWhitelistedWordOrNull(final ArrayList<SuggestedWordInfo> suggestions) {
+    private static SuggestedWordInfo getWhitelistedWordInfoOrNull(
+            @Nonnull final ArrayList<SuggestedWordInfo> suggestions) {
         if (suggestions.isEmpty()) {
             return null;
         }
@@ -125,9 +142,21 @@
         if (!firstSuggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) {
             return null;
         }
-        return firstSuggestedWordInfo.mWord;
+        return firstSuggestedWordInfo;
     }
 
+    // Quality constants for dictionary match
+    // In increasing order of quality
+    // This source dictionary does not match the typed word.
+    private static final int QUALITY_NO_MATCH = 0;
+    // This source dictionary has a null locale, and the preferred locale is also null.
+    private static final int QUALITY_MATCH_NULL = 1;
+    // This source dictionary has a non-null locale different from the preferred locale. The
+    // preferred locale may be null : this is still better than MATCH_NULL.
+    private static final int QUALITY_MATCH_OTHER_LOCALE = 2;
+    // This source dictionary matches the preferred locale.
+    private static final int QUALITY_MATCH_PREFERRED_LOCALE = 3;
+
     // Retrieves suggestions for non-batch input (typing, recorrection, predictions...)
     // and calls the callback function with the suggestions.
     private void getSuggestedWordsForNonBatchInput(final WordComposer wordComposer,
@@ -152,11 +181,54 @@
                         // For transforming suggestions that don't come for any dictionary, we
                         // use the currently most probable locale as it's our best bet.
                         mostProbableLocale);
-        @Nullable final Dictionary sourceDictionaryOfRemovedWord =
-                SuggestedWordInfo.removeDupsAndReturnSourceOfTypedWord(wordComposer.getTypedWord(),
-                        mostProbableLocale /* preferredLocale */, suggestionsContainer);
 
-        final String whitelistedWord = getWhitelistedWordOrNull(suggestionsContainer);
+        boolean typedWordExistsInAnotherLanguage = false;
+        int qualityOfFoundSourceDictionary = QUALITY_NO_MATCH;
+        @Nullable Dictionary sourceDictionaryOfRemovedWord = null;
+        for (final SuggestedWordInfo info : suggestionsContainer) {
+            // Search for the best dictionary, defined as the first one with the highest match
+            // quality we can find.
+            if (typedWordString.equals(info.mWord)) {
+                if (mostProbableLocale.equals(info.mSourceDict.mLocale)) {
+                    if (qualityOfFoundSourceDictionary < QUALITY_MATCH_PREFERRED_LOCALE) {
+                        // Use this source if the old match had lower quality than this match
+                        sourceDictionaryOfRemovedWord = info.mSourceDict;
+                        qualityOfFoundSourceDictionary = QUALITY_MATCH_PREFERRED_LOCALE;
+                    }
+                } else {
+                    final int matchQuality = (null == info.mSourceDict.mLocale)
+                            ? QUALITY_MATCH_NULL : QUALITY_MATCH_OTHER_LOCALE;
+                    if (qualityOfFoundSourceDictionary < matchQuality) {
+                        // Use this source if the old match had lower quality than this match
+                        sourceDictionaryOfRemovedWord = info.mSourceDict;
+                        qualityOfFoundSourceDictionary = matchQuality;
+                    }
+                    typedWordExistsInAnotherLanguage = true;
+                }
+            }
+        }
+
+        SuggestedWordInfo.removeDups(typedWordString, suggestionsContainer);
+
+        final SuggestedWordInfo whitelistedWordInfo =
+                getWhitelistedWordInfoOrNull(suggestionsContainer);
+        final String whitelistedWord;
+        if (null != whitelistedWordInfo &&
+                (mDictionaryFacilitator.isConfidentAboutCurrentLanguageBeing(
+                        whitelistedWordInfo.mSourceDict.mLocale)
+                || (!typedWordExistsInAnotherLanguage
+                        && !hasPlausibleCandidateInAnyOtherLanguage(suggestionsContainer,
+                                consideredWord, whitelistedWordInfo)))) {
+            // We'll use the whitelist candidate if we are confident the user is typing in the
+            // language of the dictionary it's coming from, or if there is no plausible candidate
+            // coming from another language.
+            whitelistedWord = whitelistedWordInfo.mWord;
+        } else {
+            // If on the contrary we are not confident in the current language and we have
+            // at least a plausible candidate in any other language, then we don't use this
+            // whitelist candidate.
+            whitelistedWord = null;
+        }
         final boolean resultsArePredictions = !wordComposer.isComposingWord();
 
         // We allow auto-correction if we have a whitelisted word, or if the word had more than
@@ -198,7 +270,7 @@
             hasAutoCorrection = false;
         } else {
             final SuggestedWordInfo firstSuggestion = suggestionResults.first();
-            if (!AutoCorrectionUtils.suggestionExceedsAutoCorrectionThreshold(
+            if (!AutoCorrectionUtils.suggestionExceedsThreshold(
                     firstSuggestion, consideredWord, mAutoCorrectionThreshold)) {
                 // Score is too low for autocorrect
                 hasAutoCorrection = false;
@@ -247,6 +319,20 @@
                 false /* isObsoleteSuggestions */, inputStyle, sequenceNumber));
     }
 
+    private boolean hasPlausibleCandidateInAnyOtherLanguage(
+            final ArrayList<SuggestedWordInfo> suggestionsContainer, final String consideredWord,
+            final SuggestedWordInfo whitelistedWordInfo) {
+        for (final SuggestedWordInfo info : suggestionsContainer) {
+            if (whitelistedWordInfo.mSourceDict.mLocale.equals(info.mSourceDict.mLocale)) {
+                continue;
+            }
+            return AutoCorrectionUtils.suggestionExceedsThreshold(info, consideredWord,
+                    mPlausibilityThreshold);
+        }
+        // No candidate in another language
+        return false;
+    }
+
     // Retrieves suggestions for the batch input
     // and calls the callback function with the suggestions.
     private void getSuggestedWordsForBatchInput(final WordComposer wordComposer,
@@ -280,8 +366,7 @@
             final SuggestedWordInfo rejected = suggestionsContainer.remove(0);
             suggestionsContainer.add(1, rejected);
         }
-        SuggestedWordInfo.removeDupsAndReturnSourceOfTypedWord(null /* typedWord */,
-                null /* preferredLocale */, suggestionsContainer);
+        SuggestedWordInfo.removeDups(null /* typedWord */, suggestionsContainer);
 
         // For some reason some suggestions with MIN_VALUE are making their way here.
         // TODO: Find a more robust way to detect distracters.
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index cbf48f0..30dd51a 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -27,7 +27,6 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
-import java.util.Locale;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -356,53 +355,30 @@
         }
 
         // This will always remove the higher index if a duplicate is found.
-        // Returns null if the typed word is not found. Always return the dictionary for the
-        // highest suggestion matching the locale if found, otherwise return the dictionary for
-        // the highest suggestion.
-        @Nullable
-        public static Dictionary removeDupsAndReturnSourceOfTypedWord(
-                @Nullable final String typedWord,
-                @Nullable final Locale preferredLocale,
+        public static void removeDups(@Nullable final String typedWord,
                 @Nonnull ArrayList<SuggestedWordInfo> candidates) {
             if (candidates.isEmpty()) {
-                return null;
+                return;
             }
-            final Dictionary sourceDictionaryOfTypedWord;
             if (!TextUtils.isEmpty(typedWord)) {
-                sourceDictionaryOfTypedWord =
-                        removeSuggestedWordInfoFromListAndReturnSourceDictionary(typedWord,
-                                preferredLocale, candidates, -1 /* startIndexExclusive */);
-            } else {
-                sourceDictionaryOfTypedWord = null;
+                removeSuggestedWordInfoFromList(typedWord, candidates, -1 /* startIndexExclusive */);
             }
             for (int i = 0; i < candidates.size(); ++i) {
-                removeSuggestedWordInfoFromListAndReturnSourceDictionary(candidates.get(i).mWord,
-                        null /* preferredLocale */, candidates, i /* startIndexExclusive */);
+                removeSuggestedWordInfoFromList(candidates.get(i).mWord, candidates,
+                        i /* startIndexExclusive */);
             }
-            return sourceDictionaryOfTypedWord;
         }
 
-        @Nullable
-        private static Dictionary removeSuggestedWordInfoFromListAndReturnSourceDictionary(
-                @Nonnull final String word, @Nullable final Locale preferredLocale,
-                @Nonnull final ArrayList<SuggestedWordInfo> candidates,
+        private static void removeSuggestedWordInfoFromList(
+                @Nonnull final String word, @Nonnull final ArrayList<SuggestedWordInfo> candidates,
                 final int startIndexExclusive) {
-            Dictionary sourceDictionaryOfTypedWord = null;
             for (int i = startIndexExclusive + 1; i < candidates.size(); ++i) {
                 final SuggestedWordInfo previous = candidates.get(i);
                 if (word.equals(previous.mWord)) {
-                    if (null == sourceDictionaryOfTypedWord
-                            || (null != preferredLocale
-                                    && preferredLocale.equals(previous.mSourceDict.mLocale))) {
-                        if (Dictionary.TYPE_USER_HISTORY != previous.mSourceDict.mDictType) {
-                            sourceDictionaryOfTypedWord = previous.mSourceDict;
-                        }
-                    }
                     candidates.remove(i);
                     --i;
                 }
             }
-            return sourceDictionaryOfTypedWord;
         }
     }
 
diff --git a/java/src/com/android/inputmethod/latin/SystemBroadcastReceiver.java b/java/src/com/android/inputmethod/latin/SystemBroadcastReceiver.java
index 123ab20..982d4c6 100644
--- a/java/src/com/android/inputmethod/latin/SystemBroadcastReceiver.java
+++ b/java/src/com/android/inputmethod/latin/SystemBroadcastReceiver.java
@@ -69,7 +69,7 @@
             // subtypes when the package is replaced.
             RichInputMethodManager.init(context);
             final RichInputMethodManager richImm = RichInputMethodManager.getInstance();
-            final InputMethodSubtype[] additionalSubtypes = richImm.getAdditionalSubtypes(context);
+            final InputMethodSubtype[] additionalSubtypes = richImm.getAdditionalSubtypes();
             richImm.setAdditionalInputMethodSubtypes(additionalSubtypes);
             LauncherIconVisibilityManager.updateSetupWizardIconVisibility(context);
         } else if (Intent.ACTION_BOOT_COMPLETED.equals(intentAction)) {
diff --git a/java/src/com/android/inputmethod/latin/debug/ExternalDictionaryGetterForDebug.java b/java/src/com/android/inputmethod/latin/debug/ExternalDictionaryGetterForDebug.java
index 78bfd2b..8cc3552 100644
--- a/java/src/com/android/inputmethod/latin/debug/ExternalDictionaryGetterForDebug.java
+++ b/java/src/com/android/inputmethod/latin/debug/ExternalDictionaryGetterForDebug.java
@@ -101,12 +101,12 @@
         final File file = new File(dirPath, fileName.toString());
         final DictionaryHeader header = DictionaryInfoUtils.getDictionaryFileHeaderOrNull(file);
         final StringBuilder message = new StringBuilder();
-        final String locale = header.getLocaleString();
-        for (String key : header.mDictionaryOptions.mAttributes.keySet()) {
+        final String localeString = header.mLocaleString;
+        for (final String key : header.mDictionaryOptions.mAttributes.keySet()) {
             message.append(key + " = " + header.mDictionaryOptions.mAttributes.get(key));
             message.append("\n");
         }
-        final String languageName = LocaleUtils.constructLocaleFromString(locale)
+        final String languageName = LocaleUtils.constructLocaleFromString(localeString)
                 .getDisplayName(Locale.getDefault());
         final String title = String.format(
                 context.getString(R.string.read_external_dictionary_confirm_install_message),
@@ -146,11 +146,12 @@
         BufferedOutputStream outputStream = null;
         File tempFile = null;
         try {
-            final String locale = header.getLocaleString();
+            final String localeString = header.mLocaleString;
             // Create the id for a main dictionary for this locale
             final String id = BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY
-                    + BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR + locale;
-            final String finalFileName = DictionaryInfoUtils.getCacheFileName(id, locale, context);
+                    + BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR + localeString;
+            final String finalFileName = DictionaryInfoUtils.getCacheFileName(
+                    id, localeString, context);
             final String tempFileName = BinaryDictionaryGetter.getTempFileName(id, context);
             tempFile = new File(tempFileName);
             tempFile.delete();
diff --git a/java/src/com/android/inputmethod/latin/makedict/DictionaryHeader.java b/java/src/com/android/inputmethod/latin/makedict/DictionaryHeader.java
index 644818b..4d253b0 100644
--- a/java/src/com/android/inputmethod/latin/makedict/DictionaryHeader.java
+++ b/java/src/com/android/inputmethod/latin/makedict/DictionaryHeader.java
@@ -19,13 +19,24 @@
 import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions;
 import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 /**
  * Class representing dictionary header.
  */
 public final class DictionaryHeader {
     public final int mBodyOffset;
+    @Nonnull
     public final DictionaryOptions mDictionaryOptions;
+    @Nonnull
     public final FormatOptions mFormatOptions;
+    @Nonnull
+    public final String mLocaleString;
+    @Nonnull
+    public final String mVersionString;
+    @Nonnull
+    public final String mIdString;
 
     // Note that these are corresponding definitions in native code in latinime::HeaderPolicy
     // and latinime::HeaderReadWriteUtils.
@@ -46,39 +57,32 @@
     public static final String ATTRIBUTE_VALUE_TRUE = "1";
     public static final String CODE_POINT_TABLE_KEY = "codePointTable";
 
-    public DictionaryHeader(final int headerSize, final DictionaryOptions dictionaryOptions,
-            final FormatOptions formatOptions) throws UnsupportedFormatException {
+    public DictionaryHeader(final int headerSize,
+            @Nonnull final DictionaryOptions dictionaryOptions,
+            @Nonnull final FormatOptions formatOptions) throws UnsupportedFormatException {
         mDictionaryOptions = dictionaryOptions;
         mFormatOptions = formatOptions;
         mBodyOffset = formatOptions.mVersion < FormatSpec.VERSION4 ? headerSize : 0;
-        if (null == getLocaleString()) {
+        final String localeString = dictionaryOptions.mAttributes.get(DICTIONARY_LOCALE_KEY);
+        if (null == localeString) {
             throw new UnsupportedFormatException("Cannot create a FileHeader without a locale");
         }
-        if (null == getVersion()) {
+        final String versionString = dictionaryOptions.mAttributes.get(DICTIONARY_VERSION_KEY);
+        if (null == versionString) {
             throw new UnsupportedFormatException(
                     "Cannot create a FileHeader without a version");
         }
-        if (null == getId()) {
+        final String idString = dictionaryOptions.mAttributes.get(DICTIONARY_ID_KEY);
+        if (null == idString) {
             throw new UnsupportedFormatException("Cannot create a FileHeader without an ID");
         }
-    }
-
-    // Helper method to get the locale as a String
-    public String getLocaleString() {
-        return mDictionaryOptions.mAttributes.get(DICTIONARY_LOCALE_KEY);
-    }
-
-    // Helper method to get the version String
-    public String getVersion() {
-        return mDictionaryOptions.mAttributes.get(DICTIONARY_VERSION_KEY);
-    }
-
-    // Helper method to get the dictionary ID as a String
-    public String getId() {
-        return mDictionaryOptions.mAttributes.get(DICTIONARY_ID_KEY);
+        mLocaleString = localeString;
+        mVersionString = versionString;
+        mIdString = idString;
     }
 
     // Helper method to get the description
+    @Nullable
     public String getDescription() {
         // TODO: Right now each dictionary file comes with a description in its own language.
         // It will display as is no matter the device's locale. It should be internationalized.
diff --git a/java/src/com/android/inputmethod/latin/personalization/DictionaryDecayBroadcastReciever.java b/java/src/com/android/inputmethod/latin/personalization/DictionaryDecayBroadcastReciever.java
index 221bb9a..e974f33 100644
--- a/java/src/com/android/inputmethod/latin/personalization/DictionaryDecayBroadcastReciever.java
+++ b/java/src/com/android/inputmethod/latin/personalization/DictionaryDecayBroadcastReciever.java
@@ -43,17 +43,40 @@
     /**
      * Interval to update for decaying dictionaries.
      */
-    /* package */ static final long DICTIONARY_DECAY_INTERVAL = TimeUnit.MINUTES.toMillis(60);
+    static final long DICTIONARY_DECAY_INTERVAL_IN_MILLIS = TimeUnit.MINUTES.toMillis(60);
 
-    public static void setUpIntervalAlarmForDictionaryDecaying(Context context) {
-        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
+    private static PendingIntent getPendingIntentForDictionaryDecay(final Context context) {
         final Intent updateIntent = new Intent(DICTIONARY_DECAY_INTENT_ACTION);
         updateIntent.setClass(context, DictionaryDecayBroadcastReciever.class);
-        final long alarmTime =  System.currentTimeMillis() + DICTIONARY_DECAY_INTERVAL;
-        final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0 /* requestCode */,
+        return PendingIntent.getBroadcast(context, 0 /* requestCode */,
                 updateIntent, PendingIntent.FLAG_CANCEL_CURRENT);
-        if (null != alarmManager) alarmManager.setInexactRepeating(AlarmManager.RTC,
-                alarmTime, DICTIONARY_DECAY_INTERVAL, pendingIntent);
+    }
+
+    /**
+     * Set up interval alarm for dynamic dictionaries.
+     */
+    public static void setUpIntervalAlarmForDictionaryDecaying(final Context context) {
+        final AlarmManager alarmManager =
+                (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
+        if (null == alarmManager) {
+            return;
+        }
+        final long alarmTriggerTimeInMillis =
+                System.currentTimeMillis() + DICTIONARY_DECAY_INTERVAL_IN_MILLIS;
+        alarmManager.setInexactRepeating(AlarmManager.RTC, alarmTriggerTimeInMillis,
+                DICTIONARY_DECAY_INTERVAL_IN_MILLIS, getPendingIntentForDictionaryDecay(context));
+    }
+
+    /**
+     * Cancel interval alarm that has been set up.
+     */
+    public static void cancelIntervalAlarmForDictionaryDecaying(final Context context) {
+        final AlarmManager alarmManager =
+                (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
+        if (null == alarmManager) {
+            return;
+        }
+        alarmManager.cancel(getPendingIntentForDictionaryDecay(context));
     }
 
     @Override
diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java
index cefb0dd..b595f39 100644
--- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java
+++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java
@@ -73,7 +73,7 @@
     private static int sCurrentTimestampForTesting = 0;
     public static void currentTimeChangedForTesting(final int currentTimestamp) {
         if (TimeUnit.MILLISECONDS.toSeconds(
-                DictionaryDecayBroadcastReciever.DICTIONARY_DECAY_INTERVAL)
+                DictionaryDecayBroadcastReciever.DICTIONARY_DECAY_INTERVAL_IN_MILLIS)
                         < currentTimestamp - sCurrentTimestampForTesting) {
             runGCOnAllOpenedUserHistoryDictionaries();
             runGCOnAllOpenedPersonalizationDictionaries();
diff --git a/java/src/com/android/inputmethod/latin/settings/CustomInputStylePreference.java b/java/src/com/android/inputmethod/latin/settings/CustomInputStylePreference.java
index b749aa5..21ea8f8 100644
--- a/java/src/com/android/inputmethod/latin/settings/CustomInputStylePreference.java
+++ b/java/src/com/android/inputmethod/latin/settings/CustomInputStylePreference.java
@@ -196,16 +196,6 @@
         }
     }
 
-    private static int getSpinnerPosition(final Spinner spinner) {
-        if (spinner == null) return -1;
-        return spinner.getSelectedItemPosition();
-    }
-
-    private static void setSpinnerPosition(final Spinner spinner, final int position) {
-        if (spinner == null || position < 0) return;
-        spinner.setSelection(position);
-    }
-
     @Override
     protected Parcelable onSaveInstanceState() {
         final Parcelable superState = super.onSaveInstanceState();
@@ -216,8 +206,6 @@
 
         final SavedState myState = new SavedState(superState);
         myState.mSubtype = mSubtype;
-        myState.mSubtypeLocaleSelectedPos = getSpinnerPosition(mSubtypeLocaleSpinner);
-        myState.mKeyboardLayoutSetSelectedPos = getSpinnerPosition(mKeyboardLayoutSetSpinner);
         return myState;
     }
 
@@ -230,15 +218,11 @@
 
         final SavedState myState = (SavedState) state;
         super.onRestoreInstanceState(myState.getSuperState());
-        setSpinnerPosition(mSubtypeLocaleSpinner, myState.mSubtypeLocaleSelectedPos);
-        setSpinnerPosition(mKeyboardLayoutSetSpinner, myState.mKeyboardLayoutSetSelectedPos);
         setSubtype(myState.mSubtype);
     }
 
     static final class SavedState extends Preference.BaseSavedState {
         InputMethodSubtype mSubtype;
-        int mSubtypeLocaleSelectedPos;
-        int mKeyboardLayoutSetSelectedPos;
 
         public SavedState(final Parcelable superState) {
             super(superState);
@@ -247,15 +231,11 @@
         @Override
         public void writeToParcel(final Parcel dest, final int flags) {
             super.writeToParcel(dest, flags);
-            dest.writeInt(mSubtypeLocaleSelectedPos);
-            dest.writeInt(mKeyboardLayoutSetSelectedPos);
             dest.writeParcelable(mSubtype, 0);
         }
 
         public SavedState(final Parcel source) {
             super(source);
-            mSubtypeLocaleSelectedPos = source.readInt();
-            mKeyboardLayoutSetSelectedPos = source.readInt();
             mSubtype = (InputMethodSubtype)source.readParcelable(null);
         }
 
diff --git a/java/src/com/android/inputmethod/latin/settings/DebugSettings.java b/java/src/com/android/inputmethod/latin/settings/DebugSettings.java
index 6fffb8e..eff7bde 100644
--- a/java/src/com/android/inputmethod/latin/settings/DebugSettings.java
+++ b/java/src/com/android/inputmethod/latin/settings/DebugSettings.java
@@ -31,6 +31,9 @@
             "pref_has_custom_key_preview_animation_params";
     public static final String PREF_RESIZE_KEYBOARD = "pref_resize_keyboard";
     public static final String PREF_KEYBOARD_HEIGHT_SCALE = "pref_keyboard_height_scale";
+    public static final String PREF_KEYBOARD_BOTTOM_MARGIN = "pref_keyboard_bottom_margin";
+    public static final String PREF_KEYBOARD_LEFT_MARGIN = "pref_keyboard_left_margin";
+    public static final String PREF_KEYBOARD_RIGHT_MARGIN = "pref_keyboard_right_margin";
     public static final String PREF_KEY_PREVIEW_DISMISS_DURATION =
             "pref_key_preview_dismiss_duration";
     public static final String PREF_KEY_PREVIEW_DISMISS_END_X_SCALE =
diff --git a/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java
index 068f56d..779bf47 100644
--- a/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/settings/DebugSettingsFragment.java
@@ -89,8 +89,14 @@
                 defaultKeyPreviewDismissEndScale);
         setupKeyPreviewAnimationScale(DebugSettings.PREF_KEY_PREVIEW_DISMISS_END_Y_SCALE,
                 defaultKeyPreviewDismissEndScale);
-        setupKeyboardHeight(
+        setupKeyboardScale(
                 DebugSettings.PREF_KEYBOARD_HEIGHT_SCALE, SettingsValues.DEFAULT_SIZE_SCALE);
+        setupKeyboardScale(
+            DebugSettings.PREF_KEYBOARD_BOTTOM_MARGIN, SettingsValues.DEFAULT_MARGIN_SCALE);
+        setupKeyboardScale(
+            DebugSettings.PREF_KEYBOARD_LEFT_MARGIN, SettingsValues.DEFAULT_MARGIN_SCALE);
+        setupKeyboardScale(
+            DebugSettings.PREF_KEYBOARD_RIGHT_MARGIN, SettingsValues.DEFAULT_MARGIN_SCALE);
 
         mServiceNeedsRestart = false;
         mDebugMode = (TwoStatePreference) findPreference(DebugSettings.PREF_DEBUG_MODE);
@@ -253,7 +259,7 @@
         });
     }
 
-    private void setupKeyboardHeight(final String prefKey, final float defaultValue) {
+    private void setupKeyboardScale(final String prefKey, final float defaultValue) {
         final SharedPreferences prefs = getSharedPreferences();
         final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(prefKey);
         if (pref == null) {
@@ -282,7 +288,7 @@
             @Override
             public int readValue(final String key) {
                 return getPercentageFromValue(
-                        Settings.readKeyboardHeight(prefs, key, defaultValue));
+                        Settings.readKeyboardScale(prefs, key, defaultValue));
             }
 
             @Override
diff --git a/java/src/com/android/inputmethod/latin/settings/PreferencesSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/PreferencesSettingsFragment.java
index c0ceb88..975396d 100644
--- a/java/src/com/android/inputmethod/latin/settings/PreferencesSettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/settings/PreferencesSettingsFragment.java
@@ -71,6 +71,7 @@
         super.onResume();
         final Preference voiceInputKeyOption = findPreference(Settings.PREF_VOICE_INPUT_KEY);
         if (voiceInputKeyOption != null) {
+            RichInputMethodManager.getInstance().refreshSubtypeCaches();
             final boolean isShortcutImeEnabled = RichInputMethodManager.getInstance()
                     .isShortcutImeEnabled();
             voiceInputKeyOption.setEnabled(isShortcutImeEnabled);
diff --git a/java/src/com/android/inputmethod/latin/settings/Settings.java b/java/src/com/android/inputmethod/latin/settings/Settings.java
index 16c0534..196fef4 100644
--- a/java/src/com/android/inputmethod/latin/settings/Settings.java
+++ b/java/src/com/android/inputmethod/latin/settings/Settings.java
@@ -238,6 +238,10 @@
         return !currentAutoCorrectionSetting.equals(autoCorrectionOff);
     }
 
+    public static float readPlausibilityThreshold(final Resources res) {
+        return Float.parseFloat(res.getString(R.string.plausibility_threshold));
+    }
+
     public static boolean readBlockPotentiallyOffensive(final SharedPreferences prefs,
             final Resources res) {
         return prefs.getBoolean(PREF_BLOCK_POTENTIALLY_OFFENSIVE,
@@ -363,7 +367,7 @@
         return (milliseconds != UNDEFINED_PREFERENCE_VALUE_INT) ? milliseconds : defaultValue;
     }
 
-    public static float readKeyboardHeight(final SharedPreferences prefs,
+    public static float readKeyboardScale(final SharedPreferences prefs,
             final String prefKey, final float defaultValue) {
         final float percentage = prefs.getFloat(prefKey, UNDEFINED_PREFERENCE_VALUE_FLOAT);
         return (percentage != UNDEFINED_PREFERENCE_VALUE_FLOAT) ? percentage : defaultValue;
diff --git a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java b/java/src/com/android/inputmethod/latin/settings/SettingsValues.java
index 26415e7..99e0565 100644
--- a/java/src/com/android/inputmethod/latin/settings/SettingsValues.java
+++ b/java/src/com/android/inputmethod/latin/settings/SettingsValues.java
@@ -50,6 +50,7 @@
     private static final String FLOAT_NEGATIVE_INFINITY_MARKER_STRING = "floatNegativeInfinity";
     private static final int TIMEOUT_TO_GET_TARGET_PACKAGE = 5; // seconds
     public static final float DEFAULT_SIZE_SCALE = 1.0f; // 100%
+    public static final float DEFAULT_MARGIN_SCALE = 0.0f; // 0%
 
     // From resources:
     public final SpacingAndPunctuations mSpacingAndPunctuations;
@@ -96,6 +97,7 @@
     public final int mKeyPreviewPopupDismissDelay;
     private final boolean mAutoCorrectEnabled;
     public final float mAutoCorrectionThreshold;
+    public final float mPlausibilityThreshold;
     public final boolean mAutoCorrectionEnabledPerUserSettings;
     private final boolean mSuggestionsEnabledPerUserSettings;
     private final AsyncResultHolder<AppWorkaroundsUtils> mAppWorkarounds;
@@ -112,6 +114,9 @@
     public final boolean mHasCustomKeyPreviewAnimationParams;
     public final boolean mHasKeyboardResize;
     public final float mKeyboardHeightScale;
+    public final float mKeyboardBottomMarginScale;
+    public final float mKeyboardLeftMarginScale;
+    public final float mKeyboardRightMarginScale;
     public final int mKeyPreviewShowUpDuration;
     public final int mKeyPreviewDismissDuration;
     public final float mKeyPreviewShowUpStartXScale;
@@ -172,6 +177,7 @@
                 Settings.PREF_ENABLE_EMOJI_ALT_PHYSICAL_KEY, true);
         mAutoCorrectionThreshold = readAutoCorrectionThreshold(res,
                 autoCorrectionThresholdRawValue);
+        mPlausibilityThreshold = Settings.readPlausibilityThreshold(res);
         mGestureInputEnabled = Settings.readGestureInputEnabled(prefs, res);
         mGestureTrailEnabled = prefs.getBoolean(Settings.PREF_GESTURE_PREVIEW_TRAIL, true);
         mGestureFloatingPreviewTextEnabled = !mInputAttributes.mDisableGestureFloatingPreviewText
@@ -188,8 +194,14 @@
         mHasCustomKeyPreviewAnimationParams = prefs.getBoolean(
                 DebugSettings.PREF_HAS_CUSTOM_KEY_PREVIEW_ANIMATION_PARAMS, false);
         mHasKeyboardResize = prefs.getBoolean(DebugSettings.PREF_RESIZE_KEYBOARD, false);
-        mKeyboardHeightScale = Settings.readKeyboardHeight(
+        mKeyboardHeightScale = Settings.readKeyboardScale(
                 prefs, DebugSettings.PREF_KEYBOARD_HEIGHT_SCALE, DEFAULT_SIZE_SCALE);
+        mKeyboardBottomMarginScale = Settings.readKeyboardScale(
+                prefs, DebugSettings.PREF_KEYBOARD_BOTTOM_MARGIN, DEFAULT_MARGIN_SCALE);
+        mKeyboardLeftMarginScale = Settings.readKeyboardScale(
+                prefs, DebugSettings.PREF_KEYBOARD_LEFT_MARGIN, DEFAULT_MARGIN_SCALE);
+        mKeyboardRightMarginScale = Settings.readKeyboardScale(
+                prefs, DebugSettings.PREF_KEYBOARD_RIGHT_MARGIN, DEFAULT_MARGIN_SCALE);
         mKeyPreviewShowUpDuration = Settings.readKeyPreviewAnimationDuration(
                 prefs, DebugSettings.PREF_KEY_PREVIEW_SHOW_UP_DURATION,
                 res.getInteger(R.integer.config_key_preview_show_up_duration));
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
index c90e8a3..4b8d2a3 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java
@@ -115,7 +115,8 @@
     @Override
     public void onCreate() {
         final String localeString = getLocale();
-        mLocale = LocaleUtils.constructLocaleFromString(localeString);
+        mLocale = (null == localeString) ? null
+                : LocaleUtils.constructLocaleFromString(localeString);
         mScript = ScriptUtils.getScriptFromSpellCheckerLocale(mLocale);
     }
 
diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java
index 22fc35a..69029c5 100644
--- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java
+++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java
@@ -34,6 +34,8 @@
 import java.util.Locale;
 import java.util.TreeSet;
 
+import javax.annotation.Nullable;
+
 // Caveat: This class is basically taken from
 // packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java
 // in order to deal with some devices that have issues with the user dictionary handling
@@ -218,8 +220,8 @@
     public static class LocaleRenderer {
         private final String mLocaleString;
         private final String mDescription;
-        // LocaleString may NOT be null.
-        public LocaleRenderer(final Context context, final String localeString) {
+
+        public LocaleRenderer(final Context context, @Nullable final String localeString) {
             mLocaleString = localeString;
             if (null == localeString) {
                 mDescription = context.getString(R.string.user_dict_settings_more_languages);
diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java
index b9ed353..6254b54 100644
--- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java
+++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java
@@ -37,6 +37,8 @@
 import java.util.Locale;
 import java.util.TreeSet;
 
+import javax.annotation.Nullable;
+
 // Caveat: This class is basically taken from
 // packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryList.java
 // in order to deal with some devices that have issues with the user dictionary handling
@@ -131,21 +133,23 @@
 
     /**
      * Create a single User Dictionary Preference object, with its parameters set.
-     * @param locale The locale for which this user dictionary is for.
+     * @param localeString The locale for which this user dictionary is for.
      * @return The corresponding preference.
      */
-    protected Preference createUserDictionaryPreference(final String locale) {
+    protected Preference createUserDictionaryPreference(@Nullable final String localeString) {
         final Preference newPref = new Preference(getActivity());
         final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION);
-        if (null == locale) {
+        if (null == localeString) {
             newPref.setTitle(Locale.getDefault().getDisplayName());
         } else {
-            if ("".equals(locale))
+            if (localeString.isEmpty()) {
                 newPref.setTitle(getString(R.string.user_dict_settings_all_languages));
-            else
-                newPref.setTitle(LocaleUtils.constructLocaleFromString(locale).getDisplayName());
-            intent.putExtra("locale", locale);
-            newPref.getExtras().putString("locale", locale);
+            } else {
+                newPref.setTitle(
+                        LocaleUtils.constructLocaleFromString(localeString).getDisplayName());
+            }
+            intent.putExtra("locale", localeString);
+            newPref.getExtras().putString("locale", localeString);
         }
         newPref.setIntent(intent);
         newPref.setFragment(UserDictionarySettings.class.getName());
diff --git a/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java b/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java
index 120cffb..2fd2579 100644
--- a/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/AutoCorrectionUtils.java
@@ -29,9 +29,8 @@
         // Purely static class: can't instantiate.
     }
 
-    public static boolean suggestionExceedsAutoCorrectionThreshold(
-            final SuggestedWordInfo suggestion, final String consideredWord,
-            final float autoCorrectionThreshold) {
+    public static boolean suggestionExceedsThreshold(final SuggestedWordInfo suggestion,
+            final String consideredWord, final float threshold) {
         if (null != suggestion) {
             // Shortlist a whitelisted word
             if (suggestion.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) {
@@ -45,11 +44,11 @@
             if (DBG) {
                 Log.d(TAG, "Normalized " + consideredWord + "," + suggestion + ","
                         + autoCorrectionSuggestionScore + ", " + normalizedScore
-                        + "(" + autoCorrectionThreshold + ")");
+                        + "(" + threshold + ")");
             }
-            if (normalizedScore >= autoCorrectionThreshold) {
+            if (normalizedScore >= threshold) {
                 if (DBG) {
-                    Log.d(TAG, "Auto corrected by S-threshold.");
+                    Log.d(TAG, "Exceeds threshold.");
                 }
                 return true;
             }
diff --git a/java/src/com/android/inputmethod/latin/utils/CombinedFormatUtils.java b/java/src/com/android/inputmethod/latin/utils/CombinedFormatUtils.java
index 8699f2c..476c134 100644
--- a/java/src/com/android/inputmethod/latin/utils/CombinedFormatUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/CombinedFormatUtils.java
@@ -88,7 +88,7 @@
                 for (int i = 0; i < ngramProperty.mNgramContext.getPrevWordCount(); i++) {
                     builder.append("  " + NGRAM_PREV_WORD_TAG + "[" + i + "]="
                             + ngramProperty.mNgramContext.getNthPrevWord(i + 1));
-                    if (ngramProperty.mNgramContext.isNthPrevWordBeginningOfSontence(i + 1)) {
+                    if (ngramProperty.mNgramContext.isNthPrevWordBeginningOfSentence(i + 1)) {
                         builder.append("," + BEGINNING_OF_SENTENCE_TAG + "=true");
                     }
                     builder.append("\n");
diff --git a/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java b/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java
index 81c3e3c..fcce1ec 100644
--- a/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java
@@ -40,6 +40,9 @@
 import java.util.Locale;
 import java.util.concurrent.TimeUnit;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 /**
  * This class encapsulates the logic for the Latin-IME side of dictionary information management.
  */
@@ -59,19 +62,26 @@
         private static final String DATE_COLUMN = "date";
         private static final String FILESIZE_COLUMN = "filesize";
         private static final String VERSION_COLUMN = "version";
+        @Nonnull
         public final String mId;
+        @Nonnull
         public final Locale mLocale;
+        @Nullable
         public final String mDescription;
+        @Nonnull
         public final AssetFileAddress mFileAddress;
         public final int mVersion;
-        public DictionaryInfo(final String id, final Locale locale, final String description,
-                final AssetFileAddress fileAddress, final int version) {
+
+        public DictionaryInfo(@Nonnull final String id, @Nonnull final Locale locale,
+                @Nullable final String description, @Nonnull final AssetFileAddress fileAddress,
+                final int version) {
             mId = id;
             mLocale = locale;
             mDescription = description;
             mFileAddress = fileAddress;
             mVersion = version;
         }
+
         public ContentValues toContentValues() {
             final ContentValues values = new ContentValues();
             values.put(WORDLISTID_COLUMN, mId);
@@ -144,7 +154,8 @@
     /**
      * Reverse escaping done by replaceFileNameDangerousCharacters.
      */
-    public static String getWordListIdFromFileName(final String fname) {
+    @Nonnull
+    public static String getWordListIdFromFileName(@Nonnull final String fname) {
         final StringBuilder sb = new StringBuilder();
         final int fnameLength = fname.length();
         for (int i = 0; i < fnameLength; i = fname.offsetByCodePoints(i, 1)) {
@@ -176,12 +187,15 @@
      * {@link #getMainDictId(Locale)} and {@link #isMainWordListId(String)}.
      * @return The category as a string or null if it can't be found in the file name.
      */
-    public static String getCategoryFromFileName(final String fileName) {
+    @Nullable
+    public static String getCategoryFromFileName(@Nonnull final String fileName) {
         final String id = getWordListIdFromFileName(fileName);
         final String[] idArray = id.split(BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR);
         // An id is supposed to be in format category:locale, so splitting on the separator
         // should yield a 2-elements array
-        if (2 != idArray.length) return null;
+        if (2 != idArray.length) {
+            return null;
+        }
         return idArray[0];
     }
 
@@ -225,11 +239,24 @@
         final String[] idArray = id.split(BinaryDictionaryGetter.ID_CATEGORY_SEPARATOR);
         // An id is supposed to be in format category:locale, so splitting on the separator
         // should yield a 2-elements array
-        if (2 != idArray.length) return false;
+        if (2 != idArray.length) {
+            return false;
+        }
         return BinaryDictionaryGetter.MAIN_DICTIONARY_CATEGORY.equals(idArray[0]);
     }
 
     /**
+     * Find out whether a dictionary is available for this locale.
+     * @param context the context on which to check resources.
+     * @param locale the locale to check for.
+     * @return whether a (non-placeholder) dictionary is available or not.
+     */
+    public static boolean isDictionaryAvailable(final Context context, final Locale locale) {
+        final Resources res = context.getResources();
+        return 0 != getMainDictionaryResourceIdIfAvailableForLocale(res, locale);
+    }
+
+    /**
      * Helper method to return a dictionary res id for a locale, or 0 if none.
      * @param res resources for the app
      * @param locale dictionary locale
@@ -266,7 +293,9 @@
      */
     public static int getMainDictionaryResourceId(final Resources res, final Locale locale) {
         int resourceId = getMainDictionaryResourceIdIfAvailableForLocale(res, locale);
-        if (0 != resourceId) return resourceId;
+        if (0 != resourceId) {
+            return resourceId;
+        }
         return res.getIdentifier(DEFAULT_MAIN_DICT, "raw", RESOURCE_PACKAGE_NAME);
     }
 
@@ -335,10 +364,10 @@
         if (header == null) {
             return null;
         }
-        final String id = header.getId();
-        final Locale locale = LocaleUtils.constructLocaleFromString(header.getLocaleString());
+        final String id = header.mIdString;
+        final Locale locale = LocaleUtils.constructLocaleFromString(header.mLocaleString);
         final String description = header.getDescription();
-        final String version = header.getVersion();
+        final String version = header.mVersionString;
         return new DictionaryInfo(id, locale, description, fileAddress, Integer.parseInt(version));
     }
 
@@ -366,10 +395,13 @@
         if (null != directoryList) {
             for (final File directory : directoryList) {
                 final String localeString = getWordListIdFromFileName(directory.getName());
-                File[] dicts = BinaryDictionaryGetter.getCachedWordLists(localeString, context);
+                final File[] dicts = BinaryDictionaryGetter.getCachedWordLists(
+                        localeString, context);
                 for (final File dict : dicts) {
                     final String wordListId = getWordListIdFromFileName(dict.getName());
-                    if (!DictionaryInfoUtils.isMainWordListId(wordListId)) continue;
+                    if (!DictionaryInfoUtils.isMainWordListId(wordListId)) {
+                        continue;
+                    }
                     final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
                     final AssetFileAddress fileAddress = AssetFileAddress.makeFromFile(dict);
                     final DictionaryInfo dictionaryInfo =
@@ -377,7 +409,9 @@
                     // Protect against cases of a less-specific dictionary being found, like an
                     // en dictionary being used for an en_US locale. In this case, the en dictionary
                     // should be used for en_US but discounted for listing purposes.
-                    if (dictionaryInfo == null || !dictionaryInfo.mLocale.equals(locale)) continue;
+                    if (dictionaryInfo == null || !dictionaryInfo.mLocale.equals(locale)) {
+                        continue;
+                    }
                     addOrUpdateDictInfo(dictList, dictionaryInfo);
                 }
             }
@@ -391,14 +425,18 @@
             final int resourceId =
                     DictionaryInfoUtils.getMainDictionaryResourceIdIfAvailableForLocale(
                             context.getResources(), locale);
-            if (0 == resourceId) continue;
+            if (0 == resourceId) {
+                continue;
+            }
             final AssetFileAddress fileAddress =
                     BinaryDictionaryGetter.loadFallbackResource(context, resourceId);
             final DictionaryInfo dictionaryInfo = createDictionaryInfoFromFileAddress(fileAddress);
             // Protect against cases of a less-specific dictionary being found, like an
             // en dictionary being used for an en_US locale. In this case, the en dictionary
             // should be used for en_US but discounted for listing purposes.
-            if (!dictionaryInfo.mLocale.equals(locale)) continue;
+            if (!dictionaryInfo.mLocale.equals(locale)) {
+                continue;
+            }
             addOrUpdateDictInfo(dictList, dictionaryInfo);
         }
 
@@ -408,7 +446,9 @@
     @UsedForTesting
     public static boolean looksValidForDictionaryInsertion(final CharSequence text,
             final SpacingAndPunctuations spacingAndPunctuations) {
-        if (TextUtils.isEmpty(text)) return false;
+        if (TextUtils.isEmpty(text)) {
+            return false;
+        }
         final int length = text.length();
         if (length > Constants.DICTIONARY_MAX_WORD_LENGTH) {
             return false;
@@ -424,7 +464,9 @@
                 digitCount += charCount;
                 continue;
             }
-            if (!spacingAndPunctuations.isWordCodePoint(codePoint)) return false;
+            if (!spacingAndPunctuations.isWordCodePoint(codePoint)) {
+                return false;
+            }
         }
         // We reject strings entirely comprised of digits to avoid using PIN codes or credit
         // card numbers. It would come in handy for word prediction though; a good example is
diff --git a/java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingExactMatchesAndSuggestions.java b/java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingExactMatchesAndSuggestions.java
index 9c6a948..0c01a73 100644
--- a/java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingExactMatchesAndSuggestions.java
+++ b/java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingExactMatchesAndSuggestions.java
@@ -143,7 +143,7 @@
         final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
                 mContext, editorInfo);
         final Resources res = mContext.getResources();
-        final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
+        final int keyboardWidth = ResourceUtils.getKeyboardFrameWidth(res);
         final int keyboardHeight = ResourceUtils.getDefaultKeyboardHeight(res);
         builder.setKeyboardGeometry(keyboardWidth, keyboardHeight);
         builder.setSubtype(new RichInputMethodSubtype(subtype));
diff --git a/java/src/com/android/inputmethod/latin/utils/ResourceUtils.java b/java/src/com/android/inputmethod/latin/utils/ResourceUtils.java
index cc0d470..0a75787 100644
--- a/java/src/com/android/inputmethod/latin/utils/ResourceUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/ResourceUtils.java
@@ -182,18 +182,63 @@
         return matchedAll;
     }
 
-    public static int getDefaultKeyboardWidth(final Resources res) {
+    public static int getKeyboardFrameWidth(final Resources res) {
         final DisplayMetrics dm = res.getDisplayMetrics();
         return dm.widthPixels;
     }
 
-    public static int getKeyboardHeight(final Resources res, final SettingsValues settingsValues) {
-        final int defaultKeyboardHeight = getDefaultKeyboardHeight(res);
+    public static int getKeyboardWidth(final Resources res, final SettingsValues settingsValues) {
+        final int frameWidth = getKeyboardFrameWidth(res);
         if (settingsValues.mHasKeyboardResize) {
-            // mKeyboardHeightScale Ranges from [.5,1.2], from xml/prefs_screen_debug.xml
-            return (int)(defaultKeyboardHeight * settingsValues.mKeyboardHeightScale);
+            final float widthFraction = 1f - settingsValues.mKeyboardLeftMarginScale
+                    - settingsValues.mKeyboardRightMarginScale;
+            return (int)(frameWidth * widthFraction);
         }
-        return defaultKeyboardHeight;
+        return frameWidth;
+    }
+
+    public static int getKeyboardFrameHeight(final Resources res,
+            final SettingsValues settingsValues) {
+        if (settingsValues.mHasKeyboardResize) {
+            return getKeyboardHeight(res, settingsValues)
+                    + getKeyboardBottomMargin(res, settingsValues);
+        }
+        return getDefaultKeyboardHeight(res);
+    }
+
+    public static int getKeyboardHeight(final Resources res, final SettingsValues settingsValues) {
+        final int keyboardHeight = getDefaultKeyboardHeight(res);
+        if (settingsValues.mHasKeyboardResize) {
+            return (int)(keyboardHeight * settingsValues.mKeyboardHeightScale);
+        }
+        return keyboardHeight;
+    }
+
+    public static int getKeyboardLeftMargin(
+            final Resources res, final SettingsValues settingsValues) {
+        if (settingsValues.mHasKeyboardResize) {
+          final int frameWidth = getKeyboardFrameWidth(res);
+            return (int)(frameWidth * settingsValues.mKeyboardLeftMarginScale);
+        }
+        return 0;
+    }
+
+    public static int getKeyboardRightMargin(
+            final Resources res, final SettingsValues settingsValues) {
+        if (settingsValues.mHasKeyboardResize) {
+            final int frameWidth = getKeyboardFrameWidth(res);
+            return (int)(frameWidth * settingsValues.mKeyboardRightMarginScale);
+        }
+        return 0;
+    }
+
+    public static int getKeyboardBottomMargin(
+            final Resources res, final SettingsValues settingsValues) {
+        if (settingsValues.mHasKeyboardResize) {
+            final int defaultHeight = getDefaultKeyboardHeight(res);
+            return (int)(defaultHeight * settingsValues.mKeyboardBottomMarginScale);
+        }
+        return 0;
     }
 
     public static int getDefaultKeyboardHeight(final Resources res) {
diff --git a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java
index 0e7f471..54a3fc3 100644
--- a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java
@@ -199,8 +199,7 @@
         if (sExceptionalLocaleDisplayedInRootLocale.containsKey(localeString)) {
             languageString = localeString;
         } else {
-            final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
-            languageString = locale.getLanguage();
+            languageString = LocaleUtils.constructLocaleFromString(localeString).getLanguage();
         }
         return getSubtypeLocaleDisplayNameInternal(languageString, displayLocale);
     }
@@ -232,8 +231,8 @@
             };
             displayName = getExceptionalName.runInLocale(sResources, displayLocale);
         } else {
-            final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
-            displayName = locale.getDisplayName(displayLocale);
+            displayName = LocaleUtils.constructLocaleFromString(localeString)
+                    .getDisplayName(displayLocale);
         }
         return StringUtils.capitalizeFirstCodePoint(displayName, displayLocale);
     }
diff --git a/native/dicttoolkit/src/command_executors/makedict_executor.cpp b/native/dicttoolkit/src/command_executors/makedict_executor.cpp
index 8a84e80..4b0a5ae 100644
--- a/native/dicttoolkit/src/command_executors/makedict_executor.cpp
+++ b/native/dicttoolkit/src/command_executors/makedict_executor.cpp
@@ -24,6 +24,12 @@
 const char *const MakedictExecutor::COMMAND_NAME = "makedict";
 
 /* static */ int MakedictExecutor::run(const int argc, char **argv) {
+    const ArgumentsAndOptions argumentsAndOptions =
+            getArgumentsParser().parseArguments(argc, argv, true /* printErrorMessages */);
+    if (!argumentsAndOptions.isValid()) {
+        printUsage();
+        return 1;
+    }
     fprintf(stderr, "Command '%s' has not been implemented yet.\n", COMMAND_NAME);
     return 0;
 }
diff --git a/native/dicttoolkit/src/utils/arguments_and_options.h b/native/dicttoolkit/src/utils/arguments_and_options.h
index d8f5985..2d81b1e 100644
--- a/native/dicttoolkit/src/utils/arguments_and_options.h
+++ b/native/dicttoolkit/src/utils/arguments_and_options.h
@@ -42,6 +42,29 @@
         return mOptions.find(optionName) != mOptions.end();
     }
 
+    const std::string &getOptionValue(const std::string &optionName) const {
+        const auto &it = mOptions.find(optionName);
+        ASSERT(it != mOptions.end());
+        return it->second;
+    }
+
+    bool hasArgument(const std::string &name) const {
+        const auto &it = mArguments.find(name);
+        return it != mArguments.end() && !it->second.empty();
+    }
+
+    const std::string &getSingleArgument(const std::string &name) const {
+        const auto &it = mArguments.find(name);
+        ASSERT(it != mArguments.end() && !it->second.empty());
+        return it->second.front();
+    }
+
+    const std::vector<std::string> &getVariableLengthArguments(const std::string &name) const {
+        const auto &it = mArguments.find(name);
+        ASSERT(it != mArguments.end());
+        return it->second;
+    }
+
  private:
     DISALLOW_ASSIGNMENT_OPERATOR(ArgumentsAndOptions);
 
diff --git a/native/dicttoolkit/src/utils/arguments_parser.cpp b/native/dicttoolkit/src/utils/arguments_parser.cpp
index 52cc7b2..1451284 100644
--- a/native/dicttoolkit/src/utils/arguments_parser.cpp
+++ b/native/dicttoolkit/src/utils/arguments_parser.cpp
@@ -21,7 +21,7 @@
 namespace latinime {
 namespace dicttoolkit {
 
-const int ArgumentSpec::UNLIMITED_COUNT = -1;
+const size_t ArgumentSpec::UNLIMITED_COUNT = S_INT_MAX;
 
 bool ArgumentsParser::validateSpecs() const {
     std::unordered_set<std::string> argumentNameSet;
@@ -53,7 +53,7 @@
         const std::string &optionName = option.first;
         const OptionSpec &spec = option.second;
         printf(" [-%s", optionName.c_str());
-        if (spec.takeValue()) {
+        if (spec.needsValue()) {
             printf(" <%s>", spec.getValueName().c_str());
         }
         printf("]");
@@ -74,11 +74,11 @@
         const std::string &optionName = option.first;
         const OptionSpec &spec = option.second;
         printf(" -%s", optionName.c_str());
-        if (spec.takeValue()) {
+        if (spec.needsValue()) {
             printf(" <%s>", spec.getValueName().c_str());
         }
         printf("\t\t\t%s", spec.getDescription().c_str());
-        if (spec.takeValue() && !spec.getDefaultValue().empty()) {
+        if (spec.needsValue() && !spec.getDefaultValue().empty()) {
             printf("\tdefault: %s", spec.getDefaultValue().c_str());
         }
         printf("\n");
@@ -89,9 +89,76 @@
     printf("\n\n");
 }
 
-const ArgumentsAndOptions ArgumentsParser::parseArguments(const int argc, char **argv) const {
-    // TODO: Implement
-    return ArgumentsAndOptions();
+const ArgumentsAndOptions ArgumentsParser::parseArguments(const int argc, char **argv,
+        const bool printErrorMessage) const {
+    if (argc <= 0) {
+        AKLOGE("Invalid argc (%d).", argc);
+        ASSERT(false);
+        return ArgumentsAndOptions();
+    }
+    std::unordered_map<std::string, std::string> options;
+    for (const auto &entry : mOptionSpecs) {
+        const std::string &optionName = entry.first;
+        const OptionSpec &optionSpec = entry.second;
+        if (optionSpec.needsValue() && !optionSpec.getDefaultValue().empty()) {
+            // Set default value.
+            options[optionName] = optionSpec.getDefaultValue();
+        }
+    }
+    std::unordered_map<std::string, std::vector<std::string>> arguments;
+    auto argumentSpecIt = mArgumentSpecs.cbegin();
+    for (int i = 1; i < argc; ++i) {
+        const std::string arg = argv[i];
+        if (arg.length() > 1 && arg[0] == '-') {
+            // option
+            const std::string optionName = arg.substr(1);
+            const auto it = mOptionSpecs.find(optionName);
+            if (it == mOptionSpecs.end()) {
+                if (printErrorMessage) {
+                    fprintf(stderr, "Unknown option: '%s'\n", optionName.c_str());
+                }
+                return ArgumentsAndOptions();
+            }
+            std::string optionValue;
+            if (it->second.needsValue()) {
+                ++i;
+                if (i >= argc) {
+                    if (printErrorMessage) {
+                        fprintf(stderr, "Missing argument for option '%s'\n", optionName.c_str());
+                    }
+                    return ArgumentsAndOptions();
+                }
+                optionValue = argv[i];
+            }
+            options[optionName] = optionValue;
+        } else {
+            // argument
+            if (argumentSpecIt == mArgumentSpecs.end()) {
+                if (printErrorMessage) {
+                    fprintf(stderr, "Too many arguments.\n");
+                }
+                return ArgumentsAndOptions();
+            }
+            arguments[argumentSpecIt->getName()].push_back(arg);
+            if (arguments[argumentSpecIt->getName()].size() >= argumentSpecIt->getMaxCount()) {
+                ++argumentSpecIt;
+            }
+        }
+    }
+
+    if (argumentSpecIt != mArgumentSpecs.end()) {
+        const auto &it = arguments.find(argumentSpecIt->getName());
+        const size_t minCount = argumentSpecIt->getMinCount();
+        const size_t actualcount = it == arguments.end() ? 0 : it->second.size();
+        if (minCount > actualcount) {
+            if (printErrorMessage) {
+                fprintf(stderr, "Not enough arguments. %zd argumant(s) required for <%s>\n",
+                        minCount, argumentSpecIt->getName().c_str());
+            }
+            return ArgumentsAndOptions();
+        }
+    }
+    return ArgumentsAndOptions(std::move(options), std::move(arguments));
 }
 
 } // namespace dicttoolkit
diff --git a/native/dicttoolkit/src/utils/arguments_parser.h b/native/dicttoolkit/src/utils/arguments_parser.h
index 510a872..32bd328 100644
--- a/native/dicttoolkit/src/utils/arguments_parser.h
+++ b/native/dicttoolkit/src/utils/arguments_parser.h
@@ -35,29 +35,29 @@
 
     static OptionSpec keyValueOption(const std::string &valueName, const std::string &defaultValue,
             const std::string &description) {
-        return OptionSpec(true /* takeValue */, valueName, defaultValue, description);
+        return OptionSpec(true /* needsValue */, valueName, defaultValue, description);
     }
 
     static OptionSpec switchOption(const std::string &description) {
-        return OptionSpec(false /* takeValue */, "" /* valueName */, "" /* defaultValue */,
+        return OptionSpec(false /* needsValue */, "" /* valueName */, "" /* defaultValue */,
                 description);
     }
 
-    bool takeValue() const { return mTakeValue; }
+    bool needsValue() const { return mNeedsValue; }
     const std::string &getValueName() const { return mValueName; }
     const std::string &getDefaultValue() const { return mDefaultValue; }
     const std::string &getDescription() const { return mDescription; }
 
  private:
-    OptionSpec(const bool takeValue, const std::string &valueName, const std::string &defaultValue,
+    OptionSpec(const bool needsValue, const std::string &valueName, const std::string &defaultValue,
             const std::string &description)
-            : mTakeValue(takeValue), mValueName(valueName), mDefaultValue(defaultValue),
+            : mNeedsValue(needsValue), mValueName(valueName), mDefaultValue(defaultValue),
               mDescription(description) {}
 
     // Whether the option have to be used with a value or just a switch.
-    // e.g. 'f' in "command -f /path/to/file" is mTakeValue == true.
-    //      'f' in "command -f -t" is mTakeValue == false.
-    bool mTakeValue;
+    // e.g. 'f' in "command -f /path/to/file" is mNeedsValue == true.
+    //      'f' in "command -f -t" is mNeedsValue == false.
+    bool mNeedsValue;
     // Name of the value used to show usage.
     std::string mValueName;
     std::string mDefaultValue;
@@ -66,32 +66,32 @@
 
 class ArgumentSpec {
  public:
-    static const int UNLIMITED_COUNT;
+    static const size_t UNLIMITED_COUNT;
 
     static ArgumentSpec singleArgument(const std::string &name, const std::string &description) {
         return ArgumentSpec(name, 1 /* minCount */, 1 /* maxCount */, description);
     }
 
-    static ArgumentSpec variableLengthArguments(const std::string &name, const int minCount,
-            const int maxCount, const std::string &description) {
+    static ArgumentSpec variableLengthArguments(const std::string &name, const size_t minCount,
+            const size_t maxCount, const std::string &description) {
         return ArgumentSpec(name, minCount, maxCount, description);
     }
 
     const std::string &getName() const { return mName; }
-    int getMinCount() const { return mMinCount; }
-    int getMaxCount() const { return mMaxCount; }
+    size_t getMinCount() const { return mMinCount; }
+    size_t getMaxCount() const { return mMaxCount; }
     const std::string &getDescription() const { return mDescription; }
 
  private:
     DISALLOW_DEFAULT_CONSTRUCTOR(ArgumentSpec);
 
-    ArgumentSpec(const std::string &name, const int minCount, const int maxCount,
+    ArgumentSpec(const std::string &name, const size_t minCount, const size_t maxCount,
             const std::string &description)
             : mName(name), mMinCount(minCount), mMaxCount(maxCount), mDescription(description) {}
 
     const std::string mName;
-    const int mMinCount;
-    const int mMaxCount;
+    const size_t mMinCount;
+    const size_t mMaxCount;
     const std::string mDescription;
 };
 
@@ -101,7 +101,8 @@
             const std::vector<ArgumentSpec> &&argumentSpecs)
             : mOptionSpecs(std::move(optionSpecs)), mArgumentSpecs(std::move(argumentSpecs)) {}
 
-    const ArgumentsAndOptions parseArguments(const int argc, char **argv) const;
+    const ArgumentsAndOptions parseArguments(const int argc, char **argv,
+            const bool printErrorMessage) const;
     bool validateSpecs() const;
     void printUsage(const std::string &commandName, const std::string &description) const;
 
diff --git a/native/dicttoolkit/tests/utils/arguments_parser_test.cpp b/native/dicttoolkit/tests/utils/arguments_parser_test.cpp
index e79425b..58b4998 100644
--- a/native/dicttoolkit/tests/utils/arguments_parser_test.cpp
+++ b/native/dicttoolkit/tests/utils/arguments_parser_test.cpp
@@ -68,6 +68,80 @@
     }
 }
 
+int initArgv(char *mutableCommandLine, char **argv) {
+    bool readingSeparator = false;
+    int argc = 1;
+    argv[0] = mutableCommandLine;
+    const size_t length = strlen(mutableCommandLine);
+    for (size_t i = 0; i < length; ++i) {
+        if (mutableCommandLine[i] != ' ' && readingSeparator) {
+            readingSeparator = false;
+            argv[argc] = mutableCommandLine + i;
+            ++argc;
+        } else if (mutableCommandLine[i] == ' ' && !readingSeparator) {
+            readingSeparator = true;
+            mutableCommandLine[i] = '\0';
+        }
+    }
+    argv[argc] = nullptr;
+    return argc;
+}
+
+TEST(ArgumentsParserTests, TestParseArguments) {
+    std::unordered_map<std::string, OptionSpec> optionSpecs;
+    optionSpecs["a"] = OptionSpec::switchOption("description");
+    optionSpecs["b"] = OptionSpec::keyValueOption("valueName", "default", "description");
+    const std::vector<ArgumentSpec> argumentSpecs = {
+        ArgumentSpec::singleArgument("arg0", "description"),
+        ArgumentSpec::variableLengthArguments("arg1", 0 /* minCount */,  2 /* maxCount */,
+                "description"),
+    };
+    const ArgumentsParser parser =
+            ArgumentsParser(std::move(optionSpecs), std::move(argumentSpecs));
+
+    {
+        char kMutableCommandLine[1024] = "command arg";
+        char *argv[128] = {};
+        const int argc = initArgv(kMutableCommandLine, argv);
+        ASSERT_EQ(2, argc);
+        const ArgumentsAndOptions argumentsAndOptions = parser.parseArguments(
+                argc, argv, false /* printErrorMessages */);
+        EXPECT_FALSE(argumentsAndOptions.hasOption("a"));
+        EXPECT_EQ("default", argumentsAndOptions.getOptionValue("b"));
+        EXPECT_EQ("arg", argumentsAndOptions.getSingleArgument("arg0"));
+        EXPECT_FALSE(argumentsAndOptions.hasArgument("arg1"));
+    }
+    {
+        char kArgumentBuffer[1024] = "command -a arg arg";
+        char *argv[128] = {};
+        const int argc = initArgv(kArgumentBuffer, argv);
+        ASSERT_EQ(4, argc);
+        const ArgumentsAndOptions argumentsAndOptions = parser.parseArguments(
+                argc, argv, false /* printErrorMessages */);
+        EXPECT_TRUE(argumentsAndOptions.hasOption("a"));
+        EXPECT_EQ("default", argumentsAndOptions.getOptionValue("b"));
+        EXPECT_EQ("arg", argumentsAndOptions.getSingleArgument("arg0"));
+        EXPECT_TRUE(argumentsAndOptions.hasArgument("arg1"));
+        EXPECT_EQ(1u, argumentsAndOptions.getVariableLengthArguments("arg1").size());
+    }
+    {
+        char kArgumentBuffer[1024] = "command -b value arg arg1 arg2";
+        char *argv[128] = {};
+        const int argc = initArgv(kArgumentBuffer, argv);
+        ASSERT_EQ(6, argc);
+        const ArgumentsAndOptions argumentsAndOptions = parser.parseArguments(
+                argc, argv, false /* printErrorMessages */);
+        EXPECT_FALSE(argumentsAndOptions.hasOption("a"));
+        EXPECT_EQ("value", argumentsAndOptions.getOptionValue("b"));
+        EXPECT_EQ("arg", argumentsAndOptions.getSingleArgument("arg0"));
+        const std::vector<std::string> &arg1 =
+                argumentsAndOptions.getVariableLengthArguments("arg1");
+        EXPECT_EQ(2u, arg1.size());
+        EXPECT_EQ("arg1", arg1[0]);
+        EXPECT_EQ("arg2", arg1[1]);
+    }
+}
+
 } // namespace
 } // namespace dicttoolkit
 } // namespace latinime
diff --git a/native/jni/src/defines.h b/native/jni/src/defines.h
index 0e67b4d..10b930e 100644
--- a/native/jni/src/defines.h
+++ b/native/jni/src/defines.h
@@ -275,7 +275,7 @@
 #define MAX_POINTER_COUNT_G 2
 
 // (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram is supported.
-#define MAX_PREV_WORD_COUNT_FOR_N_GRAM 2
+#define MAX_PREV_WORD_COUNT_FOR_N_GRAM 3
 
 #define DISALLOW_DEFAULT_CONSTRUCTOR(TypeName) \
   TypeName() = delete
diff --git a/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp b/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp
index 300e96c..c93f310 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp
@@ -18,6 +18,8 @@
 
 #include <algorithm>
 
+#include "utils/ngram_utils.h"
+
 namespace latinime {
 
 // Note that these are corresponding definitions in Java side in DictionaryHeader.
@@ -28,9 +30,12 @@
 const char *const HeaderPolicy::IS_DECAYING_DICT_KEY = "USES_FORGETTING_CURVE";
 const char *const HeaderPolicy::DATE_KEY = "date";
 const char *const HeaderPolicy::LAST_DECAYED_TIME_KEY = "LAST_DECAYED_TIME";
-const char *const HeaderPolicy::UNIGRAM_COUNT_KEY = "UNIGRAM_COUNT";
-const char *const HeaderPolicy::BIGRAM_COUNT_KEY = "BIGRAM_COUNT";
-const char *const HeaderPolicy::TRIGRAM_COUNT_KEY = "TRIGRAM_COUNT";
+const char *const HeaderPolicy::NGRAM_COUNT_KEYS[] =
+        {"UNIGRAM_COUNT", "BIGRAM_COUNT", "TRIGRAM_COUNT", "QUADGRAM_COUNT"};
+const char *const HeaderPolicy::MAX_NGRAM_COUNT_KEYS[] =
+        {"MAX_UNIGRAM_ENTRY_COUNT", "MAX_BIGRAM_ENTRY_COUNT", "MAX_TRIGRAM_ENTRY_COUNT",
+                "MAX_QUADGRAM_ENTRY_COUNT"};
+const int HeaderPolicy::DEFAULT_MAX_NGRAM_COUNTS[] = {10000, 30000, 30000, 30000};
 const char *const HeaderPolicy::EXTENDED_REGION_SIZE_KEY = "EXTENDED_REGION_SIZE";
 // Historical info is information that is needed to support decaying such as timestamp, level and
 // count.
@@ -39,18 +44,10 @@
 const char *const HeaderPolicy::FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID_KEY =
         "FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID";
 
-const char *const HeaderPolicy::MAX_UNIGRAM_COUNT_KEY = "MAX_UNIGRAM_ENTRY_COUNT";
-const char *const HeaderPolicy::MAX_BIGRAM_COUNT_KEY = "MAX_BIGRAM_ENTRY_COUNT";
-const char *const HeaderPolicy::MAX_TRIGRAM_COUNT_KEY = "MAX_TRIGRAM_ENTRY_COUNT";
-
 const int HeaderPolicy::DEFAULT_MULTIPLE_WORDS_DEMOTION_RATE = 100;
 const float HeaderPolicy::MULTIPLE_WORD_COST_MULTIPLIER_SCALE = 100.0f;
 const int HeaderPolicy::DEFAULT_FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID = 3;
 
-const int HeaderPolicy::DEFAULT_MAX_UNIGRAM_COUNT = 10000;
-const int HeaderPolicy::DEFAULT_MAX_BIGRAM_COUNT = 30000;
-const int HeaderPolicy::DEFAULT_MAX_TRIGRAM_COUNT = 30000;
-
 // Used for logging. Question mark is used to indicate that the key is not found.
 void HeaderPolicy::readHeaderValueOrQuestionMark(const char *const key, int *outValue,
         int outValueSize) const {
@@ -126,15 +123,22 @@
     return true;
 }
 
+namespace {
+
+int getIndexFromNgramType(const NgramType ngramType) {
+    return static_cast<int>(ngramType);
+}
+
+} // namespace
+
 void HeaderPolicy::fillInHeader(const bool updatesLastDecayedTime,
         const EntryCounts &entryCounts, const int extendedRegionSize,
         DictionaryHeaderStructurePolicy::AttributeMap *outAttributeMap) const {
-    HeaderReadWriteUtils::setIntAttribute(outAttributeMap, UNIGRAM_COUNT_KEY,
-            entryCounts.getUnigramCount());
-    HeaderReadWriteUtils::setIntAttribute(outAttributeMap, BIGRAM_COUNT_KEY,
-            entryCounts.getBigramCount());
-    HeaderReadWriteUtils::setIntAttribute(outAttributeMap, TRIGRAM_COUNT_KEY,
-            entryCounts.getTrigramCount());
+    for (const auto ngramType : AllNgramTypes::ASCENDING) {
+        HeaderReadWriteUtils::setIntAttribute(outAttributeMap,
+                NGRAM_COUNT_KEYS[getIndexFromNgramType(ngramType)],
+                entryCounts.getNgramCount(ngramType));
+    }
     HeaderReadWriteUtils::setIntAttribute(outAttributeMap, EXTENDED_REGION_SIZE_KEY,
             extendedRegionSize);
     // Set the current time as the generation time.
@@ -155,4 +159,25 @@
     return attributeMap;
 }
 
+/* static */ const EntryCounts HeaderPolicy::readNgramCounts() const {
+    MutableEntryCounters entryCounters;
+    for (const auto ngramType : AllNgramTypes::ASCENDING) {
+        const int entryCount = HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
+                NGRAM_COUNT_KEYS[getIndexFromNgramType(ngramType)], 0 /* defaultValue */);
+        entryCounters.setNgramCount(ngramType, entryCount);
+    }
+    return entryCounters.getEntryCounts();
+}
+
+/* static */ const EntryCounts HeaderPolicy::readMaxNgramCounts() const {
+    MutableEntryCounters entryCounters;
+    for (const auto ngramType : AllNgramTypes::ASCENDING) {
+        const int index = getIndexFromNgramType(ngramType);
+        const int maxEntryCount = HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
+                MAX_NGRAM_COUNT_KEYS[index], DEFAULT_MAX_NGRAM_COUNTS[index]);
+        entryCounters.setNgramCount(ngramType, maxEntryCount);
+    }
+    return entryCounters.getEntryCounts();
+}
+
 } // namespace latinime
diff --git a/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.h b/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.h
index 7a5acd7..f76931b 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/header/header_policy.h
@@ -46,12 +46,7 @@
                       DATE_KEY, TimeKeeper::peekCurrentTime() /* defaultValue */)),
               mLastDecayedTime(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
                       LAST_DECAYED_TIME_KEY, TimeKeeper::peekCurrentTime() /* defaultValue */)),
-              mUnigramCount(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
-                      UNIGRAM_COUNT_KEY, 0 /* defaultValue */)),
-              mBigramCount(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
-                      BIGRAM_COUNT_KEY, 0 /* defaultValue */)),
-              mTrigramCount(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
-                      TRIGRAM_COUNT_KEY, 0 /* defaultValue */)),
+              mNgramCounts(readNgramCounts()), mMaxNgramCounts(readMaxNgramCounts()),
               mExtendedRegionSize(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
                       EXTENDED_REGION_SIZE_KEY, 0 /* defaultValue */)),
               mHasHistoricalInfoOfWords(HeaderReadWriteUtils::readBoolAttributeValue(
@@ -59,12 +54,6 @@
               mForgettingCurveProbabilityValuesTableId(HeaderReadWriteUtils::readIntAttributeValue(
                       &mAttributeMap, FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID_KEY,
                       DEFAULT_FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID)),
-              mMaxUnigramCount(HeaderReadWriteUtils::readIntAttributeValue(
-                      &mAttributeMap, MAX_UNIGRAM_COUNT_KEY, DEFAULT_MAX_UNIGRAM_COUNT)),
-              mMaxBigramCount(HeaderReadWriteUtils::readIntAttributeValue(
-                      &mAttributeMap, MAX_BIGRAM_COUNT_KEY, DEFAULT_MAX_BIGRAM_COUNT)),
-              mMaxTrigramCount(HeaderReadWriteUtils::readIntAttributeValue(
-                      &mAttributeMap, MAX_TRIGRAM_COUNT_KEY, DEFAULT_MAX_TRIGRAM_COUNT)),
               mCodePointTable(HeaderReadWriteUtils::readCodePointTable(&mAttributeMap)) {}
 
     // Constructs header information using an attribute map.
@@ -82,18 +71,13 @@
                       DATE_KEY, TimeKeeper::peekCurrentTime() /* defaultValue */)),
               mLastDecayedTime(HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
                       DATE_KEY, TimeKeeper::peekCurrentTime() /* defaultValue */)),
-              mUnigramCount(0), mBigramCount(0), mTrigramCount(0), mExtendedRegionSize(0),
+              mNgramCounts(readNgramCounts()), mMaxNgramCounts(readMaxNgramCounts()),
+              mExtendedRegionSize(0),
               mHasHistoricalInfoOfWords(HeaderReadWriteUtils::readBoolAttributeValue(
                       &mAttributeMap, HAS_HISTORICAL_INFO_KEY, false /* defaultValue */)),
               mForgettingCurveProbabilityValuesTableId(HeaderReadWriteUtils::readIntAttributeValue(
                       &mAttributeMap, FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID_KEY,
                       DEFAULT_FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID)),
-              mMaxUnigramCount(HeaderReadWriteUtils::readIntAttributeValue(
-                      &mAttributeMap, MAX_UNIGRAM_COUNT_KEY, DEFAULT_MAX_UNIGRAM_COUNT)),
-              mMaxBigramCount(HeaderReadWriteUtils::readIntAttributeValue(
-                      &mAttributeMap, MAX_BIGRAM_COUNT_KEY, DEFAULT_MAX_BIGRAM_COUNT)),
-              mMaxTrigramCount(HeaderReadWriteUtils::readIntAttributeValue(
-                      &mAttributeMap, MAX_TRIGRAM_COUNT_KEY, DEFAULT_MAX_TRIGRAM_COUNT)),
               mCodePointTable(HeaderReadWriteUtils::readCodePointTable(&mAttributeMap)) {}
 
     // Copy header information
@@ -105,15 +89,12 @@
               mRequiresGermanUmlautProcessing(headerPolicy->mRequiresGermanUmlautProcessing),
               mIsDecayingDict(headerPolicy->mIsDecayingDict),
               mDate(headerPolicy->mDate), mLastDecayedTime(headerPolicy->mLastDecayedTime),
-              mUnigramCount(headerPolicy->mUnigramCount), mBigramCount(headerPolicy->mBigramCount),
-              mTrigramCount(headerPolicy->mTrigramCount),
+              mNgramCounts(headerPolicy->mNgramCounts),
+              mMaxNgramCounts(headerPolicy->mMaxNgramCounts),
               mExtendedRegionSize(headerPolicy->mExtendedRegionSize),
               mHasHistoricalInfoOfWords(headerPolicy->mHasHistoricalInfoOfWords),
               mForgettingCurveProbabilityValuesTableId(
                       headerPolicy->mForgettingCurveProbabilityValuesTableId),
-              mMaxUnigramCount(headerPolicy->mMaxUnigramCount),
-              mMaxBigramCount(headerPolicy->mMaxBigramCount),
-              mMaxTrigramCount(headerPolicy->mMaxTrigramCount),
               mCodePointTable(headerPolicy->mCodePointTable) {}
 
     // Temporary dummy header.
@@ -121,10 +102,9 @@
             : mDictFormatVersion(FormatUtils::UNKNOWN_VERSION), mDictionaryFlags(0), mSize(0),
               mAttributeMap(), mLocale(CharUtils::EMPTY_STRING), mMultiWordCostMultiplier(0.0f),
               mRequiresGermanUmlautProcessing(false), mIsDecayingDict(false),
-              mDate(0), mLastDecayedTime(0), mUnigramCount(0), mBigramCount(0), mTrigramCount(0),
+              mDate(0), mLastDecayedTime(0), mNgramCounts(), mMaxNgramCounts(),
               mExtendedRegionSize(0), mHasHistoricalInfoOfWords(false),
-              mForgettingCurveProbabilityValuesTableId(0), mMaxUnigramCount(0), mMaxBigramCount(0),
-              mMaxTrigramCount(0), mCodePointTable(nullptr) {}
+              mForgettingCurveProbabilityValuesTableId(0), mCodePointTable(nullptr) {}
 
     ~HeaderPolicy() {}
 
@@ -186,16 +166,12 @@
         return mLastDecayedTime;
     }
 
-    AK_FORCE_INLINE int getUnigramCount() const {
-        return mUnigramCount;
+    AK_FORCE_INLINE const EntryCounts &getNgramCounts() const {
+        return mNgramCounts;
     }
 
-    AK_FORCE_INLINE int getBigramCount() const {
-        return mBigramCount;
-    }
-
-    AK_FORCE_INLINE int getTrigramCount() const {
-        return mTrigramCount;
+    AK_FORCE_INLINE const EntryCounts getMaxNgramCounts() const {
+        return mMaxNgramCounts;
     }
 
     AK_FORCE_INLINE int getExtendedRegionSize() const {
@@ -219,18 +195,6 @@
         return mForgettingCurveProbabilityValuesTableId;
     }
 
-    AK_FORCE_INLINE int getMaxUnigramCount() const {
-        return mMaxUnigramCount;
-    }
-
-    AK_FORCE_INLINE int getMaxBigramCount() const {
-        return mMaxBigramCount;
-    }
-
-    AK_FORCE_INLINE int getMaxTrigramCount() const {
-        return mMaxTrigramCount;
-    }
-
     void readHeaderValueOrQuestionMark(const char *const key,
             int *outValue, int outValueSize) const;
 
@@ -262,24 +226,18 @@
     static const char *const IS_DECAYING_DICT_KEY;
     static const char *const DATE_KEY;
     static const char *const LAST_DECAYED_TIME_KEY;
-    static const char *const UNIGRAM_COUNT_KEY;
-    static const char *const BIGRAM_COUNT_KEY;
-    static const char *const TRIGRAM_COUNT_KEY;
+    static const char *const NGRAM_COUNT_KEYS[];
+    static const char *const MAX_NGRAM_COUNT_KEYS[];
+    static const int DEFAULT_MAX_NGRAM_COUNTS[];
     static const char *const EXTENDED_REGION_SIZE_KEY;
     static const char *const HAS_HISTORICAL_INFO_KEY;
     static const char *const LOCALE_KEY;
     static const char *const FORGETTING_CURVE_OCCURRENCES_TO_LEVEL_UP_KEY;
     static const char *const FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID_KEY;
     static const char *const FORGETTING_CURVE_DURATION_TO_LEVEL_DOWN_IN_SECONDS_KEY;
-    static const char *const MAX_UNIGRAM_COUNT_KEY;
-    static const char *const MAX_BIGRAM_COUNT_KEY;
-    static const char *const MAX_TRIGRAM_COUNT_KEY;
     static const int DEFAULT_MULTIPLE_WORDS_DEMOTION_RATE;
     static const float MULTIPLE_WORD_COST_MULTIPLIER_SCALE;
     static const int DEFAULT_FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID;
-    static const int DEFAULT_MAX_UNIGRAM_COUNT;
-    static const int DEFAULT_MAX_BIGRAM_COUNT;
-    static const int DEFAULT_MAX_TRIGRAM_COUNT;
 
     const FormatUtils::FORMAT_VERSION mDictFormatVersion;
     const HeaderReadWriteUtils::DictionaryFlags mDictionaryFlags;
@@ -291,21 +249,18 @@
     const bool mIsDecayingDict;
     const int mDate;
     const int mLastDecayedTime;
-    const int mUnigramCount;
-    const int mBigramCount;
-    const int mTrigramCount;
+    const EntryCounts mNgramCounts;
+    const EntryCounts mMaxNgramCounts;
     const int mExtendedRegionSize;
     const bool mHasHistoricalInfoOfWords;
     const int mForgettingCurveProbabilityValuesTableId;
-    const int mMaxUnigramCount;
-    const int mMaxBigramCount;
-    const int mMaxTrigramCount;
     const int *const mCodePointTable;
 
     const std::vector<int> readLocale() const;
     float readMultipleWordCostMultiplier() const;
     bool readRequiresGermanUmlautProcessing() const;
-
+    const EntryCounts readNgramCounts() const;
+    const EntryCounts readMaxNgramCounts() const;
     static DictionaryHeaderStructurePolicy::AttributeMap createAttributeMapAndReadAllAttributes(
             const uint8_t *const dictBuf);
 };
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_policy.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_policy.cpp
index ca7d93b..051aed4 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_policy.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_policy.cpp
@@ -303,7 +303,7 @@
     if (mUpdatingHelper.addUnigramWord(&readingHelper, codePointArrayView, unigramProperty,
             &addedNewUnigram)) {
         if (addedNewUnigram && !unigramProperty->representsBeginningOfSentence()) {
-            mEntryCounters.incrementUnigramCount();
+            mEntryCounters.incrementNgramCount(NgramType::Unigram);
         }
         if (unigramProperty->getShortcuts().size() > 0) {
             // Add shortcut target.
@@ -397,7 +397,7 @@
     if (mUpdatingHelper.addNgramEntry(PtNodePosArrayView::singleElementView(&prevWordPtNodePos),
             wordPos, ngramProperty, &addedNewBigram)) {
         if (addedNewBigram) {
-            mEntryCounters.incrementBigramCount();
+            mEntryCounters.incrementNgramCount(NgramType::Bigram);
         }
         return true;
     } else {
@@ -438,7 +438,7 @@
     const int prevWordPtNodePos = getTerminalPtNodePosFromWordId(prevWordIds[0]);
     if (mUpdatingHelper.removeNgramEntry(
             PtNodePosArrayView::singleElementView(&prevWordPtNodePos), wordPos)) {
-        mEntryCounters.decrementBigramCount();
+        mEntryCounters.decrementNgramCount(NgramType::Bigram);
         return true;
     } else {
         return false;
@@ -525,20 +525,23 @@
         char *const outResult, const int maxResultLength) {
     const int compareLength = queryLength + 1 /* terminator */;
     if (strncmp(query, UNIGRAM_COUNT_QUERY, compareLength) == 0) {
-        snprintf(outResult, maxResultLength, "%d", mEntryCounters.getUnigramCount());
+        snprintf(outResult, maxResultLength, "%d",
+                mEntryCounters.getNgramCount(NgramType::Unigram));
     } else if (strncmp(query, BIGRAM_COUNT_QUERY, compareLength) == 0) {
-        snprintf(outResult, maxResultLength, "%d", mEntryCounters.getBigramCount());
+        snprintf(outResult, maxResultLength, "%d", mEntryCounters.getNgramCount(NgramType::Bigram));
     } else if (strncmp(query, MAX_UNIGRAM_COUNT_QUERY, compareLength) == 0) {
         snprintf(outResult, maxResultLength, "%d",
                 mHeaderPolicy->isDecayingDict() ?
                         ForgettingCurveUtils::getEntryCountHardLimit(
-                                mHeaderPolicy->getMaxUnigramCount()) :
+                                mHeaderPolicy->getMaxNgramCounts().getNgramCount(
+                                        NgramType::Unigram)) :
                         static_cast<int>(Ver4DictConstants::MAX_DICTIONARY_SIZE));
     } else if (strncmp(query, MAX_BIGRAM_COUNT_QUERY, compareLength) == 0) {
         snprintf(outResult, maxResultLength, "%d",
                 mHeaderPolicy->isDecayingDict() ?
                         ForgettingCurveUtils::getEntryCountHardLimit(
-                                mHeaderPolicy->getMaxBigramCount()) :
+                                mHeaderPolicy->getMaxNgramCounts().getNgramCount(
+                                        NgramType::Bigram)) :
                         static_cast<int>(Ver4DictConstants::MAX_DICTIONARY_SIZE));
     }
 }
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_policy.h b/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_policy.h
index 0480876..80b1111 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_policy.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_policy.h
@@ -76,8 +76,7 @@
                       &mPtNodeArrayReader, &mBigramPolicy, &mShortcutPolicy),
               mUpdatingHelper(mDictBuffer, &mNodeReader, &mNodeWriter),
               mWritingHelper(mBuffers.get()),
-              mEntryCounters(mHeaderPolicy->getUnigramCount(), mHeaderPolicy->getBigramCount(),
-                      mHeaderPolicy->getTrigramCount()),
+              mEntryCounters(mHeaderPolicy->getNgramCounts().getCountArray()),
               mTerminalPtNodePositionsForIteratingWords(), mIsCorrupted(false) {};
 
     virtual int getRootPosition() const {
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_writing_helper.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_writing_helper.cpp
index a033d39..985c168 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_writing_helper.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/backward/v402/ver4_patricia_trie_writing_helper.cpp
@@ -53,8 +53,8 @@
             entryCounts, extendedRegionSize, &headerBuffer)) {
         AKLOGE("Cannot write header structure to buffer. "
                 "updatesLastDecayedTime: %d, unigramCount: %d, bigramCount: %d, "
-                "extendedRegionSize: %d", false, entryCounts.getUnigramCount(),
-                entryCounts.getBigramCount(), extendedRegionSize);
+                "extendedRegionSize: %d", false, entryCounts.getNgramCount(NgramType::Unigram),
+                entryCounts.getNgramCount(NgramType::Bigram), extendedRegionSize);
         return false;
     }
     return mBuffers->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer);
@@ -73,9 +73,11 @@
     }
     BufferWithExtendableBuffer headerBuffer(
             BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE);
+    MutableEntryCounters entryCounters;
+    entryCounters.setNgramCount(NgramType::Unigram, unigramCount);
+    entryCounters.setNgramCount(NgramType::Bigram, bigramCount);
     if (!headerPolicy->fillInAndWriteHeaderToBuffer(true /* updatesLastDecayedTime */,
-            EntryCounts(unigramCount, bigramCount, 0 /* trigramCount */),
-            0 /* extendedRegionSize */, &headerBuffer)) {
+            entryCounters.getEntryCounts(), 0 /* extendedRegionSize */, &headerBuffer)) {
         return false;
     }
     return dictBuffers->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer);
@@ -107,7 +109,7 @@
     }
     const int unigramCount = traversePolicyToUpdateUnigramProbabilityAndMarkUselessPtNodesAsDeleted
             .getValidUnigramCount();
-    const int maxUnigramCount = headerPolicy->getMaxUnigramCount();
+    const int maxUnigramCount = headerPolicy->getMaxNgramCounts().getNgramCount(NgramType::Unigram);
     if (headerPolicy->isDecayingDict() && unigramCount > maxUnigramCount) {
         if (!truncateUnigrams(&ptNodeReader, &ptNodeWriter, maxUnigramCount)) {
             AKLOGE("Cannot remove unigrams. current: %d, max: %d", unigramCount,
@@ -124,7 +126,7 @@
         return false;
     }
     const int bigramCount = traversePolicyToUpdateBigramProbability.getValidBigramEntryCount();
-    const int maxBigramCount = headerPolicy->getMaxBigramCount();
+    const int maxBigramCount = headerPolicy->getMaxNgramCounts().getNgramCount(NgramType::Bigram);
     if (headerPolicy->isDecayingDict() && bigramCount > maxBigramCount) {
         if (!truncateBigrams(maxBigramCount)) {
             AKLOGE("Cannot remove bigrams. current: %d, max: %d", bigramCount, maxBigramCount);
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/dynamic_language_model_probability_utils.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/dynamic_language_model_probability_utils.cpp
index b0fbb3e..025ee99 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/dynamic_language_model_probability_utils.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/dynamic_language_model_probability_utils.cpp
@@ -18,17 +18,14 @@
 
 namespace latinime {
 
-// These counts are used to provide stable probabilities even if the user's input count is small.
-const int DynamicLanguageModelProbabilityUtils::ASSUMED_MIN_COUNT_FOR_UNIGRAMS = 8192;
-const int DynamicLanguageModelProbabilityUtils::ASSUMED_MIN_COUNT_FOR_BIGRAMS = 2;
-const int DynamicLanguageModelProbabilityUtils::ASSUMED_MIN_COUNT_FOR_TRIGRAMS = 2;
+// Used to provide stable probabilities even if the user's input count is small.
+const int DynamicLanguageModelProbabilityUtils::ASSUMED_MIN_COUNTS[] = {8192, 2, 2, 1};
 
-// These are encoded backoff weights.
-// Note that we give positive value for trigrams that means the weight is more than 1.
+// Encoded backoff weights.
+// Note that we give positive values for trigrams and quadgrams that means the weight is more than
+// 1.
 // TODO: Apply backoff for main dictionaries and quit giving a positive backoff weight.
-const int DynamicLanguageModelProbabilityUtils::ENCODED_BACKOFF_WEIGHT_FOR_UNIGRAMS = -32;
-const int DynamicLanguageModelProbabilityUtils::ENCODED_BACKOFF_WEIGHT_FOR_BIGRAMS = 0;
-const int DynamicLanguageModelProbabilityUtils::ENCODED_BACKOFF_WEIGHT_FOR_TRIGRAMS = 8;
+const int DynamicLanguageModelProbabilityUtils::ENCODED_BACKOFF_WEIGHTS[] = {-32, -4, 2, 8};
 
 // This value is used to remove too old entries from the dictionary.
 const int DynamicLanguageModelProbabilityUtils::DURATION_TO_DISCARD_ENTRY_IN_SECONDS =
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/dynamic_language_model_probability_utils.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/dynamic_language_model_probability_utils.h
index 88bc58f..644ae2c 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/dynamic_language_model_probability_utils.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/dynamic_language_model_probability_utils.h
@@ -21,6 +21,7 @@
 
 #include "defines.h"
 #include "suggest/core/dictionary/property/historical_info.h"
+#include "utils/ngram_utils.h"
 #include "utils/time_keeper.h"
 
 namespace latinime {
@@ -28,46 +29,14 @@
 class DynamicLanguageModelProbabilityUtils {
  public:
     static float computeRawProbabilityFromCounts(const int count, const int contextCount,
-            const int matchedWordCountInContext) {
-        int minCount = 0;
-        switch (matchedWordCountInContext) {
-            case 1:
-                minCount = ASSUMED_MIN_COUNT_FOR_UNIGRAMS;
-                break;
-            case 2:
-                minCount = ASSUMED_MIN_COUNT_FOR_BIGRAMS;
-                break;
-            case 3:
-                minCount = ASSUMED_MIN_COUNT_FOR_TRIGRAMS;
-                break;
-            default:
-                AKLOGE("computeRawProbabilityFromCounts is called with invalid "
-                        "matchedWordCountInContext (%d).", matchedWordCountInContext);
-                ASSERT(false);
-                return 0.0f;
-        }
+            const NgramType ngramType) {
+        const int minCount = ASSUMED_MIN_COUNTS[static_cast<int>(ngramType)];
         return static_cast<float>(count) / static_cast<float>(std::max(contextCount, minCount));
     }
 
-    static float backoff(const int ngramProbability, const int matchedWordCountInContext) {
-        int probability = NOT_A_PROBABILITY;
-
-        switch (matchedWordCountInContext) {
-            case 1:
-                probability = ngramProbability + ENCODED_BACKOFF_WEIGHT_FOR_UNIGRAMS;
-                break;
-            case 2:
-                probability = ngramProbability + ENCODED_BACKOFF_WEIGHT_FOR_BIGRAMS;
-                break;
-            case 3:
-                probability = ngramProbability + ENCODED_BACKOFF_WEIGHT_FOR_TRIGRAMS;
-                break;
-            default:
-                AKLOGE("backoff is called with invalid matchedWordCountInContext (%d).",
-                        matchedWordCountInContext);
-                ASSERT(false);
-                return NOT_A_PROBABILITY;
-        }
+    static float backoff(const int ngramProbability, const NgramType ngramType) {
+        const int probability =
+                ngramProbability + ENCODED_BACKOFF_WEIGHTS[static_cast<int>(ngramType)];
         return std::min(std::max(probability, NOT_A_PROBABILITY), MAX_PROBABILITY);
     }
 
@@ -97,16 +66,10 @@
 private:
     DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicLanguageModelProbabilityUtils);
 
-    static_assert(MAX_PREV_WORD_COUNT_FOR_N_GRAM <= 2, "Max supported Ngram is Trigram.");
+    static_assert(MAX_PREV_WORD_COUNT_FOR_N_GRAM <= 3, "Max supported Ngram is Quadgram.");
 
-    static const int ASSUMED_MIN_COUNT_FOR_UNIGRAMS;
-    static const int ASSUMED_MIN_COUNT_FOR_BIGRAMS;
-    static const int ASSUMED_MIN_COUNT_FOR_TRIGRAMS;
-
-    static const int ENCODED_BACKOFF_WEIGHT_FOR_UNIGRAMS;
-    static const int ENCODED_BACKOFF_WEIGHT_FOR_BIGRAMS;
-    static const int ENCODED_BACKOFF_WEIGHT_FOR_TRIGRAMS;
-
+    static const int ASSUMED_MIN_COUNTS[];
+    static const int ENCODED_BACKOFF_WEIGHTS[];
     static const int DURATION_TO_DISCARD_ENTRY_IN_SECONDS;
 };
 
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content.cpp
index 31b1ea6..6db7ea4 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content.cpp
@@ -21,6 +21,7 @@
 
 #include "suggest/policyimpl/dictionary/structure/v4/content/dynamic_language_model_probability_utils.h"
 #include "suggest/policyimpl/dictionary/utils/probability_utils.h"
+#include "utils/ngram_utils.h"
 
 namespace latinime {
 
@@ -89,16 +90,17 @@
                 }
                 contextCount = prevWordProbabilityEntry.getHistoricalInfo()->getCount();
             }
+            const NgramType ngramType = NgramUtils::getNgramTypeFromWordCount(i + 1);
             const float rawProbability =
                     DynamicLanguageModelProbabilityUtils::computeRawProbabilityFromCounts(
-                            historicalInfo->getCount(), contextCount, i + 1);
+                            historicalInfo->getCount(), contextCount, ngramType);
             const int encodedRawProbability =
                     ProbabilityUtils::encodeRawProbability(rawProbability);
             const int decayedProbability =
                     DynamicLanguageModelProbabilityUtils::getDecayedProbability(
                             encodedRawProbability, *historicalInfo);
             probability = DynamicLanguageModelProbabilityUtils::backoff(
-                    decayedProbability, i + 1 /* n */);
+                    decayedProbability, ngramType);
         } else {
             probability = probabilityEntry.getProbability();
         }
@@ -198,18 +200,19 @@
         MutableEntryCounters *const outEntryCounters) {
     for (int prevWordCount = 0; prevWordCount <= MAX_PREV_WORD_COUNT_FOR_N_GRAM; ++prevWordCount) {
         const int totalWordCount = prevWordCount + 1;
-        if (currentEntryCounts.getNgramCount(totalWordCount)
-                <= maxEntryCounts.getNgramCount(totalWordCount)) {
-            outEntryCounters->setNgramCount(totalWordCount,
-                    currentEntryCounts.getNgramCount(totalWordCount));
+        const NgramType ngramType = NgramUtils::getNgramTypeFromWordCount(totalWordCount);
+        if (currentEntryCounts.getNgramCount(ngramType)
+                <= maxEntryCounts.getNgramCount(ngramType)) {
+            outEntryCounters->setNgramCount(ngramType,
+                    currentEntryCounts.getNgramCount(ngramType));
             continue;
         }
         int entryCount = 0;
         if (!turncateEntriesInSpecifiedLevel(headerPolicy,
-                maxEntryCounts.getNgramCount(totalWordCount), prevWordCount, &entryCount)) {
+                maxEntryCounts.getNgramCount(ngramType), prevWordCount, &entryCount)) {
             return false;
         }
-        outEntryCounters->setNgramCount(totalWordCount, entryCount);
+        outEntryCounters->setNgramCount(ngramType, entryCount);
     }
     return true;
 }
@@ -246,7 +249,10 @@
         mGlobalCounters.updateMaxValueOfCounters(
                 updatedNgramProbabilityEntry.getHistoricalInfo()->getCount());
         if (!originalNgramProbabilityEntry.isValid()) {
-            entryCountersToUpdate->incrementNgramCount(i + 2);
+            // (i + 2) words are used in total because the prevWords consists of (i + 1) words when
+            // looking at its i-th element.
+            entryCountersToUpdate->incrementNgramCount(
+                    NgramUtils::getNgramTypeFromWordCount(i + 2));
         }
     }
     return true;
@@ -369,7 +375,8 @@
                 }
             }
         }
-        outEntryCounters->incrementNgramCount(prevWordCount + 1);
+        outEntryCounters->incrementNgramCount(
+                NgramUtils::getNgramTypeFromWordCount(prevWordCount + 1));
         if (!entry.hasNextLevelMap()) {
             continue;
         }
@@ -402,7 +409,8 @@
     for (int i = 0; i < entryCountToRemove; ++i) {
         const EntryInfoToTurncate &entryInfo = entryInfoVector[i];
         if (!removeNgramProbabilityEntry(
-                WordIdArrayView(entryInfo.mPrevWordIds, entryInfo.mPrevWordCount), entryInfo.mKey)) {
+                WordIdArrayView(entryInfo.mPrevWordIds, entryInfo.mPrevWordCount),
+                entryInfo.mKey)) {
             return false;
         }
     }
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.cpp
index 7449cd0..a967195 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.cpp
@@ -31,6 +31,7 @@
 #include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_node_reader.h"
 #include "suggest/policyimpl/dictionary/utils/forgetting_curve_utils.h"
 #include "suggest/policyimpl/dictionary/utils/probability_utils.h"
+#include "utils/ngram_utils.h"
 
 namespace latinime {
 
@@ -215,7 +216,7 @@
     if (mUpdatingHelper.addUnigramWord(&readingHelper, codePointArrayView, unigramProperty,
             &addedNewUnigram)) {
         if (addedNewUnigram && !unigramProperty->representsBeginningOfSentence()) {
-            mEntryCounters.incrementUnigramCount();
+            mEntryCounters.incrementNgramCount(NgramType::Unigram);
         }
         if (unigramProperty->getShortcuts().size() > 0) {
             // Add shortcut target.
@@ -263,7 +264,7 @@
         return false;
     }
     if (!ptNodeParams.representsNonWordInfo()) {
-        mEntryCounters.decrementUnigramCount();
+        mEntryCounters.decrementNgramCount(NgramType::Unigram);
     }
     return true;
 }
@@ -321,7 +322,8 @@
     bool addedNewEntry = false;
     if (mNodeWriter.addNgramEntry(prevWordIds, wordId, ngramProperty, &addedNewEntry)) {
         if (addedNewEntry) {
-            mEntryCounters.incrementNgramCount(prevWordIds.size() + 1);
+            mEntryCounters.incrementNgramCount(
+                    NgramUtils::getNgramTypeFromWordCount(prevWordIds.size() + 1));
         }
         return true;
     } else {
@@ -359,7 +361,8 @@
         return false;
     }
     if (mNodeWriter.removeNgramEntry(prevWordIds, wordId)) {
-        mEntryCounters.decrementNgramCount(prevWordIds.size());
+        mEntryCounters.decrementNgramCount(
+                NgramUtils::getNgramTypeFromWordCount(prevWordIds.size() + 1));
         return true;
     } else {
         return false;
@@ -477,20 +480,23 @@
         char *const outResult, const int maxResultLength) {
     const int compareLength = queryLength + 1 /* terminator */;
     if (strncmp(query, UNIGRAM_COUNT_QUERY, compareLength) == 0) {
-        snprintf(outResult, maxResultLength, "%d", mEntryCounters.getUnigramCount());
+        snprintf(outResult, maxResultLength, "%d",
+                mEntryCounters.getNgramCount(NgramType::Unigram));
     } else if (strncmp(query, BIGRAM_COUNT_QUERY, compareLength) == 0) {
-        snprintf(outResult, maxResultLength, "%d", mEntryCounters.getBigramCount());
+        snprintf(outResult, maxResultLength, "%d", mEntryCounters.getNgramCount(NgramType::Bigram));
     } else if (strncmp(query, MAX_UNIGRAM_COUNT_QUERY, compareLength) == 0) {
         snprintf(outResult, maxResultLength, "%d",
                 mHeaderPolicy->isDecayingDict() ?
                         ForgettingCurveUtils::getEntryCountHardLimit(
-                                mHeaderPolicy->getMaxUnigramCount()) :
+                                mHeaderPolicy->getMaxNgramCounts().getNgramCount(
+                                        NgramType::Unigram)) :
                         static_cast<int>(Ver4DictConstants::MAX_DICTIONARY_SIZE));
     } else if (strncmp(query, MAX_BIGRAM_COUNT_QUERY, compareLength) == 0) {
         snprintf(outResult, maxResultLength, "%d",
                 mHeaderPolicy->isDecayingDict() ?
                         ForgettingCurveUtils::getEntryCountHardLimit(
-                                mHeaderPolicy->getMaxBigramCount()) :
+                                mHeaderPolicy->getMaxNgramCounts().getNgramCount(
+                                        NgramType::Bigram)) :
                         static_cast<int>(Ver4DictConstants::MAX_DICTIONARY_SIZE));
     }
 }
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.h
index 13700b3..93faa83 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.h
@@ -51,8 +51,7 @@
                       &mShortcutPolicy),
               mUpdatingHelper(mDictBuffer, &mNodeReader, &mNodeWriter),
               mWritingHelper(mBuffers.get()),
-              mEntryCounters(mHeaderPolicy->getUnigramCount(), mHeaderPolicy->getBigramCount(),
-                      mHeaderPolicy->getTrigramCount()),
+              mEntryCounters(mHeaderPolicy->getNgramCounts().getCountArray()),
               mTerminalPtNodePositionsForIteratingWords(), mIsCorrupted(false) {};
 
     AK_FORCE_INLINE int getRootPosition() const {
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.cpp
index 7f0604c..34af76c 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.cpp
@@ -29,6 +29,7 @@
 #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
 #include "suggest/policyimpl/dictionary/utils/file_utils.h"
 #include "suggest/policyimpl/dictionary/utils/forgetting_curve_utils.h"
+#include "utils/ngram_utils.h"
 
 namespace latinime {
 
@@ -43,8 +44,9 @@
             entryCounts, extendedRegionSize, &headerBuffer)) {
         AKLOGE("Cannot write header structure to buffer. "
                 "updatesLastDecayedTime: %d, unigramCount: %d, bigramCount: %d, trigramCount: %d,"
-                "extendedRegionSize: %d", false, entryCounts.getUnigramCount(),
-                entryCounts.getBigramCount(), entryCounts.getTrigramCount(),
+                "extendedRegionSize: %d", false, entryCounts.getNgramCount(NgramType::Unigram),
+                entryCounts.getNgramCount(NgramType::Bigram),
+                entryCounts.getNgramCount(NgramType::Trigram),
                 extendedRegionSize);
         return false;
     }
@@ -86,8 +88,7 @@
         return false;
     }
     if (headerPolicy->isDecayingDict()) {
-        const EntryCounts maxEntryCounts(headerPolicy->getMaxUnigramCount(),
-                headerPolicy->getMaxBigramCount(), headerPolicy->getMaxTrigramCount());
+        const EntryCounts &maxEntryCounts = headerPolicy->getMaxNgramCounts();
         if (!mBuffers->getMutableLanguageModelDictContent()->truncateEntries(
                 outEntryCounters->getEntryCounts(), maxEntryCounts, headerPolicy,
                 outEntryCounters)) {
diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/entry_counters.h b/native/jni/src/suggest/policyimpl/dictionary/utils/entry_counters.h
index 73dc42a..5e44302 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/utils/entry_counters.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/utils/entry_counters.h
@@ -20,42 +20,31 @@
 #include <array>
 
 #include "defines.h"
+#include "utils/ngram_utils.h"
 
 namespace latinime {
 
 // Copyable but immutable
 class EntryCounts final {
  public:
-    EntryCounts() : mEntryCounts({{0, 0, 0}}) {}
-
-    EntryCounts(const int unigramCount, const int bigramCount, const int trigramCount)
-            : mEntryCounts({{unigramCount, bigramCount, trigramCount}}) {}
+    EntryCounts() : mEntryCounts({{0, 0, 0, 0}}) {}
 
     explicit EntryCounts(const std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> &counters)
             : mEntryCounts(counters) {}
 
-    int getUnigramCount() const {
-        return mEntryCounts[0];
+    int getNgramCount(const NgramType ngramType) const {
+        return mEntryCounts[static_cast<int>(ngramType)];
     }
 
-    int getBigramCount() const {
-        return mEntryCounts[1];
-    }
-
-    int getTrigramCount() const {
-        return mEntryCounts[2];
-    }
-
-    int getNgramCount(const size_t n) const {
-        if (n < 1 || n > mEntryCounts.size()) {
-            return 0;
-        }
-        return mEntryCounts[n - 1];
+    const std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> &getCountArray() const {
+        return mEntryCounts;
     }
 
  private:
     DISALLOW_ASSIGNMENT_OPERATOR(EntryCounts);
 
+    // Counts from Unigram (0-th element) to (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram
+    // (MAX_PREV_WORD_COUNT_FOR_N_GRAM-th element)
     const std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> mEntryCounts;
 };
 
@@ -65,68 +54,35 @@
         mEntryCounters.fill(0);
     }
 
-    MutableEntryCounters(const int unigramCount, const int bigramCount, const int trigramCount)
-            : mEntryCounters({{unigramCount, bigramCount, trigramCount}}) {}
+    explicit MutableEntryCounters(
+            const std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> &counters)
+            : mEntryCounters(counters) {}
 
     const EntryCounts getEntryCounts() const {
         return EntryCounts(mEntryCounters);
     }
 
-    int getUnigramCount() const {
-        return mEntryCounters[0];
+    void incrementNgramCount(const NgramType ngramType) {
+        ++mEntryCounters[static_cast<int>(ngramType)];
     }
 
-    int getBigramCount() const {
-        return mEntryCounters[1];
+    void decrementNgramCount(const NgramType ngramType) {
+        --mEntryCounters[static_cast<int>(ngramType)];
     }
 
-    int getTrigramCount() const {
-        return mEntryCounters[2];
+    int getNgramCount(const NgramType ngramType) const {
+        return mEntryCounters[static_cast<int>(ngramType)];
     }
 
-    void incrementUnigramCount() {
-        ++mEntryCounters[0];
-    }
-
-    void decrementUnigramCount() {
-        ASSERT(mEntryCounters[0] != 0);
-        --mEntryCounters[0];
-    }
-
-    void incrementBigramCount() {
-        ++mEntryCounters[1];
-    }
-
-    void decrementBigramCount() {
-        ASSERT(mEntryCounters[1] != 0);
-        --mEntryCounters[1];
-    }
-
-    void incrementNgramCount(const size_t n) {
-        if (n < 1 || n > mEntryCounters.size()) {
-            return;
-        }
-        ++mEntryCounters[n - 1];
-    }
-
-    void decrementNgramCount(const size_t n) {
-        if (n < 1 || n > mEntryCounters.size()) {
-            return;
-        }
-        ASSERT(mEntryCounters[n - 1] != 0);
-        --mEntryCounters[n - 1];
-    }
-
-    void setNgramCount(const size_t n, const int count) {
-        if (n < 1 || n > mEntryCounters.size()) {
-            return;
-        }
-        mEntryCounters[n - 1] = count;
+    void setNgramCount(const NgramType ngramType, const int count) {
+        mEntryCounters[static_cast<int>(ngramType)] = count;
     }
 
  private:
     DISALLOW_COPY_AND_ASSIGN(MutableEntryCounters);
 
+    // Counters from Unigram (0-th element) to (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram
+    // (MAX_PREV_WORD_COUNT_FOR_N_GRAM-th element)
     std::array<int, MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1> mEntryCounters;
 };
 } // namespace latinime
diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/forgetting_curve_utils.cpp b/native/jni/src/suggest/policyimpl/dictionary/utils/forgetting_curve_utils.cpp
index 9055f7b..f05c614 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/utils/forgetting_curve_utils.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/utils/forgetting_curve_utils.cpp
@@ -126,20 +126,13 @@
 
 /* static */ bool ForgettingCurveUtils::needsToDecay(const bool mindsBlockByDecay,
         const EntryCounts &entryCounts, const HeaderPolicy *const headerPolicy) {
-    if (entryCounts.getUnigramCount()
-            >= getEntryCountHardLimit(headerPolicy->getMaxUnigramCount())) {
-        // Unigram count exceeds the limit.
-        return true;
-    }
-    if (entryCounts.getBigramCount()
-            >= getEntryCountHardLimit(headerPolicy->getMaxBigramCount())) {
-        // Bigram count exceeds the limit.
-        return true;
-    }
-    if (entryCounts.getTrigramCount()
-            >= getEntryCountHardLimit(headerPolicy->getMaxTrigramCount())) {
-        // Trigram count exceeds the limit.
-        return true;
+    const EntryCounts &maxNgramCounts = headerPolicy->getMaxNgramCounts();
+    for (const auto ngramType : AllNgramTypes::ASCENDING) {
+        if (entryCounts.getNgramCount(ngramType)
+                >= getEntryCountHardLimit(maxNgramCounts.getNgramCount(ngramType))) {
+            // Unigram count exceeds the limit.
+            return true;
+        }
     }
     if (mindsBlockByDecay) {
         return false;
diff --git a/native/jni/src/utils/ngram_utils.h b/native/jni/src/utils/ngram_utils.h
new file mode 100644
index 0000000..fa85ba3
--- /dev/null
+++ b/native/jni/src/utils/ngram_utils.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2014, 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.
+ */
+
+#ifndef LATINIME_NGRAM_UTILS_H
+#define LATINIME_NGRAM_UTILS_H
+
+#include "defines.h"
+
+namespace latinime {
+
+enum class NgramType : int {
+    Unigram = 0,
+    Bigram = 1,
+    Trigram = 2,
+    Quadgram = 3,
+    NotANgramType = -1,
+};
+
+namespace AllNgramTypes {
+// Use anonymous namespace to avoid ODR (One Definition Rule) violation.
+namespace {
+
+const NgramType ASCENDING[] = {
+   NgramType::Unigram, NgramType::Bigram, NgramType::Trigram
+};
+
+const NgramType DESCENDING[] = {
+   NgramType::Trigram, NgramType::Bigram, NgramType::Unigram
+};
+
+}  // namespace
+}  // namespace AllNgramTypes
+
+class NgramUtils final {
+ public:
+    static AK_FORCE_INLINE NgramType getNgramTypeFromWordCount(const int wordCount) {
+        // Max supported ngram is (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram.
+        if (wordCount <= 0 || wordCount > MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1) {
+            return NgramType::NotANgramType;
+        }
+        // Convert word count to 0-origin enum value.
+        return static_cast<NgramType>(wordCount - 1);
+    }
+
+ private:
+    DISALLOW_IMPLICIT_CONSTRUCTORS(NgramUtils);
+
+};
+}
+#endif /* LATINIME_NGRAM_UTILS_H */
diff --git a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java
index 0246c49..8855080 100644
--- a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java
+++ b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java
@@ -75,7 +75,7 @@
         mRichImm = RichInputMethodManager.getInstance();
 
         // Save and reset additional subtypes preference.
-        mSavedAdditionalSubtypes = mRichImm.getAdditionalSubtypes(context);
+        mSavedAdditionalSubtypes = mRichImm.getAdditionalSubtypes();
         final InputMethodSubtype[] predefinedAdditionalSubtypes =
                 AdditionalSubtypeUtils.createAdditionalSubtypesArray(
                         AdditionalSubtypeUtils.createPrefSubtypes(
@@ -155,7 +155,7 @@
             final boolean languageSwitchKeyEnabled, final boolean splitLayoutEnabled) {
         final Context context = getContext();
         final Resources res = context.getResources();
-        final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
+        final int keyboardWidth = ResourceUtils.getKeyboardFrameWidth(res);
         final int keyboardHeight = ResourceUtils.getDefaultKeyboardHeight(res);
         final Builder builder = new Builder(context, editorInfo);
         builder.setKeyboardGeometry(keyboardWidth, keyboardHeight)
diff --git a/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java b/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java
index a8c4ac8..27519ee 100644
--- a/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java
+++ b/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java
@@ -120,31 +120,13 @@
 
     // TODO: Add phone, phone symbols, number, number password layout tests.
 
-    public final void testAlphabet() {
+    public final void testLayouts() {
         doKeyboardTests(KeyboardId.ELEMENT_ALPHABET);
-    }
-
-    public final void testAlphabetAutomaticShifted() {
         doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED);
-    }
-
-    public final void testAlphabetManualShifted() {
         doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED);
-    }
-
-    public final void testAlphabetShiftLocked() {
         doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED);
-    }
-
-    public final void testAlphabetShiftLockShifted() {
         doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED);
-    }
-
-    public final void testSymbols() {
         doKeyboardTests(KeyboardId.ELEMENT_SYMBOLS);
-    }
-
-    public final void testSymbolsShifted() {
         doKeyboardTests(KeyboardId.ELEMENT_SYMBOLS_SHIFTED);
     }
 
diff --git a/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java b/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java
index bcf016a..8614ccc 100644
--- a/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java
+++ b/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java
@@ -176,7 +176,7 @@
         assertEquals(NgramContextUtils.getNgramContextFromNthPreviousWord(
                 "abc def", mSpacingAndPunctuations, 1).getNthPrevWord(2), "abc");
         assertTrue(NgramContextUtils.getNgramContextFromNthPreviousWord(
-                "abc def", mSpacingAndPunctuations, 2).isNthPrevWordBeginningOfSontence(2));
+                "abc def", mSpacingAndPunctuations, 2).isNthPrevWordBeginningOfSentence(2));
 
         // The following tests reflect the current behavior of the function
         // RichInputConnection#getNthPreviousWord.
diff --git a/tests/src/com/android/inputmethod/latin/RichInputMethodSubtypeTests.java b/tests/src/com/android/inputmethod/latin/RichInputMethodSubtypeTests.java
index aed7d6a..9c8e165 100644
--- a/tests/src/com/android/inputmethod/latin/RichInputMethodSubtypeTests.java
+++ b/tests/src/com/android/inputmethod/latin/RichInputMethodSubtypeTests.java
@@ -76,7 +76,7 @@
         mRichImm = RichInputMethodManager.getInstance();
 
         // Save and reset additional subtypes
-        mSavedAddtionalSubtypes = mRichImm.getAdditionalSubtypes(context);
+        mSavedAddtionalSubtypes = mRichImm.getAdditionalSubtypes();
         final InputMethodSubtype[] predefinedAddtionalSubtypes =
                 AdditionalSubtypeUtils.createAdditionalSubtypesArray(
                         AdditionalSubtypeUtils.createPrefSubtypes(
diff --git a/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java b/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java
index dfa063c..111d5c5 100644
--- a/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java
+++ b/tests/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtilsTests.java
@@ -73,7 +73,7 @@
         mRichImm = RichInputMethodManager.getInstance();
 
         // Save and reset additional subtypes
-        mSavedAddtionalSubtypes = mRichImm.getAdditionalSubtypes(context);
+        mSavedAddtionalSubtypes = mRichImm.getAdditionalSubtypes();
         final InputMethodSubtype[] predefinedAddtionalSubtypes =
                 AdditionalSubtypeUtils.createAdditionalSubtypesArray(
                         AdditionalSubtypeUtils.createPrefSubtypes(
diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java
index 84c3956..b5ed94c 100644
--- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java
+++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtils.java
@@ -37,8 +37,6 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
-import java.util.Arrays;
-import java.util.ArrayList;
 import java.util.HashMap;
 
 import javax.annotation.Nonnull;
diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Header.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Header.java
index ba96c0a..aa1762f 100644
--- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Header.java
+++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Header.java
@@ -16,14 +16,12 @@
 
 package com.android.inputmethod.latin.dicttool;
 
-import com.android.inputmethod.latin.BinaryDictionary;
 import com.android.inputmethod.latin.dicttool.BinaryDictOffdeviceUtils.DecoderChainSpec;
 import com.android.inputmethod.latin.makedict.DictionaryHeader;
 import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
 
 import java.io.File;
 import java.util.Arrays;
-import java.util.Locale;
 
 public class Header extends Dicttool.Command {
     public static final String COMMAND = "header";
diff --git a/tools/dicttool/tests/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtilsTests.java b/tools/dicttool/tests/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtilsTests.java
index ea9d4cc..e68aeb0 100644
--- a/tools/dicttool/tests/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtilsTests.java
+++ b/tools/dicttool/tests/com/android/inputmethod/latin/dicttool/BinaryDictOffdeviceUtilsTests.java
@@ -154,7 +154,6 @@
     public void runTestHeaderReaderProcessorWithOneSpec(final boolean compress, final boolean crypt)
             throws IOException, UnsupportedFormatException {
         final String dictName = "testHeaderReaderProcessor";
-        final String dictVersion = Long.toString(System.currentTimeMillis());
         final FormatOptions formatOptions = BinaryDictUtils.STATIC_OPTIONS;
         final int MAX_NUMBER_OF_OPTIONS_TO_ADD = 5;
         final HashMap<String, String> options = new HashMap<>();