Add onGetSuggestedWordsCallback.

Change-Id: Ib527d31f9c2dd6dc385cb2f767cd90d9d28a1b1b
main
Yuichiro Hanada 2013-08-29 15:15:49 +09:00
parent ab4c846b7f
commit 820c290a61
2 changed files with 121 additions and 76 deletions

View File

@ -71,6 +71,7 @@ import com.android.inputmethod.keyboard.KeyboardActionListener;
import com.android.inputmethod.keyboard.KeyboardId; import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.MainKeyboardView; import com.android.inputmethod.keyboard.MainKeyboardView;
import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.personalization.PersonalizationDictionary; import com.android.inputmethod.latin.personalization.PersonalizationDictionary;
@ -1741,9 +1742,13 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// Batch input has ended or canceled while the message was being delivered. // Batch input has ended or canceled while the message was being delivered.
return; return;
} }
final SuggestedWords suggestedWords = getSuggestedWordsGestureLocked(batchPointers);
getSuggestedWordsGestureLocked(batchPointers, new OnGetSuggestedWordsCallback() {
@Override
public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip( mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(
suggestedWords, false /* dismissGestureFloatingPreviewText */); suggestedWords, false /* dismissGestureFloatingPreviewText */);
}});
} }
} }
@ -1766,29 +1771,39 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} }
// Run in the UI thread. // Run in the UI thread.
public SuggestedWords onEndBatchInput(final InputPointers batchPointers) { public void onEndBatchInput(final InputPointers batchPointers) {
getSuggestedWordsGestureLocked(batchPointers, new OnGetSuggestedWordsCallback() {
@Override
public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
synchronized (mLock) { synchronized (mLock) {
mInBatchInput = false; mInBatchInput = false;
final SuggestedWords suggestedWords = getSuggestedWordsGestureLocked(batchPointers); mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(suggestedWords,
mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip( true /* dismissGestureFloatingPreviewText */);
suggestedWords, true /* dismissGestureFloatingPreviewText */);
return suggestedWords;
} }
mLatinIme.onEndBatchInputAsyncInternal(suggestedWords);
}
});
} }
// {@link LatinIME#getSuggestedWords(int)} method calls with same session id have to // {@link LatinIME#getSuggestedWords(int)} method calls with same session id have to
// be synchronized. // be synchronized.
private SuggestedWords getSuggestedWordsGestureLocked(final InputPointers batchPointers) { private void getSuggestedWordsGestureLocked(final InputPointers batchPointers,
final OnGetSuggestedWordsCallback callback) {
mLatinIme.mWordComposer.setBatchInputPointers(batchPointers); mLatinIme.mWordComposer.setBatchInputPointers(batchPointers);
final SuggestedWords suggestedWords = mLatinIme.getSuggestedWordsOrOlderSuggestions(Suggest.SESSION_GESTURE,
mLatinIme.getSuggestedWordsOrOlderSuggestions(Suggest.SESSION_GESTURE); new OnGetSuggestedWordsCallback() {
@Override
public void onGetSuggestedWords(SuggestedWords suggestedWords) {
final int suggestionCount = suggestedWords.size(); final int suggestionCount = suggestedWords.size();
if (suggestionCount <= 1) { if (suggestionCount <= 1) {
final String mostProbableSuggestion = (suggestionCount == 0) ? null final String mostProbableSuggestion = (suggestionCount == 0) ? null
: suggestedWords.getWord(0); : suggestedWords.getWord(0);
return mLatinIme.getOlderSuggestions(mostProbableSuggestion); callback.onGetSuggestedWords(
mLatinIme.getOlderSuggestions(mostProbableSuggestion));
} }
return suggestedWords; callback.onGetSuggestedWords(suggestedWords);
}
});
} }
} }
@ -1813,10 +1828,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
BatchInputUpdater.getInstance().onUpdateBatchInput(batchPointers); BatchInputUpdater.getInstance().onUpdateBatchInput(batchPointers);
} }
@Override public void onEndBatchInputAsyncInternal(final SuggestedWords suggestedWords) {
public void onEndBatchInput(final InputPointers batchPointers) {
final SuggestedWords suggestedWords = BatchInputUpdater.getInstance().onEndBatchInput(
batchPointers);
final String batchInputText = suggestedWords.isEmpty() final String batchInputText = suggestedWords.isEmpty()
? null : suggestedWords.getWord(0); ? null : suggestedWords.getWord(0);
if (TextUtils.isEmpty(batchInputText)) { if (TextUtils.isEmpty(batchInputText)) {
@ -1838,6 +1850,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mKeyboardSwitcher.updateShiftState(); mKeyboardSwitcher.updateShiftState();
} }
@Override
public void onEndBatchInput(final InputPointers batchPointers) {
BatchInputUpdater.getInstance().onEndBatchInput(batchPointers);
}
private String specificTldProcessingOnTextInput(final String text) { private String specificTldProcessingOnTextInput(final String text) {
if (text.length() <= 1 || text.charAt(0) != Constants.CODE_PERIOD if (text.length() <= 1 || text.charAt(0) != Constants.CODE_PERIOD
|| !Character.isLetter(text.charAt(1))) { || !Character.isLetter(text.charAt(1))) {
@ -2308,17 +2325,23 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
return; return;
} }
final SuggestedWords suggestedWords =
getSuggestedWordsOrOlderSuggestions(Suggest.SESSION_TYPING);
final String typedWord = mWordComposer.getTypedWord(); final String typedWord = mWordComposer.getTypedWord();
getSuggestedWordsOrOlderSuggestions(Suggest.SESSION_TYPING,
new OnGetSuggestedWordsCallback() {
@Override
public void onGetSuggestedWords(SuggestedWords suggestedWords) {
showSuggestionStrip(suggestedWords, typedWord); showSuggestionStrip(suggestedWords, typedWord);
} }
});
}
private SuggestedWords getSuggestedWords(final int sessionId) { private void getSuggestedWords(final int sessionId,
final OnGetSuggestedWordsCallback callback) {
final Keyboard keyboard = mKeyboardSwitcher.getKeyboard(); final Keyboard keyboard = mKeyboardSwitcher.getKeyboard();
final Suggest suggest = mSuggest; final Suggest suggest = mSuggest;
if (keyboard == null || suggest == null) { if (keyboard == null || suggest == null) {
return SuggestedWords.EMPTY; callback.onGetSuggestedWords(SuggestedWords.EMPTY);
return;
} }
// Get the word on which we should search the bigrams. If we are composing a word, it's // Get the word on which we should search the bigrams. If we are composing a word, it's
// whatever is *before* the half-committed word in the buffer, hence 2; if we aren't, we // whatever is *before* the half-committed word in the buffer, hence 2; if we aren't, we
@ -2335,14 +2358,20 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
prevWord = LastComposedWord.NOT_A_COMPOSED_WORD == mLastComposedWord ? null prevWord = LastComposedWord.NOT_A_COMPOSED_WORD == mLastComposedWord ? null
: mLastComposedWord.mCommittedWord; : mLastComposedWord.mCommittedWord;
} }
return suggest.getSuggestedWords(mWordComposer, prevWord, keyboard.getProximityInfo(), suggest.getSuggestedWords(mWordComposer, prevWord, keyboard.getProximityInfo(),
currentSettings.mBlockPotentiallyOffensive, currentSettings.mBlockPotentiallyOffensive, currentSettings.mCorrectionEnabled,
currentSettings.mCorrectionEnabled, additionalFeaturesOptions, sessionId); additionalFeaturesOptions, sessionId, callback);
} }
private SuggestedWords getSuggestedWordsOrOlderSuggestions(final int sessionId) { private void getSuggestedWordsOrOlderSuggestions(final int sessionId,
return maybeRetrieveOlderSuggestions(mWordComposer.getTypedWord(), final OnGetSuggestedWordsCallback callback) {
getSuggestedWords(sessionId)); getSuggestedWords(sessionId, new OnGetSuggestedWordsCallback() {
@Override
public void onGetSuggestedWords(SuggestedWords suggestedWords) {
callback.onGetSuggestedWords(maybeRetrieveOlderSuggestions(
mWordComposer.getTypedWord(), suggestedWords));
}
});
} }
private SuggestedWords maybeRetrieveOlderSuggestions(final String typedWord, private SuggestedWords maybeRetrieveOlderSuggestions(final String typedWord,
@ -2640,39 +2669,49 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mConnection.setComposingRegion( mConnection.setComposingRegion(
mLastSelectionStart - numberOfCharsInWordBeforeCursor, mLastSelectionStart - numberOfCharsInWordBeforeCursor,
mLastSelectionEnd + range.getNumberOfCharsInWordAfterCursor()); mLastSelectionEnd + range.getNumberOfCharsInWordAfterCursor());
final SuggestedWords suggestedWords;
if (suggestions.isEmpty()) { if (suggestions.isEmpty()) {
// We come here if there weren't any suggestion spans on this word. We will try to // We come here if there weren't any suggestion spans on this word. We will try to
// compute suggestions for it instead. // compute suggestions for it instead.
final SuggestedWords suggestedWordsIncludingTypedWord = getSuggestedWords(Suggest.SESSION_TYPING, new OnGetSuggestedWordsCallback() {
getSuggestedWords(Suggest.SESSION_TYPING); @Override
public void onGetSuggestedWords(SuggestedWords suggestedWordsIncludingTypedWord) {
final SuggestedWords suggestedWords;
if (suggestedWordsIncludingTypedWord.size() > 1) { if (suggestedWordsIncludingTypedWord.size() > 1) {
// We were able to compute new suggestions for this word. // We were able to compute new suggestions for this word.
// Remove the typed word, since we don't want to display it in this case. // Remove the typed word, since we don't want to display it in this case.
// The #getSuggestedWordsExcludingTypedWord() method sets willAutoCorrect to false. // The #getSuggestedWordsExcludingTypedWord() method sets willAutoCorrect to
suggestedWords = // false.
suggestedWordsIncludingTypedWord.getSuggestedWordsExcludingTypedWord(); suggestedWords = suggestedWordsIncludingTypedWord
.getSuggestedWordsExcludingTypedWord();
} else { } else {
// No saved suggestions, and we were unable to compute any good one either. // No saved suggestions, and we were unable to compute any good one either.
// Rather than displaying an empty suggestion strip, we'll display the original // Rather than displaying an empty suggestion strip, we'll display the
// word alone in the middle. // original word alone in the middle.
// Since there is only one word, willAutoCorrect is false. // Since there is only one word, willAutoCorrect is false.
suggestedWords = suggestedWordsIncludingTypedWord; suggestedWords = suggestedWordsIncludingTypedWord;
} }
unsetIsAutoCorrectionIndicatorOnAndCallShowSuggestionStrip(suggestedWords,
typedWord);
}});
} else { } else {
// We found suggestion spans in the word. We'll create the SuggestedWords out of // We found suggestion spans in the word. We'll create the SuggestedWords out of
// them, and make willAutoCorrect false. // them, and make willAutoCorrect false.
suggestedWords = new SuggestedWords(suggestions, final SuggestedWords suggestedWords = new SuggestedWords(suggestions,
true /* typedWordValid */, false /* willAutoCorrect */, true /* typedWordValid */, false /* willAutoCorrect */,
false /* isPunctuationSuggestions */, false /* isObsoleteSuggestions */, false /* isPunctuationSuggestions */, false /* isObsoleteSuggestions */,
false /* isPrediction */); false /* isPrediction */);
unsetIsAutoCorrectionIndicatorOnAndCallShowSuggestionStrip(suggestedWords, typedWord);
}
} }
public void unsetIsAutoCorrectionIndicatorOnAndCallShowSuggestionStrip(
final SuggestedWords suggestedWords, final String typedWord) {
// Note that it's very important here that suggestedWords.mWillAutoCorrect is false. // Note that it's very important here that suggestedWords.mWillAutoCorrect is false.
// We never want to auto-correct on a resumed suggestion. Please refer to the three // We never want to auto-correct on a resumed suggestion. Please refer to the three places
// places above where suggestedWords is affected. We also need to reset // above in restartSuggestionsOnWordTouchedByCursor() where suggestedWords is affected.
// mIsAutoCorrectionIndicatorOn to avoid showSuggestionStrip touching the text to adapt it. // We also need to unset mIsAutoCorrectionIndicatorOn to avoid showSuggestionStrip touching
// TODO: remove mIsAutoCorrectionIndicator on (see comment on definition) // the text to adapt it.
// TODO: remove mIsAutoCorrectionIndicatorOn (see comment on definition)
mIsAutoCorrectionIndicatorOn = false; mIsAutoCorrectionIndicatorOn = false;
showSuggestionStrip(suggestedWords, typedWord); showSuggestionStrip(suggestedWords, typedWord);
} }

