SuggestionSpanpublic class SuggestionSpan extends CharacterStyle implements android.text.ParcelableSpanHolds suggestion candidates for the text enclosed in this span.
When such a span is edited in an EditText, double tapping on the text enclosed in this span will
display a popup dialog listing suggestion replacement for that text. The user can then replace
the original text by one of the suggestions.
These spans should typically be created by the input method to provide correction and alternates
for the text. |
Fields Summary |
---|
private static final String | TAG | public static final int | FLAG_EASY_CORRECTSets this flag if the suggestions should be easily accessible with few interactions.
This flag should be set for every suggestions that the user is likely to use. | public static final int | FLAG_MISSPELLEDSets this flag if the suggestions apply to a misspelled word/text. This type of suggestion is
rendered differently to highlight the error. | public static final int | FLAG_AUTO_CORRECTIONSets this flag if the auto correction is about to be applied to a word/text
that the user is typing/composing. This type of suggestion is rendered differently
to indicate the auto correction is happening. | public static final String | ACTION_SUGGESTION_PICKED | public static final String | SUGGESTION_SPAN_PICKED_AFTER | public static final String | SUGGESTION_SPAN_PICKED_BEFORE | public static final String | SUGGESTION_SPAN_PICKED_HASHCODE | public static final int | SUGGESTIONS_MAX_SIZE | private int | mFlags | private final String[] | mSuggestions | private final String | mLocaleString | private final String | mNotificationTargetClassName | private final String | mNotificationTargetPackageName | private final int | mHashCode | private float | mEasyCorrectUnderlineThickness | private int | mEasyCorrectUnderlineColor | private float | mMisspelledUnderlineThickness | private int | mMisspelledUnderlineColor | private float | mAutoCorrectionUnderlineThickness | private int | mAutoCorrectionUnderlineColor | public static final Parcelable.Creator | CREATOR |
Constructors Summary |
---|
public SuggestionSpan(android.content.Context context, String[] suggestions, int flags)
this(context, null, suggestions, flags, null);
| public SuggestionSpan(Locale locale, String[] suggestions, int flags)
this(null, locale, suggestions, flags, null);
| public SuggestionSpan(android.content.Context context, Locale locale, String[] suggestions, int flags, Class notificationTargetClass)
final int N = Math.min(SUGGESTIONS_MAX_SIZE, suggestions.length);
mSuggestions = Arrays.copyOf(suggestions, N);
mFlags = flags;
if (locale != null) {
mLocaleString = locale.toString();
} else if (context != null) {
mLocaleString = context.getResources().getConfiguration().locale.toString();
} else {
Log.e("SuggestionSpan", "No locale or context specified in SuggestionSpan constructor");
mLocaleString = "";
}
if (context != null) {
mNotificationTargetPackageName = context.getPackageName();
} else {
mNotificationTargetPackageName = null;
}
if (notificationTargetClass != null) {
mNotificationTargetClassName = notificationTargetClass.getCanonicalName();
} else {
mNotificationTargetClassName = "";
}
mHashCode = hashCodeInternal(mSuggestions, mLocaleString, mNotificationTargetClassName);
initStyle(context);
| public SuggestionSpan(android.os.Parcel src)
mSuggestions = src.readStringArray();
mFlags = src.readInt();
mLocaleString = src.readString();
mNotificationTargetClassName = src.readString();
mNotificationTargetPackageName = src.readString();
mHashCode = src.readInt();
mEasyCorrectUnderlineColor = src.readInt();
mEasyCorrectUnderlineThickness = src.readFloat();
mMisspelledUnderlineColor = src.readInt();
mMisspelledUnderlineThickness = src.readFloat();
mAutoCorrectionUnderlineColor = src.readInt();
mAutoCorrectionUnderlineThickness = src.readFloat();
|
Methods Summary |
---|
public int | describeContents()
return 0;
| public boolean | equals(java.lang.Object o)
if (o instanceof SuggestionSpan) {
return ((SuggestionSpan)o).hashCode() == mHashCode;
}
return false;
| public int | getFlags()
return mFlags;
| public java.lang.String | getLocale()
return mLocaleString;
| public java.lang.String | getNotificationTargetClassName()
return mNotificationTargetClassName;
| public int | getSpanTypeId()
return TextUtils.SUGGESTION_SPAN;
| public java.lang.String[] | getSuggestions()
return mSuggestions;
| public int | getUnderlineColor()
// The order here should match what is used in updateDrawState
final boolean misspelled = (mFlags & FLAG_MISSPELLED) != 0;
final boolean easy = (mFlags & FLAG_EASY_CORRECT) != 0;
final boolean autoCorrection = (mFlags & FLAG_AUTO_CORRECTION) != 0;
if (easy) {
if (!misspelled) {
return mEasyCorrectUnderlineColor;
} else {
return mMisspelledUnderlineColor;
}
} else if (autoCorrection) {
return mAutoCorrectionUnderlineColor;
}
return 0;
| public int | hashCode()
return mHashCode;
| private static int | hashCodeInternal(java.lang.String[] suggestions, java.lang.String locale, java.lang.String notificationTargetClassName)
return Arrays.hashCode(new Object[] {Long.valueOf(SystemClock.uptimeMillis()), suggestions,
locale, notificationTargetClassName});
| private void | initStyle(android.content.Context context)
if (context == null) {
mMisspelledUnderlineThickness = 0;
mEasyCorrectUnderlineThickness = 0;
mAutoCorrectionUnderlineThickness = 0;
mMisspelledUnderlineColor = Color.BLACK;
mEasyCorrectUnderlineColor = Color.BLACK;
mAutoCorrectionUnderlineColor = Color.BLACK;
return;
}
int defStyleAttr = com.android.internal.R.attr.textAppearanceMisspelledSuggestion;
TypedArray typedArray = context.obtainStyledAttributes(
null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0);
mMisspelledUnderlineThickness = typedArray.getDimension(
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0);
mMisspelledUnderlineColor = typedArray.getColor(
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
defStyleAttr = com.android.internal.R.attr.textAppearanceEasyCorrectSuggestion;
typedArray = context.obtainStyledAttributes(
null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0);
mEasyCorrectUnderlineThickness = typedArray.getDimension(
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0);
mEasyCorrectUnderlineColor = typedArray.getColor(
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
defStyleAttr = com.android.internal.R.attr.textAppearanceAutoCorrectionSuggestion;
typedArray = context.obtainStyledAttributes(
null, com.android.internal.R.styleable.SuggestionSpan, defStyleAttr, 0);
mAutoCorrectionUnderlineThickness = typedArray.getDimension(
com.android.internal.R.styleable.SuggestionSpan_textUnderlineThickness, 0);
mAutoCorrectionUnderlineColor = typedArray.getColor(
com.android.internal.R.styleable.SuggestionSpan_textUnderlineColor, Color.BLACK);
| public void | notifySelection(android.content.Context context, java.lang.String original, int index)Notifies a suggestion selection.
final Intent intent = new Intent();
if (context == null || mNotificationTargetClassName == null) {
return;
}
// Ensures that only a class in the original IME package will receive the
// notification.
if (mSuggestions == null || index < 0 || index >= mSuggestions.length) {
Log.w(TAG, "Unable to notify the suggestion as the index is out of range index=" + index
+ " length=" + mSuggestions.length);
return;
}
// The package name is not mandatory (legacy from JB), and if the package name
// is missing, we try to notify the suggestion through the input method manager.
if (mNotificationTargetPackageName != null) {
intent.setClassName(mNotificationTargetPackageName, mNotificationTargetClassName);
intent.setAction(SuggestionSpan.ACTION_SUGGESTION_PICKED);
intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_BEFORE, original);
intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_AFTER, mSuggestions[index]);
intent.putExtra(SuggestionSpan.SUGGESTION_SPAN_PICKED_HASHCODE, hashCode());
context.sendBroadcast(intent);
} else {
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) {
imm.notifySuggestionPicked(this, original, index);
}
}
| public void | setFlags(int flags)
mFlags = flags;
| public void | updateDrawState(android.text.TextPaint tp)
final boolean misspelled = (mFlags & FLAG_MISSPELLED) != 0;
final boolean easy = (mFlags & FLAG_EASY_CORRECT) != 0;
final boolean autoCorrection = (mFlags & FLAG_AUTO_CORRECTION) != 0;
if (easy) {
if (!misspelled) {
tp.setUnderlineText(mEasyCorrectUnderlineColor, mEasyCorrectUnderlineThickness);
} else if (tp.underlineColor == 0) {
// Spans are rendered in an arbitrary order. Since misspelled is less prioritary
// than just easy, do not apply misspelled if an easy (or a mispelled) has been set
tp.setUnderlineText(mMisspelledUnderlineColor, mMisspelledUnderlineThickness);
}
} else if (autoCorrection) {
tp.setUnderlineText(mAutoCorrectionUnderlineColor, mAutoCorrectionUnderlineThickness);
}
| public void | writeToParcel(android.os.Parcel dest, int flags)
dest.writeStringArray(mSuggestions);
dest.writeInt(mFlags);
dest.writeString(mLocaleString);
dest.writeString(mNotificationTargetClassName);
dest.writeString(mNotificationTargetPackageName);
dest.writeInt(mHashCode);
dest.writeInt(mEasyCorrectUnderlineColor);
dest.writeFloat(mEasyCorrectUnderlineThickness);
dest.writeInt(mMisspelledUnderlineColor);
dest.writeFloat(mMisspelledUnderlineThickness);
dest.writeInt(mAutoCorrectionUnderlineColor);
dest.writeFloat(mAutoCorrectionUnderlineThickness);
|
|