Merge "Adaptive suggestions strip"

This commit is contained in:
Tadashi G. Takaoka 2011-06-30 08:52:04 -07:00 committed by Android (Google) Code Review
commit 7e7244873a
21 changed files with 359 additions and 219 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 936 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 713 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 681 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

View file

@ -18,12 +18,13 @@
*/ */
--> -->
<ImageView <TextView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:src="@drawable/keyboard_suggest_strip_divider" android:text="@string/label_candidate_divider"
android:textSize="@dimen/candidate_text_size"
android:gravity="center"
android:padding="0dp" android:padding="0dp"
android:focusable="false" android:focusable="false"
android:clickable="false" android:clickable="false" />
android:gravity="center_vertical|center_horizontal" />

View file

@ -18,16 +18,19 @@
*/ */
--> -->
<Button <TextView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:minWidth="@dimen/candidate_min_width" android:minWidth="@dimen/candidate_min_width"
android:textSize="@dimen/candidate_text_size" android:textSize="@dimen/candidate_text_size"
android:gravity="center"
android:paddingLeft="@dimen/candidate_padding" android:paddingLeft="@dimen/candidate_padding"
android:paddingTop="0dp" android:paddingTop="0dp"
android:paddingRight="@dimen/candidate_padding" android:paddingRight="@dimen/candidate_padding"
android:paddingBottom="0dp" android:paddingBottom="0dp"
android:focusable="false"
android:clickable="false"
android:singleLine="true" android:singleLine="true"
android:ellipsize="none" android:ellipsize="none"
style="?attr/suggestionBackgroundStyle" /> style="?attr/suggestionBackgroundStyle" />

View file

@ -25,73 +25,39 @@
<LinearLayout <LinearLayout
android:id="@+id/candidates_strip" android:id="@+id/candidates_strip"
android:orientation="horizontal" android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_weight="1.0" android:layout_weight="1.0"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:gravity="center"
> >
<include
android:id="@+id/word_left"
layout="@layout/candidate_word" />
<include
android:id="@+id/info_left"
layout="@layout/candidate_info" />
</RelativeLayout>
<include
layout="@layout/candidate_divider" />
<RelativeLayout
android:layout_weight="1.0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
>
<include
android:id="@+id/word_center"
layout="@layout/candidate_word" />
<include
android:id="@+id/info_center"
layout="@layout/candidate_info" />
</RelativeLayout>
<include
layout="@layout/candidate_divider" />
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1.0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
>
<RelativeLayout
android:layout_weight="1.0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
>
<include
android:id="@+id/word_right"
layout="@layout/candidate_word" />
<include
android:id="@+id/info_right"
layout="@layout/candidate_info" />
</RelativeLayout>
<!-- Image drawables are set in CandidateView constructor -->
<ImageButton
android:id="@+id/expand_candidates_pane"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="@null" />
<ImageButton
android:id="@+id/close_candidates_pane"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="@null" />
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/candidates_pane_control"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/expand_candidates_pane"
android:text="@string/label_expand_candidates_pane"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="30dp"
android:textSize="@dimen/candidate_text_size"
android:padding="0dp"
android:visibility="visible"
style="?attr/suggestionBackgroundStyle" />
<TextView
android:id="@+id/close_candidates_pane"
android:text="@string/label_close_candidates_pane"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="30dp"
android:textSize="@dimen/candidate_text_size"
android:padding="0dp"
android:visibility="gone"
style="?attr/suggestionBackgroundStyle" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/touch_to_save" android:id="@+id/touch_to_save"

View file

@ -53,4 +53,5 @@
<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

@ -58,4 +58,5 @@
<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

@ -120,8 +120,8 @@
<attr name="colorTypedWord" format="color" /> <attr name="colorTypedWord" format="color" />
<attr name="colorAutoCorrect" format="color" /> <attr name="colorAutoCorrect" format="color" />
<attr name="colorSuggested" format="color" /> <attr name="colorSuggested" format="color" />
<attr name="iconExpandPane" format="reference" /> <attr name="colorDivider" format="color" />
<attr name="iconClosePane" format="reference" /> <attr name="candidateCountInStrip" format="integer" />
</declare-styleable> </declare-styleable>
<declare-styleable name="Keyboard"> <declare-styleable name="Keyboard">

View file

@ -82,6 +82,8 @@
<dimen name="candidate_min_width">44dip</dimen> <dimen name="candidate_min_width">44dip</dimen>
<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>
<!-- 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. -->
<dimen name="max_height_for_fullscreen">2.5in</dimen> <dimen name="max_height_for_fullscreen">2.5in</dimen>

View file

