Merge "Add TalkBack spoken description of Emoji letters"
diff --git a/java/res/values-fr/donottranslate-config-spacing-and-punctuations.xml b/java/res/values-fr/donottranslate-config-spacing-and-punctuations.xml
index d72f72b..5a49142 100644
--- a/java/res/values-fr/donottranslate-config-spacing-and-punctuations.xml
+++ b/java/res/values-fr/donottranslate-config-spacing-and-punctuations.xml
@@ -22,6 +22,8 @@
     <string name="symbols_preceded_by_space">([{&amp;;:!?</string>
     <!-- Symbols that are normally followed by a space (used to add an auto-space after these) -->
     <string name="symbols_followed_by_space">.,;:!?)]}&amp;</string>
+    <!-- Symbols that behave like a single punctuation when typed next to each other -->
+    <string name="symbols_clustering_together">!?</string>
     <!-- Symbols that separate words -->
     <!-- Don't remove the enclosing double quotes, they protect whitespace (not just U+0020) -->
     <string name="symbols_word_separators">"&#x0009;&#x0020;&#x000A;&#x00A0;"()[]{}*&amp;&lt;&gt;+=|.,;:!?/_\"</string>
diff --git a/java/res/values/donottranslate-config-spacing-and-punctuations.xml b/java/res/values/donottranslate-config-spacing-and-punctuations.xml
index 1be5cf8..2faf578 100644
--- a/java/res/values/donottranslate-config-spacing-and-punctuations.xml
+++ b/java/res/values/donottranslate-config-spacing-and-punctuations.xml
@@ -26,6 +26,8 @@
     <string name="symbols_preceded_by_space">([{&amp;</string>
     <!-- Symbols that are normally followed by a space (used to add an auto-space after these) -->
     <string name="symbols_followed_by_space">.,;:!?)]}&amp;</string>
+    <!-- Symbols that behave like a single punctuation when typed next to each other -->
+    <string name="symbols_clustering_together"></string>
     <!-- Symbols that separate words -->
     <!-- Don't remove the enclosing double quotes, they protect whitespace (not just U+0020) -->
     <string name="symbols_word_separators">"&#x0009;&#x0020;&#x000A;&#x00A0;"()[]{}*&amp;&lt;&gt;+=|.,;:!?/_\"</string>
diff --git a/java/res/values/strings-talkback-descriptions.xml b/java/res/values/strings-talkback-descriptions.xml
index 9c1e652..4ffca10 100644
--- a/java/res/values/strings-talkback-descriptions.xml
+++ b/java/res/values/strings-talkback-descriptions.xml
@@ -74,11 +74,10 @@
     <string name="spoken_description_shiftmode_on">Shift enabled</string>
     <!-- Spoken feedback after turning "Caps lock" mode on. -->
     <string name="spoken_description_shiftmode_locked">Caps lock enabled</string>
-    <!-- Spoken feedback after turning "Shift" mode off. -->
-    <string name="spoken_description_shiftmode_off">Shift disabled</string>
-
     <!-- Spoken feedback after changing to the symbols keyboard. -->
     <string name="spoken_description_mode_symbol">Symbols mode</string>
+    <!-- Spoken feedback after changing to the symbols shift keyboard. -->
+    <string name="spoken_description_mode_symbol_shift">Symbols shift mode</string>
     <!-- Spoken feedback after changing to the alphanumeric keyboard. -->
     <string name="spoken_description_mode_alpha">Letters mode</string>
     <!-- Spoken feedback after changing to the phone dialer keyboard. -->
diff --git a/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java b/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java
index bc094b1..d50dd3e 100644
--- a/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java
+++ b/java/src/com/android/inputmethod/accessibility/AccessibilityUtils.java
@@ -68,7 +68,6 @@
         // These only need to be initialized if the kill switch is off.
         sInstance.initInternal(context);
         KeyCodeDescriptionMapper.init();
-        AccessibleKeyboardViewProxy.init(context);
     }
 
     public static AccessibilityUtils getInstance() {
diff --git a/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java
similarity index 77%
rename from java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java
rename to java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java
index 322127a..1092942 100644
--- a/java/src/com/android/inputmethod/accessibility/AccessibleKeyboardViewProxy.java
+++ b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityDelegate.java
@@ -36,9 +36,7 @@
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
 
-public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateCompat {
-    private static final AccessibleKeyboardViewProxy sInstance = new AccessibleKeyboardViewProxy();
-
+public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelegateCompat {
     /** Map of keyboard modes to resource IDs. */
     private static final SparseIntArray KEYBOARD_MODE_RES_IDS = new SparseIntArray();
 
@@ -54,9 +52,9 @@
         KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_URL, R.string.keyboard_mode_url);
     }
 
-    private MainKeyboardView mView;
+    private final MainKeyboardView mView;
     private Keyboard mKeyboard;
-    private AccessibilityEntityProvider mAccessibilityNodeProvider;
+    private MainKeyboardAccessibilityNodeProvider mAccessibilityNodeProvider;
 
     private Key mLastHoverKey = null;
 
@@ -69,46 +67,14 @@
     private int mLastKeyboardMode = KEYBOARD_IS_HIDDEN;
     private static final int KEYBOARD_IS_HIDDEN = -1;
 
-    public static void init(final Context context) {
-        sInstance.initInternal(context);
-    }
-
-    public static AccessibleKeyboardViewProxy getInstance() {
-        return sInstance;
-    }
-
-    private AccessibleKeyboardViewProxy() {
-        // Not publicly instantiable.
-    }
-
-    private void initInternal(final Context context) {
+    public MainKeyboardAccessibilityDelegate(final MainKeyboardView view) {
+        final Context context = view.getContext();
         mEdgeSlop = context.getResources().getDimensionPixelSize(
                 R.dimen.config_accessibility_edge_slop);
-    }
-
-    /**
-     * Sets the view wrapped by this proxy.
-     *
-     * @param view The view to wrap.
-     */
-    public void setView(final MainKeyboardView view) {
-        if (view == null) {
-            // Ignore null views.
-            return;
-        }
         mView = view;
 
         // Ensure that the view has an accessibility delegate.
         ViewCompat.setAccessibilityDelegate(view, this);
-
-        if (mAccessibilityNodeProvider == null) {
-            return;
-        }
-        mAccessibilityNodeProvider.setView(view);
-
-        // Since this class is constructed lazily, we might not get a subsequent
-        // call to setKeyboard() and therefore need to call it now.
-        setKeyboard(view.getKeyboard());
     }
 
     /**
@@ -136,12 +102,19 @@
             return;
         }
         // Announce the language name only when the language is changed.
-        if (lastKeyboard == null || !lastKeyboard.mId.mSubtype.equals(keyboard.mId.mSubtype)) {
+        if (lastKeyboard == null || !keyboard.mId.mSubtype.equals(lastKeyboard.mId.mSubtype)) {
             announceKeyboardLanguage(keyboard);
+            return;
         }
         // Announce the mode only when the mode is changed.
-        if (lastKeyboardMode != keyboard.mId.mMode) {
+        if (keyboard.mId.mMode != lastKeyboardMode) {
             announceKeyboardMode(keyboard);
+            return;
+        }
+        // Announce the keyboard type only when the type is changed.
+        if (keyboard.mId.mElementId != lastKeyboard.mId.mElementId) {
+            announceKeyboardType(keyboard, lastKeyboard);
+            return;
         }
     }
 
@@ -149,9 +122,6 @@
      * Called when the keyboard is hidden and accessibility is enabled.
      */
     public void onHideWindow() {
-        if (mView == null) {
-            return;
-        }
         announceKeyboardHidden();
         mLastKeyboardMode = KEYBOARD_IS_HIDDEN;
     }
@@ -174,9 +144,8 @@
      * @param keyboard The new keyboard.
      */
     private void announceKeyboardMode(final Keyboard keyboard) {
-        final int mode = keyboard.mId.mMode;
         final Context context = mView.getContext();
-        final int modeTextResId = KEYBOARD_MODE_RES_IDS.get(mode);
+        final int modeTextResId = KEYBOARD_MODE_RES_IDS.get(keyboard.mId.mMode);
         if (modeTextResId == 0) {
             return;
         }
@@ -186,6 +155,50 @@
     }
 
     /**
+     * Announces which type of keyboard is being displayed.
+     *
+     * @param keyboard The new keyboard.
+     * @param lastKeyboard The last keyboard.
+     */
+    private void announceKeyboardType(final Keyboard keyboard, final Keyboard lastKeyboard) {
+        final int lastElementId = lastKeyboard.mId.mElementId;
+        final int resId;
+        switch (keyboard.mId.mElementId) {
+        case KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED:
+        case KeyboardId.ELEMENT_ALPHABET:
+            if (lastElementId == KeyboardId.ELEMENT_ALPHABET
+                    || lastElementId == KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED) {
+                return;
+            }
+            resId = R.string.spoken_description_mode_alpha;
+            break;
+        case KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED:
+            resId = R.string.spoken_description_shiftmode_on;
+            break;
+        case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED:
+        case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED:
+            resId = R.string.spoken_description_shiftmode_locked;
+            break;
+        case KeyboardId.ELEMENT_SYMBOLS:
+            resId = R.string.spoken_description_mode_symbol;
+            break;
+        case KeyboardId.ELEMENT_SYMBOLS_SHIFTED:
+            resId = R.string.spoken_description_mode_symbol_shift;
+            break;
+        case KeyboardId.ELEMENT_PHONE:
+            resId = R.string.spoken_description_mode_phone;
+            break;
+        case KeyboardId.ELEMENT_PHONE_SYMBOLS:
+            resId = R.string.spoken_description_mode_phone_shift;
+            break;
+        default:
+            return;
+        }
+        final String text = mView.getContext().getString(resId);
+        sendWindowStateChanged(text);
+    }
+
+    /**
      * Announces that the keyboard has been hidden.
      */
     private void announceKeyboardHidden() {
@@ -214,7 +227,7 @@
     }
 
     /**
-     * Proxy method for View.getAccessibilityNodeProvider(). This method is called in SDK
+     * Delegate method for View.getAccessibilityNodeProvider(). This method is called in SDK
      * version 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) and higher to obtain the virtual
      * node hierarchy provider.
      *
@@ -222,10 +235,7 @@
      * @return The accessibility node provider for the current keyboard.
      */
     @Override
-    public AccessibilityEntityProvider getAccessibilityNodeProvider(final View host) {
-        if (mView == null) {
-            return null;
-        }
+    public MainKeyboardAccessibilityNodeProvider getAccessibilityNodeProvider(final View host) {
         return getAccessibilityNodeProvider();
     }
 
@@ -238,10 +248,6 @@
      * @return {@code true} if the event is handled
      */
     public boolean dispatchHoverEvent(final MotionEvent event, final KeyDetector keyDetector) {
-        if (mView == null) {
-            return false;
-        }
-
         final int x = (int) event.getX();
         final int y = (int) event.getY();
         final Key previousKey = mLastHoverKey;
@@ -275,14 +281,14 @@
     }
 
     /**
-     * @return A lazily-instantiated node provider for this view proxy.
+     * @return A lazily-instantiated node provider for this view delegate.
      */
-    private AccessibilityEntityProvider getAccessibilityNodeProvider() {
+    private MainKeyboardAccessibilityNodeProvider getAccessibilityNodeProvider() {
         // Instantiate the provide only when requested. Since the system
         // will call this method multiple times it is a good practice to
         // cache the provider instance.
         if (mAccessibilityNodeProvider == null) {
-            mAccessibilityNodeProvider = new AccessibilityEntityProvider(mView);
+            mAccessibilityNodeProvider = new MainKeyboardAccessibilityNodeProvider(mView);
         }
         return mAccessibilityNodeProvider;
     }
@@ -301,7 +307,7 @@
     }
 
     /**
-     * Simulates a key press by injecting touch event into the keyboard view.
+     * Simulates a key press by injecting touch an event into the keyboard view.
      * This avoids the complexity of trackers and listeners within the keyboard.
      *
      * @param key The key to press.
@@ -318,7 +324,7 @@
     }
 
     /**
-     * Simulates a key release by injecting touch event into the keyboard view.
+     * Simulates a key release by injecting touch an event into the keyboard view.
      * This avoids the complexity of trackers and listeners within the keyboard.
      *
      * @param key The key to release.
@@ -367,7 +373,7 @@
         if (key == null) {
             return false;
         }
-        final AccessibilityEntityProvider provider = getAccessibilityNodeProvider();
+        final MainKeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider();
 
         switch (event.getAction()) {
         case MotionEvent.ACTION_HOVER_ENTER:
@@ -383,72 +389,4 @@
         }
         return true;
     }
-
-    /**
-     * Notifies the user of changes in the keyboard shift state.
-     */
-    public void notifyShiftState() {
-        if (mView == null || mKeyboard == null) {
-            return;
-        }
-
-        final KeyboardId keyboardId = mKeyboard.mId;
-        final int elementId = keyboardId.mElementId;
-        final Context context = mView.getContext();
-        final CharSequence text;
-
-        switch (elementId) {
-        case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED:
-        case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED:
-            text = context.getText(R.string.spoken_description_shiftmode_locked);
-            break;
-        case KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED:
-        case KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED:
-        case KeyboardId.ELEMENT_SYMBOLS_SHIFTED:
-            text = context.getText(R.string.spoken_description_shiftmode_on);
-            break;
-        default:
-            text = context.getText(R.string.spoken_description_shiftmode_off);
-        }
-        AccessibilityUtils.getInstance().announceForAccessibility(mView, text);
-    }
-
-    /**
-     * Notifies the user of changes in the keyboard symbols state.
-     */
-    public void notifySymbolsState() {
-        if (mView == null || mKeyboard == null) {
-            return;
-        }
-
-        final KeyboardId keyboardId = mKeyboard.mId;
-        final int elementId = keyboardId.mElementId;
-        final Context context = mView.getContext();
-        final int resId;
-
-        switch (elementId) {
-        case KeyboardId.ELEMENT_ALPHABET:
-        case KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED:
-        case KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED:
-        case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED:
-        case KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED:
-            resId = R.string.spoken_description_mode_alpha;
-            break;
-        case KeyboardId.ELEMENT_SYMBOLS:
-        case KeyboardId.ELEMENT_SYMBOLS_SHIFTED:
-            resId = R.string.spoken_description_mode_symbol;
-            break;
-        case KeyboardId.ELEMENT_PHONE:
-            resId = R.string.spoken_description_mode_phone;
-            break;
-        case KeyboardId.ELEMENT_PHONE_SYMBOLS:
-            resId = R.string.spoken_description_mode_phone_shift;
-            break;
-        default:
-            return;
-        }
-
-        final String text = context.getString(resId);
-        AccessibilityUtils.getInstance().announceForAccessibility(mView, text);
-    }
 }
diff --git a/java/src/com/android/inputmethod/accessibility/AccessibilityEntityProvider.java b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityNodeProvider.java
similarity index 95%
rename from java/src/com/android/inputmethod/accessibility/AccessibilityEntityProvider.java
rename to java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityNodeProvider.java
index ec1ab35..f69d316 100644
--- a/java/src/com/android/inputmethod/accessibility/AccessibilityEntityProvider.java
+++ b/java/src/com/android/inputmethod/accessibility/MainKeyboardAccessibilityNodeProvider.java
@@ -47,8 +47,8 @@
  * virtual views, thus conveying their logical structure.
  * </p>
  */
-public final class AccessibilityEntityProvider extends AccessibilityNodeProviderCompat {
-    private static final String TAG = AccessibilityEntityProvider.class.getSimpleName();
+public final class MainKeyboardAccessibilityNodeProvider extends AccessibilityNodeProviderCompat {
+    private static final String TAG = MainKeyboardAccessibilityNodeProvider.class.getSimpleName();
     private static final int UNDEFINED = Integer.MIN_VALUE;
 
     private final KeyCodeDescriptionMapper mKeyCodeDescriptionMapper;
@@ -64,23 +64,14 @@
     private int mAccessibilityFocusedView = UNDEFINED;
 
     /** The current keyboard view. */
-    private KeyboardView mKeyboardView;
+    private final KeyboardView mKeyboardView;
 
     /** The current keyboard. */
     private Keyboard mKeyboard;
 
-    public AccessibilityEntityProvider(final KeyboardView keyboardView) {
+    public MainKeyboardAccessibilityNodeProvider(final KeyboardView keyboardView) {
         mKeyCodeDescriptionMapper = KeyCodeDescriptionMapper.getInstance();
         mAccessibilityUtils = AccessibilityUtils.getInstance();
-        setView(keyboardView);
-    }
-
-    /**
-     * Sets the keyboard view represented by this node provider.
-     *
-     * @param keyboardView The keyboard view to represent.
-     */
-    public void setView(final KeyboardView keyboardView) {
         mKeyboardView = keyboardView;
         updateParentLocation();
 
diff --git a/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java b/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java
index a0d7641..6e32e74 100644
--- a/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java
+++ b/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java
@@ -29,8 +29,8 @@
             Context.class, String.class, Integer.TYPE, String.class, Locale.class);
 
     @SuppressWarnings("deprecation")
-    public static void addWord(final Context context, final String word, final int freq,
-            final String shortcut, final Locale locale) {
+    public static void addWord(final Context context, final String word,
+            final int freq, final String shortcut, final Locale locale) {
         if (hasNewerAddWord()) {
             CompatUtils.invoke(Words.class, null, METHOD_addWord, context, word, freq, shortcut,
                     locale);
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index 0235fde..589e99e 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -26,7 +26,6 @@
 import android.view.View;
 import android.view.inputmethod.EditorInfo;
 
-import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy;
 import com.android.inputmethod.compat.InputMethodServiceCompatUtils;
 import com.android.inputmethod.keyboard.KeyboardLayoutSet.KeyboardLayoutSetException;
 import com.android.inputmethod.keyboard.internal.KeyboardState;
@@ -123,7 +122,7 @@
         builder.setOptions(
                 mSubtypeSwitcher.isShortcutImeEnabled(),
                 settingsValues.mShowsVoiceInputKey,
-                mLatinIME.shouldSwitchToOtherInputMethods());
+                mLatinIME.shouldShowLanguageSwitchKey());
         mKeyboardLayoutSet = builder.build();
         mCurrentSettingsValues = settingsValues;
         try {
@@ -148,6 +147,7 @@
 
     public void onHideWindow() {
         mIsAutoCorrectionActive = false;
+        mKeyboardView.onHideWindow();
     }
 
     private void setKeyboard(final Keyboard keyboard) {
@@ -353,11 +353,6 @@
         mEmojiPalettesView.setHardwareAcceleratedDrawingEnabled(
                 isHardwareAcceleratedDrawingEnabled);
         mEmojiPalettesView.setKeyboardActionListener(mLatinIME);
-
-        // This always needs to be set since the accessibility state can
-        // potentially change without the input view being re-created.
-        AccessibleKeyboardViewProxy.getInstance().setView(mKeyboardView);
-
         return mCurrentInputView;
     }
 
diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
index ecef8cc..8f79a91 100644
--- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
@@ -39,7 +39,7 @@
 import android.widget.TextView;
 
 import com.android.inputmethod.accessibility.AccessibilityUtils;
-import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy;
+import com.android.inputmethod.accessibility.MainKeyboardAccessibilityDelegate;
 import com.android.inputmethod.annotations.ExternallyReferenced;
 import com.android.inputmethod.keyboard.internal.DrawingHandler;
 import com.android.inputmethod.keyboard.internal.DrawingPreviewPlacerView;
@@ -179,6 +179,8 @@
     private final DrawingHandler mDrawingHandler =
             new DrawingHandler(this);
 
+    private final MainKeyboardAccessibilityDelegate mAccessibilityDelegate;
+
     public MainKeyboardView(final Context context, final AttributeSet attrs) {
         this(context, attrs, R.attr.mainKeyboardViewStyle);
     }
@@ -278,6 +280,8 @@
 
         mLanguageOnSpacebarHorizontalMargin = (int)getResources().getDimension(
                 R.dimen.config_language_on_spacebar_horizontal_margin);
+
+        mAccessibilityDelegate = new MainKeyboardAccessibilityDelegate(this);
     }
 
     @Override
@@ -404,9 +408,7 @@
             ResearchLogger.mainKeyboardView_setKeyboard(keyboard, orientation);
         }
 
-        // This always needs to be set since the accessibility state can
-        // potentially change without the keyboard being set again.
-        AccessibleKeyboardViewProxy.getInstance().setKeyboard(keyboard);
+        mAccessibilityDelegate.setKeyboard(keyboard);
     }
 
     /**
@@ -769,6 +771,12 @@
         mMoreKeysKeyboardCache.clear();
     }
 
+    public void onHideWindow() {
+        if (AccessibilityUtils.getInstance().isAccessibilityEnabled()) {
+            mAccessibilityDelegate.onHideWindow();
+        }
+    }
+
     /**
      * Receives hover events from the input framework.
      *
@@ -779,8 +787,7 @@
     @Override
     public boolean dispatchHoverEvent(final MotionEvent event) {
         if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) {
-            return AccessibleKeyboardViewProxy.getInstance().dispatchHoverEvent(
-                    event, mKeyDetector);
+            return mAccessibilityDelegate.dispatchHoverEvent(event, mKeyDetector);
         }
 
         // Reflection doesn't support calling superclass methods.
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
index 5238395..c6b0d35 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
@@ -23,7 +23,7 @@
 import com.android.inputmethod.annotations.UsedForTesting;
 import com.android.inputmethod.keyboard.ProximityInfo;
 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
-import com.android.inputmethod.latin.personalization.PersonalizationDictionary;
+import com.android.inputmethod.latin.personalization.DecayingExpandableBinaryDictionaryBase;
 import com.android.inputmethod.latin.personalization.PersonalizationHelper;
 import com.android.inputmethod.latin.personalization.UserHistoryDictionary;
 import com.android.inputmethod.latin.utils.CollectionUtils;
@@ -33,9 +33,11 @@
 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
@@ -49,11 +51,12 @@
     private static final int CAPITALIZED_FORM_MAX_PROBABILITY_FOR_INSERT = 140;
 
     private Dictionaries mDictionaries = new Dictionaries();
+    private boolean mIsUserDictEnabled = false;
     private volatile CountDownLatch mLatchForWaitingLoadingMainDictionary = new CountDownLatch(0);
     // To synchronize assigning mDictionaries to ensure closing dictionaries.
     private Object mLock = new Object();
 
-    private static final String[] dictTypesOrderedToGetSuggestion =
+    private static final String[] DICT_TYPES_ORDERED_TO_GET_SUGGESTION =
             new String[] {
                 Dictionary.TYPE_MAIN,
                 Dictionary.TYPE_USER_HISTORY,
@@ -62,6 +65,10 @@
                 Dictionary.TYPE_CONTACTS
             };
 
+    private static final String[] SUB_DICT_TYPES =
+            Arrays.copyOfRange(DICT_TYPES_ORDERED_TO_GET_SUGGESTION, 1 /* start */,
+                    DICT_TYPES_ORDERED_TO_GET_SUGGESTION.length);
+
     /**
      * Class contains dictionaries for a locale.
      */
@@ -71,26 +78,19 @@
                 CollectionUtils.newConcurrentHashMap();
         public final ConcurrentHashMap<String, ExpandableBinaryDictionary> mSubDictMap =
                 CollectionUtils.newConcurrentHashMap();
-        // TODO: Remove sub dictionary members and use mSubDictMap.
-        public final UserBinaryDictionary mUserDictionary;
 
         public Dictionaries() {
             mLocale = null;
-            mUserDictionary = null;
         }
 
         public Dictionaries(final Locale locale, final Dictionary mainDict,
-            final ExpandableBinaryDictionary contactsDict, final UserBinaryDictionary userDict,
-            final ExpandableBinaryDictionary userHistoryDict,
-            final ExpandableBinaryDictionary personalizationDict) {
+                final Map<String, ExpandableBinaryDictionary> subDicts) {
             mLocale = locale;
             // Main dictionary can be asynchronously loaded.
             setMainDict(mainDict);
-            setSubDict(Dictionary.TYPE_CONTACTS, contactsDict);
-            mUserDictionary = userDict;
-            setSubDict(Dictionary.TYPE_USER, mUserDictionary);
-            setSubDict(Dictionary.TYPE_USER_HISTORY, userHistoryDict);
-            setSubDict(Dictionary.TYPE_PERSONALIZATION, personalizationDict);
+            for (final Map.Entry<String, ExpandableBinaryDictionary> entry : subDicts.entrySet()) {
+                setSubDict(entry.getKey(), entry.getValue());
+            }
         }
 
         private void setSubDict(final String dictType, final ExpandableBinaryDictionary dict) {
@@ -144,6 +144,21 @@
         return mDictionaries.mLocale;
     }
 
+    private static ExpandableBinaryDictionary getSubDict(final String dictType,
+            final Context context, final Locale locale, final File dictFile) {
+        if (Dictionary.TYPE_CONTACTS.equals(dictType)) {
+            return new ContactsBinaryDictionary(context, locale, dictFile);
+        } else if (Dictionary.TYPE_USER.equals(dictType)) {
+            return new UserBinaryDictionary(context, locale, dictFile);
+        } else if (Dictionary.TYPE_USER_HISTORY.equals(dictType)) {
+            return PersonalizationHelper.getUserHistoryDictionary(context, locale);
+        } else if (Dictionary.TYPE_PERSONALIZATION.equals(dictType)) {
+            return PersonalizationHelper.getPersonalizationDictionary(context, locale);
+        } else {
+            return null;
+        }
+    }
+
     public void resetDictionaries(final Context context, final Locale newLocale,
             final boolean useContactsDict, final boolean usePersonalizedDicts,
             final boolean forceReloadMainDictionary,
@@ -151,11 +166,15 @@
         final boolean localeHasBeenChanged = !newLocale.equals(mDictionaries.mLocale);
         // We always try to have the main dictionary. Other dictionaries can be unused.
         final boolean reloadMainDictionary = localeHasBeenChanged || forceReloadMainDictionary;
-        final boolean closeContactsDictionary = localeHasBeenChanged || !useContactsDict;
-        final boolean closeUserDictionary = localeHasBeenChanged;
-        final boolean closeUserHistoryDictionary = localeHasBeenChanged || !usePersonalizedDicts;
-        final boolean closePersonalizationDictionary =
-                localeHasBeenChanged || !usePersonalizedDicts;
+        final Set<String> subDictTypesToUse = CollectionUtils.newHashSet();
+        if (useContactsDict) {
+            subDictTypesToUse.add(Dictionary.TYPE_USER);
+        }
+        subDictTypesToUse.add(Dictionary.TYPE_USER);
+        if (usePersonalizedDicts) {
+            subDictTypesToUse.add(Dictionary.TYPE_USER_HISTORY);
+            subDictTypesToUse.add(Dictionary.TYPE_PERSONALIZATION);
+        }
 
         final Dictionary newMainDict;
         if (reloadMainDictionary) {
@@ -165,49 +184,25 @@
             newMainDict = mDictionaries.getMainDict();
         }
 
-        // Open or move contacts dictionary.
-        final ExpandableBinaryDictionary newContactsDict;
-        if (!closeContactsDictionary && mDictionaries.hasDict(Dictionary.TYPE_CONTACTS)) {
-            newContactsDict = mDictionaries.getSubDict(Dictionary.TYPE_CONTACTS);
-        } else if (useContactsDict) {
-            newContactsDict = new ContactsBinaryDictionary(context, newLocale);
-        } else {
-            newContactsDict = null;
-        }
-
-        // Open or move user dictionary.
-        final UserBinaryDictionary newUserDictionary;
-        if (!closeUserDictionary && mDictionaries.hasDict(Dictionary.TYPE_USER)) {
-            newUserDictionary = mDictionaries.mUserDictionary;
-        } else {
-            newUserDictionary = new UserBinaryDictionary(context, newLocale);
-        }
-
-        // Open or move user history dictionary.
-        final ExpandableBinaryDictionary newUserHistoryDict;
-        if (!closeUserHistoryDictionary && mDictionaries.hasDict(Dictionary.TYPE_USER_HISTORY)) {
-            newUserHistoryDict = mDictionaries.getSubDict(Dictionary.TYPE_USER_HISTORY);
-        } else if (usePersonalizedDicts) {
-            newUserHistoryDict = PersonalizationHelper.getUserHistoryDictionary(context, newLocale);
-        } else {
-            newUserHistoryDict = null;
-        }
-
-        // Open or move personalization dictionary.
-        final ExpandableBinaryDictionary newPersonalizationDict;
-        if (!closePersonalizationDictionary
-                && mDictionaries.hasDict(Dictionary.TYPE_PERSONALIZATION)) {
-            newPersonalizationDict = mDictionaries.getSubDict(Dictionary.TYPE_PERSONALIZATION);
-        } else if (usePersonalizedDicts) {
-            newPersonalizationDict =
-                    PersonalizationHelper.getPersonalizationDictionary(context, newLocale);
-        } else {
-            newPersonalizationDict = null;
+        final Map<String, ExpandableBinaryDictionary> subDicts = CollectionUtils.newHashMap();
+        for (final String dictType : SUB_DICT_TYPES) {
+            if (!subDictTypesToUse.contains(dictType)) {
+                // This dictionary will not be used.
+                continue;
+            }
+            final ExpandableBinaryDictionary dict;
+            if (!localeHasBeenChanged && mDictionaries.hasDict(dictType)) {
+                // Continue to use current dictionary.
+                dict = mDictionaries.getSubDict(dictType);
+            } else {
+                // Start to use new dictionary.
+                dict = getSubDict(dictType, context, newLocale, null /* dictFile */);
+            }
+            subDicts.put(dictType, dict);
         }
 
         // Replace Dictionaries.
-        final Dictionaries newDictionaries = new Dictionaries(newLocale, newMainDict,
-                newContactsDict,  newUserDictionary, newUserHistoryDict, newPersonalizationDict);
+        final Dictionaries newDictionaries = new Dictionaries(newLocale, newMainDict, subDicts);
         final Dictionaries oldDictionaries;
         synchronized (mLock) {
             oldDictionaries = mDictionaries;
@@ -219,22 +214,14 @@
         if (listener != null) {
             listener.onUpdateMainDictionaryAvailability(hasInitializedMainDictionary());
         }
-
         // Clean up old dictionaries.
         if (reloadMainDictionary) {
             oldDictionaries.closeDict(Dictionary.TYPE_MAIN);
         }
-        if (closeContactsDictionary) {
-            oldDictionaries.closeDict(Dictionary.TYPE_CONTACTS);
-        }
-        if (closeUserDictionary) {
-            oldDictionaries.closeDict(Dictionary.TYPE_USER);
-        }
-        if (closeUserHistoryDictionary) {
-            oldDictionaries.closeDict(Dictionary.TYPE_USER_HISTORY);
-        }
-        if (closePersonalizationDictionary) {
-            oldDictionaries.closeDict(Dictionary.TYPE_PERSONALIZATION);
+        for (final String dictType : SUB_DICT_TYPES) {
+            if (localeHasBeenChanged || !subDictTypesToUse.contains(dictType)) {
+                oldDictionaries.closeDict(dictType);
+            }
         }
         oldDictionaries.mDictMap.clear();
         oldDictionaries.mSubDictMap.clear();
@@ -270,52 +257,28 @@
             final ArrayList<String> dictionaryTypes, final HashMap<String, File> dictionaryFiles,
             final Map<String, Map<String, String>> additionalDictAttributes) {
         Dictionary mainDictionary = null;
-        ContactsBinaryDictionary contactsDictionary = null;
-        UserBinaryDictionary userDictionary = null;
-        UserHistoryDictionary userHistoryDictionary = null;
-        PersonalizationDictionary personalizationDictionary = null;
+        final Map<String, ExpandableBinaryDictionary> subDicts = CollectionUtils.newHashMap();
 
         for (final String dictType : dictionaryTypes) {
             if (dictType.equals(Dictionary.TYPE_MAIN)) {
                 mainDictionary = DictionaryFactory.createMainDictionaryFromManager(context, locale);
-            } else if (dictType.equals(Dictionary.TYPE_USER_HISTORY)) {
-                userHistoryDictionary =
-                        PersonalizationHelper.getUserHistoryDictionary(context, locale);
-                // Staring with an empty user history dictionary for testing.
-                // Testing program may populate this dictionary before actual testing.
-                userHistoryDictionary.reloadDictionaryIfRequired();
-                userHistoryDictionary.waitAllTasksForTests();
-                if (additionalDictAttributes.containsKey(dictType)) {
-                    userHistoryDictionary.clearAndFlushDictionaryWithAdditionalAttributes(
-                            additionalDictAttributes.get(dictType));
-                }
-            } else if (dictType.equals(Dictionary.TYPE_PERSONALIZATION)) {
-                personalizationDictionary =
-                        PersonalizationHelper.getPersonalizationDictionary(context, locale);
-                // Staring with an empty personalization dictionary for testing.
-                // Testing program may populate this dictionary before actual testing.
-                personalizationDictionary.reloadDictionaryIfRequired();
-                personalizationDictionary.waitAllTasksForTests();
-                if (additionalDictAttributes.containsKey(dictType)) {
-                    personalizationDictionary.clearAndFlushDictionaryWithAdditionalAttributes(
-                            additionalDictAttributes.get(dictType));
-                }
-            } else if (dictType.equals(Dictionary.TYPE_USER)) {
-                final File file = dictionaryFiles.get(dictType);
-                userDictionary = new UserBinaryDictionary(context, locale, file);
-                userDictionary.reloadDictionaryIfRequired();
-                userDictionary.waitAllTasksForTests();
-            } else if (dictType.equals(Dictionary.TYPE_CONTACTS)) {
-                final File file = dictionaryFiles.get(dictType);
-                contactsDictionary = new ContactsBinaryDictionary(context, locale, file);
-                contactsDictionary.reloadDictionaryIfRequired();
-                contactsDictionary.waitAllTasksForTests();
             } else {
-                throw new RuntimeException("Unknown dictionary type: " + dictType);
+                final File dictFile = dictionaryFiles.get(dictType);
+                final ExpandableBinaryDictionary dict = getSubDict(
+                        dictType, context, locale, dictFile);
+                if (additionalDictAttributes.containsKey(dictType)) {
+                    dict.clearAndFlushDictionaryWithAdditionalAttributes(
+                            additionalDictAttributes.get(dictType));
+                }
+                if (dict == null) {
+                    throw new RuntimeException("Unknown dictionary type: " + dictType);
+                }
+                dict.reloadDictionaryIfRequired();
+                dict.waitAllTasksForTests();
+                subDicts.put(dictType, dict);
             }
         }
-        mDictionaries = new Dictionaries(locale, mainDictionary, contactsDictionary,
-                userDictionary, userHistoryDictionary, personalizationDictionary);
+        mDictionaries = new Dictionaries(locale, mainDictionary, subDicts);
     }
 
     public void closeDictionaries() {
@@ -364,19 +327,15 @@
     }
 
     public boolean isUserDictionaryEnabled() {
-        final UserBinaryDictionary userDictionary = mDictionaries.mUserDictionary;
-        if (userDictionary == null) {
-            return false;
-        }
-        return userDictionary.mEnabled;
+        return mIsUserDictEnabled;
     }
 
-    public void addWordToUserDictionary(String word) {
-        final UserBinaryDictionary userDictionary = mDictionaries.mUserDictionary;
-        if (userDictionary == null) {
+    public void addWordToUserDictionary(final Context context, final String word) {
+        final Locale locale = getLocale();
+        if (locale == null) {
             return;
         }
-        userDictionary.addWordToUserDictionary(word);
+        UserBinaryDictionary.addWordToUserDictionary(context, locale, word);
     }
 
     public void addToUserHistory(final String suggestion, final boolean wasAutoCapitalized,
@@ -448,7 +407,7 @@
         final SuggestionResults suggestionResults =
                 new SuggestionResults(dictionaries.mLocale, SuggestedWords.MAX_SUGGESTIONS);
         final float[] languageWeight = new float[] { Dictionary.NOT_A_LANGUAGE_WEIGHT };
-        for (final String dictType : dictTypesOrderedToGetSuggestion) {
+        for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTION) {
             final Dictionary dictionary = dictMap.get(dictType);
             if (null == dictionary) continue;
             final ArrayList<SuggestedWordInfo> dictionarySuggestions =
diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
index 550db4a..cda429e 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
@@ -97,6 +97,8 @@
 
     private final ReentrantReadWriteLock mLock;
 
+    private Map<String, String> mAdditionalAttributeMap = null;
+
     /* A extension for a binary dictionary file. */
     protected static final String DICT_FILE_EXTENSION = ".dict";
 
@@ -196,6 +198,9 @@
 
     protected Map<String, String> getHeaderAttributeMap() {
         HashMap<String, String> attributeMap = new HashMap<String, String>();
+        if (mAdditionalAttributeMap != null) {
+            attributeMap.putAll(mAdditionalAttributeMap);
+        }
         attributeMap.put(DictionaryHeader.DICTIONARY_ID_KEY, mDictName);
         attributeMap.put(DictionaryHeader.DICTIONARY_LOCALE_KEY, mLocale.toString());
         attributeMap.put(DictionaryHeader.DICTIONARY_VERSION_KEY,
@@ -592,6 +597,12 @@
     }
 
     @UsedForTesting
+    public void clearAndFlushDictionaryWithAdditionalAttributes(
+            final Map<String, String> attributeMap) {
+        mAdditionalAttributeMap = attributeMap;
+        clear();
+    }
+
     public void dumpAllWordsForDebug() {
         reloadDictionaryIfRequired();
         asyncExecuteTaskWithLock(mLock.readLock(), new Runnable() {
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index f1b1b8d..d64a1a6 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -55,7 +55,6 @@
 import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.inputmethod.accessibility.AccessibilityUtils;
-import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy;
 import com.android.inputmethod.annotations.UsedForTesting;
 import com.android.inputmethod.compat.InputMethodServiceCompatUtils;
 import com.android.inputmethod.dictionarypack.DictionaryPackConstants;
@@ -1002,10 +1001,6 @@
         LatinImeLogger.commit();
         mKeyboardSwitcher.onHideWindow();
 
-        if (AccessibilityUtils.getInstance().isAccessibilityEnabled()) {
-            AccessibleKeyboardViewProxy.getInstance().onHideWindow();
-        }
-
         if (TRACE) Debug.stopMethodTracing();
         if (isShowingOptionDialog()) {
             mOptionsDialog.dismiss();
@@ -1179,7 +1174,8 @@
         } else {
             wordToEdit = word;
         }
-        mInputLogic.mSuggest.mDictionaryFacilitator.addWordToUserDictionary(wordToEdit);
+        mInputLogic.mSuggest.mDictionaryFacilitator.addWordToUserDictionary(
+                this /* context */, wordToEdit);
     }
 
     // Callback for the {@link SuggestionStripView}, to call when the important notice strip is
@@ -1596,18 +1592,6 @@
     public void onReleaseKey(final int primaryCode, final boolean withSliding) {
         mKeyboardSwitcher.onReleaseKey(primaryCode, withSliding, getCurrentAutoCapsState(),
                 getCurrentRecapitalizeState());
-
-        // If accessibility is on, ensure the user receives keyboard state updates.
-        if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) {
-            switch (primaryCode) {
-            case Constants.CODE_SHIFT:
-                AccessibleKeyboardViewProxy.getInstance().notifyShiftState();
-                break;
-            case Constants.CODE_SWITCH_ALPHA_SYMBOL:
-                AccessibleKeyboardViewProxy.getInstance().notifySymbolsState();
-                break;
-            }
-        }
     }
 
     private HardwareEventDecoder getHardwareKeyEventDecoder(final int deviceId) {
diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
index 2b0be54..64cc562 100644
--- a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
+++ b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
@@ -410,12 +410,21 @@
 
     public boolean shouldOfferSwitchingToNextInputMethod(final IBinder binder,
             boolean defaultValue) {
-        // Use the default value instead on Jelly Bean MR2 and previous where
-        // {@link InputMethodManager#shouldOfferSwitchingToNextInputMethod} isn't yet available
-        // and on KitKat where the API is still just a stub to return true always.
-        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
+        // Use the default value instead on Jelly Bean MR2 and previous, where
+        // {@link InputMethodManager#shouldOfferSwitchingToNextInputMethod} isn't yet available.
+        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
             return defaultValue;
         }
+        // Use the default value instead on KitKat as well, where
+        // {@link InputMethodManager#shouldOfferSwitchingToNextInputMethod} is still just a stub to
+        // return true always.
+        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
+            // Make sure this is actually KitKat.
+            // TODO: Consider to remove this check once the *next* version becomes available.
+            if (Build.VERSION.CODENAME.equals("REL")) {
+                return defaultValue;
+            }
+        }
         return mImmWrapper.shouldOfferSwitchingToNextInputMethod(binder);
     }
 }
diff --git a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java
index 9d9ce01..2006a5c 100644
--- a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java
@@ -29,7 +29,6 @@
 import android.util.Log;
 
 import com.android.inputmethod.compat.UserDictionaryCompatUtils;
-import com.android.inputmethod.latin.utils.LocaleUtils;
 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
 
 import java.io.File;
@@ -51,30 +50,21 @@
     // to auto-correct, so we set this to the highest frequency that won't, i.e. 14.
     private static final int USER_DICT_SHORTCUT_FREQUENCY = 14;
 
-    // TODO: use Words.SHORTCUT when we target JellyBean or above
-    final static String SHORTCUT = "shortcut";
-    private static final String[] PROJECTION_QUERY;
-    static {
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
-            PROJECTION_QUERY = new String[] {
-                Words.WORD,
-                SHORTCUT,
-                Words.FREQUENCY,
-            };
-        } else {
-            PROJECTION_QUERY = new String[] {
-                Words.WORD,
-                Words.FREQUENCY,
-            };
-        }
-    }
+    private static final String[] PROJECTION_QUERY_WITH_SHORTCUT = new String[] {
+        Words.WORD,
+        Words.SHORTCUT,
+        Words.FREQUENCY,
+    };
+    private static final String[] PROJECTION_QUERY_WITHOUT_SHORTCUT = new String[] {
+        Words.WORD,
+        Words.FREQUENCY,
+    };
 
     private static final String NAME = "userunigram";
 
     private ContentObserver mObserver;
     final private String mLocale;
     final private boolean mAlsoUseMoreRestrictiveLocales;
-    final public boolean mEnabled;
 
     public UserBinaryDictionary(final Context context, final Locale locale) {
         this(context, locale, false /* alsoUseMoreRestrictiveLocales */, null /* dictFile */);
@@ -120,7 +110,6 @@
             }
         };
         cres.registerContentObserver(Words.CONTENT_URI, true, mObserver);
-        mEnabled = readIsEnabled();
         reloadDictionaryIfRequired();
     }
 
@@ -182,10 +171,29 @@
         } else {
             requestArguments = localeElements;
         }
+        final String requestString = request.toString();
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+            try {
+                addWordsFromProjectionLocked(PROJECTION_QUERY_WITH_SHORTCUT, requestString,
+                        requestArguments);
+            } catch (IllegalArgumentException e) {
+                // This may happen on some non-compliant devices where the declared API is JB+ but
+                // the SHORTCUT column is not present for some reason.
+                addWordsFromProjectionLocked(PROJECTION_QUERY_WITHOUT_SHORTCUT, requestString,
+                        requestArguments);
+            }
+        } else {
+            addWordsFromProjectionLocked(PROJECTION_QUERY_WITHOUT_SHORTCUT, requestString,
+                    requestArguments);
+        }
+    }
+
+    private void addWordsFromProjectionLocked(final String[] query, String request,
+            final String[] requestArguments) throws IllegalArgumentException {
         Cursor cursor = null;
         try {
             cursor = mContext.getContentResolver().query(
-                Words.CONTENT_URI, PROJECTION_QUERY, request.toString(), requestArguments, null);
+                    Words.CONTENT_URI, query, request, requestArguments, null);
             addWordsLocked(cursor);
         } catch (final SQLiteException e) {
             Log.e(TAG, "SQLiteException in the remote User dictionary process.", e);
@@ -198,8 +206,8 @@
         }
     }
 
-    private boolean readIsEnabled() {
-        final ContentResolver cr = mContext.getContentResolver();
+    public static boolean isEnabled(final Context context) {
+        final ContentResolver cr = context.getContentResolver();
         final ContentProviderClient client = cr.acquireContentProviderClient(Words.CONTENT_URI);
         if (client != null) {
             client.release();
@@ -212,18 +220,15 @@
     /**
      * Adds a word to the user dictionary and makes it persistent.
      *
+     * @param context the context
+     * @param locale the locale
      * @param word the word to add. If the word is capitalized, then the dictionary will
      * recognize it as a capitalized word when searched.
      */
-    public synchronized void addWordToUserDictionary(final String word) {
+    public static void addWordToUserDictionary(final Context context, final Locale locale,
+            final String word) {
         // Update the user dictionary provider
-        final Locale locale;
-        if (USER_DICTIONARY_ALL_LANGUAGES == mLocale) {
-            locale = null;
-        } else {
-            locale = LocaleUtils.constructLocaleFromString(mLocale);
-        }
-        UserDictionaryCompatUtils.addWord(mContext, word,
+        UserDictionaryCompatUtils.addWord(context, word,
                 HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY, null, locale);
     }
 
@@ -245,7 +250,7 @@
         if (cursor == null) return;
         if (cursor.moveToFirst()) {
             final int indexWord = cursor.getColumnIndex(Words.WORD);
-            final int indexShortcut = hasShortcutColumn ? cursor.getColumnIndex(SHORTCUT) : 0;
+            final int indexShortcut = hasShortcutColumn ? cursor.getColumnIndex(Words.SHORTCUT) : 0;
             final int indexFrequency = cursor.getColumnIndex(Words.FREQUENCY);
             while (!cursor.isAfterLast()) {
                 final String word = cursor.getString(indexWord);
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index d2100d4..75432fb 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -784,11 +784,11 @@
             // TODO: remove this argument
             final LatinIME.UIHandler handler) {
         final int codePoint = inputTransaction.mEvent.mCodePoint;
+        final SettingsValues settingsValues = inputTransaction.mSettingsValues;
         boolean didAutoCorrect = false;
         // We avoid sending spaces in languages without spaces if we were composing.
         final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == codePoint
-                && !inputTransaction.mSettingsValues.mSpacingAndPunctuations
-                        .mCurrentLanguageHasSpaces
+                && !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
                 && mWordComposer.isComposingWord();
         if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) {
             // If we are in the middle of a recorrection, we need to commit the recorrection
@@ -798,13 +798,13 @@
         }
         // isComposingWord() may have changed since we stored wasComposing
         if (mWordComposer.isComposingWord()) {
-            if (inputTransaction.mSettingsValues.mCorrectionEnabled) {
+            if (settingsValues.mCorrectionEnabled) {
                 final String separator = shouldAvoidSendingCode ? LastComposedWord.NOT_A_SEPARATOR
                         : StringUtils.newSingleCodePointString(codePoint);
-                commitCurrentAutoCorrection(inputTransaction.mSettingsValues, separator, handler);
+                commitCurrentAutoCorrection(settingsValues, separator, handler);
                 didAutoCorrect = true;
             } else {
-                commitTyped(inputTransaction.mSettingsValues,
+                commitTyped(settingsValues,
                         StringUtils.newSingleCodePointString(codePoint));
             }
         }
@@ -821,20 +821,23 @@
             // Double quotes behave like they are usually preceded by space iff we are
             // not inside a double quote or after a digit.
             needsPrecedingSpace = !isInsideDoubleQuoteOrAfterDigit;
+        } else if (settingsValues.mSpacingAndPunctuations.isClusteringSymbol(codePoint)
+                && settingsValues.mSpacingAndPunctuations.isClusteringSymbol(
+                        mConnection.getCodePointBeforeCursor())) {
+            needsPrecedingSpace = false;
         } else {
-            needsPrecedingSpace = inputTransaction.mSettingsValues.isUsuallyPrecededBySpace(
-                    codePoint);
+            needsPrecedingSpace = settingsValues.isUsuallyPrecededBySpace(codePoint);
         }
 
         if (needsPrecedingSpace) {
-            promotePhantomSpace(inputTransaction.mSettingsValues);
+            promotePhantomSpace(settingsValues);
         }
         if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
             ResearchLogger.latinIME_handleSeparator(codePoint, mWordComposer.isComposingWord());
         }
 
         if (!shouldAvoidSendingCode) {
-            sendKeyCodePoint(inputTransaction.mSettingsValues, codePoint);
+            sendKeyCodePoint(settingsValues, codePoint);
         }
 
         if (Constants.CODE_SPACE == codePoint) {
@@ -852,7 +855,7 @@
                 swapSwapperAndSpace(inputTransaction);
                 mSpaceState = SpaceState.SWAP_PUNCTUATION;
             } else if ((SpaceState.PHANTOM == inputTransaction.mSpaceState
-                    && inputTransaction.mSettingsValues.isUsuallyFollowedBySpace(codePoint))
+                    && settingsValues.isUsuallyFollowedBySpace(codePoint))
                     || (Constants.CODE_DOUBLE_QUOTE == codePoint
                             && isInsideDoubleQuoteOrAfterDigit)) {
                 // If we are in phantom space state, and the user presses a separator, we want to
diff --git a/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java b/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java
index 352288f..38c28a7 100644
--- a/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java
+++ b/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java
@@ -18,15 +18,11 @@
 
 import android.content.Context;
 
-import com.android.inputmethod.annotations.UsedForTesting;
-import com.android.inputmethod.latin.Constants;
 import com.android.inputmethod.latin.Dictionary;
 import com.android.inputmethod.latin.ExpandableBinaryDictionary;
 import com.android.inputmethod.latin.makedict.DictionaryHeader;
-import com.android.inputmethod.latin.utils.LanguageModelParam;
 
 import java.io.File;
-import java.util.ArrayList;
 import java.util.Locale;
 import java.util.Map;
 
@@ -47,8 +43,6 @@
     /** The locale for this dictionary. */
     public final Locale mLocale;
 
-    private Map<String, String> mAdditionalAttributeMap = null;
-
     protected DecayingExpandableBinaryDictionaryBase(final Context context,
             final String dictName, final Locale locale, final String dictionaryType,
             final File dictFile) {
@@ -72,9 +66,6 @@
     @Override
     protected Map<String, String> getHeaderAttributeMap() {
         final Map<String, String> attributeMap = super.getHeaderAttributeMap();
-        if (mAdditionalAttributeMap != null) {
-            attributeMap.putAll(mAdditionalAttributeMap);
-        }
         attributeMap.put(DictionaryHeader.USES_FORGETTING_CURVE_KEY,
                 DictionaryHeader.ATTRIBUTE_VALUE_TRUE);
         attributeMap.put(DictionaryHeader.HAS_HISTORICAL_INFO_KEY,
@@ -92,13 +83,6 @@
         // No initial contents.
     }
 
-    @UsedForTesting
-    public void clearAndFlushDictionaryWithAdditionalAttributes(
-            final Map<String, String> attributeMap) {
-        mAdditionalAttributeMap = attributeMap;
-        clear();
-    }
-
     /* package */ void runGCIfRequired() {
         runGCIfRequired(false /* mindsBlockByGC */);
     }
diff --git a/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java b/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java
index 796921f..b8d2a22 100644
--- a/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java
+++ b/java/src/com/android/inputmethod/latin/settings/SpacingAndPunctuations.java
@@ -30,6 +30,7 @@
 public final class SpacingAndPunctuations {
     private final int[] mSortedSymbolsPrecededBySpace;
     private final int[] mSortedSymbolsFollowedBySpace;
+    private final int[] mSortedSymbolsClusteringTogether;
     private final int[] mSortedWordConnectors;
     public final int[] mSortedWordSeparators;
     public final PunctuationSuggestions mSuggestPuncList;
@@ -46,6 +47,8 @@
         // To be able to binary search the code point. See {@link #isUsuallyFollowedBySpace(int)}.
         mSortedSymbolsFollowedBySpace = StringUtils.toSortedCodePointArray(
                 res.getString(R.string.symbols_followed_by_space));
+        mSortedSymbolsClusteringTogether = StringUtils.toSortedCodePointArray(
+                res.getString(R.string.symbols_clustering_together));
         // To be able to binary search the code point. See {@link #isWordConnector(int)}.
         mSortedWordConnectors = StringUtils.toSortedCodePointArray(
                 res.getString(R.string.symbols_word_connectors));
@@ -85,6 +88,10 @@
         return Arrays.binarySearch(mSortedSymbolsFollowedBySpace, code) >= 0;
     }
 
+    public boolean isClusteringSymbol(final int code) {
+        return Arrays.binarySearch(mSortedSymbolsClusteringTogether, code) >= 0;
+    }
+
     public boolean isSentenceSeparator(final int code) {
         return code == mSentenceSeparator;
     }
diff --git a/native/jni/src/defines.h b/native/jni/src/defines.h
index 2fe2bd8..a80c975 100644
--- a/native/jni/src/defines.h
+++ b/native/jni/src/defines.h
@@ -293,13 +293,6 @@
 #define M_PI_F 3.14159265f
 #define MAX_PERCENTILE 100
 
-// Number of base-10 digits in the largest integer + 1 to leave room for a zero terminator.
-// As such, this is the maximum number of characters will be needed to represent an int as a
-// string, including the terminator; this is used as the size of a string buffer large enough to
-// hold any value that is intended to fit in an integer, e.g. in the code that reads the header
-// of the binary dictionary where a {key,value} string pair scheme is used.
-#define LARGEST_INT_DIGIT_COUNT 11
-
 #define NOT_A_CODE_POINT (-1)
 #define NOT_A_DISTANCE (-1)
 #define NOT_A_COORDINATE (-1)
@@ -325,6 +318,8 @@
 #define KEYCODE_SPACE ' '
 #define KEYCODE_SINGLE_QUOTE '\''
 #define KEYCODE_HYPHEN_MINUS '-'
+// Code point to indicate beginning-of-sentence. This is not in the code point space of unicode.
+#define CODE_POINT_BEGINNING_OF_SENTENCE 0x110000
 
 #define SUGGEST_INTERFACE_OUTPUT_SCALE 1000000.0f
 #define MAX_PROBABILITY 255
diff --git a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp
index d20accf..5608e27 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp
@@ -26,6 +26,13 @@
 
 namespace latinime {
 
+// Number of base-10 digits in the largest integer + 1 to leave room for a zero terminator.
+// As such, this is the maximum number of characters will be needed to represent an int as a
+// string, including the terminator; this is used as the size of a string buffer large enough to
+// hold any value that is intended to fit in an integer, e.g. in the code that reads the header
+// of the binary dictionary where a {key,value} string pair scheme is used.
+const int HeaderReadWriteUtils::LARGEST_INT_DIGIT_COUNT = 11;
+
 const int HeaderReadWriteUtils::MAX_ATTRIBUTE_KEY_LENGTH = 256;
 const int HeaderReadWriteUtils::MAX_ATTRIBUTE_VALUE_LENGTH = 256;
 
@@ -154,8 +161,8 @@
 /* static */ void HeaderReadWriteUtils::setIntAttributeInner(AttributeMap *const headerAttributes,
         const AttributeMap::key_type *const key, const int value) {
     AttributeMap::mapped_type valueVector;
-    char charBuf[LARGEST_INT_DIGIT_COUNT + 1];
-    snprintf(charBuf, LARGEST_INT_DIGIT_COUNT + 1, "%d", value);
+    char charBuf[LARGEST_INT_DIGIT_COUNT];
+    snprintf(charBuf, sizeof(charBuf), "%d", value);
     insertCharactersIntoVector(charBuf, &valueVector);
     (*headerAttributes)[*key] = valueVector;
 }
diff --git a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.h b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.h
index a6b4c4e..9b90488 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.h
@@ -92,6 +92,7 @@
  private:
     DISALLOW_IMPLICIT_CONSTRUCTORS(HeaderReadWriteUtils);
 
+    static const int LARGEST_INT_DIGIT_COUNT;
     static const int MAX_ATTRIBUTE_KEY_LENGTH;
     static const int MAX_ATTRIBUTE_VALUE_LENGTH;
 
diff --git a/tests/src/com/android/inputmethod/latin/InputLogicTests.java b/tests/src/com/android/inputmethod/latin/InputLogicTests.java
index d2dd292..29423e8 100644
--- a/tests/src/com/android/inputmethod/latin/InputLogicTests.java
+++ b/tests/src/com/android/inputmethod/latin/InputLogicTests.java
@@ -334,6 +334,18 @@
         assertEquals("manual pick then separator", EXPECTED_RESULT, mEditText.getText().toString());
     }
 
+    // This test matches the one in InputLogicTestsNonEnglish. In some non-English languages,
+    // ! and ? are clustering punctuation signs.
+    public void testClusteringPunctuation() {
+        final String WORD1_TO_TYPE = "test";
+        final String WORD2_TO_TYPE = "!!?!:!";
+        final String EXPECTED_RESULT = "test!!?!:!";
+        type(WORD1_TO_TYPE);
+        pickSuggestionManually(0, WORD1_TO_TYPE);
+        type(WORD2_TO_TYPE);
+        assertEquals("clustering punctuation", EXPECTED_RESULT, mEditText.getText().toString());
+    }
+
     public void testManualPickThenStripperThenPick() {
         final String WORD_TO_TYPE = "this";
         final String STRIPPER = "\n";
diff --git a/tests/src/com/android/inputmethod/latin/InputLogicTestsNonEnglish.java b/tests/src/com/android/inputmethod/latin/InputLogicTestsNonEnglish.java
index 1257ae2..68b6ee6 100644
--- a/tests/src/com/android/inputmethod/latin/InputLogicTestsNonEnglish.java
+++ b/tests/src/com/android/inputmethod/latin/InputLogicTestsNonEnglish.java
@@ -45,6 +45,19 @@
                 mEditText.getText().toString());
     }
 
+    public void testClusteringPunctuationForFrench() {
+        final String WORD1_TO_TYPE = "test";
+        final String WORD2_TO_TYPE = "!!?!:!";
+        // In English, the expected result would be "test!!?!:!"
+        final String EXPECTED_RESULT = "test !!?! : !";
+        changeLanguage("fr");
+        type(WORD1_TO_TYPE);
+        pickSuggestionManually(0, WORD1_TO_TYPE);
+        type(WORD2_TO_TYPE);
+        assertEquals("clustering punctuation for French", EXPECTED_RESULT,
+                mEditText.getText().toString());
+    }
+
     public void testWordThenSpaceThenPunctuationFromStripTwiceForFrench() {
         final String WORD_TO_TYPE = "test ";
         final String PUNCTUATION_FROM_STRIP = "!";