Refactor to remove KeyboardSwitcher.setVoiceMode

This change also renames KeyboardSwitcher.makeKeyboards to
refreshKeyboardCache.

Change-Id: I76897562d5876a53364dc378e7800ab92fb2a37b
diff --git a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
index 08e9f94..dae5962 100644
--- a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
@@ -153,8 +153,8 @@
     /** mIsAutoCompletionActive indicates that auto completed word will be input instead of
      * what user actually typed. */
     private boolean mIsAutoCompletionActive;
-    private boolean mHasVoice;
-    private boolean mVoiceOnPrimary;
+    private boolean mVoiceButtonEnabled;
+    private boolean mVoiceButtonOnPrimary;
     private int mSymbolsModeState = SYMBOLS_MODE_STATE_NONE;
 
     // Indicates whether or not we have the settings key
@@ -197,26 +197,26 @@
         mInputLocale = mLanguageSwitcher.getInputLocale();
     }
 
-    private KeyboardId makeSymbolsId(boolean hasVoice) {
+    private KeyboardId makeSymbolsId(boolean voiceButtonEnabled) {
         final int mode = mMode == MODE_NONE ? MODE_TEXT : mMode;
         return new KeyboardId(KBD_SYMBOLS[getCharColorId()], mHasSettingsKey ?
                 SYMBOLS_WITH_SETTINGS_KEY_MODES[mode] : SYMBOLS_MODES[mode],
-                false, hasVoice);
+                false, voiceButtonEnabled);
     }
 
-    private KeyboardId makeSymbolsShiftedId(boolean hasVoice) {
+    private KeyboardId makeSymbolsShiftedId(boolean voiceButtonEnabled) {
         final int mode = mMode == MODE_NONE ? MODE_TEXT : mMode;
         return new KeyboardId(KBD_SYMBOLS_SHIFT[getCharColorId()], mHasSettingsKey ?
                 SYMBOLS_WITH_SETTINGS_KEY_MODES[mode] : SYMBOLS_MODES[mode],
-                false, hasVoice);
+                false, voiceButtonEnabled);
     }
 
     private void makeSymbolsKeyboardIds() {
-        mSymbolsId = makeSymbolsId(mHasVoice && !mVoiceOnPrimary);
-        mSymbolsShiftedId = makeSymbolsShiftedId(mHasVoice && !mVoiceOnPrimary);
+        mSymbolsId = makeSymbolsId(mVoiceButtonEnabled && !mVoiceButtonOnPrimary);
+        mSymbolsShiftedId = makeSymbolsShiftedId(mVoiceButtonEnabled && !mVoiceButtonOnPrimary);
     }
 
-    public void makeKeyboards(boolean forceCreate) {
+    public void refreshKeyboardCache(boolean forceCreate) {
         makeSymbolsKeyboardIds();
         if (forceCreate) mKeyboards.clear();
         // Configuration change is coming after the keyboard gets recreated. So don't rely on that.
@@ -237,18 +237,18 @@
         public final int mXml;
         public final int mKeyboardMode; /** A KEYBOARDMODE_XXX value */
         public final boolean mEnableShiftLock;
-        public final boolean mHasVoice;
+        public final boolean mVoiceButtonEnabled;
 
         private final int mHashCode;
 
-        public KeyboardId(int xml, int mode, boolean enableShiftLock, boolean hasVoice) {
+        public KeyboardId(int xml, int mode, boolean enableShiftLock, boolean voiceButtonEnabled) {
             this.mXml = xml;
             this.mKeyboardMode = mode;
             this.mEnableShiftLock = enableShiftLock;
-            this.mHasVoice = hasVoice;
+            this.mVoiceButtonEnabled = voiceButtonEnabled;
 
             this.mHashCode = Arrays.hashCode(new Object[] {
-               xml, mode, enableShiftLock, hasVoice
+               xml, mode, enableShiftLock, voiceButtonEnabled
             });
         }
 
@@ -261,7 +261,7 @@
             return other.mXml == this.mXml
                 && other.mKeyboardMode == this.mKeyboardMode
                 && other.mEnableShiftLock == this.mEnableShiftLock
-                && other.mHasVoice == this.mHasVoice;
+                && other.mVoiceButtonEnabled == this.mVoiceButtonEnabled;
         }
 
         @Override
@@ -270,36 +270,31 @@
         }
     }
 
-    public void setVoiceMode(boolean enableVoice, boolean voiceOnPrimary) {
-        if (enableVoice != mHasVoice || voiceOnPrimary != mVoiceOnPrimary) {
-            mKeyboards.clear();
-        }
-        mHasVoice = enableVoice;
-        mVoiceOnPrimary = voiceOnPrimary;
-        setKeyboardMode(mMode, mImeOptions, mHasVoice, mIsSymbols);
-    }
-
     private boolean hasVoiceButton(boolean isSymbols) {
-        return mHasVoice && (isSymbols != mVoiceOnPrimary);
+        return mVoiceButtonEnabled && (isSymbols != mVoiceButtonOnPrimary);
     }
 
