Merge "Revert "Make SuggestedWords immutable""

main
Tadashi Takaoka 2012-03-05 02:57:00 -08:00 committed by Android (Google) Code Review
commit d5b6360549
5 changed files with 43 additions and 50 deletions

View File

@ -1827,8 +1827,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
builder.addTypedWordAndPreviousSuggestions(typedWord, previousSuggestions); builder.addTypedWordAndPreviousSuggestions(typedWord, previousSuggestions);
} }
} }
if (Utils.shouldBlockAutoCorrectionBySafetyNet(builder, mSuggest)) { final SuggestedWords suggestedWords = builder.build();
builder.setShouldBlockAutoCorrectionBySafetyNet(); if (Utils.shouldBlockAutoCorrectionBySafetyNet(suggestedWords, mSuggest)) {
suggestedWords.setShouldBlockAutoCorrectionBySatefyNet();
} }
showSuggestions(builder.build(), typedWord); showSuggestions(builder.build(), typedWord);
} }
@ -1836,7 +1837,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
public void showSuggestions(final SuggestedWords suggestedWords, final CharSequence typedWord) { public void showSuggestions(final SuggestedWords suggestedWords, final CharSequence typedWord) {
final CharSequence autoCorrection; final CharSequence autoCorrection;
if (suggestedWords.size() > 0) { if (suggestedWords.size() > 0) {
if (!suggestedWords.mShouldBlockAutoCorrectionBySafetyNet if (!suggestedWords.shouldBlockAutoCorrectionBySafetyNet()
&& suggestedWords.hasAutoCorrectionWord()) { && suggestedWords.hasAutoCorrectionWord()) {
autoCorrection = suggestedWords.getWord(1); autoCorrection = suggestedWords.getWord(1);
} else { } else {
@ -1909,7 +1910,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
if (suggestion.length() == 1 && isShowingPunctuationList()) { if (suggestion.length() == 1 && isShowingPunctuationList()) {
// Word separators are suggested before the user inputs something. // Word separators are suggested before the user inputs something.
// So, LatinImeLogger logs "" as a user's input. // So, LatinImeLogger logs "" as a user's input.
LatinImeLogger.logOnManualSuggestion("", suggestion.toString(), index, suggestions); LatinImeLogger.logOnManualSuggestion(
"", suggestion.toString(), index, suggestions.mWords);
// Rely on onCodeInput to do the complicated swapping/stripping logic consistently. // Rely on onCodeInput to do the complicated swapping/stripping logic consistently.
final int primaryCode = suggestion.charAt(0); final int primaryCode = suggestion.charAt(0);
onCodeInput(primaryCode, new int[] { primaryCode }, onCodeInput(primaryCode, new int[] { primaryCode },
@ -1920,7 +1922,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// We need to log before we commit, because the word composer will store away the user // We need to log before we commit, because the word composer will store away the user
// typed word. // typed word.
LatinImeLogger.logOnManualSuggestion(mWordComposer.getTypedWord().toString(), LatinImeLogger.logOnManualSuggestion(mWordComposer.getTypedWord().toString(),
suggestion.toString(), index, suggestions); suggestion.toString(), index, suggestions.mWords);
mExpectingUpdateSelection = true; mExpectingUpdateSelection = true;
commitChosenWord(suggestion, LastComposedWord.COMMIT_TYPE_MANUAL_PICK, commitChosenWord(suggestion, LastComposedWord.COMMIT_TYPE_MANUAL_PICK,
LastComposedWord.NOT_A_SEPARATOR); LastComposedWord.NOT_A_SEPARATOR);

View File

@ -22,6 +22,8 @@ import android.view.inputmethod.EditorInfo;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
import java.util.List;
public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChangeListener { public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChangeListener {
public static boolean sDBG = false; public static boolean sDBG = false;
@ -42,7 +44,7 @@ public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChang
} }
public static void logOnManualSuggestion( public static void logOnManualSuggestion(
String before, String after, int position, SuggestedWords suggestions) { String before, String after, int position, List<CharSequence> suggestions) {
} }
public static void logOnAutoCorrection(String before, String after, int separatorCode) { public static void logOnAutoCorrection(String before, String after, int separatorCode) {

View File

@ -20,25 +20,22 @@ import android.text.TextUtils;
import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.CompletionInfo;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
public class SuggestedWords { public class SuggestedWords {
public static final SuggestedWords EMPTY = new SuggestedWords(null, false, false, false, false, public static final SuggestedWords EMPTY = new SuggestedWords(null, false, false, false, null);
null);
private final List<CharSequence> mWords; public final List<CharSequence> mWords;
public final boolean mTypedWordValid; public final boolean mTypedWordValid;
public final boolean mHasAutoCorrectionCandidate; public final boolean mHasAutoCorrectionCandidate;
public final boolean mIsPunctuationSuggestions; public final boolean mIsPunctuationSuggestions;
public final boolean mShouldBlockAutoCorrectionBySafetyNet;
private final List<SuggestedWordInfo> mSuggestedWordInfoList; private final List<SuggestedWordInfo> mSuggestedWordInfoList;
private boolean mShouldBlockAutoCorrectionBySafetyNet;
SuggestedWords(List<CharSequence> words, boolean typedWordValid, private SuggestedWords(List<CharSequence> words, boolean typedWordValid,
boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions, boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions,
boolean shouldBlockAutoCorrectionBySafetyNet,
List<SuggestedWordInfo> suggestedWordInfoList) { List<SuggestedWordInfo> suggestedWordInfoList) {
if (words != null) { if (words != null) {
mWords = words; mWords = words;
@ -48,8 +45,8 @@ public class SuggestedWords {
mTypedWordValid = typedWordValid; mTypedWordValid = typedWordValid;
mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate; mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate;
mIsPunctuationSuggestions = isPunctuationSuggestions; mIsPunctuationSuggestions = isPunctuationSuggestions;
mShouldBlockAutoCorrectionBySafetyNet = shouldBlockAutoCorrectionBySafetyNet;
mSuggestedWordInfoList = suggestedWordInfoList; mSuggestedWordInfoList = suggestedWordInfoList;
mShouldBlockAutoCorrectionBySafetyNet = false;
} }
public int size() { public int size() {
@ -65,23 +62,24 @@ public class SuggestedWords {
} }
public boolean hasAutoCorrectionWord() { public boolean hasAutoCorrectionWord() {
return mHasAutoCorrectionCandidate && size() > 1 && mTypedWordValid; return mHasAutoCorrectionCandidate && size() > 1 && !mTypedWordValid;
}
public boolean isPunctuationSuggestions() {
return mIsPunctuationSuggestions;
}
public void setShouldBlockAutoCorrectionBySatefyNet() {
mShouldBlockAutoCorrectionBySafetyNet = true;
}
public boolean shouldBlockAutoCorrectionBySafetyNet() {
return mShouldBlockAutoCorrectionBySafetyNet;
} }
public boolean willAutoCorrect() { public boolean willAutoCorrect() {
return !mTypedWordValid && mHasAutoCorrectionCandidate return !mTypedWordValid && mHasAutoCorrectionCandidate
&& !mShouldBlockAutoCorrectionBySafetyNet; && !shouldBlockAutoCorrectionBySafetyNet();
}
@Override
public String toString() {
// Pretty-print method to help debug
return "SuggestedWords.Builder:"
+ " mTypedWordValid = " + mTypedWordValid
+ " mHasAutoCorrectionCandidate = " + mHasAutoCorrectionCandidate
+ " mIsPunctuationSuggestions = " + mIsPunctuationSuggestions
+ " mShouldBlockAutoCorrectionBySafetyNet" + mShouldBlockAutoCorrectionBySafetyNet
+ " mWords=" + Arrays.toString(mWords.toArray());
} }
public static class Builder { public static class Builder {
@ -89,7 +87,6 @@ public class SuggestedWords {
private boolean mTypedWordValid; private boolean mTypedWordValid;
private boolean mHasMinimalSuggestion; private boolean mHasMinimalSuggestion;
private boolean mIsPunctuationSuggestions; private boolean mIsPunctuationSuggestions;
private boolean mShouldBlockAutoCorrectionBySafetyNet;
private List<SuggestedWordInfo> mSuggestedWordInfoList = private List<SuggestedWordInfo> mSuggestedWordInfoList =
new ArrayList<SuggestedWordInfo>(); new ArrayList<SuggestedWordInfo>();
@ -154,11 +151,6 @@ public class SuggestedWords {
return this; return this;
} }
public Builder setShouldBlockAutoCorrectionBySafetyNet() {
mShouldBlockAutoCorrectionBySafetyNet = true;
return this;
}
// Should get rid of the first one (what the user typed previously) from suggestions // Should get rid of the first one (what the user typed previously) from suggestions
// and replace it with what the user currently typed. // and replace it with what the user currently typed.
public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord, public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
@ -184,8 +176,7 @@ public class SuggestedWords {
public SuggestedWords build() { public SuggestedWords build() {
return new SuggestedWords(mWords, mTypedWordValid, mHasMinimalSuggestion, return new SuggestedWords(mWords, mTypedWordValid, mHasMinimalSuggestion,
mIsPunctuationSuggestions, mShouldBlockAutoCorrectionBySafetyNet, mIsPunctuationSuggestions, mSuggestedWordInfoList);
mSuggestedWordInfoList);
} }
public int size() { public int size() {
@ -196,20 +187,18 @@ public class SuggestedWords {
return mWords.get(pos); return mWords.get(pos);
} }
public boolean isTypedWordValid() {
return mTypedWordValid;
}
@Override @Override
public String toString() { public String toString() {
// Pretty-print method to help debug // Pretty-print method to help debug
return "SuggestedWords.Builder:" final StringBuilder sb = new StringBuilder("StringBuilder: mTypedWordValid = "
+ " mTypedWordValid = " + mTypedWordValid + mTypedWordValid + " ; mHasMinimalSuggestion = " + mHasMinimalSuggestion
+ " mHasMinimalSuggestion = " + mHasMinimalSuggestion + " ; mIsPunctuationSuggestions = " + mIsPunctuationSuggestions
+ " mIsPunctuationSuggestions = " + mIsPunctuationSuggestions + " --- ");
+ " mShouldBlockAutoCorrectionBySafetyNet" for (CharSequence s : mWords) {
+ mShouldBlockAutoCorrectionBySafetyNet sb.append(s);
+ " mWords=" + Arrays.toString(mWords.toArray()); sb.append(" ; ");
}
return sb.toString();
} }
} }

View File

@ -190,11 +190,11 @@ public class Utils {
// TODO: Resolve the inconsistencies between the native auto correction algorithms and // TODO: Resolve the inconsistencies between the native auto correction algorithms and
// this safety net // this safety net
public static boolean shouldBlockAutoCorrectionBySafetyNet(SuggestedWords.Builder suggestions, public static boolean shouldBlockAutoCorrectionBySafetyNet(SuggestedWords suggestions,
Suggest suggest) { Suggest suggest) {
// Safety net for auto correction. // Safety net for auto correction.
// Actually if we hit this safety net, it's actually a bug. // Actually if we hit this safety net, it's actually a bug.
if (suggestions.size() <= 1 || suggestions.isTypedWordValid()) return false; if (suggestions.size() <= 1 || suggestions.mTypedWordValid) return false;
// If user selected aggressive auto correction mode, there is no need to use the safety // If user selected aggressive auto correction mode, there is no need to use the safety
// net. // net.
if (suggest.isAggressiveAutoCorrectionMode()) return false; if (suggest.isAggressiveAutoCorrectionMode()) return false;

View File

@ -307,7 +307,7 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener,
} }
if (LatinImeLogger.sDBG) { if (LatinImeLogger.sDBG) {
if (index == mCenterSuggestionIndex && suggestions.mHasAutoCorrectionCandidate if (index == mCenterSuggestionIndex && suggestions.mHasAutoCorrectionCandidate
&& suggestions.mShouldBlockAutoCorrectionBySafetyNet) { && suggestions.shouldBlockAutoCorrectionBySafetyNet()) {
return 0xFFFF0000; return 0xFFFF0000;
} }
} }
@ -335,7 +335,7 @@ public class SuggestionsView extends RelativeLayout implements OnClickListener,
public void layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup placer, public void layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup placer,
int stripWidth) { int stripWidth) {
if (suggestions.mIsPunctuationSuggestions) { if (suggestions.isPunctuationSuggestions()) {
layoutPunctuationSuggestions(suggestions, stripView); layoutPunctuationSuggestions(suggestions, stripView);
return; return;
} }