Separate debug information display from candidate view

This change is one of the preparations for suggestion strip redesign.

Bug: 4175031
Change-Id: I82675936773ed6cf132a52486909ff21af828256
main
Tadashi G. Takaoka 2011-06-07 17:18:53 +09:00
parent f20eb55d4f
commit 1e273fa6d1
4 changed files with 87 additions and 95 deletions

View File

@ -18,31 +18,14 @@
*/ */
--> -->
<LinearLayout <Button
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:orientation="horizontal" android:minWidth="@dimen/candidate_min_width"
> android:textSize="@dimen/candidate_text_size"
<Button android:textColor="@color/candidate_typed_word"
android:id="@+id/candidate_word" android:focusable="true"
android:layout_width="wrap_content" android:clickable="true"
android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal"
android:minWidth="@dimen/candidate_min_width" style="?attr/suggestionBackgroundStyle" />
android:textSize="@dimen/candidate_text_size"
android:textColor="@color/candidate_normal"
android:focusable="true"
android:clickable="true"
android:gravity="center_vertical|center_horizontal"
style="?attr/suggestionBackgroundStyle" />
<TextView
android:id="@+id/candidate_debug_info"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:visibility="gone"
android:textSize="10dip"
android:textColor="#ff808080"
android:focusable="false"
android:clickable="false"
android:gravity="bottom" />
</LinearLayout>

View File

@ -20,7 +20,6 @@
<ImageView <ImageView
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/candidate_divider"
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:src="@drawable/keyboard_suggest_strip_divider"

View File

@ -18,9 +18,9 @@
*/ */
--> -->
<resources> <resources>
<color name="candidate_normal">#FFFFFFFF</color> <color name="candidate_typed_word">#FFFFFFFF</color>
<color name="candidate_recommended">#FFFCAE00</color> <color name="candidate_auto_correct">#FFFCAE00</color>
<color name="candidate_other">#FFFCAE00</color> <color name="candidate_suggested">#FFFCAE00</color>
<color name="latinkeyboard_bar_language_shadow_white">#80000000</color> <color name="latinkeyboard_bar_language_shadow_white">#80000000</color>
<color name="latinkeyboard_bar_language_shadow_black">#80FFFFFF</color> <color name="latinkeyboard_bar_language_shadow_black">#80FFFFFF</color>
<color name="latinkeyboard_bar_language_text">#FFC0C0C0</color> <color name="latinkeyboard_bar_language_text">#FFC0C0C0</color>

View File

