diff --git a/src/org/microg/safeparcel/SafeParcelReader.java b/src/org/microg/safeparcel/SafeParcelReader.java index 4bc26db..7ece470 100644 --- a/src/org/microg/safeparcel/SafeParcelReader.java +++ b/src/org/microg/safeparcel/SafeParcelReader.java @@ -21,7 +21,6 @@ import android.os.Parcel; import android.os.Parcelable; import java.util.ArrayList; -import java.util.List; public class SafeParcelReader { public static int halfOf(int i) { @@ -131,6 +130,46 @@ public class SafeParcelReader { return list; } + public static ArrayList readParcelableList(Parcel parcel, int position, Parcelable.Creator creator) { + int length = readStart(parcel, position); + int start = parcel.dataPosition(); + if (length == 0) + return null; + ArrayList list = parcel.createTypedArrayList(creator); + parcel.setDataPosition(start + length); + return list; + } + + public static ArrayList readStringList(Parcel parcel, int position) { + int length = readStart(parcel, position); + int start = parcel.dataPosition(); + if (length == 0) + return null; + ArrayList list = parcel.createStringArrayList(); + parcel.setDataPosition(start + length); + return list; + } + + public static T[] readParcelableArray(Parcel parcel, int position, Parcelable.Creator creator) { + int length = readStart(parcel, position); + int start = parcel.dataPosition(); + if (length == 0) + return null; + T[] arr = parcel.createTypedArray(creator); + parcel.setDataPosition(start + length); + return arr; + } + + public static String[] readStringArray(Parcel parcel, int position) { + int length = readStart(parcel, position); + int start = parcel.dataPosition(); + if (length == 0) + return null; + String[] arr = parcel.createStringArray(); + parcel.setDataPosition(start + length); + return arr; + } + public static void skip(Parcel parcel, int position) { int i = readStart(parcel, position); parcel.setDataPosition(parcel.dataPosition() + i); diff --git a/src/org/microg/safeparcel/SafeParcelUtil.java b/src/org/microg/safeparcel/SafeParcelUtil.java index 1b39ebe..108433e 100644 --- a/src/org/microg/safeparcel/SafeParcelUtil.java +++ b/src/org/microg/safeparcel/SafeParcelUtil.java @@ -115,6 +115,10 @@ public class SafeParcelUtil { } } + private static ClassLoader getArrayClassLoader(Field field) { + return field.getType().getComponentType().getClassLoader(); + } + private static void writeField(SafeParcelable object, Parcel parcel, Field field, int flags) throws IllegalAccessException { int num = field.getAnnotation(SafeParceled.class).value(); @@ -131,6 +135,12 @@ public class SafeParcelUtil { case List: SafeParcelWriter.write(parcel, num, (List) field.get(object), mayNull); break; + case ParcelableArray: + SafeParcelWriter.write(parcel, num, (Parcelable[]) field.get(object), flags, mayNull); + break; + case StringArray: + SafeParcelWriter.write(parcel, num, (String[]) field.get(object), mayNull); + break; case Integer: SafeParcelWriter.write(parcel, num, (Integer) field.get(object)); break; @@ -168,6 +178,12 @@ public class SafeParcelUtil { case List: field.set(object, SafeParcelReader.readList(parcel, position, getClassLoader(field))); break; + case ParcelableArray: + field.set(object, SafeParcelReader.readParcelableArray(parcel, position, getCreator(field))); + break; + case StringArray: + field.set(object, SafeParcelReader.readStringArray(parcel, position)); + break; case Integer: field.set(object, SafeParcelReader.readInt(parcel, position)); break; @@ -190,9 +206,14 @@ public class SafeParcelUtil { } private enum SafeParcelType { - Parcelable, Binder, Integer, Long, Boolean, Float, Double, String, List; + Parcelable, Binder, List, ParcelableArray, StringArray, + Integer, Long, Boolean, Float, Double, String; public static SafeParcelType fromClass(Class clazz) { + if (clazz.isArray() && Parcelable.class.isAssignableFrom(clazz.getComponentType())) + return ParcelableArray; + if (clazz.isArray() && String.class.isAssignableFrom(clazz.getComponentType())) + return StringArray; if (Parcelable.class.isAssignableFrom(clazz)) return Parcelable; if (IBinder.class.isAssignableFrom(clazz))