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

Commit

Permalink
LocationRequest is now Parcelable (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgreb authored and sarahsnow1 committed Apr 20, 2017
1 parent ae35abe commit d01195f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<LocationRequest> CREATOR =
new Parcelable.Creator<LocationRequest>() {
@Override public LocationRequest createFromParcel(Parcel source) {
return new LocationRequest(source);
}

@Override public LocationRequest[] newArray(int size) {
return new LocationRequest[size];
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit d01195f

Please sign in to comment.