Barometer: added settings for sensor selection.

Part of #1424.
This commit is contained in:
Dennis Guse
2023-11-22 20:24:12 +01:00
parent 892c56389b
commit 490141db2a
12 changed files with 111 additions and 27 deletions
@@ -7,6 +7,8 @@ import java.util.UUID;
import de.dennisguse.opentracks.data.models.AtmosphericPressure;
import de.dennisguse.opentracks.sensors.sensorData.Aggregator;
import de.dennisguse.opentracks.sensors.sensorData.AggregatorBarometer;
import de.dennisguse.opentracks.sensors.sensorData.Raw;
import de.dennisguse.opentracks.sensors.sensorData.SensorHandlerInterface;
public class BluetoothHandlerBarometricPressure implements SensorHandlerInterface {
@@ -23,12 +25,14 @@ public class BluetoothHandlerBarometricPressure implements SensorHandlerInterfac
@Override
public Aggregator<?, ?> createEmptySensorData(String address, String name) {
return null; //TODO
return new AggregatorBarometer(address, name);
}
@Override
public void handlePayload(SensorManager.SensorDataChangedObserver observer, ServiceMeasurementUUID serviceMeasurementUUID, String sensorName, String address, BluetoothGattCharacteristic characteristic) {
//TODO
AtmosphericPressure value = parseEnvironmentalSensing(characteristic);
if (value == null) return;
observer.onChange(new Raw<>(value));
}
public static AtmosphericPressure parseEnvironmentalSensing(BluetoothGattCharacteristic characteristic) {
@@ -29,7 +29,7 @@ public class GainManager implements SensorConnector {
driver.connect(context, handler, this);
if (driver.isConnected()) {
listener.onConnect(new AggregatorBarometer("internal"));
listener.onConnect(new AggregatorBarometer("internal", null));
}
}
@@ -37,7 +37,7 @@ public class GainManager implements SensorConnector {
Log.d(TAG, "Stop");
driver.disconnect(context);
listener.onDisconnect(new AggregatorBarometer("internal"));
listener.onDisconnect(new AggregatorBarometer("internal", null));
}
public void onSensorValueChanged(AtmosphericPressure currentSensorValue) {
@@ -11,8 +11,8 @@ public class AggregatorBarometer extends Aggregator<AtmosphericPressure, Altitud
private AtmosphericPressure lastAcceptedSensorValue;
public AggregatorBarometer(String sensorAddress) {
super(sensorAddress);
public AggregatorBarometer(String sensorAddress, String sensorName) {
super(sensorAddress, sensorName);
}
@Override
@@ -256,6 +256,18 @@ public class PreferencesUtils {
return getBluetoothSensorAddressNone().equals(currentValue);
}
private static String getBluetoothSensorAddressInternal() {
return resources.getString(R.string.sensor_type_value_internal);
}
public static boolean isBluetoothSensorAddressInternal(String currentValue) {
return getBluetoothSensorAddressInternal().equals(currentValue);
}
public static String getBluetoothPressureAddress() {
return getString(R.string.settings_sensor_bluetooth_pressure_key, getBluetoothSensorAddressNone());
}
public static String getBluetoothHeartRateSensorAddress() {
return getString(R.string.settings_sensor_bluetooth_heart_rate_key, getBluetoothSensorAddressNone());
}
@@ -5,6 +5,8 @@ import android.util.AttributeSet;
import androidx.preference.PreferenceDialogFragmentCompat;
import java.util.List;
import de.dennisguse.opentracks.sensors.BluetoothHandlerManagerCyclingPower;
public class BluetoothLeCyclingPowerPreference extends BluetoothLeSensorPreference {
@@ -27,6 +29,6 @@ public class BluetoothLeCyclingPowerPreference extends BluetoothLeSensorPreferen
@Override
public PreferenceDialogFragmentCompat createInstance() {
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothHandlerManagerCyclingPower.CYCLING_POWER);
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), List.of(BluetoothHandlerManagerCyclingPower.CYCLING_POWER));
}
}
@@ -5,6 +5,8 @@ import android.util.AttributeSet;
import androidx.preference.PreferenceDialogFragmentCompat;
import java.util.List;
import de.dennisguse.opentracks.sensors.BluetoothHandlerCyclingDistanceSpeed;
public class BluetoothLeCyclingSpeedPreference extends BluetoothLeSensorPreference {
@@ -27,6 +29,6 @@ public class BluetoothLeCyclingSpeedPreference extends BluetoothLeSensorPreferen
@Override
public PreferenceDialogFragmentCompat createInstance() {
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothHandlerCyclingDistanceSpeed.CYCLING_SPEED_CADENCE);
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), List.of(BluetoothHandlerCyclingDistanceSpeed.CYCLING_SPEED_CADENCE));
}
}
@@ -0,0 +1,41 @@
package de.dennisguse.opentracks.settings.bluetooth;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import androidx.preference.PreferenceDialogFragmentCompat;
import java.util.List;
import de.dennisguse.opentracks.sensors.BluetoothHandlerBarometricPressure;
public class BluetoothLePressurePreference extends BluetoothLeSensorPreference {
public BluetoothLePressurePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public BluetoothLePressurePreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public BluetoothLePressurePreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BluetoothLePressurePreference(Context context) {
super(context);
}
@Override
public PreferenceDialogFragmentCompat createInstance() {
SensorManager sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
Sensor pressureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
boolean hasInternalSensor = pressureSensor != null;
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog
.newInstance(getKey(), List.of(BluetoothHandlerBarometricPressure.BAROMETRIC_PRESSURE), hasInternalSensor);
}
}
@@ -5,6 +5,8 @@ import android.util.AttributeSet;
import androidx.preference.PreferenceDialogFragmentCompat;
import java.util.List;
import de.dennisguse.opentracks.sensors.BluetoothHandlerRunningSpeedAndCadence;
public class BluetoothLeRunningSpeedAndCadencePreference extends BluetoothLeSensorPreference {
@@ -27,6 +29,6 @@ public class BluetoothLeRunningSpeedAndCadencePreference extends BluetoothLeSens
@Override
public PreferenceDialogFragmentCompat createInstance() {
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothHandlerRunningSpeedAndCadence.RUNNING_SPEED_CADENCE);
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), List.of(BluetoothHandlerRunningSpeedAndCadence.RUNNING_SPEED_CADENCE));
}
}
@@ -23,7 +23,6 @@ import androidx.preference.PreferenceDialogFragmentCompat;
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@@ -43,9 +42,12 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
private static final String TAG = BluetoothLeSensorPreference.class.getSimpleName();
private static final String ARG_BLE_SERVICE_UUIDS = "bluetoothUUID";
private static final String ARG_INCLUDE_INTERNAL = "supportsInternal";
private static final int DEVICE_NONE_RESOURCEID = R.string.value_none;
private static final int SENSOR_INTERNAL_RESOURCEID = R.string.value_internal_sensor;
public BluetoothLeSensorPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@@ -65,11 +67,7 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
private String value;
private boolean valueSet = false;
public String getValue() {
return value;
}
public void setValue(String value) {
private void setValue(String value) {
final boolean changed = !TextUtils.equals(this.value, value);
if (changed || !valueSet) {
this.value = value;
@@ -88,21 +86,24 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
@Override
public CharSequence getSummary() {
if (getValue() == null || PreferencesUtils.isBluetoothSensorAddressNone(getValue())) {
if (value == null || PreferencesUtils.isBluetoothSensorAddressNone(value)) {
return getContext().getString(DEVICE_NONE_RESOURCEID);
}
if (PreferencesUtils.isBluetoothSensorAddressInternal(value)) {
return getContext().getString(SENSOR_INTERNAL_RESOURCEID);
}
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(getContext());
if (bluetoothAdapter == null) {
Log.w(TAG, "No Bluetooth adapter present");
return getValue();
return value;
}
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(getValue());
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(value);
if (device != null && device.getName() != null) {
return getContext().getString(R.string.bluetooth_sensor_summary, device.getAddress(), device.getName());
}
return getValue();
return value;
}
public abstract PreferenceDialogFragmentCompat createInstance();
@@ -138,18 +139,20 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
}
};
public static BluetoothLeSensorPreferenceDialog newInstance(String preferenceKey, ServiceMeasurementUUID sensorUUID) {
return newInstance(preferenceKey, Collections.singletonList(sensorUUID));
public static BluetoothLeSensorPreferenceDialog newInstance(String preferenceKey, List<ServiceMeasurementUUID> sensorUUIDs) {
return newInstance(preferenceKey, sensorUUIDs, false);
}
public static BluetoothLeSensorPreferenceDialog newInstance(String preferenceKey, List<ServiceMeasurementUUID> sensorUUIDs) {
public static BluetoothLeSensorPreferenceDialog newInstance(String preferenceKey, List<ServiceMeasurementUUID> sensorUUIDs, boolean includeInternalSensor) {
final BluetoothLeSensorPreferenceDialog fragment = new BluetoothLeSensorPreferenceDialog();
final Bundle b = new Bundle(1);
final Bundle b = new Bundle(3);
b.putString(ARG_KEY, preferenceKey);
b.putParcelableArrayList(ARG_BLE_SERVICE_UUIDS, new ArrayList<>(sensorUUIDs.stream()
.map(ServiceMeasurementUUID::serviceUUID)
.map(ParcelUuid::new)
.collect(Collectors.toList())));
b.putBoolean(ARG_INCLUDE_INTERNAL, includeInternalSensor);
fragment.setArguments(b);
return fragment;
}
@@ -179,6 +182,7 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
private void startBluetoothScan() {
List<ParcelUuid> serviceUUIDs = getArguments().getParcelableArrayList(ARG_BLE_SERVICE_UUIDS);
boolean includeInternalSensor = getArguments().getBoolean(ARG_INCLUDE_INTERNAL);
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(getContext());
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
@@ -201,14 +205,23 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
}
String deviceNone = getContext().getString(R.string.sensor_type_value_none);
String sensorInternal = getString(R.string.sensor_type_value_internal);
listAdapter.add(getContext().getString(DEVICE_NONE_RESOURCEID), deviceNone);
selectedEntryIndex = 0;
BluetoothLeSensorPreference preference = (BluetoothLeSensorPreference) getPreference();
String deviceSelected = preference.getValue();
if (deviceSelected != null && !deviceNone.equals(deviceSelected)) {
listAdapter.add(preference.getValue(), preference.getValue());
selectedEntryIndex = 1;
String deviceSelected = preference.value;
if (includeInternalSensor) {
listAdapter.add(getString(SENSOR_INTERNAL_RESOURCEID), sensorInternal);
if (sensorInternal.equals(deviceSelected)) {
selectedEntryIndex = 1;
}
}
if (deviceSelected != null && !deviceNone.equals(deviceSelected) && !sensorInternal.equals(deviceSelected)) {
listAdapter.add(preference.value, preference.value);
selectedEntryIndex++;
}
List<ScanFilter> scanFilter = null;