Skip to content

Commit

Permalink
fix #1957 open MarkerEditActivity to receive external marker
Browse files Browse the repository at this point in the history
  • Loading branch information
pstorch committed Jul 15, 2024
1 parent a932add commit 6c871b6
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void setUp() throws TimeoutException {
Distance sensorDistance = Distance.of(10); // recording distance interval

sendLocation(trackPointCreator, "2020-02-02T02:02:03Z", 3, 14, 10, 13, 15, 10, 1f);
service.insertMarker("Marker 1", "Marker 1 category", "Marker 1 desc", null);
service.insertMarker("Marker 1", "Marker 1 category", "Marker 1 desc", null, null, null);

// A sensor-only TrackPoint
trackPointCreator.setClock("2020-02-02T02:02:04Z");
Expand All @@ -156,7 +156,7 @@ public void setUp() throws TimeoutException {
mockSensorData(trackPointCreator, 5f, Distance.of(2), 69f, 3f, 50f, null); // Distance will be added to next TrackPoint

sendLocation(trackPointCreator, "2020-02-02T02:02:17Z", 3, 14.001, 10, 13, 15, 10, 0f);
service.insertMarker("Marker 2", "Marker 2 category", "Marker 2 desc", null);
service.insertMarker("Marker 2", "Marker 2 category", "Marker 2 desc", null, null, null);

trackPointCreator.setClock("2020-02-02T02:02:18Z");
trackPointCreator.getSensorManager().sensorDataSet = new SensorDataSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,42 @@ public void notRecording_testInsertMarker() {
assertFalse(service.isRecording());

// when
Marker.Id markerId = service.insertMarker(null, null, null, null);
Marker.Id markerId = service.insertMarker(null, null, null, null, null, null);

// then
assertNull(markerId);
}

@MediumTest
@Test
public void notRecording_but_TrackId_and_TrackPoint_provided_testInsertMarker() {
// given
var trackId = service.startNewTrack();
service.endCurrentTrack();
assertFalse(service.isRecording());

// when
Marker.Id markerId = service.insertMarker(null, null, null, null, trackId, new TrackPoint(50.0, 9.0, null, Instant.now()));

// then
assertNotEquals(new Marker.Id(-1L), markerId);
Marker wpt = contentProviderUtils.getMarker(markerId);
assertEquals(context.getString(R.string.marker_icon_url), wpt.getIcon());
assertEquals(context.getString(R.string.marker_name_format, 1), wpt.getName());
assertEquals(trackId, wpt.getTrackId());
assertEquals(50.0, wpt.getLatitude(), 0.01);
assertEquals(9.0, wpt.getLongitude(), 0.01);
assertNotNull(wpt.getLocation());
}

@MediumTest
@Test
public void recording_noGPSfix_cannotCreateMarker() {
// given
service.startNewTrack();

// when
Marker.Id markerId = service.insertMarker(null, null, null, null);
Marker.Id markerId = service.insertMarker(null, null, null, null, null, null);

// then
assertNull(markerId);
Expand All @@ -135,7 +157,7 @@ public void recording_GPSfix_createsMarker() {
);

// when
Marker.Id markerId = service.insertMarker(null, null, null, null);
Marker.Id markerId = service.insertMarker(null, null, null, null, null, null);

// then
assertNotEquals(new Marker.Id(-1L), markerId);
Expand Down
10 changes: 9 additions & 1 deletion src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,15 @@ limitations under the License.
</intent-filter>
</activity>

<activity android:name=".ui.markers.MarkerEditActivity" />
<activity
android:name=".ui.markers.MarkerEditActivity"
android:exported="true">
<intent-filter>
<action android:name="de.dennisguse.opentracks.MarkerEdit" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity android:name=".ui.markers.MarkerListActivity" />

<activity android:name=".TrackListActivity" />
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/de/dennisguse/opentracks/data/models/Marker.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,19 @@ public Marker(@Nullable Track.Id trackId, @NonNull TrackPoint trackPoint) {
}

@Deprecated
public Marker(String name, String description, String category, String icon, @NonNull Track.Id trackId, @NonNull TrackStatistics statistics, @NonNull TrackPoint trackPoint, String photoUrl) {
public Marker(String name, String description, String category, String icon, @NonNull Track.Id trackId, TrackStatistics statistics, @NonNull TrackPoint trackPoint, String photoUrl) {
this(trackId, trackPoint);
this.name = name;
this.description = description;
this.category = category;
this.icon = icon;
this.length = statistics.getTotalDistance();
this.duration = statistics.getTotalTime();
if (statistics == null) {
this.length = Distance.of(0);
this.duration = Duration.ofMillis(0);
} else {
this.length = statistics.getTotalDistance();
this.duration = statistics.getTotalTime();
}
this.photoUrl = photoUrl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.time.Duration;
import java.time.ZoneOffset;
import java.util.Objects;

import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.ContentProviderUtils;
Expand Down Expand Up @@ -138,27 +139,29 @@ Pair<Track, Pair<TrackPoint, SensorDataSet>> getDataForUI() {
return new Pair<>(track, current);
}

public Marker.Id insertMarker(String name, String category, String description, String photoUrl) {
public Marker.Id insertMarker(String name, String category, String description, String photoUrl, Track.Id trackId, TrackPoint trackPoint) {
Track.Id markerTrackId = Objects.requireNonNullElseGet(trackId, () -> this.trackId);
if (name == null) {
Integer nextMarkerNumber = contentProviderUtils.getNextMarkerNumber(trackId);
Integer nextMarkerNumber = contentProviderUtils.getNextMarkerNumber(markerTrackId);
if (nextMarkerNumber == null) {
nextMarkerNumber = 1;
}
name = context.getString(R.string.marker_name_format, nextMarkerNumber + 1);
}

if (lastStoredTrackPointWithLocation == null) {
if (lastStoredTrackPointWithLocation == null && trackPoint == null) {
Log.i(TAG, "Could not create a marker as trackPoint is unknown.");
return null;
}
TrackPoint markerTrackPoint = Objects.requireNonNullElseGet(trackPoint, () -> lastStoredTrackPointWithLocation);

category = category != null ? category : "";
description = description != null ? description : "";
String icon = context.getString(R.string.marker_icon_url);
photoUrl = photoUrl != null ? photoUrl : "";

// Insert marker
Marker marker = new Marker(name, description, category, icon, trackId, getTrackStatistics(), lastStoredTrackPointWithLocation, photoUrl);
Marker marker = new Marker(name, description, category, icon, markerTrackId, getTrackStatistics(), markerTrackPoint, photoUrl);
Uri uri = contentProviderUtils.insertMarker(marker);
return new Marker.Id(ContentUris.parseId(uri));
}
Expand Down Expand Up @@ -247,6 +250,9 @@ synchronized boolean onNewTrackPoint(@NonNull TrackPoint trackPoint) {
}

TrackStatistics getTrackStatistics() {
if (trackStatisticsUpdater == null) {
return null;
}
return trackStatisticsUpdater.getTrackStatistics();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ public void newGpsStatus(GpsStatusValue gpsStatusValue) {
gpsStatusObservable.postValue(gpsStatusValue);
}

public Marker.Id insertMarker(String name, String category, String description, String photoUrl) {
if (!isRecording()) {
public Marker.Id insertMarker(String name, String category, String description, String photoUrl, Track.Id trackId, TrackPoint trackPoint) {
if (!isRecording() && (trackId == null || trackPoint == null)) {
return null;
}

return trackRecordingManager.insertMarker(name, category, description, photoUrl);
return trackRecordingManager.insertMarker(name, category, description, photoUrl, trackId, trackPoint);
}

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
Expand All @@ -41,11 +42,13 @@

import java.io.FileDescriptor;
import java.io.IOException;
import java.time.Instant;

import de.dennisguse.opentracks.AbstractActivity;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.models.Marker;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.data.models.TrackPoint;
import de.dennisguse.opentracks.databinding.MarkerEditBinding;
import de.dennisguse.opentracks.services.TrackRecordingService;
import de.dennisguse.opentracks.services.TrackRecordingServiceConnection;
Expand All @@ -58,14 +61,17 @@
public class MarkerEditActivity extends AbstractActivity {

public static final String EXTRA_TRACK_ID = "track_id";
public static final String EXTRA_TRACK_ID_LONG = "track_id_long";
public static final String EXTRA_MARKER_ID = "marker_id";
public static final String EXTRA_LOCATION = "location";

private static final String CAMERA_PHOTO_URI_KEY = "camera_photo_uri_key";

private static final String NEW_MARKER_ID = "new_marker_id";

private static final String TAG = MarkerEditActivity.class.getSimpleName();
private Track.Id trackId;
private Location location;
private Marker marker;

private MenuItem insertPhotoMenuItem;
Expand Down Expand Up @@ -93,6 +99,10 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

trackId = getIntent().getParcelableExtra(EXTRA_TRACK_ID);
if (getIntent().hasExtra(EXTRA_TRACK_ID_LONG)) {
trackId = new Track.Id(getIntent().getLongExtra(EXTRA_TRACK_ID_LONG, 0L));
}
location = getIntent().getParcelableExtra(EXTRA_LOCATION);
@Nullable Marker.Id markerId = getIntent().getParcelableExtra(EXTRA_MARKER_ID);
final boolean isNewMarker = markerId == null;

Expand Down Expand Up @@ -176,7 +186,11 @@ protected void onCreate(Bundle savedInstanceState) {

private Marker.Id createNewMarker(TrackRecordingService trackRecordingService) {
try {
Marker.Id marker = trackRecordingService.insertMarker("", "", "", null);
TrackPoint trackPoint = null;
if (location != null) {
trackPoint = new TrackPoint(location, Instant.now());
}
Marker.Id marker = trackRecordingService.insertMarker("", "", "", null, trackId, trackPoint);
if (marker == null) {
Toast.makeText(this, R.string.marker_add_error, Toast.LENGTH_LONG).show();
return null;
Expand Down

0 comments on commit 6c871b6

Please sign in to comment.