diff --git a/lost/src/main/java/com/mapzen/android/lost/api/LocationRequest.java b/lost/src/main/java/com/mapzen/android/lost/api/LocationRequest.java index 82c8a5e..a47affd 100644 --- a/lost/src/main/java/com/mapzen/android/lost/api/LocationRequest.java +++ b/lost/src/main/java/com/mapzen/android/lost/api/LocationRequest.java @@ -1,6 +1,9 @@ package com.mapzen.android.lost.api; -public final class LocationRequest { +import android.os.Parcel; +import android.os.Parcelable; + +public final class LocationRequest implements Parcelable { public static final int PRIORITY_HIGH_ACCURACY = 0x00000064; public static final int PRIORITY_BALANCED_POWER_ACCURACY = 0x00000066; public static final int PRIORITY_LOW_POWER = 0x00000068; @@ -69,4 +72,33 @@ public LocationRequest setPriority(int priority) { this.priority = priority; return this; } + + @Override public int describeContents() { + return 0; + } + + @Override public void writeToParcel(Parcel dest, int flags) { + dest.writeLong(this.interval); + dest.writeLong(this.fastestInterval); + dest.writeFloat(this.smallestDisplacement); + dest.writeInt(this.priority); + } + + protected LocationRequest(Parcel in) { + this.interval = in.readLong(); + this.fastestInterval = in.readLong(); + this.smallestDisplacement = in.readFloat(); + this.priority = in.readInt(); + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + @Override public LocationRequest createFromParcel(Parcel source) { + return new LocationRequest(source); + } + + @Override public LocationRequest[] newArray(int size) { + return new LocationRequest[size]; + } + }; } diff --git a/lost/src/test/java/com/mapzen/android/lost/api/LocationRequestTest.java b/lost/src/test/java/com/mapzen/android/lost/api/LocationRequestTest.java index f38fb0e..ea25b90 100644 --- a/lost/src/test/java/com/mapzen/android/lost/api/LocationRequestTest.java +++ b/lost/src/test/java/com/mapzen/android/lost/api/LocationRequestTest.java @@ -9,6 +9,8 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; +import android.os.Parcel; + import static com.mapzen.android.lost.api.LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY; import static com.mapzen.android.lost.api.LocationRequest.PRIORITY_HIGH_ACCURACY; import static org.fest.assertions.api.Assertions.assertThat; @@ -74,4 +76,23 @@ public class LocationRequestTest extends BaseRobolectricTest { public void setPriority_shouldRejectInvalidValues() throws Exception { locationRequest.setPriority(-1); } + + @Test public void shouldBeParcelable() throws Exception { + LocationRequest dehydrated = LocationRequest.create() + .setPriority(PRIORITY_HIGH_ACCURACY) + .setInterval(1000) + .setFastestInterval(500) + .setSmallestDisplacement(10); + + Parcel parcel = Parcel.obtain(); + dehydrated.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + + LocationRequest rehydrated = new LocationRequest(parcel); + assertThat(rehydrated.getPriority()).isEqualTo(dehydrated.getPriority()); + assertThat(rehydrated.getInterval()).isEqualTo(dehydrated.getInterval()); + assertThat(rehydrated.getFastestInterval()).isEqualTo(dehydrated.getFastestInterval()); + assertThat(rehydrated.getSmallestDisplacement()) + .isEqualTo(dehydrated.getSmallestDisplacement()); + } }