Skip to content

Commit

Permalink
GPS: event-based using Raw.
Browse files Browse the repository at this point in the history
Part of #1424.
  • Loading branch information
dennisguse committed Jan 5, 2024
1 parent 80d7f8d commit 360c632
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import de.dennisguse.opentracks.sensors.sensorData.AggregatorCyclingDistanceSpeed;
import de.dennisguse.opentracks.sensors.sensorData.AggregatorCyclingPower;
import de.dennisguse.opentracks.sensors.sensorData.AggregatorHeartRate;
import de.dennisguse.opentracks.sensors.sensorData.Raw;
import de.dennisguse.opentracks.sensors.sensorData.SensorDataSet;
import de.dennisguse.opentracks.services.TrackRecordingService;
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
Expand Down Expand Up @@ -604,6 +605,6 @@ private void sendLocation(TrackPointCreator trackPointCreator, String time, doub
mockAltitudeChange(trackPointCreator, altitudeGain);

trackPointCreator.setClock(time);
trackPointCreator.onChange(location);
trackPointCreator.getSensorManager().onChanged(new Raw<>(location));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void start(Context context, Handler handler) {
throw new RuntimeException("SensorManager cannot be started twice; stop first.");
}

gpsManager = new GPSManager(observer); //TODO Pass listener
gpsManager = new GPSManager(observer, listener);
altitudeSumManager = new GainManager(listener);
bluetoothSensorManager = new BluetoothRemoteSensorManager(context, handler, listener);

Expand Down Expand Up @@ -104,7 +104,6 @@ public void reset() {
sensorDataSet.reset();
}

@Deprecated
@VisibleForTesting
public void onChanged(Raw<?> data) {
listener.onChange(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import de.dennisguse.opentracks.sensors.BluetoothRemoteSensorManager;

public abstract class Aggregator<Input extends Record, Output> {
public abstract class Aggregator<Input, Output> {

protected Raw<Input> previous;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.dennisguse.opentracks.sensors.sensorData;

import android.location.Location;

import androidx.annotation.NonNull;

public class AggregatorGPS extends Aggregator<Location, Location> {

public AggregatorGPS(String sensorAddress) {
super(sensorAddress);
}

@Override
protected void computeValue(Raw<Location> current) {
value = current.value();
}

@Override
public void reset() {
value = null;
}

@NonNull
@Override
protected Location getNoneValue() {
return new Location("none");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.time.Instant;

public record Raw<T extends Record>(
public record Raw<T>(
@NonNull T value,

@NonNull Instant time
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.dennisguse.opentracks.sensors.sensorData;

import android.util.Log;
import android.location.Location;
import android.util.Pair;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -40,16 +41,20 @@ public final class SensorDataSet {
@VisibleForTesting
public AggregatorBarometer barometer;

public AggregatorGPS gps;

public SensorDataSet() {
}

public SensorDataSet(SensorDataSet toCopy) {
//TODO This is not a copy constructor anymore, but it should be - aggregators are no value objects
this.heartRate = toCopy.heartRate;
this.cyclingCadence = toCopy.cyclingCadence;
this.cyclingDistanceSpeed = toCopy.cyclingDistanceSpeed;
this.cyclingPower = toCopy.cyclingPower;
this.runningDistanceSpeedCadence = toCopy.runningDistanceSpeedCadence;
this.barometer = toCopy.barometer;
this.gps = toCopy.gps;
}

public Pair<HeartRate, String> getHeartRate() {
Expand Down Expand Up @@ -93,7 +98,7 @@ public void add(@NonNull Aggregator<?, ?> data) {
}

public void update(@NonNull Raw<?> data) {
Record value = data.value();
Object value = data.value();

if (value instanceof HeartRate) {
this.heartRate.add((Raw<HeartRate>) data);
Expand Down Expand Up @@ -122,6 +127,10 @@ public void update(@NonNull Raw<?> data) {
this.barometer.add((Raw<AtmosphericPressure>) data);
return;
}
if (value instanceof Location) {
this.gps.add((Raw<Location>) data);
return;
}

throw new UnsupportedOperationException(data.getClass().getCanonicalName() + " " + data.value().getClass().getCanonicalName());
}
Expand All @@ -138,6 +147,7 @@ public void clear() {
this.cyclingPower = null;
this.runningDistanceSpeedCadence = null;
this.barometer = null;
this.gps = null;
}

public void fillTrackPoint(TrackPoint trackPoint) {
Expand Down Expand Up @@ -169,6 +179,10 @@ public void fillTrackPoint(TrackPoint trackPoint) {
trackPoint.setAltitudeGain(barometer.getValue().gain_m());
trackPoint.setAltitudeLoss(barometer.getValue().loss_m());
}

if (gps != null && gps.hasValue()) {
trackPoint.setLocation(gps.getValue());
}
}

public void reset() {
Expand All @@ -180,6 +194,7 @@ public void reset() {
if (cyclingPower != null) cyclingPower.reset();
if (runningDistanceSpeedCadence != null) runningDistanceSpeedCadence.reset();
if (barometer != null) barometer.reset();
if (gps != null) gps.reset();
}

private void set(@NonNull Aggregator<?, ?> type, @Nullable Aggregator<?, ?> sensorData) {
Expand Down Expand Up @@ -209,6 +224,10 @@ private void set(@NonNull Aggregator<?, ?> type, @Nullable Aggregator<?, ?> sens
barometer = (AggregatorBarometer) sensorData;
return;
}
if (type instanceof AggregatorGPS) {
gps = (AggregatorGPS) sensorData;
return;
}

throw new UnsupportedOperationException(type.getClass().getCanonicalName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public TrackRecordingService getService() {
private final Runnable updateRecordingData = new Runnable() {
@Override
public void run() {
updateRecordingDataWhileRecording();
// updateRecordingDataWhileRecording();

TrackRecordingService.this.handler.postDelayed(this, RECORDING_DATA_UPDATE_INTERVAL.toMillis());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.TrackPoint;
import de.dennisguse.opentracks.sensors.SensorConnector;
import de.dennisguse.opentracks.sensors.SensorManager;
import de.dennisguse.opentracks.sensors.sensorData.AggregatorGPS;
import de.dennisguse.opentracks.sensors.sensorData.Raw;
import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.util.LocationUtils;
import de.dennisguse.opentracks.util.PermissionRequester;
Expand All @@ -32,6 +35,8 @@ public class GPSManager implements SensorConnector, LocationListenerCompat, GpsS
private static final String LOCATION_PROVIDER = LocationManager.GPS_PROVIDER;

private TrackPointCreator trackPointCreator;

private SensorManager.SensorDataChangedObserver listener;
private Context context;
private Handler handler;

Expand All @@ -40,8 +45,9 @@ public class GPSManager implements SensorConnector, LocationListenerCompat, GpsS
private Duration gpsInterval;
private Distance thresholdHorizontalAccuracy;

public GPSManager(TrackPointCreator trackPointCreator) {
public GPSManager(TrackPointCreator trackPointCreator, SensorManager.SensorDataChangedObserver listener) {
this.trackPointCreator = trackPointCreator;
this.listener = listener;
}

public void start(@NonNull Context context, @NonNull Handler handler) {
Expand All @@ -52,6 +58,9 @@ public void start(@NonNull Context context, @NonNull Handler handler) {

gpsStatusManager = new GpsStatusManager(context, this, handler);
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

listener.onConnect(new AggregatorGPS("internal"));

registerLocationListener();
gpsStatusManager.start();
}
Expand All @@ -72,6 +81,9 @@ public void stop(Context context) {
gpsStatusManager.stop();
gpsStatusManager = null;

listener.onDisconnect(new AggregatorGPS("internal"));
listener = null;

trackPointCreator = null;
}

Expand Down Expand Up @@ -126,7 +138,7 @@ public void onLocationChanged(@NonNull Location location) {
return;
}

trackPointCreator.onChange(location);
listener.onChange(new Raw<>(location));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Handler;
import android.util.Log;
import android.util.Pair;
Expand Down Expand Up @@ -69,10 +68,6 @@ public void stop() {
this.context = null;
}

public synchronized void onChange(@NonNull Location location) {
onNewTrackPoint(new TrackPoint(location, createNow()));
}

/**
* Got a new TrackPoint from Bluetooth only; contains no GPS location.
*/
Expand Down

0 comments on commit 360c632

Please sign in to comment.