Refactor suggestion strip layout methods a bit

Bug: 9059710
Change-Id: I8dc698c1d61ca51b65b232e6c586472588a4accb
main
Tadashi G. Takaoka 2013-05-24 15:29:45 -07:00
parent a2d8d30f8e
commit 389d308fff
1 changed files with 104 additions and 65 deletions

View File

@ -250,10 +250,12 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
} }
private CharSequence getStyledSuggestionWord(final SuggestedWords suggestedWords, private CharSequence getStyledSuggestionWord(final SuggestedWords suggestedWords,
final int pos) { final int indexInSuggestedWords) {
final String word = suggestedWords.getWord(pos); final String word = suggestedWords.getWord(indexInSuggestedWords);
final boolean isAutoCorrect = pos == 1 && suggestedWords.willAutoCorrect(); final boolean isAutoCorrect = indexInSuggestedWords == 1
final boolean isTypedWordValid = pos == 0 && suggestedWords.mTypedWordValid; && suggestedWords.willAutoCorrect();
final boolean isTypedWordValid = indexInSuggestedWords == 0
&& suggestedWords.mTypedWordValid;
if (!isAutoCorrect && !isTypedWordValid) if (!isAutoCorrect && !isTypedWordValid)
return word; return word;
@ -270,28 +272,31 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
return spannedWord; return spannedWord;
} }
private int getWordPosition(final int index, final SuggestedWords suggestedWords) { private int getIndexInSuggestedWords(final int indexInStrip,
final SuggestedWords suggestedWords) {
// TODO: This works for 3 suggestions. Revisit this algorithm when there are 5 or more // TODO: This works for 3 suggestions. Revisit this algorithm when there are 5 or more
// suggestions. // suggestions.
final int centerPos = suggestedWords.willAutoCorrect() ? 1 : 0; final int mostImportantIndexInSuggestedWords = suggestedWords.willAutoCorrect() ? 1 : 0;
if (index == mCenterSuggestionIndex) { if (indexInStrip == mCenterSuggestionIndex) {
return centerPos; return mostImportantIndexInSuggestedWords;
} else if (index == centerPos) { } else if (indexInStrip == mostImportantIndexInSuggestedWords) {
return mCenterSuggestionIndex; return mCenterSuggestionIndex;
} else { } else {
return index; return indexInStrip;
} }
} }
private int getSuggestionTextColor(final int index, final SuggestedWords suggestedWords, private int getSuggestionTextColor(final int indexInStrip,
final int pos) { final SuggestedWords suggestedWords) {
final int indexInSuggestedWords = getIndexInSuggestedWords(
indexInStrip, suggestedWords);
// TODO: Need to revisit this logic with bigram suggestions // TODO: Need to revisit this logic with bigram suggestions
final boolean isSuggested = (pos != 0); final boolean isSuggested = (indexInSuggestedWords != 0);
final int color; final int color;
if (index == mCenterSuggestionIndex && suggestedWords.willAutoCorrect()) { if (indexInStrip == mCenterSuggestionIndex && suggestedWords.willAutoCorrect()) {
color = mColorAutoCorrect; color = mColorAutoCorrect;
} else if (index == mCenterSuggestionIndex && suggestedWords.mTypedWordValid) { } else if (indexInStrip == mCenterSuggestionIndex && suggestedWords.mTypedWordValid) {
color = mColorValidTypedWord; color = mColorValidTypedWord;
} else if (isSuggested) { } else if (isSuggested) {
color = mColorSuggested; color = mColorSuggested;
@ -301,7 +306,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
if (LatinImeLogger.sDBG && suggestedWords.size() > 1) { if (LatinImeLogger.sDBG && suggestedWords.size() > 1) {
// If we auto-correct, then the autocorrection is in slot 0 and the typed word // If we auto-correct, then the autocorrection is in slot 0 and the typed word
// is in slot 1. // is in slot 1.
if (index == mCenterSuggestionIndex if (indexInStrip == mCenterSuggestionIndex
&& AutoCorrection.shouldBlockAutoCorrectionBySafetyNet( && AutoCorrection.shouldBlockAutoCorrectionBySafetyNet(
suggestedWords.getWord(1), suggestedWords.getWord(0))) { suggestedWords.getWord(1), suggestedWords.getWord(0))) {
return 0xFFFF0000; return 0xFFFF0000;
@ -338,23 +343,56 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
setupTexts(suggestedWords, countInStrip); setupTexts(suggestedWords, countInStrip);
mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip); mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip);
int x = 0; int x = 0;
for (int index = 0; index < countInStrip; index++) { for (int indexInStrip = 0; indexInStrip < countInStrip; indexInStrip++) {
final int pos = getWordPosition(index, suggestedWords); if (indexInStrip != 0) {
final View divider = mDividers.get(indexInStrip);
if (index != 0) {
final View divider = mDividers.get(pos);
// Add divider if this isn't the left most suggestion in suggestions strip. // Add divider if this isn't the left most suggestion in suggestions strip.
addDivider(stripView, divider); addDivider(stripView, divider);
x += divider.getMeasuredWidth(); x += divider.getMeasuredWidth();
} }
final CharSequence styled = mTexts.get(pos); final int width = getSuggestionWidth(indexInStrip, stripWidth);
final TextView word = mWords.get(pos); final TextView word = layoutWord(suggestedWords, indexInStrip, width);
if (index == mCenterSuggestionIndex && mMoreSuggestionsAvailable) { stripView.addView(word);
// TODO: This "more suggestions hint" should have nicely designed icon. setLayoutWeight(word, getSuggestionWeight(indexInStrip),
ViewGroup.LayoutParams.MATCH_PARENT);
x += word.getMeasuredWidth();
if (DBG) {
layoutDebugInfo(suggestedWords, indexInStrip, placer, x);
}
}
}
/**
* Format appropriately the suggested word indirectly specified by
* <code>indexInStrip</code> as text in a corresponding {@link TextView}. When the
* suggested word doesn't exist, the corresponding {@link TextView} will be disabled
* and never respond to user interaction. The suggested word may be shrunk or ellipsized to
* fit in the specified width.
*
* The <code>indexInStrip</code> argument is the index in the suggestion strip. The indices
* increase towards the right for LTR scripts and the left for RTL scripts, starting with 0.
* The index of the most important suggestion is in {@link #mCenterSuggestionIndex}. This
* usually doesn't match the index in <code>suggedtedWords</code> -- see
* {@link #getIndexInSuggestedWords(int,SuggestedWords)}.
*
* @param suggestedWords the list of suggestions.
* @param indexInStrip the in the suggestion strip.
* @param width the maximum width for layout in pixels.
* @return the {@link TextView} containing the suggested word appropriately formatted.
*/
private TextView layoutWord(final SuggestedWords suggestedWords, final int indexInStrip,
final int width) {
final int indexInSuggestedWords = getIndexInSuggestedWords(
indexInStrip, suggestedWords);
final CharSequence styled = mTexts.get(indexInSuggestedWords);
final TextView word = mWords.get(indexInSuggestedWords);
if (indexInStrip == mCenterSuggestionIndex && mMoreSuggestionsAvailable) {
// TODO: This "more suggestions hint" should have a nicely designed icon.
word.setCompoundDrawablesWithIntrinsicBounds( word.setCompoundDrawablesWithIntrinsicBounds(
null, null, null, mMoreSuggestionsHint); null, null, null, mMoreSuggestionsHint);
// HACK: To align with other TextView that has no compound drawables. // HACK: Align with other TextViews that have no compound drawables.
word.setCompoundDrawablePadding(-mMoreSuggestionsHint.getIntrinsicHeight()); word.setCompoundDrawablePadding(-mMoreSuggestionsHint.getIntrinsicHeight());
} else { } else {
word.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); word.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
@ -362,43 +400,44 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
// Disable this suggestion if the suggestion is null or empty. // Disable this suggestion if the suggestion is null or empty.
word.setEnabled(!TextUtils.isEmpty(styled)); word.setEnabled(!TextUtils.isEmpty(styled));
word.setTextColor(getSuggestionTextColor(index, suggestedWords, pos)); word.setTextColor(getSuggestionTextColor(indexInStrip, suggestedWords));
final int width = getSuggestionWidth(index, stripWidth);
final CharSequence text = getEllipsizedText(styled, width, word.getPaint()); final CharSequence text = getEllipsizedText(styled, width, word.getPaint());
final float scaleX = word.getTextScaleX(); final float scaleX = word.getTextScaleX();
word.setText(text); // TextView.setText() resets text scale x to 1.0. word.setText(text); // TextView.setText() resets text scale x to 1.0.
word.setTextScaleX(scaleX); word.setTextScaleX(scaleX);
stripView.addView(word); return word;
setLayoutWeight( }
word, getSuggestionWeight(index), ViewGroup.LayoutParams.MATCH_PARENT);
x += word.getMeasuredWidth();
if (DBG && pos < suggestedWords.size()) { private void layoutDebugInfo(final SuggestedWords suggestedWords, final int indexInStrip,
final String debugInfo = Utils.getDebugInfo(suggestedWords, pos); final ViewGroup placer, final int x) {
if (debugInfo != null) { final int indexInSuggestedWords = getIndexInSuggestedWords(
final TextView info = mInfos.get(pos); indexInStrip, suggestedWords);
if (indexInSuggestedWords >= suggestedWords.size()) {
return;
}
final String debugInfo = Utils.getDebugInfo(suggestedWords, indexInSuggestedWords);
if (debugInfo == null) {
return;
}
final TextView info = mInfos.get(indexInSuggestedWords);
info.setText(debugInfo); info.setText(debugInfo);
placer.addView(info); placer.addView(info);
info.measure(ViewGroup.LayoutParams.WRAP_CONTENT, info.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup.LayoutParams.WRAP_CONTENT);
final int infoWidth = info.getMeasuredWidth(); final int infoWidth = info.getMeasuredWidth();
final int y = info.getMeasuredHeight(); final int y = info.getMeasuredHeight();
ViewLayoutUtils.placeViewAt( ViewLayoutUtils.placeViewAt(
info, x - infoWidth, y, infoWidth, info.getMeasuredHeight()); info, x - infoWidth, y, infoWidth, info.getMeasuredHeight());
} }
}
}
}
private int getSuggestionWidth(final int index, final int maxWidth) { private int getSuggestionWidth(final int indexInStrip, final int maxWidth) {
final int paddings = mPadding * mSuggestionsCountInStrip; final int paddings = mPadding * mSuggestionsCountInStrip;
final int dividers = mDividerWidth * (mSuggestionsCountInStrip - 1); final int dividers = mDividerWidth * (mSuggestionsCountInStrip - 1);
final int availableWidth = maxWidth - paddings - dividers; final int availableWidth = maxWidth - paddings - dividers;
return (int)(availableWidth * getSuggestionWeight(index)); return (int)(availableWidth * getSuggestionWeight(indexInStrip));
} }
private float getSuggestionWeight(final int index) { private float getSuggestionWeight(final int indexInStrip) {
if (index == mCenterSuggestionIndex) { if (indexInStrip == mCenterSuggestionIndex) {
return mCenterSuggestionWeight; return mCenterSuggestionWeight;
} else { } else {
// TODO: Revisit this for cases of 5 or more suggestions // TODO: Revisit this for cases of 5 or more suggestions
@ -422,16 +461,16 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
private void layoutPunctuationSuggestions(final SuggestedWords suggestedWords, private void layoutPunctuationSuggestions(final SuggestedWords suggestedWords,
final ViewGroup stripView) { final ViewGroup stripView) {
final int countInStrip = Math.min(suggestedWords.size(), PUNCTUATIONS_IN_STRIP); final int countInStrip = Math.min(suggestedWords.size(), PUNCTUATIONS_IN_STRIP);
for (int index = 0; index < countInStrip; index++) { for (int indexInStrip = 0; indexInStrip < countInStrip; indexInStrip++) {
if (index != 0) { if (indexInStrip != 0) {
// Add divider if this isn't the left most suggestion in suggestions strip. // Add divider if this isn't the left most suggestion in suggestions strip.
addDivider(stripView, mDividers.get(index)); addDivider(stripView, mDividers.get(indexInStrip));
} }
final TextView word = mWords.get(index); final TextView word = mWords.get(indexInStrip);
word.setEnabled(true); word.setEnabled(true);
word.setTextColor(mColorAutoCorrect); word.setTextColor(mColorAutoCorrect);
final String text = suggestedWords.getWord(index); final String text = suggestedWords.getWord(indexInStrip);
word.setText(text); word.setText(text);
word.setTextScaleX(1.0f); word.setTextScaleX(1.0f);
word.setCompoundDrawables(null, null, null, null); word.setCompoundDrawables(null, null, null, null);