(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:
- uploadMarker(track, 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 uploadMarker(track, lastLocation, false);
- }
-
- return true;
- } finally {
- locationsCursor.close();
- }
- }
-
- private boolean uploadMarker(final Track track,
- final Location location,
- final boolean isStart) {
- boolean okay = wrapper.runQuery(new QueryFunction() {
- @Override
- public void query(MapsClient client)
- throws AuthenticationException, IOException, Exception {
- String featureFeed = MapsClient.getFeaturesFeed(mapId);
- insertMarker(context, client, featureFeed, track, location, isStart);
- }
- });
- return okay;
- }
-
- /**
- * Inserts a place mark. Second try if 1st try fails. Will throw exception on
- * 2nd failure.
- */
- private void insertMarker(Context context, MapsClient client,
- String featureFeed, Track track, Location loc,
- boolean isStart) throws IOException, Exception {
- Entry entry = gdataConverter.getEntryForFeature(
- buildMyMapsPlacemarkFeature(context, track, loc, isStart));
- Log.d(MyTracksConstants.TAG, "SendToMyMaps: Creating placemark "
- + entry.getTitle());
- try {
- client.createEntry(featureFeed, auth.getAuthToken(), entry);
- Log.d(MyTracksConstants.TAG, "SendToMyMaps: createEntry success!");
- } catch (IOException e) {
- Log.w(MyTracksConstants.TAG,
- "SendToMyMaps: createEntry 1st try failed. Trying again.");
- // Retry once (often IOException is thrown on a timeout):
- client.createEntry(featureFeed, auth.getAuthToken(), entry);
- Log.d(MyTracksConstants.TAG,
- "SendToMyMaps: createEntry success on 2nd try!");
- }
- }
-
- /**
- * Builds a placemark MyMapsFeature from a track.
- *
- * @param track the track
- * @param isStart true if it's the start of the track, or false for end
- * @return a MyMapsFeature
- */
- private static MyMapsFeature buildMyMapsPlacemarkFeature(Context context,
- Track track, Location loc, boolean isStart) {
- MyMapsFeature myMapsFeature = new MyMapsFeature();
- myMapsFeature.generateAndroidId();
- myMapsFeature.setType(MyMapsFeature.MARKER);
- if (isStart) {
- myMapsFeature.setIconUrl(START_ICON_URL);
- } else {
- myMapsFeature.setIconUrl(END_ICON_URL);
- }
- myMapsFeature.addPoint(MyTracksUtils.getGeoPoint(loc));
- String name = track.getName() + " "
- + (isStart ? context.getString(R.string.start)
- : context.getString(R.string.end));
- myMapsFeature.setTitle(name);
- myMapsFeature.setDescription(isStart ? "" : track.getDescription());
- return myMapsFeature;
- }
-
- 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