forked from upstream-mirrors/OpenTracks
Bugfix: TrackRecordingService must handle restarts while recording.
We now store the Track.Id in a sharedPreferences.
Functionality was removed in e09b04308c
Fixes #2333
This commit is contained in:
+36
@@ -2,6 +2,7 @@ package de.dennisguse.opentracks.services;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -93,6 +94,41 @@ public class TrackRecordingServiceRecordingTest {
|
|||||||
service = startService();
|
service = startService();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MediumTest
|
||||||
|
@Test
|
||||||
|
public void reCreate_not_recording() {
|
||||||
|
// given
|
||||||
|
TrackIdStorage.unset(context);
|
||||||
|
|
||||||
|
assertFalse(service.isRecording());
|
||||||
|
|
||||||
|
// when
|
||||||
|
service.onStartCommand(null, 0, 1);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertFalse(service.isRecording());
|
||||||
|
}
|
||||||
|
|
||||||
|
@MediumTest
|
||||||
|
@Test
|
||||||
|
public void reCreate_recording() {
|
||||||
|
// given
|
||||||
|
assertFalse(service.isRecording());
|
||||||
|
Track.Id trackId = service.startNewTrack();
|
||||||
|
assertTrue(service.isRecording());
|
||||||
|
|
||||||
|
// when (crash and restart)
|
||||||
|
service.onDestroy();
|
||||||
|
service.onCreate();
|
||||||
|
service.onStartCommand(null, 0, 1);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertTrue(service.isRecording());
|
||||||
|
assertEquals(trackId, service.getRecordingStatusObservable().getValue().trackId());
|
||||||
|
|
||||||
|
service.endCurrentTrack();
|
||||||
|
}
|
||||||
|
|
||||||
@MediumTest
|
@MediumTest
|
||||||
@Test
|
@Test
|
||||||
public void recording_startStop() {
|
public void recording_startStop() {
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package de.dennisguse.opentracks.services;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
|
import de.dennisguse.opentracks.data.models.Track;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For handling restarts of TrackRecordingService: while recording, stores the {@link de.dennisguse.opentracks.data.models.Track.Id}.
|
||||||
|
*/
|
||||||
|
class TrackIdStorage {
|
||||||
|
|
||||||
|
private final static String SHARED_PREFERENCE_NAME = "TrackRecordingService_pref";
|
||||||
|
|
||||||
|
private final static String KEY = "trackId_key";
|
||||||
|
|
||||||
|
private static SharedPreferences sharedPreferences;
|
||||||
|
|
||||||
|
private static void init(Context context) {
|
||||||
|
if (sharedPreferences == null) {
|
||||||
|
sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static Track.Id get(Context context) {
|
||||||
|
TrackIdStorage.init(context);
|
||||||
|
|
||||||
|
long value = sharedPreferences.getLong(KEY, -1);
|
||||||
|
if (value < 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Track.Id(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void set(Context context, @NonNull Track.Id trackId) {
|
||||||
|
TrackIdStorage.init(context);
|
||||||
|
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.putLong(KEY, trackId.id());
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void unset(Context context) {
|
||||||
|
TrackIdStorage.init(context);
|
||||||
|
|
||||||
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
editor.remove(KEY);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -86,7 +86,6 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
private MutableLiveData<GpsStatusValue> gpsStatusObservable;
|
private MutableLiveData<GpsStatusValue> gpsStatusObservable;
|
||||||
private MutableLiveData<RecordingData> recordingDataObservable;
|
private MutableLiveData<RecordingData> recordingDataObservable;
|
||||||
|
|
||||||
// The following variables are set when recording:
|
|
||||||
private Handler handler;
|
private Handler handler;
|
||||||
|
|
||||||
private TrackPointCreator trackPointCreator;
|
private TrackPointCreator trackPointCreator;
|
||||||
@@ -119,9 +118,6 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
Log.d(TAG, "Destroying");
|
Log.d(TAG, "Destroying");
|
||||||
if (isRecording()) {
|
|
||||||
endCurrentTrack();
|
|
||||||
}
|
|
||||||
if (trackPointCreator != null && trackPointCreator.isStarted()) {
|
if (trackPointCreator != null && trackPointCreator.isStarted()) {
|
||||||
stopSensors();
|
stopSensors();
|
||||||
}
|
}
|
||||||
@@ -150,6 +146,16 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
if (intent == null) {
|
||||||
|
Log.i(TAG, "Service got killed and is restarting");
|
||||||
|
|
||||||
|
//Best guess: do we need to resume a recording?
|
||||||
|
Track.Id trackId = TrackIdStorage.get(this);
|
||||||
|
if (trackId != null) {
|
||||||
|
Log.i(TAG, "After restart: resuming recording for " + trackId);
|
||||||
|
resumeTrack(trackId);
|
||||||
|
}
|
||||||
|
}
|
||||||
return START_STICKY;
|
return START_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,9 +173,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
|
|
||||||
// Set recording status
|
// Set recording status
|
||||||
Track.Id trackId = trackRecordingManager.startNewTrack();
|
Track.Id trackId = trackRecordingManager.startNewTrack();
|
||||||
updateRecordingStatus(RecordingStatus.record(trackId));
|
startRecording(trackId);
|
||||||
|
|
||||||
startRecording();
|
|
||||||
return trackId;
|
return trackId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,12 +184,13 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
}
|
}
|
||||||
Log.i(TAG, "resumeTrack");
|
Log.i(TAG, "resumeTrack");
|
||||||
|
|
||||||
updateRecordingStatus(RecordingStatus.record(trackId));
|
startRecording(trackId);
|
||||||
|
|
||||||
startRecording();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startRecording() {
|
private void startRecording(Track.Id trackId) {
|
||||||
|
updateRecordingStatus(RecordingStatus.record(trackId));
|
||||||
|
TrackIdStorage.set(this, trackId);
|
||||||
|
|
||||||
startSensors();
|
startSensors();
|
||||||
|
|
||||||
voiceAnnouncementManager.start(trackRecordingManager.getTrackStatistics());
|
voiceAnnouncementManager.start(trackRecordingManager.getTrackStatistics());
|
||||||
@@ -226,6 +231,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
|||||||
updateRecordingStatus(STATUS_DEFAULT);
|
updateRecordingStatus(STATUS_DEFAULT);
|
||||||
|
|
||||||
trackRecordingManager.endCurrentTrack();
|
trackRecordingManager.endCurrentTrack();
|
||||||
|
TrackIdStorage.unset(this);
|
||||||
|
|
||||||
stopUpdateRecordingData();
|
stopUpdateRecordingData();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user