Skip to content

Commit

Permalink
Public API: allow marker creation.
Browse files Browse the repository at this point in the history
Fixes #2031.
  • Loading branch information
dennisguse committed Dec 21, 2024
1 parent fd9646b commit d531d5e
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 25 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,20 @@ The API can be invoked by sending an explicit Intent to start an activity.
* Debug: `de.dennisguse.opentracks.debug`
* Nightly: `de.dennisguse.opentracks.nightly`

`Class`:

`Classes`:
* **Start a recording:** `de.dennisguse.opentracks.publicapi.StartRecording`
* Set track data: `TRACK_NAME`, `TRACK_DESCRIPTION`, `TRACK_CATEGORY`, and `TRACK_ICON` (
non-localized identifier
see [/src/main/java/de/dennisguse/opentracks/util/TrackIconUtils.java#L38](/src/main/java/de/dennisguse/opentracks/util/TrackIconUtils.java#L38)).
NOTE: if `TRACK_ICON` is not present, `TRACK_CATEGORY` will be used to determine the icon (
localized).
* Send recorded data to another application via _Dashboard API_: `STATS_TARGET_PACKAGE` and
`STATS_TARGET_CLASS`
* **Stop a recording:** `de.dennisguse.opentracks.publicapi.StopRecording`
* **Create a marker:** `de.dennisguse.opentracks.publicapi.CreateMarker`

For testing via adb: `adb shell am start -e someParameter someValue -n "package/class"`

`StartRecording` supports the following parameters:

* Set track data: `TRACK_NAME`, `TRACK_DESCRIPTION`, `TRACK_CATEGORY`, and `TRACK_ICON` (non-localized identifier see [/src/main/java/de/dennisguse/opentracks/util/TrackIconUtils.java#L38](/src/main/java/de/dennisguse/opentracks/util/TrackIconUtils.java#L38)).
NOTE: if `TRACK_ICON` is not present, `TRACK_CATEGORY` will be used to determine the icon (localized).
* Send recorded data to another application via _Dashboard API_: `STATS_TARGET_PACKAGE` and `STATS_TARGET_CLASS`

The Public API is disabled by default to protect the user's privacy, but it can easily be enabled in the settings.

## File formats compatibility with open-source software
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public void testDeleteMarker_onlyOneMarker() {
assertEquals(contentProviderUtils.getMarkers(trackId).size(), 1);

// Get marker id that needs to delete.
Marker.Id marker1Id = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker1)));
Marker.Id marker1Id = contentProviderUtils.insertMarker(marker1);

// Delete
contentProviderUtils.deleteMarker(context, marker1Id);
Expand All @@ -509,7 +509,7 @@ public void testDeleteMarker_onlyOneMarkerWithPhotoUrl() throws IOException {
assertEquals(contentProviderUtils.getMarkers(trackId).size(), 1);

// Get marker id that needs to delete.
Marker.Id marker1Id = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker1)));
Marker.Id marker1Id = contentProviderUtils.insertMarker(marker1);

// Check marker has photo and it's in the external storage.
assertTrue(marker1.hasPhoto());
Expand Down Expand Up @@ -553,11 +553,11 @@ public void testDeleteMarker_hasNextMarker() {
// Insert at first.
Marker marker1 = new Marker(trackId, contentProviderUtils.getLastValidTrackPoint(trackId));
marker1.setDescription(MOCK_DESC);
Marker.Id marker1Id = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker1)));
Marker.Id marker1Id = contentProviderUtils.insertMarker(marker1);

Marker marker2 = new Marker(trackId, contentProviderUtils.getLastValidTrackPoint(trackId));
marker2.setDescription(MOCK_DESC);
Marker.Id marker2Id = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker2)));
Marker.Id marker2Id = contentProviderUtils.insertMarker(marker2);

// Delete
assertNotNull(contentProviderUtils.getMarker(marker1Id));
Expand Down Expand Up @@ -598,7 +598,7 @@ public void testInsertAndGetMarker() {

Marker marker = new Marker(trackId, contentProviderUtils.getLastValidTrackPoint(trackId));
marker.setDescription(TEST_DESC);
Marker.Id markerId = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker)));
Marker.Id markerId = contentProviderUtils.insertMarker(marker);

assertEquals(TEST_DESC, contentProviderUtils.getMarker(markerId).getDescription());
}
Expand All @@ -614,7 +614,7 @@ public void testUpdateMarker() {
// Insert at first.
Marker marker = new Marker(trackId, contentProviderUtils.getLastValidTrackPoint(trackId));
marker.setDescription(TEST_DESC);
Marker.Id markerId = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker)));
Marker.Id markerId = contentProviderUtils.insertMarker(marker);

