Merge "Remove enableShiftLock attribute from KeyboardId"
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index f160f65..8840c79 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -76,6 +76,8 @@
     public static final int CODE_CLOSING_SQUARE_BRACKET = ']';
     public static final int CODE_CLOSING_CURLY_BRACKET = '}';
     public static final int CODE_CLOSING_ANGLE_BRACKET = '>';
+    public static final int CODE_DIGIT0 = '0';
+    public static final int CODE_PLUS = '+';
 
 
     /** Special keys code.  These should be aligned with values/keycodes.xml */
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardId.java b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
index 3ed0dbe..c3f4d0a 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardId.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
@@ -155,7 +155,7 @@
         return other instanceof KeyboardId && equals((KeyboardId) other);
     }
 
-    boolean equals(KeyboardId other) {
+    private boolean equals(KeyboardId other) {
         return other.mLocale.equals(this.mLocale)
             && other.mOrientation == this.mOrientation
             && other.mWidth == this.mWidth
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index e7e707c..0fcd8ae 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -161,24 +161,25 @@
         }
     }
 
+    public void onHideWindow() {
+        mWindowWidth = 0;
+        mIsAutoCorrectionActive = false;
+    }
+
     @SuppressWarnings("unused")
     public void onSizeChanged(int w, int h, int oldw, int oldh) {
+        // TODO: This hack should be removed when display metric returns a proper width.
+        // Until then, the behavior of KeyboardSwitcher is suboptimal on a device that has a
+        // vertical system navigation bar in landscape screen orientation, for instance.
         final int width = mInputMethodService.getWindow().getWindow().getDecorView().getWidth();
         // If the window width hasn't fixed yet or keyboard doesn't exist, nothing to do with.
         if (width == 0 || mCurrentId == null)
             return;
         // The window width is fixed.
         mWindowWidth = width;
-        // If this is the first time the {@link KeyboardView} has been shown, no need to reload
-        // keyboard.
-        if (oldw == 0 && oldh == 0)
-            return;
         // Reload keyboard with new width.
         final int orientation = mInputMethodService.getResources().getConfiguration().orientation;
         final KeyboardId newId = mCurrentId.cloneWithNewGeometry(orientation, width);
-        // If the new keyboard is the same as the current one, no need to reload it.
-        if (newId.equals(mCurrentId))
-            return;
         setKeyboard(getKeyboard(newId));
     }
 
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
index c404a5d..b78fd94 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
@@ -53,55 +53,55 @@
 
     @Override
     public void setKeyPreviewPopupEnabled(boolean previewEnabled, int delay) {
-        LatinKeyboard latinKeyboard = getLatinKeyboard();
-        if (latinKeyboard != null
-                && (latinKeyboard.isPhoneKeyboard() || latinKeyboard.isNumberKeyboard())) {
-            // Phone and number keyboard never shows popup preview (except language switch).
-            super.setKeyPreviewPopupEnabled(false, delay);
-        } else {
-            super.setKeyPreviewPopupEnabled(previewEnabled, delay);
+        final Keyboard keyboard = getKeyboard();
+        if (keyboard instanceof LatinKeyboard) {
+            final LatinKeyboard latinKeyboard = (LatinKeyboard)keyboard;
+            if (latinKeyboard.isPhoneKeyboard() || latinKeyboard.isNumberKeyboard()) {
+                // Phone and number keyboard never shows popup preview.
+                super.setKeyPreviewPopupEnabled(false, delay);
+                return;
+            }
         }
+        super.setKeyPreviewPopupEnabled(previewEnabled, delay);
     }
 
     @Override
     public void setKeyboard(Keyboard newKeyboard) {
         super.setKeyboard(newKeyboard);
         // One-seventh of the keyboard width seems like a reasonable threshold
-        mJumpThresholdSquare = newKeyboard.getMinWidth() / 7;
-        mJumpThresholdSquare *= mJumpThresholdSquare;
-    }
-
-    private LatinKeyboard getLatinKeyboard() {
-        Keyboard keyboard = getKeyboard();
-        if (keyboard instanceof LatinKeyboard) {
-            return (LatinKeyboard)keyboard;
-        } else {
-            return null;
-        }
+        final int jumpThreshold = newKeyboard.getMinWidth() / 7;
+        mJumpThresholdSquare = jumpThreshold * jumpThreshold;
     }
 
     public void setSpacebarTextFadeFactor(float fadeFactor, LatinKeyboard oldKeyboard) {
-        final LatinKeyboard currentKeyboard = getLatinKeyboard();
+        final Keyboard keyboard = getKeyboard();
         // We should not set text fade factor to the keyboard which does not display the language on
         // its spacebar.
-        if (currentKeyboard != null && currentKeyboard == oldKeyboard)
-            currentKeyboard.setSpacebarTextFadeFactor(fadeFactor, this);
+        if (keyboard instanceof LatinKeyboard && keyboard == oldKeyboard) {
+            ((LatinKeyboard)keyboard).setSpacebarTextFadeFactor(fadeFactor, this);
+        }
     }
 
     @Override
     protected boolean onLongPress(Key key, PointerTracker tracker) {
-        int primaryCode = key.mCode;
+        final int primaryCode = key.mCode;
+        final Keyboard keyboard = getKeyboard();
+        if (keyboard instanceof LatinKeyboard) {
+            final LatinKeyboard latinKeyboard = (LatinKeyboard) keyboard;
+            if (primaryCode == Keyboard.CODE_DIGIT0 && latinKeyboard.isPhoneKeyboard()) {
+                tracker.onLongPressed();
+                // Long pressing on 0 in phone number keypad gives you a '+'.
+                return invokeOnKey(Keyboard.CODE_PLUS);
+            }
+            if (primaryCode == Keyboard.CODE_SHIFT && latinKeyboard.isAlphaKeyboard()) {
+                tracker.onLongPressed();
+                return invokeOnKey(Keyboard.CODE_CAPSLOCK);
+            }
+        }
         if (primaryCode == Keyboard.CODE_SETTINGS || primaryCode == Keyboard.CODE_SPACE) {
             tracker.onLongPressed();
             // Both long pressing settings key and space key invoke IME switcher dialog.
             return invokeOnKey(Keyboard.CODE_SETTINGS_LONGPRESS);
-        } else if (primaryCode == '0' && getLatinKeyboard().isPhoneKeyboard()) {
-            tracker.onLongPressed();
-            // Long pressing on 0 in phone number keypad gives you a '+'.
-            return invokeOnKey('+');
-        } else if (primaryCode == Keyboard.CODE_SHIFT) {
-            tracker.onLongPressed();
-            return invokeOnKey(Keyboard.CODE_CAPSLOCK);
         } else {
             return super.onLongPress(key, tracker);
         }
@@ -194,7 +194,7 @@
 
     @Override
     public boolean onTouchEvent(MotionEvent me) {
-        if (getLatinKeyboard() == null) return true;
+        if (getKeyboard() == null) return true;
 
         // If there was a sudden jump, return without processing the actual motion event.
         if (handleSuddenJump(me)) {
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index f648357..b25754d 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -329,36 +329,28 @@
         return mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
     }
 
-    public boolean isSpaceKey(int keyIndex) {
-        Key key = getKey(keyIndex);
-        return key != null && key.mCode == Keyboard.CODE_SPACE;
-    }
-
     private void setReleasedKeyGraphics(int keyIndex) {
         mDrawingProxy.dismissKeyPreview(this);
         final Key key = getKey(keyIndex);
-        if (key != null) {
+        if (key != null && key.isEnabled()) {
             key.onReleased();
             mDrawingProxy.invalidateKey(key);
         }
     }
 
     private void setPressedKeyGraphics(int keyIndex) {
-        if (isKeyPreviewRequired(keyIndex)) {
-            mDrawingProxy.showKeyPreview(keyIndex, this);
-        }
         final Key key = getKey(keyIndex);
         if (key != null && key.isEnabled()) {
+            if (isKeyPreviewRequired(key)) {
+                mDrawingProxy.showKeyPreview(keyIndex, this);
+            }
             key.onPressed();
             mDrawingProxy.invalidateKey(key);
         }
     }
 
     // The modifier key, such as shift key, should not show its key preview.
-    private boolean isKeyPreviewRequired(int keyIndex) {
-        final Key key = getKey(keyIndex);
-        if (key == null || !key.isEnabled())
-            return false;
+    private static boolean isKeyPreviewRequired(Key key) {
         final int code = key.mCode;
         if (isModifierCode(code) || code == Keyboard.CODE_DELETE
                 || code == Keyboard.CODE_ENTER || code == Keyboard.CODE_SPACE)
@@ -674,6 +666,7 @@
 
     private void startLongPressTimer(int keyIndex) {
         Key key = getKey(keyIndex);
+        if (key == null) return;
         if (key.mCode == Keyboard.CODE_SHIFT) {
             if (sLongPressShiftKeyTimeout > 0) {
                 mTimerProxy.startLongPressTimer(sLongPressShiftKeyTimeout, keyIndex, this);
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index d9d4214..ddc69f1 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -872,7 +872,7 @@
     @Override
     public void hideWindow() {
         LatinImeLogger.commit();
-        mKeyboardSwitcher.onAutoCorrectionStateChanged(false);
+        mKeyboardSwitcher.onHideWindow();
 
         if (TRACE) Debug.stopMethodTracing();
         if (mOptionsDialog != null && mOptionsDialog.isShowing()) {