Merge "Unbundle members in unigram_dictionary"
diff --git a/java/res/values/keycodes.xml b/java/res/values/keycodes.xml
index 59cc075..4f8bfed 100644
--- a/java/res/values/keycodes.xml
+++ b/java/res/values/keycodes.xml
@@ -23,6 +23,7 @@
     <integer name="key_tab">9</integer>
     <integer name="key_return">10</integer>
     <integer name="key_space">32</integer>
+    <integer name="key_dummy">0</integer>
     <integer name="key_shift">-1</integer>
     <integer name="key_switch_alpha_symbol">-2</integer>
     <integer name="key_delete">-5</integer>
diff --git a/java/res/xml-sw600dp/kbd_key_styles.xml b/java/res/xml-sw600dp/kbd_key_styles.xml
index aba1a80..e0171b0 100644
--- a/java/res/xml-sw600dp/kbd_key_styles.xml
+++ b/java/res/xml-sw600dp/kbd_key_styles.xml
@@ -78,14 +78,14 @@
         latin:code="@integer/key_shortcut"
         latin:keyIcon="iconShortcutKey"
         latin:keyActionFlags="noKeyPreview|altCodeWhileTyping"
-        latin:altCode="@integer/key_space"
+        latin:altCode="@integer/key_dummy"
         latin:parentStyle="f2PopupStyle" />
     <key-style
         latin:styleName="settingsKeyStyle"
         latin:code="@integer/key_settings"
         latin:keyIcon="iconSettingsKey"
         latin:keyActionFlags="noKeyPreview|altCodeWhileTyping"
-        latin:altCode="@integer/key_space"
+        latin:altCode="@integer/key_dummy"
         latin:backgroundType="functional" />
     <key-style
         latin:styleName="tabKeyStyle"
diff --git a/java/res/xml-sw768dp/kbd_key_styles.xml b/java/res/xml-sw768dp/kbd_key_styles.xml
index e6ec53d..d926650 100644
--- a/java/res/xml-sw768dp/kbd_key_styles.xml
+++ b/java/res/xml-sw768dp/kbd_key_styles.xml
@@ -60,14 +60,14 @@
         latin:code="@integer/key_shortcut"
         latin:keyIcon="iconShortcutKey"
         latin:keyActionFlags="noKeyPreview|altCodeWhileTyping"
-        latin:altCode="@integer/key_space"
+        latin:altCode="@integer/key_dummy"
         latin:backgroundType="functional" />
     <key-style
         latin:styleName="settingsKeyStyle"
         latin:code="@integer/key_settings"
         latin:keyIcon="iconSettingsKey"
         latin:keyActionFlags="noKeyPreview|altCodeWhileTyping"
