Home » Android » android – Explain the definitions of these flags (SPAN_COMPOSING, SPAN_USER, etc.) from the Spanned interface

android – Explain the definitions of these flags (SPAN_COMPOSING, SPAN_USER, etc.) from the Spanned interface

Posted by: admin June 15, 2020 Leave a comment

Questions:

Can someone provide a clear explanation of the flags found in the Spanned Java interface for Android? The documentation is not clear and I require more details in the same way other flags were explained in this SO answer.

Requesting definitions for the following flags:

SPAN_COMPOSING
SPAN_INTERMEDIATE
SPAN_POINT_MARK_MASK
SPAN_PRIORITY
SPAN_USER
SPAN_USER_SHIFT
How to&Answers:

SPAN_COMPOSING is a flag used on text being input, and is considered a temporary span, intended to be removed once input is completed, “This flag is set on spans that are being used to apply temporary styling information on the composing text of an input method, so that they can be found and removed when the composing text is being replaced.”

Here is an example of code which uses SPAN_COMPOSING and removes it from the text; relevant code is quoted below:

public static final void removeComposingSpans(Spannable text) {

    text.removeSpan(COMPOSING);
    Object[] sps = text.getSpans(0, text.length(), Object.class);

    if (sps != null) {

        for (int i = sps.length-1; i >= 0; i--) {

            Object o = sps[i];

            if ((text.getSpanFlags(o) & Spanned.SPAN_COMPOSING) != 0) {

                text.removeSpan(o);
            }
        }
    }
}

public static void setComposingSpans(Spannable text) {

    final Object[] sps = text.getSpans(0, text.length(), Object.class);

    if (sps != null) {

        for (int i = sps.length - 1; i >= 0; i--) {

            final Object o = sps[i];

            if (o == COMPOSING) {

                text.removeSpan(o);
                continue;
            }

            final int fl = text.getSpanFlags(o);

            if ((fl & (Spanned.SPAN_COMPOSING | Spanned.SPAN_POINT_MARK_MASK)) 
                    != (Spanned.SPAN_COMPOSING | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)) {

                text.setSpan(o, text.getSpanStart(o), text.getSpanEnd(o),
                        (fl & Spanned.SPAN_POINT_MARK_MASK)
                                | Spanned.SPAN_COMPOSING
                                | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }

    text.setSpan(COMPOSING, 0, text.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
}

SPAN_INTERMEDIATE is to be treated as a flag used to assign to a span temporarily while it is undergoing change, and to be removed after the change is applied. “This flag will be set for intermediate span changes, meaning there is guaranteed to be another change following it.”

SPAN_POINT_MARK_MASK is a bitmask, primarily used for comparison purposes to check if a qualifying bit state was applied. See proper use for it in the code quoted above.

SPAN_PRIORITY refers to the priority of the text layout for update purposes; the API notes that it should only be set during extraordinary circumstances and is therefore not necessary to be set by the developer.

SPAN_USER and SPAN_USER_SHIFT are storage areas for additional custom scalar data to be stored with the span if the developer chooses to use them.