Skip to content

Commit

Permalink
Implement gain/loss via Bluetooth LE Environmental Sensing Sensor.
Browse files Browse the repository at this point in the history
Fixes #1424.
  • Loading branch information
dennisguse committed Dec 30, 2023
1 parent a5a44c6 commit 10fe815
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/main/java/de/dennisguse/opentracks/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.util.Log;

import com.google.android.material.color.DynamicColors;
Expand Down Expand Up @@ -32,7 +33,7 @@ public void onCreate() {
// In debug builds: show thread and VM warnings.
if (BuildConfig.DEBUG) {
Log.d(TAG, "Enabling strict mode");
// StrictMode.enableDefaults();
StrictMode.enableDefaults();
}

PreferencesUtils.initPreferences(this, getResources());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,6 @@ private static String getBluetoothSensorAddressNone() {
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -52,59 +54,67 @@ public View getView(int position, View convertView, ViewGroup parent) {
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) {
return devices.get(index);
}

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

public String getAddress() {
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public CharSequence getSummary() {

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;
}
Expand All @@ -121,14 +121,12 @@ public static class BluetoothLeSensorPreferenceDialog extends PreferenceDialogFr
@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
Expand Down Expand Up @@ -205,19 +203,19 @@ private void startBluetoothScan() {
return;
}

listAdapter.add(getContext().getString(DEVICE_NONE_RESOURCEID), SensorType.NONE.getPreferenceValue());
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), SensorType.INTERNAL.getPreferenceValue());
listAdapter.add(SensorType.INTERNAL.getPreferenceValue(), getString(SENSOR_INTERNAL_RESOURCEID));
if (SensorType.INTERNAL.getPreferenceValue().equals(deviceSelected)) {
selectedEntryIndex = 1;
}
}

if (SensorType.REMOTE.equals(PreferencesUtils.getSensorType(deviceSelected))) {
if (deviceSelected != null && SensorType.REMOTE.equals(PreferencesUtils.getSensorType(deviceSelected))) {
listAdapter.add(preference.value, preference.value);
selectedEntryIndex = !includeInternalSensor ? 1 : 2;
}
Expand Down

0 comments on commit 10fe815

Please sign in to comment.