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