diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LatinIME.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/LatinIME.java | 213 |
1 files changed, 106 insertions, 107 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 99b6c739a..243306a35 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -63,6 +63,7 @@ import android.view.ViewParent; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.CompletionInfo; +import android.view.inputmethod.CorrectionInfo; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.ExtractedText; import android.view.inputmethod.ExtractedTextRequest; @@ -77,7 +78,6 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import java.util.Locale; /** @@ -120,7 +120,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private View mCandidateViewContainer; private CandidateView mCandidateView; private Suggest mSuggest; - private CompletionInfo[] mCompletions; + private CompletionInfo[] mApplicationSpecifiedCompletions; private AlertDialog mOptionsDialog; @@ -142,7 +142,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private CharSequence mBestWord; private boolean mPredicting; private boolean mPredictionOn; - private boolean mCompletionOn; + private boolean mApplicationSpecifiedCompletionOn; private boolean mHasDictionary; private boolean mAutoSpace; private boolean mJustAddedAutoSpace; @@ -162,7 +162,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Keep track of the last selection range to decide if we need to show word alternatives private int mLastSelectionStart; private int mLastSelectionEnd; - private List<CharSequence> mSuggestPuncList; + private SuggestedWords mSuggestPuncList; // Input type is such that we should not auto-correct private boolean mInputTypeNoAutoCorrect; @@ -212,7 +212,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen return mChosenWord; } - public abstract List<CharSequence> getAlternatives(); + public abstract SuggestedWords.Builder getAlternatives(); } public class TypedWordAlternatives extends WordAlternatives { @@ -233,7 +233,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } @Override - public List<CharSequence> getAlternatives() { + public SuggestedWords.Builder getAlternatives() { return getTypedSuggestions(word); } } @@ -394,7 +394,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen int[] dictionaries = getDictionary(orig); mSuggest = new Suggest(this, dictionaries); - loadAndSetAutoCompletionThreshold(prefs); + loadAndSetAutoCorrectionThreshold(prefs); if (mUserDictionary != null) mUserDictionary.close(); mUserDictionary = new UserDictionary(this, locale); if (mContactsDictionary == null) { @@ -520,8 +520,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mVoiceConnector.resetVoiceStates(isPasswordVariation(variation)); mInputTypeNoAutoCorrect = false; mPredictionOn = false; - mCompletionOn = false; - mCompletions = null; + mApplicationSpecifiedCompletionOn = false; + mApplicationSpecifiedCompletions = null; mEnteredText = null; final int mode; @@ -580,7 +580,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } if ((attribute.inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) { mPredictionOn = false; - mCompletionOn = isFullscreenMode(); + mApplicationSpecifiedCompletionOn = isFullscreenMode(); } break; default: @@ -603,7 +603,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen setCandidatesViewShownInternal(isCandidateStripVisible(), false /* needsInputViewShown */ ); - updateSuggestions(); + // Delay updating suggestions because keyboard input view may not be shown at this point. + mHandler.postUpdateSuggestions(); // If the dictionary is not big enough, don't auto correct mHasDictionary = mSuggest.hasMainDictionary(); @@ -633,7 +634,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (isSuggestionShown() && isPredictionOn()) { // First get the cursor position. This is required by setOldSuggestions(), so that // it can pass the correct range to setComposingRegion(). At this point, we don't - // have valid values for mLastSelectionStart/Stop because onUpdateSelection() has + // have valid values for mLastSelectionStart/End because onUpdateSelection() has // not been called yet. ExtractedTextRequest etr = new ExtractedTextRequest(); etr.token = 0; // anything is fine here @@ -655,7 +656,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen super.onFinishInput(); LatinImeLogger.commit(); - onAutoCompletionStateChanged(false); + mKeyboardSwitcher.onAutoCorrectionStateChanged(false); mVoiceConnector.flushVoiceInputLogs(mConfigurationChanging); @@ -790,7 +791,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen @Override public void hideWindow() { LatinImeLogger.commit(); - onAutoCompletionStateChanged(false); + mKeyboardSwitcher.onAutoCorrectionStateChanged(false); if (TRACE) Debug.stopMethodTracing(); if (mOptionsDialog != null && mOptionsDialog.isShowing()) { @@ -804,27 +805,28 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } @Override - public void onDisplayCompletions(CompletionInfo[] completions) { + public void onDisplayCompletions(CompletionInfo[] applicationSpecifiedCompletions) { if (DEBUG) { Log.i("foo", "Received completions:"); - for (int i=0; i<(completions != null ? completions.length : 0); i++) { - Log.i("foo", " #" + i + ": " + completions[i]); + final int count = (applicationSpecifiedCompletions != null) + ? applicationSpecifiedCompletions.length : 0; + for (int i = 0; i < count; i++) { + Log.i("foo", " #" + i + ": " + applicationSpecifiedCompletions[i]); } } - if (mCompletionOn) { - mCompletions = completions; - if (completions == null) { + if (mApplicationSpecifiedCompletionOn) { + mApplicationSpecifiedCompletions = applicationSpecifiedCompletions; + if (applicationSpecifiedCompletions == null) { clearSuggestions(); return; } - List<CharSequence> stringList = new ArrayList<CharSequence>(); - for (int i = 0; i < completions.length; i++) { - CompletionInfo ci = completions[i]; - if (ci != null) stringList.add(ci.getText()); - } + SuggestedWords.Builder builder = new SuggestedWords.Builder() + .setApplicationSpecifiedCompletions(applicationSpecifiedCompletions) + .setTypedWordValid(true) + .setHasMinimalSuggestion(true); // When in fullscreen mode, show completions generated by the application - setSuggestions(stringList, true, true, true); + setSuggestions(builder.build()); mBestWord = null; setCandidatesViewShown(true); } @@ -1293,7 +1295,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen boolean pickedDefault = false; // Handle separator - InputConnection ic = getCurrentInputConnection(); + final InputConnection ic = getCurrentInputConnection(); if (ic != null) { ic.beginBatchEdit(); abortCorrection(false); @@ -1338,7 +1340,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen CharSequence typedWord = mWord.getTypedWord(); TextEntryState.backToAcceptedDefault(typedWord); if (!TextUtils.isEmpty(typedWord) && !typedWord.equals(mBestWord)) { - // TODO: Will call InputConnection.commitCorrection() here. + if (ic != null) { + CorrectionInfo correctionInfo = new CorrectionInfo( + mLastSelectionEnd - typedWord.length(), typedWord, mBestWord); + ic.commitCorrection(correctionInfo); + } if (mCandidateView != null) mCandidateView.onAutoCorrectionInverted(mBestWord); } @@ -1382,7 +1388,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private boolean isShowingPunctuationList() { - return mSuggestPuncList.equals(mCandidateView.getSuggestions()); + return mSuggestPuncList == mCandidateView.getSuggestions(); } private boolean isSuggestionShown() { @@ -1394,8 +1400,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private boolean isCandidateStripVisible() { boolean forceVisible = mCandidateView.isShowingAddToDictionaryHint() || TextEntryState.isCorrecting(); - return forceVisible || (isSuggestionShown() - && (isPredictionOn() || mCompletionOn || isShowingPunctuationList())); + return forceVisible || ( + isSuggestionShown() && (isPredictionOn() || mApplicationSpecifiedCompletionOn + || isShowingPunctuationList())); } public void switchToKeyboardView() { @@ -1422,22 +1429,20 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } public void clearSuggestions() { - setSuggestions(null, false, false, false); + setSuggestions(SuggestedWords.EMPTY); } - public void setSuggestions( - List<CharSequence> suggestions, - boolean completions, - boolean typedWordValid, - boolean haveMinimalSuggestion) { - + public void setSuggestions(SuggestedWords words) { if (mVoiceConnector.getAndResetIsShowingHint()) { setCandidatesView(mCandidateViewContainer); } if (mCandidateView != null) { - mCandidateView.setSuggestions( - suggestions, completions, typedWordValid, haveMinimalSuggestion); + mCandidateView.setSuggestions(words); + if (mCandidateView.isConfigCandidateHighlightFontColorEnabled()) { + mKeyboardSwitcher.onAutoCorrectionStateChanged( + words.hasWordAboveAutoCorrectionScoreThreshold()); + } } } @@ -1457,16 +1462,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen showSuggestions(mWord); } - private List<CharSequence> getTypedSuggestions(WordComposer word) { - List<CharSequence> stringList = mSuggest.getSuggestions( - mKeyboardSwitcher.getInputView(), word, false, null); - return stringList; + private SuggestedWords.Builder getTypedSuggestions(WordComposer word) { + return mSuggest.getSuggestedWordBuilder(mKeyboardSwitcher.getInputView(), word, null); } private void showCorrections(WordAlternatives alternatives) { mKeyboardSwitcher.setPreferredLetters(null); - List<CharSequence> stringList = alternatives.getAlternatives(); - showSuggestions(stringList, alternatives.getOriginalWord(), false, false); + SuggestedWords.Builder builder = alternatives.getAlternatives(); + builder.setTypedWordValid(false).setHasMinimalSuggestion(false); + showSuggestions(builder.build(), alternatives.getOriginalWord()); } private void showSuggestions(WordComposer word) { @@ -1474,10 +1478,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // TODO Maybe need better way of retrieving previous word CharSequence prevWord = EditingUtils.getPreviousWord(getCurrentInputConnection(), mWordSeparators); - List<CharSequence> stringList = mSuggest.getSuggestions( - mKeyboardSwitcher.getInputView(), word, false, prevWord); - // long stopTime = System.currentTimeMillis(); // TIME MEASUREMENT! - // Log.d("LatinIME","Suggest Total Time - " + (stopTime - startTime)); + SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder( + mKeyboardSwitcher.getInputView(), word, prevWord); int[] nextLettersFrequencies = mSuggest.getNextLettersFrequencies(); mKeyboardSwitcher.setPreferredLetters(nextLettersFrequencies); @@ -1497,15 +1499,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen correctionAvailable &= !word.isMostlyCaps(); correctionAvailable &= !TextEntryState.isCorrecting(); - showSuggestions(stringList, typedWord, typedWordValid, correctionAvailable); + builder.setTypedWordValid(typedWordValid).setHasMinimalSuggestion(correctionAvailable); + showSuggestions(builder.build(), typedWord); } - private void showSuggestions(List<CharSequence> stringList, CharSequence typedWord, - boolean typedWordValid, boolean correctionAvailable) { - setSuggestions(stringList, false, typedWordValid, correctionAvailable); - if (stringList.size() > 0) { - if (correctionAvailable && !typedWordValid && stringList.size() > 1) { - mBestWord = stringList.get(1); + private void showSuggestions(SuggestedWords suggestedWords, CharSequence typedWord) { + setSuggestions(suggestedWords); + if (suggestedWords.size() > 0) { + if (suggestedWords.hasAutoCorrectionWord()) { + mBestWord = suggestedWords.getWord(1); } else { mBestWord = typedWord; } @@ -1534,7 +1536,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } public void pickSuggestionManually(int index, CharSequence suggestion) { - List<CharSequence> suggestions = mCandidateView.getSuggestions(); + SuggestedWords suggestions = mCandidateView.getSuggestions(); mVoiceConnector.flushAndLogAllTextModificationCounters(index, suggestion, mWordSeparators); final boolean correcting = TextEntryState.isCorrecting(); @@ -1542,9 +1544,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (ic != null) { ic.beginBatchEdit(); } - if (mCompletionOn && mCompletions != null && index >= 0 - && index < mCompletions.length) { - CompletionInfo ci = mCompletions[index]; + if (mApplicationSpecifiedCompletionOn && mApplicationSpecifiedCompletions != null + && index >= 0 && index < mApplicationSpecifiedCompletions.length) { + CompletionInfo ci = mApplicationSpecifiedCompletions[index]; if (ic != null) { ic.commitCompletion(ci); } @@ -1565,7 +1567,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Word separators are suggested before the user inputs something. // So, LatinImeLogger logs "" as a user's input. LatinImeLogger.logOnManualSuggestion( - "", suggestion.toString(), index, suggestions); + "", suggestion.toString(), index, suggestions.mWords); final char primaryCode = suggestion.charAt(0); onKey(primaryCode, new int[]{primaryCode}, KeyboardView.NOT_A_TOUCH_COORDINATE, KeyboardView.NOT_A_TOUCH_COORDINATE); @@ -1583,7 +1585,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen addToBigramDictionary(suggestion, 1); } LatinImeLogger.logOnManualSuggestion(mComposing.toString(), suggestion.toString(), - index, suggestions); + index, suggestions.mWords); TextEntryState.acceptedSuggestion(mComposing.toString(), suggestion); // Follow it with a space if (mAutoSpace && !correcting) { @@ -1655,7 +1657,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen break; } } - // If we didn't find a match, at least suggest completions + // If we didn't find a match, at least suggest corrections. if (foundWord == null && (mSuggest.isValidWord(touching.mWord) || mSuggest.isValidWord(touching.mWord.toString().toLowerCase()))) { @@ -1718,7 +1720,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private void setPunctuationSuggestions() { setCandidatesViewShown(isCandidateStripVisible()); - setSuggestions(mSuggestPuncList, false, false, false); + setSuggestions(mSuggestPuncList); } private void addToDictionaries(CharSequence suggestion, int frequencyDelta) { @@ -2043,7 +2045,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mAutoCorrectEnabled = isAutoCorrectEnabled(prefs); mBigramSuggestionEnabled = mAutoCorrectEnabled && isBigramSuggestionEnabled(prefs); - loadAndSetAutoCompletionThreshold(prefs); + loadAndSetAutoCorrectionThreshold(prefs); mVoiceConnector.loadSettings(attribute, prefs); @@ -2054,50 +2056,47 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } /** - * load Auto completion threshold from SharedPreferences, - * and modify mSuggest's threshold. + * Load Auto correction threshold from SharedPreferences, and modify mSuggest's threshold. */ - private void loadAndSetAutoCompletionThreshold(SharedPreferences sp) { + private void loadAndSetAutoCorrectionThreshold(SharedPreferences sp) { // When mSuggest is not initialized, cannnot modify mSuggest's threshold. if (mSuggest == null) return; - // When auto completion setting is turned off, the threshold is ignored. + // When auto correction setting is turned off, the threshold is ignored. if (!isAutoCorrectEnabled(sp)) return; - final String currentAutoCompletionSetting = sp.getString( - Settings.PREF_AUTO_COMPLETION_THRESHOLD, - mResources.getString(R.string.auto_completion_threshold_mode_value_modest)); - final String[] autoCompletionThresholdValues = mResources.getStringArray( - R.array.auto_complete_threshold_values); - // When autoCompletionThreshold is greater than 1.0, - // auto completion is virtually turned off. - double autoCompletionThreshold = Double.MAX_VALUE; + final String currentAutoCorrectionSetting = sp.getString( + Settings.PREF_AUTO_CORRECTION_THRESHOLD, + mResources.getString(R.string.auto_correction_threshold_mode_index_modest)); + final String[] autoCorrectionThresholdValues = mResources.getStringArray( + R.array.auto_correction_threshold_values); + // When autoCrrectionThreshold is greater than 1.0, auto correction is virtually turned off. + double autoCorrectionThreshold = Double.MAX_VALUE; try { - final int arrayIndex = Integer.valueOf(currentAutoCompletionSetting); - if (arrayIndex >= 0 && arrayIndex < autoCompletionThresholdValues.length) { - autoCompletionThreshold = Double.parseDouble( - autoCompletionThresholdValues[arrayIndex]); + final int arrayIndex = Integer.valueOf(currentAutoCorrectionSetting); + if (arrayIndex >= 0 && arrayIndex < autoCorrectionThresholdValues.length) { + autoCorrectionThreshold = Double.parseDouble( + autoCorrectionThresholdValues[arrayIndex]); } } catch (NumberFormatException e) { - // Whenever the threshold settings are correct, - // never come here. - autoCompletionThreshold = Double.MAX_VALUE; - Log.w(TAG, "Cannot load auto completion threshold setting." - + " currentAutoCompletionSetting: " + currentAutoCompletionSetting - + ", autoCompletionThresholdValues: " - + Arrays.toString(autoCompletionThresholdValues)); + // Whenever the threshold settings are correct, never come here. + autoCorrectionThreshold = Double.MAX_VALUE; + Log.w(TAG, "Cannot load auto correction threshold setting." + + " currentAutoCorrectionSetting: " + currentAutoCorrectionSetting + + ", autoCorrectionThresholdValues: " + + Arrays.toString(autoCorrectionThresholdValues)); } // TODO: This should be refactored : - // setAutoCompleteThreshold should be called outside of this method. - mSuggest.setAutoCompleteThreshold(autoCompletionThreshold); + // setAutoCorrectionThreshold should be called outside of this method. + mSuggest.setAutoCorrectionThreshold(autoCorrectionThreshold); } private boolean isAutoCorrectEnabled(SharedPreferences sp) { - final String currentAutoCompletionSetting = sp.getString( - Settings.PREF_AUTO_COMPLETION_THRESHOLD, - mResources.getString(R.string.auto_completion_threshold_mode_value_modest)); - final String autoCompletionOff = mResources.getString( - R.string.auto_completion_threshold_mode_value_off); - return !currentAutoCompletionSetting.equals(autoCompletionOff); + final String currentAutoCorrectionSetting = sp.getString( + Settings.PREF_AUTO_CORRECTION_THRESHOLD, + mResources.getString(R.string.auto_correction_threshold_mode_index_modest)); + final String autoCorrectionOff = mResources.getString( + R.string.auto_correction_threshold_mode_index_off); + return !currentAutoCorrectionSetting.equals(autoCorrectionOff); } private boolean isBigramSuggestionEnabled(SharedPreferences sp) { @@ -2106,13 +2105,17 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private void initSuggestPuncList() { - mSuggestPuncList = new ArrayList<CharSequence>(); - mSuggestPuncs = mResources.getString(R.string.suggested_punctuations); - if (mSuggestPuncs != null) { - for (int i = 0; i < mSuggestPuncs.length(); i++) { - mSuggestPuncList.add(mSuggestPuncs.subSequence(i, i + 1)); + if (mSuggestPuncs != null || mSuggestPuncList != null) + return; + SuggestedWords.Builder builder = new SuggestedWords.Builder(); + String puncs = mResources.getString(R.string.suggested_punctuations); + if (puncs != null) { + for (int i = 0; i < puncs.length(); i++) { + builder.addWord(puncs.subSequence(i, i + 1)); } } + mSuggestPuncList = builder.build(); + mSuggestPuncs = puncs; } private boolean isSuggestedPunctuation(int code) { @@ -2167,7 +2170,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen p.println(" mPredicting=" + mPredicting); p.println(" mAutoCorrectOn=" + mAutoCorrectOn); p.println(" mAutoSpace=" + mAutoSpace); - p.println(" mCompletionOn=" + mCompletionOn); + p.println(" mApplicationSpecifiedCompletionOn=" + mApplicationSpecifiedCompletionOn); p.println(" TextEntryState.state=" + TextEntryState.getState()); p.println(" mSoundOn=" + mSoundOn); p.println(" mVibrateOn=" + mVibrateOn); @@ -2192,10 +2195,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen System.out.println("CPS = " + ((CPS_BUFFER_SIZE * 1000f) / total)); } - public void onAutoCompletionStateChanged(boolean isAutoCompletion) { - mKeyboardSwitcher.onAutoCompletionStateChanged(isAutoCompletion); - } - @Override public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) { SubtypeSwitcher.getInstance().updateSubtype(subtype); |