am 1e4b1300: Merge "Refactor SuggestionsStripLayoutHelper class"

* commit '1e4b1300e65095ca4b064afb5590d4bc02ff7725':
  Refactor SuggestionsStripLayoutHelper class
main
Tadashi G. Takaoka 2014-01-27 01:39:02 -08:00 committed by Android Git Automerger
commit c8bddebf1a
3 changed files with 77 additions and 65 deletions

View File

@ -25,4 +25,19 @@
android:orientation="horizontal" android:orientation="horizontal"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/add_to_dictionary_strip"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
<include
layout="@layout/suggestion_word"
android:id="@+id/word_to_save" />
<include
layout="@layout/suggestion_divider" />
<include
layout="@layout/hint_add_to_dictionary"
android:id="@+id/hint_add_to_dictionary" />
</LinearLayout>
</merge> </merge>

View File

@ -38,7 +38,6 @@ 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;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -100,10 +99,6 @@ final class SuggestionStripLayoutHelper {
private static final int AUTO_CORRECT_UNDERLINE = 0x02; private static final int AUTO_CORRECT_UNDERLINE = 0x02;
private static final int VALID_TYPED_WORD_BOLD = 0x04; private static final int VALID_TYPED_WORD_BOLD = 0x04;
private final TextView mWordToSaveView;
private final TextView mLeftwardsArrowView;
private final TextView mHintToSaveView;
public SuggestionStripLayoutHelper(final Context context, final AttributeSet attrs, public SuggestionStripLayoutHelper(final Context context, final AttributeSet attrs,
final int defStyle, final ArrayList<TextView> wordViews, final int defStyle, final ArrayList<TextView> wordViews,
final ArrayList<View> dividerViews, final ArrayList<TextView> debugInfoViews) { final ArrayList<View> dividerViews, final ArrayList<TextView> debugInfoViews) {
@ -157,11 +152,6 @@ final class SuggestionStripLayoutHelper {
R.dimen.config_more_suggestions_bottom_gap); R.dimen.config_more_suggestions_bottom_gap);
mMoreSuggestionsRowHeight = res.getDimensionPixelSize( mMoreSuggestionsRowHeight = res.getDimensionPixelSize(
R.dimen.config_more_suggestions_row_height); R.dimen.config_more_suggestions_row_height);
final LayoutInflater inflater = LayoutInflater.from(context);
mWordToSaveView = (TextView)inflater.inflate(R.layout.suggestion_word, null);
mLeftwardsArrowView = (TextView)inflater.inflate(R.layout.hint_add_to_dictionary, null);
mHintToSaveView = (TextView)inflater.inflate(R.layout.hint_add_to_dictionary, null);
} }
public int getMaxMoreSuggestionsRow() { public int getMaxMoreSuggestionsRow() {
@ -466,54 +456,30 @@ final class SuggestionStripLayoutHelper {
mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip); mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip);
} }
public void layoutAddToDictionaryHint(final String word, final ViewGroup stripView, public void layoutAddToDictionaryHint(final String word, final ViewGroup addToDictionaryStrip,
final int stripWidth, final CharSequence hintText, final OnClickListener listener) { final int stripWidth, final CharSequence hintText) {
final int width = stripWidth - mDividerWidth - mPadding * 2; final int width = stripWidth - mDividerWidth - mPadding * 2;
final TextView wordView = mWordToSaveView; final TextView wordView = (TextView)addToDictionaryStrip.findViewById(R.id.word_to_save);
wordView.setTextColor(mColorTypedWord); wordView.setTextColor(mColorTypedWord);
final int wordWidth = (int)(width * mCenterSuggestionWeight); final int wordWidth = (int)(width * mCenterSuggestionWeight);
final CharSequence text = getEllipsizedText(word, wordWidth, wordView.getPaint()); final CharSequence wordToSave = getEllipsizedText(word, wordWidth, wordView.getPaint());
final float wordScaleX = wordView.getTextScaleX(); final float wordScaleX = wordView.getTextScaleX();
// {@link TextView#setTag()} is used to hold the word to be added to dictionary. The word wordView.setText(wordToSave);
// will be extracted at {@link #getAddToDictionaryWord()}.
wordView.setTag(word);
wordView.setText(text);
wordView.setTextScaleX(wordScaleX); wordView.setTextScaleX(wordScaleX);
stripView.addView(wordView);
setLayoutWeight(wordView, mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT); setLayoutWeight(wordView, mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT);
stripView.addView(mDividerViews.get(0)); final TextView hintView = (TextView)addToDictionaryStrip.findViewById(
R.id.hint_add_to_dictionary);
final TextView leftArrowView = mLeftwardsArrowView;
leftArrowView.setTextColor(mColorAutoCorrect);
leftArrowView.setText(LEFTWARDS_ARROW);
stripView.addView(leftArrowView);
final TextView hintView = mHintToSaveView;
hintView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); hintView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
hintView.setTextColor(mColorAutoCorrect); hintView.setTextColor(mColorAutoCorrect);
final int hintWidth = width - wordWidth - leftArrowView.getWidth(); final int hintWidth = width - wordWidth;
final float hintScaleX = getTextScaleX(hintText, hintWidth, hintView.getPaint()); final String hintWithArrow = LEFTWARDS_ARROW + hintText;
hintView.setText(hintText); final float hintScaleX = getTextScaleX(hintWithArrow, hintWidth, hintView.getPaint());
hintView.setText(hintWithArrow);
hintView.setTextScaleX(hintScaleX); hintView.setTextScaleX(hintScaleX);
stripView.addView(hintView);
setLayoutWeight( setLayoutWeight(
hintView, 1.0f - mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT); hintView, 1.0f - mCenterSuggestionWeight, ViewGroup.LayoutParams.MATCH_PARENT);
wordView.setOnClickListener(listener);
leftArrowView.setOnClickListener(listener);
hintView.setOnClickListener(listener);
}
public String getAddToDictionaryWord() {
// String tag is set at
// {@link #layoutAddToDictionaryHint(String,ViewGroup,int,CharSequence,OnClickListener}.
return (String)mWordToSaveView.getTag();
}
public boolean isAddToDictionaryShowing(final View v) {
return v == mWordToSaveView || v == mHintToSaveView || v == mLeftwardsArrowView;
} }
private static void setLayoutWeight(final View v, final float weight, final int height) { private static void setLayoutWeight(final View v, final float weight, final int height) {

View File

@ -56,6 +56,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
static final boolean DBG = LatinImeLogger.sDBG; static final boolean DBG = LatinImeLogger.sDBG;
private final ViewGroup mSuggestionsStrip; private final ViewGroup mSuggestionsStrip;
private final ViewGroup mAddToDictionaryStrip;
MainKeyboardView mMainKeyboardView; MainKeyboardView mMainKeyboardView;
private final View mMoreSuggestionsContainer; private final View mMoreSuggestionsContainer;
@ -70,6 +71,32 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
private final SuggestionStripLayoutHelper mLayoutHelper; private final SuggestionStripLayoutHelper mLayoutHelper;
private final StripVisibilityGroup mStripVisibilityGroup;
private static class StripVisibilityGroup {
private final View mSuggestionsStrip;
private final View mAddToDictionaryStrip;
public StripVisibilityGroup(final View suggestionsStrip, final View addToDictionaryStrip) {
mSuggestionsStrip = suggestionsStrip;
mAddToDictionaryStrip = addToDictionaryStrip;
showSuggestionsStrip();
}
public void showSuggestionsStrip() {
mSuggestionsStrip.setVisibility(VISIBLE);
mAddToDictionaryStrip.setVisibility(INVISIBLE);
}
public void showAddToDictionaryStrip() {
mSuggestionsStrip.setVisibility(INVISIBLE);
mAddToDictionaryStrip.setVisibility(VISIBLE);
}
public boolean isShowingAddToDictionaryStrip() {
return mAddToDictionaryStrip.getVisibility() == VISIBLE;
}
}
/** /**
* Construct a {@link SuggestionStripView} for showing suggestions to be picked by the user. * Construct a {@link SuggestionStripView} for showing suggestions to be picked by the user.
@ -88,6 +115,9 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
inflater.inflate(R.layout.suggestions_strip, this); inflater.inflate(R.layout.suggestions_strip, this);
mSuggestionsStrip = (ViewGroup)findViewById(R.id.suggestions_strip); mSuggestionsStrip = (ViewGroup)findViewById(R.id.suggestions_strip);
mAddToDictionaryStrip = (ViewGroup)findViewById(R.id.add_to_dictionary_strip);
mStripVisibilityGroup = new StripVisibilityGroup(mSuggestionsStrip, mAddToDictionaryStrip);
for (int pos = 0; pos < SuggestedWords.MAX_SUGGESTIONS; pos++) { for (int pos = 0; pos < SuggestedWords.MAX_SUGGESTIONS; pos++) {
final TextView word = (TextView)inflater.inflate(R.layout.suggestion_word, null); final TextView word = (TextView)inflater.inflate(R.layout.suggestion_word, null);
word.setOnClickListener(this); word.setOnClickListener(this);
@ -137,14 +167,16 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
} }
public boolean isShowingAddToDictionaryHint() { public boolean isShowingAddToDictionaryHint() {
return mSuggestionsStrip.getChildCount() > 0 return mStripVisibilityGroup.isShowingAddToDictionaryStrip();
&& mLayoutHelper.isAddToDictionaryShowing(mSuggestionsStrip.getChildAt(0));
} }
public void showAddToDictionaryHint(final String word, final CharSequence hintText) { public void showAddToDictionaryHint(final String word, final CharSequence hintText) {
clear(); mLayoutHelper.layoutAddToDictionaryHint(word, mAddToDictionaryStrip, getWidth(), hintText);
mLayoutHelper.layoutAddToDictionaryHint( // {@link TextView#setTag()} is used to hold the word to be added to dictionary. The word
word, mSuggestionsStrip, getWidth(), hintText, this); // will be extracted at {@link #onClick(View)}.
mAddToDictionaryStrip.setTag(word);
mAddToDictionaryStrip.setOnClickListener(this);
mStripVisibilityGroup.showAddToDictionaryStrip();
} }
public boolean dismissAddToDictionaryHint() { public boolean dismissAddToDictionaryHint() {
@ -157,8 +189,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
public void clear() { public void clear() {
mSuggestionsStrip.removeAllViews(); mSuggestionsStrip.removeAllViews();
removeAllViews(); mStripVisibilityGroup.showSuggestionsStrip();
addView(mSuggestionsStrip);
dismissMoreSuggestionsPanel(); dismissMoreSuggestionsPanel();
} }
@ -302,27 +333,27 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
@Override @Override
public void onClick(final View view) { public void onClick(final View view) {
if (mLayoutHelper.isAddToDictionaryShowing(view)) { final Object tag = view.getTag();
mListener.addWordToUserDictionary(mLayoutHelper.getAddToDictionaryWord()); // {@link String} tag is set at {@link #showAddToDictionaryHint(String,CharSequence)}.
if (tag instanceof String) {
final String wordToSave = (String)tag;
mListener.addWordToUserDictionary(wordToSave);
clear(); clear();
return; return;
} }
final Object tag = view.getTag(); // {@link Integer} tag is set at
// Integer tag is set at
// {@link SuggestionStripLayoutHelper#setupWordViewsTextAndColor(SuggestedWords,int)} and // {@link SuggestionStripLayoutHelper#setupWordViewsTextAndColor(SuggestedWords,int)} and
// {@link SuggestionStripLayoutHelper#layoutPunctuationSuggestions(SuggestedWords,ViewGroup} // {@link SuggestionStripLayoutHelper#layoutPunctuationSuggestions(SuggestedWords,ViewGroup}
if (!(tag instanceof Integer)) { if (tag instanceof Integer) {
return;
}
final int index = (Integer) tag; final int index = (Integer) tag;
if (index >= mSuggestedWords.size()) { if (index >= mSuggestedWords.size()) {
return; return;
} }
final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(index); final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(index);
mListener.pickSuggestionManually(index, wordInfo); mListener.pickSuggestionManually(index, wordInfo);
} }
}
@Override @Override
protected void onDetachedFromWindow() { protected void onDetachedFromWindow() {