From 95b7c701a2e815f5abafa413240c45b0864c576b Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sun, 15 Sep 2019 00:14:15 +0200 Subject: [PATCH] Cleanup: do not assume first waypoint contains (always) TripStatistics. --- .../opentracks/MarkerDetailActivity.java | 25 +++++++++++-------- .../opentracks/content/Waypoint.java | 4 +++ .../io/file/exporter/FileTrackExporter.java | 8 ++++-- .../io/file/exporter/KmzTrackExporter.java | 8 +++--- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/MarkerDetailActivity.java b/src/main/java/de/dennisguse/opentracks/MarkerDetailActivity.java index e890bbd84..2d62b9e4e 100644 --- a/src/main/java/de/dennisguse/opentracks/MarkerDetailActivity.java +++ b/src/main/java/de/dennisguse/opentracks/MarkerDetailActivity.java @@ -20,12 +20,14 @@ import android.database.Cursor; import android.os.Bundle; import android.util.Log; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.viewpager.widget.ViewPager; import java.util.ArrayList; +import java.util.List; import de.dennisguse.opentracks.content.ContentProviderUtils; import de.dennisguse.opentracks.content.Waypoint; @@ -35,6 +37,8 @@ import de.dennisguse.opentracks.fragments.MarkerDetailFragment; /** * An activity to display marker detail info. * + * Allows to swipe to the next and previous marker. + * * @author Leif Hendrik Wilden */ public class MarkerDetailActivity extends AbstractActivity implements DeleteMarkerCaller { @@ -43,7 +47,7 @@ public class MarkerDetailActivity extends AbstractActivity implements DeleteMark private static final String TAG = MarkerDetailActivity.class.getSimpleName(); - private ArrayList markerIds; + private List markerIds; @Override protected void onCreate(Bundle bundle) { @@ -64,17 +68,17 @@ public class MarkerDetailActivity extends AbstractActivity implements DeleteMark try (Cursor cursor = contentProviderUtils.getWaypointCursor(waypoint.getTrackId(), -1L, -1)) { if (cursor != null && cursor.moveToFirst()) { - /* - * Yes, this will skip the first waypoint and that is intentional as the - * first waypoint holds the stats for the track. - */ - while (cursor.moveToNext()) { - Waypoint current = contentProviderUtils.createWaypoint(cursor); + for (int i = 0; i < cursor.getCount(); i++) { + Waypoint currentMarker = contentProviderUtils.createWaypoint(cursor); - markerIds.add(current.getId()); - if (current.getId() == markerId) { - markerIndex = markerIds.size() - 1; + if (!currentMarker.isTripStatistics()) { + markerIds.add(currentMarker.getId()); + if (currentMarker.getId() == markerId) { + markerIndex = markerIds.size() - 1; + } } + + cursor.moveToNext(); } } } @@ -113,6 +117,7 @@ public class MarkerDetailActivity extends AbstractActivity implements DeleteMark } @Override + @NonNull public Fragment getItem(int position) { String title = getString(R.string.marker_title, position + 1, getCount()); return MarkerDetailFragment.newInstance(markerIds.get(position), title); diff --git a/src/main/java/de/dennisguse/opentracks/content/Waypoint.java b/src/main/java/de/dennisguse/opentracks/content/Waypoint.java index d2324cd92..87984f996 100644 --- a/src/main/java/de/dennisguse/opentracks/content/Waypoint.java +++ b/src/main/java/de/dennisguse/opentracks/content/Waypoint.java @@ -159,6 +159,10 @@ public final class Waypoint { this.location = location; } + public boolean isTripStatistics() { + return tripStatistics != null; + } + public TripStatistics getTripStatistics() { return tripStatistics; } diff --git a/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java b/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java index f098eadff..891bd9559 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java @@ -99,7 +99,7 @@ public class FileTrackExporter implements TrackExporter { try (Cursor cursor = contentProviderUtils.getWaypointCursor(track.getId(), -1L, ContentProviderUtils.MAX_LOADED_WAYPOINTS_POINTS)) { if (cursor != null && cursor.moveToFirst()) { // Intentionally skip first waypoint (contains statistics). - while (cursor.moveToNext()) { + for (int i = 0; i < cursor.getCount(); i++) { if (Thread.interrupted()) { throw new InterruptedException(); } @@ -108,7 +108,11 @@ public class FileTrackExporter implements TrackExporter { hasWaypoints = true; } Waypoint waypoint = contentProviderUtils.createWaypoint(cursor); - trackWriter.writeWaypoint(waypoint); + if (!waypoint.isTripStatistics()) { + trackWriter.writeWaypoint(waypoint); + } + + cursor.moveToNext(); } } } diff --git a/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmzTrackExporter.java b/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmzTrackExporter.java index 2a0de52ff..a9d5ec835 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmzTrackExporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/exporter/KmzTrackExporter.java @@ -103,11 +103,7 @@ public class KmzTrackExporter implements TrackExporter { for (Track track : tracks) { try (Cursor cursor = contentProviderUtils.getWaypointCursor(track.getId(), -1L, -1)) { if (cursor != null && cursor.moveToFirst()) { - /* - * Yes, this will skip the first waypoint and that is intentional as - * the first waypoint holds the stats for the track. - */ - while (cursor.moveToNext()) { + for (int i = 0; i < cursor.getCount(); i++) { if (Thread.interrupted()) { throw new InterruptedException(); } @@ -115,6 +111,8 @@ public class KmzTrackExporter implements TrackExporter { if (waypoint.hasPhoto()) { addImage(context, zipOutputStream, waypoint.getPhotoUrl()); } + + cursor.moveToNext(); } } }