Add Array support

This commit is contained in:
mar-v-in 2015-03-05 23:25:11 +01:00
parent a650ca5bea
commit 462316b18e
2 changed files with 62 additions and 2 deletions

View File

@ -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 <T extends Parcelable> ArrayList<T> readParcelableList(Parcel parcel, int position, Parcelable.Creator<T> creator) {
int length = readStart(parcel, position);
int start = parcel.dataPosition();
if (length == 0)
return null;
ArrayList<T> list = parcel.createTypedArrayList(creator);
parcel.setDataPosition(start + length);
return list;
}
public static ArrayList<String> readStringList(Parcel parcel, int position) {
int length = readStart(parcel, position);
int start = parcel.dataPosition();
if (length == 0)
return null;
ArrayList<String> list = parcel.createStringArrayList();
parcel.setDataPosition(start + length);
return list;
}
public static <T extends Parcelable> T[] readParcelableArray(Parcel parcel, int position, Parcelable.Creator<T> 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);

View File

@ -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))