[IL16] Improve getTextWithSuggestionSpan
Dynamically test for the presence of main dict suggestions. This is much more potent and more accurate than the vaguely defined boolean. Also, resolve a TODO and avoid creating an object uselessly. Not directly related, but helpful with: Bug: 8636060 Change-Id: Ib1745f77ee6d9ec7cd8bbfa5a548652ec84ec902
This commit is contained in:
parent
6e97efef78
commit
a905fcec00
2 changed files with 21 additions and 7 deletions
|
@ -23,8 +23,10 @@ import android.text.Spanned;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.text.style.SuggestionSpan;
|
import android.text.style.SuggestionSpan;
|
||||||
|
|
||||||
|
import com.android.inputmethod.latin.Dictionary;
|
||||||
import com.android.inputmethod.latin.LatinImeLogger;
|
import com.android.inputmethod.latin.LatinImeLogger;
|
||||||
import com.android.inputmethod.latin.SuggestedWords;
|
import com.android.inputmethod.latin.SuggestedWords;
|
||||||
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
||||||
import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver;
|
import com.android.inputmethod.latin.SuggestionSpanPickedNotificationReceiver;
|
||||||
import com.android.inputmethod.latin.utils.CollectionUtils;
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
||||||
|
|
||||||
|
@ -66,30 +68,42 @@ public final class SuggestionSpanUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CharSequence getTextWithSuggestionSpan(final Context context,
|
public static CharSequence getTextWithSuggestionSpan(final Context context,
|
||||||
final String pickedWord, final SuggestedWords suggestedWords,
|
final String pickedWord, final SuggestedWords suggestedWords) {
|
||||||
final boolean dictionaryAvailable) {
|
if (TextUtils.isEmpty(pickedWord) || suggestedWords.isEmpty()
|
||||||
if (!dictionaryAvailable || TextUtils.isEmpty(pickedWord) || suggestedWords.isEmpty()
|
|
||||||
|| suggestedWords.mIsPrediction || suggestedWords.mIsPunctuationSuggestions) {
|
|| suggestedWords.mIsPrediction || suggestedWords.mIsPunctuationSuggestions) {
|
||||||
return pickedWord;
|
return pickedWord;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Spannable spannable = new SpannableString(pickedWord);
|
boolean hasSuggestionFromMainDictionary = false;
|
||||||
final ArrayList<String> suggestionsList = CollectionUtils.newArrayList();
|
final ArrayList<String> suggestionsList = CollectionUtils.newArrayList();
|
||||||
for (int i = 0; i < suggestedWords.size(); ++i) {
|
for (int i = 0; i < suggestedWords.size(); ++i) {
|
||||||
if (suggestionsList.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
|
if (suggestionsList.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
final SuggestedWordInfo info = suggestedWords.getInfo(i);
|
||||||
|
if (info.mKind == SuggestedWordInfo.KIND_PREDICTION) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (info.mSourceDict.mDictType == Dictionary.TYPE_MAIN) {
|
||||||
|
hasSuggestionFromMainDictionary = true;
|
||||||
|
}
|
||||||
final String word = suggestedWords.getWord(i);
|
final String word = suggestedWords.getWord(i);
|
||||||
if (!TextUtils.equals(pickedWord, word)) {
|
if (!TextUtils.equals(pickedWord, word)) {
|
||||||
suggestionsList.add(word.toString());
|
suggestionsList.add(word.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!hasSuggestionFromMainDictionary) {
|
||||||
|
// If we don't have any suggestions from the dictionary, it probably looks bad
|
||||||
|
// enough as it is already because suggestions come pretty much only from contacts.
|
||||||
|
// Let's not embed these bad suggestions in the text view so as to avoid using
|
||||||
|
// them with recorrection.
|
||||||
|
return pickedWord;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: We should avoid adding suggestion span candidates that came from the bigram
|
|
||||||
// prediction.
|
|
||||||
final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null /* locale */,
|
final SuggestionSpan suggestionSpan = new SuggestionSpan(context, null /* locale */,
|
||||||
suggestionsList.toArray(new String[suggestionsList.size()]), 0 /* flags */,
|
suggestionsList.toArray(new String[suggestionsList.size()]), 0 /* flags */,
|
||||||
SuggestionSpanPickedNotificationReceiver.class);
|
SuggestionSpanPickedNotificationReceiver.class);
|
||||||
|
final Spannable spannable = new SpannableString(pickedWord);
|
||||||
spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
spannable.setSpan(suggestionSpan, 0, pickedWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
return spannable;
|
return spannable;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2026,7 +2026,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
|
||||||
final String separatorString) {
|
final String separatorString) {
|
||||||
final SuggestedWords suggestedWords = mInputLogic.mSuggestedWords;
|
final SuggestedWords suggestedWords = mInputLogic.mSuggestedWords;
|
||||||
mInputLogic.mConnection.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan(
|
mInputLogic.mConnection.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan(
|
||||||
this, chosenWord, suggestedWords, mIsMainDictionaryAvailable), 1);
|
this, chosenWord, suggestedWords), 1);
|
||||||
// Add the word to the user history dictionary
|
// Add the word to the user history dictionary
|
||||||
final String prevWord = addToUserHistoryDictionary(chosenWord);
|
final String prevWord = addToUserHistoryDictionary(chosenWord);
|
||||||
// TODO: figure out here if this is an auto-correct or if the best word is actually
|
// TODO: figure out here if this is an auto-correct or if the best word is actually
|
||||||
|
|
Loading…
Reference in a new issue