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)); Mockito.when(barometer.getValue()).thenReturn(new AltitudeGainLoss(altitudeGain, altitudeGain));
sensorDataSet.add(barometer); sensorDataSet.add(barometer);
} else { } else {
sensorDataSet.add(new AggregatorBarometer("")); sensorDataSet.add(new AggregatorBarometer("test", null));
} }
} }
@@ -16,7 +16,7 @@ public class AggregatorBarometerTest {
@Test @Test
public void getAltitudeGainLoss_downhill() { public void getAltitudeGainLoss_downhill() {
// given // given
AggregatorBarometer subject = new AggregatorBarometer(""); AggregatorBarometer subject = new AggregatorBarometer("test", null);
// then // 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}); 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; return;
} }
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); if (!descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)) {
gatt.writeDescriptor(descriptor); 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 @Override
@@ -105,7 +105,7 @@ public class BluetoothRemoteSensorManager implements SensorConnector, SharedPref
return; return;
} }
if (PreferencesUtils.isBluetoothSensorAddressNone(address)) { if (SensorType.NONE.getPreferenceValue().equals(address)) {
Log.w(TAG, "No Bluetooth address."); Log.w(TAG, "No Bluetooth address.");
connectionManager.disconnect(); connectionManager.disconnect();
return; return;
@@ -1,13 +1,17 @@
package de.dennisguse.opentracks.sensors; package de.dennisguse.opentracks.sensors;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import de.dennisguse.opentracks.data.models.AtmosphericPressure; import de.dennisguse.opentracks.data.models.AtmosphericPressure;
import de.dennisguse.opentracks.sensors.driver.BarometerInternal; import de.dennisguse.opentracks.sensors.driver.BarometerInternal;
import de.dennisguse.opentracks.sensors.sensorData.AggregatorBarometer; import de.dennisguse.opentracks.sensors.sensorData.AggregatorBarometer;
import de.dennisguse.opentracks.sensors.sensorData.Raw; 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). * 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 static final String TAG = GainManager.class.getSimpleName();
private final BarometerInternal driver; private BarometerInternal driver;
private final SensorManager.SensorDataChangedObserver listener; private final SensorManager.SensorDataChangedObserver listener;
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = (sharedPreferences, key) -> {
connect();
};
private Context context;
private Handler handler;
public GainManager(SensorManager.SensorDataChangedObserver listener) { public GainManager(SensorManager.SensorDataChangedObserver listener) {
this.listener = listener; this.listener = listener;
driver = new BarometerInternal(); driver = new BarometerInternal();
} }
public void start(Context context, Handler handler) { public void start(Context context, Handler handler) {
driver.connect(context, handler, this); this.context = context;
this.handler = handler;
if (driver.isConnected()) { PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
listener.onConnect(new AggregatorBarometer("internal", null));
}
} }
public void stop(Context context) { public void stop(Context context) {
Log.d(TAG, "Stop"); Log.d(TAG, "Stop");
this.context = null;
this.handler = null;
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
driver.disconnect(context); onDisconnect(context);
listener.onDisconnect(new AggregatorBarometer("internal", null));
} }
public void onSensorValueChanged(AtmosphericPressure currentSensorValue) { public void onSensorValueChanged(AtmosphericPressure currentSensorValue) {
listener.onChange(new Raw<>(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.data.models.Track;
import de.dennisguse.opentracks.io.file.TrackFileFormat; import de.dennisguse.opentracks.io.file.TrackFileFormat;
import de.dennisguse.opentracks.io.file.TrackFilenameGenerator; 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.CsvLayoutUtils;
import de.dennisguse.opentracks.ui.customRecordingLayout.RecordingLayout; import de.dennisguse.opentracks.ui.customRecordingLayout.RecordingLayout;
import de.dennisguse.opentracks.ui.customRecordingLayout.RecordingLayoutIO; import de.dennisguse.opentracks.ui.customRecordingLayout.RecordingLayoutIO;
@@ -249,22 +250,21 @@ public class PreferencesUtils {
} }
private static String getBluetoothSensorAddressNone() { private static String getBluetoothSensorAddressNone() {
return resources.getString(R.string.sensor_type_value_none); return SensorType.NONE.getPreferenceValue();
} }
public static boolean isBluetoothSensorAddressNone(String currentValue) { public static boolean isBluetoothSensorAddressNone(String currentValue) {
return getBluetoothSensorAddressNone().equals(currentValue); return getBluetoothSensorAddressNone().equals(currentValue);
} }
private static String getBluetoothSensorAddressInternal() { public static SensorType getSensorType(String address) {
return resources.getString(R.string.sensor_type_value_internal); return Arrays.stream(SensorType.values())
.filter(it -> it.getPreferenceValue().equals(address))
.findFirst()
.orElse(SensorType.REMOTE);
} }
public static boolean isBluetoothSensorAddressInternal(String currentValue) { public static String getBarometerSensorAddress() {
return getBluetoothSensorAddressInternal().equals(currentValue);
}
public static String getBluetoothPressureAddress() {
return getString(R.string.settings_sensor_bluetooth_pressure_key, getBluetoothSensorAddressNone()); return getString(R.string.settings_sensor_bluetooth_pressure_key, getBluetoothSensorAddressNone());
} }
@@ -7,9 +7,7 @@ import android.util.AttributeSet;
import androidx.preference.PreferenceDialogFragmentCompat; import androidx.preference.PreferenceDialogFragmentCompat;
import java.util.List; import java.util.Collections;
import de.dennisguse.opentracks.sensors.BluetoothHandlerBarometricPressure;
public class BluetoothLePressurePreference extends BluetoothLeSensorPreference { public class BluetoothLePressurePreference extends BluetoothLeSensorPreference {
@@ -36,6 +34,7 @@ public class BluetoothLePressurePreference extends BluetoothLeSensorPreference {
boolean hasInternalSensor = pressureSensor != null; boolean hasInternalSensor = pressureSensor != null;
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog 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.R;
import de.dennisguse.opentracks.sensors.BluetoothUtils; import de.dennisguse.opentracks.sensors.BluetoothUtils;
import de.dennisguse.opentracks.sensors.SensorType;
import de.dennisguse.opentracks.sensors.ServiceMeasurementUUID; import de.dennisguse.opentracks.sensors.ServiceMeasurementUUID;
import de.dennisguse.opentracks.settings.PreferencesUtils; import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.util.PermissionRequester; import de.dennisguse.opentracks.util.PermissionRequester;
@@ -86,10 +87,10 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
@Override @Override
public CharSequence getSummary() { public CharSequence getSummary() {
if (value == null || PreferencesUtils.isBluetoothSensorAddressNone(value)) { if (value == null || SensorType.NONE.getPreferenceValue().equals(value)) {
return getContext().getString(DEVICE_NONE_RESOURCEID); return getContext().getString(DEVICE_NONE_RESOURCEID);
} }
if (PreferencesUtils.isBluetoothSensorAddressInternal(value)) { if (SensorType.INTERNAL.getPreferenceValue().equals(value)) {
return getContext().getString(SENSOR_INTERNAL_RESOURCEID); return getContext().getString(SENSOR_INTERNAL_RESOURCEID);
} }