View File

@ -211,26 +211,31 @@ public final class Suggest {
mAutoCorrectionThreshold = threshold; mAutoCorrectionThreshold = threshold;
} }
public SuggestedWords getSuggestedWords(final WordComposer wordComposer, public interface OnGetSuggestedWordsCallback {
public void onGetSuggestedWords(final SuggestedWords suggestedWords);
}
public void getSuggestedWords(final WordComposer wordComposer,
final String prevWordForBigram, final ProximityInfo proximityInfo, final String prevWordForBigram, final ProximityInfo proximityInfo,
final boolean blockOffensiveWords, final boolean isCorrectionEnabled, final boolean blockOffensiveWords, final boolean isCorrectionEnabled,
final int[] additionalFeaturesOptions, final int sessionId) { final int[] additionalFeaturesOptions, final int sessionId,
final OnGetSuggestedWordsCallback callback) {
LatinImeLogger.onStartSuggestion(prevWordForBigram); LatinImeLogger.onStartSuggestion(prevWordForBigram);
if (wordComposer.isBatchMode()) { if (wordComposer.isBatchMode()) {
return getSuggestedWordsForBatchInput( getSuggestedWordsForBatchInput(wordComposer, prevWordForBigram, proximityInfo,
wordComposer, prevWordForBigram, proximityInfo, blockOffensiveWords, blockOffensiveWords, additionalFeaturesOptions, sessionId, callback);
additionalFeaturesOptions, sessionId);
} else { } else {
return getSuggestedWordsForTypingInput(wordComposer, prevWordForBigram, proximityInfo, getSuggestedWordsForTypingInput(wordComposer, prevWordForBigram, proximityInfo,
blockOffensiveWords, isCorrectionEnabled, additionalFeaturesOptions); blockOffensiveWords, isCorrectionEnabled, additionalFeaturesOptions, callback);
} }
} }
// Retrieves suggestions for the typing input. // Retrieves suggestions for the typing input
private SuggestedWords getSuggestedWordsForTypingInput(final WordComposer wordComposer, // and calls the callback function with the suggestions.
private void getSuggestedWordsForTypingInput(final WordComposer wordComposer,
final String prevWordForBigram, final ProximityInfo proximityInfo, final String prevWordForBigram, final ProximityInfo proximityInfo,
final boolean blockOffensiveWords, final boolean isCorrectionEnabled, final boolean blockOffensiveWords, final boolean isCorrectionEnabled,
final int[] additionalFeaturesOptions) { final int[] additionalFeaturesOptions, final OnGetSuggestedWordsCallback callback) {
final int trailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount(); final int trailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount();
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator, final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
MAX_SUGGESTIONS); MAX_SUGGESTIONS);
@ -253,8 +258,8 @@ public final class Suggest {
for (final String key : mDictionaries.keySet()) { for (final String key : mDictionaries.keySet()) {
final Dictionary dictionary = mDictionaries.get(key); final Dictionary dictionary = mDictionaries.get(key);
suggestionsSet.addAll(dictionary.getSuggestions( suggestionsSet.addAll(dictionary.getSuggestions(wordComposerForLookup,
wordComposerForLookup, prevWordForBigram, proximityInfo, blockOffensiveWords, prevWordForBigram, proximityInfo, blockOffensiveWords,
additionalFeaturesOptions)); additionalFeaturesOptions));
} }
@ -332,7 +337,7 @@ public final class Suggest {
suggestionsList = suggestionsContainer; suggestionsList = suggestionsContainer;
} }
return new SuggestedWords(suggestionsList, callback.onGetSuggestedWords(new SuggestedWords(suggestionsList,
// TODO: this first argument is lying. If this is a whitelisted word which is an // TODO: this first argument is lying. If this is a whitelisted word which is an
// actual word, it says typedWordValid = false, which looks wrong. We should either // actual word, it says typedWordValid = false, which looks wrong. We should either
// rename the attribute or change the value. // rename the attribute or change the value.
@ -340,14 +345,15 @@ public final class Suggest {
hasAutoCorrection, /* willAutoCorrect */ hasAutoCorrection, /* willAutoCorrect */
false /* isPunctuationSuggestions */, false /* isPunctuationSuggestions */,
false /* isObsoleteSuggestions */, false /* isObsoleteSuggestions */,
!wordComposer.isComposingWord() /* isPrediction */); !wordComposer.isComposingWord() /* isPrediction */));
} }
// Retrieves suggestions for the batch input. // Retrieves suggestions for the batch input
private SuggestedWords getSuggestedWordsForBatchInput(final WordComposer wordComposer, // and calls the callback function with the suggestions.
private void getSuggestedWordsForBatchInput(final WordComposer wordComposer,
final String prevWordForBigram, final ProximityInfo proximityInfo, final String prevWordForBigram, final ProximityInfo proximityInfo,
final boolean blockOffensiveWords, final int[] additionalFeaturesOptions, final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
final int sessionId) { final int sessionId, final OnGetSuggestedWordsCallback callback) {
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator, final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
MAX_SUGGESTIONS); MAX_SUGGESTIONS);
@ -401,12 +407,12 @@ public final class Suggest {
// In the batch input mode, the most relevant suggested word should act as a "typed word" // In the batch input mode, the most relevant suggested word should act as a "typed word"
// (typedWordValid=true), not as an "auto correct word" (willAutoCorrect=false). // (typedWordValid=true), not as an "auto correct word" (willAutoCorrect=false).
return new SuggestedWords(suggestionsContainer, callback.onGetSuggestedWords(new SuggestedWords(suggestionsContainer,
true /* typedWordValid */, true /* typedWordValid */,
false /* willAutoCorrect */, false /* willAutoCorrect */,
false /* isPunctuationSuggestions */, false /* isPunctuationSuggestions */,
false /* isObsoleteSuggestions */, false /* isObsoleteSuggestions */,
false /* isPrediction */); false /* isPrediction */));
} }
private static ArrayList<SuggestedWordInfo> getSuggestionsInfoListWithDebugInfo( private static ArrayList<SuggestedWordInfo> getSuggestionsInfoListWithDebugInfo(