diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java index d4d28ff59..d61fa848d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSettingsPreferenceConst.java @@ -335,6 +335,8 @@ public class DeviceSettingsPreferenceConst { public static final String PREFS_GALAXY_BUDS_SEAMLESS_CONNECTION="prefs_galaxy_buds_seamless_connection"; public static final String PREF_SONY_AUDIO_CODEC = "pref_sony_audio_codec"; + public static final String PREF_SONY_PROTOCOL_VERSION = "pref_protocol_version"; + public static final String PREF_SONY_ACTUAL_PROTOCOL_VERSION = "pref_actual_protocol_version"; public static final String PREF_SONY_AMBIENT_SOUND_CONTROL = "pref_sony_ambient_sound_control"; public static final String PREF_SONY_AMBIENT_SOUND_CONTROL_BUTTON_MODE = "pref_sony_ambient_sound_control_button_mode"; public static final String PREF_SONY_FOCUS_VOICE = "pref_sony_focus_voice"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/SonyHeadphonesCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/SonyHeadphonesCoordinator.java index e7b93cab5..dd24a56db 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/SonyHeadphonesCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/SonyHeadphonesCoordinator.java @@ -213,6 +213,9 @@ public abstract class SonyHeadphonesCoordinator extends AbstractBLClassicDeviceC put(SonyHeadphonesCapabilities.VoiceNotifications, R.xml.devicesettings_sony_headphones_notifications_voice_guide); }}); + settings.add(R.xml.devicesettings_header_developer); + settings.add(R.xml.devicesettings_sony_headphones_protocol_version); + settings.add(R.xml.devicesettings_sony_headphones_device_info); return ArrayUtils.toPrimitive(settings.toArray(new Integer[0])); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/sony/headphones/SonyHeadphonesProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/sony/headphones/SonyHeadphonesProtocol.java index 35d4e05d7..de70a2e21 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/sony/headphones/SonyHeadphonesProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/sony/headphones/SonyHeadphonesProtocol.java @@ -31,6 +31,7 @@ import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSett import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdateDeviceState; +import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdatePreferences; import nodomain.freeyourgadget.gadgetbridge.devices.sony.headphones.prefs.AmbientSoundControl; import nodomain.freeyourgadget.gadgetbridge.devices.sony.headphones.prefs.AmbientSoundControlButtonMode; import nodomain.freeyourgadget.gadgetbridge.devices.sony.headphones.prefs.AudioUpsampling; @@ -99,42 +100,78 @@ public class SonyHeadphonesProtocol extends GBDeviceProtocol { return null; } - final List events = new ArrayList<>(); + final SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress()); + + final List events = new ArrayList<>(); if (protocolImpl == null) { // Check if we got an init response, which should indicate the protocol version if (MessageType.COMMAND_1.equals(messageType) && message.getPayload()[0] == 0x01) { // Init reply, set the protocol version - if (message.getPayload().length == 4) { - // WH-1000XM3: 01:00:40:10 - // WF-SP800N 1.0.1: 01:00:70:00 - protocolImpl = new SonyProtocolImplV1(getDevice()); - } else if (message.getPayload().length == 8) { - switch (message.getPayload()[2]) { - case 0x01: - // WF-1000XM4 1.1.5: 01:00:01:00:00:00:00:00 - protocolImpl = new SonyProtocolImplV2(getDevice()); - break; - case 0x03: - // LinkBuds S 2.0.2: 01:00:03:00:00:07:00:00 - // WH-1000XM5 1.1.3: 01:00:03:00:00:00:00:00 - // WF-1000XM5 2.0.1: 01:00:03:00:10:04:00:00 - protocolImpl = new SonyProtocolImplV3(getDevice()); - break; - default: - LOG.error("Unexpected version for payload of length 8: {}", message.getPayload()[2]); - return null; + + final GBDeviceEventUpdatePreferences eventUpdatePreferences = new GBDeviceEventUpdatePreferences( + DeviceSettingsPreferenceConst.PREF_SONY_ACTUAL_PROTOCOL_VERSION, null + ); + events.add(eventUpdatePreferences); + + final String protocolVersionPref = prefs.getString("pref_protocol_version", "auto"); + final String protocolVersion; + if (protocolVersionPref.equals("auto")) { + if (message.getPayload().length == 4) { + // WH-1000XM3: 01:00:40:10 + // WF-SP800N 1.0.1: 01:00:70:00 + protocolVersion = "v1"; + } else if (message.getPayload().length == 8) { + switch (message.getPayload()[2]) { + case 0x01: + // WF-1000XM4 1.1.5: 01:00:01:00:00:00:00:00 + protocolVersion = "v2"; + break; + case 0x03: + // LinkBuds S 2.0.2: 01:00:03:00:00:07:00:00 + // WH-1000XM5 1.1.3: 01:00:03:00:00:00:00:00 + // WF-1000XM5 2.0.1: 01:00:03:00:10:04:00:00 + protocolVersion = "v3"; + break; + default: + LOG.error("Unexpected version for payload of length 8: {}", message.getPayload()[2]); + return events.toArray(new GBDeviceEvent[0]); + } + } else { + LOG.error("Unexpected init response payload length: {}", message.getPayload().length); + return events.toArray(new GBDeviceEvent[0]); } } else { - LOG.error("Unexpected init response payload length: {}", message.getPayload().length); - return null; + protocolVersion = protocolVersionPref; + } + + LOG.info("Sony protocol version: {}/{}", protocolVersionPref, protocolVersion); + + eventUpdatePreferences.withPreference( + DeviceSettingsPreferenceConst.PREF_SONY_ACTUAL_PROTOCOL_VERSION, + protocolVersion + ); + + switch (protocolVersion) { + case "v1": + protocolImpl = new SonyProtocolImplV1(getDevice()); + break; + case "v2": + protocolImpl = new SonyProtocolImplV2(getDevice()); + break; + case "v3": + protocolImpl = new SonyProtocolImplV3(getDevice()); + break; + default: + LOG.warn("Unknown protocol version {}", protocolVersion); + return events.toArray(new GBDeviceEvent[0]); } } } if (protocolImpl == null) { LOG.error("No protocol implementation, ignoring message"); - return null; + return events.toArray(new GBDeviceEvent[0]); } try { @@ -146,7 +183,7 @@ public class SonyHeadphonesProtocol extends GBDeviceProtocol { break; default: LOG.warn("Unknown message type for {}", message); - return null; + return events.toArray(new GBDeviceEvent[0]); } } catch (final Exception e) { // Don't crash the app if we somehow fail to handle the payload diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index d405422f9..d92181dde 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -3019,6 +3019,20 @@ ambient_sound + + @string/automatic + @string/sony_protocol_v1 + @string/sony_protocol_v2 + @string/sony_protocol_v3 + + + + auto + v1 + v2 + v3 + + @string/sony_sound_position_off @string/sony_sound_position_front diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 33637e8b2..14c6722d4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2181,6 +2181,10 @@ Ambient Sound, Off Quick Access (Double Tap) Quick Access (Triple Tap) + Version 1 + Version 2 + Version 3 + Protocol Version Auto Brightness Adjust screen brightness according to ambient light Screen Brightness diff --git a/app/src/main/res/xml/devicesettings_sony_headphones_device_info.xml b/app/src/main/res/xml/devicesettings_sony_headphones_device_info.xml index 0997efcfb..6bfc4c275 100644 --- a/app/src/main/res/xml/devicesettings_sony_headphones_device_info.xml +++ b/app/src/main/res/xml/devicesettings_sony_headphones_device_info.xml @@ -14,5 +14,14 @@ android:title="@string/audio_codec" app:useSimpleSummaryProvider="true" /> + + diff --git a/app/src/main/res/xml/devicesettings_sony_headphones_protocol_version.xml b/app/src/main/res/xml/devicesettings_sony_headphones_protocol_version.xml new file mode 100644 index 000000000..c9e1d7337 --- /dev/null +++ b/app/src/main/res/xml/devicesettings_sony_headphones_protocol_version.xml @@ -0,0 +1,15 @@ + + + + + +