Merge "Make AccessibleKeyboardViewProxy not singleton"
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 b06955b..589e99e 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -122,7 +122,7 @@
         builder.setOptions(
                 mSubtypeSwitcher.isShortcutImeEnabled(),
                 settingsValues.mShowsVoiceInputKey,
-                mLatinIME.shouldSwitchToOtherInputMethods());
+                mLatinIME.shouldShowLanguageSwitchKey());
         mKeyboardLayoutSet = builder.build();
         mCurrentSettingsValues = settingsValues;
         try {
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
index 5238395..b6fcbd1 100644
--- a/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
+++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitatorForSuggest.java
@@ -49,6 +49,7 @@
     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();
@@ -71,24 +72,21 @@
                 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 contactsDict,
+            final ExpandableBinaryDictionary userDict,
             final ExpandableBinaryDictionary userHistoryDict,
             final ExpandableBinaryDictionary personalizationDict) {
             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, userDict);
             setSubDict(Dictionary.TYPE_USER_HISTORY, userHistoryDict);
             setSubDict(Dictionary.TYPE_PERSONALIZATION, personalizationDict);
         }
@@ -176,11 +174,12 @@
         }
 
         // Open or move user dictionary.
-        final UserBinaryDictionary newUserDictionary;
+        final ExpandableBinaryDictionary newUserDictionary;
         if (!closeUserDictionary && mDictionaries.hasDict(Dictionary.TYPE_USER)) {
-            newUserDictionary = mDictionaries.mUserDictionary;
+            newUserDictionary = mDictionaries.getSubDict(Dictionary.TYPE_USER);
         } else {
             newUserDictionary = new UserBinaryDictionary(context, newLocale);
+            mIsUserDictEnabled = UserBinaryDictionary.isEnabled(context);
         }
 
         // Open or move user history dictionary.
@@ -364,19 +363,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,
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index b653a23..d64a1a6 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1174,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
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/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;