Fix label of switch to symbols key when symbols has shortcut key

This change fixes the labels of switch to symbols key and switch back
from more symbols key when symbols layout has shortcut key.

Change-Id: I3fbbef2d929486d0f0542f4d89cb086d9f5bad82
diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml
index 3eb27e7..2f1a209 100644
--- a/java/res/values/attrs.xml
+++ b/java/res/values/attrs.xml
@@ -389,6 +389,7 @@
         <attr name="passwordInput" format="boolean" />
         <attr name="clobberSettingsKey" format="boolean" />
         <attr name="shortcutKeyEnabled" format="boolean" />
+        <attr name="shortcutKeyOnSymbols" format="boolean" />
         <attr name="hasShortcutKey" format="boolean" />
         <attr name="languageSwitchKeyEnabled" format="boolean" />
         <attr name="isMultiLine" format="boolean" />
diff --git a/java/res/xml-sw600dp/key_styles_common.xml b/java/res/xml-sw600dp/key_styles_common.xml
index bf2e76a..6b06ce7 100644
--- a/java/res/xml-sw600dp/key_styles_common.xml
+++ b/java/res/xml-sw600dp/key_styles_common.xml
@@ -154,31 +154,30 @@
         </default>
     </switch>
     <key-style
-        latin:styleName="toSymbolKeyStyle"
-        latin:code="!code/key_switch_alpha_symbol"
-        latin:keyLabel="!text/label_to_symbol_key"
+        latin:styleName="baseForLayoutSwitchKeyStyle"
         latin:keyLabelFlags="preserveCase"
         latin:keyActionFlags="noKeyPreview"
         latin:backgroundType="functional" />
     <key-style
+        latin:styleName="toSymbolKeyStyle"
+        latin:code="!code/key_switch_alpha_symbol"
+        latin:keyLabel="!text/label_to_symbol_key"
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
+    <key-style
         latin:styleName="toAlphaKeyStyle"
         latin:code="!code/key_switch_alpha_symbol"
         latin:keyLabel="!text/label_to_alpha_key"
-        latin:keyLabelFlags="preserveCase"
-        latin:keyActionFlags="noKeyPreview"
-        latin:backgroundType="functional" />
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
     <key-style
         latin:styleName="toMoreSymbolKeyStyle"
         latin:code="!code/key_shift"
         latin:keyLabel="!text/label_to_more_symbol_for_tablet_key"
-        latin:keyActionFlags="noKeyPreview"
-        latin:backgroundType="functional" />
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
     <key-style
         latin:styleName="backFromMoreSymbolKeyStyle"
         latin:code="!code/key_shift"
         latin:keyLabel="!text/label_to_symbol_key"
-        latin:keyActionFlags="noKeyPreview"
-        latin:backgroundType="functional" />
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
     <key-style
         latin:styleName="comKeyStyle"
         latin:keyLabel="!text/keylabel_for_popular_domain"
diff --git a/java/res/xml-sw768dp/key_styles_common.xml b/java/res/xml-sw768dp/key_styles_common.xml
index 537e768..4d10f5b 100644
--- a/java/res/xml-sw768dp/key_styles_common.xml
+++ b/java/res/xml-sw768dp/key_styles_common.xml
@@ -144,33 +144,30 @@
         </default>
     </switch>
     <key-style
-        latin:styleName="toSymbolKeyStyle"
-        latin:code="!code/key_switch_alpha_symbol"
-        latin:keyLabel="!text/label_to_symbol_key"
+        latin:styleName="baseForLayoutSwitchKeyStyle"
         latin:keyLabelFlags="fontNormal|preserveCase"
         latin:keyActionFlags="noKeyPreview"
         latin:backgroundType="functional" />
     <key-style
+        latin:styleName="toSymbolKeyStyle"
+        latin:code="!code/key_switch_alpha_symbol"
+        latin:keyLabel="!text/label_to_symbol_key"
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
+    <key-style
         latin:styleName="toAlphaKeyStyle"
         latin:code="!code/key_switch_alpha_symbol"
         latin:keyLabel="!text/label_to_alpha_key"
-        latin:keyLabelFlags="fontNormal|preserveCase"
-        latin:keyActionFlags="noKeyPreview"
-        latin:backgroundType="functional" />
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
     <key-style
         latin:styleName="toMoreSymbolKeyStyle"
         latin:code="!code/key_shift"
         latin:keyLabel="!text/label_to_more_symbol_for_tablet_key"
