Merge "Bug 5278456: text highlight color depends on the type of suggestion spans."
This commit is contained in:
committed by
Android (Google) Code Review
commit
4f2b0dc552
@@ -28,15 +28,11 @@ import android.text.TextUtils;
|
|||||||
* @hide
|
* @hide
|
||||||
*/
|
*/
|
||||||
public class SuggestionRangeSpan extends CharacterStyle implements ParcelableSpan {
|
public class SuggestionRangeSpan extends CharacterStyle implements ParcelableSpan {
|
||||||
private final int mBackgroundColor;
|
private int mBackgroundColor;
|
||||||
|
|
||||||
@Override
|
public SuggestionRangeSpan() {
|
||||||
public void updateDrawState(TextPaint tp) {
|
// 0 is a fully transparent black. Has to be set using #setBackgroundColor
|
||||||
tp.bgColor = mBackgroundColor;
|
mBackgroundColor = 0;
|
||||||
}
|
|
||||||
|
|
||||||
public SuggestionRangeSpan(int color) {
|
|
||||||
mBackgroundColor = color;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SuggestionRangeSpan(Parcel src) {
|
public SuggestionRangeSpan(Parcel src) {
|
||||||
@@ -57,4 +53,13 @@ public class SuggestionRangeSpan extends CharacterStyle implements ParcelableSpa
|
|||||||
public int getSpanTypeId() {
|
public int getSpanTypeId() {
|
||||||
return TextUtils.SUGGESTION_RANGE_SPAN;
|
return TextUtils.SUGGESTION_RANGE_SPAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setBackgroundColor(int backgroundColor) {
|
||||||
|
mBackgroundColor = backgroundColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDrawState(TextPaint tp) {
|
||||||
|
tp.bgColor = mBackgroundColor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -264,4 +264,14 @@ public class SuggestionSpan extends CharacterStyle implements ParcelableSpan {
|
|||||||
tp.setUnderlineText(mEasyCorrectUnderlineColor, mEasyCorrectUnderlineThickness);
|
tp.setUnderlineText(mEasyCorrectUnderlineColor, mEasyCorrectUnderlineThickness);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The color of the underline for that span, or 0 if there is no underline
|
||||||
|
*/
|
||||||
|
public int getUnderlineColor() {
|
||||||
|
// The order here should match what is used in updateDrawState
|
||||||
|
if ((mFlags & FLAG_MISSPELLED) != 0) return mMisspelledUnderlineColor;
|
||||||
|
if ((mFlags & FLAG_EASY_CORRECT) != 0) return mEasyCorrectUnderlineColor;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9559,8 +9559,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
|
|
||||||
TextView.this.getPositionListener().removeSubscriber(SuggestionsPopupWindow.this);
|
TextView.this.getPositionListener().removeSubscriber(SuggestionsPopupWindow.this);
|
||||||
|
|
||||||
if ((mText instanceof Editable) && mSuggestionRangeSpan != null) {
|
if ((mText instanceof Spannable)) {
|
||||||
((Editable) mText).removeSpan(mSuggestionRangeSpan);
|
((Spannable) mText).removeSpan(mSuggestionRangeSpan);
|
||||||
}
|
}
|
||||||
|
|
||||||
setCursorVisible(mCursorWasVisibleBeforeSuggestions);
|
setCursorVisible(mCursorWasVisibleBeforeSuggestions);
|
||||||
@@ -9744,7 +9744,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean updateSuggestions() {
|
private boolean updateSuggestions() {
|
||||||
Spannable spannable = (Spannable)TextView.this.mText;
|
Spannable spannable = (Spannable) TextView.this.mText;
|
||||||
SuggestionSpan[] suggestionSpans = getSuggestionSpans();
|
SuggestionSpan[] suggestionSpans = getSuggestionSpans();
|
||||||
|
|
||||||
final int nbSpans = suggestionSpans.length;
|
final int nbSpans = suggestionSpans.length;
|
||||||
@@ -9754,6 +9754,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
int spanUnionEnd = 0;
|
int spanUnionEnd = 0;
|
||||||
|
|
||||||
SuggestionSpan misspelledSpan = null;
|
SuggestionSpan misspelledSpan = null;
|
||||||
|
int underlineColor = 0;
|
||||||
|
|
||||||
for (int spanIndex = 0; spanIndex < nbSpans; spanIndex++) {
|
for (int spanIndex = 0; spanIndex < nbSpans; spanIndex++) {
|
||||||
SuggestionSpan suggestionSpan = suggestionSpans[spanIndex];
|
SuggestionSpan suggestionSpan = suggestionSpans[spanIndex];
|
||||||
@@ -9766,6 +9767,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
misspelledSpan = suggestionSpan;
|
misspelledSpan = suggestionSpan;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The first span dictates the background color of the highlighted text
|
||||||
|
if (spanIndex == 0) underlineColor = suggestionSpan.getUnderlineColor();
|
||||||
|
|
||||||
String[] suggestions = suggestionSpan.getSuggestions();
|
String[] suggestions = suggestionSpan.getSuggestions();
|
||||||
int nbSuggestions = suggestions.length;
|
int nbSuggestions = suggestions.length;
|
||||||
for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) {
|
for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) {
|
||||||
@@ -9808,9 +9812,17 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
|
|
||||||
if (mNumberOfSuggestions == 0) return false;
|
if (mNumberOfSuggestions == 0) return false;
|
||||||
|
|
||||||
if (mSuggestionRangeSpan == null) mSuggestionRangeSpan =
|
if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = new SuggestionRangeSpan();
|
||||||
new SuggestionRangeSpan(mHighlightColor);
|
if (underlineColor == 0) {
|
||||||
((Editable) mText).setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd,
|
// Fallback on the default highlight color when the first span does not provide one
|
||||||
|
mSuggestionRangeSpan.setBackgroundColor(mHighlightColor);
|
||||||
|
} else {
|
||||||
|
final float BACKGROUND_TRANSPARENCY = 0.3f;
|
||||||
|
final int newAlpha = (int) (Color.alpha(underlineColor) * BACKGROUND_TRANSPARENCY);
|
||||||
|
mSuggestionRangeSpan.setBackgroundColor(
|
||||||
|
(underlineColor & 0x00FFFFFF) + (newAlpha << 24));
|
||||||
|
}
|
||||||
|
spannable.setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd,
|
||||||
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||||
|
|
||||||
mSuggestionsAdapter.notifyDataSetChanged();
|
mSuggestionsAdapter.notifyDataSetChanged();
|
||||||
|
|||||||
Reference in New Issue
Block a user