Implement Track.Id and Waypoint.Id (without tests).

Fixes #276.
This commit is contained in:
Dennis Guse
2020-08-18 22:06:26 +02:00
parent b2e8713db0
commit bcbaf4985a
49 changed files with 678 additions and 483 deletions
@@ -76,7 +76,7 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
private PeriodicTaskExecutor voiceExecutor;
private TrackRecordingServiceNotificationManager notificationManager;
private long recordingTrackId;
private Track.Id recordingTrackId;
private boolean recordingTrackPaused;
private int recordingDistanceInterval;
private int maxRecordingDistance;
@@ -138,7 +138,7 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
notificationManager = new TrackRecordingServiceNotificationManager(this);
// onSharedPreferenceChanged might not set recordingTrackId.
recordingTrackId = PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
recordingTrackId = new Track.Id(PreferencesUtils.RECORDING_TRACK_ID_DEFAULT);
PreferencesUtils.register(this, sharedPreferenceChangeListener);
sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null);
@@ -204,7 +204,7 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
return recordingTrackPaused;
}
public long getRecordingTrackId() {
public Track.Id getRecordingTrackId() {
return recordingTrackId;
}
@@ -230,9 +230,9 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
*
* @return the waypoint id
*/
public long insertWaypoint(String name, String category, String description, String photoUrl) {
public Waypoint.Id insertWaypoint(String name, String category, String description, String photoUrl) {
if (!isRecording() || isPaused()) {
return -1L;
return null;
}
if (name == null) {
@@ -246,7 +246,7 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
TrackPoint trackPoint = getLastValidTrackPointInCurrentSegment(recordingTrackId);
if (trackPoint == null) {
Log.i(TAG, "Could not create a waypoint as trackPoint is unknown.");
return -1L;
return null;
}
category = category != null ? category : "";
@@ -261,7 +261,7 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
// Insert waypoint
Waypoint waypoint = new Waypoint(name, description, category, icon, recordingTrackId, length, duration, trackPoint.getLocation(), photoUrl);
Uri uri = contentProviderUtils.insertWaypoint(waypoint);
return ContentUris.parseId(uri);
return new Waypoint.Id(ContentUris.parseId(uri));
}
/**
@@ -269,10 +269,10 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
*
* @return the track id
*/
long startNewTrack() {
Track.Id startNewTrack() {
if (isRecording()) {
Log.d(TAG, "Ignore startNewTrack. Already recording.");
return -1L;
return null;
}
long now = System.currentTimeMillis();
trackStatisticsUpdater = new TrackStatisticsUpdater(now);
@@ -280,7 +280,7 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
// Insert a track
Track track = new Track();
Uri uri = contentProviderUtils.insertTrack(track);
long trackId = ContentUris.parseId(uri);
Track.Id trackId = new Track.Id(ContentUris.parseId(uri));
// Update shared preferences
updateRecordingState(trackId, false);
@@ -305,7 +305,7 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
*
* @param trackId the id of the track to be resumed.
*/
void resumeTrack(long trackId) {
void resumeTrack(Track.Id trackId) {
Track track = contentProviderUtils.getTrack(trackId);
if (track == null) {
Log.e(TAG, "Ignore resumeTrack. Track " + trackId + " does not exists.");
@@ -333,7 +333,7 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
if (track == null) {
if (isRecording()) {
Log.w(TAG, "track is null, but recordingTrackId not -1L. " + recordingTrackId);
updateRecordingState(PreferencesUtils.RECORDING_TRACK_ID_DEFAULT, true);
updateRecordingState(new Track.Id(PreferencesUtils.RECORDING_TRACK_ID_DEFAULT), true);
}
showNotification(false);
return;
@@ -403,10 +403,10 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
}
// Need to remember the recordingTrackId before setting it to -1L
long trackId = recordingTrackId;
Track.Id trackId = recordingTrackId;
boolean wasPaused = recordingTrackPaused;
updateRecordingState(PreferencesUtils.RECORDING_TRACK_ID_DEFAULT, true);
updateRecordingState(new Track.Id(PreferencesUtils.RECORDING_TRACK_ID_DEFAULT), true);
// Update database
Track track = contentProviderUtils.getTrack(trackId);
@@ -489,7 +489,7 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
* @param trackId the track id
* @return the location or null
*/
private TrackPoint getLastValidTrackPointInCurrentSegment(long trackId) {
private TrackPoint getLastValidTrackPointInCurrentSegment(Track.Id trackId) {
if (!currentSegmentHasTrackPoint()) {
return null;
}
@@ -507,9 +507,9 @@ public class TrackRecordingService extends Service implements HandlerServer.Hand
* @param trackId the recording track id
* @param paused true if the recording is paused
*/
private void updateRecordingState(long trackId, boolean paused) {
private void updateRecordingState(Track.Id trackId, boolean paused) {
recordingTrackId = trackId;
PreferencesUtils.setLong(this, R.string.recording_track_id_key, trackId);
PreferencesUtils.setLong(this, R.string.recording_track_id_key, trackId.getId());
recordingTrackPaused = paused;
PreferencesUtils.setBoolean(this, R.string.recording_track_paused_key, recordingTrackPaused);
}
@@ -2,7 +2,9 @@ package de.dennisguse.opentracks.services;
import androidx.annotation.VisibleForTesting;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.content.data.TrackPoint;
import de.dennisguse.opentracks.content.data.Waypoint;
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
import de.dennisguse.opentracks.services.handlers.GpsStatusValue;
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
@@ -43,12 +45,12 @@ class TrackRecordingServiceBinder extends android.os.Binder implements TrackReco
}
@Override
public long startNewTrack() {
public Track.Id startNewTrack() {
return trackRecordingService.startNewTrack();
}
@Override
public void resumeTrack(long trackId) {
public void resumeTrack(Track.Id trackId) {
trackRecordingService.resumeTrack(trackId);
}
@@ -78,7 +80,7 @@ class TrackRecordingServiceBinder extends android.os.Binder implements TrackReco
}
@Override
public long getRecordingTrackId() {
public Track.Id getRecordingTrackId() {
return trackRecordingService.getRecordingTrackId();
}
@@ -88,7 +90,7 @@ class TrackRecordingServiceBinder extends android.os.Binder implements TrackReco
}
@Override
public long insertWaypoint(String name, String category, String description, String photoUrl) {
public Waypoint.Id insertWaypoint(String name, String category, String description, String photoUrl) {
return trackRecordingService.insertWaypoint(name, category, description, photoUrl);
}
@@ -31,6 +31,8 @@ import androidx.annotation.NonNull;
import de.dennisguse.opentracks.BuildConfig;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.TrackEditActivity;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.content.data.Waypoint;
import de.dennisguse.opentracks.util.IntentUtils;
import de.dennisguse.opentracks.util.PreferencesUtils;
@@ -177,18 +179,18 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
/**
* Adds a marker.
*
* @return the id of the marker or -1L if none could be created.
* @return the id of the marker or null if none could be created.
*/
public long addMarker(Context context, String name, String category, String description, String photoUrl) {
public Waypoint.Id addMarker(Context context, String name, String category, String description, String photoUrl) {
TrackRecordingServiceInterface trackRecordingService = getServiceIfBound();
if (trackRecordingService == null) {
Log.d(TAG, "Unable to add marker, no track recording service");
} else {
try {
long markerId = trackRecordingService.insertWaypoint(name, category, description, photoUrl);
if (markerId != -1L) {
Waypoint.Id waypoint = trackRecordingService.insertWaypoint(name, category, description, photoUrl);
if (waypoint != null) {
Toast.makeText(context, R.string.marker_add_success, Toast.LENGTH_SHORT).show();
return markerId;
return waypoint;
}
} catch (IllegalStateException e) {
Log.e(TAG, "Unable to add marker.", e);
@@ -196,7 +198,7 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
}
Toast.makeText(context, R.string.marker_add_error, Toast.LENGTH_LONG).show();
return -1L;
return null;
}
/**
@@ -213,7 +215,7 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
try {
if (showEditor) {
// Need to remember the recordingTrackId before calling endCurrentTrack() as endCurrentTrack() sets the value to -1L.
long recordingTrackId = PreferencesUtils.getRecordingTrackId(context);
Track.Id recordingTrackId = PreferencesUtils.getRecordingTrackId(context);
trackRecordingService.endCurrentTrack();
if (PreferencesUtils.isRecording(context)) {
Intent intent = IntentUtils.newIntent(context, TrackEditActivity.class)
@@ -17,7 +17,9 @@ package de.dennisguse.opentracks.services;
import androidx.annotation.VisibleForTesting;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.content.data.TrackPoint;
import de.dennisguse.opentracks.content.data.Waypoint;
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
import de.dennisguse.opentracks.services.handlers.GpsStatusValue;
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
@@ -43,12 +45,12 @@ public interface TrackRecordingServiceInterface {
*
* @return the track ID of the new track.
*/
long startNewTrack();
Track.Id startNewTrack();
/**
* Resumes the track identified by trackId.
*/
void resumeTrack(long trackId);
void resumeTrack(Track.Id trackId);
/**
* Pauses the current recording track.
@@ -78,11 +80,12 @@ public interface TrackRecordingServiceInterface {
/**
* Gets the current recording track ID. Returns -1 if not recording.
*/
long getRecordingTrackId();
Track.Id getRecordingTrackId();
/**
* Gets the total time for the current recording track. Returns 0 if not recording.
*/
//TODO milliseconds?
long getTotalTime();
/**
@@ -90,7 +93,7 @@ public interface TrackRecordingServiceInterface {
*
* @return the ID of the inserted waypoint
*/
long insertWaypoint(String name, String category, String description, String photoUrl);
Waypoint.Id insertWaypoint(String name, String category, String description, String photoUrl);
/**
* Gets the current sensor data. Returns null if there is no data.