Requires GPS location to insert a marker except for

the track statistics marker.
This commit is contained in:
Jimmy Shih
2012-05-10 18:02:07 -07:00
parent f2ef8c7b08
commit b0207e5cd7
7 changed files with 88 additions and 59 deletions
+3 -2
View File
@@ -934,8 +934,9 @@ limitations under the License.
<!-- Marker --> <!-- Marker -->
<string name="marker_add_error" <string name="marker_add_error"
translation_description="When adding a marker, the message indicating error."> translation_description="When adding a marker, the message indicating error due to no GPS
Unable to insert a marker. Try again. signal.">
Unable to insert a marker. No GPS signal. Try again.
</string> </string>
<string name="marker_add_success" <string name="marker_add_success"
translation_description="When adding a marker, the message indicating success."> translation_description="When adding a marker, the message indicating success.">
@@ -160,6 +160,7 @@ public class MarkerEditActivity extends AbstractMyTracksActivity {
private void addMarker() { private void addMarker() {
WaypointCreationRequest waypointCreationRequest = new WaypointCreationRequest( WaypointCreationRequest waypointCreationRequest = new WaypointCreationRequest(
WaypointType.WAYPOINT, WaypointType.WAYPOINT,
false,
waypointName.getText().toString(), waypointName.getText().toString(),
waypointMarkerType.getText().toString(), waypointMarkerType.getText().toString(),
waypointDescription.getText().toString(), waypointDescription.getText().toString(),
@@ -346,7 +346,7 @@ public class TrackDetailActivity extends AbstractMyTracksActivity implements Del
if (event.getAction() == MotionEvent.ACTION_DOWN) { if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (TrackRecordingServiceConnectionUtils.isRecording(this, trackRecordingServiceConnection)) { if (TrackRecordingServiceConnectionUtils.isRecording(this, trackRecordingServiceConnection)) {
TrackRecordingServiceConnectionUtils.addMarker( TrackRecordingServiceConnectionUtils.addMarker(
this, trackRecordingServiceConnection, WaypointCreationRequest.DEFAULT_STATISTICS); this, trackRecordingServiceConnection, WaypointCreationRequest.DEFAULT_WAYPOINT);
return true; return true;
} }
} }
@@ -30,6 +30,7 @@ import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.TracksColumns; import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.content.Waypoint; import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.content.WaypointCreationRequest; import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.content.WaypointCreationRequest.WaypointType;
import com.google.android.apps.mytracks.content.WaypointsColumns; import com.google.android.apps.mytracks.content.WaypointsColumns;
import com.google.android.apps.mytracks.services.sensors.SensorManager; import com.google.android.apps.mytracks.services.sensors.SensorManager;
import com.google.android.apps.mytracks.services.sensors.SensorManagerFactory; import com.google.android.apps.mytracks.services.sensors.SensorManagerFactory;
@@ -589,7 +590,7 @@ public class TrackRecordingService extends Service {
statsBuilder.setMinRecordingDistance(minRecordingDistance); statsBuilder.setMinRecordingDistance(minRecordingDistance);
waypointStatsBuilder = new TripStatisticsBuilder(startTime); waypointStatsBuilder = new TripStatisticsBuilder(startTime);
waypointStatsBuilder.setMinRecordingDistance(minRecordingDistance); waypointStatsBuilder.setMinRecordingDistance(minRecordingDistance);
currentWaypointId = insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS); currentWaypointId = insertWaypoint(WaypointCreationRequest.DEFAULT_START_TRACK);
length = 0; length = 0;
showNotification(); showNotification();
registerLocationListener(); registerLocationListener();
@@ -916,37 +917,33 @@ public class TrackRecordingService extends Service {
public long insertWaypoint(WaypointCreationRequest request) { public long insertWaypoint(WaypointCreationRequest request) {
if (!isRecording()) { if (!isRecording()) {
throw new IllegalStateException( throw new IllegalStateException("Unable to insert marker while not recording!");
"Unable to insert waypoint marker while not recording!");
} }
if (request == null) { Waypoint waypoint = new Waypoint();
request = WaypointCreationRequest.DEFAULT_WAYPOINT; if (request.getType() == WaypointType.WAYPOINT) {
} buildWaypointMarker(waypoint, request);
Waypoint wpt = new Waypoint();
switch (request.getType()) {
case WAYPOINT:
buildWaypointMarker(wpt, request);
break;
case STATISTICS:
buildStatisticsMarker(wpt, request);
break;
}
wpt.setTrackId(recordingTrackId);
wpt.setLength(length);
if (lastLocation == null
|| statsBuilder == null || statsBuilder.getStatistics() == null) {
// A null location is ok, and expected on track start.
// Make it an impossible location.
Location l = new Location("");
l.setLatitude(100);
l.setLongitude(180);
wpt.setLocation(l);
} else { } else {
wpt.setLocation(lastLocation); buildStatisticsMarker(waypoint, request);
wpt.setDuration(lastLocation.getTime()
- statsBuilder.getStatistics().getStartTime());
} }
Uri uri = providerUtils.insertWaypoint(wpt); waypoint.setTrackId(recordingTrackId);
waypoint.setLength(length);
if (lastLocation == null || statsBuilder == null || statsBuilder.getStatistics() == null) {
if (!request.isTrackStatistics()) {
return -1L;
}
/*
* For track statistics, a null location is OK. Make it an impossible
* location.
*/
Location location = new Location("");
location.setLatitude(100);
location.setLongitude(180);
waypoint.setLocation(location);
} else {
waypoint.setLocation(lastLocation);
waypoint.setDuration(lastLocation.getTime() - statsBuilder.getStatistics().getStartTime());
}
Uri uri = providerUtils.insertWaypoint(waypoint);
return Long.parseLong(uri.getLastPathSegment()); return Long.parseLong(uri.getLastPathSegment());
} }
@@ -20,34 +20,38 @@ import android.os.Parcelable;
/** /**
* A request for the service to create a waypoint at the current location. * A request for the service to create a waypoint at the current location.
* *
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public class WaypointCreationRequest implements Parcelable { public class WaypointCreationRequest implements Parcelable {
public static enum WaypointType { public static enum WaypointType {
WAYPOINT, WAYPOINT, STATISTICS;
STATISTICS;
} }
private WaypointType type; private WaypointType type;
// true if this marker contains the track statistics
private boolean isTrackStatistics;
private String name; private String name;
private String category; private String category;
private String description; private String description;
private String iconUrl; private String iconUrl;
public final static WaypointCreationRequest DEFAULT_WAYPOINT = public final static WaypointCreationRequest DEFAULT_WAYPOINT = new WaypointCreationRequest(
new WaypointCreationRequest(WaypointType.WAYPOINT); WaypointType.WAYPOINT, false);
public final static WaypointCreationRequest DEFAULT_STATISTICS = public final static WaypointCreationRequest DEFAULT_STATISTICS = new WaypointCreationRequest(
new WaypointCreationRequest(WaypointType.STATISTICS); WaypointType.STATISTICS, false);
public final static WaypointCreationRequest DEFAULT_START_TRACK = new WaypointCreationRequest(
private WaypointCreationRequest(WaypointType type) { WaypointType.STATISTICS, true);
this.type = type;
private WaypointCreationRequest(WaypointType type, boolean isTrackStatistics) {
this(type, isTrackStatistics, null, null, null, null);
} }
public WaypointCreationRequest(WaypointType type, String name, String category, public WaypointCreationRequest(WaypointType type, boolean isTrackStatistics, String name,
String description, String iconUrl) { String category, String description, String iconUrl) {
this.type = type; this.type = type;
this.isTrackStatistics = isTrackStatistics;
this.name = name; this.name = name;
this.category = category; this.category = category;
this.description = description; this.description = description;
@@ -58,15 +62,13 @@ public class WaypointCreationRequest implements Parcelable {
@Override @Override
public WaypointCreationRequest createFromParcel(Parcel source) { public WaypointCreationRequest createFromParcel(Parcel source) {
int i = source.readInt(); WaypointCreationRequest request = new WaypointCreationRequest(
if (i > WaypointType.values().length) { WaypointType.values()[source.readInt()],
throw new IllegalArgumentException("Could not find waypoint type: " + i); source.readByte() == 1,
} source.readString(),
WaypointCreationRequest request = new WaypointCreationRequest(WaypointType.values()[i]); source.readString(),
request.name = source.readString(); source.readString(),
request.category = source.readString(); source.readString());
request.description = source.readString();
request.iconUrl = source.readString();
return request; return request;
} }
@@ -76,7 +78,7 @@ public class WaypointCreationRequest implements Parcelable {
} }
public static final Creator CREATOR = new Creator(); public static final Creator CREATOR = new Creator();
@Override @Override
public int describeContents() { public int describeContents() {
return 0; return 0;
@@ -85,16 +87,21 @@ public class WaypointCreationRequest implements Parcelable {
@Override @Override
public void writeToParcel(Parcel parcel, int arg1) { public void writeToParcel(Parcel parcel, int arg1) {
parcel.writeInt(type.ordinal()); parcel.writeInt(type.ordinal());
parcel.writeByte((byte) (isTrackStatistics ? 1 : 0));
parcel.writeString(name); parcel.writeString(name);
parcel.writeString(category); parcel.writeString(category);
parcel.writeString(description); parcel.writeString(description);
parcel.writeString(iconUrl); parcel.writeString(iconUrl);
} }
public WaypointType getType() { public WaypointType getType() {
return type; return type;
} }
public boolean isTrackStatistics() {
return isTrackStatistics;
}
public String getName() { public String getName() {
return name; return name;
} }
@@ -102,7 +109,7 @@ public class WaypointCreationRequest implements Parcelable {
public String getCategory() { public String getCategory() {
return category; return category;
} }
public String getDescription() { public String getDescription() {
return description; return description;
} }
@@ -35,19 +35,21 @@ public class WaypointCreationRequestTest extends AndroidTestCase {
p.setDataPosition(0); p.setDataPosition(0);
WaypointCreationRequest copy = WaypointCreationRequest.CREATOR.createFromParcel(p); WaypointCreationRequest copy = WaypointCreationRequest.CREATOR.createFromParcel(p);
assertEquals(original.getType(), copy.getType()); assertEquals(original.getType(), copy.getType());
assertFalse(copy.isTrackStatistics());
assertNull(copy.getName()); assertNull(copy.getName());
assertNull(copy.getDescription()); assertNull(copy.getDescription());
assertNull(copy.getIconUrl()); assertNull(copy.getIconUrl());
} }
public void testAllAttributesParceling() { public void testAllAttributesParceling() {
WaypointCreationRequest original = WaypointCreationRequest original = new WaypointCreationRequest(
new WaypointCreationRequest(WaypointType.WAYPOINT, "name", "category", "description", "img.png"); WaypointType.WAYPOINT, false, "name", "category", "description", "img.png");
Parcel p = Parcel.obtain(); Parcel p = Parcel.obtain();
original.writeToParcel(p, 0); original.writeToParcel(p, 0);
p.setDataPosition(0); p.setDataPosition(0);
WaypointCreationRequest copy = WaypointCreationRequest.CREATOR.createFromParcel(p); WaypointCreationRequest copy = WaypointCreationRequest.CREATOR.createFromParcel(p);
assertEquals(original.getType(), copy.getType()); assertEquals(original.getType(), copy.getType());
assertFalse(copy.isTrackStatistics());
assertEquals("name", copy.getName()); assertEquals("name", copy.getName());
assertEquals("category", copy.getCategory()); assertEquals("category", copy.getCategory());
assertEquals("description", copy.getDescription()); assertEquals("description", copy.getDescription());
@@ -39,6 +39,7 @@ import android.location.Location;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException;
import android.provider.Settings; import android.provider.Settings;
import android.test.RenamingDelegatingContext; import android.test.RenamingDelegatingContext;
import android.test.ServiceTestCase; import android.test.ServiceTestCase;
@@ -494,6 +495,7 @@ public class TrackRecordingServiceTest extends ServiceTestCase<TestRecordingServ
ITrackRecordingService service = bindAndGetService(createStartIntent()); ITrackRecordingService service = bindAndGetService(createStartIntent());
assertTrue(service.isRecording()); assertTrue(service.isRecording());
insertLocation(service);
assertEquals(1, service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS)); assertEquals(1, service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS));
assertEquals(2, service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS)); assertEquals(2, service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS));
@@ -530,6 +532,7 @@ public class TrackRecordingServiceTest extends ServiceTestCase<TestRecordingServ
ITrackRecordingService service = bindAndGetService(createStartIntent()); ITrackRecordingService service = bindAndGetService(createStartIntent());
assertTrue(service.isRecording()); assertTrue(service.isRecording());
insertLocation(service);
assertEquals(1, service.insertWaypoint(WaypointCreationRequest.DEFAULT_WAYPOINT)); assertEquals(1, service.insertWaypoint(WaypointCreationRequest.DEFAULT_WAYPOINT));
Waypoint wpt = providerUtils.getWaypoint(1); Waypoint wpt = providerUtils.getWaypoint(1);
@@ -742,4 +745,22 @@ public class TrackRecordingServiceTest extends ServiceTestCase<TestRecordingServ
assertTrue(tripStatistics.getStartTime() > 0); assertTrue(tripStatistics.getStartTime() > 0);
assertTrue(tripStatistics.getStopTime() >= tripStatistics.getStartTime()); assertTrue(tripStatistics.getStopTime() >= tripStatistics.getStartTime());
} }
/**
* Inserts a location and waits for 100ms.
*
* @param trackRecordingService the track recording service
*/
private void insertLocation(ITrackRecordingService trackRecordingService)
throws RemoteException, InterruptedException {
Location location = new Location("gps");
location.setLongitude(35.0f);
location.setLatitude(45.0f);
location.setAccuracy(5);
location.setSpeed(10);
location.setTime(System.currentTimeMillis());
location.setBearing(3.0f);
trackRecordingService.recordLocation(location);
Thread.sleep(100);
}
} }