Skip to content

Commit 6223d06

Browse files
authored
Merge pull request #619 from natario1/parcelable-point
Implementing Parcelable in ParseGeoPoint
2 parents 46a0233 + 9ac8589 commit 6223d06

File tree

6 files changed

+86
-5
lines changed

6 files changed

+86
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ protected ParseFile(Parcel source) {
240240
* Creates a new file instance from a {@link Parcel} using the given {@link ParseParcelDecoder}.
241241
* The decoder is currently unused, but it might be in the future, plus this is the pattern we
242242
* are using in parcelable classes.
243+
*
243244
* @param source the parcel
244245
* @param decoder the decoder
245246
*/

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

Lines changed: 54 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

@@ -68,6 +70,30 @@ public ParseGeoPoint(ParseGeoPoint point) {
6870
this(point.getLatitude(), point.getLongitude());
6971
}
7072

73+
/**
74+
* Creates a new point instance from a {@link Parcel} source. This is used when unparceling a
75+
* ParseGeoPoint. Subclasses that need Parcelable behavior should provide their own
76+
* {@link android.os.Parcelable.Creator} and override this constructor.
77+
*
78+
* @param source The recovered parcel.
79+
*/
80+
protected ParseGeoPoint(Parcel source) {
81+
this(source, ParseParcelDecoder.get());
82+
}
83+
84+
/**
85+
* Creates a new point instance from a {@link Parcel} using the given {@link ParseParcelDecoder}.
86+
* The decoder is currently unused, but it might be in the future, plus this is the pattern we
87+
* are using in parcelable classes.
88+
*
89+
* @param source the parcel
90+
* @param decoder the decoder
91+
*/
92+
ParseGeoPoint(Parcel source, ParseParcelDecoder decoder) {
93+
setLatitude(source.readDouble());
94+
setLongitude(source.readDouble());
95+
}
96+
7197
/**
7298
* Set latitude. Valid range is (-90.0, 90.0). Extremes should not be used.
7399
*
@@ -268,4 +294,31 @@ public static void getCurrentLocationInBackground(long timeout, Criteria criteri
268294
public String toString() {
269295
return String.format(Locale.US, "ParseGeoPoint[%.6f,%.6f]", latitude, longitude);
270296
}
297+
298+
@Override
299+
public int describeContents() {
300+
return 0;
301+
}
302+
303+
@Override
304+
public void writeToParcel(Parcel dest, int flags) {
305+
writeToParcel(dest, ParseParcelEncoder.get());
306+
}
307+
308+
void writeToParcel(Parcel dest, ParseParcelEncoder encoder) {
309+
dest.writeDouble(latitude);
310+
dest.writeDouble(longitude);
311+
}
312+
313+
public final static Creator<ParseGeoPoint> CREATOR = new Creator<ParseGeoPoint>() {
314+
@Override
315+
public ParseGeoPoint createFromParcel(Parcel source) {
316+
return new ParseGeoPoint(source, ParseParcelDecoder.get());
317+
}
318+
319+
@Override
320+
public ParseGeoPoint[] newArray(int size) {
321+
return new ParseGeoPoint[size];
322+
}
323+
};
271324
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public Object decode(Parcel source) {
6262
case ParseParcelEncoder.TYPE_FILE:
6363
return new ParseFile(source, this);
6464

65+
case ParseParcelEncoder.TYPE_GEOPOINT:
66+
return new ParseGeoPoint(source, this);
67+
6568
case ParseParcelEncoder.TYPE_ACL:
6669
return new ParseACL(source, this);
6770

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static boolean isValidType(Object value) {
4141
return ParseEncoder.isValidType(value);
4242
}
4343

44-
/* package */ final static String TYPE_OBJECT = "ParseObject";
44+
/* package */ final static String TYPE_OBJECT = "Object";
4545
/* package */ final static String TYPE_POINTER = "Pointer";
4646
/* package */ final static String TYPE_DATE = "Date";
4747
/* package */ final static String TYPE_BYTES = "Bytes";
@@ -53,7 +53,8 @@ private static boolean isValidType(Object value) {
5353
/* package */ final static String TYPE_NULL = "Null";
5454
/* package */ final static String TYPE_NATIVE = "Native";
5555
/* package */ final static String TYPE_OP = "Operation";
56-
/* package */ final static String TYPE_FILE = "ParseFile";
56+
/* package */ final static String TYPE_FILE = "File";
57+
/* package */ final static String TYPE_GEOPOINT = "GeoPoint";
5758

5859
public void encode(Object object, Parcel dest) {
5960
try {
@@ -80,7 +81,8 @@ public void encode(Object object, Parcel dest) {
8081
((ParseFile) object).writeToParcel(dest, this);
8182

8283
} else if (object instanceof ParseGeoPoint) {
83-
throw new IllegalArgumentException("Not supported yet");
84+
dest.writeString(TYPE_GEOPOINT);
85+
((ParseGeoPoint) object).writeToParcel(dest, this);
8486

8587
} else if (object instanceof ParseACL) {
8688
dest.writeString(TYPE_ACL);

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 = TestHelper.ROBOLECTRIC_SDK_VERSION)
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
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ public void testGetLongWithWrongValue() throws Exception {
497497

498498
@Test
499499
public void testParcelable() throws Exception {
500-
// TODO test ParseGeoPoint after merge
501500
ParseObject object = ParseObject.createWithoutData("Test", "objectId");
502501
object.isDeleted = true;
503502
object.put("long", 200L);
@@ -531,6 +530,9 @@ public void testParcelable() throws Exception {
531530
// File
532531
ParseFile file = new ParseFile(new ParseFile.State.Builder().url("fileUrl").build());
533532
object.put("file", file);
533+
// GeoPoint
534+
ParseGeoPoint point = new ParseGeoPoint(30d, 50d);
535+
object.put("point", point);
534536

535537
Parcel parcel = Parcel.obtain();
536538
object.writeToParcel(parcel, 0);
@@ -557,6 +559,8 @@ public void testParcelable() throws Exception {
557559
assertEquals(newRel.getKnownObjects().size(), rel.getKnownObjects().size());
558560
newRel.hasKnownObject(related);
559561
assertEquals(newObject.getParseFile("file").getUrl(), object.getParseFile("file").getUrl());
562+
assertEquals(newObject.getParseGeoPoint("point").getLatitude(),
563+
object.getParseGeoPoint("point").getLatitude(), 0);
560564
}
561565

562566
@Test

0 commit comments

Comments
 (0)