TrackRecordingService: test inserting locations.

This commit is contained in:
Dennis Guse
2020-04-15 22:37:31 +02:00
parent fd826199e2
commit 1d838344dc
7 changed files with 412 additions and 18 deletions
@@ -181,4 +181,10 @@ public class TrackPoint {
public float bearingTo(@NonNull Location dest) {
return location.bearingTo(dest);
}
@NonNull
@Override
public String toString() {
return "time=" + getTime() + ": lat=" + getLatitude() + " lng=" + getLongitude() + " acc=" + getAccuracy();
}
}
@@ -1,5 +1,7 @@
package de.dennisguse.opentracks.content.sensor;
import androidx.annotation.NonNull;
public final class SensorDataSet {
public static final float DATA_UNAVAILABLE = Float.NaN;
@@ -11,14 +13,14 @@ public final class SensorDataSet {
private float cadence;
private float power;
private float batteryLevel;
private long creationTimestamp;
private long time;
public SensorDataSet(float heartRate, float cadence, float power, float batteryLevel, long creationTimestamp) {
public SensorDataSet(float heartRate, float cadence, float power, float batteryLevel, long time) {
this.heartRate = heartRate;
this.cadence = cadence;
this.power = power;
this.batteryLevel = batteryLevel;
this.creationTimestamp = creationTimestamp;
this.time = time;
}
public SensorDataSet(float heartRate, float cadence, float power, float batteryLevel) {
@@ -63,8 +65,8 @@ public final class SensorDataSet {
return power;
}
public long getCreationTime() {
return creationTimestamp;
public long getTime() {
return time;
}
/**
@@ -73,7 +75,7 @@ public final class SensorDataSet {
* @param maxAge the maximal age in milliseconds.
*/
public boolean isRecent(long maxAge) {
return creationTimestamp + maxAge > System.currentTimeMillis();
return time + maxAge > System.currentTimeMillis();
}
public boolean hasBatteryLevel() {
@@ -91,4 +93,10 @@ public final class SensorDataSet {
public String getSensorAddress() {
return sensorAddress;
}
@NonNull
@Override
public String toString() {
return "time=" + getTime() + " sensor=" + getSensorAddress() + " heart=" + getHeartRate();
}
}
@@ -34,6 +34,7 @@ import android.os.PowerManager.WakeLock;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.core.app.TaskStackBuilder;
import java.util.concurrent.ExecutorService;
@@ -75,7 +76,7 @@ public class TrackRecordingService extends Service {
// The following variables are set in onCreate:
@Deprecated //TODO Should not be necessary
private ExecutorService executorService; // Enforces order of location changes.
private ExecutorService locationExecutorService; // Enforces order of location changes.
private ContentProviderUtils contentProviderUtils;
private LocationManager locationManager;
private PeriodicTaskExecutor voiceExecutor;
@@ -146,10 +147,10 @@ public class TrackRecordingService extends Service {
@Override
public void onLocationChanged(final Location location) {
if (executorService == null || executorService.isShutdown() || executorService.isTerminated()) {
if (locationExecutorService == null || locationExecutorService.isShutdown() || locationExecutorService.isTerminated()) {
return;
}
executorService.submit(new Runnable() {
locationExecutorService.submit(new Runnable() {
@Override
public void run() {
onLocationChangedAsync(location);
@@ -176,7 +177,7 @@ public class TrackRecordingService extends Service {
@Override
public void onCreate() {
super.onCreate();
executorService = Executors.newSingleThreadExecutor();
locationExecutorService = Executors.newSingleThreadExecutor();
contentProviderUtils = new ContentProviderUtils(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
voiceExecutor = new PeriodicTaskExecutor(this, new AnnouncementPeriodicTaskFactory());
@@ -232,7 +233,7 @@ public class TrackRecordingService extends Service {
wakeLock = SystemUtils.releaseWakeLock(wakeLock);
// Shutdown the executorService last to avoid sending events to a dead executor.
executorService.shutdown();
locationExecutorService.shutdown();
super.onDestroy();
}
@@ -763,4 +764,22 @@ public class TrackRecordingService extends Service {
notificationManager.cancelNotification();
}
}
/**
* Disables processing of location updates from {@link android.location.LocationManager}.
*/
@VisibleForTesting
public void enableLocationExecutor(boolean enable) {
if (enable) {
locationExecutorService = Executors.newSingleThreadExecutor();
} else {
locationExecutorService.shutdownNow();
locationExecutorService = null;
}
}
@VisibleForTesting
public void setRemoteSensorManager(BluetoothRemoteSensorManager remoteSensorManager) {
this.remoteSensorManager = remoteSensorManager;
}
}
@@ -5,6 +5,7 @@ import android.location.Location;
import androidx.annotation.VisibleForTesting;
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
/**
* TODO: There is a bug in Android that leaks Binder instances. This bug is
@@ -92,6 +93,18 @@ class TrackRecordingServiceBinder extends android.os.Binder implements TrackReco
return trackRecordingService.getSensorDataSet();
}
@VisibleForTesting
@Override
public void enableLocationExecutor(boolean enable) {
trackRecordingService.enableLocationExecutor(enable);
}
@VisibleForTesting
@Override
public void setRemoteSensorManager(BluetoothRemoteSensorManager remoteSensorManager) {
trackRecordingService.setRemoteSensorManager(remoteSensorManager);
}
/**
* Detaches from the track recording service. Clears the reference to the
* outer class to minimize the leak.
@@ -20,6 +20,7 @@ import android.location.Location;
import androidx.annotation.VisibleForTesting;
import de.dennisguse.opentracks.content.sensor.SensorDataSet;
import de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager;
/**
* App's service.
@@ -112,4 +113,13 @@ public interface TrackRecordingServiceInterface {
* @return SensorDataSet object.
*/
SensorDataSet getSensorData();
/**
* Disables processing of location updates from {@link android.location.LocationManager}.
*/
@VisibleForTesting
void enableLocationExecutor(boolean enable);
@VisibleForTesting
void setRemoteSensorManager(BluetoothRemoteSensorManager remoteSensorManager);
}