@ -43,6 +43,13 @@
<!-- Label for "switch to phone symbols" key. Must be short to fit on key! --> <!-- Label for "switch to phone symbols" key. Must be short to fit on key! -->
<string name="label_to_phone_symbols_key">\uff0a\uff03\uff08</string> <string name="label_to_phone_symbols_key">\uff0a\uff03\uff08</string>
<!-- Character for candidate divider (BOX DRAWINGS LIGHT VERTICAL) -->
<string name="label_candidate_divider">\u2502</string>
<!-- Character for expand candidates pane (BLACK DOWN-POINTING TRIANGLE) -->
<string name="label_expand_candidates_pane">\u25bc</string>
<!-- Character for close candidates pane (BLACK UP-POINTING TRIANGLE) -->
<string name="label_close_candidates_pane">\u25b2</string>
<!-- Option values to show/hide the settings key in onscreen keyboard --> <!-- Option values to show/hide the settings key in onscreen keyboard -->
<!-- Automatically decide to show or hide the settings key --> <!-- Automatically decide to show or hide the settings key -->
<string name="settings_key_mode_auto">0</string> <string name="settings_key_mode_auto">0</string>

View file

@ -92,8 +92,8 @@
<item name="colorTypedWord">#FFFFFFFF</item> <item name="colorTypedWord">#FFFFFFFF</item>
<item name="colorAutoCorrect">#FFFCAE00</item> <item name="colorAutoCorrect">#FFFCAE00</item>
<item name="colorSuggested">#FFFCAE00</item> <item name="colorSuggested">#FFFCAE00</item>
<item name="iconExpandPane">@drawable/btn_expand_candidates_pane</item> <item name="colorDivider">#20FFFFFF</item>
<item name="iconClosePane">@drawable/btn_close_candidates_pane</item>" <item name="candidateCountInStrip">@integer/candidate_count_in_strip</item>
</style> </style>
<!-- Theme "Basic high contrast" --> <!-- Theme "Basic high contrast" -->
<style name="KeyboardView.HighContrast" parent="KeyboardView"> <style name="KeyboardView.HighContrast" parent="KeyboardView">
@ -189,12 +189,12 @@
<item name="android:background">@drawable/keyboard_popup_panel_background_holo</item> <item name="android:background">@drawable/keyboard_popup_panel_background_holo</item>
</style> </style>
<style name="CandidateViewStyle.IceCreamSandwich" parent="SuggestionsStripBackgroundStyle.IceCreamSandwich"> <style name="CandidateViewStyle.IceCreamSandwich" parent="SuggestionsStripBackgroundStyle.IceCreamSandwich">
<item name="autoCorrectHighlight">autoCorrectUnderline|autoCorrectInvert</item> <item name="autoCorrectHighlight">autoCorrectBold|autoCorrectInvert</item>
<item name="colorTypedWord">#FFFFFFFF</item> <item name="colorTypedWord">#FFFFFFFF</item>
<item name="colorAutoCorrect">#FFFFFFFF</item> <item name="colorAutoCorrect">#FFFFFFFF</item>
<item name="colorSuggested">#FFFFFFFF</item> <item name="colorSuggested">#FFFFFFFF</item>
<item name="iconExpandPane">@drawable/btn_expand_candidates_pane</item> <item name="colorDivider">#20FFFFFF</item>
<item name="iconClosePane">@drawable/btn_close_candidates_pane</item>" <item name="candidateCountInStrip">@integer/candidate_count_in_strip</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

