From e9b02df631711532c4d0845db7aca9eb4ba49793 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Thu, 28 Sep 2023 19:39:43 +0200 Subject: [PATCH 1/4] Cleanup. --- .../java/de/dennisguse/opentracks/EspressoDeleteTrackTest.java | 2 -- .../java/de/dennisguse/opentracks/EspressoUITest.java | 2 -- .../opentracks/io/file/exporter/KMLTrackExporter.java | 2 +- .../dennisguse/opentracks/services/TrackRecordingService.java | 2 +- .../de/dennisguse/opentracks/ui/markers/MarkerListAdapter.java | 3 --- 5 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/androidTest/java/de/dennisguse/opentracks/EspressoDeleteTrackTest.java b/src/androidTest/java/de/dennisguse/opentracks/EspressoDeleteTrackTest.java index 9d53df2e7c..88527c20d1 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/EspressoDeleteTrackTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/EspressoDeleteTrackTest.java @@ -1,7 +1,6 @@ package de.dennisguse.opentracks; -import static androidx.test.espresso.Espresso.onData; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.longClick; @@ -12,7 +11,6 @@ import static androidx.test.espresso.matcher.ViewMatchers.withParentIndex; import static androidx.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.anything; import static de.dennisguse.opentracks.util.EspressoUtils.childAtPosition; import static de.dennisguse.opentracks.util.EspressoUtils.waitFor; diff --git a/src/androidTest/java/de/dennisguse/opentracks/EspressoUITest.java b/src/androidTest/java/de/dennisguse/opentracks/EspressoUITest.java index 3ba3c777d0..b74fb0ed6c 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/EspressoUITest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/EspressoUITest.java @@ -1,6 +1,5 @@ package de.dennisguse.opentracks; -import static androidx.test.espresso.Espresso.onData; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.longClick; @@ -11,7 +10,6 @@ import static androidx.test.espresso.matcher.ViewMatchers.withParent; import static androidx.test.espresso.matcher.ViewMatchers.withParentIndex; import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.anything; import static de.dennisguse.opentracks.util.EspressoUtils.selectTabAtIndex; import static de.dennisguse.opentracks.util.EspressoUtils.waitFor; diff --git a/src/main/java/de/dennisguse/opentracks/io/file/exporter/KMLTrackExporter.java b/src/main/java/de/dennisguse/opentracks/io/file/exporter/KMLTrackExporter.java index 7ba3723237..3fc460d868 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/exporter/KMLTrackExporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/exporter/KMLTrackExporter.java @@ -85,7 +85,7 @@ public class KMLTrackExporter implements TrackExporter { private PrintWriter printWriter; - private ArrayList trackpointTypeList = new ArrayList<>(); + private final ArrayList trackpointTypeList = new ArrayList<>(); private final List speedList = new ArrayList<>(); private final List distanceList = new ArrayList<>(); diff --git a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java index cddd801328..e4b6fa4d99 100644 --- a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java +++ b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java @@ -249,7 +249,7 @@ public void newGpsStatus(GpsStatusValue gpsStatusValue) { if (notificationManager == null) { StringWriter writer = new StringWriter(); - Exception e = new RuntimeException("TrackRecording.newGpsStatus() called after onDestroy(); objectID: " + this + " with thread: " + Thread.currentThread().toString()); + Exception e = new RuntimeException("TrackRecording.newGpsStatus() called after onDestroy(); objectID: " + this + " with thread: " + Thread.currentThread()); e.printStackTrace(new PrintWriter(writer)); Log.e(TAG, e.getMessage() + " " + writer); diff --git a/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerListAdapter.java b/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerListAdapter.java index f02a651266..37c6e92f52 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerListAdapter.java +++ b/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerListAdapter.java @@ -14,7 +14,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; -import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; @@ -23,9 +22,7 @@ import java.io.IOException; import java.io.InputStream; -import java.time.OffsetDateTime; import java.time.ZoneOffset; -import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; From bc857faa180f1ebf5d1409206d3628ad410c40e6 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Thu, 28 Sep 2023 19:57:15 +0200 Subject: [PATCH 2/4] Cleanup: ChartPoint is a record. --- .../opentracks/chart/ChartPointTest.java | 32 ++--- .../chart/ChartValueSeriesTest.java | 32 +++-- .../opentracks/chart/ChartFragment.java | 2 +- .../opentracks/chart/ChartPoint.java | 115 ++++++------------ .../opentracks/chart/ChartView.java | 16 +-- 5 files changed, 85 insertions(+), 112 deletions(-) diff --git a/src/androidTest/java/de/dennisguse/opentracks/chart/ChartPointTest.java b/src/androidTest/java/de/dennisguse/opentracks/chart/ChartPointTest.java index 217802f58a..46dead9d52 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/chart/ChartPointTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/chart/ChartPointTest.java @@ -27,10 +27,10 @@ public void create_by_time() { statistics.setTotalTime(Duration.ofSeconds(1000)); // when - ChartPoint point = new ChartPoint(statistics, TrackStubUtils.createDefaultTrackPoint(), Speed.of(0), false, UnitSystem.IMPERIAL_FEET); + ChartPoint point = ChartPoint.create(statistics, TrackStubUtils.createDefaultTrackPoint(), Speed.of(0), false, UnitSystem.IMPERIAL_FEET); // then - assertEquals(1000000, (long) point.getTimeOrDistance()); + assertEquals(1000000, (long) point.timeOrDistance()); } @Test @@ -40,10 +40,10 @@ public void create_by_distance() { statistics.setTotalDistance(Distance.of(1000)); // when - ChartPoint point = new ChartPoint(statistics, TrackStubUtils.createDefaultTrackPoint(), Speed.of(0), true, UnitSystem.METRIC); + ChartPoint point = ChartPoint.create(statistics, TrackStubUtils.createDefaultTrackPoint(), Speed.of(0), true, UnitSystem.METRIC); // then - assertEquals(1, (long) point.getTimeOrDistance()); + assertEquals(1, (long) point.timeOrDistance()); } @Test @@ -54,12 +54,12 @@ public void create_get_altitude_speed_and_pace() { .setAltitude(Altitude.EGM2008.of(50)); // when - ChartPoint point = new ChartPoint(statistics, trackPoint, Speed.of(10), false, UnitSystem.METRIC); + ChartPoint point = ChartPoint.create(statistics, trackPoint, Speed.of(10), false, UnitSystem.METRIC); // then - assertEquals(50, point.getAltitude(), 0.01); - assertEquals(36, point.getSpeed(), 0.01); - assertEquals(1.66, point.getPace(), 0.01); + assertEquals(50, point.altitude(), 0.01); + assertEquals(36, point.speed(), 0.01); + assertEquals(1.66, point.pace(), 0.01); } @Test @@ -69,12 +69,12 @@ public void create_sensorNotAvailable() { TrackPoint trackPoint = TrackStubUtils.createDefaultTrackPoint() .setAltitude(Altitude.EGM2008.of(50)); // when - ChartPoint point = new ChartPoint(statistics, trackPoint, Speed.of(10), false, UnitSystem.METRIC); + ChartPoint point = ChartPoint.create(statistics, trackPoint, Speed.of(10), false, UnitSystem.METRIC); // then - assertNull(point.getHeartRate()); - assertNull(point.getCadence()); - assertNull(point.getPower()); + assertNull(point.heartRate()); + assertNull(point.cadence()); + assertNull(point.power()); } @Test @@ -89,11 +89,11 @@ public void create_sensorAvailable() { TrackStatistics statistics = new TrackStatistics(); // when - ChartPoint point = new ChartPoint(statistics, trackPoint, Speed.of(10), false, UnitSystem.METRIC); + ChartPoint point = ChartPoint.create(statistics, trackPoint, Speed.of(10), false, UnitSystem.METRIC); // then - assertEquals(100.0, point.getHeartRate(), 0.01); - assertEquals(101.0, point.getCadence(), 0.01); - assertEquals(102.0, point.getPower(), 0.01); + assertEquals(100.0, point.heartRate(), 0.01); + assertEquals(101.0, point.cadence(), 0.01); + assertEquals(102.0, point.power(), 0.01); } } \ No newline at end of file diff --git a/src/androidTest/java/de/dennisguse/opentracks/chart/ChartValueSeriesTest.java b/src/androidTest/java/de/dennisguse/opentracks/chart/ChartValueSeriesTest.java index 3930c2a4cc..2c4d880fec 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/chart/ChartValueSeriesTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/chart/ChartValueSeriesTest.java @@ -54,7 +54,7 @@ public void setUp() { 18) { @Override Double extractDataFromChartPoint(@NonNull ChartPoint chartPoint) { - return chartPoint.getAltitude(); + return chartPoint.altitude(); } @Override @@ -80,9 +80,9 @@ public void testEnabled() { @Test public void testVerySmallUpdates() { - series.update(new ChartPoint(1f)); - series.update(new ChartPoint(2f)); - series.update(new ChartPoint(3f)); + series.update(withAltitude(1f)); + series.update(withAltitude(2f)); + series.update(withAltitude(3f)); series.updateDimension(); assertEquals(1, series.getInterval()); assertEquals(1, series.getMinMarkerValue()); @@ -91,8 +91,8 @@ public void testVerySmallUpdates() { @Test public void testSmallUpdates() { - series.update(new ChartPoint(0)); - series.update(new ChartPoint(10)); + series.update(withAltitude(0)); + series.update(withAltitude(10)); series.updateDimension(); assertEquals(100, series.getInterval()); assertEquals(0, series.getMinMarkerValue()); @@ -101,8 +101,8 @@ public void testSmallUpdates() { @Test public void testBigUpdates() { - series.update(new ChartPoint(0)); - series.update(new ChartPoint(901)); + series.update(withAltitude(0)); + series.update(withAltitude(901)); series.updateDimension(); assertEquals(1000, series.getInterval()); assertEquals(0, series.getMinMarkerValue()); @@ -111,11 +111,23 @@ public void testBigUpdates() { @Test public void testNotZeroBasedUpdates() { - series.update(new ChartPoint(220)); - series.update(new ChartPoint(250)); + series.update(withAltitude(220)); + series.update(withAltitude(250)); series.updateDimension(); assertEquals(100, series.getInterval()); assertEquals(200, series.getMinMarkerValue()); assertEquals(700, series.getMaxMarkerValue()); } + + static ChartPoint withAltitude(double altitude) { + return new ChartPoint( + 0, + altitude, + null, + null, + null, + null, + null + ); + } } diff --git a/src/main/java/de/dennisguse/opentracks/chart/ChartFragment.java b/src/main/java/de/dennisguse/opentracks/chart/ChartFragment.java index bea47c7fe3..a847f8a26d 100644 --- a/src/main/java/de/dennisguse/opentracks/chart/ChartFragment.java +++ b/src/main/java/de/dennisguse/opentracks/chart/ChartFragment.java @@ -188,7 +188,7 @@ public void clearTrackPoints() { public void onSampledInTrackPoint(@NonNull TrackPoint trackPoint, @NonNull TrackStatistics trackStatistics) { if (isResumed()) { - ChartPoint point = new ChartPoint(trackStatistics, trackPoint, trackPoint.getSpeed(), chartByDistance, viewBinding.chartView.getUnitSystem()); + ChartPoint point = ChartPoint.create(trackStatistics, trackPoint, trackPoint.getSpeed(), chartByDistance, viewBinding.chartView.getUnitSystem()); pendingPoints.add(point); } } diff --git a/src/main/java/de/dennisguse/opentracks/chart/ChartPoint.java b/src/main/java/de/dennisguse/opentracks/chart/ChartPoint.java index af44edb129..9ceb9003e8 100644 --- a/src/main/java/de/dennisguse/opentracks/chart/ChartPoint.java +++ b/src/main/java/de/dennisguse/opentracks/chart/ChartPoint.java @@ -1,7 +1,6 @@ package de.dennisguse.opentracks.chart; import androidx.annotation.NonNull; -import androidx.annotation.VisibleForTesting; import de.dennisguse.opentracks.data.models.Distance; import de.dennisguse.opentracks.data.models.Speed; @@ -9,81 +8,43 @@ import de.dennisguse.opentracks.settings.UnitSystem; import de.dennisguse.opentracks.stats.TrackStatistics; -public class ChartPoint { - //X-axis - private double timeOrDistance; - - //Y-axis - private Double altitude; - private Double speed; - private Double pace; - private Double heartRate; - private Double cadence; - private Double power; - - @Deprecated - @VisibleForTesting - ChartPoint(double altitude) { - this.altitude = altitude; - } - - public ChartPoint(@NonNull TrackStatistics trackStatistics, @NonNull TrackPoint trackPoint, Speed smoothedSpeed, boolean chartByDistance, UnitSystem unitSystem) { - if (chartByDistance) { - timeOrDistance = trackStatistics.getTotalDistance().toKM_Miles(unitSystem); - } else { - timeOrDistance = trackStatistics.getTotalTime().toMillis(); - } - - if (trackPoint.hasAltitude()) { - altitude = Distance.of(trackPoint.getAltitude().toM()).toM_FT(unitSystem); - } - - if (smoothedSpeed != null) { - speed = smoothedSpeed.to(unitSystem); - pace = smoothedSpeed.toPace(unitSystem).toSeconds() / 60d; - } - if (trackPoint.hasHeartRate()) { - heartRate = (double) trackPoint.getHeartRate().getBPM(); - } - if (trackPoint.hasCadence()) { - cadence = (double) trackPoint.getCadence().getRPM(); - } - if (trackPoint.hasPower()) { - power = (double) trackPoint.getPower().getW(); - } - } - - public double getTimeOrDistance() { - return timeOrDistance; - } - - public Double getAltitude() { - return altitude; - } - - public Double getSpeed() { - return speed; - } - - public Double getPace() { - return pace; - } - - public Double getHeartRate() { - return heartRate; - } - - public Double getCadence() { - return cadence; - } - - public Double getPower() { - return power; - } - - @NonNull - @Override - public String toString() { - return "ChartPoint{" + "timeOrDistance=" + timeOrDistance + '}'; +public record ChartPoint( + //X-axis + double timeOrDistance, + + //Y-axis + Double altitude, + Double speed, + Double pace, + Double heartRate, + Double cadence, + Double power +) { + + + public static ChartPoint create(@NonNull TrackStatistics trackStatistics, @NonNull TrackPoint trackPoint, Speed smoothedSpeed, boolean chartByDistance, UnitSystem unitSystem) { + return new ChartPoint( + chartByDistance + ? trackStatistics.getTotalDistance().toKM_Miles(unitSystem) + : trackStatistics.getTotalTime().toMillis(), + trackPoint.hasAltitude() + ? Distance.of(trackPoint.getAltitude().toM()).toM_FT(unitSystem) + : null, + smoothedSpeed != null + ? smoothedSpeed.to(unitSystem) + : null, + smoothedSpeed != null + ? smoothedSpeed.toPace(unitSystem).toSeconds() / 60d + : null, + trackPoint.hasHeartRate() + ? (double) trackPoint.getHeartRate().getBPM() + : null, + trackPoint.hasCadence() + ? (double) trackPoint.getCadence().getRPM() + : null, + trackPoint.hasPower() + ? (double) trackPoint.getPower().getW() + : null + ); } } diff --git a/src/main/java/de/dennisguse/opentracks/chart/ChartView.java b/src/main/java/de/dennisguse/opentracks/chart/ChartView.java index a9ab2ce086..b6430d34cb 100644 --- a/src/main/java/de/dennisguse/opentracks/chart/ChartView.java +++ b/src/main/java/de/dennisguse/opentracks/chart/ChartView.java @@ -210,7 +210,7 @@ public ChartView(Context context, AttributeSet attributeSet) { fontSizeMedium) { @Override protected Double extractDataFromChartPoint(@NonNull ChartPoint chartPoint) { - return chartPoint.getAltitude(); + return chartPoint.altitude(); } @Override @@ -232,7 +232,7 @@ protected boolean drawIfChartPointHasNoData() { fontSizeMedium) { @Override protected Double extractDataFromChartPoint(@NonNull ChartPoint chartPoint) { - return chartPoint.getSpeed(); + return chartPoint.speed(); } @Override @@ -255,7 +255,7 @@ protected boolean drawIfChartPointHasNoData() { fontSizeMedium) { @Override protected Double extractDataFromChartPoint(@NonNull ChartPoint chartPoint) { - return chartPoint.getPace(); + return chartPoint.pace(); } @Override @@ -278,7 +278,7 @@ protected boolean drawIfChartPointHasNoData() { fontSizeMedium) { @Override protected Double extractDataFromChartPoint(@NonNull ChartPoint chartPoint) { - return chartPoint.getHeartRate(); + return chartPoint.heartRate(); } @Override @@ -300,7 +300,7 @@ protected boolean drawIfChartPointHasNoData() { fontSizeMedium) { @Override protected Double extractDataFromChartPoint(@NonNull ChartPoint chartPoint) { - return chartPoint.getCadence(); + return chartPoint.cadence(); } @Override @@ -321,7 +321,7 @@ protected boolean drawIfChartPointHasNoData() { fontSizeMedium) { @Override protected Double extractDataFromChartPoint(@NonNull ChartPoint chartPoint) { - return chartPoint.getPower(); + return chartPoint.power(); } @Override @@ -425,7 +425,7 @@ public void addChartPoints(List dataPoints) { synchronized (chartPoints) { chartPoints.addAll(dataPoints); for (ChartPoint dataPoint : dataPoints) { - xExtremityMonitor.update(dataPoint.getTimeOrDistance()); + xExtremityMonitor.update(dataPoint.timeOrDistance()); for (ChartValueSeries i : seriesList) { i.update(dataPoint); } @@ -887,7 +887,7 @@ private void updateSerie(ChartValueSeries series) { } double value = series.extractDataFromChartPoint(point); - int x = getX(point.getTimeOrDistance()); + int x = getX(point.timeOrDistance()); int y = getY(series, value); // start from lower left corner From 684756c264a464441b67ee47fd3ec10fa0b24ef2 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Thu, 28 Sep 2023 20:00:47 +0200 Subject: [PATCH 3/4] Cleanup: SelectionData is a record. --- .../opentracks/data/TrackSelectionTest.java | 94 +++++++++---------- .../opentracks/data/ContentProviderUtils.java | 2 +- .../opentracks/data/SelectionData.java | 23 +---- 3 files changed, 53 insertions(+), 66 deletions(-) diff --git a/src/androidTest/java/de/dennisguse/opentracks/data/TrackSelectionTest.java b/src/androidTest/java/de/dennisguse/opentracks/data/TrackSelectionTest.java index 8724e9c69a..98aff7b151 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/data/TrackSelectionTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/data/TrackSelectionTest.java @@ -23,8 +23,8 @@ public void testFilterBuildSelection_empty() { SelectionData selection = filter.buildSelection(); // Then - assertNull(selection.getSelection()); - assertNull(selection.getSelectionArgs()); + assertNull(selection.selection()); + assertNull(selection.selectionArgs()); } @Test @@ -37,9 +37,9 @@ public void testFilterBuildSelection_onlyOneTrackId() { SelectionData selection = filter.buildSelection(); // Then - assertEquals(selection.getSelection(), "_id IN (?)"); - assertEquals(selection.getSelectionArgs().length, 1); - assertEquals(selection.getSelectionArgs()[0], "1"); + assertEquals(selection.selection(), "_id IN (?)"); + assertEquals(selection.selectionArgs().length, 1); + assertEquals(selection.selectionArgs()[0], "1"); } @Test @@ -57,11 +57,11 @@ public void testFilterBuildSelection_severalTracksId() { SelectionData selection = filter.buildSelection(); // Then - assertEquals(selection.getSelection(), "_id IN (?,?,?)"); - assertEquals(selection.getSelectionArgs().length, 3); - assertEquals(selection.getSelectionArgs()[0], "1"); - assertEquals(selection.getSelectionArgs()[1], "2"); - assertEquals(selection.getSelectionArgs()[2], "3"); + assertEquals(selection.selection(), "_id IN (?,?,?)"); + assertEquals(selection.selectionArgs().length, 3); + assertEquals(selection.selectionArgs()[0], "1"); + assertEquals(selection.selectionArgs()[1], "2"); + assertEquals(selection.selectionArgs()[2], "3"); } @Test @@ -73,9 +73,9 @@ public void testFilterBuildSelection_onlyOneCategory() { SelectionData selection = filter.buildSelection(); // Then - assertEquals(selection.getSelection(), "category IN (?)"); - assertEquals(selection.getSelectionArgs().length, 1); - assertEquals(selection.getSelectionArgs()[0], "running"); + assertEquals(selection.selection(), "category IN (?)"); + assertEquals(selection.selectionArgs().length, 1); + assertEquals(selection.selectionArgs()[0], "running"); } @Test @@ -91,12 +91,12 @@ public void testFilterBuildSelection_severalCategories() { SelectionData selection = filter.buildSelection(); // Then - assertEquals(selection.getSelection(), "category IN (?,?,?,?)"); - assertEquals(selection.getSelectionArgs().length, 4); - assertEquals(selection.getSelectionArgs()[0], "running"); - assertEquals(selection.getSelectionArgs()[1], "road biking"); - assertEquals(selection.getSelectionArgs()[2], "mountain biking"); - assertEquals(selection.getSelectionArgs()[3], "trail walking"); + assertEquals(selection.selection(), "category IN (?,?,?,?)"); + assertEquals(selection.selectionArgs().length, 4); + assertEquals(selection.selectionArgs()[0], "running"); + assertEquals(selection.selectionArgs()[1], "road biking"); + assertEquals(selection.selectionArgs()[2], "mountain biking"); + assertEquals(selection.selectionArgs()[3], "trail walking"); } @Test @@ -114,16 +114,16 @@ public void testFilterBuildSelection_onlyDateRange() { SelectionData selectionOk = filterOk.buildSelection(); // Then - assertNull(selectionWrong1.getSelection()); - assertNull(selectionWrong1.getSelectionArgs()); + assertNull(selectionWrong1.selection()); + assertNull(selectionWrong1.selectionArgs()); - assertNull(selectionWrong2.getSelection()); - assertNull(selectionWrong2.getSelectionArgs()); + assertNull(selectionWrong2.selection()); + assertNull(selectionWrong2.selectionArgs()); - assertEquals(selectionOk.getSelection(), "starttime BETWEEN ? AND ?"); - assertEquals(selectionOk.getSelectionArgs().length, 2); - assertEquals(selectionOk.getSelectionArgs()[0], Long.toString(instant.toEpochMilli())); - assertEquals(selectionOk.getSelectionArgs()[1], Long.toString(instant.toEpochMilli() + oneDay)); + assertEquals(selectionOk.selection(), "starttime BETWEEN ? AND ?"); + assertEquals(selectionOk.selectionArgs().length, 2); + assertEquals(selectionOk.selectionArgs()[0], Long.toString(instant.toEpochMilli())); + assertEquals(selectionOk.selectionArgs()[1], Long.toString(instant.toEpochMilli() + oneDay)); } @Test @@ -143,13 +143,13 @@ public void testFilterBuildSelection_tracksId_and_categories() { SelectionData selection = filter.buildSelection(); // Then - assertEquals(selection.getSelection(), "_id IN (?,?,?) AND category IN (?,?)"); - assertEquals(selection.getSelectionArgs().length, 5); - assertEquals(selection.getSelectionArgs()[0], "1"); - assertEquals(selection.getSelectionArgs()[1], "2"); - assertEquals(selection.getSelectionArgs()[2], "3"); - assertEquals(selection.getSelectionArgs()[3], "running"); - assertEquals(selection.getSelectionArgs()[4], "road biking"); + assertEquals(selection.selection(), "_id IN (?,?,?) AND category IN (?,?)"); + assertEquals(selection.selectionArgs().length, 5); + assertEquals(selection.selectionArgs()[0], "1"); + assertEquals(selection.selectionArgs()[1], "2"); + assertEquals(selection.selectionArgs()[2], "3"); + assertEquals(selection.selectionArgs()[3], "running"); + assertEquals(selection.selectionArgs()[4], "road biking"); } @Test @@ -172,13 +172,13 @@ public void testFilterBuildSelection_tracksId_and_dateRange() { SelectionData selection = filter.buildSelection(); // Then - assertEquals(selection.getSelection(), "_id IN (?,?,?) AND starttime BETWEEN ? AND ?"); - assertEquals(selection.getSelectionArgs().length, 5); - assertEquals(selection.getSelectionArgs()[0], "1"); - assertEquals(selection.getSelectionArgs()[1], "2"); - assertEquals(selection.getSelectionArgs()[2], "3"); - assertEquals(selection.getSelectionArgs()[3], Long.toString(instant.toEpochMilli())); - assertEquals(selection.getSelectionArgs()[4], Long.toString(instant.toEpochMilli() + oneDay)); + assertEquals(selection.selection(), "_id IN (?,?,?) AND starttime BETWEEN ? AND ?"); + assertEquals(selection.selectionArgs().length, 5); + assertEquals(selection.selectionArgs()[0], "1"); + assertEquals(selection.selectionArgs()[1], "2"); + assertEquals(selection.selectionArgs()[2], "3"); + assertEquals(selection.selectionArgs()[3], Long.toString(instant.toEpochMilli())); + assertEquals(selection.selectionArgs()[4], Long.toString(instant.toEpochMilli() + oneDay)); } @Test @@ -196,11 +196,11 @@ public void testFilterBuildSelection_categories_and_dateRange() { SelectionData selection = filter.buildSelection(); // Then - assertEquals(selection.getSelection(), "category IN (?,?) AND starttime BETWEEN ? AND ?"); - assertEquals(selection.getSelectionArgs().length, 4); - assertEquals(selection.getSelectionArgs()[0], "running"); - assertEquals(selection.getSelectionArgs()[1], "road biking"); - assertEquals(selection.getSelectionArgs()[2], Long.toString(instant.toEpochMilli())); - assertEquals(selection.getSelectionArgs()[3], Long.toString(instant.toEpochMilli() + oneDay)); + assertEquals(selection.selection(), "category IN (?,?) AND starttime BETWEEN ? AND ?"); + assertEquals(selection.selectionArgs().length, 4); + assertEquals(selection.selectionArgs()[0], "running"); + assertEquals(selection.selectionArgs()[1], "road biking"); + assertEquals(selection.selectionArgs()[2], Long.toString(instant.toEpochMilli())); + assertEquals(selection.selectionArgs()[3], Long.toString(instant.toEpochMilli() + oneDay)); } } diff --git a/src/main/java/de/dennisguse/opentracks/data/ContentProviderUtils.java b/src/main/java/de/dennisguse/opentracks/data/ContentProviderUtils.java index 4d1d9a76c8..5e24f1027f 100644 --- a/src/main/java/de/dennisguse/opentracks/data/ContentProviderUtils.java +++ b/src/main/java/de/dennisguse/opentracks/data/ContentProviderUtils.java @@ -216,7 +216,7 @@ public List getTracks() { public List getTracks(ContentProviderSelectionInterface selection) { SelectionData selectionData = selection.buildSelection(); ArrayList tracks = new ArrayList<>(); - try (Cursor cursor = getTrackCursor(selectionData.getSelection(), selectionData.getSelectionArgs(), TracksColumns._ID)) { + try (Cursor cursor = getTrackCursor(selectionData.selection(), selectionData.selectionArgs(), TracksColumns._ID)) { if (cursor != null && cursor.moveToFirst()) { tracks.ensureCapacity(cursor.getCount()); do { diff --git a/src/main/java/de/dennisguse/opentracks/data/SelectionData.java b/src/main/java/de/dennisguse/opentracks/data/SelectionData.java index 886847b58e..f1bf97798d 100644 --- a/src/main/java/de/dennisguse/opentracks/data/SelectionData.java +++ b/src/main/java/de/dennisguse/opentracks/data/SelectionData.java @@ -1,24 +1,11 @@ package de.dennisguse.opentracks.data; -public class SelectionData { - private final String selection; - private final String[] selectionArgs; +public record SelectionData( + String selection, + String[] selectionArgs +) { public SelectionData() { - selection = null; - selectionArgs = null; - } - - public SelectionData(String selection, String[] selectionArgs) { - this.selection = selection; - this.selectionArgs = selectionArgs; - } - - public String getSelection() { - return selection; - } - - public String[] getSelectionArgs() { - return selectionArgs; + this(null, null); } } From cb3904ee0b4bf96a9d8b9d5b10ee0a88bc398b9d Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Thu, 28 Sep 2023 20:06:13 +0200 Subject: [PATCH 4/4] Cleanup: SensorStatistics is a record. --- .../data/CustomContentProviderUtilsTest.java | 60 +++++++++---------- .../fragments/StatisticsRecordedFragment.java | 12 ++-- .../announcement/VoiceAnnouncementUtils.java | 2 +- .../opentracks/stats/SensorStatistics.java | 50 +++------------- 4 files changed, 46 insertions(+), 78 deletions(-) diff --git a/src/androidTest/java/de/dennisguse/opentracks/data/CustomContentProviderUtilsTest.java b/src/androidTest/java/de/dennisguse/opentracks/data/CustomContentProviderUtilsTest.java index 48f1e26f9b..94dc1238d6 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/data/CustomContentProviderUtilsTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/data/CustomContentProviderUtilsTest.java @@ -986,13 +986,13 @@ public void testGetSensorStats_needAtLeastTwoTrackPointsTrue() { // then assertTrue(sensorStatistics.hasHeartRate()); - assertEquals(sensorStatistics.getAvgHeartRate().getBPM(), stats.avgHr, 0f); - assertEquals(sensorStatistics.getMaxHeartRate().getBPM(), stats.maxHr, 0f); + assertEquals(sensorStatistics.avgHeartRate().getBPM(), stats.avgHr, 0f); + assertEquals(sensorStatistics.maxHeartRate().getBPM(), stats.maxHr, 0f); assertTrue(sensorStatistics.hasCadence()); - assertEquals(sensorStatistics.getAvgCadence().getRPM(), stats.avgCadence, 0f); - assertEquals(sensorStatistics.getMaxCadence().getRPM(), stats.maxCadence, 0f); + assertEquals(sensorStatistics.avgCadence().getRPM(), stats.avgCadence, 0f); + assertEquals(sensorStatistics.maxCadence().getRPM(), stats.maxCadence, 0f); assertTrue(sensorStatistics.hasPower()); - assertEquals(sensorStatistics.getAvgPower().getW(), stats.avgPower, 0f); + assertEquals(sensorStatistics.avgPower().getW(), stats.avgPower, 0f); } @Test @@ -1018,8 +1018,8 @@ public void testGetSensorStats_onlyHr() { // then assertTrue(sensorStatistics.hasHeartRate()); - assertEquals(sensorStatistics.getAvgHeartRate().getBPM(), stats.avgHr, 0f); - assertEquals(sensorStatistics.getMaxHeartRate().getBPM(), stats.maxHr, 0f); + assertEquals(sensorStatistics.avgHeartRate().getBPM(), stats.avgHr, 0f); + assertEquals(sensorStatistics.maxHeartRate().getBPM(), stats.maxHr, 0f); assertFalse(sensorStatistics.hasCadence()); assertFalse(sensorStatistics.hasPower()); } @@ -1048,8 +1048,8 @@ public void testGetSensorStats_onlyCadence() { // then assertFalse(sensorStatistics.hasHeartRate()); assertTrue(sensorStatistics.hasCadence()); - assertEquals(sensorStatistics.getAvgCadence().getRPM(), stats.avgCadence, 0f); - assertEquals(sensorStatistics.getMaxCadence().getRPM(), stats.maxCadence, 0f); + assertEquals(sensorStatistics.avgCadence().getRPM(), stats.avgCadence, 0f); + assertEquals(sensorStatistics.maxCadence().getRPM(), stats.maxCadence, 0f); assertFalse(sensorStatistics.hasPower()); } @@ -1078,7 +1078,7 @@ public void testGetSensorStats_onlyPower() { assertFalse(sensorStatistics.hasHeartRate()); assertFalse(sensorStatistics.hasCadence()); assertTrue(sensorStatistics.hasPower()); - assertEquals(sensorStatistics.getAvgPower().getW(), stats.avgPower, 0f); + assertEquals(sensorStatistics.avgPower().getW(), stats.avgPower, 0f); } @Test @@ -1111,11 +1111,11 @@ public void testGetSensorStats() { TestSensorDataUtil.SensorDataStats stats = sensorDataUtil.computeStats(); // then - assertEquals(sensorStatistics.getAvgHeartRate().getBPM(), stats.avgHr, 0f); - assertEquals(sensorStatistics.getMaxHeartRate().getBPM(), stats.maxHr, 0f); - assertEquals(sensorStatistics.getAvgCadence().getRPM(), stats.avgCadence, 0f); - assertEquals(sensorStatistics.getMaxCadence().getRPM(), stats.maxCadence, 0f); - assertEquals(sensorStatistics.getAvgPower().getW(), stats.avgPower, 0f); + assertEquals(sensorStatistics.avgHeartRate().getBPM(), stats.avgHr, 0f); + assertEquals(sensorStatistics.maxHeartRate().getBPM(), stats.maxHr, 0f); + assertEquals(sensorStatistics.avgCadence().getRPM(), stats.avgCadence, 0f); + assertEquals(sensorStatistics.maxCadence().getRPM(), stats.maxCadence, 0f); + assertEquals(sensorStatistics.avgPower().getW(), stats.avgPower, 0f); } @Test @@ -1156,11 +1156,11 @@ public void testGetSensorStats_withManualResume() { TestSensorDataUtil.SensorDataStats stats = sensorDataUtil.computeStats(); // then - assertEquals(sensorStatistics.getAvgHeartRate().getBPM(), stats.avgHr, 0f); - assertEquals(sensorStatistics.getMaxHeartRate().getBPM(), stats.maxHr, 0f); - assertEquals(sensorStatistics.getAvgCadence().getRPM(), stats.avgCadence, 0f); - assertEquals(sensorStatistics.getMaxCadence().getRPM(), stats.maxCadence, 0f); - assertEquals(sensorStatistics.getAvgPower().getW(), stats.avgPower, 0f); + assertEquals(sensorStatistics.avgHeartRate().getBPM(), stats.avgHr, 0f); + assertEquals(sensorStatistics.maxHeartRate().getBPM(), stats.maxHr, 0f); + assertEquals(sensorStatistics.avgCadence().getRPM(), stats.avgCadence, 0f); + assertEquals(sensorStatistics.maxCadence().getRPM(), stats.maxCadence, 0f); + assertEquals(sensorStatistics.avgPower().getW(), stats.avgPower, 0f); } @Test @@ -1201,11 +1201,11 @@ public void testGetSensorStats_withStartAutomatic() { TestSensorDataUtil.SensorDataStats stats = sensorDataUtil.computeStats(); // then - assertEquals(sensorStatistics.getAvgHeartRate().getBPM(), stats.avgHr, 0f); - assertEquals(sensorStatistics.getMaxHeartRate().getBPM(), stats.maxHr, 0f); - assertEquals(sensorStatistics.getAvgCadence().getRPM(), stats.avgCadence, 0f); - assertEquals(sensorStatistics.getMaxCadence().getRPM(), stats.maxCadence, 0f); - assertEquals(sensorStatistics.getAvgPower().getW(), stats.avgPower, 0f); + assertEquals(sensorStatistics.avgHeartRate().getBPM(), stats.avgHr, 0f); + assertEquals(sensorStatistics.maxHeartRate().getBPM(), stats.maxHr, 0f); + assertEquals(sensorStatistics.avgCadence().getRPM(), stats.avgCadence, 0f); + assertEquals(sensorStatistics.maxCadence().getRPM(), stats.maxCadence, 0f); + assertEquals(sensorStatistics.avgPower().getW(), stats.avgPower, 0f); } private void testGetSensorStats_randomData(int totalPoints, boolean withStartSegments) { @@ -1232,11 +1232,11 @@ private void testGetSensorStats_randomData(int totalPoints, boolean withStartSeg TestSensorDataUtil.SensorDataStats stats = sensorDataUtil.computeStats(); // then - assertEquals(sensorStatistics.getAvgHeartRate().getBPM(), stats.avgHr, 0.01f); - assertEquals(sensorStatistics.getMaxHeartRate().getBPM(), stats.maxHr, 0.01f); - assertEquals(sensorStatistics.getAvgCadence().getRPM(), stats.avgCadence, 0.01f); - assertEquals(sensorStatistics.getMaxCadence().getRPM(), stats.maxCadence, 0.01f); - assertEquals(sensorStatistics.getAvgPower().getW(), stats.avgPower, 0.01f); + assertEquals(sensorStatistics.avgHeartRate().getBPM(), stats.avgHr, 0.01f); + assertEquals(sensorStatistics.maxHeartRate().getBPM(), stats.maxHr, 0.01f); + assertEquals(sensorStatistics.avgCadence().getRPM(), stats.avgCadence, 0.01f); + assertEquals(sensorStatistics.maxCadence().getRPM(), stats.maxCadence, 0.01f); + assertEquals(sensorStatistics.avgPower().getW(), stats.avgPower, 0.01f); } @Test diff --git a/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordedFragment.java b/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordedFragment.java index cf6b5bba0e..6118d32a64 100644 --- a/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordedFragment.java +++ b/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordedFragment.java @@ -254,24 +254,24 @@ private void updateSensorUI() { } if (sensorStatistics.hasHeartRate()) { - String maxBPM = String.valueOf(Math.round(sensorStatistics.getMaxHeartRate().getBPM())); - String avgBPM = String.valueOf(Math.round(sensorStatistics.getAvgHeartRate().getBPM())); + String maxBPM = String.valueOf(Math.round(sensorStatistics.maxHeartRate().getBPM())); + String avgBPM = String.valueOf(Math.round(sensorStatistics.avgHeartRate().getBPM())); viewBinding.statsHeartRateGroup.setVisibility(View.VISIBLE); viewBinding.statsMaxHeartRateValue.setText(maxBPM); viewBinding.statsAvgHeartRateValue.setText(avgBPM); } if (sensorStatistics.hasCadence()) { - String maxRPM = String.valueOf(Math.round(sensorStatistics.getMaxCadence().getRPM())); - String avgRPM = String.valueOf(Math.round(sensorStatistics.getAvgCadence().getRPM())); + String maxRPM = String.valueOf(Math.round(sensorStatistics.maxCadence().getRPM())); + String avgRPM = String.valueOf(Math.round(sensorStatistics.avgCadence().getRPM())); viewBinding.statsCadenceGroup.setVisibility(View.VISIBLE); viewBinding.statsMaxCadenceValue.setText(maxRPM); viewBinding.statsAvgCadenceValue.setText(avgRPM); } if (sensorStatistics.hasPower()) { - String maxW = String.valueOf(Math.round(sensorStatistics.getMaxPower().getW())); - String avgW = String.valueOf(Math.round(sensorStatistics.getAvgPower().getW())); + String maxW = String.valueOf(Math.round(sensorStatistics.maxPower().getW())); + String avgW = String.valueOf(Math.round(sensorStatistics.avgPower().getW())); viewBinding.statsPowerGroup.setVisibility(View.VISIBLE); viewBinding.statsMaxPowerValue.setText(maxW); diff --git a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementUtils.java b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementUtils.java index 801de5b2b0..bf0de22c41 100644 --- a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementUtils.java +++ b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncementUtils.java @@ -138,7 +138,7 @@ static Spannable createStatistics(Context context, TrackStatistics trackStatisti } if (shouldVoiceAnnounceAverageHeartRate() && sensorStatistics != null && sensorStatistics.hasHeartRate()) { - int averageHeartRate = Math.round(sensorStatistics.getAvgHeartRate().getBPM()); + int averageHeartRate = Math.round(sensorStatistics.avgHeartRate().getBPM()); builder.append(" ") .append(context.getString(R.string.average_heart_rate)); diff --git a/src/main/java/de/dennisguse/opentracks/stats/SensorStatistics.java b/src/main/java/de/dennisguse/opentracks/stats/SensorStatistics.java index cad709fda8..a1f432c2e6 100644 --- a/src/main/java/de/dennisguse/opentracks/stats/SensorStatistics.java +++ b/src/main/java/de/dennisguse/opentracks/stats/SensorStatistics.java @@ -4,56 +4,24 @@ import de.dennisguse.opentracks.data.models.HeartRate; import de.dennisguse.opentracks.data.models.Power; -public class SensorStatistics { - private final HeartRate maxHr; - private final HeartRate avgHr; - private final Cadence maxCadence; - private final Cadence avgCadence; - private final Power maxPower; - private final Power avgPower; - - public SensorStatistics(HeartRate maxHr, HeartRate avgHr, Cadence maxCadence, Cadence avgCadence, Power maxPower, Power avgPower) { - this.maxHr = maxHr; - this.avgHr = avgHr; - this.maxCadence = maxCadence; - this.avgCadence = avgCadence; - this.maxPower = maxPower; - this.avgPower = avgPower; - } +public record SensorStatistics( + HeartRate maxHeartRate, + HeartRate avgHeartRate, + Cadence maxCadence, + Cadence avgCadence, + Power maxPower, + Power avgPower +) { public boolean hasHeartRate() { - return avgHr != null && maxHr != null; - } - - public HeartRate getMaxHeartRate() { - return maxHr; - } - - public HeartRate getAvgHeartRate() { - return avgHr; + return avgHeartRate != null && maxHeartRate != null; } public boolean hasCadence() { return avgCadence != null && maxCadence != null; } - public Cadence getMaxCadence() { - return maxCadence; - } - - public Cadence getAvgCadence() { - return avgCadence; - } - public boolean hasPower() { return avgPower != null; } - - public Power getMaxPower() { - return maxPower; - } - - public Power getAvgPower() { - return avgPower; - } }