mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-03 09:53:06 +02:00
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:
@@ -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<RecordingData> recordingDataObservable;
|
||||
|
||||
// The following variables are set when recording:
|
||||
private Handler handler;
|
||||
|
||||
private TrackPointCreator trackPointCreator;
|
||||
@@ -119,9 +118,6 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
Log.d(TAG, "Destroying");
|
||||
if (isRecording()) {
|
||||
endCurrentTrack();
|
||||
}
|
||||
if (trackPointCreator != null && trackPointCreator.isStarted()) {
|
||||
stopSensors();
|
||||
}
|
||||
@@ -150,6 +146,16 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -167,9 +173,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
||||
|
||||
// Set recording status
|
||||
Track.Id trackId = trackRecordingManager.startNewTrack();
|
||||
updateRecordingStatus(RecordingStatus.record(trackId));
|
||||
|
||||
startRecording();
|
||||
startRecording(trackId);
|
||||
return trackId;
|
||||
}
|
||||
|
||||
@@ -180,12 +184,13 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
||||
}
|
||||
Log.i(TAG, "resumeTrack");
|
||||
|
||||
updateRecordingStatus(RecordingStatus.record(trackId));
|
||||
|
||||
startRecording();
|
||||
startRecording(trackId);
|
||||
}
|
||||
|
||||
private void startRecording() {
|
||||
private void startRecording(Track.Id trackId) {
|
||||
updateRecordingStatus(RecordingStatus.record(trackId));
|
||||
TrackIdStorage.set(this, trackId);
|
||||
|
||||
startSensors();
|
||||
|
||||
voiceAnnouncementManager.start(trackRecordingManager.getTrackStatistics());
|
||||
@@ -226,6 +231,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
|
||||
updateRecordingStatus(STATUS_DEFAULT);
|
||||
|
||||
trackRecordingManager.endCurrentTrack();
|
||||
TrackIdStorage.unset(this);
|
||||
|
||||
stopUpdateRecordingData();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user