Barometer: select INTERNAL or NONE (also while recording).

Part of #1424.
This commit is contained in:
Dennis Guse
2023-12-27 18:43:47 +01:00
parent 490141db2a
commit 94540606ef
9 changed files with 86 additions and 28 deletions
@@ -586,7 +586,7 @@ public class ExportImportTest {
Mockito.when(barometer.getValue()).thenReturn(new AltitudeGainLoss(altitudeGain, altitudeGain));
sensorDataSet.add(barometer);
} else {
sensorDataSet.add(new AggregatorBarometer(""));
sensorDataSet.add(new AggregatorBarometer("test", null));
}
}
@@ -16,7 +16,7 @@ public class AggregatorBarometerTest {
@Test
public void getAltitudeGainLoss_downhill() {
// given
AggregatorBarometer subject = new AggregatorBarometer("");
AggregatorBarometer subject = new AggregatorBarometer("test", null);
// then
addSensorValue(subject, new float[]{1015f, 1015.01f, 1015.02f, 1015.03f, 1015.04f, 1015.05f, 1015.06f, 1015.07f, 1015.08f, 1015.09f, 1015.10f, 1015.11f, 1015.12f, 1015.13f, 1018f, 1018.1f, 1018.1f, 1018.1f, 1018.1f});
@@ -101,9 +101,12 @@ public class BluetoothConnectionManager {
return;
}
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
if (!descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)) {
Log.e(TAG, "CLIENT_CHARACTERISTIC_CONFIG_UUID could not be set to ENABLE_NOTIFICATION_VALUE");
}
if (!gatt.writeDescriptor(descriptor)) {
Log.e(TAG, "CLIENT_CHARACTERISTIC_CONFIG_UUID descriptor could not be written");
}
}
@Override
@@ -105,7 +105,7 @@ public class BluetoothRemoteSensorManager implements SensorConnector, SharedPref
return;
}
if (PreferencesUtils.isBluetoothSensorAddressNone(address)) {
if (SensorType.NONE.getPreferenceValue().equals(address)) {
Log.w(TAG, "No Bluetooth address.");
connectionManager.disconnect();
return;
@@ -1,13 +1,17 @@
package de.dennisguse.opentracks.sensors;
import android.content.Context;
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.sensorData.AggregatorBarometer;
import de.dennisguse.opentracks.sensors.sensorData.Raw;
import de.dennisguse.opentracks.settings.PreferencesUtils;
/**
* Estimates the altitude gain and altitude loss using the device's pressure sensor (i.e., barometer).
@@ -16,31 +20,65 @@ public class GainManager implements SensorConnector {
private static final String TAG = GainManager.class.getSimpleName();
private final BarometerInternal driver;
private BarometerInternal driver;
private final SensorManager.SensorDataChangedObserver listener;
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = (sharedPreferences, key) -> {
connect();
};
private Context context;
private Handler handler;
public GainManager(SensorManager.SensorDataChangedObserver listener) {
this.listener = listener;
driver = new BarometerInternal();
}
public void start(Context context, Handler handler) {
driver.connect(context, handler, this);
if (driver.isConnected()) {
listener.onConnect(new AggregatorBarometer("internal", null));
}
this.context = context;
this.handler = handler;
PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}
public void stop(Context context) {
Log.d(TAG, "Stop");
this.context = null;
this.handler = null;
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
driver.disconnect(context);
listener.onDisconnect(new AggregatorBarometer("internal", null));
onDisconnect(context);
}
public void onSensorValueChanged(AtmosphericPressure currentSensorValue) {
listener.onChange(new Raw<>(currentSensorValue));
}
private void connect() {
onDisconnect(context);
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
default -> throw new RuntimeException("Not implemented");
}
if (driver != null) {
driver.connect(context, handler, this);
if (driver.isConnected()) {
listener.onConnect(new AggregatorBarometer("internal", null));
}
}
}
private void onDisconnect(@NonNull Context context) {
if (driver == null) return;
driver.disconnect(context);
listener.onDisconnect(new AggregatorBarometer("internal", null));
}
}
@@ -0,0 +1,17 @@
package de.dennisguse.opentracks.sensors;
public enum SensorType {
NONE("NONE"),
INTERNAL("INTERNAL"),
REMOTE("*");
private final String preferenceValue;
SensorType(String preferenceValue) {
this.preferenceValue = preferenceValue;
}
public String getPreferenceValue() {
return preferenceValue;
}
}
@@ -48,6 +48,7 @@ import de.dennisguse.opentracks.data.models.HeartRateZones;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.io.file.TrackFileFormat;
import de.dennisguse.opentracks.io.file.TrackFilenameGenerator;
import de.dennisguse.opentracks.sensors.SensorType;
import de.dennisguse.opentracks.ui.customRecordingLayout.CsvLayoutUtils;
import de.dennisguse.opentracks.ui.customRecordingLayout.RecordingLayout;
import de.dennisguse.opentracks.ui.customRecordingLayout.RecordingLayoutIO;
@@ -249,22 +250,21 @@ public class PreferencesUtils {
}
private static String getBluetoothSensorAddressNone() {
return resources.getString(R.string.sensor_type_value_none);
return SensorType.NONE.getPreferenceValue();
}
public static boolean isBluetoothSensorAddressNone(String currentValue) {
return getBluetoothSensorAddressNone().equals(currentValue);
}
private static String getBluetoothSensorAddressInternal() {
return resources.getString(R.string.sensor_type_value_internal);
public static SensorType getSensorType(String address) {
return Arrays.stream(SensorType.values())
.filter(it -> it.getPreferenceValue().equals(address))
.findFirst()
.orElse(SensorType.REMOTE);
}
public static boolean isBluetoothSensorAddressInternal(String currentValue) {
return getBluetoothSensorAddressInternal().equals(currentValue);
}
public static String getBluetoothPressureAddress() {
public static String getBarometerSensorAddress() {
return getString(R.string.settings_sensor_bluetooth_pressure_key, getBluetoothSensorAddressNone());
}
@@ -7,9 +7,7 @@ import android.util.AttributeSet;
import androidx.preference.PreferenceDialogFragmentCompat;
import java.util.List;
import de.dennisguse.opentracks.sensors.BluetoothHandlerBarometricPressure;
import java.util.Collections;
public class BluetoothLePressurePreference extends BluetoothLeSensorPreference {
@@ -36,6 +34,7 @@ public class BluetoothLePressurePreference extends BluetoothLeSensorPreference {
boolean hasInternalSensor = pressureSensor != null;
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog
.newInstance(getKey(), List.of(BluetoothHandlerBarometricPressure.BAROMETRIC_PRESSURE), hasInternalSensor);
// .newInstance(getKey(), List.of(BluetoothHandlerBarometricPressure.BAROMETRIC_PRESSURE), hasInternalSensor); //TODO #1424
.newInstance(getKey(), Collections.emptyList(), hasInternalSensor);
}
}
@@ -28,6 +28,7 @@ import java.util.stream.Collectors;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.sensors.BluetoothUtils;
import de.dennisguse.opentracks.sensors.SensorType;
import de.dennisguse.opentracks.sensors.ServiceMeasurementUUID;
import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.util.PermissionRequester;
@@ -86,10 +87,10 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
@Override
public CharSequence getSummary() {
if (value == null || PreferencesUtils.isBluetoothSensorAddressNone(value)) {
if (value == null || SensorType.NONE.getPreferenceValue().equals(value)) {
return getContext().getString(DEVICE_NONE_RESOURCEID);
}
if (PreferencesUtils.isBluetoothSensorAddressInternal(value)) {
if (SensorType.INTERNAL.getPreferenceValue().equals(value)) {
return getContext().getString(SENSOR_INTERNAL_RESOURCEID);
}