Cleanup CandidateView class
Bug: 5023981 Change-Id: Id79e31c60dfb41be546f5c5a3680e8651457151d
This commit is contained in:
parent
3889462439
commit
d95a6365be
2 changed files with 189 additions and 151 deletions
java/src/com/android/inputmethod/latin
|
@ -57,8 +57,6 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
public void pickSuggestionManually(int index, CharSequence word);
|
public void pickSuggestionManually(int index, CharSequence word);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD);
|
|
||||||
private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan();
|
|
||||||
// The maximum number of suggestions available. See {@link Suggest#mPrefMaxSuggestions}.
|
// The maximum number of suggestions available. See {@link Suggest#mPrefMaxSuggestions}.
|
||||||
private static final int MAX_SUGGESTIONS = 18;
|
private static final int MAX_SUGGESTIONS = 18;
|
||||||
private static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
|
private static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
|
||||||
|
@ -67,8 +65,6 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
private static final boolean DBG = LatinImeLogger.sDBG;
|
private static final boolean DBG = LatinImeLogger.sDBG;
|
||||||
|
|
||||||
private final ViewGroup mCandidatesStrip;
|
private final ViewGroup mCandidatesStrip;
|
||||||
private final int mCandidateCountInStrip;
|
|
||||||
private static final int DEFAULT_CANDIDATE_COUNT_IN_STRIP = 3;
|
|
||||||
private final ViewGroup mCandidatesPaneControl;
|
private final ViewGroup mCandidatesPaneControl;
|
||||||
private final TextView mExpandCandidatesPane;
|
private final TextView mExpandCandidatesPane;
|
||||||
private final TextView mCloseCandidatesPane;
|
private final TextView mCloseCandidatesPane;
|
||||||
|
@ -81,15 +77,6 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
private final ArrayList<View> mDividers = new ArrayList<View>();
|
private final ArrayList<View> mDividers = new ArrayList<View>();
|
||||||
|
|
||||||
private final int mCandidateStripHeight;
|
private final int mCandidateStripHeight;
|
||||||
private final CharacterStyle mInvertedForegroundColorSpan;
|
|
||||||
private final CharacterStyle mInvertedBackgroundColorSpan;
|
|
||||||
private final int mAutoCorrectHighlight;
|
|
||||||
private static final int AUTO_CORRECT_BOLD = 0x01;
|
|
||||||
private static final int AUTO_CORRECT_UNDERLINE = 0x02;
|
|
||||||
private static final int AUTO_CORRECT_INVERT = 0x04;
|
|
||||||
private final int mColorTypedWord;
|
|
||||||
private final int mColorAutoCorrect;
|
|
||||||
private final int mColorSuggestedCandidate;
|
|
||||||
|
|
||||||
private final PopupWindow mPreviewPopup;
|
private final PopupWindow mPreviewPopup;
|
||||||
private final TextView mPreviewText;
|
private final TextView mPreviewText;
|
||||||
|
@ -102,8 +89,8 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
private boolean mShowingAutoCorrectionInverted;
|
private boolean mShowingAutoCorrectionInverted;
|
||||||
private boolean mShowingAddToDictionary;
|
private boolean mShowingAddToDictionary;
|
||||||
|
|
||||||
private final CandidateViewLayoutParams mParams;
|
private final SuggestionsStripParams mStripParams;
|
||||||
private static final int PUNCTUATIONS_IN_STRIP = 6;
|
private final SuggestionsPaneParams mPaneParams;
|
||||||
private static final float MIN_TEXT_XSCALE = 0.75f;
|
private static final float MIN_TEXT_XSCALE = 0.75f;
|
||||||
|
|
||||||
private final UiHandler mHandler = new UiHandler(this);
|
private final UiHandler mHandler = new UiHandler(this);
|
||||||
|
@ -157,15 +144,49 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CandidateViewLayoutParams {
|
private static class CandidateViewParams {
|
||||||
public final TextPaint mPaint;
|
|
||||||
public final int mPadding;
|
public final int mPadding;
|
||||||
public final int mDividerWidth;
|
public final int mDividerWidth;
|
||||||
public final int mDividerHeight;
|
public final int mDividerHeight;
|
||||||
public final int mControlWidth;
|
public final int mControlWidth;
|
||||||
|
|
||||||
|
protected CandidateViewParams(TextView word, View divider, View control) {
|
||||||
|
mPadding = word.getCompoundPaddingLeft() + word.getCompoundPaddingRight();
|
||||||
|
divider.measure(WRAP_CONTENT, MATCH_PARENT);
|
||||||
|
mDividerWidth = divider.getMeasuredWidth();
|
||||||
|
mDividerHeight = divider.getMeasuredHeight();
|
||||||
|
mControlWidth = control.getMeasuredWidth();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SuggestionsPaneParams extends CandidateViewParams {
|
||||||
|
public SuggestionsPaneParams(List<TextView> words, List<View> dividers, View control) {
|
||||||
|
super(words.get(0), dividers.get(0), control);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SuggestionsStripParams extends CandidateViewParams {
|
||||||
|
private static final int DEFAULT_CANDIDATE_COUNT_IN_STRIP = 3;
|
||||||
|
private static final int PUNCTUATIONS_IN_STRIP = 6;
|
||||||
|
|
||||||
|
private final int mColorTypedWord;
|
||||||
|
private final int mColorAutoCorrect;
|
||||||
|
private final int mColorSuggestedCandidate;
|
||||||
|
private final int mCandidateCountInStrip;
|
||||||
|
|
||||||
|
private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD);
|
||||||
|
private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan();
|
||||||
|
private final CharacterStyle mInvertedForegroundColorSpan;
|
||||||
|
private final CharacterStyle mInvertedBackgroundColorSpan;
|
||||||
|
private static final int AUTO_CORRECT_BOLD = 0x01;
|
||||||
|
private static final int AUTO_CORRECT_UNDERLINE = 0x02;
|
||||||
|
private static final int AUTO_CORRECT_INVERT = 0x04;
|
||||||
|
|
||||||
|
public final TextPaint mPaint;
|
||||||
private final int mAutoCorrectHighlight;
|
private final int mAutoCorrectHighlight;
|
||||||
|
|
||||||
public final ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>();
|
private final ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>();
|
||||||
|
private SuggestedWords mSuggestedWords;
|
||||||
|
|
||||||
public int mCountInStrip;
|
public int mCountInStrip;
|
||||||
// True if the mCountInStrip suggestions can fit in suggestion strip in equally divided
|
// True if the mCountInStrip suggestions can fit in suggestion strip in equally divided
|
||||||
|
@ -177,23 +198,114 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
public int mVariableWidthForWords;
|
public int mVariableWidthForWords;
|
||||||
public float mScaleX;
|
public float mScaleX;
|
||||||
|
|
||||||
public CandidateViewLayoutParams(Resources res, TextView word, View divider, View control,
|
public SuggestionsStripParams(Context context, AttributeSet attrs, int defStyle,
|
||||||
int autoCorrectHighlight) {
|
List<TextView> words, List<View> dividers, View control) {
|
||||||
|
super(words.get(0), dividers.get(0), control);
|
||||||
|
final TypedArray a = context.obtainStyledAttributes(
|
||||||
|
attrs, R.styleable.CandidateView, defStyle, R.style.CandidateViewStyle);
|
||||||
|
mAutoCorrectHighlight = a.getInt(R.styleable.CandidateView_autoCorrectHighlight, 0);
|
||||||
|
mColorTypedWord = a.getColor(R.styleable.CandidateView_colorTypedWord, 0);
|
||||||
|
mColorAutoCorrect = a.getColor(R.styleable.CandidateView_colorAutoCorrect, 0);
|
||||||
|
mColorSuggestedCandidate = a.getColor(R.styleable.CandidateView_colorSuggested, 0);
|
||||||
|
mCandidateCountInStrip = a.getInt(
|
||||||
|
R.styleable.CandidateView_candidateCountInStrip,
|
||||||
|
DEFAULT_CANDIDATE_COUNT_IN_STRIP);
|
||||||
|
a.recycle();
|
||||||
|
|
||||||
|
mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff);
|
||||||
|
mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord);
|
||||||
|
|
||||||
mPaint = new TextPaint();
|
mPaint = new TextPaint();
|
||||||
final float textSize = res.getDimension(R.dimen.candidate_text_size);
|
final float textSize = context.getResources().getDimension(R.dimen.candidate_text_size);
|
||||||
mPaint.setTextSize(textSize);
|
mPaint.setTextSize(textSize);
|
||||||
mPadding = word.getCompoundPaddingLeft() + word.getCompoundPaddingRight();
|
|
||||||
divider.measure(WRAP_CONTENT, MATCH_PARENT);
|
|
||||||
mDividerWidth = divider.getMeasuredWidth();
|
|
||||||
mDividerHeight = divider.getMeasuredHeight();
|
|
||||||
mControlWidth = control.getMeasuredWidth();
|
|
||||||
mAutoCorrectHighlight = autoCorrectHighlight;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void layoutStrip(SuggestedWords suggestions, int maxWidth, int maxCount) {
|
public CharSequence getWord(int pos) {
|
||||||
|
return mTexts.get(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CharSequence getDebugInfo(int pos) {
|
||||||
|
if (DBG) {
|
||||||
|
final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(pos);
|
||||||
|
if (wordInfo != null) {
|
||||||
|
final CharSequence debugInfo = wordInfo.getDebugString();
|
||||||
|
if (!TextUtils.isEmpty(debugInfo)) {
|
||||||
|
return debugInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CharSequence getStyledCandidateWord(CharSequence word, boolean isAutoCorrect) {
|
||||||
|
if (!isAutoCorrect)
|
||||||
|
return word;
|
||||||
|
final int len = word.length();
|
||||||
|
final Spannable spannedWord = new SpannableString(word);
|
||||||
|
if ((mAutoCorrectHighlight & AUTO_CORRECT_BOLD) != 0)
|
||||||
|
spannedWord.setSpan(BOLD_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
||||||
|
if ((mAutoCorrectHighlight & AUTO_CORRECT_UNDERLINE) != 0)
|
||||||
|
spannedWord.setSpan(UNDERLINE_SPAN, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
||||||
|
return spannedWord;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWordPosition(int index) {
|
||||||
|
if (index >= 2) {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
final boolean willAutoCorrect = !mSuggestedWords.mTypedWordValid
|
||||||
|
&& mSuggestedWords.mHasMinimalSuggestion;
|
||||||
|
return willAutoCorrect ? 1 - index : index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCandidateTextColor(int pos) {
|
||||||
|
final SuggestedWords suggestions = mSuggestedWords;
|
||||||
|
final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion
|
||||||
|
&& ((pos == 1 && !suggestions.mTypedWordValid)
|
||||||
|
|| (pos == 0 && suggestions.mTypedWordValid));
|
||||||
|
// TODO: Need to revisit this logic with bigram suggestions
|
||||||
|
final boolean isSuggestedCandidate = (pos != 0);
|
||||||
|
final boolean isPunctuationSuggestions = suggestions.isPunctuationSuggestions();
|
||||||
|
|
||||||
|
final int color;
|
||||||
|
if (isPunctuationSuggestions) {
|
||||||
|
color = mColorTypedWord;
|
||||||
|
} else if (isAutoCorrect) {
|
||||||
|
color = mColorAutoCorrect;
|
||||||
|
} else if (isSuggestedCandidate) {
|
||||||
|
color = mColorSuggestedCandidate;
|
||||||
|
} else {
|
||||||
|
color = mColorTypedWord;
|
||||||
|
}
|
||||||
|
final SuggestedWordInfo info = suggestions.getInfo(pos);
|
||||||
|
if (info != null && info.isPreviousSuggestedWord()) {
|
||||||
|
return applyAlpha(color, 0.5f);
|
||||||
|
} else {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int applyAlpha(final int color, final float alpha) {
|
||||||
|
final int newAlpha = (int)(Color.alpha(color) * alpha);
|
||||||
|
return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color));
|
||||||
|
}
|
||||||
|
|
||||||
|
public CharSequence getInvertedText(CharSequence text) {
|
||||||
|
if ((mAutoCorrectHighlight & AUTO_CORRECT_INVERT) == 0)
|
||||||
|
return null;
|
||||||
|
final int len = text.length();
|
||||||
|
final Spannable word = new SpannableString(text);
|
||||||
|
word.setSpan(mInvertedBackgroundColorSpan, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
||||||
|
word.setSpan(mInvertedForegroundColorSpan, 0, len, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
||||||
|
return word;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void layoutStrip(SuggestedWords suggestions, int maxWidth) {
|
||||||
|
mSuggestedWords = suggestions;
|
||||||
|
final int maxCount = suggestions.isPunctuationSuggestions()
|
||||||
|
? PUNCTUATIONS_IN_STRIP : mCandidateCountInStrip;
|
||||||
final int size = suggestions.size();
|
final int size = suggestions.size();
|
||||||
if (size == 0) return;
|
setupTexts(suggestions, size);
|
||||||
setupTexts(suggestions, size, mAutoCorrectHighlight);
|
|
||||||
mCountInStrip = Math.min(maxCount, size);
|
mCountInStrip = Math.min(maxCount, size);
|
||||||
mScaleX = 1.0f;
|
mScaleX = 1.0f;
|
||||||
|
|
||||||
|
@ -240,25 +352,14 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupTexts(SuggestedWords suggestions, int count, int autoCorrectHighlight) {
|
private void setupTexts(SuggestedWords suggestions, int count) {
|
||||||
mTexts.clear();
|
mTexts.clear();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
final CharSequence suggestion = suggestions.getWord(i);
|
final CharSequence word = suggestions.getWord(i);
|
||||||
if (suggestion == null) {
|
|
||||||
// Skip an empty suggestion, but we need to add a place-holder for it in order
|
|
||||||
// to avoid an exception in the loop in updateSuggestions().
|
|
||||||
mTexts.add("");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion
|
final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion
|
||||||
&& ((i == 1 && !suggestions.mTypedWordValid)
|
&& ((i == 1 && !suggestions.mTypedWordValid)
|
||||||
|| (i == 0 && suggestions.mTypedWordValid));
|
|| (i == 0 && suggestions.mTypedWordValid));
|
||||||
// HACK: even if i == 0, we use mColorOther when this suggestion's length is 1
|
final CharSequence styled = getStyledCandidateWord(word, isAutoCorrect);
|
||||||
// and there are multiple suggestions, such as the default punctuation list.
|
|
||||||
// TODO: Need to revisit this logic with bigram suggestions
|
|
||||||
final CharSequence styled = getStyledCandidateWord(suggestion, isAutoCorrect,
|
|
||||||
autoCorrectHighlight);
|
|
||||||
mTexts.add(styled);
|
mTexts.add(styled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -295,16 +396,6 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
setBackgroundDrawable(LinearLayoutCompatUtils.getBackgroundDrawable(
|
setBackgroundDrawable(LinearLayoutCompatUtils.getBackgroundDrawable(
|
||||||
context, attrs, defStyle, R.style.CandidateViewStyle));
|
context, attrs, defStyle, R.style.CandidateViewStyle));
|
||||||
|
|
||||||
final TypedArray a = context.obtainStyledAttributes(
|
|
||||||
attrs, R.styleable.CandidateView, defStyle, R.style.CandidateViewStyle);
|
|
||||||
mAutoCorrectHighlight = a.getInt(R.styleable.CandidateView_autoCorrectHighlight, 0);
|
|
||||||
mColorTypedWord = a.getColor(R.styleable.CandidateView_colorTypedWord, 0);
|
|
||||||
mColorAutoCorrect = a.getColor(R.styleable.CandidateView_colorAutoCorrect, 0);
|
|
||||||
mColorSuggestedCandidate = a.getColor(R.styleable.CandidateView_colorSuggested, 0);
|
|
||||||
mCandidateCountInStrip = a.getInt(
|
|
||||||
R.styleable.CandidateView_candidateCountInStrip, DEFAULT_CANDIDATE_COUNT_IN_STRIP);
|
|
||||||
a.recycle();
|
|
||||||
|
|
||||||
Resources res = context.getResources();
|
Resources res = context.getResources();
|
||||||
LayoutInflater inflater = LayoutInflater.from(context);
|
LayoutInflater inflater = LayoutInflater.from(context);
|
||||||
inflater.inflate(R.layout.candidates_strip, this);
|
inflater.inflate(R.layout.candidates_strip, this);
|
||||||
|
@ -334,9 +425,6 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
mWordToSave = (TextView)findViewById(R.id.word_to_save);
|
mWordToSave = (TextView)findViewById(R.id.word_to_save);
|
||||||
mWordToSave.setOnClickListener(this);
|
mWordToSave.setOnClickListener(this);
|
||||||
|
|
||||||
mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff);
|
|
||||||
mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord);
|
|
||||||
|
|
||||||
final TypedArray keyboardViewAttr = context.obtainStyledAttributes(
|
final TypedArray keyboardViewAttr = context.obtainStyledAttributes(
|
||||||
attrs, R.styleable.KeyboardView, R.attr.keyboardViewStyle, R.style.KeyboardView);
|
attrs, R.styleable.KeyboardView, R.attr.keyboardViewStyle, R.style.KeyboardView);
|
||||||
final Drawable expandBackground = keyboardViewAttr.getDrawable(
|
final Drawable expandBackground = keyboardViewAttr.getDrawable(
|
||||||
|
@ -368,8 +456,9 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
});
|
});
|
||||||
mCandidatesPaneControl.measure(WRAP_CONTENT, WRAP_CONTENT);
|
mCandidatesPaneControl.measure(WRAP_CONTENT, WRAP_CONTENT);
|
||||||
|
|
||||||
mParams = new CandidateViewLayoutParams(res,
|
mStripParams = new SuggestionsStripParams(context, attrs, defStyle,
|
||||||
mWords.get(0), mDividers.get(0), mCandidatesPaneControl, mAutoCorrectHighlight);
|
mWords, mDividers, mCandidatesPaneControl);
|
||||||
|
mPaneParams = new SuggestionsPaneParams(mWords, mDividers, mCandidatesPaneControl);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -398,52 +487,20 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CharSequence getStyledCandidateWord(CharSequence word, boolean isAutoCorrect,
|
|
||||||
int autoCorrectHighlight) {
|
|
||||||
if (!isAutoCorrect)
|
|
||||||
return word;
|
|
||||||
final Spannable spannedWord = new SpannableString(word);
|
|
||||||
if ((autoCorrectHighlight & AUTO_CORRECT_BOLD) != 0)
|
|
||||||
spannedWord.setSpan(BOLD_SPAN, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
||||||
if ((autoCorrectHighlight & AUTO_CORRECT_UNDERLINE) != 0)
|
|
||||||
spannedWord.setSpan(UNDERLINE_SPAN, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
||||||
return spannedWord;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getCandidateTextColor(boolean isAutoCorrect, boolean isSuggestedCandidate,
|
|
||||||
SuggestedWordInfo info) {
|
|
||||||
final int color;
|
|
||||||
if (isAutoCorrect) {
|
|
||||||
color = mColorAutoCorrect;
|
|
||||||
} else if (isSuggestedCandidate) {
|
|
||||||
color = mColorSuggestedCandidate;
|
|
||||||
} else {
|
|
||||||
color = mColorTypedWord;
|
|
||||||
}
|
|
||||||
if (info != null && info.isPreviousSuggestedWord()) {
|
|
||||||
final int newAlpha = (int)(Color.alpha(color) * 0.5f);
|
|
||||||
return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color));
|
|
||||||
} else {
|
|
||||||
return color;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateSuggestions() {
|
private void updateSuggestions() {
|
||||||
final SuggestedWords suggestions = mSuggestions;
|
|
||||||
final List<SuggestedWordInfo> suggestedWordInfoList = suggestions.mSuggestedWordInfoList;
|
|
||||||
final int paneWidth = getWidth();
|
|
||||||
final CandidateViewLayoutParams params = mParams;
|
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
closeCandidatesPane();
|
closeCandidatesPane();
|
||||||
|
final SuggestedWords suggestions = mSuggestions;
|
||||||
if (suggestions.size() == 0)
|
if (suggestions.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
params.layoutStrip(suggestions, paneWidth, suggestions.isPunctuationSuggestions()
|
final int paneWidth = getWidth();
|
||||||
? PUNCTUATIONS_IN_STRIP : mCandidateCountInStrip);
|
final SuggestionsStripParams stripParams = mStripParams;
|
||||||
|
final SuggestionsPaneParams paneParams = mPaneParams;
|
||||||
|
stripParams.layoutStrip(suggestions, paneWidth);
|
||||||
|
|
||||||
final int count = Math.min(mWords.size(), suggestions.size());
|
final int count = Math.min(mWords.size(), suggestions.size());
|
||||||
if (count <= params.mCountInStrip && !DBG) {
|
if (count <= stripParams.mCountInStrip && !DBG) {
|
||||||
mCandidatesPaneControl.setVisibility(GONE);
|
mCandidatesPaneControl.setVisibility(GONE);
|
||||||
} else {
|
} else {
|
||||||
mCandidatesPaneControl.setVisibility(VISIBLE);
|
mCandidatesPaneControl.setVisibility(VISIBLE);
|
||||||
|
@ -451,71 +508,53 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
mExpandCandidatesPane.setEnabled(true);
|
mExpandCandidatesPane.setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
final int countInStrip = params.mCountInStrip;
|
final int countInStrip = stripParams.mCountInStrip;
|
||||||
View centeringFrom = null, lastView = null;
|
View centeringFrom = null, lastView = null;
|
||||||
int x = 0, y = 0, infoX = 0;
|
int x = 0, y = 0, infoX = 0;
|
||||||
for (int i = 0; i < count; i++) {
|
for (int index = 0; index < count; index++) {
|
||||||
final int pos;
|
final int pos = stripParams.getWordPosition(index);
|
||||||
if (i <= 1) {
|
|
||||||
final boolean willAutoCorrect = !suggestions.mTypedWordValid
|
|
||||||
&& suggestions.mHasMinimalSuggestion;
|
|
||||||
pos = willAutoCorrect ? 1 - i : i;
|
|
||||||
} else {
|
|
||||||
pos = i;
|
|
||||||
}
|
|
||||||
final CharSequence suggestion = suggestions.getWord(pos);
|
|
||||||
if (suggestion == null) continue;
|
|
||||||
|
|
||||||
final SuggestedWordInfo suggestionInfo = (suggestedWordInfoList != null)
|
|
||||||
? suggestedWordInfoList.get(pos) : null;
|
|
||||||
final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion
|
|
||||||
&& ((pos == 1 && !suggestions.mTypedWordValid)
|
|
||||||
|| (pos == 0 && suggestions.mTypedWordValid));
|
|
||||||
// HACK: even if i == 0, we use mColorOther when this suggestion's length is 1
|
|
||||||
// and there are multiple suggestions, such as the default punctuation list.
|
|
||||||
// TODO: Need to revisit this logic with bigram suggestions
|
|
||||||
final boolean isSuggestedCandidate = (pos != 0);
|
|
||||||
final boolean isPunctuationSuggestions = (suggestion.length() == 1 && count > 1);
|
|
||||||
|
|
||||||
final TextView word = mWords.get(pos);
|
final TextView word = mWords.get(pos);
|
||||||
final TextPaint paint = word.getPaint();
|
|
||||||
final View divider = mDividers.get(pos);
|
final View divider = mDividers.get(pos);
|
||||||
|
final TextPaint paint = word.getPaint();
|
||||||
// TODO: Reorder candidates in strip as appropriate. The center candidate should hold
|
// TODO: Reorder candidates in strip as appropriate. The center candidate should hold
|
||||||
// the word when space is typed (valid typed word or auto corrected word).
|
// the word when space is typed (valid typed word or auto corrected word).
|
||||||
word.setTextColor(getCandidateTextColor(isAutoCorrect,
|
word.setTextColor(stripParams.getCandidateTextColor(pos));
|
||||||
isSuggestedCandidate || isPunctuationSuggestions, suggestionInfo));
|
final CharSequence styled = stripParams.getWord(pos);
|
||||||
final CharSequence styled = params.mTexts.get(pos);
|
|
||||||
|
|
||||||
final TextView info;
|
final TextView info;
|
||||||
if (DBG && suggestionInfo != null
|
if (DBG) {
|
||||||
&& !TextUtils.isEmpty(suggestionInfo.getDebugString())) {
|
final CharSequence debugInfo = stripParams.getDebugInfo(index);
|
||||||
info = mInfos.get(i);
|
if (debugInfo != null) {
|
||||||
info.setText(suggestionInfo.getDebugString());
|
info = mInfos.get(index);
|
||||||
|
info.setText(debugInfo);
|
||||||
|
} else {
|
||||||
|
info = null;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
info = null;
|
info = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final CharSequence text;
|
final CharSequence text;
|
||||||
final float scaleX;
|
final float scaleX;
|
||||||
if (i < countInStrip) {
|
if (index < countInStrip) {
|
||||||
if (i == 0 && params.mCountInStrip == 1) {
|
if (index == 0 && stripParams.mCountInStrip == 1) {
|
||||||
text = getEllipsizedText(styled, params.mMaxWidth, paint);
|
text = getEllipsizedText(styled, stripParams.mMaxWidth, paint);
|
||||||
scaleX = paint.getTextScaleX();
|
scaleX = paint.getTextScaleX();
|
||||||
} else {
|
} else {
|
||||||
text = styled;
|
text = styled;
|
||||||
scaleX = params.mScaleX;
|
scaleX = stripParams.mScaleX;
|
||||||
}
|
}
|
||||||
word.setText(text);
|
word.setText(text);
|
||||||
word.setTextScaleX(scaleX);
|
word.setTextScaleX(scaleX);
|
||||||
if (i != 0) {
|
if (index != 0) {
|
||||||
// Add divider if this isn't the left most suggestion in candidate strip.
|
// Add divider if this isn't the left most suggestion in candidate strip.
|
||||||
mCandidatesStrip.addView(divider);
|
mCandidatesStrip.addView(divider);
|
||||||
}
|
}
|
||||||
mCandidatesStrip.addView(word);
|
mCandidatesStrip.addView(word);
|
||||||
if (params.mCanUseFixedWidthColumns) {
|
if (stripParams.mCanUseFixedWidthColumns) {
|
||||||
setLayoutWeight(word, 1.0f, mCandidateStripHeight);
|
setLayoutWeight(word, 1.0f, mCandidateStripHeight);
|
||||||
} else {
|
} else {
|
||||||
final int width = getTextWidth(text, paint) + params.mPadding;
|
final int width = getTextWidth(text, paint) + stripParams.mPadding;
|
||||||
setLayoutWeight(word, width, mCandidateStripHeight);
|
setLayoutWeight(word, width, mCandidateStripHeight);
|
||||||
}
|
}
|
||||||
if (info != null) {
|
if (info != null) {
|
||||||
|
@ -529,7 +568,7 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
} else {
|
} else {
|
||||||
paint.setTextScaleX(1.0f);
|
paint.setTextScaleX(1.0f);
|
||||||
final int textWidth = getTextWidth(styled, paint);
|
final int textWidth = getTextWidth(styled, paint);
|
||||||
int available = paneWidth - x - params.mPadding;
|
int available = paneWidth - x - paneParams.mPadding;
|
||||||
if (textWidth >= available) {
|
if (textWidth >= available) {
|
||||||
// Needs new row, centering previous row.
|
// Needs new row, centering previous row.
|
||||||
centeringCandidates(centeringFrom, lastView, x, paneWidth);
|
centeringCandidates(centeringFrom, lastView, x, paneWidth);
|
||||||
|
@ -540,11 +579,11 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
// Add divider if this isn't the left most suggestion in current row.
|
// Add divider if this isn't the left most suggestion in current row.
|
||||||
mCandidatesPane.addView(divider);
|
mCandidatesPane.addView(divider);
|
||||||
FrameLayoutCompatUtils.placeViewAt(
|
FrameLayoutCompatUtils.placeViewAt(
|
||||||
divider, x, y + (mCandidateStripHeight - params.mDividerHeight) / 2,
|
divider, x, y + (mCandidateStripHeight - paneParams.mDividerHeight) / 2,
|
||||||
params.mDividerWidth, params.mDividerHeight);
|
paneParams.mDividerWidth, paneParams.mDividerHeight);
|
||||||
x += params.mDividerWidth;
|
x += paneParams.mDividerWidth;
|
||||||
}
|
}
|
||||||
available = paneWidth - x - params.mPadding;
|
available = paneWidth - x - paneParams.mPadding;
|
||||||
text = getEllipsizedText(styled, available, paint);
|
text = getEllipsizedText(styled, available, paint);
|
||||||
scaleX = paint.getTextScaleX();
|
scaleX = paint.getTextScaleX();
|
||||||
word.setText(text);
|
word.setText(text);
|
||||||
|
@ -670,16 +709,11 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onAutoCorrectionInverted(CharSequence autoCorrectedWord) {
|
public void onAutoCorrectionInverted(CharSequence autoCorrectedWord) {
|
||||||
if ((mAutoCorrectHighlight & AUTO_CORRECT_INVERT) == 0)
|
final CharSequence inverted = mStripParams.getInvertedText(autoCorrectedWord);
|
||||||
|
if (inverted == null)
|
||||||
return;
|
return;
|
||||||
final TextView tv = mWords.get(1);
|
final TextView tv = mWords.get(1);
|
||||||
final Spannable word = new SpannableString(autoCorrectedWord);
|
tv.setText(inverted);
|
||||||
final int wordLength = word.length();
|
|
||||||
word.setSpan(mInvertedBackgroundColorSpan, 0, wordLength,
|
|
||||||
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
||||||
word.setSpan(mInvertedForegroundColorSpan, 0, wordLength,
|
|
||||||
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
||||||
tv.setText(word);
|
|
||||||
mShowingAutoCorrectionInverted = true;
|
mShowingAutoCorrectionInverted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -724,7 +758,7 @@ public class CandidateView extends LinearLayout implements OnClickListener {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
final TextView previewText = mPreviewText;
|
final TextView previewText = mPreviewText;
|
||||||
previewText.setTextColor(mColorTypedWord);
|
previewText.setTextColor(mStripParams.mColorTypedWord);
|
||||||
previewText.setText(word);
|
previewText.setText(word);
|
||||||
previewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
|
previewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
|
||||||
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
|
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
|
||||||
|
|
|
@ -31,7 +31,7 @@ public class SuggestedWords {
|
||||||
public final boolean mTypedWordValid;
|
public final boolean mTypedWordValid;
|
||||||
public final boolean mHasMinimalSuggestion;
|
public final boolean mHasMinimalSuggestion;
|
||||||
public final boolean mIsPunctuationSuggestions;
|
public final boolean mIsPunctuationSuggestions;
|
||||||
public final List<SuggestedWordInfo> mSuggestedWordInfoList;
|
private final List<SuggestedWordInfo> mSuggestedWordInfoList;
|
||||||
|
|
||||||
private SuggestedWords(List<CharSequence> words, boolean typedWordValid,
|
private SuggestedWords(List<CharSequence> words, boolean typedWordValid,
|
||||||
boolean hasMinimalSuggestion, boolean isPunctuationSuggestions,
|
boolean hasMinimalSuggestion, boolean isPunctuationSuggestions,
|
||||||
|
@ -55,6 +55,10 @@ public class SuggestedWords {
|
||||||
return mWords.get(pos);
|
return mWords.get(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SuggestedWordInfo getInfo(int pos) {
|
||||||
|
return mSuggestedWordInfoList != null ? mSuggestedWordInfoList.get(pos) : null;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean hasAutoCorrectionWord() {
|
public boolean hasAutoCorrectionWord() {
|
||||||
return mHasMinimalSuggestion && size() > 1 && !mTypedWordValid;
|
return mHasMinimalSuggestion && size() > 1 && !mTypedWordValid;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue