Skip to content

Commit

Permalink
GH-127: 40261875: Added Day Specific Activity details (#161)
Browse files Browse the repository at this point in the history
**Describe the pull request**

[Issue-127](#127).

**Link to the the issue**
(If available): The link to the issue that this pull request solves.
  • Loading branch information
kevwad authored Apr 8, 2024
2 parents b4479a9 + bb0865f commit 903f8b2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package de.dennisguse.opentracks.data.models;

import android.util.Log;

import androidx.annotation.NonNull;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.ArrayList;

import de.dennisguse.opentracks.io.file.importer.TrackImporter;
import java.util.Optional;

public class TrackSegment {

Expand Down Expand Up @@ -37,32 +34,53 @@ public Boolean hasTrackPoints() {
return !trackPoints.isEmpty();
}

public Distance getDistanceBetweenFirstAndLast() {
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);
Distance distance = last.distanceToPrevious(first);

return distance;
return last.distanceToPrevious(first);
}

public Long getTotalTime(){
public Duration getTotalTime(){

if(trackPoints == null){
return null;
}
TrackPoint startTime = trackPoints.get(0);
TrackPoint endTime = trackPoints.get(trackPoints.size() - 1);

Duration totalTime = Duration.between(startTime.getTime(), endTime.getTime());
return totalTime.toMinutes();
return Duration.between(startTime.getTime(), endTime.getTime());
}

public double getSpeed(Distance totalDistance, Long totalTime){
return totalDistance.distance_m()/totalTime;
public double getSpeed(){
// in m/s
double totalDistance = getDistance().toM();
long totalTime = getTotalTime().toSeconds();
return totalDistance/totalTime;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import de.dennisguse.opentracks.AbstractTrackDeleteActivity;
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.TrackDataHub;
import de.dennisguse.opentracks.data.TrackPointIterator;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.data.models.TrackPoint;
import de.dennisguse.opentracks.data.models.TrackSegment;
Expand All @@ -22,20 +22,20 @@
import java.time.ZoneId;
import java.util.List;
import java.util.ArrayList;
import androidx.recyclerview.widget.LinearLayoutManager;

public class DaySpecificActivity extends AbstractTrackDeleteActivity {
public class DaySpecificActivity extends AbstractActivity {

private DaySpecificActivityBinding viewBinding;
private static final String TAG = DaySpecificActivity.class.getSimpleName();
public static final String EXTRA_TRACK_ID = "track_id";
public static final String EXTRA_TRACK_DATE = "track_date";
private Date activityDate;
private ContentProviderUtils contentProviderUtils;
private TrackDataHub trackDataHub;
private Track.Id trackId;
private List<TrackSegment> trackSegments;
private DaySpecificAdapter dataAdapter;

private String fallbackDate = "2024-03-09";


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -44,13 +44,12 @@ protected void onCreate(Bundle savedInstanceState) {
contentProviderUtils = new ContentProviderUtils(this);
handleIntent(getIntent());
updateTrackSegments();
trackDataHub = new TrackDataHub(this);
setSupportActionBar(viewBinding.bottomAppBarLayout.bottomAppBar);

viewBinding = DaySpecificActivityBinding.inflate(getLayoutInflater());
dataAdapter = new DaySpecificAdapter(this, viewBinding.segmentList);
dataAdapter.swapData(trackSegments);
viewBinding.segmentList.setAdapter(dataAdapter);
viewBinding.segmentListToolbar.setTitle(fallbackDate);
}

@Override
Expand All @@ -66,6 +65,9 @@ protected void onResume() {
}

public void updateTrackSegments() {
if (trackId == null) {
return;
}
try (TrackPointIterator trackPointIterator = contentProviderUtils.getTrackPointLocationIterator(trackId, null)) {
TrackSegment currentSegment = null;
while (trackPointIterator.hasNext()) {
Expand Down Expand Up @@ -94,39 +96,35 @@ public void updateTrackSegments() {
Log.d(TAG, "No Action for TrackPoint IDLE/SENSORPOINT while recording segments");
}
}

for(int i=0; i<trackSegments.size(); i++){
Log.d("RUSHI", "TrackPoint No.: " + i);
Log.d("RUSHI", "Distance for segment " + trackSegments.get(i).getDistanceBetweenFirstAndLast().toM());
Log.d("RUSHI", "Time for segment " + trackSegments.get(i).getTotalTime() + "Minutes");
Log.d("RUSHI", "Speed for segment " + trackSegments.get(i).getSpeed(trackSegments.get(i).getDistanceBetweenFirstAndLast(), trackSegments.get(i).getTotalTime()) + "M/Sec");
}
}
}
private Date getDummyDate() {
String dateString = "2024-03-02";

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(dateString, formatter);
LocalDate localDate = LocalDate.parse(fallbackDate, formatter);
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}

private void handleIntent(Intent intent) {
trackId = intent.getParcelableExtra(EXTRA_TRACK_ID);
if (trackId == null) {
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 = getDummyDate();
List<Track> tracks = contentProviderUtils.getTracks();
for (Track track: tracks) {
System.out.println("Track date = " + track.getStartTime().toString());
}
Track track = contentProviderUtils.getTrack(activityDate);
if (track == null) {
finish();
} else {
trackId = track.getId();
}
activityDate = getFallbackDate();
} else {
activityDate = dateFromCalendar;
}

Track track = contentProviderUtils.getTrack(activityDate);
if (track == null) {
showNoTracksFoundToast();
} else {
trackId = track.getId();
}
}

Expand All @@ -135,19 +133,4 @@ protected View getRootView() {
viewBinding = DaySpecificActivityBinding.inflate(getLayoutInflater());
return viewBinding.getRoot();
}

@Override
protected void onDeleteConfirmed() {

}

@Override
protected Track.Id getRecordingTrackId() {
return trackId;
}

@Override
public void onDeleteFinished() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,19 @@ public ViewHolder(@NonNull View itemView) {
}

public void bind(TrackSegment segment) {
Distance distance = segment.getDistanceBetweenFirstAndLast();
Long time = segment.getTotalTime();
double speed = segment.getSpeed(distance, time);
viewBinding.daySpecificActivity.setText("Run");
viewBinding.daySpecificActivityDisplacement.setText("0 m");
viewBinding.daySpecificActivityDistance.setText("0.14 km");
viewBinding.daySpecificActivitySpeed.setText(speed + " km/h");
viewBinding.daySpecificActivityTime.setText("" + segment.getTotalTime());
double speed = segment.getSpeed();
String formattedSpeed = String.format("%.2f", speed);

double distance = segment.getDistance().toM();
String formattedDistance = String.format("%.2f", distance);

double elevation = segment.getInitialElevation();
String formattedElevation = String.format("%.1f", elevation);

viewBinding.daySpecificActivityDisplacement.setText(formattedElevation + " m");
viewBinding.daySpecificActivityDistance.setText(formattedDistance + " mts");
viewBinding.daySpecificActivitySpeed.setText(formattedSpeed + " m/s");
viewBinding.daySpecificActivityTime.setText(segment.getTotalTime().toMinutes() + " minutes");
}

public void setSelected(boolean isSelected) {
Expand Down
17 changes: 13 additions & 4 deletions src/main/res/layout/day_specific_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@
android:layout_height="match_parent"
tools:context=".ui.aggregatedStatistics.daySpecificStats.DaySpecificActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/segment_list_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="@string/app_name"
/>
</com.google.android.material.appbar.AppBarLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:orientation="vertical"
android:paddingBottom="?attr/actionBarSize"
android:padding="16dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/segment_list"
android:layout_width="match_parent"
android:background="#C0C0C0"
android:background="#D0D0D0"
android:layout_height="0dp"
android:layout_weight="0.8"
android:paddingTop="16dp"
android:paddingBottom="16dp"
app:layoutManager="LinearLayoutManager"/>
</LinearLayout>

Expand Down
29 changes: 16 additions & 13 deletions src/main/res/layout/day_specific_activity_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<LinearLayout
Expand All @@ -18,9 +18,8 @@
android:id="@+id/day_specific_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="Run"
android:textSize="15dp"
android:text="@string/segment_name_run"
android:textSize="15sp"
android:textStyle="bold" />

<!-- Row for Run -->
Expand All @@ -30,7 +29,7 @@
android:layout_marginBottom="8dp"
android:padding="4dp"
android:weightSum="4"
android:textSize="10dp"
android:textSize="12sp"
android:visibility="visible">

<TextView
Expand All @@ -39,35 +38,39 @@
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textSize="13dp"
android:text="0 m" />
android:textSize="13sp"
android:text=""
tools:text="0 m"/>

<TextView
android:id="@+id/day_specific_activity_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textSize="13dp"
android:text="0.14 km" />
android:textSize="13sp"
android:text=""
tools:text="0.14 km"/>

<TextView
android:id="@+id/day_specific_activity_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textSize="13dp"
android:text="0.50" />
android:textSize="13sp"
android:text=""
tools:text="0.50"/>

<TextView
android:id="@+id/day_specific_activity_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="13dp"
android:textSize="13sp"
android:layout_weight="1"
android:text="36.3 km/h" />
android:text=""
tools:text="36.3 km/h"/>
</LinearLayout>

<!-- Bottom Line -->
Expand Down
3 changes: 2 additions & 1 deletion src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,10 @@ limitations under the License.
<string name="location_coordinate">%1$s°</string>
<string name="location_latitude_longitude">%1$s, %2$s</string>
<!-- Value -->
<!-- Day-Specific Activities -->
<!-- Day-Specific Stats -->
<string name="day_specific_calendar">Calendar</string>
<string name="day_specific_title">Day Specific Run/Lift</string>
<string name="segment_name_run">Run</string>
<!-- Day-Specific Activities -->

<string name="value_integer_feet_excellent_gps">%1$d ft (excellent GPS)</string>
Expand Down

0 comments on commit 903f8b2

Please sign in to comment.