diff options
Diffstat (limited to 'src/com/android/inputmethod/latin/LatinIME.java')
-rw-r--r-- | src/com/android/inputmethod/latin/LatinIME.java | 186 |
1 files changed, 85 insertions, 101 deletions
diff --git a/src/com/android/inputmethod/latin/LatinIME.java b/src/com/android/inputmethod/latin/LatinIME.java index 2af59d651..18b277c5a 100644 --- a/src/com/android/inputmethod/latin/LatinIME.java +++ b/src/com/android/inputmethod/latin/LatinIME.java @@ -130,24 +130,14 @@ public class LatinIME extends InputMethodService // ignored, since it may in fact be two key presses in quick succession. private static final long MIN_MILLIS_AFTER_TYPING_BEFORE_SWIPE = 1000; - // If we detect a swipe gesture, and the user types N ms later, cancel the - // swipe since it was probably a false trigger. - private static final long MIN_MILLIS_AFTER_SWIPE_TO_WAIT_FOR_TYPING = 500; - // How many continuous deletes at which to start deleting at a higher speed. private static final int DELETE_ACCELERATE_AT = 20; // Key events coming any faster than this are long-presses. private static final int QUICK_PRESS = 200; - // Weight added to a user picking a new word from the suggestion strip - static final int FREQUENCY_FOR_PICKED = 3; - // Weight added to a user typing a new word that doesn't get corrected (or is reverted) - static final int FREQUENCY_FOR_TYPED = 1; - // A word that is frequently typed and get's promoted to the user dictionary, uses this - // frequency. - static final int FREQUENCY_FOR_AUTO_ADD = 250; static final int KEYCODE_ENTER = '\n'; static final int KEYCODE_SPACE = ' '; + static final int KEYCODE_PERIOD = '.'; // Contextual menu positions private static final int POS_SETTINGS = 0; @@ -318,23 +308,24 @@ public class LatinIME extends InputMethodService Resources orig = getResources(); Configuration conf = orig.getConfiguration(); Locale saveLocale = conf.locale; - boolean different = !conf.locale.getCountry().equalsIgnoreCase(locale.substring(0, 2)); conf.locale = new Locale(locale); orig.updateConfiguration(conf, orig.getDisplayMetrics()); if (mSuggest != null) { mSuggest.close(); } + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); + mQuickFixes = sp.getBoolean(PREF_QUICK_FIXES, true); mSuggest = new Suggest(this, R.raw.main); - mSuggest.setAutoTextEnabled(!different); + updateAutoTextEnabled(saveLocale); if (mUserDictionary != null) mUserDictionary.close(); - mUserDictionary = new UserDictionary(this); + mUserDictionary = new UserDictionary(this, mLocale); if (mContactsDictionary == null) { mContactsDictionary = new ContactsDictionary(this); } - // TODO: Save and restore the dictionary for the current input language. - if (mAutoDictionary == null) { - mAutoDictionary = new AutoDictionary(this); + if (mAutoDictionary != null) { + mAutoDictionary.close(); } + mAutoDictionary = new AutoDictionary(this, this, mLocale); mSuggest.setUserDictionary(mUserDictionary); mSuggest.setContactsDictionary(mContactsDictionary); mSuggest.setAutoDictionary(mAutoDictionary); @@ -359,8 +350,18 @@ public class LatinIME extends InputMethodService @Override public void onConfigurationChanged(Configuration conf) { + // If the system locale changes and is different from the saved + // locale (mLocale), then reload the input locale list from the + // latin ime settings (shared prefs) and reset the input locale + // to the first one. if (!TextUtils.equals(conf.locale.toString(), mLocale)) { - initSuggest(conf.locale.toString()); + if (mLanguageSwitcher != null) { + mLanguageSwitcher.loadLocales( + PreferenceManager.getDefaultSharedPreferences(this)); + toggleLanguage(true, true); + } else { + reloadKeyboards(); + } } // If orientation changed while predicting, commit the change if (conf.orientation != mOrientation) { @@ -368,8 +369,8 @@ public class LatinIME extends InputMethodService commitTyped(ic); if (ic != null) ic.finishComposingText(); // For voice input mOrientation = conf.orientation; + reloadKeyboards(); } - reloadKeyboards(); super.onConfigurationChanged(conf); } @@ -447,7 +448,6 @@ public class LatinIME extends InputMethodService mShowingVoiceSuggestions = false; mImmediatelyAfterVoiceSuggestions = false; mVoiceInputHighlighted = false; - boolean disableAutoCorrect = false; mWordToSuggestions.clear(); mInputTypeNoAutoCorrect = false; mPredictionOn = false; @@ -534,24 +534,18 @@ public class LatinIME extends InputMethodService mDeleteCount = 0; mJustAddedAutoSpace = false; loadSettings(); + updateShiftKeyState(attribute); setCandidatesViewShown(false); setSuggestions(null, false, false, false); - // Override auto correct - if (disableAutoCorrect) { - mAutoCorrectOn = false; - if (mCorrectionMode == Suggest.CORRECTION_FULL) { - mCorrectionMode = Suggest.CORRECTION_BASIC; - } - } // If the dictionary is not big enough, don't auto correct mHasDictionary = mSuggest.hasMainDictionary(); updateCorrectionMode(); mInputView.setProximityCorrectionEnabled(true); - mPredictionOn = mPredictionOn && mCorrectionMode > 0; + mPredictionOn = mPredictionOn && (mCorrectionMode > 0 || mShowSuggestions); checkTutorial(attribute.privateImeOptions); if (TRACE) Debug.startMethodTracing("/data/trace/latinime"); } @@ -797,7 +791,7 @@ public class LatinIME extends InputMethodService } mKeyboardSwitcher.setLanguageSwitcher(mLanguageSwitcher); if (mInputView != null) { - mKeyboardSwitcher.setVoiceMode(mEnableVoice, mVoiceOnPrimary); + mKeyboardSwitcher.setVoiceMode(mEnableVoice && mEnableVoiceButton, mVoiceOnPrimary); } mKeyboardSwitcher.makeKeyboards(true); } @@ -811,7 +805,7 @@ public class LatinIME extends InputMethodService } mCommittedLength = mComposing.length(); TextEntryState.acceptedTyped(mComposing); - mAutoDictionary.addWord(mComposing.toString(), FREQUENCY_FOR_TYPED); + checkAddToDictionary(mComposing, AutoDictionary.FREQUENCY_FOR_TYPED); } updateSuggestions(); } @@ -854,6 +848,22 @@ public class LatinIME extends InputMethodService } } + private void reswapPeriodAndSpace() { + final InputConnection ic = getCurrentInputConnection(); + if (ic == null) return; + CharSequence lastThree = ic.getTextBeforeCursor(3, 0); + if (lastThree != null && lastThree.length() == 3 + && lastThree.charAt(0) == KEYCODE_PERIOD + && lastThree.charAt(1) == KEYCODE_SPACE + && lastThree.charAt(2) == KEYCODE_PERIOD) { + ic.beginBatchEdit(); + ic.deleteSurroundingText(3, 0); + ic.commitText(" ..", 1); + ic.endBatchEdit(); + updateShiftKeyState(getCurrentInputEditorInfo()); + } + } + private void doubleSpace() { //if (!mAutoPunctuate) return; if (mCorrectionMode == Suggest.CORRECTION_NONE) return; @@ -879,8 +889,9 @@ public class LatinIME extends InputMethodService // When the text's first character is '.', remove the previous period // if there is one. CharSequence lastOne = ic.getTextBeforeCursor(1, 0); - if (lastOne != null && lastOne.length() == 1 && lastOne.charAt(0) == '.' - && text.charAt(0) == '.') { + if (lastOne != null && lastOne.length() == 1 + && lastOne.charAt(0) == KEYCODE_PERIOD + && text.charAt(0) == KEYCODE_PERIOD) { ic.deleteSurroundingText(1, 0); } } @@ -976,6 +987,9 @@ public class LatinIME extends InputMethodService } public void onText(CharSequence text) { + if (VOICE_INSTALLED && mVoiceInputHighlighted) { + commitVoiceInput(); + } InputConnection ic = getCurrentInputConnection(); if (ic == null) return; ic.beginBatchEdit(); @@ -1117,6 +1131,14 @@ public class LatinIME extends InputMethodService mJustAddedAutoSpace = false; } sendKeyChar((char)primaryCode); + + // Handle the case of ". ." -> " .." with auto-space if necessary + // before changing the TextEntryState. + if (TextEntryState.getState() == TextEntryState.STATE_PUNCTUATION_AFTER_ACCEPTED + && primaryCode == KEYCODE_PERIOD) { + reswapPeriodAndSpace(); + } + TextEntryState.typedCharacter((char) primaryCode, true); if (TextEntryState.getState() == TextEntryState.STATE_PUNCTUATION_AFTER_ACCEPTED && primaryCode != KEYCODE_ENTER) { @@ -1164,7 +1186,6 @@ public class LatinIME extends InputMethodService private boolean isPredictionOn() { boolean predictionOn = mPredictionOn; - //if (isFullscreenMode()) predictionOn &= mPredictionLandscape; return predictionOn; } @@ -1419,6 +1440,8 @@ public class LatinIME extends InputMethodService TextEntryState.acceptedDefault(mWord.getTypedWord(), mBestWord); mJustAccepted = true; pickSuggestion(mBestWord); + // Add the word to the auto dictionary if it's not a known word + checkAddToDictionary(mBestWord, AutoDictionary.FREQUENCY_FOR_TYPED); } } @@ -1456,6 +1479,8 @@ public class LatinIME extends InputMethodService } mJustAccepted = true; pickSuggestion(suggestion); + // Add the word to the auto dictionary if it's not a known word + checkAddToDictionary(suggestion, AutoDictionary.FREQUENCY_FOR_PICKED); TextEntryState.acceptedSuggestion(mComposing.toString(), suggestion); // Follow it with a space if (mAutoSpace) { @@ -1464,7 +1489,7 @@ public class LatinIME extends InputMethodService } // Fool the state watcher so that a subsequent backspace will not do a revert TextEntryState.typedCharacter((char) KEYCODE_SPACE, true); - if (index == 0 && !mSuggest.isValidWord(suggestion)) { + if (index == 0 && mCorrectionMode > 0 && !mSuggest.isValidWord(suggestion)) { mCandidateView.showAddToDictionaryHint(suggestion); } if (ic != null) { @@ -1489,12 +1514,9 @@ public class LatinIME extends InputMethodService ic.commitText(suggestion, 1); } } - // Add the word to the auto dictionary if it's not a known word - if (mAutoDictionary.isValidWord(suggestion) || !mSuggest.isValidWord(suggestion)) { - mAutoDictionary.addWord(suggestion.toString(), FREQUENCY_FOR_PICKED); - } mPredicting = false; mCommittedLength = suggestion.length(); + ((LatinKeyboard) mInputView.getKeyboard()).setPreferredLetters(null); setNextSuggestions(); updateShiftKeyState(getCurrentInputEditorInfo()); } @@ -1503,6 +1525,13 @@ public class LatinIME extends InputMethodService setSuggestions(mSuggestPuncList, false, false, false); } + private void checkAddToDictionary(CharSequence suggestion, int frequencyDelta) { + if (mAutoDictionary.isValidWord(suggestion) + || !mSuggest.isValidWord(suggestion.toString().toLowerCase())) { + mAutoDictionary.addWord(suggestion.toString(), frequencyDelta); + } + } + private boolean isCursorTouchingWord() { InputConnection ic = getCurrentInputConnection(); if (ic == null) return false; @@ -1646,11 +1675,6 @@ public class LatinIME extends InputMethodService && !mVoiceInput.isBlacklistedField(fieldContext); } - private boolean fieldIsRecommendedForVoice(FieldContext fieldContext) { - // TODO: Move this logic into the VoiceInput method. - return !mPasswordText && !mEmailText && mVoiceInput.isRecommendedField(fieldContext); - } - private boolean shouldShowVoiceButton(FieldContext fieldContext, EditorInfo attribute) { return ENABLE_VOICE_BUTTON && fieldCanDoVoice(fieldContext) && !(attribute != null && attribute.privateImeOptions != null @@ -1681,21 +1705,6 @@ public class LatinIME extends InputMethodService > MIN_MILLIS_AFTER_TYPING_BEFORE_SWIPE; } - /* - * Only trigger a swipe action if the user hasn't typed X millis before - * now, and if they don't type Y millis after the swipe is detected. This - * delays the onset of the swipe action by Y millis. - */ - private void conservativelyTriggerSwipeAction(final Runnable action) { - if (userHasNotTypedRecently()) { - mSwipeTriggerTimeMillis = System.currentTimeMillis(); - mHandler.sendMessageDelayed( - mHandler.obtainMessage(MSG_START_LISTENING_AFTER_SWIPE), - MIN_MILLIS_AFTER_SWIPE_TO_WAIT_FOR_TYPING); - } - } - - private void playKeyClick(int primaryCode) { // if mAudioManager is null, we don't have the ringer state yet // mAudioManager will be set by updateRingerMode @@ -1759,18 +1768,28 @@ public class LatinIME extends InputMethodService mUserDictionary.addWord(word, frequency); } + WordComposer getCurrentWord() { + return mWord; + } + private void updateCorrectionMode() { mHasDictionary = mSuggest != null ? mSuggest.hasMainDictionary() : false; mAutoCorrectOn = (mAutoCorrectEnabled || mQuickFixes) && !mInputTypeNoAutoCorrect && mHasDictionary; - mCorrectionMode = mAutoCorrectOn + mCorrectionMode = (mAutoCorrectOn && mAutoCorrectEnabled) ? Suggest.CORRECTION_FULL - : (mQuickFixes ? Suggest.CORRECTION_BASIC : Suggest.CORRECTION_NONE); + : (mAutoCorrectOn ? Suggest.CORRECTION_BASIC : Suggest.CORRECTION_NONE); if (mSuggest != null) { mSuggest.setCorrectionMode(mCorrectionMode); } } + private void updateAutoTextEnabled(Locale systemLocale) { + if (mSuggest == null) return; + boolean different = !systemLocale.getLanguage().equalsIgnoreCase(mLocale.substring(0, 2)); + mSuggest.setAutoTextEnabled(!different && mQuickFixes); + } + protected void launchSettings() { launchSettings(LatinIMESettings.class); } @@ -1808,15 +1827,13 @@ public class LatinIME extends InputMethodService mLocaleSupportedForVoiceInput = voiceInputSupportedLocales.contains(mLocale); - // If there is no auto text data, then quickfix is forced to "on", so that the other options - // will continue to work - - if (AutoText.getSize(mInputView) < 1) mQuickFixes = true; - mShowSuggestions = sp.getBoolean(PREF_SHOW_SUGGESTIONS, true) & mQuickFixes; + mShowSuggestions = sp.getBoolean(PREF_SHOW_SUGGESTIONS, true); if (VOICE_INSTALLED) { - final String voiceMode = sp.getString(PREF_VOICE_MODE, ""); - boolean enableVoice = !voiceMode.equals(getString(R.string.voice_mode_off)); + 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)) { @@ -1828,6 +1845,7 @@ public class LatinIME extends InputMethodService mAutoCorrectEnabled = sp.getBoolean(PREF_AUTO_COMPLETE, mResources.getBoolean(R.bool.enable_autocorrect)) & mShowSuggestions; updateCorrectionMode(); + updateAutoTextEnabled(mResources.getConfiguration().locale); mLanguageSwitcher.loadLocales(sp); } @@ -1932,38 +1950,4 @@ public class LatinIME extends InputMethodService System.out.println("CPS = " + ((CPS_BUFFER_SIZE * 1000f) / total)); } - class AutoDictionary extends ExpandableDictionary { - // If the user touches a typed word 2 times or more, it will become valid. - private static final int VALIDITY_THRESHOLD = 2 * FREQUENCY_FOR_PICKED; - // If the user touches a typed word 5 times or more, it will be added to the user dict. - private static final int PROMOTION_THRESHOLD = 4 * FREQUENCY_FOR_PICKED; - - public AutoDictionary(Context context) { - super(context); - } - - @Override - public boolean isValidWord(CharSequence word) { - final int frequency = getWordFrequency(word); - return frequency >= VALIDITY_THRESHOLD; - } - - @Override - public void addWord(String word, int addFrequency) { - final int length = word.length(); - // Don't add very short or very long words. - if (length < 2 || length > getMaxWordLength()) return; - if (mWord.isAutoCapitalized()) { - // Remove caps before adding - word = Character.toLowerCase(word.charAt(0)) - + word.substring(1); - } - int freq = getWordFrequency(word); - freq = freq < 0 ? addFrequency : freq + addFrequency; - super.addWord(word, freq); - if (freq >= PROMOTION_THRESHOLD) { - LatinIME.this.promoteToUserDictionary(word, FREQUENCY_FOR_AUTO_ADD); - } - } - } } |