Fixed count suggestions strip

Bug: 5023981
Change-Id: I434d23bdfb653989866d3822c978cd929a2b553c
This commit is contained in:
Tadashi G. Takaoka 2011-08-05 02:54:52 -07:00
parent 5bcf8ee66c
commit 60a004f78e
7 changed files with 118 additions and 144 deletions

View File

@ -51,5 +51,4 @@
<fraction name="key_uppercase_letter_ratio">29%</fraction> <fraction name="key_uppercase_letter_ratio">29%</fraction>
<dimen name="candidate_strip_padding">40.0mm</dimen> <dimen name="candidate_strip_padding">40.0mm</dimen>
<integer name="candidate_count_in_strip">5</integer>
</resources> </resources>

View File

@ -59,5 +59,4 @@
<dimen name="key_preview_offset_ics">0.05in</dimen> <dimen name="key_preview_offset_ics">0.05in</dimen>
<dimen name="candidate_strip_padding">40.0mm</dimen> <dimen name="candidate_strip_padding">40.0mm</dimen>
<integer name="candidate_count_in_strip">5</integer>
</resources> </resources>

View File

@ -116,6 +116,7 @@
<attr name="colorAutoCorrect" format="color" /> <attr name="colorAutoCorrect" format="color" />
<attr name="colorSuggested" format="color" /> <attr name="colorSuggested" format="color" />
<attr name="candidateCountInStrip" format="integer" /> <attr name="candidateCountInStrip" format="integer" />
<attr name="centerCandidatePercentile" format="integer" />
</declare-styleable> </declare-styleable>
<declare-styleable name="Keyboard"> <declare-styleable name="Keyboard">

View File

@ -86,6 +86,7 @@
<dimen name="candidate_padding">6dip</dimen> <dimen name="candidate_padding">6dip</dimen>
<dimen name="candidate_text_size">18dip</dimen> <dimen name="candidate_text_size">18dip</dimen>
<integer name="candidate_count_in_strip">3</integer> <integer name="candidate_count_in_strip">3</integer>
<integer name="center_candidate_percentile">40</integer>
<!-- If the screen height in landscape is larger than the below value, then the keyboard <!-- If the screen height in landscape is larger than the below value, then the keyboard
will not go into extract (fullscreen) mode. --> will not go into extract (fullscreen) mode. -->

View File

@ -19,7 +19,7 @@
--> -->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- Symbols that are suggested between words --> <!-- Symbols that are suggested between words -->
<string name="suggested_punctuations">!?,\u0022\u0027:();-/@_</string> <string name="suggested_punctuations">!?,\u0022:;()\u0027-/@_</string>
<!-- Symbols that should be swapped with a magic space --> <!-- Symbols that should be swapped with a magic space -->
<string name="magic_space_swapping_symbols">.,;:!?)]}\u0022</string> <string name="magic_space_swapping_symbols">.,;:!?)]}\u0022</string>
<!-- Symbols that should strip a magic space --> <!-- Symbols that should strip a magic space -->

View File

@ -90,6 +90,7 @@
<item name="colorAutoCorrect">#FFFCAE00</item> <item name="colorAutoCorrect">#FFFCAE00</item>
<item name="colorSuggested">#FFFCAE00</item> <item name="colorSuggested">#FFFCAE00</item>
<item name="candidateCountInStrip">@integer/candidate_count_in_strip</item> <item name="candidateCountInStrip">@integer/candidate_count_in_strip</item>
<item name="centerCandidatePercentile">@integer/center_candidate_percentile</item>
</style> </style>
<!-- Theme "Basic high contrast" --> <!-- Theme "Basic high contrast" -->
<style name="KeyboardView.HighContrast" parent="KeyboardView"> <style name="KeyboardView.HighContrast" parent="KeyboardView">
@ -192,6 +193,7 @@
<item name="colorAutoCorrect">#FF3DC8FF</item> <item name="colorAutoCorrect">#FF3DC8FF</item>
<item name="colorSuggested">#FFFFFFFF</item> <item name="colorSuggested">#FFFFFFFF</item>
<item name="candidateCountInStrip">@integer/candidate_count_in_strip</item> <item name="candidateCountInStrip">@integer/candidate_count_in_strip</item>
<item name="centerCandidatePercentile">@integer/center_candidate_percentile</item>
</style> </style>
<style name="PopupMiniKeyboardAnimation"> <style name="PopupMiniKeyboardAnimation">
<item name="android:windowEnterAnimation">@anim/mini_keyboard_fadein</item> <item name="android:windowEnterAnimation">@anim/mini_keyboard_fadein</item>

