From bea4871fc8096617fffb21cd44ff82bd0e3523d3 Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Thu, 1 Dec 2016 15:38:28 +0000 Subject: [PATCH] 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 --- core/java/android/os/Parcel.java | 54 ++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/core/java/android/os/Parcel.java b/core/java/android/os/Parcel.java index 85f999b26176f..e15f086a88743 100644 --- a/core/java/android/os/Parcel.java +++ b/core/java/android/os/Parcel.java @@ -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 void writeParcelableList(List 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 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 void readParcelableList(List 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