-        latin:altCode="@integer/key_space"
+        latin:altCode="@integer/key_dummy"
         latin:backgroundType="functional" />
     <key-style
         latin:styleName="tabKeyStyle"
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index e267aa6..814b523 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -151,6 +151,9 @@
     }
 
     public Key getKey(int code) {
+        if (code == CODE_DUMMY) {
+            return null;
+        }
         final Integer keyCode = code;
         if (mKeyCache.containsKey(keyCode)) {
             return mKeyCache.get(keyCode);
@@ -232,19 +235,6 @@
         return mProximityInfo.getNearestKeys(x, y);
     }
 
-    public static String themeName(int themeId) {
-        // This should be aligned with theme-*.xml resource files' themeId attribute.
-        switch (themeId) {
-        case 0: return "Basic";
-        case 1: return "BasicHighContrast";
-        case 5: return "IceCreamSandwich";
-        case 6: return "Stone";
-        case 7: return "StoneBold";
-        case 8: return "GingerBread";
-        default: return null;
-        }
-    }
-
     public static String printableCode(int code) {
         switch (code) {
         case CODE_SHIFT: return "shift";
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardId.java b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
index 46285bf..3b3ff07 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardId.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
@@ -63,67 +63,82 @@
     // TODO: Remove this field.
     private final int mXmlId;
     public final int mElementState;
-    public final boolean mNavigateAction;
-    public final boolean mPasswordInput;
+    private final int mInputType;
+    private final int mImeOptions;
     private final boolean mSettingsKeyEnabled;
     public final boolean mClobberSettingsKey;
     public final boolean mShortcutKeyEnabled;
     public final boolean mHasShortcutKey;
-    public final int mImeAction;
-
-    // TODO: Remove this field.
-    private final EditorInfo mEditorInfo;
 
     private final int mHashCode;
 
     public KeyboardId(int xmlId, int elementState, Locale locale, int orientation, int width,
             int mode, EditorInfo editorInfo, boolean settingsKeyEnabled,
             boolean clobberSettingsKey, boolean shortcutKeyEnabled, boolean hasShortcutKey) {
-        final int inputType = (editorInfo != null) ? editorInfo.inputType : 0;
-        final int imeOptions = (editorInfo != null) ? editorInfo.imeOptions : 0;
-        this.mElementState = elementState;
+        this(xmlId, elementState, locale, orientation, width, mode,
+                (editorInfo != null ? editorInfo.inputType : 0),
+                (editorInfo != null ? editorInfo.imeOptions : 0),
+                settingsKeyEnabled, clobberSettingsKey, shortcutKeyEnabled, hasShortcutKey);
+    }
+
+    private KeyboardId(int xmlId, int elementState, Locale locale, int orientation, int width,
+            int mode, int inputType, int imeOptions, boolean settingsKeyEnabled,
+            boolean clobberSettingsKey, boolean shortcutKeyEnabled, boolean hasShortcutKey) {
         this.mLocale = locale;
         this.mOrientation = orientation;
         this.mWidth = width;
         this.mMode = mode;
         this.mXmlId = xmlId;
-        // Note: Turn off checking navigation flag to show TAB key for now.
-        this.mNavigateAction = InputTypeCompatUtils.isWebInputType(inputType);
-//                || EditorInfoCompatUtils.hasFlagNavigateNext(imeOptions)
-//                || EditorInfoCompatUtils.hasFlagNavigatePrevious(imeOptions);
-        this.mPasswordInput = InputTypeCompatUtils.isPasswordInputType(inputType)
-                || InputTypeCompatUtils.isVisiblePasswordInputType(inputType);
+        this.mElementState = elementState;
+        this.mInputType = inputType;
+        this.mImeOptions = imeOptions;
         this.mSettingsKeyEnabled = settingsKeyEnabled;
         this.mClobberSettingsKey = clobberSettingsKey;
         this.mShortcutKeyEnabled = shortcutKeyEnabled;
         this.mHasShortcutKey = hasShortcutKey;
-        // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and
-        // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}.
-        this.mImeAction = imeOptions & (
-                EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
 
-        this.mEditorInfo = editorInfo;
+        this.mHashCode = hashCode(this);
+    }
 
-        this.mHashCode = Arrays.hashCode(new Object[] {
-                locale,
-                orientation,
-                width,
-                mode,
-                xmlId,
-                elementState,
-                mNavigateAction,
-                mPasswordInput,
-                mSettingsKeyEnabled,
-                mClobberSettingsKey,
-                shortcutKeyEnabled,
-                hasShortcutKey,
-                mImeAction,
+    private static int hashCode(KeyboardId id) {
+        return Arrays.hashCode(new Object[] {
+                id.mOrientation,
+                id.mElementState,
+                id.mMode,
+                id.mWidth,
+                id.mXmlId,
+                id.navigateAction(),
+                id.passwordInput(),
+                id.mSettingsKeyEnabled,
+                id.mClobberSettingsKey,
+                id.mShortcutKeyEnabled,
+                id.mHasShortcutKey,
+                id.imeAction(),
+                id.mLocale
         });
     }
 
+    private boolean equals(KeyboardId other) {
+        if (other == this)
+            return true;
+        return other.mOrientation == this.mOrientation
+                && other.mElementState == this.mElementState
+                && other.mMode == this.mMode
+                && other.mWidth == this.mWidth
+                && other.mXmlId == this.mXmlId
+                && other.navigateAction() == this.navigateAction()
+                && other.passwordInput() == this.passwordInput()
+                && other.mSettingsKeyEnabled == this.mSettingsKeyEnabled
+                && other.mClobberSettingsKey == this.mClobberSettingsKey
+                && other.mShortcutKeyEnabled == this.mShortcutKeyEnabled
+                && other.mHasShortcutKey == this.mHasShortcutKey
+                && other.imeAction() == this.imeAction()
+                && other.mLocale.equals(this.mLocale);
+    }
+
     public KeyboardId cloneWithNewXml(int xmlId) {
         return new KeyboardId(xmlId, mElementState, mLocale, mOrientation, mWidth, mMode,
-                mEditorInfo, false, false, false, false);
+                mInputType, mImeOptions, false, false, false, false);
     }
 
     // Remove this method.
@@ -147,6 +162,26 @@
         return mElementState == ELEMENT_PHONE_SHIFT;
     }
 
+    public boolean navigateAction() {
+        // Note: Turn off checking navigation flag to show TAB key for now.
+        boolean navigateAction = InputTypeCompatUtils.isWebInputType(mInputType);
+//                || EditorInfoCompatUtils.hasFlagNavigateNext(mImeOptions)
+//                || EditorInfoCompatUtils.hasFlagNavigatePrevious(mImeOptions);
+        return navigateAction;
+    }
+
+    public boolean passwordInput() {
+        return InputTypeCompatUtils.isPasswordInputType(mInputType)
+                || InputTypeCompatUtils.isVisiblePasswordInputType(mInputType);
+    }
+
+    public int imeAction() {
+        // We are interested only in {@link EditorInfo#IME_MASK_ACTION} enum value and
+        // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}.
+        return mImeOptions & (
+                EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
+    }
+
     public boolean hasSettingsKey() {
         return mSettingsKeyEnabled && !mClobberSettingsKey;
     }
@@ -171,22 +206,6 @@
         return other instanceof KeyboardId && equals((KeyboardId) other);
     }
 
-    private boolean equals(KeyboardId other) {
-        return other.mLocale.equals(this.mLocale)
-            && other.mOrientation == this.mOrientation
-            && other.mWidth == this.mWidth
-            && other.mMode == this.mMode
-            && other.mXmlId == this.mXmlId
-            && other.mElementState == this.mElementState
-            && other.mNavigateAction == this.mNavigateAction
-            && other.mPasswordInput == this.mPasswordInput
-            && other.mSettingsKeyEnabled == this.mSettingsKeyEnabled
-            && other.mClobberSettingsKey == this.mClobberSettingsKey
-            && other.mShortcutKeyEnabled == this.mShortcutKeyEnabled
-            && other.mHasShortcutKey == this.mHasShortcutKey
-            && other.mImeAction == this.mImeAction;
-    }
-
     @Override
     public int hashCode() {
         return mHashCode;
@@ -199,11 +218,11 @@
                 mLocale,
                 (mOrientation == 1 ? "port" : "land"), mWidth,
                 modeName(mMode),
-                EditorInfoCompatUtils.imeOptionsName(mImeAction),
+                EditorInfoCompatUtils.imeOptionsName(imeAction()),
                 f2KeyModeName(f2KeyMode()),
                 (mClobberSettingsKey ? " clobberSettingsKey" : ""),
-                (mNavigateAction ? " navigateAction" : ""),
-                (mPasswordInput ? " passwordInput" : ""),
+                (navigateAction() ? " navigateAction" : ""),
+                (passwordInput() ? " passwordInput" : ""),
                 (hasSettingsKey() ? " hasSettingsKey" : ""),
                 (mShortcutKeyEnabled ? " shortcutKeyEnabled" : ""),
                 (mHasShortcutKey ? " hasShortcutKey" : "")
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index 165e9ae..1f3006d 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -191,11 +191,11 @@
             if (DEBUG_CACHE) {
                 Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": "
                         + ((ref == null) ? "LOAD" : "GCed") + " id=" + id
-                        + " theme=" + Keyboard.themeName(keyboard.mThemeId));
+                        + " theme=" + themeName(keyboard.mThemeId));
             }
         } else if (DEBUG_CACHE) {
             Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": HIT  id=" + id
-                    + " theme=" + Keyboard.themeName(keyboard.mThemeId));
+                    + " theme=" + themeName(keyboard.mThemeId));
         }
 
         keyboard.onAutoCorrectionStateChanged(mIsAutoCorrectionActive);
