Properly handling invalid track/point/waypoint IDs.

This makes the provider utils implementation return null for those cases, and fixes all callers which didn't check for that.
This commit is contained in:
Rodrigo Damazio
2011-06-09 19:12:51 -03:00
parent ae4ce12c17
commit 98ad96115a
9 changed files with 173 additions and 91 deletions
@@ -144,6 +144,7 @@ public class ChartActivity extends Activity implements TrackDataListener {
dataHub.registerTrackDataListener(this, EnumSet.of( dataHub.registerTrackDataListener(this, EnumSet.of(
ListenerDataType.SELECTED_TRACK_CHANGED, ListenerDataType.SELECTED_TRACK_CHANGED,
ListenerDataType.TRACK_UPDATES,
ListenerDataType.POINT_UPDATES, ListenerDataType.POINT_UPDATES,
ListenerDataType.SAMPLED_OUT_POINT_UPDATES, ListenerDataType.SAMPLED_OUT_POINT_UPDATES,
ListenerDataType.WAYPOINT_UPDATES, ListenerDataType.WAYPOINT_UPDATES,
@@ -424,7 +425,7 @@ public class ChartActivity extends Activity implements TrackDataListener {
this.metricUnits = metric; this.metricUnits = metric;
chartView.setMetricUnits(metric); chartView.setMetricUnits(metric);
return true; // Reload data return true; // Reload data
} }
@@ -189,6 +189,11 @@ public class ExportAllTracks {
Log.i(Constants.TAG, "ExportAllTracks: exporting: " + id); Log.i(Constants.TAG, "ExportAllTracks: exporting: " + id);
TrackWriter writer = TrackWriter writer =
TrackWriterFactory.newWriter(activity, providerUtils, id, format); TrackWriterFactory.newWriter(activity, providerUtils, id, format);
if (writer == null) {
showToast(R.string.error_track_does_not_exist, Toast.LENGTH_LONG);
return;
}
writer.writeTrack(); writer.writeTrack();
if (!writer.wasSuccess()) { if (!writer.wasSuccess()) {
@@ -905,7 +905,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
* Initiates the process to send tracks to google. * Initiates the process to send tracks to google.
* This is called once the user has selected sending options via the * This is called once the user has selected sending options via the
* SendToGoogleDialog. * SendToGoogleDialog.
* *
* TODO: Change this whole flow to an actual state machine. * TODO: Change this whole flow to an actual state machine.
*/ */
public void sendToGoogle() { public void sendToGoogle() {
@@ -963,8 +963,12 @@ public class MyTracks extends TabActivity implements OnTouchListener,
// Update the map id for this track: // Update the map id for this track:
try { try {
Track track = providerUtils.getTrack(trackId); Track track = providerUtils.getTrack(trackId);
track.setMapId(mapId); if (track != null) {
providerUtils.updateTrack(track); track.setMapId(mapId);
providerUtils.updateTrack(track);
} else {
Log.w(TAG, "Updating map id failed.");
}
} catch (RuntimeException e) { } catch (RuntimeException e) {
// If that fails whatever reasons we'll just log an error, but // If that fails whatever reasons we'll just log an error, but
// continue. // continue.
@@ -1017,15 +1021,19 @@ public class MyTracks extends TabActivity implements OnTouchListener,
// Update the table id for this track: // Update the table id for this track:
try { try {
Track track = providerUtils.getTrack(trackId); Track track = providerUtils.getTrack(trackId);
track.setTableId(tableId); if (track != null) {
providerUtils.updateTrack(track); track.setTableId(tableId);
providerUtils.updateTrack(track);
} else {
Log.w(TAG, "Updating table id failed.");
}
} catch (RuntimeException e) { } catch (RuntimeException e) {
// If that fails whatever reasons we'll just log an error, but // If that fails whatever reasons we'll just log an error, but
// continue. // continue.
Log.w(TAG, "Updating table id failed.", e); Log.w(TAG, "Updating table id failed.", e);
} }
} }
onSendToFusionTablesDone(); onSendToFusionTablesDone();
} }
}; };
@@ -1072,7 +1080,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
Log.d(TAG, "Sending to Docs...."); Log.d(TAG, "Sending to Docs....");
setProgressValue(50); setProgressValue(50);
setProgressMessage(R.string.progress_message_sending_docs); setProgressMessage(R.string.progress_message_sending_docs);
final SendToDocs sender = new SendToDocs(this, final SendToDocs sender = new SendToDocs(this,
authMap.get(SendToDocs.GDATA_SERVICE_NAME_TRIX), authMap.get(SendToDocs.GDATA_SERVICE_NAME_TRIX),
authMap.get(SendToDocs.GDATA_SERVICE_NAME_DOCLIST), trackId); authMap.get(SendToDocs.GDATA_SERVICE_NAME_DOCLIST), trackId);
sendToTrackId = trackId; sendToTrackId = trackId;
@@ -1228,6 +1236,11 @@ public class MyTracks extends TabActivity implements OnTouchListener,
*/ */
public void saveTrack(long trackId, TrackFileFormat format) { public void saveTrack(long trackId, TrackFileFormat format) {
TrackWriter writer = TrackWriterFactory.newWriter(this, providerUtils, trackId, format); TrackWriter writer = TrackWriterFactory.newWriter(this, providerUtils, trackId, format);
if (writer == null) {
dialogManager.showMessageDialog(R.string.error_track_does_not_exist, false);
return;
}
WriteProgressController controller = new WriteProgressController(this, writer); WriteProgressController controller = new WriteProgressController(this, writer);
controller.setOnCompletionListener(new WriteProgressController.OnCompletionListener() { controller.setOnCompletionListener(new WriteProgressController.OnCompletionListener() {
@Override @Override
@@ -1247,6 +1260,10 @@ public class MyTracks extends TabActivity implements OnTouchListener,
*/ */
public void sendTrack(long trackId, final TrackFileFormat format) { public void sendTrack(long trackId, final TrackFileFormat format) {
TrackWriter writer = TrackWriterFactory.newWriter(this, providerUtils, trackId, format); TrackWriter writer = TrackWriterFactory.newWriter(this, providerUtils, trackId, format);
if (writer == null) {
dialogManager.showMessageDialog(R.string.error_track_does_not_exist, false);
return;
}
FileUtils fileUtils = new FileUtils(); FileUtils fileUtils = new FileUtils();
String extension = format.getExtension(); String extension = format.getExtension();
@@ -1313,17 +1330,17 @@ public class MyTracks extends TabActivity implements OnTouchListener,
long getRecordingTrackId() { long getRecordingTrackId() {
return sharedPreferences.getLong(getString(R.string.recording_track_key), -1); return sharedPreferences.getLong(getString(R.string.recording_track_key), -1);
} }
// @VisibleForTesting // @VisibleForTesting
SharedPreferences getSharedPreferences() { SharedPreferences getSharedPreferences() {
return sharedPreferences; return sharedPreferences;
} }
// @VisibleForTesting // @VisibleForTesting
static void clearInstance() { static void clearInstance() {
instance = null; instance = null;
} }
// @VisibleForTesting // @VisibleForTesting
ITrackRecordingService getTrackRecordingService() { ITrackRecordingService getTrackRecordingService() {
return trackRecordingService; return trackRecordingService;
@@ -1,12 +1,12 @@
/* /*
* Copyright 2009 Google Inc. * Copyright 2009 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not * Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of * use this file except in compliance with the License. You may obtain a copy of
* the License at * the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -73,15 +73,18 @@ public class WaypointsList extends ListActivity
contextPosition = info.position; contextPosition = info.position;
waypointId = WaypointsList.this.listView.getAdapter() waypointId = WaypointsList.this.listView.getAdapter()
.getItemId(contextPosition); .getItemId(contextPosition);
int type = providerUtils.getWaypoint(info.id).getType(); Waypoint waypoint = providerUtils.getWaypoint(info.id);
menu.add(0, Constants.MENU_SHOW, 0, if (waypoint != null) {
R.string.waypointslist_show_waypoint); int type = waypoint.getType();
menu.add(0, Constants.MENU_EDIT, 0, menu.add(0, Constants.MENU_SHOW, 0,
R.string.waypointslist_edit_waypoint); R.string.waypointslist_show_waypoint);
menu.add(0, Constants.MENU_DELETE, 0, menu.add(0, Constants.MENU_EDIT, 0,
R.string.waypointslist_delete_waypoint).setEnabled( R.string.waypointslist_edit_waypoint);
recordingTrackId < 0 || type == Waypoint.TYPE_WAYPOINT || menu.add(0, Constants.MENU_DELETE, 0,
info.id != providerUtils.getLastWaypointId(recordingTrackId)); R.string.waypointslist_delete_waypoint).setEnabled(
recordingTrackId < 0 || type == Waypoint.TYPE_WAYPOINT ||
info.id != providerUtils.getLastWaypointId(recordingTrackId));
}
} }
}; };
@@ -159,7 +162,7 @@ public class WaypointsList extends ListActivity
trackId = -1; trackId = -1;
} }
final long firstWaypointId = providerUtils.getFirstWaypointId(trackId); final long firstWaypointId = providerUtils.getFirstWaypointId(trackId);
waypointsCursor = getContentResolver().query( waypointsCursor = getContentResolver().query(
WaypointsColumns.CONTENT_URI, null, WaypointsColumns.CONTENT_URI, null,
WaypointsColumns.TRACKID + "=" + trackId + " AND " WaypointsColumns.TRACKID + "=" + trackId + " AND "
@@ -209,7 +212,7 @@ public class WaypointsList extends ListActivity
new String[] { WaypointsColumns.NAME, WaypointsColumns.TIME, new String[] { WaypointsColumns.NAME, WaypointsColumns.TIME,
WaypointsColumns.CATEGORY, WaypointsColumns.TYPE }, WaypointsColumns.CATEGORY, WaypointsColumns.TYPE },
new int[] { R.id.waypointslist_item_name, new int[] { R.id.waypointslist_item_name,
R.id.waypointslist_item_time, R.id.waypointslist_item_time,
R.id.waypointslist_item_category, R.id.waypointslist_item_category,
R.id.waypointslist_item_icon }); R.id.waypointslist_item_icon });
@@ -219,7 +222,7 @@ public class WaypointsList extends ListActivity
waypointsCursor.getColumnIndexOrThrow(WaypointsColumns.TYPE); waypointsCursor.getColumnIndexOrThrow(WaypointsColumns.TYPE);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override @Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) { public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == timeIdx) { if (columnIndex == timeIdx) {
long time = cursor.getLong(timeIdx); long time = cursor.getLong(timeIdx);
TextView textView = (TextView) view; TextView textView = (TextView) view;
@@ -17,10 +17,10 @@ package com.google.android.apps.mytracks.content;
import static com.google.android.apps.mytracks.Constants.DEFAULT_MIN_REQUIRED_ACCURACY; import static com.google.android.apps.mytracks.Constants.DEFAULT_MIN_REQUIRED_ACCURACY;
import static com.google.android.apps.mytracks.Constants.MAX_DISPLAYED_WAYPOINTS_POINTS; import static com.google.android.apps.mytracks.Constants.MAX_DISPLAYED_WAYPOINTS_POINTS;
import static com.google.android.apps.mytracks.Constants.TARGET_DISPLAYED_TRACK_POINTS;
import static com.google.android.apps.mytracks.Constants.MAX_LOCATION_AGE_MS; import static com.google.android.apps.mytracks.Constants.MAX_LOCATION_AGE_MS;
import static com.google.android.apps.mytracks.Constants.MAX_NETWORK_AGE_MS; import static com.google.android.apps.mytracks.Constants.MAX_NETWORK_AGE_MS;
import static com.google.android.apps.mytracks.Constants.TAG; import static com.google.android.apps.mytracks.Constants.TAG;
import static com.google.android.apps.mytracks.Constants.TARGET_DISPLAYED_TRACK_POINTS;
import com.google.android.apps.mytracks.content.DataSourceManager.DataSourceListener; import com.google.android.apps.mytracks.content.DataSourceManager.DataSourceListener;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils.DoubleBufferedLocationFactory; import com.google.android.apps.mytracks.content.MyTracksProviderUtils.DoubleBufferedLocationFactory;
@@ -40,7 +40,6 @@ import android.location.LocationManager;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
@@ -791,10 +790,6 @@ public class TrackDataHub {
public void run() { public void run() {
for (TrackDataListener listener : listeners) { for (TrackDataListener listener : listeners) {
listener.onSelectedTrackChanged(track, isRecordingSelected()); listener.onSelectedTrackChanged(track, isRecordingSelected());
if (track != null) {
listener.onTrackUpdated(track);
}
} }
} }
}); });
@@ -179,6 +179,11 @@ public class SendToFusionTables implements Runnable {
// Get the track meta-data // Get the track meta-data
Track track = providerUtils.getTrack(trackId); Track track = providerUtils.getTrack(trackId);
if (track == null) {
Log.w(Constants.TAG, "Cannot get track.");
return;
}
String originalDescription = track.getDescription(); String originalDescription = track.getDescription();
// Create a new table: // Create a new table:
@@ -207,7 +212,6 @@ public class SendToFusionTables implements Runnable {
Log.d(Constants.TAG, "SendToFusionTables: Done: " + success); Log.d(Constants.TAG, "SendToFusionTables: Done: " + success);
progressIndicator.setProgressValue(PROGRESS_COMPLETE); progressIndicator.setProgressValue(PROGRESS_COMPLETE);
} finally { } finally {
final boolean finalSuccess = success; final boolean finalSuccess = success;
final int finalStatusMessageId = statusMessageId; final int finalStatusMessageId = statusMessageId;
context.runOnUiThread(new Runnable() { context.runOnUiThread(new Runnable() {
@@ -304,7 +308,7 @@ public class SendToFusionTables implements Runnable {
Cursor locationsCursor = providerUtils.getLocationsCursor(track.getId(), 0, -1, false); Cursor locationsCursor = providerUtils.getLocationsCursor(track.getId(), 0, -1, false);
try { try {
if (!locationsCursor.moveToFirst()) { if (locationsCursor == null || !locationsCursor.moveToFirst()) {
Log.w(Constants.TAG, "Unable to get any points to upload"); Log.w(Constants.TAG, "Unable to get any points to upload");
return false; return false;
} }
@@ -391,7 +395,9 @@ public class SendToFusionTables implements Runnable {
return true; return true;
} finally { } finally {
locationsCursor.close(); if (locationsCursor != null) {
locationsCursor.close();
}
} }
} }
@@ -1,12 +1,12 @@
/* /*
* Copyright 2008 Google Inc. * Copyright 2008 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not * Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of * use this file except in compliance with the License. You may obtain a copy of
* the License at * the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -76,7 +76,7 @@ public class SendToMyMaps implements Runnable {
public interface OnSendCompletedListener { public interface OnSendCompletedListener {
void onSendCompleted(String mapId, boolean success, int statusMessage); void onSendCompleted(String mapId, boolean success, int statusMessage);
} }
public SendToMyMaps(Activity context, String mapId, AuthManager auth, public SendToMyMaps(Activity context, String mapId, AuthManager auth,
long trackId, ProgressIndicator progressIndicator, long trackId, ProgressIndicator progressIndicator,
OnSendCompletedListener onCompletion) { OnSendCompletedListener onCompletion) {
@@ -103,15 +103,20 @@ public class SendToMyMaps implements Runnable {
progressIndicator.setProgressValue(1); progressIndicator.setProgressValue(1);
progressIndicator.setProgressMessage( progressIndicator.setProgressMessage(
R.string.progress_message_reading_track); R.string.progress_message_reading_track);
// Get the track meta-data // Get the track meta-data
Track track = providerUtils.getTrack(trackId); Track track = providerUtils.getTrack(trackId);
if (track == null) {
Log.w(Constants.TAG, "Cannot get track.");
return;
}
String originalDescription = track.getDescription(); String originalDescription = track.getDescription();
track.setDescription("<p>" + track.getDescription() + "</p><p>" track.setDescription("<p>" + track.getDescription() + "</p><p>"
+ stringUtils.generateTrackDescription(track, null, null) + "</p>"); + stringUtils.generateTrackDescription(track, null, null) + "</p>");
mapsClient = new MapsFacade(context, auth); mapsClient = new MapsFacade(context, auth);
// Create a new map if necessary: // Create a new map if necessary:
boolean isNewMap = mapId.equals(NEW_MAP_ID); boolean isNewMap = mapId.equals(NEW_MAP_ID);
if (isNewMap) { if (isNewMap) {
@@ -166,7 +171,7 @@ public class SendToMyMaps implements Runnable {
Log.w(TAG, "SendToMyMaps: upload waypoints failed."); Log.w(TAG, "SendToMyMaps: upload waypoints failed.");
} }
} }
if (success) { if (success) {
statusMessageId = isNewMap statusMessageId = isNewMap
? R.string.status_new_mymap_has_been_created ? R.string.status_new_mymap_has_been_created
@@ -178,7 +183,7 @@ public class SendToMyMaps implements Runnable {
if (mapsClient != null) { if (mapsClient != null) {
mapsClient.cleanUp(); mapsClient.cleanUp();
} }
final boolean finalSuccess = success; final boolean finalSuccess = success;
final int finalStatusMessageId = statusMessageId; final int finalStatusMessageId = statusMessageId;
context.runOnUiThread(new Runnable() { context.runOnUiThread(new Runnable() {
@@ -206,17 +211,17 @@ public class SendToMyMaps implements Runnable {
Cursor locationsCursor = Cursor locationsCursor =
providerUtils.getLocationsCursor(track.getId(), 0, -1, false); providerUtils.getLocationsCursor(track.getId(), 0, -1, false);
try { try {
if (!locationsCursor.moveToFirst()) { if (locationsCursor == null || !locationsCursor.moveToFirst()) {
Log.w(TAG, "Unable to get any points to upload"); Log.w(TAG, "Unable to get any points to upload");
return false; return false;
} }
totalLocationsRead = 0; totalLocationsRead = 0;
totalLocationsPrepared = 0; totalLocationsPrepared = 0;
totalLocationsUploaded = 0; totalLocationsUploaded = 0;
totalLocations = locationsCursor.getCount(); totalLocations = locationsCursor.getCount();
totalSegmentsUploaded = 0; totalSegmentsUploaded = 0;
// Limit the number of elevation readings. Ideally we would want around 250. // Limit the number of elevation readings. Ideally we would want around 250.
int elevationSamplingFrequency = int elevationSamplingFrequency =
Math.max(1, (int) (totalLocations / 250.0)); Math.max(1, (int) (totalLocations / 250.0));
@@ -224,12 +229,12 @@ public class SendToMyMaps implements Runnable {
"Using elevation sampling factor: " + elevationSamplingFrequency "Using elevation sampling factor: " + elevationSamplingFrequency
+ " on " + totalLocations); + " on " + totalLocations);
double totalDistance = 0; double totalDistance = 0;
Vector<Double> distances = new Vector<Double>(); Vector<Double> distances = new Vector<Double>();
Vector<Double> elevations = new Vector<Double>(); Vector<Double> elevations = new Vector<Double>();
DoubleBuffer elevationBuffer = DoubleBuffer elevationBuffer =
new DoubleBuffer(Constants.ELEVATION_SMOOTHING_FACTOR); new DoubleBuffer(Constants.ELEVATION_SMOOTHING_FACTOR);
List<Location> locations = new ArrayList<Location>(MAX_POINTS_PER_UPLOAD); List<Location> locations = new ArrayList<Location>(MAX_POINTS_PER_UPLOAD);
progressIndicator.setProgressMessage( progressIndicator.setProgressMessage(
R.string.progress_message_reading_track); R.string.progress_message_reading_track);
@@ -238,15 +243,15 @@ public class SendToMyMaps implements Runnable {
if (totalLocationsRead % 100 == 0) { if (totalLocationsRead % 100 == 0) {
updateProgress(); updateProgress();
} }
Location loc = providerUtils.createLocation(locationsCursor); Location loc = providerUtils.createLocation(locationsCursor);
locations.add(loc); locations.add(loc);
if (totalLocationsRead == 0) { if (totalLocationsRead == 0) {
// Put a marker at the first point of the first valid segment: // Put a marker at the first point of the first valid segment:
mapsClient.uploadMarker(mapId, track.getName(), track.getDescription(), loc, true); mapsClient.uploadMarker(mapId, track.getName(), track.getDescription(), loc, true);
} }
// Add to the elevation profile. // Add to the elevation profile.
if (loc != null && LocationUtils.isValidLocation(loc)) { if (loc != null && LocationUtils.isValidLocation(loc)) {
// All points go into the smoothing buffer... // All points go into the smoothing buffer...
@@ -256,19 +261,19 @@ public class SendToMyMaps implements Runnable {
double dist = lastLocation.distanceTo(loc); double dist = lastLocation.distanceTo(loc);
totalDistance += dist; totalDistance += dist;
} }
// ...but only a few points are really used to keep the url short. // ...but only a few points are really used to keep the url short.
if (totalLocationsRead % elevationSamplingFrequency == 0) { if (totalLocationsRead % elevationSamplingFrequency == 0) {
distances.add(totalDistance); distances.add(totalDistance);
elevations.add(elevationBuffer.getAverage()); elevations.add(elevationBuffer.getAverage());
} }
} }
// If the location was not valid, it's a segment split, so make sure the // If the location was not valid, it's a segment split, so make sure the
// distance between the previous segment and the new one is not accounted // distance between the previous segment and the new one is not accounted
// for in the next iteration. // for in the next iteration.
lastLocation = loc; lastLocation = loc;
// Every now and then, upload the accumulated points // Every now and then, upload the accumulated points
if (totalLocationsRead % MAX_POINTS_PER_UPLOAD if (totalLocationsRead % MAX_POINTS_PER_UPLOAD
== MAX_POINTS_PER_UPLOAD - 1) { == MAX_POINTS_PER_UPLOAD - 1) {
@@ -276,28 +281,30 @@ public class SendToMyMaps implements Runnable {
return false; return false;
} }
} }
totalLocationsRead++; totalLocationsRead++;
} while (locationsCursor.moveToNext()); } while (locationsCursor.moveToNext());
// Do a final upload with what's left // Do a final upload with what's left
if (!prepareAndUploadPoints(track, locations)) { if (!prepareAndUploadPoints(track, locations)) {
return false; return false;
} }
// Put an end marker at the last point of the last valid segment: // Put an end marker at the last point of the last valid segment:
if (lastLocation != null) { if (lastLocation != null) {
track.setDescription("<p>" + originalDescription + "</p><p>" track.setDescription("<p>" + originalDescription + "</p><p>"
+ stringUtils.generateTrackDescription( + stringUtils.generateTrackDescription(
track, distances, elevations) track, distances, elevations)
+ "</p>"); + "</p>");
return mapsClient.uploadMarker(mapId, track.getName(), track.getDescription(), return mapsClient.uploadMarker(mapId, track.getName(), track.getDescription(),
lastLocation, false); lastLocation, false);
} }
return true; return true;
} finally { } finally {
locationsCursor.close(); if (locationsCursor != null) {
locationsCursor.close();
}
} }
} }
@@ -306,17 +313,17 @@ public class SendToMyMaps implements Runnable {
progressIndicator.setProgressMessage( progressIndicator.setProgressMessage(
R.string.progress_message_preparing_track); R.string.progress_message_preparing_track);
updateProgress(); updateProgress();
int numLocations = locations.size(); int numLocations = locations.size();
if (numLocations < 2) { if (numLocations < 2) {
Log.d(TAG, "Not preparing/uploading too few points"); Log.d(TAG, "Not preparing/uploading too few points");
totalLocationsUploaded += numLocations; totalLocationsUploaded += numLocations;
return true; return true;
} }
// Prepare/pre-process the points // Prepare/pre-process the points
ArrayList<Track> splitTracks = prepareLocations(track, locations); ArrayList<Track> splitTracks = prepareLocations(track, locations);
// Start uploading them // Start uploading them
progressIndicator.setProgressMessage( progressIndicator.setProgressMessage(
R.string.progress_message_sending_mymaps); R.string.progress_message_sending_mymaps);
@@ -330,7 +337,7 @@ public class SendToMyMaps implements Runnable {
Log.d(TAG, Log.d(TAG,
"SendToMyMaps: Prepared feature for upload w/ " "SendToMyMaps: Prepared feature for upload w/ "
+ splitTrack.getLocations().size() + " points."); + splitTrack.getLocations().size() + " points.");
// Transmit tracks via GData feed: // Transmit tracks via GData feed:
// ------------------------------- // -------------------------------
Log.d(TAG, Log.d(TAG,
@@ -340,7 +347,7 @@ public class SendToMyMaps implements Runnable {
return false; return false;
} }
} }
locations.clear(); locations.clear();
totalLocationsUploaded += numLocations; totalLocationsUploaded += numLocations;
updateProgress(); updateProgress();
@@ -358,7 +365,7 @@ public class SendToMyMaps implements Runnable {
private ArrayList<Track> prepareLocations( private ArrayList<Track> prepareLocations(
Track track, Iterable<Location> locations) { Track track, Iterable<Location> locations) {
ArrayList<Track> splitTracks = new ArrayList<Track>(); ArrayList<Track> splitTracks = new ArrayList<Track>();
// Create segments from each full track: // Create segments from each full track:
Track segment = new Track(); Track segment = new Track();
TripStatistics segmentStats = segment.getStatistics(); TripStatistics segmentStats = segment.getStatistics();
@@ -377,11 +384,11 @@ public class SendToMyMaps implements Runnable {
if (loc.getLatitude() > 90) { if (loc.getLatitude() > 90) {
startNewTrackSegment = true; startNewTrackSegment = true;
} }
if (startNewTrackSegment) { if (startNewTrackSegment) {
// Close up the last segment. // Close up the last segment.
prepareTrackSegment(segment, splitTracks); prepareTrackSegment(segment, splitTracks);
Log.d(TAG, Log.d(TAG,
"MyTracksSendToMyMaps: Starting new track segment..."); "MyTracksSendToMyMaps: Starting new track segment...");
startNewTrackSegment = false; startNewTrackSegment = false;
@@ -391,7 +398,7 @@ public class SendToMyMaps implements Runnable {
segment.setDescription(/* track.getDescription() */ ""); segment.setDescription(/* track.getDescription() */ "");
segment.setCategory(track.getCategory()); segment.setCategory(track.getCategory());
} }
if (loc.getLatitude() <= 90) { if (loc.getLatitude() <= 90) {
segment.addLocation(loc); segment.addLocation(loc);
if (segmentStats.getStartTime() < 0) { if (segmentStats.getStartTime() < 0) {
@@ -400,9 +407,9 @@ public class SendToMyMaps implements Runnable {
} }
totalLocationsPrepared++; totalLocationsPrepared++;
} }
prepareTrackSegment(segment, splitTracks); prepareTrackSegment(segment, splitTracks);
return splitTracks; return splitTracks;
} }
@@ -425,13 +432,13 @@ public class SendToMyMaps implements Runnable {
&& segment.getLocations().size() > 0) { && segment.getLocations().size() > 0) {
segmentStats.setStopTime(segment.getLocations().size() - 1); segmentStats.setStopTime(segment.getLocations().size() - 1);
} }
/* /*
* Decimate to 2 meter precision. Mapshop doesn't like too many * Decimate to 2 meter precision. Mapshop doesn't like too many
* points: * points:
*/ */
LocationUtils.decimate(segment, 2.0); LocationUtils.decimate(segment, 2.0);
/* It the track still has > 500 points, split it in pieces: */ /* It the track still has > 500 points, split it in pieces: */
if (segment.getLocations().size() > 500) { if (segment.getLocations().size() > 500) {
splitTracks.addAll(LocationUtils.split(segment, 500)); splitTracks.addAll(LocationUtils.split(segment, 500));
@@ -16,10 +16,13 @@
package com.google.android.apps.mytracks.io.file; package com.google.android.apps.mytracks.io.file;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.Track;
import android.content.Context; import android.content.Context;
import android.util.Log;
/** /**
* A factory to produce track writers for any format. * A factory to produce track writers for any format.
@@ -90,6 +93,11 @@ public class TrackWriterFactory {
MyTracksProviderUtils providerUtils, MyTracksProviderUtils providerUtils,
long trackId, TrackFileFormat format) { long trackId, TrackFileFormat format) {
Track track = providerUtils.getTrack(trackId); Track track = providerUtils.getTrack(trackId);
if (track == null) {
Log.w(TAG, "Trying to create a writer for an invalid track, id=" + trackId);
return null;
}
return newWriter(context, providerUtils, track, format); return newWriter(context, providerUtils, track, format);
} }
@@ -1,12 +1,12 @@
/* /*
* Copyright 2008 Google Inc. * Copyright 2008 Google Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not * Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of * use this file except in compliance with the License. You may obtain a copy of
* the License at * the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
@@ -40,7 +40,7 @@ import java.util.NoSuchElementException;
public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils { public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
private final ContentResolver contentResolver; private final ContentResolver contentResolver;
private int defaultCursorBatchSize = 2000; private int defaultCursorBatchSize = 2000;
public MyTracksProviderUtilsImpl(ContentResolver contentResolver) { public MyTracksProviderUtilsImpl(ContentResolver contentResolver) {
@@ -199,12 +199,12 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
public final int idxLatitude; public final int idxLatitude;
public final int idxLongitude; public final int idxLongitude;
public final int idxAltitude; public final int idxAltitude;
public final int idxTime; public final int idxTime;
public final int idxBearing; public final int idxBearing;
public final int idxAccuracy; public final int idxAccuracy;
public final int idxSpeed; public final int idxSpeed;
public final int idxSensor; public final int idxSensor;
public CachedTrackColumnIndices(Cursor cursor) { public CachedTrackColumnIndices(Cursor cursor) {
idxId = cursor.getColumnIndex(TrackPointsColumns._ID); idxId = cursor.getColumnIndex(TrackPointsColumns._ID);
idxLatitude = cursor.getColumnIndexOrThrow(TrackPointsColumns.LATITUDE); idxLatitude = cursor.getColumnIndexOrThrow(TrackPointsColumns.LATITUDE);
@@ -217,11 +217,11 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
idxSensor = cursor.getColumnIndexOrThrow(TrackPointsColumns.SENSOR); idxSensor = cursor.getColumnIndexOrThrow(TrackPointsColumns.SENSOR);
} }
} }
private void fillLocation(Cursor cursor, CachedTrackColumnIndices columnIndices, private void fillLocation(Cursor cursor, CachedTrackColumnIndices columnIndices,
Location location) { Location location) {
location.reset(); location.reset();
if (!cursor.isNull(columnIndices.idxLatitude)) { if (!cursor.isNull(columnIndices.idxLatitude)) {
location.setLatitude(1. * cursor.getInt(columnIndices.idxLatitude) / 1E6); location.setLatitude(1. * cursor.getInt(columnIndices.idxLatitude) / 1E6);
} }
@@ -256,7 +256,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
} }
} }
} }
@Override @Override
public void fillLocation(Cursor cursor, Location location) { public void fillLocation(Cursor cursor, Location location) {
CachedTrackColumnIndices columnIndicies = new CachedTrackColumnIndices(cursor); CachedTrackColumnIndices columnIndicies = new CachedTrackColumnIndices(cursor);
@@ -660,6 +660,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public Waypoint getFirstWaypoint(long trackId) { public Waypoint getFirstWaypoint(long trackId) {
if (trackId <= 0) {
return null;
}
Cursor cursor = contentResolver.query( Cursor cursor = contentResolver.query(
WaypointsColumns.CONTENT_URI, WaypointsColumns.CONTENT_URI,
null /*projection*/, null /*projection*/,
@@ -682,6 +686,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public Waypoint getWaypoint(long waypointId) { public Waypoint getWaypoint(long waypointId) {
if (waypointId < 0) {
return null;
}
Cursor cursor = contentResolver.query( Cursor cursor = contentResolver.query(
WaypointsColumns.CONTENT_URI, WaypointsColumns.CONTENT_URI,
null /*projection*/, null /*projection*/,
@@ -704,6 +712,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public long getLastLocationId(long trackId) { public long getLastLocationId(long trackId) {
if (trackId < 0) {
return -1;
}
final String[] projection = {"_id"}; final String[] projection = {"_id"};
Cursor cursor = contentResolver.query( Cursor cursor = contentResolver.query(
TrackPointsColumns.CONTENT_URI, TrackPointsColumns.CONTENT_URI,
@@ -728,6 +740,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public long getFirstWaypointId(long trackId) { public long getFirstWaypointId(long trackId) {
if (trackId < 0) {
return -1;
}
final String[] projection = {"_id"}; final String[] projection = {"_id"};
Cursor cursor = contentResolver.query( Cursor cursor = contentResolver.query(
WaypointsColumns.CONTENT_URI, WaypointsColumns.CONTENT_URI,
@@ -752,6 +768,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public long getLastWaypointId(long trackId) { public long getLastWaypointId(long trackId) {
if (trackId < 0) {
return -1;
}
final String[] projection = {"_id"}; final String[] projection = {"_id"};
Cursor cursor = contentResolver.query( Cursor cursor = contentResolver.query(
WaypointsColumns.CONTENT_URI, WaypointsColumns.CONTENT_URI,
@@ -815,6 +835,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public Location getLocation(long id) { public Location getLocation(long id) {
if (id < 0) {
return null;
}
String selection = TrackPointsColumns._ID + "=" + id; String selection = TrackPointsColumns._ID + "=" + id;
return findLocationBy(selection); return findLocationBy(selection);
} }
@@ -822,7 +846,11 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public Cursor getLocationsCursor(long trackId, long minTrackPointId, public Cursor getLocationsCursor(long trackId, long minTrackPointId,
int maxLocations, boolean descending) { int maxLocations, boolean descending) {
String selection; if (trackId < 0 || maxLocations < 1) {
return null;
}
String selection;
if (minTrackPointId >= 0) { if (minTrackPointId >= 0) {
selection = String.format("%s=%d AND %s%s%d", selection = String.format("%s=%d AND %s%s%d",
TrackPointsColumns.TRACKID, trackId, TrackPointsColumns._ID, TrackPointsColumns.TRACKID, trackId, TrackPointsColumns._ID,
@@ -842,6 +870,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public Cursor getWaypointsCursor(long trackId, long minWaypointId, public Cursor getWaypointsCursor(long trackId, long minWaypointId,
int maxWaypoints) { int maxWaypoints) {
if (trackId < 0 || maxWaypoints < 1) {
return null;
}
String selection; String selection;
if (minWaypointId > 0) { if (minWaypointId > 0) {
selection = String.format("%s=%d AND %s>=%d", selection = String.format("%s=%d AND %s>=%d",
@@ -863,6 +895,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public Track getTrack(long id) { public Track getTrack(long id) {
if (id < 0) {
return null;
}
String select = TracksColumns._ID + "=" + id; String select = TracksColumns._ID + "=" + id;
return findTrackBy(select); return findTrackBy(select);
} }
@@ -929,6 +965,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
@Override @Override
public boolean trackExists(long id) { public boolean trackExists(long id) {
if (id < 0) {
return false;
}
Cursor cursor = null; Cursor cursor = null;
try { try {
final String[] projection = { TracksColumns._ID }; final String[] projection = { TracksColumns._ID };
@@ -967,7 +1007,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
private Cursor cursor = getCursor(startTrackPointId); private Cursor cursor = getCursor(startTrackPointId);
private final CachedTrackColumnIndices columnIndices = cursor != null ? private final CachedTrackColumnIndices columnIndices = cursor != null ?
new CachedTrackColumnIndices(cursor) : null; new CachedTrackColumnIndices(cursor) : null;
private Cursor getCursor(long trackPointId) { private Cursor getCursor(long trackPointId) {
return getLocationsCursor(trackId, trackPointId, defaultCursorBatchSize, descending); return getLocationsCursor(trackId, trackPointId, defaultCursorBatchSize, descending);
} }
@@ -979,7 +1019,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
cursor = getCursor(pointId); cursor = getCursor(pointId);
return cursor != null; return cursor != null;
} }
@Override @Override
public long getLocationId() { public long getLocationId() {
return lastTrackPointId; return lastTrackPointId;
@@ -999,7 +1039,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
return cursor.getCount() == defaultCursorBatchSize && return cursor.getCount() == defaultCursorBatchSize &&
advanceCursorToNextBatch() && !cursor.isAfterLast(); advanceCursorToNextBatch() && !cursor.isAfterLast();
} }
return true; return true;
} }
@@ -1009,11 +1049,11 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
!(cursor.moveToNext() || advanceCursorToNextBatch() || cursor.moveToNext())) { !(cursor.moveToNext() || advanceCursorToNextBatch() || cursor.moveToNext())) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
lastTrackPointId = cursor.getLong(columnIndices.idxId); lastTrackPointId = cursor.getLong(columnIndices.idxId);
Location location = locationFactory.createLocation(); Location location = locationFactory.createLocation();
fillLocation(cursor, columnIndices, location); fillLocation(cursor, columnIndices, location);
return location; return location;
} }
@@ -1031,7 +1071,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
} }
}; };
} }
// @VisibleForTesting // @VisibleForTesting
void setDefaultCursorBatchSize(int defaultCursorBatchSize) { void setDefaultCursorBatchSize(int defaultCursorBatchSize) {
this.defaultCursorBatchSize = defaultCursorBatchSize; this.defaultCursorBatchSize = defaultCursorBatchSize;