1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-08-24 16:21:16 +02:00

Sony Headphones: Allow choice of protocol version

This commit is contained in:
José Rebelo 2024-01-17 18:48:10 +00:00
parent c60ff90779
commit 4231e97cff
7 changed files with 108 additions and 24 deletions

View File

@ -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";

View File

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

View File

@ -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<Object> events = new ArrayList<>();
final SharedPreferences prefs = GBApplication.getDeviceSpecificSharedPrefs(getDevice().getAddress());
final List<GBDeviceEvent> 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

View File

@ -3019,6 +3019,20 @@
<item>ambient_sound</item>
</string-array>
<string-array name="sony_protocol_version_names">
<item>@string/automatic</item>
<item>@string/sony_protocol_v1</item>
<item>@string/sony_protocol_v2</item>
<item>@string/sony_protocol_v3</item>
</string-array>
<string-array name="sony_protocol_version_values">
<item>auto</item>
<item>v1</item>
<item>v2</item>
<item>v3</item>
</string-array>
<string-array name="sony_sound_position_names">
<item>@string/sony_sound_position_off</item>
<item>@string/sony_sound_position_front</item>

View File

@ -2181,6 +2181,10 @@
<string name="sony_ambient_sound_control_button_mode_as_off">Ambient Sound, Off</string>
<string name="sony_quick_access_double_tap">Quick Access (Double Tap)</string>
<string name="sony_quick_access_triple_tap">Quick Access (Triple Tap)</string>
<string name="sony_protocol_v1">Version 1</string>
<string name="sony_protocol_v2">Version 2</string>
<string name="sony_protocol_v3">Version 3</string>
<string name="protocol_version">Protocol Version</string>
<string name="pref_screen_auto_brightness_title">Auto Brightness</string>
<string name="pref_screen_auto_brightness_summary">Adjust screen brightness according to ambient light</string>
<string name="pref_screen_brightness">Screen Brightness</string>

View File

@ -14,5 +14,14 @@
android:title="@string/audio_codec"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
android:defaultValue="\?"
android:enabled="false"
android:icon="@drawable/ic_developer_mode"
android:key="pref_actual_protocol_version"
android:shouldDisableView="false"
android:title="@string/protocol_version"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
</androidx.preference.PreferenceScreen>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ListPreference
android:defaultValue="auto"
android:entries="@array/sony_protocol_version_names"
android:entryValues="@array/sony_protocol_version_values"
android:icon="@drawable/ic_developer_mode"
android:key="pref_protocol_version"
android:summary="%s"
android:title="@string/protocol_version"
app:useSimpleSummaryProvider="true" />
</androidx.preference.PreferenceScreen>