mirror of
https://github.com/moparisthebest/android_external_SafeParcel
synced 2024-12-12 10:22:14 -05:00
Add Array support
This commit is contained in:
parent
a650ca5bea
commit
462316b18e
@ -21,7 +21,6 @@ import android.os.Parcel;
|
|||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SafeParcelReader {
|
public class SafeParcelReader {
|
||||||
public static int halfOf(int i) {
|
public static int halfOf(int i) {
|
||||||
@ -131,6 +130,46 @@ public class SafeParcelReader {
|
|||||||
return list;
|
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) {
|
public static void skip(Parcel parcel, int position) {
|
||||||
int i = readStart(parcel, position);
|
int i = readStart(parcel, position);
|
||||||
parcel.setDataPosition(parcel.dataPosition() + i);
|
parcel.setDataPosition(parcel.dataPosition() + i);
|
||||||
|
@ -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)
|
private static void writeField(SafeParcelable object, Parcel parcel, Field field, int flags)
|
||||||
throws IllegalAccessException {
|
throws IllegalAccessException {
|
||||||
int num = field.getAnnotation(SafeParceled.class).value();
|
int num = field.getAnnotation(SafeParceled.class).value();
|
||||||
@ -131,6 +135,12 @@ public class SafeParcelUtil {
|
|||||||
case List:
|
case List:
|
||||||
SafeParcelWriter.write(parcel, num, (List) field.get(object), mayNull);
|
SafeParcelWriter.write(parcel, num, (List) field.get(object), mayNull);
|
||||||
break;
|
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:
|
case Integer:
|
||||||
SafeParcelWriter.write(parcel, num, (Integer) field.get(object));
|
SafeParcelWriter.write(parcel, num, (Integer) field.get(object));
|
||||||
break;
|
break;
|
||||||
@ -168,6 +178,12 @@ public class SafeParcelUtil {
|
|||||||
case List:
|
case List:
|
||||||
field.set(object, SafeParcelReader.readList(parcel, position, getClassLoader(field)));
|
field.set(object, SafeParcelReader.readList(parcel, position, getClassLoader(field)));
|
||||||
break;
|
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:
|
case Integer:
|
||||||
field.set(object, SafeParcelReader.readInt(parcel, position));
|
field.set(object, SafeParcelReader.readInt(parcel, position));
|
||||||
break;
|
break;
|
||||||
@ -190,9 +206,14 @@ public class SafeParcelUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private enum SafeParcelType {
|
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) {
|
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))
|
if (Parcelable.class.isAssignableFrom(clazz))
|
||||||
return Parcelable;
|
return Parcelable;
|
||||||
if (IBinder.class.isAssignableFrom(clazz))
|
if (IBinder.class.isAssignableFrom(clazz))
|
||||||
|
Loading…
Reference in New Issue
Block a user