FileDocCategorySizeDatePackage
SuggestionSpan.javaAPI DocAndroid 5.1 API15051Thu Mar 12 22:22:10 GMT 2015android.text.style

SuggestionSpan

public class SuggestionSpan extends CharacterStyle implements android.text.ParcelableSpan
Holds 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.
see
TextView#isSuggestionsEnabled()

Fields Summary
private static final String
TAG
public static final int
FLAG_EASY_CORRECT
Sets 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_MISSPELLED
Sets 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_CORRECTION
Sets 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)

param
context Context for the application
param
suggestions Suggestions for the string under the span
param
flags Additional flags indicating how this span is handled in TextView


                                    
           
        this(context, null, suggestions, flags, null);
    
public SuggestionSpan(Locale locale, String[] suggestions, int flags)

param
locale Locale of the suggestions
param
suggestions Suggestions for the string under the span
param
flags Additional flags indicating how this span is handled in TextView

        this(null, locale, suggestions, flags, null);
    
public SuggestionSpan(android.content.Context context, Locale locale, String[] suggestions, int flags, Class notificationTargetClass)

param
context Context for the application
param
locale locale Locale of the suggestions
param
suggestions Suggestions for the string under the span. Only the first up to {@link SuggestionSpan#SUGGESTIONS_MAX_SIZE} will be considered. Null values not permitted.
param
flags Additional flags indicating how this span is handled in TextView
param
notificationTargetClass if not null, this class will get notified when the user selects one of the suggestions.

        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 intdescribeContents()

        return 0;
    
public booleanequals(java.lang.Object o)

        if (o instanceof SuggestionSpan) {
            return ((SuggestionSpan)o).hashCode() == mHashCode;
        }
        return false;
    
public intgetFlags()

        return mFlags;
    
public java.lang.StringgetLocale()

return
the locale of the suggestions

        return mLocaleString;
    
public java.lang.StringgetNotificationTargetClassName()

return
The name of the class to notify. The class of the original IME package will receive a notification when the user selects one of the suggestions. The notification will include the original string, the suggested replacement string as well as the hashCode of this span. The class will get notified by an intent that has those information. This is an internal API because only the framework should know the class name.
hide

        return mNotificationTargetClassName;
    
public intgetSpanTypeId()

        return TextUtils.SUGGESTION_SPAN;
    
public java.lang.String[]getSuggestions()

return
an array of suggestion texts for this span

        return mSuggestions;
    
public intgetUnderlineColor()

return
The color of the underline for that span, or 0 if there is no underline
hide

        // 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 inthashCode()

        return mHashCode;
    
private static inthashCodeInternal(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 voidinitStyle(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 voidnotifySelection(android.content.Context context, java.lang.String original, int index)
Notifies a suggestion selection.

hide

        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 voidsetFlags(int flags)

        mFlags = flags;
    
public voidupdateDrawState(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 voidwriteToParcel(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);