-        latin:keyLabelFlags="fontNormal"
-        latin:keyActionFlags="noKeyPreview"
-        latin:backgroundType="functional" />
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
     <key-style
         latin:styleName="backFromMoreSymbolKeyStyle"
         latin:code="!code/key_shift"
         latin:keyLabel="!text/label_to_symbol_key"
-        latin:keyLabelFlags="fontNormal"
-        latin:keyActionFlags="noKeyPreview"
-        latin:backgroundType="functional" />
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
     <key-style
         latin:styleName="comKeyStyle"
         latin:keyLabel="!text/keylabel_for_popular_domain"
diff --git a/java/res/xml/key_styles_common.xml b/java/res/xml/key_styles_common.xml
index 162119d..91ebac1 100644
--- a/java/res/xml/key_styles_common.xml
+++ b/java/res/xml/key_styles_common.xml
@@ -134,52 +134,50 @@
         latin:code="!code/key_tab"
         latin:keyIcon="!icon/tab_key"
         latin:keyIconPreview="!icon/tab_key_preview" />
+    <key-style
+        latin:styleName="baseForLayoutSwitchKeyStyle"
+        latin:keyLabelFlags="preserveCase"
+        latin:keyActionFlags="noKeyPreview"
+        latin:backgroundType="functional" />
     <switch>
         <!-- When this qwerty keyboard has no shortcut keys but shortcut key is enabled, then symbol
              keyboard will have a shortcut key. That means we should use label_to_symbol_key label
              and shortcut_for_label icon. -->
         <case
-            latin:shortcutKeyEnabled="true"
-            latin:hasShortcutKey="false"
+            latin:shortcutKeyOnSymbols="true"
         >
             <key-style
-                latin:styleName="toSymbolKeyStyle"
-                latin:code="!code/key_switch_alpha_symbol"
+                latin:styleName="baseForToSymbolKeyStyle"
                 latin:keyIcon="!icon/shortcut_for_label"
                 latin:keyLabel="!text/label_to_symbol_with_microphone_key"
                 latin:keyLabelFlags="withIconRight|preserveCase"
-                latin:keyActionFlags="noKeyPreview"
-                latin:backgroundType="functional" />
+                latin:parentStyle="baseForLayoutSwitchKeyStyle" />
         </case>
         <default>
             <key-style
-                latin:styleName="toSymbolKeyStyle"
-                latin:code="!code/key_switch_alpha_symbol"
+                latin:styleName="baseForToSymbolKeyStyle"
                 latin:keyLabel="!text/label_to_symbol_key"
-                latin:keyLabelFlags="preserveCase"
-                latin:keyActionFlags="noKeyPreview"
-                latin:backgroundType="functional" />
+                latin:parentStyle="baseForLayoutSwitchKeyStyle" />
         </default>
     </switch>
     <key-style
+        latin:styleName="toSymbolKeyStyle"
+        latin:code="!code/key_switch_alpha_symbol"
+        latin:parentStyle="baseForToSymbolKeyStyle" />
+    <key-style
         latin:styleName="toAlphaKeyStyle"
         latin:code="!code/key_switch_alpha_symbol"
         latin:keyLabel="!text/label_to_alpha_key"
-        latin:keyLabelFlags="preserveCase"
-        latin:keyActionFlags="noKeyPreview"
-        latin:backgroundType="functional" />
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
     <key-style
         latin:styleName="toMoreSymbolKeyStyle"
         latin:code="!code/key_shift"
         latin:keyLabel="!text/label_to_more_symbol_key"
-        latin:keyActionFlags="noKeyPreview"
-        latin:backgroundType="functional" />
+        latin:parentStyle="baseForLayoutSwitchKeyStyle" />
     <key-style
         latin:styleName="backFromMoreSymbolKeyStyle"
         latin:code="!code/key_shift"
-        latin:keyLabel="!text/label_to_symbol_key"
-        latin:keyActionFlags="noKeyPreview"
-        latin:backgroundType="functional" />
+        latin:parentStyle="baseForToSymbolKeyStyle" />
     <key-style
         latin:styleName="punctuationKeyStyle"
         latin:keyLabel="."
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardId.java b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
index 5e8a8f6..b413615 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardId.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
@@ -71,34 +71,39 @@
     private final EditorInfo mEditorInfo;
     public final boolean mClobberSettingsKey;
     public final boolean mShortcutKeyEnabled;
