Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bea4871f authored by Narayan Kamath's avatar Narayan Kamath
Browse files

Parcel: Add support for reading/writing lists of Parcelables.

Follows the same marshalling scheme as other list types, such
as lists of Strings etc.

Bug: 30792387
Test: ParcelTest
Change-Id: I2003f4fcf4de5a1bee060f3c2723cfb155105b14
parent a856247e
Loading
Loading
Loading
Loading
+51 −3
Original line number Diff line number Diff line
@@ -1296,6 +1296,29 @@ public final class Parcel {
        }
    }

    /**
     * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
     * at the current position. They can later be retrieved using
     * {@link #readParcelableList(List, ClassLoader)} if required.
     *
     * @see #readParcelableList(List, ClassLoader)
     * @hide
     */
    public final <T extends Parcelable> void writeParcelableList(List<T> val, int flags) {
        if (val == null) {
            writeInt(-1);
            return;
        }

        int N = val.size();
        int i=0;
        writeInt(N);
        while (i < N) {
            writeParcelable(val.get(i), flags);
            i++;
        }
    }

    /**
     * Flatten a heterogeneous array containing a particular object type into
     * the parcel, at
@@ -2244,9 +2267,6 @@ public final class Parcel {
     * Read into the given List items IBinder objects that were written with
     * {@link #writeBinderList} at the current dataPosition().
     *
     * @return A newly created ArrayList containing strings with the same data
     *         as those that were previously written.
     *
     * @see #writeBinderList
     */
    public final void readBinderList(List<IBinder> list) {
@@ -2264,6 +2284,34 @@ public final class Parcel {
        }
    }

    /**
     * Read the list of {@code Parcelable} objects at the current data position into the
     * given {@code list}. The contents of the {@code list} are replaced. If the serialized
     * list was {@code null}, {@code list} is cleared.
     *
     * @see #writeParcelableList(List, int)
     * @hide
     */
    public final <T extends Parcelable> void readParcelableList(List<T> list, ClassLoader cl) {
        final int N = readInt();
        if (N == -1) {
            list.clear();
            return;
        }

        final int M = list.size();
        int i = 0;
        for (; i < M && i < N; i++) {
            list.set(i, (T) readParcelable(cl));
        }
        for (; i<N; i++) {
            list.add((T) readParcelable(cl));
        }
        for (; i<M; i++) {
            list.remove(N);
        }
    }

    /**
     * Read and return a new array containing a particular object type from
     * the parcel at the current dataPosition().  Returns null if the