Remove enableShiftLock attribute from KeyboardId

This change is a follow up of Ia72de236.

Bug: 4311428
Change-Id: I0ad0f1fb93545ceb40df8dffe377240b020c2602
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java
index 0ee8d71..68868f1 100644
--- a/java/src/com/android/inputmethod/keyboard/Key.java
+++ b/java/src/com/android/inputmethod/keyboard/Key.java
@@ -325,6 +325,13 @@
                     keyAttr, R.styleable.Keyboard_Key_keyIcon,
                     KeyboardIconsSet.ICON_UNDEFINED));
             Keyboard.setDefaultBounds(mIcon);
+            final int shiftedIconId = style.getInt(keyAttr, R.styleable.Keyboard_Key_keyIconShifted,
+                    KeyboardIconsSet.ICON_UNDEFINED);
+            if (shiftedIconId != KeyboardIconsSet.ICON_UNDEFINED) {
+                final Drawable shiftedIcon = iconsSet.getIcon(shiftedIconId);
+                Keyboard.setDefaultBounds(shiftedIcon);
+                mKeyboard.addShiftedIcon(this, shiftedIcon);
+            }
             mHintLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyHintLabel);
 
             mLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyLabel);
@@ -342,12 +349,9 @@
             } else {
                 mCode = Keyboard.CODE_DUMMY;
             }
-
-            final Drawable shiftedIcon = iconsSet.getIcon(style.getInt(
-                    keyAttr, R.styleable.Keyboard_Key_keyIconShifted,
-                    KeyboardIconsSet.ICON_UNDEFINED));
-            if (shiftedIcon != null)
-                mKeyboard.getShiftedIcons().put(this, shiftedIcon);
+            if (mCode == Keyboard.CODE_SHIFT) {
+                mKeyboard.addShiftKey(this);
+            }
         } finally {
             keyAttr.recycle();
         }
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index 19847c5..f160f65 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -34,7 +34,6 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 
 /**
  * Loads an XML description of a keyboard and stores the attributes of the keys. A keyboard
@@ -116,8 +115,8 @@
     /** List of shift keys in this keyboard and its icons and state */
     private final List<Key> mShiftKeys = new ArrayList<Key>();
     private final HashMap<Key, Drawable> mShiftedIcons = new HashMap<Key, Drawable>();
-    private final HashMap<Key, Drawable> mNormalShiftIcons = new HashMap<Key, Drawable>();
-    private final HashSet<Key> mShiftLockEnabled = new HashSet<Key>();
+    private final HashMap<Key, Drawable> mUnshiftedIcons = new HashMap<Key, Drawable>();
+    private final HashSet<Key> mShiftLockKeys = new HashSet<Key>();
     private final KeyboardShiftState mShiftState = new KeyboardShiftState();
 
     /** Total height of the keyboard, including the padding and keys */
@@ -284,30 +283,35 @@
         mMaxPopupColumn = column;
     }
 
