Implement gain/loss via Bluetooth LE Environmental Sensing Sensor.

Fixes #1424.
This commit is contained in:
Dennis Guse
2023-12-28 15:10:44 +01:00
parent 8eed8e9d8d
commit 03dd0c2308
11 changed files with 170 additions and 134 deletions
@@ -253,10 +253,6 @@ public class PreferencesUtils {
return SensorType.NONE.getPreferenceValue();
}
public static boolean isBluetoothSensorAddressNone(String currentValue) {
return getBluetoothSensorAddressNone().equals(currentValue);
}
public static SensorType getSensorType(String address) {
return Arrays.stream(SensorType.values())
.filter(it -> it.getPreferenceValue().equals(address))
@@ -1,5 +1,6 @@
package de.dennisguse.opentracks.settings.bluetooth;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
@@ -7,10 +8,13 @@ import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@SuppressLint("MissingPermission")
public class BluetoothLeAdapter extends BaseAdapter {
/**
@@ -50,22 +54,30 @@ public class BluetoothLeAdapter extends BaseAdapter {
return currentView;
}
public void add(String name, String address) {
Device device = new Device(name, address);
/**
* @return Data changed?
*/
public boolean add(String address, String name) {
Device device = new Device(address, name);
if (!devices.contains(device)) {
devices.add(new Device(name, address));
devices.add(device);
return true;
} else {
for (Device currentDevice : devices) {
if (currentDevice.getAddress().equals(address)) {
currentDevice.setName(name);
if (currentDevice.address.equals(address)) {
currentDevice.name = name;
return true;
}
}
}
notifyDataSetChanged();
return false;
}
public void add(BluetoothDevice bluetoothDevice) {
add(bluetoothDevice.getName(), bluetoothDevice.getAddress());
public void addAll(List<BluetoothDevice> bluetoothDevices) {
boolean dataSetChanged = bluetoothDevices.stream()
.anyMatch(bluetoothDevice -> add(bluetoothDevice.getAddress(), bluetoothDevice.getName()));
if (dataSetChanged) notifyDataSetChanged();
}
public Device get(int index) {
@@ -73,19 +85,14 @@ public class BluetoothLeAdapter extends BaseAdapter {
}
public static class Device {
private String name;
@NonNull
private final String address;
private String name;
public Device(String name, String address) {
this.name = name;
Device(@NonNull String address, String name) {
Objects.requireNonNull(address);
this.address = address;
}
public String getNameOrAddress() {
return name != null ? name : getAddress();
}
public void setName(String name) {
this.name = name;
}
@@ -93,16 +100,21 @@ public class BluetoothLeAdapter extends BaseAdapter {
return address;
}
public String getNameOrAddress() {
return name != null ? name : address;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Device device)) return false;
return address.equals(device.address);
}
@Override
public int hashCode() {
return Objects.hash(name, address);
return Objects.hash(address);
}
}
}
@@ -7,7 +7,9 @@ import android.util.AttributeSet;
import androidx.preference.PreferenceDialogFragmentCompat;
import java.util.Collections;
import java.util.List;
import de.dennisguse.opentracks.sensors.BluetoothHandlerBarometricPressure;
public class BluetoothLePressurePreference extends BluetoothLeSensorPreference {
@@ -34,7 +36,6 @@ public class BluetoothLePressurePreference extends BluetoothLeSensorPreference {
boolean hasInternalSensor = pressureSensor != null;
return BluetoothLeSensorPreference.BluetoothLeSensorPreferenceDialog
// .newInstance(getKey(), List.of(BluetoothHandlerBarometricPressure.BAROMETRIC_PRESSURE), hasInternalSensor); //TODO #1424
.newInstance(getKey(), Collections.emptyList(), hasInternalSensor);
.newInstance(getKey(), List.of(BluetoothHandlerBarometricPressure.BAROMETRIC_PRESSURE), hasInternalSensor);
}
}
@@ -102,7 +102,7 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(value);
if (device != null && device.getName() != null) {
return getContext().getString(R.string.bluetooth_sensor_summary, device.getAddress(), device.getName());
return getContext().getString(R.string.bluetooth_sensor_summary, device.getAddress(), device.getName());
}
return value;
}
@@ -121,14 +121,12 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.d(TAG, "Found device " + result.getDevice().getName() + " " + result);
listAdapter.add(result.getDevice());
onBatchScanResults(List.of(result));
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult result : results) {
onScanResult(-1, result);
}
listAdapter.addAll(results.stream().map(ScanResult::getDevice).collect(Collectors.toList()));
}
@Override
@@ -205,24 +203,21 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
return;
}
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);
listAdapter.add(SensorType.NONE.getPreferenceValue(), getContext().getString(DEVICE_NONE_RESOURCEID));
selectedEntryIndex = 0;
BluetoothLeSensorPreference preference = (BluetoothLeSensorPreference) getPreference();
String deviceSelected = preference.value;
if (includeInternalSensor) {
listAdapter.add(getString(SENSOR_INTERNAL_RESOURCEID), sensorInternal);
if (sensorInternal.equals(deviceSelected)) {
listAdapter.add(SensorType.INTERNAL.getPreferenceValue(), getString(SENSOR_INTERNAL_RESOURCEID));
if (SensorType.INTERNAL.getPreferenceValue().equals(deviceSelected)) {
selectedEntryIndex = 1;
}
}
if (deviceSelected != null && !deviceNone.equals(deviceSelected) && !sensorInternal.equals(deviceSelected)) {
if (deviceSelected != null && SensorType.REMOTE.equals(PreferencesUtils.getSensorType(deviceSelected))) {
listAdapter.add(preference.value, preference.value);
selectedEntryIndex++;
selectedEntryIndex = !includeInternalSensor ? 1 : 2;
}
List<ScanFilter> scanFilter = null;