();
+
+ for (int i = 0; i < locationsCount; i++) {
+ locationsCursor.moveToPosition(i);
+
+ Location location = myTracksProviderUtils.createLocation(locationsCursor);
+ locations.add(location);
+
+ int readCount = i + 1;
+
+ if (readCount == 1) {
+ // Create a start marker
+ String name = context.getString(R.string.marker_label_start, track.getName());
+ if (!createNewPoint(name, "", location, MARKER_TYPE_START)) {
+ Log.d(TAG, "Unable to create the start marker");
+ return false;
+ }
+ }
+
+ // Add to the distances and elevations vectors
+ if (LocationUtils.isValidLocation(location)) {
+ // All points go into the smoothing buffer
+ elevationBuffer.setNext(metricUnits ? location.getAltitude()
+ : location.getAltitude() * UnitConversions.M_TO_FT);
+ if (lastLocation != null) {
+ totalDistance += (double) lastLocation.distanceTo(location);
+ }
+ if (readCount % elevationSamplingFrequency == 0) {
+ distances.add(totalDistance);
+ elevations.add(elevationBuffer.getAverage());
+ }
+ }
+
+ // Upload periodically
+ if (readCount % MAX_POINTS_PER_UPLOAD == MAX_POINTS_PER_UPLOAD) {
+ if (!prepareAndUploadPoints(track, locations, false)) {
+ Log.d(TAG, "Unable to upload points");
+ return false;
+ }
+ updateProgress(readCount, locationsCount);
+ locations.clear();
+ }
+ lastLocation = location;
+ }
+
+ // Do a final upload with the remaining locations
+ if (!prepareAndUploadPoints(track, locations, true)) {
+ Log.d(TAG, "Unable to upload points");
+ return false;
+ }
+
+ // Create an end marker
+ if (lastLocation != null) {
+ StringUtils stringUtils = new StringUtils(context);
+ track.setDescription("" + track.getDescription() + "
"
+ + stringUtils.generateTrackDescription(track, distances, elevations) + "
");
+ String name = context.getString(R.string.marker_label_end, track.getName());
+ if (!createNewPoint(name, track.getDescription(), lastLocation, MARKER_TYPE_END)) {
+ Log.d(TAG, "Unable to create the end marker");
+ return false;
+ }
+ }
+
+ return true;
+ } finally {
+ if (locationsCursor != null) {
+ locationsCursor.close();
+ }
+ }
+ }
+
+ /**
+ * Prepares and uploads a list of locations from a track.
+ *
+ * @param track the track
+ * @param locations the locations from the track
+ * @param lastBatch true if it is the last batch of locations
+ */
+ private boolean prepareAndUploadPoints(Track track, List locations, boolean lastBatch) {
+ int numLocations = locations.size();
+ if (numLocations < 2) {
+ Log.d(TAG, "Not preparing/uploading, too few points");
+ return true;
+ }
+
+ // Prepare locations
+ ArrayList