-    public final boolean mHasShortcutKey;
+    public final boolean mShortcutKeyOnSymbols;
     public final boolean mLanguageSwitchKeyEnabled;
     public final String mCustomActionLabel;
+    public final boolean mHasShortcutKey;
 
     private final int mHashCode;
 
-    public KeyboardId(int elementId, InputMethodSubtype subtype, int deviceFormFactor,
-            int orientation, int width, int mode, EditorInfo editorInfo, boolean clobberSettingsKey,
-            boolean shortcutKeyEnabled, boolean hasShortcutKey, boolean languageSwitchKeyEnabled) {
-        mSubtype = subtype;
-        mLocale = SubtypeLocale.getSubtypeLocale(subtype);
-        mDeviceFormFactor = deviceFormFactor;
-        mOrientation = orientation;
-        mWidth = width;
-        mMode = mode;
+    public KeyboardId(final int elementId, final KeyboardLayoutSet.Params params) {
+        mSubtype = params.mSubtype;
+        mLocale = SubtypeLocale.getSubtypeLocale(mSubtype);
+        mDeviceFormFactor = params.mDeviceFormFactor;
+        mOrientation = params.mOrientation;
+        mWidth = params.mWidth;
+        mMode = params.mMode;
         mElementId = elementId;
-        mEditorInfo = editorInfo;
-        mClobberSettingsKey = clobberSettingsKey;
-        mShortcutKeyEnabled = shortcutKeyEnabled;
-        mHasShortcutKey = hasShortcutKey;
-        mLanguageSwitchKeyEnabled = languageSwitchKeyEnabled;
-        mCustomActionLabel = (editorInfo.actionLabel != null)
-                ? editorInfo.actionLabel.toString() : null;
+        mEditorInfo = params.mEditorInfo;
+        mClobberSettingsKey = params.mNoSettingsKey;
+        mShortcutKeyEnabled = params.mVoiceKeyEnabled;
+        mShortcutKeyOnSymbols = mShortcutKeyEnabled && !params.mVoiceKeyOnMain;
+        mLanguageSwitchKeyEnabled = params.mLanguageSwitchKeyEnabled;
+        mCustomActionLabel = (mEditorInfo.actionLabel != null)
+                ? mEditorInfo.actionLabel.toString() : null;
+        final boolean alphabetMayHaveShortcutKey = isAlphabetKeyboard(elementId)
+                && !mShortcutKeyOnSymbols;
+        final boolean symbolsMayHaveShortcutKey = (elementId == KeyboardId.ELEMENT_SYMBOLS)
+                && mShortcutKeyOnSymbols;
+        mHasShortcutKey = mShortcutKeyEnabled
+                && (alphabetMayHaveShortcutKey || symbolsMayHaveShortcutKey);
 
         mHashCode = computeHashCode(this);
     }
 
-    private static int computeHashCode(KeyboardId id) {
+    private static int computeHashCode(final KeyboardId id) {
         return Arrays.hashCode(new Object[] {
                 id.mDeviceFormFactor,
                 id.mOrientation,
@@ -108,7 +113,7 @@
                 id.passwordInput(),
                 id.mClobberSettingsKey,
                 id.mShortcutKeyEnabled,
-                id.mHasShortcutKey,
+                id.mShortcutKeyOnSymbols,
                 id.mLanguageSwitchKeyEnabled,
                 id.isMultiLine(),
                 id.imeAction(),
@@ -119,7 +124,7 @@
         });
     }
 
-    private boolean equals(KeyboardId other) {
+    private boolean equals(final KeyboardId other) {
         if (other == this)
             return true;
         return other.mDeviceFormFactor == mDeviceFormFactor
@@ -130,7 +135,7 @@
                 && other.passwordInput() == passwordInput()
                 && other.mClobberSettingsKey == mClobberSettingsKey
                 && other.mShortcutKeyEnabled == mShortcutKeyEnabled
-                && other.mHasShortcutKey == mHasShortcutKey
+                && other.mShortcutKeyOnSymbols == mShortcutKeyOnSymbols
                 && other.mLanguageSwitchKeyEnabled == mLanguageSwitchKeyEnabled
                 && other.isMultiLine() == isMultiLine()
                 && other.imeAction() == imeAction()
@@ -140,8 +145,12 @@
                 && other.mSubtype.equals(mSubtype);
     }
 
+    private static boolean isAlphabetKeyboard(final int elementId) {
+        return elementId < ELEMENT_SYMBOLS;
+    }
+
     public boolean isAlphabetKeyboard() {
-        return mElementId < ELEMENT_SYMBOLS;
+        return isAlphabetKeyboard(mElementId);
     }
 
     public boolean navigateNext() {
@@ -181,7 +190,7 @@
     }
 
     @Override
-    public boolean equals(Object other) {
+    public boolean equals(final Object other) {
         return other instanceof KeyboardId && equals((KeyboardId) other);
     }
 
@@ -192,7 +201,7 @@
 
     @Override
     public String toString() {
-        return String.format("[%s %s:%s %s-%s:%d %s %s %s%s%s%s%s%s%s%s]",
+        return String.format("[%s %s:%s %s-%s:%d %s %s %s%s%s%s%s%s%s%s%s]",
                 elementIdToName(mElementId),
                 mLocale,
                 mSubtype.getExtraValueOf(KEYBOARD_LAYOUT_SET),
@@ -204,13 +213,14 @@
                 (mClobberSettingsKey ? " clobberSettingsKey" : ""),
                 (passwordInput() ? " passwordInput" : ""),
                 (mShortcutKeyEnabled ? " shortcutKeyEnabled" : ""),
+                (mShortcutKeyOnSymbols ? " shortcutKeyOnSymbols" : ""),
                 (mHasShortcutKey ? " hasShortcutKey" : ""),
                 (mLanguageSwitchKeyEnabled ? " languageSwitchKeyEnabled" : ""),
                 (isMultiLine() ? "isMultiLine" : "")
         );
     }
 
-    public static boolean equivalentEditorInfoForKeyboard(EditorInfo a, EditorInfo b) {
+    public static boolean equivalentEditorInfoForKeyboard(final EditorInfo a, final EditorInfo b) {
         if (a == null && b == null) return true;
         if (a == null || b == null) return false;
         return a.inputType == b.inputType
@@ -218,7 +228,7 @@
                 && TextUtils.equals(a.privateImeOptions, b.privateImeOptions);
     }
 
-    public static String elementIdToName(int elementId) {
+    public static String elementIdToName(final int elementId) {
         switch (elementId) {
         case ELEMENT_ALPHABET: return "alphabet";
         case ELEMENT_ALPHABET_MANUAL_SHIFTED: return "alphabetManualShifted";
@@ -234,8 +244,8 @@
         }
     }
 
-    public static String deviceFormFactor(int devoceFormFactor) {
-        switch (devoceFormFactor) {
+    public static String deviceFormFactor(final int deviceFormFactor) {
+        switch (deviceFormFactor) {
         case FORM_FACTOR_PHONE: return "phone";
         case FORM_FACTOR_TABLET7: return "tablet7";
         case FORM_FACTOR_TABLET10: return "tablet10";
@@ -243,7 +253,7 @@
         }
     }
 
-    public static String modeName(int mode) {
+    public static String modeName(final int mode) {
         switch (mode) {
         case MODE_TEXT: return "text";
         case MODE_URL: return "url";
@@ -258,7 +268,7 @@
         }
     }
 
-    public static String actionName(int actionId) {
+    public static String actionName(final int actionId) {
         return (actionId == IME_ACTION_CUSTOM_LABEL) ? "actionCustomLabel"
                 : EditorInfoCompatUtils.imeActionName(actionId);
     }
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java b/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
index 4d5d7e1..2950475 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
@@ -78,6 +78,7 @@
             CollectionUtils.newHashMap();
     private static final KeysCache sKeysCache = new KeysCache();
 
+    @SuppressWarnings("serial")
     public static final class KeyboardLayoutSetException extends RuntimeException {
         public final KeyboardId mKeyboardId;
 
@@ -93,7 +94,7 @@
         public ElementParams() {}
     }
 
-    private static final class Params {
+    public static final class Params {
         String mKeyboardLayoutSetName;
         int mMode;
         EditorInfo mEditorInfo;
@@ -109,7 +110,6 @@
         // Sparse array of KeyboardLayoutSet element parameters indexed by element's id.
         final SparseArray<ElementParams> mKeyboardLayoutSetElementIdToParamsMap =
                 CollectionUtils.newSparseArray();
-        public Params() {}
     }
 
     public static void clearKeyboardCache() {
@@ -149,7 +149,11 @@
             elementParams = mParams.mKeyboardLayoutSetElementIdToParamsMap.get(
                     KeyboardId.ELEMENT_ALPHABET);
         }
-        final KeyboardId id = getKeyboardId(keyboardLayoutSetElementId);
+        // Note: The keyboard for each shift state, and mode are represented as an elementName
+        // attribute in a keyboard_layout_set XML file.  Also each keyboard layout XML resource is
+        // specified as an elementKeyboard attribute in the file.
+        // The KeyboardId is an internal key for a Keyboard object.
+        final KeyboardId id = new KeyboardId(keyboardLayoutSetElementId, mParams);
         try {
             return getKeyboard(elementParams, id);
         } catch (RuntimeException e) {
@@ -187,22 +191,6 @@
         return keyboard;
     }
 
-    // Note: The keyboard for each locale, shift state, and mode are represented as
-    // KeyboardLayoutSet element id that is a key in keyboard_set.xml.  Also that file specifies
-    // which XML layout should be used for each keyboard.  The KeyboardId is an internal key for
-    // Keyboard object.
-    private KeyboardId getKeyboardId(final int keyboardLayoutSetElementId) {
-        final Params params = mParams;
-        final boolean isSymbols = (keyboardLayoutSetElementId == KeyboardId.ELEMENT_SYMBOLS
-                || keyboardLayoutSetElementId == KeyboardId.ELEMENT_SYMBOLS_SHIFTED);
-        final boolean hasShortcutKey = params.mVoiceKeyEnabled
-                && (isSymbols != params.mVoiceKeyOnMain);
-        return new KeyboardId(keyboardLayoutSetElementId, params.mSubtype, params.mDeviceFormFactor,
-                params.mOrientation, params.mWidth, params.mMode, params.mEditorInfo,
-                params.mNoSettingsKey, params.mVoiceKeyEnabled, hasShortcutKey,
-                params.mLanguageSwitchKeyEnabled);
-    }
-
     public static final class Builder {
         private final Context mContext;
         private final String mPackageName;
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
index 3634268..da418f4 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardBuilder.java
@@ -622,6 +622,8 @@
                     R.styleable.Keyboard_Case_clobberSettingsKey, id.mClobberSettingsKey);
             final boolean shortcutKeyEnabledMatched = matchBoolean(a,
                     R.styleable.Keyboard_Case_shortcutKeyEnabled, id.mShortcutKeyEnabled);
+            final boolean shortcutKeyOnSymbolsMatched = matchBoolean(a,
+                    R.styleable.Keyboard_Case_shortcutKeyOnSymbols, id.mShortcutKeyOnSymbols);
             final boolean hasShortcutKeyMatched = matchBoolean(a,
                     R.styleable.Keyboard_Case_hasShortcutKey, id.mHasShortcutKey);
             final boolean languageSwitchKeyEnabledMatched = matchBoolean(a,
@@ -640,12 +642,12 @@
             final boolean selected = keyboardLayoutSetElementMatched && modeMatched
                     && navigateNextMatched && navigatePreviousMatched && passwordInputMatched
                     && clobberSettingsKeyMatched && shortcutKeyEnabledMatched
-                    && hasShortcutKeyMatched && languageSwitchKeyEnabledMatched
-                    && isMultiLineMatched && imeActionMatched && localeCodeMatched
-                    && languageCodeMatched && countryCodeMatched;
+                    && shortcutKeyOnSymbolsMatched && hasShortcutKeyMatched
+                    && languageSwitchKeyEnabledMatched && isMultiLineMatched && imeActionMatched
+                    && localeCodeMatched && languageCodeMatched && countryCodeMatched;
 
             if (DEBUG) {
-                startTag("<%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s>%s", TAG_CASE,
+                startTag("<%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s>%s", TAG_CASE,
                         textAttr(a.getString(
                                 R.styleable.Keyboard_Case_keyboardLayoutSetElement),
                                 "keyboardLayoutSetElement"),
@@ -662,6 +664,8 @@
                                 "passwordInput"),
                         booleanAttr(a, R.styleable.Keyboard_Case_shortcutKeyEnabled,
                                 "shortcutKeyEnabled"),
+                        booleanAttr(a, R.styleable.Keyboard_Case_shortcutKeyOnSymbols,
+                                "shortcutKeyOnSymbols"),
                         booleanAttr(a, R.styleable.Keyboard_Case_hasShortcutKey,
                                 "hasShortcutKey"),
                         booleanAttr(a, R.styleable.Keyboard_Case_languageSwitchKeyEnabled,