@@ -463,4 +463,17 @@
             }
         }
     }
+
+    private static String themeName(int themeId) {
+        // This should be aligned with theme-*.xml resource files' themeId attribute.
+        switch (themeId) {
+        case 0: return "Basic";
+        case 1: return "BasicHighContrast";
+        case 5: return "IceCreamSandwich";
+        case 6: return "Stone";
+        case 7: return "StoneBold";
+        case 8: return "GingerBread";
+        default: return null;
+        }
+    }
 }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
index 31785ff..66a9d04 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
@@ -609,9 +609,9 @@
             final boolean modeMatched = matchTypedValue(a,
                     R.styleable.Keyboard_Case_mode, id.mMode, KeyboardId.modeName(id.mMode));
             final boolean navigateActionMatched = matchBoolean(a,
-                    R.styleable.Keyboard_Case_navigateAction, id.mNavigateAction);
+                    R.styleable.Keyboard_Case_navigateAction, id.navigateAction());
             final boolean passwordInputMatched = matchBoolean(a,
-                    R.styleable.Keyboard_Case_passwordInput, id.mPasswordInput);
+                    R.styleable.Keyboard_Case_passwordInput, id.passwordInput());
             final boolean hasSettingsKeyMatched = matchBoolean(a,
                     R.styleable.Keyboard_Case_hasSettingsKey, id.hasSettingsKey());
             final boolean f2KeyModeMatched = matchInteger(a,
@@ -627,7 +627,7 @@
             // {@link android.view.inputmethod.EditorInfo#IME_FLAG_NO_ENTER_ACTION}. So matching
             // this attribute with id.mImeOptions as integer value is enough for our purpose.
             final boolean imeActionMatched = matchInteger(a,
-                    R.styleable.Keyboard_Case_imeAction, id.mImeAction);
+                    R.styleable.Keyboard_Case_imeAction, id.imeAction());
             final boolean localeCodeMatched = matchString(a,
                     R.styleable.Keyboard_Case_localeCode, id.mLocale.toString());
             final boolean languageCodeMatched = matchString(a,
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index ff850b7..98fea1b 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1602,21 +1602,7 @@
             final boolean shouldAutoCorrect = mSettingsValues.mAutoCorrectEnabled
                     && !mInputTypeNoAutoCorrect;
             if (shouldAutoCorrect && primaryCode != Keyboard.CODE_SINGLE_QUOTE) {
-                final boolean pickedDefaultSuggestion = pickDefaultSuggestion(primaryCode);
-                if (pickedDefaultSuggestion) {
-                    final CharSequence autoCorrection = mWordComposer.getAutoCorrectionOrNull();
-                    final String typedWord = mWordComposer.getTypedWord();
-                    if (TextUtils.isEmpty(typedWord)) {
-                        throw new RuntimeException("We have non-committed chars but the typed word "
-                                + "is empty? Impossible! I must commit suicide.");
-                    }
-                    if (!typedWord.equals(autoCorrection)) {
-                        // This will make the correction flash for a short while as a visual clue
-                        // to the user that auto-correction happened.
-                        InputConnectionCompatUtils.commitCorrection(ic,
-                                mLastSelectionEnd - typedWord.length(), typedWord, autoCorrection);
-                    }
-                }
+                commitCurrentAutoCorrection(primaryCode, ic);
             } else {
                 commitTyped(ic);
             }
@@ -1873,7 +1859,8 @@
         setSuggestionStripShown(isSuggestionsStripVisible());
     }
 