-    public void setKeyboardMode(int mode, int imeOptions, boolean enableVoice) {
+    public void setKeyboardMode(int mode, int imeOptions, boolean voiceButtonEnabled,
+            boolean voiceButtonOnPrimary) {
         mSymbolsModeState = SYMBOLS_MODE_STATE_NONE;
         try {
-            setKeyboardMode(mode, imeOptions, enableVoice, false);
+            setKeyboardModeInternal(mode, imeOptions, voiceButtonEnabled, voiceButtonOnPrimary,
+                    false);
         } catch (RuntimeException e) {
             LatinImeLogger.logOnException(mode + "," + imeOptions, e);
         }
     }
 
-    private void setKeyboardMode(int mode, int imeOptions, boolean enableVoice, boolean isSymbols) {
+    private void setKeyboardModeInternal(int mode, int imeOptions, boolean voiceButtonEnabled,
+            boolean voiceButtonOnPrimary, boolean isSymbols) {
         if (mInputView == null) return;
         mMode = mode;
         mImeOptions = imeOptions;
         makeSymbolsKeyboardIds();
-        if (enableVoice != mHasVoice) {
+        if (voiceButtonEnabled != mVoiceButtonEnabled || voiceButtonOnPrimary != mVoiceButtonOnPrimary) {
             mKeyboards.clear();
-            mHasVoice = enableVoice;
+            mVoiceButtonEnabled = voiceButtonEnabled;
+            mVoiceButtonOnPrimary = voiceButtonOnPrimary;
         }
         mIsSymbols = isSymbols;
 
@@ -333,7 +328,7 @@
             orig.updateConfiguration(conf, null);
             keyboard = new LatinKeyboard(mInputMethodService, id.mXml, id.mKeyboardMode);
             keyboard.setVoiceMode(hasVoiceButton(id.mXml == R.xml.kbd_symbols
-                    || id.mXml == R.xml.kbd_symbols_black), mHasVoice);
+                    || id.mXml == R.xml.kbd_symbols_black), mVoiceButtonEnabled);
             keyboard.setLanguageSwitcher(mLanguageSwitcher, mIsAutoCompletionActive, isBlackSym());
 
             if (id.mEnableShiftLock) {
@@ -434,7 +429,8 @@
     }
 
     public void toggleSymbols() {
-        setKeyboardMode(mMode, mImeOptions, mHasVoice, !mIsSymbols);
+        setKeyboardModeInternal(mMode, mImeOptions, mVoiceButtonEnabled, mVoiceButtonOnPrimary,
+                !mIsSymbols);
         if (mIsSymbols) {
             mSymbolsModeState = SYMBOLS_MODE_STATE_BEGIN;
         } else {
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 113027c..cb50cfb 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -186,7 +186,6 @@
     private boolean mImmediatelyAfterVoiceInput;
     private boolean mShowingVoiceSuggestions;
     private boolean mVoiceInputHighlighted;
-    private boolean mEnableVoiceButton;
     private CharSequence mBestWord;
     private boolean mPredictionOn;
     private boolean mCompletionOn;
@@ -209,8 +208,8 @@
     private boolean mShowSuggestions;
     private boolean mIsShowingHint;
     private int     mCorrectionMode;
-    private boolean mEnableVoice = true;
-    private boolean mVoiceOnPrimary;
+    private boolean mVoiceButtonEnabled;
+    private boolean mVoiceButtonOnPrimary;
     private int     mOrientation;
     private List<CharSequence> mSuggestPuncList;
     // Keep track of the last selection range to decide if we need to show word alternatives
@@ -530,16 +529,13 @@
     @Override
     public View onCreateInputView() {
         mKeyboardSwitcher.recreateInputView();
-        mKeyboardSwitcher.makeKeyboards(true);
-        mKeyboardSwitcher.setKeyboardMode(
-                KeyboardSwitcher.MODE_TEXT, 0,
-                shouldShowVoiceButton(makeFieldContext(), getCurrentInputEditorInfo()));
+        mKeyboardSwitcher.refreshKeyboardCache(true);
         return mKeyboardSwitcher.getInputView();
     }
 
     @Override
     public View onCreateCandidatesView() {
-        mKeyboardSwitcher.makeKeyboards(true);
+        mKeyboardSwitcher.refreshKeyboardCache(true);
         mCandidateViewContainer = (LinearLayout) getLayoutInflater().inflate(
                 R.layout.candidates, null);
         mCandidateView = (CandidateView) mCandidateViewContainer.findViewById(R.id.candidates);
@@ -572,7 +568,7 @@
             toggleLanguage(true, true);
         }
 
-        mKeyboardSwitcher.makeKeyboards(false);
+        mKeyboardSwitcher.refreshKeyboardCache(false);
 
         TextEntryState.newSession(this);
 
@@ -666,10 +662,9 @@
         mDeleteCount = 0;
         mJustAddedAutoSpace = false;
 
-        loadSettings();
-        mEnableVoiceButton = shouldShowVoiceButton(makeFieldContext(), attribute);
-        mKeyboardSwitcher.setKeyboardMode(mode, attribute.imeOptions,
-                mEnableVoiceButton && mEnableVoice);
+        loadSettings(attribute);
+        mKeyboardSwitcher.setKeyboardMode(mode, attribute.imeOptions, mVoiceButtonEnabled,
+                mVoiceButtonOnPrimary);
         updateShiftKeyState(attribute);
 
         setCandidatesViewShownInternal(isCandidateStripVisible() || mCompletionOn,
@@ -1031,11 +1026,8 @@
 
     private void reloadKeyboards() {
         mKeyboardSwitcher.setLanguageSwitcher(mLanguageSwitcher);
-        if (mKeyboardSwitcher.getInputView() != null
-                && mKeyboardSwitcher.getKeyboardMode() != KeyboardSwitcher.MODE_NONE) {
-            mKeyboardSwitcher.setVoiceMode(mEnableVoice && mEnableVoiceButton, mVoiceOnPrimary);
-        }
-        mKeyboardSwitcher.makeKeyboards(true);
+        int mode = mKeyboardSwitcher.getKeyboardMode();
+        mKeyboardSwitcher.setKeyboardMode(mode, 0, mVoiceButtonEnabled, mVoiceButtonOnPrimary);
     }
 
     private void commitTyped(InputConnection inputConnection) {
@@ -2287,9 +2279,9 @@
         }
         int currentKeyboardMode = mKeyboardSwitcher.getKeyboardMode();
         reloadKeyboards();
-        mKeyboardSwitcher.makeKeyboards(true);
-        mKeyboardSwitcher.setKeyboardMode(currentKeyboardMode, 0,
-                mEnableVoiceButton && mEnableVoice);
+        mKeyboardSwitcher.refreshKeyboardCache(true);
+        mKeyboardSwitcher.setKeyboardMode(currentKeyboardMode, 0, mVoiceButtonEnabled,
+                mVoiceButtonOnPrimary);
         initSuggest(mLanguageSwitcher.getInputLanguage());
         mLanguageSwitcher.persist();
         updateShiftKeyState(getCurrentInputEditorInfo());
@@ -2525,7 +2517,7 @@
         launchSettings(LatinIMEDebugSettings.class);
     }
 
-    protected void launchSettings (Class<? extends PreferenceActivity> settingsClass) {
+    protected void launchSettings(Class<? extends PreferenceActivity> settingsClass) {
         handleClose();
         Intent intent = new Intent();
         intent.setClass(LatinIME.this, settingsClass);
@@ -2533,7 +2525,7 @@
         startActivity(intent);
     }
 
-    private void loadSettings() {
+    private void loadSettings(EditorInfo attribute) {
         // Get the settings preferences
         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
         mVibrateOn = sp.getBoolean(PREF_VIBRATE_ON, false);
@@ -2568,15 +2560,9 @@
         if (VOICE_INSTALLED) {
             final String voiceMode = sp.getString(PREF_VOICE_MODE,
                     getString(R.string.voice_mode_main));
-            boolean enableVoice = !voiceMode.equals(getString(R.string.voice_mode_off))
-                    && mEnableVoiceButton;
-            boolean voiceOnPrimary = voiceMode.equals(getString(R.string.voice_mode_main));
-            if (mKeyboardSwitcher != null &&
-                    (enableVoice != mEnableVoice || voiceOnPrimary != mVoiceOnPrimary)) {
-                mKeyboardSwitcher.setVoiceMode(enableVoice, voiceOnPrimary);
-            }
-            mEnableVoice = enableVoice;
-            mVoiceOnPrimary = voiceOnPrimary;
+            mVoiceButtonEnabled = !voiceMode.equals(getString(R.string.voice_mode_off))
+                    && shouldShowVoiceButton(makeFieldContext(), attribute);
+            mVoiceButtonOnPrimary = voiceMode.equals(getString(R.string.voice_mode_main));
         }
         updateCorrectionMode();
         updateAutoTextEnabled(mResources.getConfiguration().locale);