// Update
marker = contentProviderUtils.getMarker(markerId);
Expand All @@ -638,7 +638,7 @@ public void testUpdateMarker_withPhoto() throws IOException {
TrackPoint trackPoint = contentProviderUtils.getLastValidTrackPoint(trackId);
Marker marker = TestDataUtil.createMarkerWithPhoto(context, trackId, trackPoint);
marker.setDescription(TEST_DESC);
Marker.Id markerId = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker)));
Marker.Id markerId = contentProviderUtils.insertMarker(marker);

File dir = new File(FileUtils.getPhotoDir(context), "" + trackId.id());
assertTrue(dir.exists());
Expand Down Expand Up @@ -673,7 +673,7 @@ public void testUpdateMarker_delPhotoAndDir() throws IOException {
TrackPoint trackPoint = contentProviderUtils.getLastValidTrackPoint(trackId);
Marker marker = TestDataUtil.createMarkerWithPhoto(context, trackId, trackPoint);
marker.setDescription(TEST_DESC);
Marker.Id markerId = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker)));
Marker.Id markerId = contentProviderUtils.insertMarker(marker);

File dir = new File(FileUtils.getPhotoDir(context), "" + trackId.id());
assertTrue(dir.exists());
Expand Down Expand Up @@ -709,7 +709,7 @@ public void testUpdateMarker_delPhotoNotDir() throws IOException {
marker.setDescription(TEST_DESC);
Marker otherMarker = TestDataUtil.createMarkerWithPhoto(context, trackId, trackPoint);
otherMarker.setDescription(TEST_DESC);
Marker.Id markerId = new Marker.Id(ContentUris.parseId(contentProviderUtils.insertMarker(marker)));
Marker.Id markerId = contentProviderUtils.insertMarker(marker);
contentProviderUtils.insertMarker(otherMarker);

File dir = new File(FileUtils.getPhotoDir(context), "" + trackId.id());
Expand Down
8 changes: 8 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ limitations under the License.
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".publicapi.CreateMarker"
android:exported="true"
android:theme="@style/SplashTheme">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<!-- NOTE: the name of the MAIN/LAUNCHER is used by icon packs; if this is changed, icon packs break.-->
<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,11 @@ public List<Marker> getMarkers(Track.Id trackId) {
return markers;
}

/**
* @return the content provider URI of the inserted marker.
*/
public Uri insertMarker(@NonNull Marker marker) {
// TODO Merge with updateMarker
public Marker.Id insertMarker(@NonNull Marker marker) {
marker.setId(null);
return contentResolver.insert(MarkerColumns.CONTENT_URI, createContentValues(marker));
Uri uri = contentResolver.insert(MarkerColumns.CONTENT_URI, createContentValues(marker));
return new Marker.Id(ContentUris.parseId(uri));
}

private void deleteMarkerPhoto(Context context, Marker marker) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import de.dennisguse.opentracks.services.TrackRecordingServiceConnection;
import de.dennisguse.opentracks.settings.PreferencesUtils;

public abstract class AbstractAPIActivity extends AppCompatActivity {
abstract class AbstractAPIActivity extends AppCompatActivity {

private final String TAG = AbstractAPIActivity.class.getSimpleName();

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/de/dennisguse/opentracks/publicapi/CreateMarker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.dennisguse.opentracks.publicapi;

import de.dennisguse.opentracks.services.TrackRecordingService;

public class CreateMarker extends AbstractAPIActivity {

@Override
protected void execute(TrackRecordingService service) {
service.createMarker();
}

@Override
protected boolean isPostExecuteStopService() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import de.dennisguse.opentracks.util.IntentUtils;

/**
* Public API to creates a Marker for a given track with a given location
* INTERNAL: only meant for clients of OSMDashboard API.
*/
public class CreateMarkerActivity extends AppCompatActivity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import de.dennisguse.opentracks.util.IntentUtils;

/**
* Public api to show an existing marker
* INTERNAL: only meant for clients of OSMDashboard API.
*/
public class ShowMarkerActivity extends AppCompatActivity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import java.io.StringWriter;
import java.time.Duration;

import de.dennisguse.opentracks.data.ContentProviderUtils;
import de.dennisguse.opentracks.data.models.Distance;
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.sensors.sensorData.SensorDataSet;
Expand Down Expand Up @@ -243,6 +245,20 @@ void stopSensors() {
gpsStatusObservable.postValue(STATUS_GPS_DEFAULT);
}

public Marker.Id createMarker() {
if (!isRecording()) {
return null;
}

//TODO This contains some duplication to TrackRecodingActivity's Marker creation
TrackPoint trackPoint = trackRecordingManager.getLastStoredTrackPointWithLocation();
if (trackPoint == null) {
return null;
}
Marker marker = new Marker(recordingStatus.trackId(), trackPoint);
return new ContentProviderUtils(this).insertMarker(marker);
}

@Override
public boolean newTrackPoint(TrackPoint trackPoint, Distance thresholdHorizontalAccuracy) {
if (!isRecording()) {
Expand Down

0 comments on commit d531d5e

Please sign in to comment.