Skip to content

Commit cc8101c

Browse files
committed
Implementing Parcelable in ParseGeoPoint
1 parent 46a0233 commit cc8101c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Parse/src/main/java/com/parse/ParseGeoPoint.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import android.location.Criteria;
1212
import android.location.Location;
13+
import android.os.Parcel;
14+
import android.os.Parcelable;
1315

1416
import java.util.Locale;
1517

@@ -32,7 +34,7 @@
3234
* </pre>
3335
*/
3436

35-
public class ParseGeoPoint {
37+
public class ParseGeoPoint implements Parcelable {
3638
static double EARTH_MEAN_RADIUS_KM = 6371.0;
3739
static double EARTH_MEAN_RADIUS_MILE = 3958.8;
3840

@@ -268,4 +270,27 @@ public static void getCurrentLocationInBackground(long timeout, Criteria criteri
268270
public String toString() {
269271
return String.format(Locale.US, "ParseGeoPoint[%.6f,%.6f]", latitude, longitude);
270272
}
273+
274+
@Override
275+
public int describeContents() {
276+
return 0;
277+
}
278+
279+
@Override
280+
public void writeToParcel(Parcel dest, int flags) {
281+
dest.writeDouble(latitude);
282+
dest.writeDouble(longitude);
283+
}
284+
285+
public final static Creator<ParseGeoPoint> CREATOR = new Creator<ParseGeoPoint>() {
286+
@Override
287+
public ParseGeoPoint createFromParcel(Parcel source) {
288+
return new ParseGeoPoint(source.readDouble(), source.readDouble());
289+
}
290+
291+
@Override
292+
public ParseGeoPoint[] newArray(int size) {
293+
return new ParseGeoPoint[size];
294+
}
295+
};
271296
}

Parse/src/test/java/com/parse/ParseGeoPointTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
*/
99
package com.parse;
1010

11+
import android.os.Parcel;
12+
1113
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.robolectric.RobolectricTestRunner;
16+
import org.robolectric.annotation.Config;
1217

1318
import static org.junit.Assert.*;
1419

20+
@RunWith(RobolectricTestRunner.class)
21+
@Config(constants = BuildConfig.class, sdk = 23)
1522
public class ParseGeoPointTest {
1623

1724
@Test
@@ -30,4 +37,15 @@ public void testConstructors() {
3037
assertEquals(lat, copy.getLatitude(), 0);
3138
assertEquals(lng, copy.getLongitude(), 0);
3239
}
40+
41+
@Test
42+
public void testParcelable() {
43+
ParseGeoPoint point = new ParseGeoPoint(30d, 50d);
44+
Parcel parcel = Parcel.obtain();
45+
point.writeToParcel(parcel, 0);
46+
parcel.setDataPosition(0);
47+
point = ParseGeoPoint.CREATOR.createFromParcel(parcel);
48+
assertEquals(point.getLatitude(), 30d, 0);
49+
assertEquals(point.getLongitude(), 50d, 0);
50+
}
3351
}

0 commit comments

Comments
 (0)