DriverObserver: renamed methods and all drivers properly use onConnected, onConnectionLost and onDisconnected.

This commit is contained in:
Dennis Guse
2026-02-02 21:15:50 +01:00
parent dfa80d61d7
commit 1c767c41a7
8 changed files with 62 additions and 60 deletions
@@ -756,6 +756,6 @@ public class ExportImportTest {
mockAltitudeChange(trackPointCreator, altitudeGain);
trackPointCreator.setClock(time);
trackPointCreator.getSensorManager().getGpsManager().onData(location);
trackPointCreator.getSensorManager().getGpsManager().onDataReceived(location);
}
}
@@ -995,6 +995,6 @@ public class TrackRecordingServiceRecordingTest {
location.setSpeed(speed);
trackPointCreator.setClock(time);
trackPointCreator.getSensorManager().getGpsManager().onData(location);
trackPointCreator.getSensorManager().getGpsManager().onDataReceived(location);
}
}
@@ -120,22 +120,22 @@ public class GpsManager implements DriverObserver<Location>, GpsStatusManager.Gp
}
@Override
public void onConnect(String sensorAddress, String sensorName) {
public void onConnected(String sensorAddress, String sensorName) {
gpsStatusManager.onGpsEnabled();
}
@Override
public void onDisconnect() {
public void onConnectionLost() {
gpsStatusManager.onGpsDisabled();
}
@Override
public void onRemove() {
public void onDisconnected() {
gpsStatusManager.onGpsDisabled();
}
@Override
public void onData(Location location) {
public void onDataReceived(Location location) {
// Send each update to the status; please note that this TrackPoint is not stored.
Position position = Position.of(location, observer.getNow());
gpsStatusManager.onNewTrackPoint(position);
@@ -25,23 +25,23 @@ public abstract class SensorHandler<AggregatorInput, AggregatorOutput> {
protected final DriverObserver<AggregatorInput> driverObserver = new DriverObserver<>() {
@Override
public void onConnect(String sensorAddress, String sensorName) {
public void onConnected(String sensorAddress, String sensorName) {
SensorHandler.this.aggregator = createAggregator(sensorAddress, sensorName);
}
@Override
public void onDisconnect() {
public void onConnectionLost() {
SensorHandler.this.aggregator.resetAggregated();
}
@Override
public void onData(AggregatorInput value) {
public void onDataReceived(AggregatorInput value) {
SensorHandler.this.aggregator.add(sensorManager.getNow(), value);
sensorManager.onChange();
}
@Override
public void onRemove() {
public void onDisconnected() {
SensorHandler.this.aggregator = null;
}
};
@@ -57,6 +57,7 @@ public abstract class SensorHandler<AggregatorInput, AggregatorOutput> {
switch (PreferencesUtils.getSensorType(address)) {
case NONE -> {
driverObserver.onDisconnected();
return;
}
case INTERNAL -> {
@@ -21,7 +21,7 @@ public class BarometerInternal implements Driver {
private final DriverObserver<AtmosphericPressure> observer;
private Context context;
private android.hardware.SensorManager sensorService;
private final SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
@@ -31,7 +31,7 @@ public class BarometerInternal implements Driver {
return;
}
observer.onData(AtmosphericPressure.ofHPA(event.values[0]));
observer.onDataReceived(AtmosphericPressure.ofHPA(event.values[0]));
}
@Override
@@ -46,34 +46,28 @@ public class BarometerInternal implements Driver {
@Override
public void connect(Context context, Handler handler, String addressIgnored) {
sensorService = (android.hardware.SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor pressureSensor = sensorService.getDefaultSensor(Sensor.TYPE_PRESSURE);
android.hardware.SensorManager sensorManager = (android.hardware.SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor pressureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
if (pressureSensor == null) {
Log.w(TAG, "No pressure sensor available.");
if (pressureSensor != null && sensorService.registerListener(sensorEventListener, pressureSensor, SAMPLING_PERIOD, handler)) {
observer.onConnected(null, null);
return;
}
if (sensorManager.registerListener(sensorEventListener, pressureSensor, SAMPLING_PERIOD, handler)) {
this.context = context;
observer.onConnect(null, null);
return;
}
disconnect();
Log.w(TAG, "No pressure sensor available.");
observer.onDisconnected();
}
@Override
public boolean isConnected() {
return context != null;
return sensorService != null;
}
@Override
public void disconnect() {
if (!isConnected()) return;
android.hardware.SensorManager sensorManager = (android.hardware.SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensorManager.unregisterListener(sensorEventListener);
this.context = null;
sensorService.unregisterListener(sensorEventListener);
sensorService = null;
}
}
@@ -56,27 +56,29 @@ public class BluetoothDriver<T> implements Driver {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTING ->
Log.i(TAG, gatt.getDevice() + ": connecting to sensor");
case BluetoothProfile.STATE_CONNECTING -> {
Log.i(TAG, gatt.getDevice() + ": connecting to sensor");
}
case BluetoothProfile.STATE_CONNECTED -> {
Log.i(TAG, gatt.getDevice() + ": connected to sensor; discovering services");
gatt.discoverServices();
}
case BluetoothProfile.STATE_DISCONNECTING ->
Log.i(TAG, gatt.getDevice() + ": disconnecting from sensor: ");
case BluetoothProfile.STATE_DISCONNECTING -> Log.i(TAG, gatt.getDevice() + ": disconnecting from sensor: ");
case BluetoothProfile.STATE_DISCONNECTED -> {
//This is also triggered, if no connection was established (ca. 30s)
Log.i(TAG, gatt.getDevice() + ": disconnected from sensor: trying to reconnect");
if (gatt.connect()) {
Log.e(TAG, gatt.getDevice() + ": could not trigger reconnect for sensor");
}
clearData();
observer.onConnectionLost();
}
}
}
@Override
public void onServicesDiscovered(@NonNull BluetoothGatt gatt, int status) {
BluetoothDevice device = gatt.getDevice();
BluetoothGattService gattService = null;
ServiceMeasurementUUID serviceMeasurement = null;
for (ServiceMeasurementUUID s : sensorHandler.getServices()) {
@@ -88,13 +90,13 @@ public class BluetoothDriver<T> implements Driver {
}
if (gattService == null) {
Log.e(TAG, gatt.getDevice() + ": could not get gattService for serviceUUID=" + serviceMeasurement);
Log.e(TAG, device + ": could not get gattService for serviceUUID=" + serviceMeasurement);
return;
}
BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(serviceMeasurement.measurementUUID());
if (characteristic == null) {
Log.e(TAG, gatt.getDevice() + ": could not get BluetoothCharacteristic for serviceUUID=" + serviceMeasurement.serviceUUID() + " characteristicUUID=" + serviceMeasurement.measurementUUID());
Log.e(TAG, device + ": could not get BluetoothCharacteristic for serviceUUID=" + serviceMeasurement.serviceUUID() + " characteristicUUID=" + serviceMeasurement.measurementUUID());
return;
}
gatt.setCharacteristicNotification(characteristic, true);
@@ -112,6 +114,8 @@ public class BluetoothDriver<T> implements Driver {
if (!gatt.writeDescriptor(descriptor)) {
Log.e(TAG, "CLIENT_CHARACTERISTIC_CONFIG_UUID descriptor could not be written");
}
observer.onConnected(device.getAddress(), device.getName());
}
@Override
@@ -132,7 +136,7 @@ public class BluetoothDriver<T> implements Driver {
if (value == null) {
return;
}
observer.onData(value);
observer.onDataReceived(value);
}
};
@@ -165,37 +169,23 @@ public class BluetoothDriver<T> implements Driver {
Log.d(TAG, device + ": trying to connect");
bluetoothGatt = device.connectGatt(context, false, connectCallback, BluetoothDevice.TRANSPORT_AUTO, 0, handler);
observer.onConnect(device.getAddress(), device.getName());
}
private synchronized void clearData() {
observer.onDisconnect();
}
@Override
public synchronized void disconnect() {
if (!isConnected()) {
Log.w(TAG, "Not connected; no need to re-connect.");
Log.w(TAG, "Not connected; cannot disconnect.");
return;
}
Log.i(TAG, bluetoothGatt.getDevice() + ": start disconnect");
bluetoothGatt.disconnect();
bluetoothGatt.close();
clearData();
Log.i(TAG, bluetoothGatt.getDevice() + ": disconnect finished");
bluetoothGatt = null;
observer.onDisconnected();
}
private synchronized boolean isSameBluetoothDevice(String address) {
if (bluetoothGatt == null) {
return false;
}
return address.equals(bluetoothGatt.getDevice().getAddress());
}
private boolean isBluetoothEnabled() {
return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
}
@@ -2,11 +2,24 @@ package de.dennisguse.opentracks.sensors.driver;
public interface DriverObserver<T> {
void onConnect(String sensorAddress, String sensorName);
/**
* The Driver could connect to the sensor.
*/
void onConnected(String sensorAddress, String sensorName);
void onDisconnect();
/**
* Sensor got temporarily disconnected, Driver should try to re-connect.
*/
void onConnectionLost();
void onData(T value);
/**
* The Driver provided data from the sensor.
*/
void onDataReceived(T value);
void onRemove();
/**
* The Driver was shutdown and no further data will be provided.
*/
//TODO check if really need this, because the driver was told to disconnect and then just signalizes that it finished this task.
void onDisconnected();
}
@@ -23,17 +23,17 @@ public class GpsInternal implements Driver {
private final LocationListenerCompat locationListenerCompat = new LocationListenerCompat() {
@Override
public void onLocationChanged(@NonNull Location location) {
driverObserver.onData(location);
driverObserver.onDataReceived(location);
}
@Override
public void onProviderEnabled(@NonNull String provider) {
driverObserver.onConnect(null, provider);
driverObserver.onConnected(null, provider);
}
@Override
public void onProviderDisabled(@NonNull String provider) {
driverObserver.onDisconnect();
driverObserver.onConnectionLost();
}
};
@@ -63,10 +63,14 @@ public class GpsInternal implements Driver {
try {
Log.i(TAG, "Register for location updates " + context);
LocationManagerCompat.requestLocationUpdates(locationManager, LOCATION_PROVIDER, locationRequest, handler::post, locationListenerCompat);
driverObserver.onConnected(null, null);
return;
} catch (SecurityException e) {
Log.e(TAG, "Could not register location listener; permissions not granted.", e);
}
}
driverObserver.onDisconnected();
}
@SuppressWarnings({"MissingPermission"})
@@ -77,7 +81,7 @@ public class GpsInternal implements Driver {
LocationManagerCompat.removeUpdates(locationManager, locationListenerCompat);
locationManager = null;
driverObserver.onDisconnect();
driverObserver.onDisconnected();
}