-    private boolean pickDefaultSuggestion(int separatorCode) {
+    private void commitCurrentAutoCorrection(final int separatorCodePoint,
+            final InputConnection ic) {
         // Complete any pending suggestions query first
         if (mHandler.hasPendingUpdateSuggestions()) {
             mHandler.cancelUpdateSuggestions();
@@ -1882,7 +1869,11 @@
         final CharSequence autoCorrection = mWordComposer.getAutoCorrectionOrNull();
         if (autoCorrection != null) {
             final String typedWord = mWordComposer.getTypedWord();
-            Utils.Stats.onAutoCorrection(typedWord, autoCorrection.toString(), separatorCode);
+            if (TextUtils.isEmpty(typedWord)) {
+                throw new RuntimeException("We have an auto-correction but the typed word "
+                        + "is empty? Impossible! I must commit suicide.");
+            }
+            Utils.Stats.onAutoCorrection(typedWord, autoCorrection.toString(), separatorCodePoint);
             mExpectingUpdateSelection = true;
             commitBestWord(autoCorrection);
             if (!autoCorrection.equals(typedWord)) {
@@ -1891,9 +1882,13 @@
             // Add the word to the user unigram dictionary if it's not a known word
             addToUserUnigramAndBigramDictionaries(autoCorrection,
                     UserUnigramDictionary.FREQUENCY_FOR_TYPED);
-            return true;
+            if (!typedWord.equals(autoCorrection) && null != ic) {
+                // This will make the correction flash for a short while as a visual clue
+                // to the user that auto-correction happened.
+                InputConnectionCompatUtils.commitCorrection(ic,
+                        mLastSelectionEnd - typedWord.length(), typedWord, autoCorrection);
+            }
         }
-        return false;
     }
 
     @Override
diff --git a/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java b/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java
index 406c97f..7b4c6a9 100644
--- a/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java
+++ b/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java
@@ -21,7 +21,6 @@
 import android.test.AndroidTestCase;
 import android.text.TextUtils;
 import android.util.DisplayMetrics;
-import android.view.inputmethod.EditorInfo;
 
 import com.android.inputmethod.keyboard.KeyboardId;
 
@@ -52,7 +51,7 @@
         }
         return new KeyboardId(com.android.inputmethod.latin.R.xml.kbd_qwerty,
                 KeyboardId.ELEMENT_ALPHABET, locale, orientation, width, KeyboardId.MODE_TEXT,
-                new EditorInfo(), false, false, false, false);
+                null, false, false, false, false);
     }
 
     protected InputStream openTestRawResource(int resIdInTest) {