-    public List<Key> getShiftKeys() {
-        return mShiftKeys;
-    }
-
-    public Map<Key, Drawable> getShiftedIcons() {
-        return mShiftedIcons;
-    }
-
-    public void enableShiftLock() {
-        for (final Key key : getShiftKeys()) {
-            mShiftLockEnabled.add(key);
-            mNormalShiftIcons.put(key, key.getIcon());
+    public void addShiftKey(Key key) {
+        if (key == null) return;
+        mShiftKeys.add(key);
+        if (key.mSticky) {
+            mShiftLockKeys.add(key);
         }
     }
 
-    public boolean isShiftLockEnabled(Key key) {
-        return mShiftLockEnabled.contains(key);
+    public void addShiftedIcon(Key key, Drawable icon) {
+        if (key == null) return;
+        mUnshiftedIcons.put(key, key.getIcon());
+        mShiftedIcons.put(key, icon);
+    }
+
+    public boolean hasShiftLockKey() {
+        return !mShiftLockKeys.isEmpty();
     }
 
     public boolean setShiftLocked(boolean newShiftLockState) {
-        final Map<Key, Drawable> shiftedIcons = getShiftedIcons();
-        for (final Key key : getShiftKeys()) {
+        for (final Key key : mShiftLockKeys) {
+            // To represent "shift locked" state. The highlight is handled by background image that
+            // might be a StateListDrawable.
             key.setHighlightOn(newShiftLockState);
-            key.setIcon(newShiftLockState ? shiftedIcons.get(key) : mNormalShiftIcons.get(key));
+            // To represent "shifted" state. The key might have a shifted icon.
+            if (newShiftLockState && mShiftedIcons.containsKey(key)) {
+                key.setIcon(mShiftedIcons.get(key));
+            } else {
+                key.setIcon(mUnshiftedIcons.get(key));
+            }
         }
         mShiftState.setShiftLocked(newShiftLockState);
         return true;
@@ -318,12 +322,11 @@
     }
 
     public boolean setShifted(boolean newShiftState) {
-        final Map<Key, Drawable> shiftedIcons = getShiftedIcons();
-        for (final Key key : getShiftKeys()) {
+        for (final Key key : mShiftKeys) {
             if (!newShiftState && !mShiftState.isShiftLocked()) {
-                key.setIcon(mNormalShiftIcons.get(key));
+                key.setIcon(mUnshiftedIcons.get(key));
             } else if (newShiftState && !mShiftState.isShiftedOrShiftLocked()) {
-                key.setIcon(shiftedIcons.get(key));
+                key.setIcon(mShiftedIcons.get(key));
             }
         }
         return mShiftState.setShifted(newShiftState);
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardId.java b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
index 3f30165..3ed0dbe 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardId.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
@@ -58,7 +58,6 @@
     public final boolean mVoiceKeyEnabled;
     public final boolean mHasVoiceKey;
     public final int mImeAction;
-    public final boolean mEnableShiftLock;
 
     public final String mXmlName;
     public final EditorInfo mAttribute;
@@ -67,8 +66,7 @@
 
     public KeyboardId(String xmlName, int xmlId, Locale locale, int orientation, int width,
             int mode, EditorInfo attribute, boolean hasSettingsKey, int f2KeyMode,
-            boolean clobberSettingsKey, boolean voiceKeyEnabled, boolean hasVoiceKey,
-            boolean enableShiftLock) {
+            boolean clobberSettingsKey, boolean voiceKeyEnabled, boolean hasVoiceKey) {
         final int inputType = (attribute != null) ? attribute.inputType : 0;
         final int imeOptions = (attribute != null) ? attribute.imeOptions : 0;
         this.mLocale = locale;
@@ -91,7 +89,6 @@
         // {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION}.
         this.mImeAction = imeOptions & (
                 EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
-        this.mEnableShiftLock = enableShiftLock;
 
         this.mXmlName = xmlName;
         this.mAttribute = attribute;
@@ -110,21 +107,19 @@
                 voiceKeyEnabled,
                 hasVoiceKey,
                 mImeAction,
-                enableShiftLock,
         });
     }
 
     public KeyboardId cloneAsMiniKeyboard() {
         return new KeyboardId("mini popup keyboard", MINI_KEYBOARD_ID_MARKER, mLocale, mOrientation,
-                mWidth, mMode, mAttribute, false, F2KEY_MODE_NONE, false, false, false, false);
+                mWidth, mMode, mAttribute, false, F2KEY_MODE_NONE, false, false, false);
     }
 
     public KeyboardId cloneWithNewGeometry(int orientation, int width) {
         if (mWidth == width)
             return this;
         return new KeyboardId(mXmlName, mXmlId, mLocale, orientation, width, mMode, mAttribute,
-                mHasSettingsKey, mF2KeyMode, mClobberSettingsKey, mVoiceKeyEnabled, mHasVoiceKey,
-                mEnableShiftLock);
+                mHasSettingsKey, mF2KeyMode, mClobberSettingsKey, mVoiceKeyEnabled, mHasVoiceKey);
     }
 
     public int getXmlId() {
@@ -173,8 +168,7 @@
             && other.mClobberSettingsKey == this.mClobberSettingsKey
             && other.mVoiceKeyEnabled == this.mVoiceKeyEnabled
             && other.mHasVoiceKey == this.mHasVoiceKey
-            && other.mImeAction == this.mImeAction
-            && other.mEnableShiftLock == this.mEnableShiftLock;
+            && other.mImeAction == this.mImeAction;
     }
 
     @Override
@@ -184,7 +178,7 @@
 
     @Override
     public String toString() {
-        return String.format("[%s.xml %s %s%d %s %s %s%s%s%s%s%s%s%s]",
+        return String.format("[%s.xml %s %s%d %s %s %s%s%s%s%s%s%s]",
                 mXmlName,
                 mLocale,
                 (mOrientation == 1 ? "port" : "land"), mWidth,
@@ -196,8 +190,7 @@
                 (mPasswordInput ? " passwordInput" : ""),
                 (mHasSettingsKey ? " hasSettingsKey" : ""),
                 (mVoiceKeyEnabled ? " voiceKeyEnabled" : ""),
-                (mHasVoiceKey ? " hasVoiceKey" : ""),
-                (mEnableShiftLock ? " enableShiftLock" : "")
+                (mHasVoiceKey ? " hasVoiceKey" : "")
         );
     }
 
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index 552a3cd..e7e707c 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -204,11 +204,6 @@
                     mSubtypeSwitcher.getInputLocale());
 
             keyboard = new LatinKeyboard(mThemeContext, id, id.mWidth);
-
-            if (id.mEnableShiftLock) {
-                keyboard.enableShiftLock();
-            }
-
             mKeyboardCache.put(id, new SoftReference<LatinKeyboard>(keyboard));
             if (DEBUG_CACHE)
                 Log.d(TAG, "keyboard cache size=" + mKeyboardCache.size() + ": "
@@ -241,16 +236,13 @@
         final int mode = Utils.getKeyboardMode(attribute);
         final boolean hasVoiceKey = voiceKeyEnabled && (isSymbols != voiceKeyOnMain);
         final int xmlId;
-        final boolean enableShiftLock;
 
         switch (mode) {
         case KeyboardId.MODE_PHONE:
             xmlId = (isSymbols && isShift) ? R.xml.kbd_phone_shift : R.xml.kbd_phone;
-            enableShiftLock = true;
             break;
         case KeyboardId.MODE_NUMBER:
             xmlId = R.xml.kbd_number;
-            enableShiftLock = false;
             break;
         default:
             if (isSymbols) {
@@ -258,7 +250,6 @@
             } else {
                 xmlId = R.xml.kbd_qwerty;
             }
-            enableShiftLock = true;
             break;
         }
 
@@ -275,7 +266,7 @@
         return new KeyboardId(
                 res.getResourceEntryName(xmlId), xmlId, locale, orientation, mWindowWidth,
                 mode, attribute, hasSettingsKey, f2KeyMode, clobberSettingsKey, voiceKeyEnabled,
-                hasVoiceKey, enableShiftLock);
+                hasVoiceKey);
     }
 
     public int getKeyboardMode() {
@@ -571,7 +562,7 @@
             // Symbol keyboard may have an ALT key that has a caps lock style indicator (a.k.a.
             // sticky shift key). To show or dismiss the indicator, we need to call setShiftLocked()
             // that takes care of the current keyboard having such ALT key or not.
-            keyboard.setShiftLocked(hasStickyShiftKey(keyboard));
+            keyboard.setShiftLocked(keyboard.hasShiftLockKey());
         } else {
             keyboard = getKeyboard(mSymbolsKeyboardId);
             // Symbol keyboard has an ALT key that has a caps lock style indicator. To disable the
@@ -581,14 +572,6 @@
         setKeyboard(keyboard);
     }
 
-    private static boolean hasStickyShiftKey(Keyboard keyboard) {
-        for (final Key shiftKey : keyboard.getShiftKeys()) {
-            if (shiftKey.mSticky)
-                return true;
-        }
-        return false;
-    }
-
     public boolean isInMomentarySwitchState() {
         return mSwitchState == SWITCH_STATE_MOMENTARY_ALPHA_AND_SYMBOL
                 || mSwitchState == SWITCH_STATE_MOMENTARY_SYMBOL_AND_MORE;
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParser.java
index 8eae2bb..6a83b3d 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardParser.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardParser.java
@@ -338,8 +338,6 @@
                     Arrays.toString(key.mPopupCharacters)));
             checkEndTag(TAG_KEY, parser);
             keys.add(key);
-            if (key.mCode == Keyboard.CODE_SHIFT)
-                mKeyboard.getShiftKeys().add(key);
             endKey(key);
         }
     }
diff --git a/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java b/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java
index 8aadee4..7d61d00 100644
--- a/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java
+++ b/tests/src/com/android/inputmethod/latin/SuggestTestsBase.java
@@ -42,7 +42,7 @@
         return new KeyboardId(locale.toString() + " keyboard",
                 com.android.inputmethod.latin.R.xml.kbd_qwerty, locale,
                 Configuration.ORIENTATION_LANDSCAPE, displayWidth, KeyboardId.MODE_TEXT,
-                new EditorInfo(), false, KeyboardId.F2KEY_MODE_NONE, false, false, false, false);
+                new EditorInfo(), false, KeyboardId.F2KEY_MODE_NONE, false, false, false);
     }
 
     protected InputStream openTestRawResource(int resIdInTest) {