-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
ParcelUtils.java
176 lines (156 loc) · 4.39 KB
/
ParcelUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cn.trinea.android.common.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import android.os.Parcel;
import android.os.Parcelable;
/**
* ParcelUtils
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-27
*/
public class ParcelUtils {
private ParcelUtils() {
throw new AssertionError();
}
/**
* read boolean
*
* @param in
* @return
*/
public static boolean readBoolean(Parcel in) {
return in.readInt() == 1;
}
/**
* write boolean
*
* @param b
* @param out
*/
public static void writeBoolean(boolean b, Parcel out) {
out.writeInt(b ? 1 : 0);
}
/**
* Read a HashMap from a Parcel, class of key and value are both String
*
* @param in
* @return
*/
public static Map<String, String> readHashMapStringAndString(Parcel in) {
if (in == null) {
return null;
}
int size = in.readInt();
if (size == -1) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < size; i++) {
String key = in.readString();
map.put(key, in.readString());
}
return map;
}
/**
* Write a HashMap to a Parcel, class of key and value are both String
*
* @param map
* @param out
* @param flags
*/
public static void writeHashMapStringAndString(Map<String, String> map, Parcel out, int flags) {
if (map != null) {
out.writeInt(map.size());
for (Entry<String, String> entry : map.entrySet()) {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
} else {
out.writeInt(-1);
}
}
/**
* Read a HashMap from a Parcel, class of key is String, class of Value can parcelable
*
* @param <V>
* @param in
* @param loader
* @return
*/
@SuppressWarnings("unchecked")
public static <V extends Parcelable> Map<String, V> readHashMapStringKey(Parcel in, ClassLoader loader) {
if (in == null) {
return null;
}
int size = in.readInt();
if (size == -1) {
return null;
}
Map<String, V> map = new HashMap<String, V>();
for (int i = 0; i < size; i++) {
String key = in.readString();
map.put(key, (V)in.readParcelable(loader));
}
return map;
}
/**
* Write a HashMap to a Parcel, class of key is String, class of Value can parcelable
*
* @param map
* @param out
* @param flags
*/
public static <V extends Parcelable> void writeHashMapStringKey(Map<String, V> map, Parcel out, int flags) {
if (map != null) {
out.writeInt(map.size());
for (Entry<String, V> entry : map.entrySet()) {
out.writeString(entry.getKey());
out.writeParcelable(entry.getValue(), flags);
}
} else {
out.writeInt(-1);
}
}
/**
* Read a HashMap from a Parcel, class of key and value can parcelable both
*
* @param <V>
* @param in
* @param loader
* @return
*/
@SuppressWarnings("unchecked")
public static <K extends Parcelable, V extends Parcelable> Map<K, V> readHashMap(Parcel in, ClassLoader loader) {
if (in == null) {
return null;
}
int size = in.readInt();
if (size == -1) {
return null;
}
Map<K, V> map = new HashMap<K, V>();
for (int i = 0; i < size; i++) {
map.put((K)in.readParcelable(loader), (V)in.readParcelable(loader));
}
return map;
}
/**
* Write a HashMap to a Parcel, class of key and value can parcelable both
*
* @param map
* @param out
* @param flags
*/
public static <K extends Parcelable, V extends Parcelable> void writeHashMap(Map<K, V> map, Parcel out, int flags) {
if (map != null) {
out.writeInt(map.size());
for (Entry<K, V> entry : map.entrySet()) {
out.writeParcelable(entry.getKey(), flags);
out.writeParcelable(entry.getValue(), flags);
}
} else {
out.writeInt(-1);
}
}
}