forked from upstream-mirrors/OpenTracks
HandlerServer: use executor service to send data to TrackRecordingService.
Fixes #329.
This commit is contained in:
@@ -3,26 +3,46 @@ package de.dennisguse.opentracks.services.handlers;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||
|
||||
public class HandlerServer {
|
||||
private String TAG = HandlerServer.class.getSimpleName();
|
||||
|
||||
private static String TAG = HandlerServer.class.getSimpleName();
|
||||
|
||||
private final LocationHandler locationHandler;
|
||||
private final HandlerServerInterface service;
|
||||
private ExecutorService serviceExecutor;
|
||||
|
||||
public HandlerServer(HandlerServerInterface service) {
|
||||
this.locationHandler = new LocationHandler(this);
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
HandlerServer(LocationHandler locationHandler, HandlerServerInterface service) {
|
||||
this.locationHandler = locationHandler;
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public void start(Context context) {
|
||||
serviceExecutor = Executors.newSingleThreadExecutor();
|
||||
|
||||
locationHandler.onStart(context);
|
||||
locationHandler.onSharedPreferenceChanged(context, null, null);
|
||||
}
|
||||
|
||||
public void stop(Context context) {
|
||||
locationHandler.onStop(context);
|
||||
|
||||
if (serviceExecutor != null) {
|
||||
serviceExecutor.shutdownNow();
|
||||
}
|
||||
serviceExecutor = null;
|
||||
}
|
||||
|
||||
public void onSharedPreferenceChanged(Context context, SharedPreferences preferences, String key) {
|
||||
@@ -30,7 +50,10 @@ public class HandlerServer {
|
||||
}
|
||||
|
||||
public void sendTrackPoint(TrackPoint trackPoint, int recordingGpsAccuracy) {
|
||||
service.newTrackPoint(trackPoint, recordingGpsAccuracy);
|
||||
if (serviceExecutor == null || serviceExecutor.isTerminated() || serviceExecutor.isShutdown()) {
|
||||
return;
|
||||
}
|
||||
serviceExecutor.execute(() -> service.newTrackPoint(trackPoint, recordingGpsAccuracy));
|
||||
}
|
||||
|
||||
public interface HandlerServerInterface {
|
||||
|
||||
Reference in New Issue
Block a user