forked from upstream-mirrors/OpenTracks
Implement gain/loss via Bluetooth LE Environmental Sensing Sensor.
Fixes #1424.
This commit is contained in:
@@ -5,12 +5,9 @@ import android.content.SharedPreferences;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.AtmosphericPressure;
|
||||
import de.dennisguse.opentracks.sensors.driver.BarometerInternal;
|
||||
import de.dennisguse.opentracks.sensors.driver.Driver;
|
||||
import de.dennisguse.opentracks.sensors.sensorData.AggregatorBarometer;
|
||||
import de.dennisguse.opentracks.sensors.sensorData.Raw;
|
||||
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
||||
|
||||
/**
|
||||
@@ -20,7 +17,6 @@ public class GainManager implements SensorConnector {
|
||||
|
||||
private static final String TAG = GainManager.class.getSimpleName();
|
||||
|
||||
private BarometerInternal driver;
|
||||
|
||||
private final SensorManager.SensorDataChangedObserver listener;
|
||||
|
||||
@@ -30,10 +26,10 @@ public class GainManager implements SensorConnector {
|
||||
|
||||
private Context context;
|
||||
private Handler handler;
|
||||
private Driver driver;
|
||||
|
||||
public GainManager(SensorManager.SensorDataChangedObserver listener) {
|
||||
this.listener = listener;
|
||||
driver = new BarometerInternal();
|
||||
}
|
||||
|
||||
public void start(Context context, Handler handler) {
|
||||
@@ -48,37 +44,34 @@ public class GainManager implements SensorConnector {
|
||||
this.handler = null;
|
||||
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||
|
||||
onDisconnect(context);
|
||||
}
|
||||
|
||||
public void onSensorValueChanged(AtmosphericPressure currentSensorValue) {
|
||||
listener.onChange(new Raw<>(currentSensorValue));
|
||||
onDisconnect();
|
||||
}
|
||||
|
||||
private void connect() {
|
||||
onDisconnect(context);
|
||||
onDisconnect();
|
||||
|
||||
String address = PreferencesUtils.getBarometerSensorAddress();
|
||||
switch (PreferencesUtils.getSensorType(address)) {
|
||||
case NONE -> driver = null;
|
||||
case INTERNAL -> driver = new BarometerInternal();
|
||||
case REMOTE -> throw new RuntimeException("Not implemented"); //TODO #1424
|
||||
case INTERNAL -> driver = new BarometerInternal(listener);
|
||||
case REMOTE -> driver =
|
||||
new BluetoothConnectionManager(
|
||||
BluetoothUtils.getAdapter(context),
|
||||
listener,
|
||||
new BluetoothHandlerBarometricPressure()
|
||||
);
|
||||
default -> throw new RuntimeException("Not implemented");
|
||||
}
|
||||
|
||||
if (driver != null) {
|
||||
driver.connect(context, handler, this);
|
||||
|
||||
if (driver.isConnected()) {
|
||||
listener.onConnect(new AggregatorBarometer("internal", null));
|
||||
}
|
||||
driver.connect(context, handler, address);
|
||||
}
|
||||
}
|
||||
|
||||
private void onDisconnect(@NonNull Context context) {
|
||||
private void onDisconnect() {
|
||||
if (driver == null) return;
|
||||
|
||||
driver.disconnect(context);
|
||||
listener.onDisconnect(new AggregatorBarometer("internal", null));
|
||||
driver.disconnect();
|
||||
listener.onDisconnect(new AggregatorBarometer("GainManager", null));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user