View File

@ -259,12 +259,15 @@ public class CandidateView extends LinearLayout implements OnClickListener {
private static class SuggestionsStripParams extends CandidateViewParams { private static class SuggestionsStripParams extends CandidateViewParams {
private static final int DEFAULT_CANDIDATE_COUNT_IN_STRIP = 3; private static final int DEFAULT_CANDIDATE_COUNT_IN_STRIP = 3;
private static final int DEFAULT_CENTER_CANDIDATE_PERCENTILE = 40;
private static final int PUNCTUATIONS_IN_STRIP = 6; private static final int PUNCTUATIONS_IN_STRIP = 6;
private final int mColorTypedWord; private final int mColorTypedWord;
private final int mColorAutoCorrect; private final int mColorAutoCorrect;
private final int mColorSuggestedCandidate; private final int mColorSuggestedCandidate;
private final int mCandidateCountInStrip; private final int mCandidateCountInStrip;
private final float mCenterCandidateWeight;
private final int mCenterCandidateIndex;
private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD); private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD);
private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan(); private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan();
@ -278,17 +281,6 @@ public class CandidateView extends LinearLayout implements OnClickListener {
private final int mAutoCorrectHighlight; private final int mAutoCorrectHighlight;
private final ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(); private final ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>();
private SuggestedWords mSuggestedWords;
private int mCountInStrip;
// True if the mCountInStrip suggestions can fit in suggestion strip in equally divided
// width without squeezing the text.
private boolean mCanUseFixedWidthColumns;
private int mMaxWidth;
private int mAvailableWidthForWords;
private int mConstantWidthForPaddings;
private int mVariableWidthForWords;
private float mScaleX;
public SuggestionsStripParams(Context context, AttributeSet attrs, int defStyle, public SuggestionsStripParams(Context context, AttributeSet attrs, int defStyle,
List<TextView> words, List<View> dividers, List<TextView> infos, View control) { List<TextView> words, List<View> dividers, List<TextView> infos, View control) {
@ -302,8 +294,13 @@ public class CandidateView extends LinearLayout implements OnClickListener {
mCandidateCountInStrip = a.getInt( mCandidateCountInStrip = a.getInt(
R.styleable.CandidateView_candidateCountInStrip, R.styleable.CandidateView_candidateCountInStrip,
DEFAULT_CANDIDATE_COUNT_IN_STRIP); DEFAULT_CANDIDATE_COUNT_IN_STRIP);
mCenterCandidateWeight = a.getInt(
R.styleable.CandidateView_centerCandidatePercentile,
DEFAULT_CENTER_CANDIDATE_PERCENTILE) / 100.0f;
a.recycle(); a.recycle();
mCenterCandidateIndex = mCandidateCountInStrip / 2;
mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff); mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff);
mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord); mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord);
@ -328,35 +325,38 @@ public class CandidateView extends LinearLayout implements OnClickListener {
return spannedWord; return spannedWord;
} }
private int getWordPosition(int index) { private static boolean willAutoCorrect(SuggestedWords suggestions) {
if (index >= 2) { return !suggestions.mTypedWordValid && suggestions.mHasMinimalSuggestion;
return index;
}
final boolean willAutoCorrect = !mSuggestedWords.mTypedWordValid
&& mSuggestedWords.mHasMinimalSuggestion;
return willAutoCorrect ? 1 - index : index;
} }
private int getCandidateTextColor(int pos) { private int getWordPosition(int index, SuggestedWords suggestions) {
final SuggestedWords suggestions = mSuggestedWords; // TODO: This works for 3 suggestions. Revisit this algorithm when there are 5 or more
final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion // suggestions.
&& ((pos == 1 && !suggestions.mTypedWordValid) final int centerPos = willAutoCorrect(suggestions) ? 1 : 0;
|| (pos == 0 && suggestions.mTypedWordValid)); if (index == mCenterCandidateIndex) {
return centerPos;
} else if (index == centerPos) {
return mCenterCandidateIndex;
} else {
return index;
}
}
private int getCandidateTextColor(int index, SuggestedWords suggestions, int pos) {
// TODO: Need to revisit this logic with bigram suggestions // TODO: Need to revisit this logic with bigram suggestions
final boolean isSuggestedCandidate = (pos != 0); final boolean isSuggestedCandidate = (pos != 0);
final boolean isPunctuationSuggestions = suggestions.isPunctuationSuggestions();
final int color; final int color;
if (isPunctuationSuggestions) { if (index == mCenterCandidateIndex && willAutoCorrect(suggestions)) {
color = mColorTypedWord;
} else if (isAutoCorrect) {
color = mColorAutoCorrect; color = mColorAutoCorrect;
} else if (isSuggestedCandidate) { } else if (isSuggestedCandidate) {
color = mColorSuggestedCandidate; color = mColorSuggestedCandidate;
} else { } else {
color = mColorTypedWord; color = mColorTypedWord;
} }
final SuggestedWordInfo info = suggestions.getInfo(pos);
final SuggestedWordInfo info = (pos < suggestions.size())
? suggestions.getInfo(pos) : null;
if (info != null && info.isPreviousSuggestedWord()) { if (info != null && info.isPreviousSuggestedWord()) {
return applyAlpha(color, 0.5f); return applyAlpha(color, 0.5f);
} else { } else {
@ -381,136 +381,105 @@ public class CandidateView extends LinearLayout implements OnClickListener {
public int layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup paneView, public int layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup paneView,
int stripWidth) { int stripWidth) {
mSuggestedWords = suggestions; if (suggestions.isPunctuationSuggestions()) {
final int maxCount = suggestions.isPunctuationSuggestions() return layoutPunctuationSuggestions(suggestions, stripView);
? PUNCTUATIONS_IN_STRIP : mCandidateCountInStrip; }
final int size = suggestions.size();
setupTexts(suggestions, size);
mCountInStrip = Math.min(maxCount, size);
mScaleX = 1.0f;
calculateParameters(size, stripWidth);
int infoX = 0; final int countInStrip = mCandidateCountInStrip;
for (int index = 0; index < mCountInStrip; index++) { setupTexts(suggestions, countInStrip);
final int pos = getWordPosition(index); final int maxWidth = (suggestions.size() <= countInStrip)
final TextView word = mWords.get(pos); ? stripWidth : stripWidth - mControlWidth;
final View divider = mDividers.get(pos);
final TextPaint paint = word.getPaint();
// 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).
word.setTextColor(getCandidateTextColor(pos));
final CharSequence styled = mTexts.get(pos);
final TextView info; int x = 0;
if (DBG) { for (int index = 0; index < countInStrip; index++) {
final CharSequence debugInfo = getDebugInfo(mSuggestedWords, index); final int pos = getWordPosition(index, suggestions);
if (debugInfo != null) {
info = mInfos.get(index); if (index != 0) {
info.setText(debugInfo); final View divider = mDividers.get(pos);
} else { // Add divider if this isn't the left most suggestion in candidate strip.
info = null; stripView.addView(divider);
}
} else {
info = null;
} }
final CharSequence text; final CharSequence styled = mTexts.get(pos);
final float scaleX; final TextView word = mWords.get(pos);
if (index == 0 && mCountInStrip == 1) { // Disable this candidate if the suggestion is null or empty.
text = getEllipsizedText(styled, mMaxWidth, paint); word.setEnabled(!TextUtils.isEmpty(styled));
scaleX = paint.getTextScaleX(); word.setTextColor(getCandidateTextColor(index, suggestions, pos));
} else { final int width = getCandidateWidth(index, maxWidth);
text = styled; final CharSequence text = getEllipsizedText(styled, width, word.getPaint());
scaleX = mScaleX; final float scaleX = word.getTextScaleX();
} word.setText(text); // TextView.setText() resets text scale x to 1.0.
word.setText(text); word.setTextScaleX(scaleX);
word.setTextScaleX(scaleX); stripView.addView(word);
if (index != 0) { setLayoutWeight(word, getCandidateWeight(index), mCandidateStripHeight);
// Add divider if this isn't the left most suggestion in candidate strip.
stripView.addView(divider); if (DBG) {
} final CharSequence debugInfo = getDebugInfo(suggestions, pos);
stripView.addView(word); if (debugInfo != null) {
if (mCanUseFixedWidthColumns) { final TextView info = mInfos.get(pos);
setLayoutWeight(word, 1.0f, mCandidateStripHeight); info.setText(debugInfo);
} else {
final int width = getTextWidth(text, paint) + mPadding;
setLayoutWeight(word, width, mCandidateStripHeight);
}
if (info != null) {
paneView.addView(info); paneView.addView(info);
info.measure(WRAP_CONTENT, WRAP_CONTENT); info.measure(WRAP_CONTENT, WRAP_CONTENT);
final int width = info.getMeasuredWidth(); final int infoWidth = info.getMeasuredWidth();
final int y = info.getMeasuredHeight(); final int y = info.getMeasuredHeight();
FrameLayoutCompatUtils.placeViewAt(info, infoX, 0, width, y); FrameLayoutCompatUtils.placeViewAt(info, x, 0, infoWidth, y);
infoX += width * 2; x += infoWidth * 2;
} }
}
} }
return mCountInStrip; return countInStrip;
} }
private void calculateParameters(int size, int maxWidth) { private int getCandidateWidth(int index, int maxWidth) {
do { final int paddings = mPadding * mCandidateCountInStrip;
mMaxWidth = maxWidth; final int dividers = mDividerWidth * (mCandidateCountInStrip - 1);
if (size > mCountInStrip) { final int availableWidth = maxWidth - paddings - dividers;
mMaxWidth -= mControlWidth; return (int)(availableWidth * getCandidateWeight(index));
}
tryLayout();
if (mCanUseFixedWidthColumns) {
return;
}
if (mVariableWidthForWords <= mAvailableWidthForWords) {
return;
}
final float scaleX = mAvailableWidthForWords / (float)mVariableWidthForWords;
if (scaleX >= MIN_TEXT_XSCALE) {
mScaleX = scaleX;
return;
}
mCountInStrip--;
} while (mCountInStrip > 1);
} }
private void tryLayout() { private float getCandidateWeight(int index) {
final int maxCount = mCountInStrip; if (index == mCenterCandidateIndex) {
final int dividers = mDividerWidth * (maxCount - 1); return mCenterCandidateWeight;
mConstantWidthForPaddings = dividers + mPadding * maxCount; } else {
mAvailableWidthForWords = mMaxWidth - mConstantWidthForPaddings; // TODO: Revisit this for cases of 5 or more suggestions
return (1.0f - mCenterCandidateWeight) / (mCandidateCountInStrip - 1);
mPaint.setTextScaleX(mScaleX);
final int maxFixedWidthForWord = (mMaxWidth - dividers) / maxCount - mPadding;
mCanUseFixedWidthColumns = true;
mVariableWidthForWords = 0;
for (int i = 0; i < maxCount; i++) {
final int width = getTextWidth(mTexts.get(i), mPaint);
if (width > maxFixedWidthForWord)
mCanUseFixedWidthColumns = false;
mVariableWidthForWords += width;
} }
} }
private void setupTexts(SuggestedWords suggestions, int count) { private void setupTexts(SuggestedWords suggestions, int countInStrip) {
mTexts.clear(); mTexts.clear();
for (int i = 0; i < count; i++) { final int count = Math.min(suggestions.size(), countInStrip);
final CharSequence word = suggestions.getWord(i); for (int pos = 0; pos < count; pos++) {
final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion final CharSequence word = suggestions.getWord(pos);
&& ((i == 1 && !suggestions.mTypedWordValid) final boolean isAutoCorrect = pos == 1 && willAutoCorrect(suggestions);
|| (i == 0 && suggestions.mTypedWordValid));
final CharSequence styled = getStyledCandidateWord(word, isAutoCorrect); final CharSequence styled = getStyledCandidateWord(word, isAutoCorrect);
mTexts.add(styled); mTexts.add(styled);
} }
for (int pos = count; pos < countInStrip; pos++) {
// Make this inactive for touches in layout().
mTexts.add(null);
}
} }
@Override private int layoutPunctuationSuggestions(SuggestedWords suggestions, ViewGroup stripView) {
public String toString() { final int countInStrip = Math.min(suggestions.size(), PUNCTUATIONS_IN_STRIP);
return String.format( for (int index = 0; index < countInStrip; index++) {
"count=%d width=%d avail=%d fixcol=%s scaleX=%4.2f const=%d var=%d", if (index != 0) {
mCountInStrip, mMaxWidth, mAvailableWidthForWords, mCanUseFixedWidthColumns, // Add divider if this isn't the left most suggestion in candidate strip.
mScaleX, mConstantWidthForPaddings, mVariableWidthForWords); stripView.addView(mDividers.get(index));
}
final TextView word = mWords.get(index);
word.setEnabled(true);
word.setTextColor(mColorTypedWord);
final CharSequence text = suggestions.getWord(index);
word.setText(text);
word.setTextScaleX(1.0f);
stripView.addView(word);
setLayoutWeight(word, 1.0f, mCandidateStripHeight);
}
return countInStrip;
} }
} }
@ -548,13 +517,13 @@ public class CandidateView extends LinearLayout implements OnClickListener {
mPreviewPopup.setBackgroundDrawable(null); mPreviewPopup.setBackgroundDrawable(null);
mCandidatesStrip = (ViewGroup)findViewById(R.id.candidates_strip); mCandidatesStrip = (ViewGroup)findViewById(R.id.candidates_strip);
for (int i = 0; i < MAX_SUGGESTIONS; i++) { for (int pos = 0; pos < MAX_SUGGESTIONS; pos++) {
final TextView word = (TextView)inflater.inflate(R.layout.candidate_word, null); final TextView word = (TextView)inflater.inflate(R.layout.candidate_word, null);
word.setTag(i); word.setTag(pos);
word.setOnClickListener(this); word.setOnClickListener(this);
mWords.add(word); mWords.add(word);
final View divider = inflater.inflate(R.layout.candidate_divider, null); final View divider = inflater.inflate(R.layout.candidate_divider, null);
divider.setTag(i); divider.setTag(pos);
divider.setOnClickListener(this); divider.setOnClickListener(this);
mDividers.add(divider); mDividers.add(divider);
mInfos.add((TextView)inflater.inflate(R.layout.candidate_info, null)); mInfos.add((TextView)inflater.inflate(R.layout.candidate_info, null));
@ -649,7 +618,7 @@ public class CandidateView extends LinearLayout implements OnClickListener {
} }
private static CharSequence getDebugInfo(SuggestedWords suggestions, int pos) { private static CharSequence getDebugInfo(SuggestedWords suggestions, int pos) {
if (DBG) { if (DBG && pos < suggestions.size()) {
final SuggestedWordInfo wordInfo = suggestions.getInfo(pos); final SuggestedWordInfo wordInfo = suggestions.getInfo(pos);
if (wordInfo != null) { if (wordInfo != null) {
final CharSequence debugInfo = wordInfo.getDebugString(); final CharSequence debugInfo = wordInfo.getDebugString();
@ -695,14 +664,17 @@ public class CandidateView extends LinearLayout implements OnClickListener {
TextPaint paint) { TextPaint paint) {
paint.setTextScaleX(1.0f); paint.setTextScaleX(1.0f);
final int width = getTextWidth(text, paint); final int width = getTextWidth(text, paint);
final float scaleX = Math.min(maxWidth / (float)width, 1.0f); if (width <= maxWidth) {
return text;
}
final float scaleX = maxWidth / (float)width;
if (scaleX >= MIN_TEXT_XSCALE) { if (scaleX >= MIN_TEXT_XSCALE) {
paint.setTextScaleX(scaleX); paint.setTextScaleX(scaleX);
return text; return text;
} }
// Note that TextUtils.ellipsize() use text-x-scale as 1.0 if ellipsize is needed. To get // Note that TextUtils.ellipsize() use text-x-scale as 1.0 if ellipsize is needed. To get
// squeezed and ellipsezed text, passes enlarged width (maxWidth / MIN_TEXT_XSCALE). // squeezed and ellipsized text, passes enlarged width (maxWidth / MIN_TEXT_XSCALE).
final CharSequence ellipsized = TextUtils.ellipsize( final CharSequence ellipsized = TextUtils.ellipsize(
text, paint, maxWidth / MIN_TEXT_XSCALE, TextUtils.TruncateAt.MIDDLE); text, paint, maxWidth / MIN_TEXT_XSCALE, TextUtils.TruncateAt.MIDDLE);
paint.setTextScaleX(MIN_TEXT_XSCALE); paint.setTextScaleX(MIN_TEXT_XSCALE);