Get rid of the reference to LatinKeyboard from LatinIME

Change-Id: I94286ecec0aa8dbfdfaa74d3a76b744c4a250370
main
Tadashi G. Takaoka 2010-11-13 00:01:13 -08:00
parent 3009310283
commit 1679432d1c
2 changed files with 93 additions and 99 deletions

View File

@ -306,8 +306,6 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
keyboard.onAutoCompletionStateChanged(mIsAutoCompletionActive); keyboard.onAutoCompletionStateChanged(mIsAutoCompletionActive);
keyboard.setShifted(false); keyboard.setShifted(false);
// TODO: delete this?
keyboard.setShiftLocked(keyboard.isShiftLocked());
return keyboard; return keyboard;
} }
@ -333,11 +331,43 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha
public int getKeyboardMode() { public int getKeyboardMode() {
return mMode; return mMode;
} }
public boolean isAlphabetMode() { public boolean isAlphabetMode() {
return mCurrentId != null && mCurrentId.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) { public void setShifted(boolean shifted) {
if (mInputView == null) return; if (mInputView == null) return;
LatinKeyboard latinKeyboard = mInputView.getLatinKeyboard(); LatinKeyboard latinKeyboard = mInputView.getLatinKeyboard();

View File

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