Refactor to remove KeyboardSwitcher.setVoiceMode

This change also renames KeyboardSwitcher.makeKeyboards to
refreshKeyboardCache.

Change-Id: I76897562d5876a53364dc378e7800ab92fb2a37b
main
Tadashi G. Takaoka 2010-11-10 16:10:10 -08:00
parent 02d7bdead6
commit 507495efd5
2 changed files with 45 additions and 63 deletions

View File

@ -153,8 +153,8 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
/** mIsAutoCompletionActive indicates that auto completed word will be input instead of /** mIsAutoCompletionActive indicates that auto completed word will be input instead of
* what user actually typed. */ * what user actually typed. */
private boolean mIsAutoCompletionActive; private boolean mIsAutoCompletionActive;
private boolean mHasVoice; private boolean mVoiceButtonEnabled;
private boolean mVoiceOnPrimary; private boolean mVoiceButtonOnPrimary;
private int mSymbolsModeState = SYMBOLS_MODE_STATE_NONE; private int mSymbolsModeState = SYMBOLS_MODE_STATE_NONE;
// Indicates whether or not we have the settings key // Indicates whether or not we have the settings key
@ -197,26 +197,26 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
mInputLocale = mLanguageSwitcher.getInputLocale(); mInputLocale = mLanguageSwitcher.getInputLocale();
} }
private KeyboardId makeSymbolsId(boolean hasVoice) { private KeyboardId makeSymbolsId(boolean voiceButtonEnabled) {
final int mode = mMode == MODE_NONE ? MODE_TEXT : mMode; final int mode = mMode == MODE_NONE ? MODE_TEXT : mMode;
return new KeyboardId(KBD_SYMBOLS[getCharColorId()], mHasSettingsKey ? return new KeyboardId(KBD_SYMBOLS[getCharColorId()], mHasSettingsKey ?
SYMBOLS_WITH_SETTINGS_KEY_MODES[mode] : SYMBOLS_MODES[mode], 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; final int mode = mMode == MODE_NONE ? MODE_TEXT : mMode;
return new KeyboardId(KBD_SYMBOLS_SHIFT[getCharColorId()], mHasSettingsKey ? return new KeyboardId(KBD_SYMBOLS_SHIFT[getCharColorId()], mHasSettingsKey ?
SYMBOLS_WITH_SETTINGS_KEY_MODES[mode] : SYMBOLS_MODES[mode], SYMBOLS_WITH_SETTINGS_KEY_MODES[mode] : SYMBOLS_MODES[mode],
false, hasVoice); false, voiceButtonEnabled);
} }
private void makeSymbolsKeyboardIds() { private void makeSymbolsKeyboardIds() {
mSymbolsId = makeSymbolsId(mHasVoice && !mVoiceOnPrimary); mSymbolsId = makeSymbolsId(mVoiceButtonEnabled && !mVoiceButtonOnPrimary);
mSymbolsShiftedId = makeSymbolsShiftedId(mHasVoice && !mVoiceOnPrimary); mSymbolsShiftedId = makeSymbolsShiftedId(mVoiceButtonEnabled && !mVoiceButtonOnPrimary);
} }
public void makeKeyboards(boolean forceCreate) { public void refreshKeyboardCache(boolean forceCreate) {
makeSymbolsKeyboardIds(); makeSymbolsKeyboardIds();
if (forceCreate) mKeyboards.clear(); if (forceCreate) mKeyboards.clear();
// Configuration change is coming after the keyboard gets recreated. So don't rely on that. // Configuration change is coming after the keyboard gets recreated. So don't rely on that.
@ -237,18 +237,18 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
public final int mXml; public final int mXml;
public final int mKeyboardMode; /** A KEYBOARDMODE_XXX value */ public final int mKeyboardMode; /** A KEYBOARDMODE_XXX value */
public final boolean mEnableShiftLock; public final boolean mEnableShiftLock;
public final boolean mHasVoice; public final boolean mVoiceButtonEnabled;
private final int mHashCode; 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.mXml = xml;
this.mKeyboardMode = mode; this.mKeyboardMode = mode;
this.mEnableShiftLock = enableShiftLock; this.mEnableShiftLock = enableShiftLock;
this.mHasVoice = hasVoice; this.mVoiceButtonEnabled = voiceButtonEnabled;
this.mHashCode = Arrays.hashCode(new Object[] { this.mHashCode = Arrays.hashCode(new Object[] {
xml, mode, enableShiftLock, hasVoice xml, mode, enableShiftLock, voiceButtonEnabled
}); });
} }
@ -261,7 +261,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
return other.mXml == this.mXml return other.mXml == this.mXml
&& other.mKeyboardMode == this.mKeyboardMode && other.mKeyboardMode == this.mKeyboardMode
&& other.mEnableShiftLock == this.mEnableShiftLock && other.mEnableShiftLock == this.mEnableShiftLock
&& other.mHasVoice == this.mHasVoice; && other.mVoiceButtonEnabled == this.mVoiceButtonEnabled;
} }
@Override @Override
@ -270,36 +270,31 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
} }
} }
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) { 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; mSymbolsModeState = SYMBOLS_MODE_STATE_NONE;
try { try {
setKeyboardMode(mode, imeOptions, enableVoice, false); setKeyboardModeInternal(mode, imeOptions, voiceButtonEnabled, voiceButtonOnPrimary,
false);
} catch (RuntimeException e) { } catch (RuntimeException e) {
LatinImeLogger.logOnException(mode + "," + imeOptions, 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; if (mInputView == null) return;
mMode = mode; mMode = mode;
mImeOptions = imeOptions; mImeOptions = imeOptions;
makeSymbolsKeyboardIds(); makeSymbolsKeyboardIds();
if (enableVoice != mHasVoice) { if (voiceButtonEnabled != mVoiceButtonEnabled || voiceButtonOnPrimary != mVoiceButtonOnPrimary) {
mKeyboards.clear(); mKeyboards.clear();
mHasVoice = enableVoice; mVoiceButtonEnabled = voiceButtonEnabled;
mVoiceButtonOnPrimary = voiceButtonOnPrimary;
} }
mIsSymbols = isSymbols; mIsSymbols = isSymbols;
@ -333,7 +328,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
orig.updateConfiguration(conf, null); orig.updateConfiguration(conf, null);
keyboard = new LatinKeyboard(mInputMethodService, id.mXml, id.mKeyboardMode); keyboard = new LatinKeyboard(mInputMethodService, id.mXml, id.mKeyboardMode);
keyboard.setVoiceMode(hasVoiceButton(id.mXml == R.xml.kbd_symbols 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()); keyboard.setLanguageSwitcher(mLanguageSwitcher, mIsAutoCompletionActive, isBlackSym());
if (id.mEnableShiftLock) { if (id.mEnableShiftLock) {
@ -434,7 +429,8 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
} }
public void toggleSymbols() { public void toggleSymbols() {
setKeyboardMode(mMode, mImeOptions, mHasVoice, !mIsSymbols); setKeyboardModeInternal(mMode, mImeOptions, mVoiceButtonEnabled, mVoiceButtonOnPrimary,
!mIsSymbols);
if (mIsSymbols) { if (mIsSymbols) {
mSymbolsModeState = SYMBOLS_MODE_STATE_BEGIN; mSymbolsModeState = SYMBOLS_MODE_STATE_BEGIN;
} else { } else {

View File

@ -186,7 +186,6 @@ public class LatinIME extends InputMethodService
private boolean mImmediatelyAfterVoiceInput; private boolean mImmediatelyAfterVoiceInput;
private boolean mShowingVoiceSuggestions; private boolean mShowingVoiceSuggestions;
private boolean mVoiceInputHighlighted; private boolean mVoiceInputHighlighted;
private boolean mEnableVoiceButton;
private CharSequence mBestWord; private CharSequence mBestWord;
private boolean mPredictionOn; private boolean mPredictionOn;
private boolean mCompletionOn; private boolean mCompletionOn;
@ -209,8 +208,8 @@ public class LatinIME extends InputMethodService
private boolean mShowSuggestions; private boolean mShowSuggestions;
private boolean mIsShowingHint; private boolean mIsShowingHint;
private int mCorrectionMode; private int mCorrectionMode;
private boolean mEnableVoice = true; private boolean mVoiceButtonEnabled;
private boolean mVoiceOnPrimary; private boolean mVoiceButtonOnPrimary;
private int mOrientation; private int mOrientation;
private List<CharSequence> mSuggestPuncList; private List<CharSequence> mSuggestPuncList;
// Keep track of the last selection range to decide if we need to show word alternatives // Keep track of the last selection range to decide if we need to show word alternatives
@ -530,16 +529,13 @@ public class LatinIME extends InputMethodService
@Override @Override
public View onCreateInputView() { public View onCreateInputView() {
mKeyboardSwitcher.recreateInputView(); mKeyboardSwitcher.recreateInputView();
mKeyboardSwitcher.makeKeyboards(true); mKeyboardSwitcher.refreshKeyboardCache(true);
mKeyboardSwitcher.setKeyboardMode(
KeyboardSwitcher.MODE_TEXT, 0,
shouldShowVoiceButton(makeFieldContext(), getCurrentInputEditorInfo()));
return mKeyboardSwitcher.getInputView(); return mKeyboardSwitcher.getInputView();
} }
@Override @Override
public View onCreateCandidatesView() { public View onCreateCandidatesView() {
mKeyboardSwitcher.makeKeyboards(true); mKeyboardSwitcher.refreshKeyboardCache(true);
mCandidateViewContainer = (LinearLayout) getLayoutInflater().inflate( mCandidateViewContainer = (LinearLayout) getLayoutInflater().inflate(
R.layout.candidates, null); R.layout.candidates, null);
mCandidateView = (CandidateView) mCandidateViewContainer.findViewById(R.id.candidates); mCandidateView = (CandidateView) mCandidateViewContainer.findViewById(R.id.candidates);
@ -572,7 +568,7 @@ public class LatinIME extends InputMethodService
toggleLanguage(true, true); toggleLanguage(true, true);
} }
mKeyboardSwitcher.makeKeyboards(false); mKeyboardSwitcher.refreshKeyboardCache(false);
TextEntryState.newSession(this); TextEntryState.newSession(this);
@ -666,10 +662,9 @@ public class LatinIME extends InputMethodService
mDeleteCount = 0; mDeleteCount = 0;
mJustAddedAutoSpace = false; mJustAddedAutoSpace = false;
loadSettings(); loadSettings(attribute);
mEnableVoiceButton = shouldShowVoiceButton(makeFieldContext(), attribute); mKeyboardSwitcher.setKeyboardMode(mode, attribute.imeOptions, mVoiceButtonEnabled,
mKeyboardSwitcher.setKeyboardMode(mode, attribute.imeOptions, mVoiceButtonOnPrimary);
mEnableVoiceButton && mEnableVoice);
updateShiftKeyState(attribute); updateShiftKeyState(attribute);
setCandidatesViewShownInternal(isCandidateStripVisible() || mCompletionOn, setCandidatesViewShownInternal(isCandidateStripVisible() || mCompletionOn,
@ -1031,11 +1026,8 @@ public class LatinIME extends InputMethodService
private void reloadKeyboards() { private void reloadKeyboards() {
mKeyboardSwitcher.setLanguageSwitcher(mLanguageSwitcher); mKeyboardSwitcher.setLanguageSwitcher(mLanguageSwitcher);
if (mKeyboardSwitcher.getInputView() != null int mode = mKeyboardSwitcher.getKeyboardMode();
&& mKeyboardSwitcher.getKeyboardMode() != KeyboardSwitcher.MODE_NONE) { mKeyboardSwitcher.setKeyboardMode(mode, 0, mVoiceButtonEnabled, mVoiceButtonOnPrimary);
mKeyboardSwitcher.setVoiceMode(mEnableVoice && mEnableVoiceButton, mVoiceOnPrimary);
}
mKeyboardSwitcher.makeKeyboards(true);
} }
private void commitTyped(InputConnection inputConnection) { private void commitTyped(InputConnection inputConnection) {
@ -2287,9 +2279,9 @@ public class LatinIME extends InputMethodService
} }
int currentKeyboardMode = mKeyboardSwitcher.getKeyboardMode(); int currentKeyboardMode = mKeyboardSwitcher.getKeyboardMode();
reloadKeyboards(); reloadKeyboards();
mKeyboardSwitcher.makeKeyboards(true); mKeyboardSwitcher.refreshKeyboardCache(true);
mKeyboardSwitcher.setKeyboardMode(currentKeyboardMode, 0, mKeyboardSwitcher.setKeyboardMode(currentKeyboardMode, 0, mVoiceButtonEnabled,
mEnableVoiceButton && mEnableVoice); mVoiceButtonOnPrimary);
initSuggest(mLanguageSwitcher.getInputLanguage()); initSuggest(mLanguageSwitcher.getInputLanguage());
mLanguageSwitcher.persist(); mLanguageSwitcher.persist();
updateShiftKeyState(getCurrentInputEditorInfo()); updateShiftKeyState(getCurrentInputEditorInfo());
@ -2533,7 +2525,7 @@ public class LatinIME extends InputMethodService
startActivity(intent); startActivity(intent);
} }
private void loadSettings() { private void loadSettings(EditorInfo attribute) {
// Get the settings preferences // Get the settings preferences
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
mVibrateOn = sp.getBoolean(PREF_VIBRATE_ON, false); mVibrateOn = sp.getBoolean(PREF_VIBRATE_ON, false);
@ -2568,15 +2560,9 @@ public class LatinIME extends InputMethodService
if (VOICE_INSTALLED) { if (VOICE_INSTALLED) {
final String voiceMode = sp.getString(PREF_VOICE_MODE, final String voiceMode = sp.getString(PREF_VOICE_MODE,
getString(R.string.voice_mode_main)); getString(R.string.voice_mode_main));
boolean enableVoice = !voiceMode.equals(getString(R.string.voice_mode_off)) mVoiceButtonEnabled = !voiceMode.equals(getString(R.string.voice_mode_off))
&& mEnableVoiceButton; && shouldShowVoiceButton(makeFieldContext(), attribute);
boolean voiceOnPrimary = voiceMode.equals(getString(R.string.voice_mode_main)); mVoiceButtonOnPrimary = voiceMode.equals(getString(R.string.voice_mode_main));
if (mKeyboardSwitcher != null &&
(enableVoice != mEnableVoice || voiceOnPrimary != mVoiceOnPrimary)) {
mKeyboardSwitcher.setVoiceMode(enableVoice, voiceOnPrimary);
}
mEnableVoice = enableVoice;
mVoiceOnPrimary = voiceOnPrimary;
} }
updateCorrectionMode(); updateCorrectionMode();
updateAutoTextEnabled(mResources.getConfiguration().locale); updateAutoTextEnabled(mResources.getConfiguration().locale);