Sony Headphones: Do not crash on invalid battery config

These can now be selected from preferences, which will make Gb crash on
startup if users make a mistake during configuration, so avoid crashing.
This commit is contained in:
José Rebelo
2025-04-14 20:09:52 +01:00
parent 9b873ada7e
commit 33aaec7300
@@ -19,6 +19,9 @@ package nodomain.freeyourgadget.gadgetbridge.devices.sony.headphones;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
@@ -45,6 +48,8 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.sony.headphones.Sony
import nodomain.freeyourgadget.gadgetbridge.util.preferences.DevicePrefs; import nodomain.freeyourgadget.gadgetbridge.util.preferences.DevicePrefs;
public abstract class SonyHeadphonesCoordinator extends AbstractBLClassicDeviceCoordinator { public abstract class SonyHeadphonesCoordinator extends AbstractBLClassicDeviceCoordinator {
private static final Logger LOG = LoggerFactory.getLogger(SonyHeadphonesCoordinator.class);
@Override @Override
public String getManufacturer() { public String getManufacturer() {
return "Sony"; return "Sony";
@@ -73,9 +78,11 @@ public abstract class SonyHeadphonesCoordinator extends AbstractBLClassicDeviceC
public int getBatteryCount(final GBDevice device) { public int getBatteryCount(final GBDevice device) {
if (supports(device, SonyHeadphonesCapabilities.BatterySingle)) { if (supports(device, SonyHeadphonesCapabilities.BatterySingle)) {
if (supports(device, SonyHeadphonesCapabilities.BatteryDual) || supports(device, SonyHeadphonesCapabilities.BatteryDual2)) { if (supports(device, SonyHeadphonesCapabilities.BatteryDual) || supports(device, SonyHeadphonesCapabilities.BatteryDual2)) {
throw new IllegalStateException("A device can't have both single and dual battery"); LOG.error("A device can't have both single and dual battery");
return 0;
} else if (supports(device, SonyHeadphonesCapabilities.BatteryCase)) { } else if (supports(device, SonyHeadphonesCapabilities.BatteryCase)) {
throw new IllegalStateException("Devices with single battery + case are not supported by the protocol"); LOG.error("Devices with single battery + case are not supported by the protocol");
return 0;
} }
} }