Get rid of the reference to LatinKeyboard from LatinIME

Change-Id: I94286ecec0aa8dbfdfaa74d3a76b744c4a250370
diff --git a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
index 7307fcf..fa3cf22 100644
--- a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
@@ -306,8 +306,6 @@
 
         keyboard.onAutoCompletionStateChanged(mIsAutoCompletionActive);
         keyboard.setShifted(false);
-        // TODO: delete this?
-        keyboard.setShiftLocked(keyboard.isShiftLocked());
         return keyboard;
     }
 
@@ -333,11 +331,43 @@
     public int getKeyboardMode() {
         return mMode;
     }
-    
+
     public boolean isAlphabetMode() {
         return mCurrentId != null && mCurrentId.isAlphabetMode();
     }
 
+    public boolean isInputViewShown() {
+        return mInputView != null && mInputView.isShown();
+    }
+
+    public boolean isKeyboardAvailable() {
+        return mInputView != null && mInputView.getLatinKeyboard() != null;
+    }
+
+    public void setPreferredLetters(int[] frequencies) {
+        LatinKeyboard latinKeyboard;
+        if (mInputView != null && (latinKeyboard = mInputView.getLatinKeyboard()) != null)
+            latinKeyboard.setPreferredLetters(frequencies);
+    }
+
+    public void keyReleased() {
+        LatinKeyboard latinKeyboard;
+        if (mInputView != null && (latinKeyboard = mInputView.getLatinKeyboard()) != null)
+            latinKeyboard.keyReleased();
+    }
+
+    public boolean isShifted() {
+        LatinKeyboard latinKeyboard;
+        return mInputView != null && (latinKeyboard = mInputView.getLatinKeyboard()) != null
+                && latinKeyboard.isShifted();
+    }
+
+    public boolean isShiftLocked() {
+        LatinKeyboard latinKeyboard;
+        return mInputView != null && (latinKeyboard = mInputView.getLatinKeyboard()) != null
+                && latinKeyboard.isShiftLocked();
+    }
+
     public void setShifted(boolean shifted) {
         if (mInputView == null) return;
         LatinKeyboard latinKeyboard = mInputView.getLatinKeyboard();
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index b58575b..cd40dd3 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -318,7 +318,7 @@
                     break;
                 case MSG_START_TUTORIAL:
                     if (mTutorial == null) {
-                        if (mKeyboardSwitcher.getInputView().isShown()) {
+                        if (mKeyboardSwitcher.isInputViewShown()) {
                             mTutorial = new Tutorial(
                                     LatinIME.this, mKeyboardSwitcher.getInputView());
                             mTutorial.start();
@@ -720,9 +720,9 @@
             mVoiceInput.flushLogs();
             mVoiceInput.cancel();
         }
-        if (mKeyboardSwitcher.getInputView() != null) {
-            mKeyboardSwitcher.getInputView().closing();
-        }
+        BaseKeyboardView inputView = mKeyboardSwitcher.getInputView();
+        if (inputView != null)
+            inputView.closing();
         if (mAutoDictionary != null) mAutoDictionary.flushPendingWrites();
         if (mUserBigramDictionary != null) mUserBigramDictionary.flushPendingWrites();
     }
@@ -805,8 +805,7 @@
 
         if (mReCorrectionEnabled) {
             // Don't look for corrections if the keyboard is not visible
-            if (mKeyboardSwitcher != null && mKeyboardSwitcher.getInputView() != null
-                    && mKeyboardSwitcher.getInputView().isShown()) {
+            if (mKeyboardSwitcher.isInputViewShown()) {
                 // Check if we should go in or out of correction mode.
                 if (isPredictionOn()
                         && mJustRevertedSeparator == null
@@ -919,8 +918,8 @@
     private void setCandidatesViewShownInternal(boolean shown, boolean needsInputViewShown) {
         // TODO: Remove this if we support candidates with hard keyboard
         if (onEvaluateInputViewShown()) {
-            super.setCandidatesViewShown(shown && mKeyboardSwitcher.getInputView() != null
-                    && (needsInputViewShown ? mKeyboardSwitcher.getInputView().isShown() : true));
+            super.setCandidatesViewShown(shown
+                    && (needsInputViewShown ? mKeyboardSwitcher.isInputViewShown() : true));
         }
     }
 
@@ -987,12 +986,8 @@
                 if (mTutorial != null) {
                     return true;
                 }
-                LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
-                if (inputView == null) break;
-                LatinKeyboard latinKeyboard = inputView.getLatinKeyboard();
-                if (latinKeyboard == null) break;
                 // Enable shift key and DPAD to do selections
-                if (inputView.isShown() && latinKeyboard.isShifted()) {
+                if (mKeyboardSwitcher.isInputViewShown() && mKeyboardSwitcher.isShifted()) {
                     event = new KeyEvent(event.getDownTime(), event.getEventTime(),
                             event.getAction(), event.getKeyCode(), event.getRepeatCount(),
                             event.getDeviceId(), event.getScanCode(),
@@ -1043,14 +1038,13 @@
 
     public void updateShiftKeyState(EditorInfo attr) {
         InputConnection ic = getCurrentInputConnection();
-        LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
-        if (inputView == null) return;
-        LatinKeyboard latinKeyboard = inputView.getLatinKeyboard();
-        if (latinKeyboard == null) return;
-        if (ic != null && attr != null && mKeyboardSwitcher.isAlphabetMode()
+        KeyboardSwitcher switcher = mKeyboardSwitcher;
+        if (!switcher.isKeyboardAvailable())
+            return;
+        if (ic != null && attr != null && switcher.isAlphabetMode()
                 && !mShiftKeyState.isIgnoring()) {
-            mKeyboardSwitcher.setShifted(mShiftKeyState.isMomentary()
-                    || latinKeyboard.isShiftLocked() || getCursorCapsMode(ic, attr) != 0);
+            switcher.setShifted(mShiftKeyState.isMomentary()
+                    || switcher.isShiftLocked() || getCursorCapsMode(ic, attr) != 0);
         }
     }
 
@@ -1362,14 +1356,12 @@
         mHandler.removeMessages(MSG_UPDATE_SHIFT_STATE);
         KeyboardSwitcher switcher = mKeyboardSwitcher;
         if (switcher.isAlphabetMode()) {
-            LatinKeyboardView inputView = switcher.getInputView();
-            if (inputView == null) return;
-            LatinKeyboard latinKeyboard = inputView.getLatinKeyboard();
-            if (latinKeyboard == null) return;
-            if (latinKeyboard.isShiftLocked() || forceNormal) {
+            if (!switcher.isKeyboardAvailable())
+                return;
+            if (switcher.isShiftLocked() || forceNormal) {
                 switcher.setShifted(false);
             } else {
-                switcher.setShifted(!latinKeyboard.isShifted());
+                switcher.setShifted(!switcher.isShifted());
             }
         } else {
             switcher.toggleShift();
@@ -1380,16 +1372,14 @@
         mHandler.removeMessages(MSG_UPDATE_SHIFT_STATE);
         KeyboardSwitcher switcher = mKeyboardSwitcher;
         if (switcher.isAlphabetMode()) {
-            LatinKeyboardView inputView = switcher.getInputView();
-            if (inputView == null) return;
-            LatinKeyboard latinKeyboard = inputView.getLatinKeyboard();
-            if (latinKeyboard == null) return;
-            if (latinKeyboard.isShiftLocked()) {
-                // LatinKeyboard.setShifted(false) also disable shift locked state.
+            if (!switcher.isKeyboardAvailable())
+                return;
+            if (switcher.isShiftLocked()) {
+                // KeyboardSwitcher.setShifted(false) also disable shift locked state.
                 // Note: Caps lock LED is off when Key.on is false.
                 switcher.setShifted(false);
             } else {
-                // LatinKeyboard.setShiftLocked(true) enable shift state too.
+                // KeyboardSwitcher.setShiftLocked(true) enable shift state too.
                 // Note: Caps lock LED is on when Key.on is true.
                 switcher.setShiftLocked(true);
             }
@@ -1426,14 +1416,14 @@
                 mWord.reset();
             }
         }
-        LatinKeyboard latinKeyboard = mKeyboardSwitcher.getInputView().getLatinKeyboard();
-        if (latinKeyboard != null && latinKeyboard.isShifted()) {
+        KeyboardSwitcher switcher = mKeyboardSwitcher;
+        if (switcher.isShifted()) {
             if (keyCodes == null || keyCodes[0] < Character.MIN_CODE_POINT
                     || keyCodes[0] > Character.MAX_CODE_POINT) {
                 return;
             }
             primaryCode = keyCodes[0];
-            if (mKeyboardSwitcher.isAlphabetMode() && Character.isLowerCase(primaryCode)) {
+            if (switcher.isAlphabetMode() && Character.isLowerCase(primaryCode)) {
                 int upperCaseCode = Character.toUpperCase(primaryCode);
                 if (upperCaseCode != primaryCode) {
                     primaryCode = upperCaseCode;
@@ -1446,9 +1436,7 @@
             }
         }
         if (mPredicting) {
-            if (latinKeyboard != null && latinKeyboard.isShifted()
-                    && mKeyboardSwitcher.isAlphabetMode()
-                    && mComposing.length() == 0) {
+            if (mComposing.length() == 0 && switcher.isAlphabetMode() && switcher.isShifted()) {
                 mWord.setFirstCharCapitalized(true);
             }
             mComposing.append((char) primaryCode);
@@ -1547,12 +1535,9 @@
             mVoiceInput.cancel();
         }
         requestHideSelf(0);
-        if (mKeyboardSwitcher != null) {
-            LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
-            if (inputView != null) {
-                inputView.closing();
-            }
-        }
+        LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
+        if (inputView != null)
+            inputView.closing();
         TextEntryState.endSession();
     }
 
@@ -1605,7 +1590,7 @@
           public void run() {
               mRecognizing = false;
               if (mKeyboardSwitcher.getInputView() != null) {
-                setInputView(mKeyboardSwitcher.getInputView());
+                  setInputView(mKeyboardSwitcher.getInputView());
               }
               setCandidatesViewShown(isCandidateStripVisible());
               updateInputViewShown();
@@ -1742,10 +1727,9 @@
         switchToKeyboardView();
 
         final List<CharSequence> nBest = new ArrayList<CharSequence>();
+        KeyboardSwitcher switcher = mKeyboardSwitcher;
         boolean capitalizeFirstWord = preferCapitalization()
-                || (mKeyboardSwitcher.isAlphabetMode()
-                        && mKeyboardSwitcher.getInputView().getLatinKeyboard() != null
-                                && mKeyboardSwitcher.getInputView().getLatinKeyboard().isShifted());
+                || (switcher.isAlphabetMode() && switcher.isShifted());
         for (String c : mVoiceResults.candidates) {
             if (capitalizeFirstWord) {
                 c = Character.toUpperCase(c.charAt(0)) + c.substring(1, c.length());
@@ -1796,11 +1780,7 @@
     }
 
     private void updateSuggestions() {
-        LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
-        LatinKeyboard latinKeyboard = inputView.getLatinKeyboard();
-        if (latinKeyboard != null) {
-            latinKeyboard.setPreferredLetters(null);
-        }
+        mKeyboardSwitcher.setPreferredLetters(null);
 
         // Check if we have a suggestion engine attached.
         if ((mSuggest == null || !isPredictionOn()) && !mVoiceInputHighlighted) {
@@ -1821,11 +1801,8 @@
     }
 
     private void showCorrections(WordAlternatives alternatives) {
+        mKeyboardSwitcher.setPreferredLetters(null);
         List<CharSequence> stringList = alternatives.getAlternatives();
-        LatinKeyboard latinKeyboard = mKeyboardSwitcher.getInputView().getLatinKeyboard();
-        if (latinKeyboard != null) {
-            latinKeyboard.setPreferredLetters(null);
-        }
         showSuggestions(stringList, alternatives.getOriginalWord(), false, false);
     }
 
@@ -1840,11 +1817,7 @@
         // Log.d("LatinIME","Suggest Total Time - " + (stopTime - startTime));
 
         int[] nextLettersFrequencies = mSuggest.getNextLettersFrequencies();
-
-        LatinKeyboard latinKeyboard = mKeyboardSwitcher.getInputView().getLatinKeyboard();
-        if (latinKeyboard != null) {
-            latinKeyboard.setPreferredLetters(nextLettersFrequencies);
-        }
+        mKeyboardSwitcher.setPreferredLetters(nextLettersFrequencies);
 
         boolean correctionAvailable = !mInputTypeNoAutoCorrect && mSuggest.hasMinimalCorrection();
         //|| mCorrectionMode == mSuggest.CORRECTION_FULL;
@@ -2015,10 +1988,8 @@
      *            word.
      */
     private void pickSuggestion(CharSequence suggestion, boolean correcting) {
-        LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
-        if (inputView == null) return;
-        LatinKeyboard latinKeyboard = inputView.getLatinKeyboard();
-        if (latinKeyboard == null) return;
+        if (!mKeyboardSwitcher.isKeyboardAvailable())
+            return;
         InputConnection ic = getCurrentInputConnection();
         if (ic != null) {
             rememberReplacedWord(suggestion);
@@ -2027,9 +1998,7 @@
         saveWordInHistory(suggestion);
         mPredicting = false;
         mCommittedLength = suggestion.length();
-        if (latinKeyboard != null) {
-            latinKeyboard.setPreferredLetters(null);
-        }
+        mKeyboardSwitcher.setPreferredLetters(null);
         // If we just corrected a word, then don't show punctuations
         if (!correcting) {
             setPunctuationSuggestions();
@@ -2316,15 +2285,14 @@
     public void onPress(int primaryCode) {
         vibrate();
         playKeyClick(primaryCode);
-        final boolean distinctMultiTouch = mKeyboardSwitcher.hasDistinctMultitouch();
-        LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
-        if (inputView == null) return;
-        LatinKeyboard latinKeyboard = inputView.getLatinKeyboard();
-        if (latinKeyboard == null) return;
+        KeyboardSwitcher switcher = mKeyboardSwitcher;
+        if (!switcher.isKeyboardAvailable())
+            return;
+        final boolean distinctMultiTouch = switcher.hasDistinctMultitouch();
         if (distinctMultiTouch && primaryCode == BaseKeyboard.KEYCODE_SHIFT) {
             // In alphabet mode, we call handleShift() to go into the shifted mode in this
             // method, onPress(), only when we are in the small letter mode.
-            if (mKeyboardSwitcher.isAlphabetMode() && latinKeyboard.isShifted()) {
+            if (switcher.isAlphabetMode() && switcher.isShifted()) {
                 mShiftKeyState.onPressOnShifted();
             } else {
                 mShiftKeyState.onPress();
@@ -2339,26 +2307,22 @@
         }
     }
 
-    // TODO: Bug - onRelease() could be dropped if the user slides finger out of the key.  It's OK
-    // for general keys, but we need to obtain onRelease() for the shift key even in such case.
     public void onRelease(int primaryCode) {
+        KeyboardSwitcher switcher = mKeyboardSwitcher;
+        if (!switcher.isKeyboardAvailable())
+            return;
         // Reset any drag flags in the keyboard
-        LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
-        if (inputView == null) return;
-        LatinKeyboard latinKeyboard = inputView.getLatinKeyboard();
-        if (latinKeyboard == null) return;
-        latinKeyboard.keyReleased();
-        //vibrate();
-        final boolean distinctMultiTouch = mKeyboardSwitcher.hasDistinctMultitouch();
+        switcher.keyReleased();
+        final boolean distinctMultiTouch = switcher.hasDistinctMultitouch();
         if (distinctMultiTouch && primaryCode == BaseKeyboard.KEYCODE_SHIFT) {
             if (mShiftKeyState.isMomentary()) {
                 resetShift();
             }
-            if (mKeyboardSwitcher.isAlphabetMode()) {
+            if (switcher.isAlphabetMode()) {
                 // In alphabet mode, we call handleShift() to go into the small letter mode in this
                 // method, onRelease(), only when we are in the shifted modes -- temporary shifted
                 // mode or caps lock mode.
-                if (latinKeyboard.isShifted() && mShiftKeyState.isPressingOnShifted()) {
+                if (switcher.isShifted() && mShiftKeyState.isPressingOnShifted()) {
                     handleShift();
                 }
             }
@@ -2441,8 +2405,9 @@
         if (!mVibrateOn) {
             return;
         }
-        if (mKeyboardSwitcher.getInputView() != null) {
-            mKeyboardSwitcher.getInputView().performHapticFeedback(
+        LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
+        if (inputView != null) {
+            inputView.performHapticFeedback(
                     HapticFeedbackConstants.KEYBOARD_TAP,
                     HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
         }
@@ -2664,13 +2629,12 @@
     }
 
     private void changeKeyboardMode() {
-        mKeyboardSwitcher.toggleSymbols();
-        LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
-        if (inputView == null) return;
-        LatinKeyboard latinKeyboard = inputView.getLatinKeyboard();
-        if (latinKeyboard == null) return;
-        if (latinKeyboard.isShiftLocked() && mKeyboardSwitcher.isAlphabetMode()) {
-            mKeyboardSwitcher.setShiftLocked(true);
+        KeyboardSwitcher switcher = mKeyboardSwitcher;
+        switcher.toggleSymbols();
+        if (!switcher.isKeyboardAvailable())
+            return;
+        if (switcher.isShiftLocked() && switcher.isAlphabetMode()) {
+            switcher.setShiftLocked(true);
         }
 
         updateShiftKeyState(getCurrentInputEditorInfo());