Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - manually write and read camera's padding array to parcel
Browse files Browse the repository at this point in the history
This avoids relying on Parcel's util methods that do not handle null arrays gracefully.
  • Loading branch information
Łukasz Paczos committed Oct 10, 2019
1 parent e909997 commit 3e06965
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ public CameraPosition createFromParcel(Parcel in) {
LatLng target = in.readParcelable(LatLng.class.getClassLoader());
double tilt = in.readDouble();
double zoom = in.readDouble();
double[] padding = new double[4];
in.readDoubleArray(padding);

double[] padding = null;
int paddingSize = in.readInt();
if (paddingSize > 0) {
padding = new double[paddingSize];
for (int i = 0; i < paddingSize; i++) {
padding[i] = in.readDouble();
}
}

return new CameraPosition(target, zoom, tilt, bearing, padding);
}

Expand Down Expand Up @@ -139,7 +147,16 @@ public void writeToParcel(Parcel out, int flags) {
out.writeParcelable(target, flags);
out.writeDouble(tilt);
out.writeDouble(zoom);
out.writeDoubleArray(padding);

if (padding != null) {
int length = padding.length;
out.writeInt(length);
for (double v : padding) {
out.writeDouble(v);
}
} else {
out.writeInt(-1);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,15 @@ public void testParcelable() {
CameraPosition cameraPosition2 = CameraPosition.CREATOR.createFromParcel(parcel);
assertEquals("Parcel should match original object", cameraPosition1, cameraPosition2);
}

@Test
public void testParcelableNulls() {
CameraPosition cameraPosition1 = new CameraPosition(null, 3, 4, 5, null);
Parcel parcel = Parcel.obtain();
cameraPosition1.writeToParcel(parcel, 0);
parcel.setDataPosition(0);

CameraPosition cameraPosition2 = CameraPosition.CREATOR.createFromParcel(parcel);
assertEquals("Parcel should match original object", cameraPosition1, cameraPosition2);
}
}

0 comments on commit 3e06965

Please sign in to comment.