· 7 years ago · Nov 02, 2018, 05:02 PM
1The Parcelable class is a subclass of parent class Message, see below:
2
3package x.y.z.InboxManager;
4
5import android.os.Parcel;
6import android.os.Parcelable;
7import android.support.annotation.NonNull;
8
9import java.util.Objects;
10
11import x.y.z.Application.XXXApp;
12
13public class Message implements Parcelable{
14
15 private static String TAG = "Message";
16 //client_id (same as wam_id) - String/text – Id of the JAMS unit
17 private String wam_id= null;
18 //severity level (Optional) currently not used
19 private String level = null;
20 //message - String/text – if topic = “EMERGENCY†then value {Alias possibly having life threating emergency!} else nothing or user provided
21 private String message = null;
22 private String image_id = null;
23 //topic - String/text – “EMERGENCY†OR "ALERT"
24 private String topic = null;
25 // time_of_arrival_in_milli_secs_from_epoch -Long/numerical- time of message arrival in milliseconds from epoch based receivers device time
26 private long timeofArrivalInNanoSecsFromEpoch = 0L;
27 //viewed - int/numerical - 0 or 1
28 private int viewed = 0;
29 //image_data - byte/blob - binary data of the image (all images are assumed to be PNG)
30 private byte [] imageData = null;
31 //client alias - String/text – Alias or friendly name associated with clientId
32 private String clientAlias;
33 //sql table related attributes
34 public static String TABLE_NAME="ALERT_MESSAGE";
35 //COLUMN_NAME_TIME_OF_ARRIVAL will be the primary key
36 public static String COLUMN_NAME_TIME_OF_ARRIVAL="TIME_OF_ARRIVAL";
37 public static String COLUMN_NAME_WAM_ID="WAM_ID";
38 public static String COLUMN_NAME_LEVEL="LEVEL";
39 public static String COLUMN_NAME_MESSAGE="MESSAGE";
40 public static String COLUMN_NAME_IMAGE_ID="IMAGE_ID";
41 public static String COLUMN_NAME_TOPIC="TOPIC";
42 public static String COLUMN_NAME_VIEWED="VIEWED";
43 public static String COLUMN__NAME_IMAGE_DATA="IMAGE_DATA";
44 //SQL
45 public static final String SQL_CREATE_ENTRIES =
46 "create table " + Message.TABLE_NAME + " ( "+ Message.COLUMN_NAME_TIME_OF_ARRIVAL + " INTEGER PRIMARY KEY," +
47 Message.COLUMN_NAME_WAM_ID + " TEXT," +
48 Message.COLUMN_NAME_LEVEL + " TEXT," +
49 Message.COLUMN_NAME_MESSAGE + " TEXT," +
50 Message.COLUMN_NAME_IMAGE_ID + " TEXT," +
51 Message.COLUMN_NAME_TOPIC + " TEXT,"+
52 Message.COLUMN_NAME_VIEWED + " BOOLEAN NOT NULL CHECK ( "+ Message.COLUMN_NAME_VIEWED+" IN (0,1))," +
53 Message.COLUMN__NAME_IMAGE_DATA + " BLOB)";
54
55 public static final String SQL_DROP_TABLE_ENTRIES = "DROP TABLE IF EXISTS "+ Message.TABLE_NAME;
56 private GetImagDataFromUrlTask getImageTask = null;
57
58 public void downloadImage()
59 {
60 getImageTask = new GetImagDataFromUrlTask(XXXApp.getContext());
61 getImageTask.execute(this);
62 }
63
64
65 public void cancelImageDownload()
66 {
67 if (getImageTask!=null) {
68 getImageTask.cancel(true);
69 }
70 }
71
72
73 public Message() {
74
75 }
76
77 public Message(String wam_id, String level, String message, String image_id, long timeofArrivalInNanoSecsFromEpoch) {
78 this.wam_id = wam_id;
79 this.level = level;
80 this.message = message;
81 this.image_id = image_id;
82 this. timeofArrivalInNanoSecsFromEpoch = timeofArrivalInNanoSecsFromEpoch;
83 }
84
85 public Message(Parcel in)
86 {
87 this.wam_id = in.readString();
88 this.level = in.readString();
89 this.message = in.readString();
90 this.image_id = in.readString();
91 this.timeofArrivalInNanoSecsFromEpoch = in.readLong();
92 if (this.imageData!=null) {
93 in.readByteArray(this.imageData);
94 }
95 }
96
97 public String getWam_id() {
98 return wam_id;
99 }
100
101 public void setWam_id(String wam_id) {
102 this.wam_id = wam_id;
103 }
104
105 public String getLevel() {
106 return level;
107 }
108
109 public void setLevel(String level) {
110 this.level = level;
111 }
112
113 public String getMessage() {
114 return message;
115 }
116
117 public void setMessage(String message) {
118 this.message = message;
119 }
120
121 public String getImage_id() {
122 return image_id;
123 }
124
125 public void setImage_id(String image_id) {
126 this.image_id = image_id;
127 }
128
129 public long getTimeofArrivalInMilliSecsFromEpoch() {
130 return timeofArrivalInNanoSecsFromEpoch;
131 }
132
133 public void setTimeofArrivalInMilliSecsFromEpoch(long timeofArrivalInNanoSecsFromEpoch) {
134 this.timeofArrivalInNanoSecsFromEpoch = timeofArrivalInNanoSecsFromEpoch;
135 }
136
137 public String getTopic() {
138 return topic;
139 }
140
141 public void setTopic(String topic) {
142 this.topic = topic;
143 }
144
145 public int getViewed() {
146 return viewed;
147 }
148
149 public void setViewed(int viewed) {
150 this.viewed = viewed;
151 }
152
153 public byte[] getImageData() {
154 return imageData;
155 }
156
157 public void setImageData(byte[] imageData) {
158 this.imageData = imageData;
159 }
160
161
162 @Override
163 public int describeContents() {
164 return 0;
165 }
166
167 @Override
168 public void writeToParcel(Parcel dest, int flags) {
169 dest.writeString(this.wam_id);
170 dest.writeString(this.level);
171 dest.writeString(this.message);
172 dest.writeString(this.image_id);
173 dest.writeLong(this.timeofArrivalInNanoSecsFromEpoch);
174 dest.writeByteArray(imageData);
175 }
176
177 public static final Parcelable.Creator<Message> CREATOR= new Parcelable.Creator<Message>() {
178
179 @Override
180 public Message createFromParcel(Parcel source) {
181// TODO Auto-generated method stub
182 return new Message(source); //using parcelable constructor
183 }
184
185 @Override
186 public Message[] newArray(int size) {
187 return new Message[size];
188 }
189 };
190
191
192
193 @Override
194 public boolean equals(Object o) {
195 if (this == o) return true;
196 if (o == null || getClass() != o.getClass()) return false;
197 Message that = (Message) o;
198
199 return timeofArrivalInNanoSecsFromEpoch == that.timeofArrivalInNanoSecsFromEpoch;
200 //TODO: update equivalency
201 /*if (message!=null) {
202 return timeofArrivalInNanoSecsFromEpoch == that.timeofArrivalInNanoSecsFromEpoch && message.equalsIgnoreCase(that.message);
203 }
204 else
205 {
206 return timeofArrivalInNanoSecsFromEpoch == that.timeofArrivalInNanoSecsFromEpoch && wam_id.equalsIgnoreCase(that.wam_id);
207 }*/
208 }
209
210 @Override
211 public String toString() {
212 return "Message{" +
213 "wam_id='" + wam_id + ''' +
214 ", level='" + level + ''' +
215 ", message='" + message + ''' +
216 ", image_id='" + image_id + ''' +
217 ", topic='" + topic + ''' +
218 ", timeofArrivalInMillisecs=" + timeofArrivalInNanoSecsFromEpoch +
219 ", viewed=" + viewed +
220 '}';
221 }
222
223 @Override
224 public int hashCode() {
225
226 return Objects.hash(timeofArrivalInNanoSecsFromEpoch);
227 }
228
229
230}
231
232in.readList(extras,Byte[].class.getClassLoader());
233in.readList(extrasFormat,Formats.ExtrasFormat.class.getClassLoader());
234
235package x.y.z.InboxManager;
236
237import android.os.Parcel;
238import android.os.Parcelable;
239
240import java.util.ArrayList;
241import java.util.List;
242
243public class Emergency extends Message implements Parcelable {
244
245
246 private String clientAlias;
247 private String topic;
248 private Long timeOfPublish;
249 private Double lat;
250 private Double lon;
251 private List<Byte[]> extras = new ArrayList<Byte[]>();
252 private List<Formats.ExtrasFormat> extrasFormat = new ArrayList<Formats.ExtrasFormat>();
253
254
255 public Emergency()
256 {
257
258 }
259
260
261 public Emergency(Parcel in)
262 {
263 //super(in.readString(),in.readString(),in.readString(),in.readString(),in.readLong());
264 super(in);
265 this.clientAlias = in.readString();
266 this.topic = in.readString();
267 this.timeOfPublish = in.readLong();
268 this.lat = in.readDouble();
269 this.lon = in.readDouble();
270 super.setViewed(in.readInt());
271
272 in.readList(extras,Byte[].class.getClassLoader());
273 in.readList(extrasFormat,Formats.ExtrasFormat.class.getClassLoader());
274
275 }
276
277
278 public Emergency(String clientId,String level,String message,String imageId,Long timeOfArrival,String clientAlias, String topic,Long timeOfPublish,Double lat, Double lon,int viewed,List<Byte[]> extras,List<Formats.ExtrasFormat> extrasFormats)
279 {
280 super(clientId,level,message,imageId,timeOfArrival);
281 super.setViewed(viewed);
282 this.clientAlias = clientAlias;
283 this.topic = topic;
284 this.timeOfPublish = timeOfPublish;
285 this.lat = lat;
286 this.lon = lon;
287 this.extras = extras;
288 this.extrasFormat = extrasFormats;
289
290 }
291
292 public String getClientAlias() {
293 return clientAlias;
294 }
295
296 @Override
297 public String getTopic() {
298 return topic;
299 }
300
301 public Long getTimeOfPublish() {
302 return timeOfPublish;
303 }
304
305 public Double getLat() {
306 return lat;
307 }
308
309 public Double getLon() {
310 return lon;
311 }
312
313 public List<Byte[]> getExtras() {
314 return extras;
315 }
316
317 public List<Formats.ExtrasFormat> getExtrasFormat() {
318 return extrasFormat;
319 }
320
321 @Override
322 public int describeContents() {
323 return 0;
324 }
325
326 @Override
327 public void writeToParcel(Parcel dest, int flags) {
328
329 super.writeToParcel(dest,flags);
330 dest.writeString(this.clientAlias);
331 dest.writeString(this.topic);
332 dest.writeLong( this.timeOfPublish);
333 dest.writeDouble(this.lat);
334 dest.writeDouble(this.lon);
335 dest.writeInt(this.getViewed());
336
337 dest.writeList(extras);
338 dest.writeList(extrasFormat);
339 }
340
341 public static final Parcelable.Creator<Emergency> CREATOR= new Parcelable.Creator<Emergency>() {
342
343 @Override
344 public Emergency createFromParcel(Parcel source) {
345// TODO Auto-generated method stub
346 return new Emergency(source); //using parcelable constructor
347 }
348
349 @Override
350 public Emergency[] newArray(int size) {
351 return new Emergency[size];
352 }
353 };
354
355
356}
357
358Process: x.y.z, PID: 14292
359java.lang.RuntimeException: Unable to start service x.y.z.services.MqttBrokerService@3a0f1bd with Intent { act=x.y.z.action.EMERGENCY_MESSAGE cmp=x.y.z/.services.MqttBrokerService (has extras) }: java.lang.RuntimeException: Parcel android.os.Parcel@748514: Unmarshalling unknown type code 6357111 at offset 408
360 at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3347)
361 at android.app.ActivityThread.-wrap21(ActivityThread.java)
362 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1586)
363 at android.os.Handler.dispatchMessage(Handler.java:102)
364 at android.os.Looper.loop(Looper.java:154)
365 at android.app.ActivityThread.main(ActivityThread.java:6145)
366 at java.lang.reflect.Method.invoke(Native Method)
367 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
368 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
369 Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@748514: Unmarshalling unknown type code 6357111 at offset 408
370 at android.os.Parcel.readValue(Parcel.java:2444)
371 at android.os.Parcel.readListInternal(Parcel.java:2793)
372 at android.os.Parcel.readList(Parcel.java:1836)
373 at x.y.z.InboxManager.Emergency.<init>(Emergency.java:65)
374 at x.y.z.InboxManager.Emergency$1.createFromParcel(Emergency.java:139)
375 at x.y.z.InboxManager.Emergency$1.createFromParcel(Emergency.java:134)
376
377public Message(Parcel in)
378{
379 ...
380 if (this.imageData!=null) {
381 in.readByteArray(this.imageData);
382 }
383}
384
385@Override
386public void writeToParcel(Parcel dest, int flags) {
387 ...
388 dest.writeByteArray(imageData);
389}
390
391public Message(Parcel in)
392{
393 ...
394 this.imageData = in.createByteArray();
395}
396
397public Emergency(Parcel in)
398{
399 ...
400 in.readList(extrasFormat,Formats.ExtrasFormat.class.getClassLoader());
401}
402
403@Override
404public void writeToParcel(Parcel dest, int flags) {
405 ...
406 dest.writeList(extrasFormat);
407}