Add support for Lists

This commit is contained in:
mar-v-in 2015-02-10 03:03:27 +01:00
parent 3efecf38ca
commit a650ca5bea
5 changed files with 313 additions and 279 deletions

View File

@ -20,6 +20,9 @@ import android.os.IBinder;
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) {
return i & 0xFFFF;
@ -118,6 +121,16 @@ public class SafeParcelReader {
return t;
}
public static ArrayList readList(Parcel parcel, int position, ClassLoader classLoader) {
int length = readStart(parcel, position);
int start = parcel.dataPosition();
if (length == 0)
return null;
ArrayList list = parcel.readArrayList(classLoader);
parcel.setDataPosition(start + length);
return list;
}
public static void skip(Parcel parcel, int position) {
int i = readStart(parcel, position);
parcel.setDataPosition(parcel.dataPosition() + i);

View File

@ -8,7 +8,9 @@ import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SafeParcelUtil {
@ -104,6 +106,15 @@ public class SafeParcelUtil {
}
}
private static ClassLoader getClassLoader(Field field) {
try {
String type = field.getAnnotation(SafeParceled.class).subType();
return Class.forName(type).getClassLoader();
} catch (ClassNotFoundException e) {
return ClassLoader.getSystemClassLoader();
}
}
private static void writeField(SafeParcelable object, Parcel parcel, Field field, int flags)
throws IllegalAccessException {
int num = field.getAnnotation(SafeParceled.class).value();
@ -117,6 +128,9 @@ public class SafeParcelUtil {
case Binder:
SafeParcelWriter.write(parcel, num, (IBinder) field.get(object), mayNull);
break;
case List:
SafeParcelWriter.write(parcel, num, (List) field.get(object), mayNull);
break;
case Integer:
SafeParcelWriter.write(parcel, num, (Integer) field.get(object));
break;
@ -151,6 +165,9 @@ public class SafeParcelUtil {
field.set(object, SafeParcelReader.readBinder(parcel,
position));
break;
case List:
field.set(object, SafeParcelReader.readList(parcel, position, getClassLoader(field)));
break;
case Integer:
field.set(object, SafeParcelReader.readInt(parcel, position));
break;
@ -173,13 +190,15 @@ public class SafeParcelUtil {
}
private enum SafeParcelType {
Parcelable, Binder, Integer, Long, Boolean, Float, Double, String;
Parcelable, Binder, Integer, Long, Boolean, Float, Double, String, List;
public static SafeParcelType fromClass(Class clazz) {
if (Parcelable.class.isAssignableFrom(clazz))
return Parcelable;
if (IBinder.class.isAssignableFrom(clazz))
return Binder;
if (clazz == List.class || clazz == ArrayList.class)
return List;
if (clazz == int.class || clazz == Integer.class)
return Integer;
if (clazz == boolean.class || clazz == Boolean.class)

View File

@ -11,4 +11,6 @@ public @interface SafeParceled {
int value();
boolean mayNull() default false;
String subType() default "undefined";
}