Skip to content

Commit 4e2f195

Browse files
Merge pull request #1382 from smartdevicelink/bugfix/issue_1377_Parcelable
Bugfix/issue 1377 parcelable
2 parents dcbb008 + 354293b commit 4e2f195

12 files changed

Lines changed: 351 additions & 153 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.smartdevicelink.protocol;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
import com.smartdevicelink.transport.utl.TransportRecord;
7+
import com.smartdevicelink.util.DebugTool;
8+
9+
public class SdlPacket extends BaseSdlPacket implements Parcelable {
10+
private static final int EXTRA_PARCEL_DATA_LENGTH = 24;
11+
12+
public SdlPacket(int version, boolean encryption, int frameType,
13+
int serviceType, int frameInfo, int sessionId,
14+
int dataSize, int messageId, byte[] payload) {
15+
super(version, encryption, frameType, serviceType, frameInfo, sessionId, dataSize, messageId, payload);
16+
}
17+
18+
public SdlPacket(int version, boolean encryption, int frameType,
19+
int serviceType, int frameInfo, int sessionId,
20+
int dataSize, int messageId, byte[] payload, int offset, int bytesToWrite) {
21+
super(version, encryption, frameType, serviceType, frameInfo, sessionId, dataSize, messageId, payload, offset, bytesToWrite);
22+
}
23+
24+
protected SdlPacket() {
25+
super();
26+
}
27+
28+
protected SdlPacket(BaseSdlPacket packet) {
29+
super(packet);
30+
}
31+
32+
/* ***************************************************************************************************************************************************
33+
* *********************************************************** Parceable Overrides *****************************************************************
34+
*****************************************************************************************************************************************************/
35+
36+
37+
38+
//I think this is FIFO...right?
39+
public SdlPacket(Parcel p) {
40+
this.version = p.readInt();
41+
this.encryption = (p.readInt() == 0) ? false : true;
42+
this.frameType = p.readInt();
43+
this.serviceType = p.readInt();
44+
this.frameInfo = p.readInt();
45+
this.sessionId = p.readInt();
46+
this.dataSize = p.readInt();
47+
this.messageId = p.readInt();
48+
if(p.readInt() == 1){ //We should have a payload attached
49+
payload = new byte[dataSize];
50+
p.readByteArray(payload);
51+
}
52+
53+
this.priorityCoefficient = p.readInt();
54+
55+
if(p.dataAvail() > EXTRA_PARCEL_DATA_LENGTH) { //See note on constant for why not 0
56+
try {
57+
messagingVersion = p.readInt();
58+
if (messagingVersion >= 2) {
59+
if (p.readInt() == 1) { //We should have a transport type attached
60+
this.transportRecord = (TransportRecord) p.readParcelable(TransportRecord.class.getClassLoader());
61+
}
62+
}
63+
}catch (RuntimeException e){
64+
DebugTool.logError("Error creating packet from parcel", e);
65+
}
66+
}
67+
}
68+
69+
70+
@Override
71+
public int describeContents() {
72+
return 0;
73+
}
74+
75+
@Override
76+
public void writeToParcel(Parcel dest, int flags) {
77+
78+
dest.writeInt(version);
79+
dest.writeInt(encryption? 1 : 0);
80+
dest.writeInt(frameType);
81+
dest.writeInt(serviceType);
82+
dest.writeInt(frameInfo);
83+
dest.writeInt(sessionId);
84+
dest.writeInt(dataSize);
85+
dest.writeInt(messageId);
86+
dest.writeInt(payload!=null? 1 : 0);
87+
if(payload!=null){
88+
dest.writeByteArray(payload);
89+
}
90+
dest.writeInt(priorityCoefficient);
91+
92+
///Additions after initial creation
93+
if(messagingVersion > 1){
94+
dest.writeInt(messagingVersion);
95+
96+
dest.writeInt(transportRecord!=null? 1 : 0);
97+
if(transportRecord != null){
98+
dest.writeParcelable(transportRecord,0);
99+
}
100+
}
101+
102+
}
103+
104+
public static final Parcelable.Creator<SdlPacket> CREATOR = new Parcelable.Creator<SdlPacket>() {
105+
public SdlPacket createFromParcel(Parcel in) {
106+
return new SdlPacket(in);
107+
}
108+
109+
@Override
110+
public SdlPacket[] newArray(int size) {
111+
return new SdlPacket[size];
112+
}
113+
114+
};
115+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.smartdevicelink.transport.utl;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
import com.smartdevicelink.transport.enums.TransportType;
7+
8+
public class TransportRecord extends BaseTransportRecord implements Parcelable {
9+
10+
public TransportRecord(TransportType transportType, String address) {
11+
super(transportType, address);
12+
}
13+
14+
public TransportRecord(Parcel p) {
15+
if (p.readInt() == 1) { //We should have a transport type attached
16+
String transportName = p.readString();
17+
if(transportName != null){
18+
this.type = TransportType.valueOf(transportName);
19+
}
20+
}
21+
22+
if (p.readInt() == 1) { //We should have a transport address attached
23+
address = p.readString();
24+
}
25+
}
26+
27+
@Override
28+
public int describeContents() {
29+
return 0;
30+
}
31+
32+
@Override
33+
public void writeToParcel(Parcel dest, int flags) {
34+
dest.writeInt(type!=null? 1 : 0);
35+
if(type != null){
36+
dest.writeString(type.name());
37+
}
38+
39+
dest.writeInt(address !=null? 1 : 0);
40+
if(address != null){
41+
dest.writeString(address);
42+
}
43+
}
44+
45+
public static final Parcelable.Creator<TransportRecord> CREATOR = new Parcelable.Creator<TransportRecord>() {
46+
public TransportRecord createFromParcel(Parcel in) {
47+
return new TransportRecord(in);
48+
}
49+
50+
@Override
51+
public TransportRecord[] newArray(int size) {
52+
return new TransportRecord[size];
53+
}
54+
55+
};
56+
}

base/src/main/java/android/os/Parcel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package android.os;
2020

21+
@Deprecated
2122
public class Parcel {
2223

2324
public void writeInt(int data){

base/src/main/java/android/os/Parcelable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package android.os;
1919

20+
@Deprecated
2021
public interface Parcelable {
2122

2223
int describeContents();

base/src/main/java/com/smartdevicelink/protocol/SdlPacket.java renamed to base/src/main/java/com/smartdevicelink/protocol/BaseSdlPacket.java

Lines changed: 9 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@
3131
*/
3232
package com.smartdevicelink.protocol;
3333

34-
import android.os.Parcel;
35-
import android.os.Parcelable;
36-
3734
import com.livio.BSON.BsonEncoder;
3835
import com.smartdevicelink.protocol.enums.FrameType;
3936
import com.smartdevicelink.transport.utl.TransportRecord;
40-
import com.smartdevicelink.util.DebugTool;
4137

4238
import java.nio.ByteBuffer;
4339
import java.util.HashMap;
@@ -47,7 +43,7 @@
4743
* Any other binder transactions must include an additional int flag into their bundle or the parsing
4844
* of this object will fail.
4945
*/
50-
public class SdlPacket implements Parcelable{
46+
class BaseSdlPacket {
5147

5248
/**
5349
* This is the amount of bytes added to the bundle from the router service for a specific int
@@ -122,9 +118,9 @@ public class SdlPacket implements Parcelable{
122118
int messagingVersion = 1;
123119
TransportRecord transportRecord;
124120

125-
public SdlPacket(int version, boolean encryption, int frameType,
126-
int serviceType, int frameInfo, int sessionId,
127-
int dataSize, int messageId, byte[] payload) {
121+
BaseSdlPacket(int version, boolean encryption, int frameType,
122+
int serviceType, int frameInfo, int sessionId,
123+
int dataSize, int messageId, byte[] payload) {
128124
this.version = version;
129125
this.encryption = encryption;
130126
this.frameType = frameType;
@@ -140,9 +136,9 @@ public SdlPacket(int version, boolean encryption, int frameType,
140136
}
141137
}
142138

143-
public SdlPacket(int version, boolean encryption, int frameType,
144-
int serviceType, int frameInfo, int sessionId,
145-
int dataSize, int messageId, byte[] payload, int offset,int bytesToWrite) {
139+
BaseSdlPacket(int version, boolean encryption, int frameType,
140+
int serviceType, int frameInfo, int sessionId,
141+
int dataSize, int messageId, byte[] payload, int offset, int bytesToWrite) {
146142
this.version = version;
147143
this.encryption = encryption;
148144
this.frameType = frameType;
@@ -166,7 +162,7 @@ public SdlPacket(int version, boolean encryption, int frameType,
166162
* <p>Frame Info
167163
* <p>
168164
*/
169-
protected SdlPacket(){
165+
protected BaseSdlPacket(){
170166
//Package only empty constructor
171167
this.version = 1;
172168
this.encryption = false;
@@ -183,7 +179,7 @@ protected SdlPacket(){
183179
* Creates a new packet based on previous packet definitions. Will not copy payload.
184180
* @param packet an instance of the packet that should be copied.
185181
*/
186-
protected SdlPacket(SdlPacket packet){
182+
protected BaseSdlPacket(BaseSdlPacket packet){
187183
this.version = packet.version;
188184
this.encryption = packet.encryption;
189185
this.frameType = packet.frameType;
@@ -359,92 +355,6 @@ public String toString() {
359355
public void setMessagingVersion(int version){
360356
this.messagingVersion = version;
361357
}
362-
363-
364-
365-
/* ***************************************************************************************************************************************************
366-
* *********************************************************** Parceable Overrides *****************************************************************
367-
*****************************************************************************************************************************************************/
368-
369-
370-
371-
//I think this is FIFO...right?
372-
public SdlPacket(Parcel p) {
373-
this.version = p.readInt();
374-
this.encryption = (p.readInt() == 0) ? false : true;
375-
this.frameType = p.readInt();
376-
this.serviceType = p.readInt();
377-
this.frameInfo = p.readInt();
378-
this.sessionId = p.readInt();
379-
this.dataSize = p.readInt();
380-
this.messageId = p.readInt();
381-
if(p.readInt() == 1){ //We should have a payload attached
382-
payload = new byte[dataSize];
383-
p.readByteArray(payload);
384-
}
385-
386-
this.priorityCoefficient = p.readInt();
387-
388-
if(p.dataAvail() > EXTRA_PARCEL_DATA_LENGTH) { //See note on constant for why not 0
389-
try {
390-
messagingVersion = p.readInt();
391-
if (messagingVersion >= 2) {
392-
if (p.readInt() == 1) { //We should have a transport type attached
393-
this.transportRecord = (TransportRecord) p.readParcelable(TransportRecord.class.getClassLoader());
394-
}
395-
}
396-
}catch (RuntimeException e){
397-
DebugTool.logError("Error creating packet from parcel", e);
398-
}
399-
}
400-
}
401-
402-
403-
@Override
404-
public int describeContents() {
405-
return 0;
406-
}
407-
408-
@Override
409-
public void writeToParcel(Parcel dest, int flags) {
410-
411-
dest.writeInt(version);
412-
dest.writeInt(encryption? 1 : 0);
413-
dest.writeInt(frameType);
414-
dest.writeInt(serviceType);
415-
dest.writeInt(frameInfo);
416-
dest.writeInt(sessionId);
417-
dest.writeInt(dataSize);
418-
dest.writeInt(messageId);
419-
dest.writeInt(payload!=null? 1 : 0);
420-
if(payload!=null){
421-
dest.writeByteArray(payload);
422-
}
423-
dest.writeInt(priorityCoefficient);
424-
425-
///Additions after initial creation
426-
if(messagingVersion > 1){
427-
dest.writeInt(messagingVersion);
428-
429-
dest.writeInt(transportRecord!=null? 1 : 0);
430-
if(transportRecord != null){
431-
dest.writeParcelable(transportRecord,0);
432-
}
433-
}
434-
435-
}
436-
437-
public static final Parcelable.Creator<SdlPacket> CREATOR = new Parcelable.Creator<SdlPacket>() {
438-
public SdlPacket createFromParcel(Parcel in) {
439-
return new SdlPacket(in);
440-
}
441-
442-
@Override
443-
public SdlPacket[] newArray(int size) {
444-
return new SdlPacket[size];
445-
}
446-
447-
};
448358

449359
public void putTag(String tag, Object data){
450360
if(bsonPayload == null){

0 commit comments

Comments
 (0)