(MAX_POINTS_PER_UPLOAD);
+ progressIndicator.setProgressMessage(
+ R.string.progress_message_reading_track);
+ Location lastLocation = null;
+ do {
+ if (totalLocationsRead % 100 == 0) {
+ updateProgress();
+ }
+
+ Location loc = providerUtils.createLocation(locationsCursor);
+ locations.add(loc);
+
+ if (totalLocationsRead == 0) {
+ // Put a marker at the first point of the first valid segment:
+ mapsClient.uploadMarker(mapId, track.getName(), track.getDescription(), loc, true);
+ }
+
+ // Add to the elevation profile.
+ if (loc != null && MyTracksUtils.isValidLocation(loc)) {
+ // All points go into the smoothing buffer...
+ elevationBuffer.setNext(metricUnits ? loc.getAltitude()
+ : loc.getAltitude() * UnitConversions.M_TO_FT);
+ if (lastLocation != null) {
+ double dist = lastLocation.distanceTo(loc);
+ totalDistance += dist;
+ }
+
+ // ...but only a few points are really used to keep the url short.
+ if (totalLocationsRead % elevationSamplingFrequency == 0) {
+ distances.add(totalDistance);
+ elevations.add(elevationBuffer.getAverage());
+ }
+ }
+
+ // 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
+ // for in the next iteration.
+ lastLocation = loc;
+
+ // Every now and then, upload the accumulated points
+ if (totalLocationsRead % MAX_POINTS_PER_UPLOAD
+ == MAX_POINTS_PER_UPLOAD - 1) {
+ if (!prepareAndUploadPoints(track, locations)) {
+ return false;
+ }
+ }
+
+ totalLocationsRead++;
+ } while (locationsCursor.moveToNext());
+
+ // Do a final upload with what's left
+ if (!prepareAndUploadPoints(track, locations)) {
+ return false;
+ }
+
+ // Put an end marker at the last point of the last valid segment:
+ if (lastLocation != null) {
+ track.setDescription("" + originalDescription + "
"
+ + stringUtils.generateTrackDescription(
+ track, distances, elevations)
+ + "
");
+ return mapsClient.uploadMarker(mapId, track.getName(), track.getDescription(),
+ lastLocation, false);
+ }
+
+ return true;
+ } finally {
+ locationsCursor.close();
+ }
+ }
+
+
+ private boolean prepareAndUploadPoints(Track track, List locations) {
+ progressIndicator.setProgressMessage(
+ R.string.progress_message_preparing_track);
+ updateProgress();
+
+ int numLocations = locations.size();
+ if (numLocations < 2) {
+ Log.d(MyTracksConstants.TAG, "Not preparing/uploading too few points");
+ totalLocationsUploaded += numLocations;
+ return true;
+ }
+
+ // Prepare/pre-process the points
+ ArrayList