@ -30,6 +30,7 @@ import android.text.TextUtils;
import android.text.style.BackgroundColorSpan; import android.text.style.BackgroundColorSpan;
import android.text.style.CharacterStyle; import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan; import android.text.style.UnderlineSpan;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.Gravity; import android.view.Gravity;
@ -38,7 +39,6 @@ import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener; import android.view.View.OnLongClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import android.widget.TextView; import android.widget.TextView;
@ -57,24 +57,29 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
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(); 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 MATCH_PARENT = MeasureSpec.makeMeasureSpec( private static final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;
-1, MeasureSpec.UNSPECIFIED); private static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
private static final boolean DBG = LatinImeLogger.sDBG; private static final boolean DBG = LatinImeLogger.sDBG;
private final View mCandidatesStrip; private final ViewGroup mCandidatesStrip;
private static final int NUM_CANDIDATES_IN_STRIP = 3; private final int mCandidateCountInStrip;
private final ImageView mExpandCandidatesPane; private static final int DEFAULT_CANDIDATE_COUNT_IN_STRIP = 3;
private final ImageView mCloseCandidatesPane; private final ViewGroup mCandidatesPaneControl;
private final TextView mExpandCandidatesPane;
private final TextView mCloseCandidatesPane;
private ViewGroup mCandidatesPane; private ViewGroup mCandidatesPane;
private ViewGroup mCandidatesPaneContainer; private ViewGroup mCandidatesPaneContainer;
private View mKeyboardView; private View mKeyboardView;
private final ArrayList<TextView> mWords = new ArrayList<TextView>(); private final ArrayList<TextView> mWords = new ArrayList<TextView>();
private final ArrayList<TextView> mInfos = new ArrayList<TextView>(); private final ArrayList<TextView> mInfos = new ArrayList<TextView>();
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 mInvertedForegroundColorSpan;
private final CharacterStyle mInvertedBackgroundColorSpan; private final CharacterStyle mInvertedBackgroundColorSpan;
@ -85,6 +90,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
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 mColorDivider;
private final PopupWindow mPreviewPopup; private final PopupWindow mPreviewPopup;
private final TextView mPreviewText; private final TextView mPreviewText;
@ -96,8 +103,9 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
private boolean mShowingAutoCorrectionInverted; private boolean mShowingAutoCorrectionInverted;
private boolean mShowingAddToDictionary; private boolean mShowingAddToDictionary;
private static final float MIN_TEXT_XSCALE = 0.4f; private final CandidateViewLayoutParams mParams;
private static final String ELLIPSIS = "\u2026"; private static final int PUNCTUATIONS_IN_STRIP = 6;
private static final float MIN_TEXT_XSCALE = 0.8f;
private final UiHandler mHandler = new UiHandler(this); private final UiHandler mHandler = new UiHandler(this);
@ -150,6 +158,114 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
} }
} }
private static class CandidateViewLayoutParams {
public final TextPaint mPaint;
public final int mPadding;
public final int mDividerWidth;
public final int mDividerHeight;
public final int mControlWidth;
private final int mAutoCorrectHighlight;
public final ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>();
public int mCountInStrip;
// True if the mCountInStrip suggestions can fit in suggestion strip in equally divided
// width without squeezing the text.
public boolean mCanUseFixedWidthColumns;
public int mMaxWidth;
public int mAvailableWidthForWords;
public int mConstantWidthForPaddings;
public int mVariableWidthForWords;
public float mScaleX;
public CandidateViewLayoutParams(Resources res, View divider, View control,
int autoCorrectHighlight) {
mPaint = new TextPaint();
final float textSize = res.getDimension(R.dimen.candidate_text_size);
mPaint.setTextSize(textSize);
mPadding = res.getDimensionPixelSize(R.dimen.candidate_padding);
mDividerWidth = divider.getMeasuredWidth();
mDividerHeight = divider.getMeasuredHeight();
mControlWidth = control.getMeasuredWidth();
mAutoCorrectHighlight = autoCorrectHighlight;
}
public void layoutStrip(SuggestedWords suggestions, int maxWidth, int maxCount) {
final int size = suggestions.size();
setupTexts(suggestions, size, mAutoCorrectHighlight);
mCountInStrip = Math.min(maxCount, size);
mScaleX = 1.0f;
do {
mMaxWidth = maxWidth;
if (size > mCountInStrip) {
mMaxWidth -= mControlWidth;
}
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);
}
public void tryLayout() {
final int maxCount = mCountInStrip;
final int dividers = mDividerWidth * (maxCount - 1);
mConstantWidthForPaddings = dividers + mPadding * maxCount * 2;
mAvailableWidthForWords = mMaxWidth - mConstantWidthForPaddings;
mPaint.setTextScaleX(mScaleX);
final int maxFixedWidthForWord = (mMaxWidth - dividers) / maxCount - mPadding * 2;
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, int autoCorrectHighlight) {
mTexts.clear();
for (int i = 0; i < count; i++) {
final CharSequence suggestion = suggestions.getWord(i);
if (suggestion == null) continue;
final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion
&& ((i == 1 && !suggestions.mTypedWordValid)
|| (i == 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 CharSequence styled = getStyledCandidateWord(suggestion, isAutoCorrect,
autoCorrectHighlight);
mTexts.add(styled);
}
}
@Override
public String toString() {
return String.format(
"count=%d width=%d avail=%d fixcol=%s scaleX=%4.2f const=%d var=%d",
mCountInStrip, mMaxWidth, mAvailableWidthForWords, mCanUseFixedWidthColumns,
mScaleX, mConstantWidthForPaddings, mVariableWidthForWords);
}
}
/** /**
* Construct a CandidateView for showing suggested words for completion. * Construct a CandidateView for showing suggested words for completion.
* @param context * @param context
@ -173,6 +289,17 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
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);
mColorDivider = a.getColor(R.styleable.CandidateView_colorDivider, 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);
@ -184,74 +311,52 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
mPreviewPopup.setContentView(mPreviewText); mPreviewPopup.setContentView(mPreviewText);
mPreviewPopup.setBackgroundDrawable(null); mPreviewPopup.setBackgroundDrawable(null);
mCandidatesStrip = findViewById(R.id.candidates_strip); mCandidatesStrip = (ViewGroup)findViewById(R.id.candidates_strip);
mCandidateStripHeight = res.getDimensionPixelOffset(R.dimen.candidate_strip_height); mCandidateStripHeight = res.getDimensionPixelOffset(R.dimen.candidate_strip_height);
for (int i = 0; i < MAX_SUGGESTIONS; i++) { for (int i = 0; i < MAX_SUGGESTIONS; i++) {
final TextView word, info; final TextView word = (TextView)inflater.inflate(R.layout.candidate_word, null);
switch (i) {
case 0:
word = (TextView)findViewById(R.id.word_left);
info = (TextView)findViewById(R.id.info_left);
break;
case 1:
word = (TextView)findViewById(R.id.word_center);
info = (TextView)findViewById(R.id.info_center);
break;
case 2:
word = (TextView)findViewById(R.id.word_right);
info = (TextView)findViewById(R.id.info_right);
break;
default:
word = (TextView)inflater.inflate(R.layout.candidate_word, null);
info = (TextView)inflater.inflate(R.layout.candidate_info, null);
break;
}
word.setTag(i); word.setTag(i);
word.setOnClickListener(this); word.setOnClickListener(this);
if (i == 0) if (i == 0)
word.setOnLongClickListener(this); word.setOnLongClickListener(this);
mWords.add(word); mWords.add(word);
mInfos.add(info); mInfos.add((TextView)inflater.inflate(R.layout.candidate_info, null));
if (i > 0) { mDividers.add(getDivider(inflater));
final View divider = inflater.inflate(R.layout.candidate_divider, null);
divider.measure(MATCH_PARENT, MATCH_PARENT);
mDividers.add(divider);
}
} }
mTouchToSave = findViewById(R.id.touch_to_save); mTouchToSave = findViewById(R.id.touch_to_save);
mWordToSave = (TextView)findViewById(R.id.word_to_save); mWordToSave = (TextView)findViewById(R.id.word_to_save);
mWordToSave.setOnClickListener(this); mWordToSave.setOnClickListener(this);
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);
mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff); mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff);
mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord); mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord);
mExpandCandidatesPane = (ImageView)findViewById(R.id.expand_candidates_pane); mCandidatesPaneControl = (ViewGroup)findViewById(R.id.candidates_pane_control);
mExpandCandidatesPane.setImageDrawable( mExpandCandidatesPane = (TextView)findViewById(R.id.expand_candidates_pane);
a.getDrawable(R.styleable.CandidateView_iconExpandPane));
mExpandCandidatesPane.setOnClickListener(new OnClickListener() { mExpandCandidatesPane.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
expandCandidatesPane(); expandCandidatesPane();
} }
}); });
mCloseCandidatesPane = (ImageView)findViewById(R.id.close_candidates_pane); mCloseCandidatesPane = (TextView)findViewById(R.id.close_candidates_pane);
mCloseCandidatesPane.setImageDrawable(
a.getDrawable(R.styleable.CandidateView_iconClosePane));
mCloseCandidatesPane.setOnClickListener(new OnClickListener() { mCloseCandidatesPane.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
closeCandidatesPane(); closeCandidatesPane();
} }
}); });
mCandidatesPaneControl.measure(WRAP_CONTENT, WRAP_CONTENT);
a.recycle(); mParams = new CandidateViewLayoutParams(res, mDividers.get(0), mCandidatesPaneControl,
mAutoCorrectHighlight);
}
private View getDivider(LayoutInflater inflater) {
final TextView divider = (TextView)inflater.inflate(R.layout.candidate_divider, null);
divider.setTextColor(mColorDivider);
divider.measure(WRAP_CONTENT, WRAP_CONTENT);
return divider;
} }
/** /**
@ -279,15 +384,14 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
} }
} }
private CharSequence getStyledCandidateWord(CharSequence word, TextView v, private static CharSequence getStyledCandidateWord(CharSequence word, boolean isAutoCorrect,
boolean isAutoCorrect) { int autoCorrectHighlight) {
v.setTypeface(Typeface.DEFAULT);
if (!isAutoCorrect) if (!isAutoCorrect)
return word; return word;
final Spannable spannedWord = new SpannableString(word); final Spannable spannedWord = new SpannableString(word);
if ((mAutoCorrectHighlight & AUTO_CORRECT_BOLD) != 0) if ((autoCorrectHighlight & AUTO_CORRECT_BOLD) != 0)
v.setTypeface(Typeface.DEFAULT_BOLD); spannedWord.setSpan(BOLD_SPAN, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
if ((mAutoCorrectHighlight & AUTO_CORRECT_UNDERLINE) != 0) if ((autoCorrectHighlight & AUTO_CORRECT_UNDERLINE) != 0)
spannedWord.setSpan(UNDERLINE_SPAN, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannedWord.setSpan(UNDERLINE_SPAN, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return spannedWord; return spannedWord;
} }
@ -313,91 +417,133 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
private void updateSuggestions() { private void updateSuggestions() {
final SuggestedWords suggestions = mSuggestions; final SuggestedWords suggestions = mSuggestions;
final List<SuggestedWordInfo> suggestedWordInfoList = suggestions.mSuggestedWordInfoList; final List<SuggestedWordInfo> suggestedWordInfoList = suggestions.mSuggestedWordInfoList;
final int paneWidth = getWidth();
final CandidateViewLayoutParams params = mParams;
clear(); clear();
final int paneWidth = getWidth();
final int dividerWidth = mDividers.get(0).getMeasuredWidth();
final int dividerHeight = mDividers.get(0).getMeasuredHeight();
int x = 0;
int y = 0;
int fromIndex = NUM_CANDIDATES_IN_STRIP;
final int count = Math.min(mWords.size(), suggestions.size());
closeCandidatesPane(); closeCandidatesPane();
mExpandCandidatesPane.setVisibility(count > NUM_CANDIDATES_IN_STRIP ? VISIBLE : GONE); if (suggestions.size() == 0)
return;
params.layoutStrip(suggestions, paneWidth, suggestions.isPunctuationSuggestions()
? PUNCTUATIONS_IN_STRIP : mCandidateCountInStrip);
final int count = Math.min(mWords.size(), suggestions.size());
if (count <= params.mCountInStrip) {
mCandidatesPaneControl.setVisibility(GONE);
} else {
mCandidatesPaneControl.setVisibility(VISIBLE);
mExpandCandidatesPane.setVisibility(VISIBLE);
}
final int countInStrip = params.mCountInStrip;
int fromIndex = countInStrip;
int x = 0, y = 0;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
final CharSequence suggestion = suggestions.getWord(i); final int pos;
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; if (suggestion == null) continue;
final SuggestedWordInfo suggestionInfo = (suggestedWordInfoList != null) final SuggestedWordInfo suggestionInfo = (suggestedWordInfoList != null)
? suggestedWordInfoList.get(i) : null; ? suggestedWordInfoList.get(pos) : null;
final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion
&& ((i == 1 && !suggestions.mTypedWordValid) && ((pos == 1 && !suggestions.mTypedWordValid)
|| (i == 0 && suggestions.mTypedWordValid)); || (pos == 0 && suggestions.mTypedWordValid));
// HACK: even if i == 0, we use mColorOther when this suggestion's length is 1 // 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. // and there are multiple suggestions, such as the default punctuation list.
// TODO: Need to revisit this logic with bigram suggestions // TODO: Need to revisit this logic with bigram suggestions
final boolean isSuggestedCandidate = (i != 0); final boolean isSuggestedCandidate = (pos != 0);
final boolean isPunctuationSuggestions = (suggestion.length() == 1 && count > 1); final boolean isPunctuationSuggestions = (suggestion.length() == 1 && count > 1);
final TextView word = mWords.get(i); final TextView word = mWords.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(getCandidateTextColor(isAutoCorrect,
isSuggestedCandidate || isPunctuationSuggestions, suggestionInfo)); isSuggestedCandidate || isPunctuationSuggestions, suggestionInfo));
final CharSequence text = getStyledCandidateWord(suggestion, word, isAutoCorrect); final CharSequence styled = params.mTexts.get(pos);
if (i < NUM_CANDIDATES_IN_STRIP) {
final View parent = (View)word.getParent();
final int width = parent.getWidth() - word.getPaddingLeft()
- word.getPaddingRight();
setTextWithAutoScaleAndEllipsis(text, width, word);
} else {
setTextWithAutoScaleAndEllipsis(text, paneWidth, word);
}
final TextView info; final TextView info;
if (DBG && suggestionInfo != null if (DBG && suggestionInfo != null
&& !TextUtils.isEmpty(suggestionInfo.getDebugString())) { && !TextUtils.isEmpty(suggestionInfo.getDebugString())) {
info = mInfos.get(i); info = mInfos.get(i);
info.setText(suggestionInfo.getDebugString()); info.setText(suggestionInfo.getDebugString());
info.setVisibility(View.VISIBLE);
} else { } else {
info = null; info = null;
} }
if (i < NUM_CANDIDATES_IN_STRIP) { final CharSequence text;
final float scaleX;
if (i < countInStrip) {
if (i == 0 && params.mCountInStrip == 1) {
text = getEllipsizedText(styled, params.mMaxWidth, paint);
scaleX = paint.getTextScaleX();
} else {
text = styled;
scaleX = params.mScaleX;
}
word.setText(text);
word.setTextScaleX(scaleX);
if (i != 0) {
// Add divider if this isn't the left most suggestion in candidate strip.
mCandidatesStrip.addView(mDividers.get(i));
}
mCandidatesStrip.addView(word);
if (params.mCanUseFixedWidthColumns) {
setLayoutWeight(word, 1.0f);
} else {
final int width = getTextWidth(text, paint) + params.mPadding * 2;
setLayoutWeight(word, width);
}
if (info != null) { if (info != null) {
word.measure(MATCH_PARENT, MATCH_PARENT); word.measure(WRAP_CONTENT, MATCH_PARENT);
info.measure(MATCH_PARENT, MATCH_PARENT);
final int width = word.getMeasuredWidth(); final int width = word.getMeasuredWidth();
info.measure(WRAP_CONTENT, WRAP_CONTENT);
final int infoWidth = info.getMeasuredWidth(); final int infoWidth = info.getMeasuredWidth();
FrameLayoutCompatUtils.placeViewAt( FrameLayoutCompatUtils.placeViewAt(
info, width - infoWidth, 0, infoWidth, info.getMeasuredHeight()); info, width - infoWidth, 0, infoWidth, info.getMeasuredHeight());
} }
} else { } else {
word.measure(MATCH_PARENT, MATCH_PARENT); paint.setTextScaleX(1.0f);
final int width = word.getMeasuredWidth(); final int textWidth = getTextWidth(styled, paint);
final int height = word.getMeasuredHeight(); int available = paneWidth - x - params.mPadding * 2;
// TODO: Handle overflow case. if (textWidth >= available) {
if (dividerWidth + x + width >= paneWidth) { // Needs new row, centering previous row.
centeringCandidates(fromIndex, i - 1, x, paneWidth); centeringCandidates(fromIndex, i - 1, x, paneWidth);
x = 0; x = 0;
y += mCandidateStripHeight; y += mCandidateStripHeight;
fromIndex = i; fromIndex = i;
} }
if (x != 0) { if (x != 0) {
final View divider = mDividers.get(i - NUM_CANDIDATES_IN_STRIP); // Add divider if this isn't the left most suggestion in current row.
final View divider = mDividers.get(i);
mCandidatesPane.addView(divider); mCandidatesPane.addView(divider);
FrameLayoutCompatUtils.placeViewAt( FrameLayoutCompatUtils.placeViewAt(
divider, x, y + (mCandidateStripHeight - dividerHeight) / 2, divider, x, y + (mCandidateStripHeight - params.mDividerHeight) / 2,
dividerWidth, dividerHeight); params.mDividerWidth, params.mDividerHeight);
x += dividerWidth; x += params.mDividerWidth;
} }
available = paneWidth - x - params.mPadding * 2;
text = getEllipsizedText(styled, available, paint);
scaleX = paint.getTextScaleX();
word.setText(text);
word.setTextScaleX(scaleX);
mCandidatesPane.addView(word); mCandidatesPane.addView(word);
word.measure(WRAP_CONTENT, WRAP_CONTENT);
final int width = word.getMeasuredWidth();
final int height = word.getMeasuredHeight();
FrameLayoutCompatUtils.placeViewAt( FrameLayoutCompatUtils.placeViewAt(
word, x, y + (mCandidateStripHeight - height) / 2, width, height); word, x, y + (mCandidateStripHeight - height) / 2, width, height);
if (info != null) { if (info != null) {
mCandidatesPane.addView(info); mCandidatesPane.addView(info);
info.measure(MATCH_PARENT, MATCH_PARENT); info.measure(WRAP_CONTENT, WRAP_CONTENT);
final int infoWidth = info.getMeasuredWidth(); final int infoWidth = info.getMeasuredWidth();
FrameLayoutCompatUtils.placeViewAt( FrameLayoutCompatUtils.placeViewAt(
info, x + width - infoWidth, y, infoWidth, info.getMeasuredHeight()); info, x + width - infoWidth, y, infoWidth, info.getMeasuredHeight());
@ -411,6 +557,16 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
} }
} }
private static void setLayoutWeight(View v, float weight) {
final ViewGroup.LayoutParams lp = v.getLayoutParams();
if (lp instanceof LinearLayout.LayoutParams) {
final LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams)lp;
llp.weight = weight;
llp.width = 0;
llp.height = MATCH_PARENT;
}
}
private void centeringCandidates(int from, int to, int width, int paneWidth) { private void centeringCandidates(int from, int to, int width, int paneWidth) {
final ViewGroup pane = mCandidatesPane; final ViewGroup pane = mCandidatesPane;
final int fromIndex = pane.indexOfChild(mWords.get(from)); final int fromIndex = pane.indexOfChild(mWords.get(from));
@ -436,38 +592,29 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
} }
} }
private static void setTextWithAutoScaleAndEllipsis(CharSequence text, int w, TextView v) { private static CharSequence getEllipsizedText(CharSequence text, int maxWidth,
// To prevent partially rendered character at the end of text, subtract few extra pixels TextPaint paint) {
// from the width. paint.setTextScaleX(1.0f);
final int width = w - 4; final int width = getTextWidth(text, paint);
final float scaleX = Math.min(maxWidth / (float)width, 1.0f);
final TextPaint paint = v.getPaint();
final int textWidth = getTextWidth(text, paint, 1.0f);
if (textWidth < width || textWidth == 0 || width <= 0) {
v.setTextScaleX(1.0f);
v.setText(text);
return;
}
final float scaleX = Math.min((float)width / textWidth, 1.0f);
if (scaleX >= MIN_TEXT_XSCALE) { if (scaleX >= MIN_TEXT_XSCALE) {
v.setTextScaleX(scaleX); paint.setTextScaleX(scaleX);
v.setText(text); return text;
return;
} }
final int truncatedWidth = width - getTextWidth(ELLIPSIS, paint, MIN_TEXT_XSCALE); // Note that TextUtils.ellipsize() use text-x-scale as 1.0 if ellipsize is needed. To get
final CharSequence ellipsized = getTextEllipsizedAtStart(text, paint, truncatedWidth); // squeezed and ellipsezed text, passes enlarged width (maxWidth / MIN_TEXT_XSCALE).
v.setTextScaleX(MIN_TEXT_XSCALE); final CharSequence ellipsized = TextUtils.ellipsize(
v.setText(ELLIPSIS); text, paint, maxWidth / MIN_TEXT_XSCALE, TextUtils.TruncateAt.MIDDLE);
v.append(ellipsized); paint.setTextScaleX(MIN_TEXT_XSCALE);
return ellipsized;
} }
private static int getTextWidth(CharSequence text, TextPaint paint, float scaleX) { private static int getTextWidth(CharSequence text, TextPaint paint) {
if (TextUtils.isEmpty(text)) return 0; if (TextUtils.isEmpty(text)) return 0;
paint.setTypeface(getTextTypeface(text));
final int len = text.length(); final int len = text.length();
final float[] widths = new float[len]; final float[] widths = new float[len];
paint.setTextScaleX(scaleX);
final int count = paint.getTextWidths(text, 0, len, widths); final int count = paint.getTextWidths(text, 0, len, widths);
float width = 0; float width = 0;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
@ -476,33 +623,35 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
return (int)Math.round(width + 0.5); return (int)Math.round(width + 0.5);
} }
private static CharSequence getTextEllipsizedAtStart(CharSequence text, TextPaint paint, private static Typeface getTextTypeface(CharSequence text) {
int maxWidth) { if (!(text instanceof SpannableString))
final int len = text.length(); return Typeface.DEFAULT;
final float[] widths = new float[len];
final int count = paint.getTextWidths(text, 0, len, widths); final SpannableString ss = (SpannableString)text;
float width = 0; final StyleSpan[] styles = ss.getSpans(0, text.length(), StyleSpan.class);
for (int i = count - 1; i >= 0; i--) { if (styles.length == 0)
width += widths[i]; return Typeface.DEFAULT;
if (width > maxWidth)
return text.subSequence(i + 1, len); switch (styles[0].getStyle()) {
case Typeface.BOLD: return Typeface.DEFAULT_BOLD;
// TODO: BOLD_ITALIC, ITALIC case?
default: return Typeface.DEFAULT;
} }
return text;
} }
private void expandCandidatesPane() { private void expandCandidatesPane() {
mExpandCandidatesPane.setVisibility(View.GONE); mExpandCandidatesPane.setVisibility(GONE);
mCloseCandidatesPane.setVisibility(View.VISIBLE); mCloseCandidatesPane.setVisibility(VISIBLE);
mCandidatesPaneContainer.setMinimumHeight(mKeyboardView.getMeasuredHeight()); mCandidatesPaneContainer.setMinimumHeight(mKeyboardView.getMeasuredHeight());
mCandidatesPaneContainer.setVisibility(View.VISIBLE); mCandidatesPaneContainer.setVisibility(VISIBLE);
mKeyboardView.setVisibility(View.GONE); mKeyboardView.setVisibility(GONE);
} }
private void closeCandidatesPane() { private void closeCandidatesPane() {
mExpandCandidatesPane.setVisibility(View.VISIBLE); mExpandCandidatesPane.setVisibility(VISIBLE);
mCloseCandidatesPane.setVisibility(View.GONE); mCloseCandidatesPane.setVisibility(GONE);
mCandidatesPaneContainer.setVisibility(View.GONE); mCandidatesPaneContainer.setVisibility(GONE);
mKeyboardView.setVisibility(View.VISIBLE); mKeyboardView.setVisibility(VISIBLE);
} }
public void onAutoCorrectionInverted(CharSequence autoCorrectedWord) { public void onAutoCorrectionInverted(CharSequence autoCorrectedWord) {
@ -526,8 +675,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
public void showAddToDictionaryHint(CharSequence word) { public void showAddToDictionaryHint(CharSequence word) {
mWordToSave.setText(word); mWordToSave.setText(word);
mShowingAddToDictionary = true; mShowingAddToDictionary = true;
mCandidatesStrip.setVisibility(View.GONE); mCandidatesStrip.setVisibility(GONE);
mTouchToSave.setVisibility(View.VISIBLE); mTouchToSave.setVisibility(VISIBLE);
} }
public boolean dismissAddToDictionaryHint() { public boolean dismissAddToDictionaryHint() {
@ -543,12 +692,9 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
public void clear() { public void clear() {
mShowingAddToDictionary = false; mShowingAddToDictionary = false;
mShowingAutoCorrectionInverted = false; mShowingAutoCorrectionInverted = false;
for (int i = 0; i < NUM_CANDIDATES_IN_STRIP; i++) { mTouchToSave.setVisibility(GONE);
mWords.get(i).setText(null); mCandidatesStrip.setVisibility(VISIBLE);
mInfos.get(i).setVisibility(View.GONE); mCandidatesStrip.removeAllViews();
}
mTouchToSave.setVisibility(View.GONE);
mCandidatesStrip.setVisibility(View.VISIBLE);
mCandidatesPane.removeAllViews(); mCandidatesPane.removeAllViews();
} }

View file

@ -288,7 +288,7 @@ public class Settings extends PreferenceActivity
builder.addWord(puncs.subSequence(i, i + 1)); builder.addWord(puncs.subSequence(i, i + 1));
} }
} }
return builder.build(); return builder.setIsPunctuationSuggestions().build();
} }
} }

View file

@ -24,15 +24,17 @@ 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, null); public static final SuggestedWords EMPTY = new SuggestedWords(null, false, false, false, null);
public final List<CharSequence> mWords; public final List<CharSequence> mWords;
public final boolean mTypedWordValid; public final boolean mTypedWordValid;
public final boolean mHasMinimalSuggestion; public final boolean mHasMinimalSuggestion;
public final boolean mIsPunctuationSuggestions;
public final List<SuggestedWordInfo> mSuggestedWordInfoList; public final List<SuggestedWordInfo> mSuggestedWordInfoList;
private SuggestedWords(List<CharSequence> words, boolean typedWordValid, private SuggestedWords(List<CharSequence> words, boolean typedWordValid,
boolean hasMinimalSuggestion, List<SuggestedWordInfo> suggestedWordInfoList) { boolean hasMinimalSuggestion, boolean isPunctuationSuggestions,
List<SuggestedWordInfo> suggestedWordInfoList) {
if (words != null) { if (words != null) {
mWords = words; mWords = words;
} else { } else {
@ -40,6 +42,7 @@ public class SuggestedWords {
} }
mTypedWordValid = typedWordValid; mTypedWordValid = typedWordValid;
mHasMinimalSuggestion = hasMinimalSuggestion; mHasMinimalSuggestion = hasMinimalSuggestion;
mIsPunctuationSuggestions = isPunctuationSuggestions;
mSuggestedWordInfoList = suggestedWordInfoList; mSuggestedWordInfoList = suggestedWordInfoList;
} }
@ -59,10 +62,15 @@ public class SuggestedWords {
return mHasMinimalSuggestion && ((size() > 1 && !mTypedWordValid) || mTypedWordValid); return mHasMinimalSuggestion && ((size() > 1 && !mTypedWordValid) || mTypedWordValid);
} }
public boolean isPunctuationSuggestions() {
return mIsPunctuationSuggestions;
}
public static class Builder { public static class Builder {
private List<CharSequence> mWords = new ArrayList<CharSequence>(); private List<CharSequence> mWords = new ArrayList<CharSequence>();
private boolean mTypedWordValid; private boolean mTypedWordValid;
private boolean mHasMinimalSuggestion; private boolean mHasMinimalSuggestion;
private boolean mIsPunctuationSuggestions;
private List<SuggestedWordInfo> mSuggestedWordInfoList = private List<SuggestedWordInfo> mSuggestedWordInfoList =
new ArrayList<SuggestedWordInfo>(); new ArrayList<SuggestedWordInfo>();
@ -118,6 +126,11 @@ public class SuggestedWords {
return this; return this;
} }
public Builder setIsPunctuationSuggestions() {
mIsPunctuationSuggestions = 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,
@ -143,7 +156,7 @@ public class SuggestedWords {
public SuggestedWords build() { public SuggestedWords build() {
return new SuggestedWords(mWords, mTypedWordValid, mHasMinimalSuggestion, return new SuggestedWords(mWords, mTypedWordValid, mHasMinimalSuggestion,
mSuggestedWordInfoList); mIsPunctuationSuggestions, mSuggestedWordInfoList);
} }
public int size() { public int size() {