-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kevin/group 8/day specific activity (#162)
**Describe the pull request** #87 Show statistics for day specific run/Chairlift **Link to the the issue** [Issue-187](#87). **License agreement** By opening this pull request, you are providing your contribution under the _Apache License 2.0_ (see [LICENSE.md](LICENSE.md)). **Note: new dependencies/libraries** Please refrain from introducing new libraries without consulting the team.
- Loading branch information
Showing
10 changed files
with
560 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/de/dennisguse/opentracks/data/models/TrackSegment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package de.dennisguse.opentracks.data.models; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
|
||
public class TrackSegment { | ||
|
||
private static final String TAG = TrackSegment.class.getSimpleName(); | ||
private TrackPoint.Id id; | ||
@NonNull | ||
private final Instant time; | ||
|
||
private List<TrackPoint> trackPoints; | ||
|
||
public TrackSegment(@NonNull Instant time) { | ||
this.time = time; | ||
trackPoints = new ArrayList<>(); | ||
} | ||
|
||
public void addTrackPoint(TrackPoint point) { | ||
this.trackPoints.add(point); | ||
} | ||
|
||
public int getTrackPointCount() { | ||
return trackPoints.size(); | ||
} | ||
|
||
public Boolean hasTrackPoints() { | ||
return !trackPoints.isEmpty(); | ||
} | ||
|
||
public double getInitialElevation() { | ||
Optional<TrackPoint> firstPoint = trackPoints.stream().findFirst(); | ||
if (firstPoint.isPresent()) { | ||
TrackPoint point = firstPoint.get(); | ||
return point.getAltitude().toM(); | ||
} | ||
return 0; | ||
} | ||
|
||
public long getDisplacement() { | ||
long displacement = 0; | ||
for (TrackPoint point: trackPoints) { | ||
if (point.hasAltitudeGain()) { | ||
displacement += point.getAltitudeGain(); | ||
} | ||
|
||
if (point.hasAltitudeLoss()) { | ||
displacement += point.getAltitudeLoss(); | ||
} | ||
} | ||
|
||
return displacement; | ||
} | ||
public Distance getDistance() { | ||
if (trackPoints == null) { | ||
return null; | ||
} | ||
TrackPoint first = trackPoints.get(0); | ||
TrackPoint last = trackPoints.get(trackPoints.size() - 1); | ||
return last.distanceToPrevious(first); | ||
} | ||
|
||
public Duration getTotalTime(){ | ||
|
||
if(trackPoints == null){ | ||
return null; | ||
} | ||
TrackPoint startTime = trackPoints.get(0); | ||
TrackPoint endTime = trackPoints.get(trackPoints.size() - 1); | ||
|
||
return Duration.between(startTime.getTime(), endTime.getTime()); | ||
} | ||
|
||
public double getSpeed(){ | ||
// in m/s | ||
double totalDistance = getDistance().toM(); | ||
long totalTime = getTotalTime().toSeconds(); | ||
return totalDistance/totalTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...e/dennisguse/opentracks/ui/aggregatedStatistics/daySpecificStats/DaySpecificActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package de.dennisguse.opentracks.ui.aggregatedStatistics.daySpecificStats; | ||
|
||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Toast; | ||
|
||
import de.dennisguse.opentracks.AbstractActivity; | ||
import de.dennisguse.opentracks.R; | ||
import de.dennisguse.opentracks.TrackListActivity; | ||
import de.dennisguse.opentracks.data.ContentProviderUtils; | ||
import de.dennisguse.opentracks.data.TrackPointIterator; | ||
import de.dennisguse.opentracks.data.models.Track; | ||
import de.dennisguse.opentracks.data.models.TrackPoint; | ||
import de.dennisguse.opentracks.data.models.TrackSegment; | ||
import de.dennisguse.opentracks.databinding.DaySpecificActivityBinding; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.time.LocalDate; | ||
import java.util.Date; | ||
import java.time.ZoneId; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
public class DaySpecificActivity extends AbstractActivity { | ||
|
||
private DaySpecificActivityBinding viewBinding; | ||
private static final String TAG = DaySpecificActivity.class.getSimpleName(); | ||
public static final String EXTRA_TRACK_DATE = "track_date"; | ||
private Date activityDate; | ||
private ContentProviderUtils contentProviderUtils; | ||
private Track.Id trackId; | ||
private List<TrackSegment> trackSegments; | ||
private DaySpecificAdapter dataAdapter; | ||
|
||
private String fallbackDate = "2024-03-09"; | ||
|
||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
trackSegments = new ArrayList<>(); | ||
contentProviderUtils = new ContentProviderUtils(this); | ||
handleIntent(getIntent()); | ||
updateTrackSegments(); | ||
setSupportActionBar(viewBinding.bottomAppBarLayout.bottomAppBar); | ||
|
||
dataAdapter = new DaySpecificAdapter(this, viewBinding.segmentList); | ||
dataAdapter.swapData(trackSegments); | ||
viewBinding.segmentList.setAdapter(dataAdapter); | ||
viewBinding.segmentListToolbar.setTitle(fallbackDate); | ||
} | ||
|
||
@Override | ||
protected void onStart() { | ||
super.onStart(); | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
updateTrackSegments(); | ||
dataAdapter.swapData(trackSegments); | ||
} | ||
|
||
public void updateTrackSegments() { | ||
if (trackId == null) { | ||
return; | ||
} | ||
try (TrackPointIterator trackPointIterator = contentProviderUtils.getTrackPointLocationIterator(trackId, null)) { | ||
TrackSegment currentSegment = null; | ||
while (trackPointIterator.hasNext()) { | ||
TrackPoint nextPoint = trackPointIterator.next(); | ||
|
||
switch (nextPoint.getType()) { | ||
case SEGMENT_START_AUTOMATIC: | ||
case SEGMENT_START_MANUAL: | ||
if (currentSegment != null) { | ||
trackSegments.add(currentSegment); | ||
} | ||
currentSegment = new TrackSegment(nextPoint.getTime()); | ||
break; | ||
|
||
case SEGMENT_END_MANUAL: | ||
trackSegments.add(currentSegment); | ||
currentSegment = null; | ||
|
||
case TRACKPOINT: | ||
if (currentSegment != null) { | ||
currentSegment.addTrackPoint(nextPoint); | ||
} | ||
break; | ||
|
||
default: | ||
Log.d(TAG, "No Action for TrackPoint IDLE/SENSORPOINT while recording segments"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void showNoTracksFoundToast() { | ||
finish(); | ||
Toast.makeText(DaySpecificActivity.this, "No Tracks found for date: " + fallbackDate + "\n Please import GPX file from Moodle", Toast.LENGTH_LONG).show(); | ||
} | ||
private Date getFallbackDate() { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
LocalDate localDate = LocalDate.parse(fallbackDate, formatter); | ||
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); | ||
} | ||
|
||
private void handleIntent(Intent intent) { | ||
Date dateFromCalendar = intent.getParcelableExtra(EXTRA_TRACK_DATE); | ||
if (dateFromCalendar == null) { | ||
Log.e(TAG, DaySpecificActivity.class.getSimpleName() + " needs EXTRA_TRACK_ID."); | ||
|
||
// None provided, we will assume a specific date on our own | ||
activityDate = getFallbackDate(); | ||
} else { | ||
activityDate = dateFromCalendar; | ||
} | ||
|
||
Track track = contentProviderUtils.getTrack(activityDate); | ||
if (track == null) { | ||
showNoTracksFoundToast(); | ||
} else { | ||
trackId = track.getId(); | ||
} | ||
} | ||
|
||
@Override | ||
protected View getRootView() { | ||
viewBinding = DaySpecificActivityBinding.inflate(getLayoutInflater()); | ||
return viewBinding.getRoot(); | ||
} | ||
} |
Oops, something went wrong.