Guest User

Untitled

a guest
May 31st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. import java.net.MalformedURLException;
  2. import java.net.URL;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import android.os.Parcel;
  10. import android.os.Parcelable;
  11.  
  12. public class ParcelableUtils {
  13. public static void write(Parcel dest, String string) {
  14. dest.writeByte((byte) (string == null ? 0 : 1));
  15. if (string != null) {
  16. dest.writeString(string);
  17. }
  18. }
  19.  
  20. public static String readString(Parcel source) {
  21. if (source.readByte() == 1) {
  22. return source.readString();
  23. }
  24. return null;
  25. }
  26.  
  27. public static void write(Parcel dest, Parcelable parcelable, int flags) {
  28. dest.writeByte((byte) (parcelable == null ? 0 : 1));
  29. if (parcelable != null) {
  30. dest.writeParcelable(parcelable, flags);
  31. }
  32. }
  33.  
  34. public static <T extends Parcelable> T readParcelable(Parcel source) {
  35. if (source.readByte() == 1) {
  36. return source.readParcelable(null);
  37. }
  38. return null;
  39. }
  40.  
  41. public static void write(Parcel dest, Map<String, String> strings) {
  42. if (strings == null) {
  43. dest.writeInt(-1);
  44. }
  45. {
  46. dest.writeInt(strings.keySet().size());
  47. for (String key : strings.keySet()) {
  48. dest.writeString(key);
  49. dest.writeString(strings.get(key));
  50. }
  51. }
  52. }
  53.  
  54. public static Map<String, String> readStringMap(Parcel source) {
  55. int numKeys = source.readInt();
  56. if (numKeys == -1) {
  57. return null;
  58. }
  59. HashMap<String, String> map = new HashMap<String, String>();
  60. for (int i = 0; i < numKeys; i++) {
  61. String key = source.readString();
  62. String value = source.readString();
  63. map.put(key, value);
  64. }
  65. return map;
  66. }
  67.  
  68. public static <T extends Parcelable> void write(Parcel dest,
  69. Map<String, T> objects, int flags) {
  70. if (objects == null) {
  71. dest.writeInt(-1);
  72. } else {
  73. dest.writeInt(objects.keySet().size());
  74. for (String key : objects.keySet()) {
  75. dest.writeString(key);
  76. dest.writeParcelable(objects.get(key), flags);
  77. }
  78. }
  79. }
  80.  
  81. public static <T extends Parcelable> Map<String, T> readParcelableMap(
  82. Parcel source) {
  83. int numKeys = source.readInt();
  84. if (numKeys == -1) {
  85. return null;
  86. }
  87. HashMap<String, T> map = new HashMap<String, T>();
  88. for (int i = 0; i < numKeys; i++) {
  89. String key = source.readString();
  90. T value = source.readParcelable(null);
  91. map.put(key, value);
  92. }
  93. return map;
  94. }
  95.  
  96. public static void write(Parcel dest, URL url) {
  97. dest.writeString(url.toExternalForm());
  98. }
  99.  
  100. public static URL readURL(Parcel source) {
  101. try {
  102. return new URL(source.readString());
  103. } catch (MalformedURLException e) {
  104. e.printStackTrace();
  105. }
  106. return null;
  107. }
  108.  
  109. public static void write(Parcel dest, Date date) {
  110. dest.writeByte((byte) (date == null ? 0 : 1));
  111. if (date != null) {
  112. dest.writeLong(date.getTime());
  113. }
  114. }
  115.  
  116. public static Date readDate(Parcel source) {
  117. if (source.readByte() == 1) {
  118. return new Date(source.readLong());
  119. }
  120. return null;
  121. }
  122.  
  123. public static <T extends Enum<T>> void write(Parcel dest, Enum<T> enu) {
  124. if (enu == null) {
  125. dest.writeString("");
  126. } else {
  127. dest.writeString(enu.name());
  128. }
  129. }
  130.  
  131. public static <T extends Enum<T>> T readEnum(Parcel dest, Class<T> clazz) {
  132. String name = dest.readString();
  133. if ("".equals(name)) {
  134. return null;
  135. }
  136. return Enum.valueOf(clazz, name);
  137. }
  138.  
  139. public static void write(Parcel dest, boolean bool) {
  140. dest.writeByte((byte) (bool ? 1 : 0));
  141. }
  142.  
  143. public static boolean readBoolean(Parcel source) {
  144. return source.readByte() == 1;
  145. }
  146.  
  147. public static <T extends Parcelable> void write(Parcel dest,
  148. List<T> fields, int flags) {
  149. if (fields == null) {
  150. dest.writeInt(-1);
  151. } else {
  152. dest.writeInt(fields.size());
  153. for (T field : fields) {
  154. dest.writeParcelable(field, flags);
  155. }
  156. }
  157. }
  158.  
  159. @SuppressWarnings("unchecked")
  160. public static <T extends Parcelable> List<T> readParcelableList(
  161. Parcel source) {
  162. int size = source.readInt();
  163. if (size == -1) {
  164. return null;
  165. }
  166. ArrayList<T> list = new ArrayList<T>();
  167. for (int i = 0; i < size; i++) {
  168. list.add((T) source.readParcelable(null));
  169. }
  170. return list;
  171. }
  172. }
Add Comment
Please, Sign In to add comment