aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/CandidateView.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/CandidateView.java')
-rw-r--r--java/src/com/android/inputmethod/latin/CandidateView.java115
1 files changed, 76 insertions, 39 deletions
diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java
index 5f20c70b4..f445abf48 100644
--- a/java/src/com/android/inputmethod/latin/CandidateView.java
+++ b/java/src/com/android/inputmethod/latin/CandidateView.java
@@ -64,6 +64,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
private static final boolean DBG = LatinImeLogger.sDBG;
+ private final ViewGroup mCandidatesPlacer;
private final ViewGroup mCandidatesStrip;
private ViewGroup mCandidatesPane;
private ViewGroup mCandidatesPaneContainer;
@@ -76,13 +77,9 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
private final PopupWindow mPreviewPopup;
private final TextView mPreviewText;
- private final View mTouchToSave;
- private final TextView mWordToSave;
-
private Listener mListener;
private SuggestedWords mSuggestions = SuggestedWords.EMPTY;
private boolean mShowingAutoCorrectionInverted;
- private boolean mShowingAddToDictionary;
private final SuggestionsStripParams mStripParams;
private final SuggestionsPaneParams mPaneParams;
@@ -94,7 +91,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
private static final int MSG_HIDE_PREVIEW = 0;
private static final int MSG_UPDATE_SUGGESTION = 1;
- private static final long DELAY_HIDE_PREVIEW = 1000;
+ private static final long DELAY_HIDE_PREVIEW = 1300;
private static final long DELAY_UPDATE_SUGGESTION = 300;
public UiHandler(CandidateView outerInstance) {
@@ -239,8 +236,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
lastView = info;
info.measure(WRAP_CONTENT, WRAP_CONTENT);
final int infoWidth = info.getMeasuredWidth();
- FrameLayoutCompatUtils.placeViewAt(info, x - infoWidth, y, infoWidth,
- info.getMeasuredHeight());
+ FrameLayoutCompatUtils.placeViewAt(
+ info, x - infoWidth, y, infoWidth, info.getMeasuredHeight());
}
}
if (x != 0) {
@@ -274,13 +271,16 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
private static final int AUTO_CORRECT_INVERT = 0x04;
private static final int VALID_TYPED_WORD_BOLD = 0x08;
- private final TextPaint mPaint;
private final int mSuggestionStripOption;
private final ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>();
public boolean mMoreSuggestionsAvailable;
+ public final TextView mWordToSaveView;
+ private final TextView mHintToSaveView;
+ private final CharSequence mHintToSaveText;
+
public SuggestionsStripParams(Context context, AttributeSet attrs, int defStyle,
List<TextView> words, List<View> dividers, List<TextView> infos) {
super(words, dividers, infos);
@@ -305,9 +305,10 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff);
mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord);
- mPaint = new TextPaint();
- final float textSize = res.getDimension(R.dimen.candidate_text_size);
- mPaint.setTextSize(textSize);
+ final LayoutInflater inflater = LayoutInflater.from(context);
+ mWordToSaveView = (TextView)inflater.inflate(R.layout.candidate_word, null);
+ mHintToSaveView = (TextView)inflater.inflate(R.layout.candidate_word, null);
+ mHintToSaveText = context.getText(R.string.hint_add_to_dictionary);
}
public int getTextColor() {
@@ -366,7 +367,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
final SuggestedWordInfo info = (pos < suggestions.size())
? suggestions.getInfo(pos) : null;
- if (info != null && info.isPreviousSuggestedWord()) {
+ if (info != null && info.isObsoleteSuggestedWord()) {
return applyAlpha(color, 0.5f);
} else {
return color;
@@ -388,7 +389,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
return word;
}
- public int layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup paneView,
+ public int layout(SuggestedWords suggestions, ViewGroup stripView, ViewGroup placer,
int stripWidth) {
if (suggestions.isPunctuationSuggestions()) {
return layoutPunctuationSuggestions(suggestions, stripView);
@@ -405,6 +406,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
final View divider = mDividers.get(pos);
// Add divider if this isn't the left most suggestion in candidate strip.
stripView.addView(divider);
+ x += divider.getMeasuredWidth();
}
final CharSequence styled = mTexts.get(pos);
@@ -429,18 +431,19 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
word.setTextScaleX(scaleX);
stripView.addView(word);
setLayoutWeight(word, getCandidateWeight(index), MATCH_PARENT);
+ x += word.getMeasuredWidth();
if (DBG) {
final CharSequence debugInfo = getDebugInfo(suggestions, pos);
if (debugInfo != null) {
final TextView info = mInfos.get(pos);
info.setText(debugInfo);
- paneView.addView(info);
+ placer.addView(info);
info.measure(WRAP_CONTENT, WRAP_CONTENT);
final int infoWidth = info.getMeasuredWidth();
final int y = info.getMeasuredHeight();
- FrameLayoutCompatUtils.placeViewAt(info, x, 0, infoWidth, y);
- x += infoWidth * 2;
+ FrameLayoutCompatUtils.placeViewAt(
+ info, x - infoWidth, y, infoWidth, info.getMeasuredHeight());
}
}
}
@@ -498,6 +501,33 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
mMoreSuggestionsAvailable = false;
return countInStrip;
}
+
+ public void layoutAddToDictionaryHint(CharSequence word, ViewGroup stripView,
+ int stripWidth) {
+ final int width = stripWidth - mDividerWidth - mPadding * 2;
+
+ final TextView wordView = mWordToSaveView;
+ wordView.setTextColor(mColorTypedWord);
+ final int wordWidth = (int)(width * mCenterCandidateWeight);
+ final CharSequence text = getEllipsizedText(word, wordWidth, wordView.getPaint());
+ final float wordScaleX = wordView.getTextScaleX();
+ wordView.setTag(word);
+ wordView.setText(text);
+ wordView.setTextScaleX(wordScaleX);
+ stripView.addView(wordView);
+ setLayoutWeight(wordView, mCenterCandidateWeight, MATCH_PARENT);
+
+ stripView.addView(mDividers.get(0));
+
+ final TextView hintView = mHintToSaveView;
+ hintView.setTextColor(mColorAutoCorrect);
+ final int hintWidth = width - wordWidth;
+ final float hintScaleX = getTextScaleX(mHintToSaveText, hintWidth, hintView.getPaint());
+ hintView.setText(mHintToSaveText);
+ hintView.setTextScaleX(hintScaleX);
+ stripView.addView(hintView);
+ setLayoutWeight(hintView, 1.0f - mCenterCandidateWeight, MATCH_PARENT);
+ }
}
/**
@@ -517,7 +547,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
super(context, attrs);
if (defStyle != R.attr.candidateViewStyle) {
throw new IllegalArgumentException(
- "can't accept defStyle other than R.attr.candidayeViewStyle: defStyle="
+ "can't accept defStyle other than R.attr.candidateViewStyle: defStyle="
+ defStyle);
}
setBackgroundDrawable(LinearLayoutCompatUtils.getBackgroundDrawable(
@@ -532,6 +562,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
mPreviewPopup.setContentView(mPreviewText);
mPreviewPopup.setBackgroundDrawable(null);
+ mCandidatesPlacer = (ViewGroup)findViewById(R.id.candidates_placer);
mCandidatesStrip = (ViewGroup)findViewById(R.id.candidates_strip);
for (int pos = 0; pos < MAX_SUGGESTIONS; pos++) {
final TextView word = (TextView)inflater.inflate(R.layout.candidate_word, null);
@@ -546,13 +577,10 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
mInfos.add((TextView)inflater.inflate(R.layout.candidate_info, null));
}
- mTouchToSave = findViewById(R.id.touch_to_save);
- mWordToSave = (TextView)findViewById(R.id.word_to_save);
- mWordToSave.setOnClickListener(this);
-
mStripParams = new SuggestionsStripParams(context, attrs, defStyle, mWords, mDividers,
mInfos);
mPaneParams = new SuggestionsPaneParams(mWords, mDividers, mInfos);
+ mStripParams.mWordToSaveView.setOnClickListener(this);
}
/**
@@ -588,7 +616,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
final int width = getWidth();
final int countInStrip = mStripParams.layout(
- mSuggestions, mCandidatesStrip, mCandidatesPane, width);
+ mSuggestions, mCandidatesStrip, mCandidatesPlacer, width);
mPaneParams.layout(
mSuggestions, mCandidatesPane, countInStrip, mStripParams.getTextColor(), width);
}
@@ -636,6 +664,15 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
}
}
+ private static float getTextScaleX(CharSequence text, int maxWidth, TextPaint paint) {
+ paint.setTextScaleX(1.0f);
+ final int width = getTextWidth(text, paint);
+ if (width <= maxWidth) {
+ return 1.0f;
+ }
+ return maxWidth / (float)width;
+ }
+
private static CharSequence getEllipsizedText(CharSequence text, int maxWidth,
TextPaint paint) {
if (text == null) return null;
@@ -718,20 +755,21 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
}
public boolean isShowingAddToDictionaryHint() {
- return mShowingAddToDictionary;
+ return mCandidatesStrip.getChildCount() > 0
+ && mCandidatesStrip.getChildAt(0) == mStripParams.mWordToSaveView;
}
public void showAddToDictionaryHint(CharSequence word) {
- mWordToSave.setText(word);
- mShowingAddToDictionary = true;
- mCandidatesStrip.setVisibility(GONE);
- mTouchToSave.setVisibility(VISIBLE);
+ clear();
+ mStripParams.layoutAddToDictionaryHint(word, mCandidatesStrip, getWidth());
}
public boolean dismissAddToDictionaryHint() {
- if (!mShowingAddToDictionary) return false;
- clear();
- return true;
+ if (isShowingAddToDictionaryHint()) {
+ clear();
+ return true;
+ }
+ return false;
}
public SuggestedWords getSuggestions() {
@@ -739,10 +777,9 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
}
public void clear() {
- mShowingAddToDictionary = false;
mShowingAutoCorrectionInverted = false;
- mTouchToSave.setVisibility(GONE);
- mCandidatesStrip.setVisibility(VISIBLE);
+ mCandidatesPlacer.removeAllViews();
+ mCandidatesPlacer.addView(mCandidatesStrip);
mCandidatesStrip.removeAllViews();
mCandidatesPane.removeAllViews();
closeCandidatesPane();
@@ -752,7 +789,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
mPreviewPopup.dismiss();
}
- private void showPreview(int index, CharSequence word) {
+ private void showPreview(View view, CharSequence word) {
if (TextUtils.isEmpty(word))
return;
@@ -761,9 +798,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
previewText.setText(word);
previewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
- View v = mWords.get(index);
final int[] offsetInWindow = new int[2];
- v.getLocationInWindow(offsetInWindow);
+ view.getLocationInWindow(offsetInWindow);
final int posX = offsetInWindow[0];
final int posY = offsetInWindow[1] - previewText.getMeasuredHeight();
final PopupWindow previewPopup = mPreviewPopup;
@@ -778,7 +814,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
private void addToDictionary(CharSequence word) {
if (mListener.addWordToDictionary(word.toString())) {
- showPreview(0, getContext().getString(R.string.added_word, word));
+ final CharSequence message = getContext().getString(R.string.added_word, word);
+ showPreview(mStripParams.mWordToSaveView, message);
}
}
@@ -793,8 +830,8 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
@Override
public void onClick(View view) {
- if (view == mWordToSave) {
- addToDictionary(((TextView)view).getText());
+ if (view == mStripParams.mWordToSaveView) {
+ addToDictionary((CharSequence)view.getTag());
clear();
return;
}