Example usage for android.text ParcelableSpan writeToParcel

List of usage examples for android.text ParcelableSpan writeToParcel

Introduction

In this page you can find the example usage for android.text ParcelableSpan writeToParcel.

Prototype

public void writeToParcel(Parcel dest, @WriteFlags int flags);

Source Link

Document

Flatten this object in to a Parcel.

Usage

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

/**
 * Flatten a CharSequence and whatever styles can be copied across processes
 * into the parcel.//w w  w. ja  v  a  2s  .c  o  m
 */
public static void writeToParcel(CharSequence cs, Parcel p, int parcelableFlags) {
    if (cs instanceof Spanned) {
        p.writeInt(0);
        p.writeString(cs.toString());

        Spanned sp = (Spanned) cs;
        Object[] os = sp.getSpans(0, cs.length(), Object.class);

        // note to people adding to this: check more specific types
        // before more generic types.  also notice that it uses
        // "if" instead of "else if" where there are interfaces
        // so one object can be several.

        for (int i = 0; i < os.length; i++) {
            Object o = os[i];
            Object prop = os[i];

            if (prop instanceof CharacterStyle) {
                prop = ((CharacterStyle) prop).getUnderlying();
            }

            if (prop instanceof ParcelableSpan) {
                ParcelableSpan ps = (ParcelableSpan) prop;
                int spanTypeId = ps.getSpanTypeId();
                if (spanTypeId < FIRST_SPAN || spanTypeId > LAST_SPAN) {
                    Log.e(TAG, "external class \"" + ps.getClass().getSimpleName()
                            + "\" is attempting to use the frameworks-only ParcelableSpan" + " interface");
                } else {
                    p.writeInt(spanTypeId);
                    ps.writeToParcel(p, parcelableFlags);
                    writeWhere(p, sp, o);
                }
            }
        }

        p.writeInt(0);
    } else {
        p.writeInt(1);
        if (cs != null) {
            p.writeString(cs.toString());
        } else {
            p.writeString(null);
        }
    }
}