Skip to content

Commit

Permalink
Barometer: added settings for sensor selection.
Browse files Browse the repository at this point in the history
Part of #1424.
  • Loading branch information
dennisguse committed Dec 27, 2023
1 parent eaad33c commit 877382f
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

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 {
Expand All @@ -23,12 +25,14 @@ public List<ServiceMeasurementUUID> getServices() {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ 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 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import androidx.preference.PreferenceDialogFragmentCompat;

import java.util.List;

import de.dennisguse.opentracks.sensors.BluetoothHandlerManagerCyclingPower;

public class BluetoothLeCyclingPowerPreference extends BluetoothLeSensorPreference {
Expand All @@ -27,6 +29,6 @@ public BluetoothLeCyclingPowerPreference(Context context) {

@Override
public PreferenceDialogFragmentCompat createInstance() {
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothHandlerManagerCyclingPower.CYCLING_POWER);
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), List.of(BluetoothHandlerManagerCyclingPower.CYCLING_POWER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import androidx.preference.PreferenceDialogFragmentCompat;

import java.util.List;

import de.dennisguse.opentracks.sensors.BluetoothHandlerCyclingDistanceSpeed;

public class BluetoothLeCyclingSpeedPreference extends BluetoothLeSensorPreference {
Expand All @@ -27,6 +29,6 @@ public BluetoothLeCyclingSpeedPreference(Context context) {

@Override
public PreferenceDialogFragmentCompat createInstance() {
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothHandlerCyclingDistanceSpeed.CYCLING_SPEED_CADENCE);
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), List.of(BluetoothHandlerCyclingDistanceSpeed.CYCLING_SPEED_CADENCE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.dennisguse.opentracks.settings.bluetooth;

import android.content.Context;
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() {
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog
.newInstance(getKey(), List.of(BluetoothHandlerBarometricPressure.BAROMETRIC_PRESSURE), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import androidx.preference.PreferenceDialogFragmentCompat;

import java.util.List;

import de.dennisguse.opentracks.sensors.BluetoothHandlerRunningSpeedAndCadence;

public class BluetoothLeRunningSpeedAndCadencePreference extends BluetoothLeSensorPreference {
Expand All @@ -27,6 +29,6 @@ public BluetoothLeRunningSpeedAndCadencePreference(Context context) {

@Override
public PreferenceDialogFragmentCompat createInstance() {
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), BluetoothHandlerRunningSpeedAndCadence.RUNNING_SPEED_CADENCE);
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog.newInstance(getKey(), List.of(BluetoothHandlerRunningSpeedAndCadence.RUNNING_SPEED_CADENCE));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.dennisguse.opentracks.settings.bluetooth;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
Expand All @@ -21,7 +22,6 @@
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -35,14 +35,18 @@
* Preference to select a discoverable Bluetooth LE device.
* Based upon ListPreference.
*/
@SuppressLint("MissingPermission")
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);
}
Expand All @@ -62,11 +66,7 @@ public BluetoothLeSensorPreference(Context context) {
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;
Expand All @@ -85,11 +85,14 @@ protected void onSetInitialValue(Object defaultValue) {

@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);
}

return getValue();
return value;
}

public abstract PreferenceDialogFragmentCompat createInstance();
Expand Down Expand Up @@ -125,18 +128,20 @@ public void onScanFailed(int errorCode) {
}
};

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);
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;
}
Expand Down Expand Up @@ -166,6 +171,7 @@ public void onCreate(Bundle savedInstanceState) {

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()) {
Expand All @@ -188,14 +194,23 @@ private void startBluetoothScan() {
}

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;
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
<string name="settings_sensor_bluetooth_cycling_speed_key" translatable="false">bluetoothCyclingSpeedSensor</string>
<string name="settings_sensor_bluetooth_cycling_power_key" translatable="false">bluetoothCyclingPowerSensor</string>
<string name="settings_sensor_bluetooth_running_speed_and_cadence_key" translatable="false">bluetoothRunningSpeedAndCadenceSensor</string>
<string name="settings_sensor_bluetooth_pressure_key" translatable="false">bluetoothPressureSensor</string>
<string name="sensor_type_value_none" translatable="false">NONE</string>
<string name="sensor_type_value_internal" translatable="false">INTERNAL</string>

<string name="settings_sensor_bluetooth_cycling_speed_wheel_circumference_key" translatable="false">bluetoothCyclingSpeedWheelCircumference</string>
<string name="settings_sensor_bluetooth_cycling_speed_wheel_circumference_default" translatable="false">2135</string>
Expand Down
2 changes: 2 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ limitations under the License.
<string name="sensor_state_heart_rate_unit">bpm</string>
<string name="sensor_state_heart_rate_value">%1$d bpm</string>
<string name="sensor_state_power">Power</string>
<string name="sensor_state_pressure">Barometer</string>
<string name="sensor_state_power_avg">Avg Power</string>
<string name="sensor_state_power_max">Max Power</string>
<string name="sensor_state_power_unit">W</string>
Expand Down Expand Up @@ -558,6 +559,7 @@ limitations under the License.
<string name="value_integer_second">%1$d s</string>
<string name="value_none">None</string>
<string name="value_off">Off</string>
<string name="value_internal_sensor">Internal sensor</string>
<string name="value_smallest_recommended">Smallest (recommended)</string>
<string name="value_int_seconds">%1$d s (recommended)</string>
<string name="voiceIdle">Becoming idle.</string>
Expand Down
4 changes: 4 additions & 0 deletions src/main/res/xml/settings_sensors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
android:key="@string/settings_sensor_heart_rate_max_key"
android:title="@string/settings_sensor_heart_rate_max"
app:useSimpleSummaryProvider="true" />
<de.dennisguse.opentracks.settings.bluetooth.BluetoothLePressurePreference
android:defaultValue="@string/sensor_type_value_none"
android:key="@string/settings_sensor_bluetooth_pressure_key"
android:title="@string/sensor_state_pressure" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/settings_cycling_sensor">
Expand Down

0 comments on commit 877382f

Please sign in to comment.