Skip to content

Implementing Parcelable in ParseGeoPoint #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Parse/src/main/java/com/parse/ParseFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ protected ParseFile(Parcel source) {
* Creates a new file instance from a {@link Parcel} using the given {@link ParseParcelDecoder}.
* The decoder is currently unused, but it might be in the future, plus this is the pattern we
* are using in parcelable classes.
*
* @param source the parcel
* @param decoder the decoder
*/
Expand Down
55 changes: 54 additions & 1 deletion Parse/src/main/java/com/parse/ParseGeoPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import android.location.Criteria;
import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Locale;

Expand All @@ -32,7 +34,7 @@
* </pre>
*/

public class ParseGeoPoint {
public class ParseGeoPoint implements Parcelable {
static double EARTH_MEAN_RADIUS_KM = 6371.0;
static double EARTH_MEAN_RADIUS_MILE = 3958.8;

Expand Down Expand Up @@ -68,6 +70,30 @@ public ParseGeoPoint(ParseGeoPoint point) {
this(point.getLatitude(), point.getLongitude());
}

/**
* Creates a new point instance from a {@link Parcel} source. This is used when unparceling a
* ParseGeoPoint. Subclasses that need Parcelable behavior should provide their own
* {@link android.os.Parcelable.Creator} and override this constructor.
*
* @param source The recovered parcel.
*/
protected ParseGeoPoint(Parcel source) {
this(source, ParseParcelDecoder.get());
}

/**
* Creates a new point instance from a {@link Parcel} using the given {@link ParseParcelDecoder}.
* The decoder is currently unused, but it might be in the future, plus this is the pattern we
* are using in parcelable classes.
*
* @param source the parcel
* @param decoder the decoder
*/
ParseGeoPoint(Parcel source, ParseParcelDecoder decoder) {
setLatitude(source.readDouble());
setLongitude(source.readDouble());
}

/**
* Set latitude. Valid range is (-90.0, 90.0). Extremes should not be used.
*
Expand Down Expand Up @@ -268,4 +294,31 @@ public static void getCurrentLocationInBackground(long timeout, Criteria criteri
public String toString() {
return String.format(Locale.US, "ParseGeoPoint[%.6f,%.6f]", latitude, longitude);
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
writeToParcel(dest, ParseParcelEncoder.get());
}

void writeToParcel(Parcel dest, ParseParcelEncoder encoder) {
dest.writeDouble(latitude);
dest.writeDouble(longitude);
}

public final static Creator<ParseGeoPoint> CREATOR = new Creator<ParseGeoPoint>() {
@Override
public ParseGeoPoint createFromParcel(Parcel source) {
return new ParseGeoPoint(source, ParseParcelDecoder.get());
}

@Override
public ParseGeoPoint[] newArray(int size) {
return new ParseGeoPoint[size];
}
};
}
3 changes: 3 additions & 0 deletions Parse/src/main/java/com/parse/ParseParcelDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public Object decode(Parcel source) {
case ParseParcelEncoder.TYPE_FILE:
return new ParseFile(source, this);

case ParseParcelEncoder.TYPE_GEOPOINT:
return new ParseGeoPoint(source, this);

case ParseParcelEncoder.TYPE_ACL:
return new ParseACL(source, this);

Expand Down
8 changes: 5 additions & 3 deletions Parse/src/main/java/com/parse/ParseParcelEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static boolean isValidType(Object value) {
return ParseEncoder.isValidType(value);
}

/* package */ final static String TYPE_OBJECT = "ParseObject";
/* package */ final static String TYPE_OBJECT = "Object";
/* package */ final static String TYPE_POINTER = "Pointer";
/* package */ final static String TYPE_DATE = "Date";
/* package */ final static String TYPE_BYTES = "Bytes";
Expand All @@ -53,7 +53,8 @@ private static boolean isValidType(Object value) {
/* package */ final static String TYPE_NULL = "Null";
/* package */ final static String TYPE_NATIVE = "Native";
/* package */ final static String TYPE_OP = "Operation";
/* package */ final static String TYPE_FILE = "ParseFile";
/* package */ final static String TYPE_FILE = "File";
/* package */ final static String TYPE_GEOPOINT = "GeoPoint";

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

} else if (object instanceof ParseGeoPoint) {
throw new IllegalArgumentException("Not supported yet");
dest.writeString(TYPE_GEOPOINT);
((ParseGeoPoint) object).writeToParcel(dest, this);

} else if (object instanceof ParseACL) {
dest.writeString(TYPE_ACL);
Expand Down
18 changes: 18 additions & 0 deletions Parse/src/test/java/com/parse/ParseGeoPointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
*/
package com.parse;

import android.os.Parcel;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static org.junit.Assert.*;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = TestHelper.ROBOLECTRIC_SDK_VERSION)
public class ParseGeoPointTest {

@Test
Expand All @@ -30,4 +37,15 @@ public void testConstructors() {
assertEquals(lat, copy.getLatitude(), 0);
assertEquals(lng, copy.getLongitude(), 0);
}

@Test
public void testParcelable() {
ParseGeoPoint point = new ParseGeoPoint(30d, 50d);
Parcel parcel = Parcel.obtain();
point.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
point = ParseGeoPoint.CREATOR.createFromParcel(parcel);
assertEquals(point.getLatitude(), 30d, 0);
assertEquals(point.getLongitude(), 50d, 0);
}
}
6 changes: 5 additions & 1 deletion Parse/src/test/java/com/parse/ParseObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ public void testGetLongWithWrongValue() throws Exception {

@Test
public void testParcelable() throws Exception {
// TODO test ParseGeoPoint after merge
ParseObject object = ParseObject.createWithoutData("Test", "objectId");
object.isDeleted = true;
object.put("long", 200L);
Expand Down Expand Up @@ -531,6 +530,9 @@ public void testParcelable() throws Exception {
// File
ParseFile file = new ParseFile(new ParseFile.State.Builder().url("fileUrl").build());
object.put("file", file);
// GeoPoint
ParseGeoPoint point = new ParseGeoPoint(30d, 50d);
object.put("point", point);

Parcel parcel = Parcel.obtain();
object.writeToParcel(parcel, 0);
Expand All @@ -557,6 +559,8 @@ public void testParcelable() throws Exception {
assertEquals(newRel.getKnownObjects().size(), rel.getKnownObjects().size());
newRel.hasKnownObject(related);
assertEquals(newObject.getParseFile("file").getUrl(), object.getParseFile("file").getUrl());
assertEquals(newObject.getParseGeoPoint("point").getLatitude(),
object.getParseGeoPoint("point").getLatitude(), 0);
}

@Test
Expand Down