@ -55,14 +55,15 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
private static final boolean DBG = LatinImeLogger.sDBG; private static final boolean DBG = LatinImeLogger.sDBG;
private final ArrayList<View> mWords = new ArrayList<View>(); private final ArrayList<TextView> mWords = new ArrayList<TextView>();
private final ArrayList<View> mDividers = new ArrayList<View>(); private final ArrayList<View> mDividers = new ArrayList<View>();
private final int mCandidatePadding;
private final boolean mConfigCandidateHighlightFontColorEnabled; private final boolean mConfigCandidateHighlightFontColorEnabled;
private final CharacterStyle mInvertedForegroundColorSpan; private final CharacterStyle mInvertedForegroundColorSpan;
private final CharacterStyle mInvertedBackgroundColorSpan; private final CharacterStyle mInvertedBackgroundColorSpan;
private final int mColorNormal; private final int mColorTypedWord;
private final int mColorRecommended; private final int mColorAutoCorrect;
private final int mColorOther; private final int mColorSuggestedCandidate;
private final PopupWindow mPreviewPopup; private final PopupWindow mPreviewPopup;
private final TextView mPreviewText; private final TextView mPreviewText;
@ -135,20 +136,20 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
mPreviewPopup.setBackgroundDrawable(null); mPreviewPopup.setBackgroundDrawable(null);
mConfigCandidateHighlightFontColorEnabled = mConfigCandidateHighlightFontColorEnabled =
res.getBoolean(R.bool.config_candidate_highlight_font_color_enabled); res.getBoolean(R.bool.config_candidate_highlight_font_color_enabled);
mColorNormal = res.getColor(R.color.candidate_normal); mColorTypedWord = res.getColor(R.color.candidate_typed_word);
mColorRecommended = res.getColor(R.color.candidate_recommended); mColorAutoCorrect = res.getColor(R.color.candidate_auto_correct);
mColorOther = res.getColor(R.color.candidate_other); mColorSuggestedCandidate = res.getColor(R.color.candidate_suggested);
mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorNormal ^ 0x00ffffff); mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff);
mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorNormal); mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord);
mCandidatePadding = res.getDimensionPixelOffset(R.dimen.candidate_padding);
for (int i = 0; i < MAX_SUGGESTIONS; i++) { for (int i = 0; i < MAX_SUGGESTIONS; i++) {
View v = inflater.inflate(R.layout.candidate, null); final TextView tv = (TextView)inflater.inflate(R.layout.candidate, null);
TextView tv = (TextView)v.findViewById(R.id.candidate_word);
tv.setTag(i); tv.setTag(i);
tv.setOnClickListener(this); tv.setOnClickListener(this);
if (i == 0) if (i == 0)
tv.setOnLongClickListener(this); tv.setOnLongClickListener(this);
mWords.add(v); mWords.add(tv);
if (i > 0) { if (i > 0) {
View divider = inflater.inflate(R.layout.candidate_divider, null); View divider = inflater.inflate(R.layout.candidate_divider, null);
mDividers.add(divider); mDividers.add(divider);
@ -177,70 +178,79 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
} }
} }
private CharSequence getStyledCandidateWord(CharSequence word, boolean isAutoCorrect) {
if (!isAutoCorrect)
return word;
final CharacterStyle style = mConfigCandidateHighlightFontColorEnabled ? BOLD_SPAN
: UNDERLINE_SPAN;
final Spannable spannedWord = new SpannableString(word);
spannedWord.setSpan(style, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return spannedWord;
}
private int getCandidateTextColor(boolean isAutoCorrect, boolean isSuggestedCandidate,
SuggestedWordInfo info) {
final int color;
if (isAutoCorrect && mConfigCandidateHighlightFontColorEnabled) {
color = mColorAutoCorrect;
} else if (isSuggestedCandidate) {
color = mColorSuggestedCandidate;
} else {
color = mColorTypedWord;
}
if (info != null && info.isPreviousSuggestedWord()) {
final int newAlpha = (int)(Color.alpha(color) * 0.5f);
return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color));
} else {
return color;
}
}
private void updateSuggestions() { private void updateSuggestions() {
final SuggestedWords suggestions = mSuggestions; final SuggestedWords suggestions = mSuggestions;
final List<SuggestedWordInfo> suggestedWordInfoList = suggestions.mSuggestedWordInfoList;
clear(); clear();
final int count = Math.min(mWords.size(), suggestions.size()); final int count = Math.min(mWords.size(), suggestions.size());
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
CharSequence word = suggestions.getWord(i); final CharSequence word = suggestions.getWord(i);
if (word == null) continue; if (word == null) continue;
final int wordLength = word.length();
final List<SuggestedWordInfo> suggestedWordInfoList =
suggestions.mSuggestedWordInfoList;
final View v = mWords.get(i); final SuggestedWordInfo info = (suggestedWordInfoList != null)
final TextView tv = (TextView)v.findViewById(R.id.candidate_word); ? suggestedWordInfoList.get(i) : null;
final TextView dv = (TextView)v.findViewById(R.id.candidate_debug_info); final boolean isAutoCorrect = suggestions.mHasMinimalSuggestion
tv.setTextColor(mColorNormal);
// TODO: Needs safety net?
if (suggestions.mHasMinimalSuggestion
&& ((i == 1 && !suggestions.mTypedWordValid) && ((i == 1 && !suggestions.mTypedWordValid)
|| (i == 0 && suggestions.mTypedWordValid))) { || (i == 0 && suggestions.mTypedWordValid));
final CharacterStyle style; // HACK: even if i == 0, we use mColorOther when this suggestion's length is 1
if (mConfigCandidateHighlightFontColorEnabled) { // and there are multiple suggestions, such as the default punctuation list.
style = BOLD_SPAN; // TODO: Need to revisit this logic with bigram suggestions
tv.setTextColor(mColorRecommended); final boolean isSuggestedCandidate = (i != 0);
} else { final boolean isPunctuationSuggestions = (word.length() == 1 && count > 1);
style = UNDERLINE_SPAN;
}
final Spannable spannedWord = new SpannableString(word);
spannedWord.setSpan(style, 0, wordLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
word = spannedWord;
} else if (i != 0 || (wordLength == 1 && count > 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.
if (mConfigCandidateHighlightFontColorEnabled)
tv.setTextColor(mColorOther);
}
tv.setText(word);
tv.setClickable(true);
if (suggestedWordInfoList != null && suggestedWordInfoList.get(i) != null) { final TextView tv = mWords.get(i);
final SuggestedWordInfo info = suggestedWordInfoList.get(i); tv.setTextColor(getCandidateTextColor(isAutoCorrect,
if (info.isPreviousSuggestedWord()) { isSuggestedCandidate || isPunctuationSuggestions, info));
int color = tv.getCurrentTextColor(); tv.setText(getStyledCandidateWord(word, isAutoCorrect));
tv.setTextColor(Color.argb((int)(Color.alpha(color) * 0.5f), Color.red(color), if (i == 0) {
Color.green(color), Color.blue(color))); tv.setPadding(mCandidatePadding, 0, 0, 0);
} } else if (i == count - 1) {
final String debugString = info.getDebugString(); tv.setPadding(0, 0, mCandidatePadding, 0);
if (DBG) {
if (TextUtils.isEmpty(debugString)) {
dv.setVisibility(GONE);
} else {
dv.setText(debugString);
dv.setVisibility(VISIBLE);
}
} else {
dv.setVisibility(GONE);
}
} else { } else {
dv.setVisibility(GONE); tv.setPadding(0, 0, 0, 0);
} }
if (i > 0) if (i > 0)
addView(mDividers.get(i - 1)); addView(mDividers.get(i - 1));
addView(v); addView(tv);
if (DBG && info != null) {
final TextView dv = new TextView(getContext(), null);
dv.setTextSize(10.0f);
dv.setTextColor(0xff808080);
dv.setText(info.getDebugString());
addView(dv);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)dv.getLayoutParams();
lp.gravity = Gravity.BOTTOM;
}
} }
scrollTo(0, getScrollY()); scrollTo(0, getScrollY());
@ -252,7 +262,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
// with color is disabled. // with color is disabled.
if (mConfigCandidateHighlightFontColorEnabled) if (mConfigCandidateHighlightFontColorEnabled)
return; return;
final TextView tv = (TextView)mWords.get(1).findViewById(R.id.candidate_word); final TextView tv = mWords.get(1);
final Spannable word = new SpannableString(autoCorrectedWord); final Spannable word = new SpannableString(autoCorrectedWord);
final int wordLength = word.length(); final int wordLength = word.length();
word.setSpan(mInvertedBackgroundColorSpan, 0, wordLength, word.setSpan(mInvertedBackgroundColorSpan, 0, wordLength,
@ -278,7 +288,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
setSuggestions(builder.build()); setSuggestions(builder.build());
mShowingAddToDictionary = true; mShowingAddToDictionary = true;
// Disable R.string.hint_add_to_dictionary button // Disable R.string.hint_add_to_dictionary button
TextView tv = (TextView)mWords.get(1).findViewById(R.id.candidate_word); TextView tv = mWords.get(1);
tv.setClickable(false); tv.setClickable(false);
} }
@ -307,7 +317,7 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo
return; return;
final TextView previewText = mPreviewText; final TextView previewText = mPreviewText;
previewText.setTextColor(mColorNormal); previewText.setTextColor(mColorTypedWord);
previewText.setText(word); previewText.setText(word);
previewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), previewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));