From 83c42ebef8f96b221fbb755ff47fcde17d515223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Fri, 14 Jan 2022 00:31:58 +0000 Subject: [PATCH] Sony WH-1000XM3: Disable equalizer, surround and sound position while in SBC codec --- .../DeviceSettingsPreferenceConst.java | 1 + .../DeviceSpecificSettingsCustomizer.java | 10 ++++ .../DeviceSpecificSettingsFragment.java | 9 ++- .../headphones/SonyHeadphonesCoordinator.java | 2 +- .../SonyHeadphonesSettingsCustomizer.java | 55 ++++++++++++++++++- .../coordinators/SonyWFSP800NCoordinator.java | 3 +- .../SonyWH1000XM3Coordinator.java | 5 +- .../SonyWH1000XM4Coordinator.java | 3 +- app/src/main/res/values/strings.xml | 2 + ...cesettings_sony_headphones_device_info.xml | 18 ++++++ ...vicesettings_sony_headphones_equalizer.xml | 2 +- 11 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 app/src/main/res/xml/devicesettings_sony_headphones_device_info.xml 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 b3acd54d6..f313dca26 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 @@ -123,6 +123,7 @@ public class DeviceSettingsPreferenceConst { public static final String PREF_SONY_AMBIENT_SOUND_LEVEL = "pref_sony_ambient_sound_level"; public static final String PREF_SONY_SOUND_POSITION = "pref_sony_sound_position"; public static final String PREF_SONY_SURROUND_MODE = "pref_sony_surround_mode"; + public static final String PREF_SONY_EQUALIZER = "pref_sony_equalizer"; public static final String PREF_SONY_EQUALIZER_MODE = "pref_sony_equalizer_mode"; public static final String PREF_SONY_AUDIO_UPSAMPLING = "pref_sony_audio_upsampling"; public static final String PREF_SONY_EQUALIZER_BAND_400 = "pref_sony_equalizer_band_400"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsCustomizer.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsCustomizer.java index 6e0195f5d..3a8ac1323 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsCustomizer.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsCustomizer.java @@ -18,11 +18,21 @@ package nodomain.freeyourgadget.gadgetbridge.activities.devicesettings; import android.os.Parcelable; +import androidx.preference.Preference; + /** * A device-specific preference handler, that allows for concrete implementations to customize the preferences in * the {@link DeviceSpecificSettingsFragment}. */ public interface DeviceSpecificSettingsCustomizer extends Parcelable { + /** + * Called when a {@link Preference} changes, not caused by user input (so the preference change listener is not called). + * + * @param preference the {@link Preference} preference that changed + * @param handler the {@link DeviceSpecificSettingsHandler} + */ + void onPreferenceChange(final Preference preference, final DeviceSpecificSettingsHandler handler); + /** * Customize the settings on the {@link DeviceSpecificSettingsFragment}. * diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java index 382d1dda7..e53d66586 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/devicesettings/DeviceSpecificSettingsFragment.java @@ -869,7 +869,7 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat imp final Preference preference = findPreference(key); if (preference == null) { - LOG.warn("Preference {} not found, ignoring", key); + LOG.warn("Preference {} not found", key); return; } @@ -883,11 +883,18 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat imp } else if (preference instanceof ListPreference) { final ListPreference listPreference = (ListPreference) preference; listPreference.setValue(prefs.getString(key, listPreference.getValue())); + } else if (preference instanceof EditTextPreference) { + final EditTextPreference editTextPreference = (EditTextPreference) preference; + editTextPreference.setText(prefs.getString(key, editTextPreference.getText())); } else if (preference instanceof PreferenceScreen) { // Ignoring } else { LOG.warn("Unknown preference class {}, ignoring", preference.getClass()); } + + if (deviceSpecificSettingsCustomizer != null) { + deviceSpecificSettingsCustomizer.onPreferenceChange(preference, DeviceSpecificSettingsFragment.this); + } } } } 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 1834e462e..10e6a2ea1 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 @@ -41,7 +41,7 @@ public abstract class SonyHeadphonesCoordinator extends AbstractDeviceCoordinato @Override public DeviceSpecificSettingsCustomizer getDeviceSpecificSettingsCustomizer(final GBDevice device) { - return new SonyHeadphonesSettingsCustomizer(); + return new SonyHeadphonesSettingsCustomizer(device); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/SonyHeadphonesSettingsCustomizer.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/SonyHeadphonesSettingsCustomizer.java index 7b45a5871..8ff6e9731 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/SonyHeadphonesSettingsCustomizer.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/SonyHeadphonesSettingsCustomizer.java @@ -18,20 +18,70 @@ package nodomain.freeyourgadget.gadgetbridge.devices.sony.headphones; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_AMBIENT_SOUND_CONTROL; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_AMBIENT_SOUND_LEVEL; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_AUDIO_CODEC; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_EQUALIZER; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_EQUALIZER_BAND_1000; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_EQUALIZER_BAND_16000; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_EQUALIZER_BAND_2500; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_EQUALIZER_BAND_400; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_EQUALIZER_BAND_6300; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_EQUALIZER_BASS; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_EQUALIZER_MODE; import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_FOCUS_VOICE; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_SOUND_POSITION; +import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SONY_SURROUND_MODE; import android.os.Parcel; +import androidx.preference.EditTextPreference; import androidx.preference.ListPreference; import androidx.preference.Preference; +import java.util.Arrays; +import java.util.List; import java.util.Locale; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsCustomizer; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsHandler; import nodomain.freeyourgadget.gadgetbridge.devices.sony.headphones.prefs.AmbientSoundControl; +import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; public class SonyHeadphonesSettingsCustomizer implements DeviceSpecificSettingsCustomizer { + final GBDevice device; + + public SonyHeadphonesSettingsCustomizer(final GBDevice device) { + this.device = device; + } + + @Override + public void onPreferenceChange(final Preference preference, final DeviceSpecificSettingsHandler handler) { + // Disable equalizer, sound position and surround mode if not in SBC codec, for WH-1000XM3 + // TODO: Should the coordinator be responsible for this compatibility check? + if (preference.getKey().equals(PREF_SONY_AUDIO_CODEC) && device.getType().equals(DeviceType.SONY_WH_1000XM3)) { + final boolean isSbcCodec = ((EditTextPreference) preference).getText().equalsIgnoreCase("sbc"); + + final List prefsToDisable = Arrays.asList( + handler.findPreference(PREF_SONY_EQUALIZER), + handler.findPreference(PREF_SONY_EQUALIZER_MODE), + handler.findPreference(PREF_SONY_EQUALIZER_BAND_400), + handler.findPreference(PREF_SONY_EQUALIZER_BAND_1000), + handler.findPreference(PREF_SONY_EQUALIZER_BAND_2500), + handler.findPreference(PREF_SONY_EQUALIZER_BAND_6300), + handler.findPreference(PREF_SONY_EQUALIZER_BAND_16000), + handler.findPreference(PREF_SONY_EQUALIZER_BASS), + handler.findPreference(PREF_SONY_SOUND_POSITION), + handler.findPreference(PREF_SONY_SURROUND_MODE) + ); + + for (Preference pref : prefsToDisable) { + if (pref != null) { + pref.setEnabled(isSbcCodec); + } + } + } + } + @Override public void customizeSettings(final DeviceSpecificSettingsHandler handler) { // Only enable the focus on voice check and voice level slider if the ambient sound control mode is ambient sound @@ -59,7 +109,8 @@ public class SonyHeadphonesSettingsCustomizer implements DeviceSpecificSettingsC public static final Creator CREATOR = new Creator() { @Override public SonyHeadphonesSettingsCustomizer createFromParcel(final Parcel in) { - return new SonyHeadphonesSettingsCustomizer(); + final GBDevice device = in.readParcelable(SonyHeadphonesSettingsCustomizer.class.getClassLoader()); + return new SonyHeadphonesSettingsCustomizer(device); } @Override @@ -75,6 +126,6 @@ public class SonyHeadphonesSettingsCustomizer implements DeviceSpecificSettingsC @Override public void writeToParcel(final Parcel dest, final int flags) { - // Nothing to do + dest.writeParcelable(device, 0); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWFSP800NCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWFSP800NCoordinator.java index 2f8efc16a..806f39e31 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWFSP800NCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWFSP800NCoordinator.java @@ -70,7 +70,8 @@ public class SonyWFSP800NCoordinator extends SonyHeadphonesCoordinator { R.xml.devicesettings_sony_headphones_button_modes_left_right, R.xml.devicesettings_sony_headphones_pause_when_taken_off, R.xml.devicesettings_automatic_power_off_when_taken_off, - R.xml.devicesettings_sony_headphones_notifications_voice_guide + R.xml.devicesettings_sony_headphones_notifications_voice_guide, + R.xml.devicesettings_sony_headphones_device_info }; } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWH1000XM3Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWH1000XM3Coordinator.java index ad97e5e1b..f61ebf2d8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWH1000XM3Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWH1000XM3Coordinator.java @@ -43,9 +43,9 @@ public class SonyWH1000XM3Coordinator extends SonyHeadphonesCoordinator { @Override public int[] getSupportedDeviceSpecificSettings(final GBDevice device) { return new int[]{ - R.xml.devicesettings_sony_warning_wh1000xm3, R.xml.devicesettings_sony_headphones_ambient_sound_control_wind_noise_reduction, R.xml.devicesettings_header_other, + R.xml.devicesettings_sony_warning_wh1000xm3, R.xml.devicesettings_sony_headphones_equalizer, R.xml.devicesettings_sony_headphones_sound_position, R.xml.devicesettings_sony_headphones_surround_mode, @@ -53,7 +53,8 @@ public class SonyWH1000XM3Coordinator extends SonyHeadphonesCoordinator { R.xml.devicesettings_header_system, R.xml.devicesettings_sony_headphones_touch_sensor_single, R.xml.devicesettings_automatic_power_off_by_time, - R.xml.devicesettings_sony_headphones_notifications_voice_guide + R.xml.devicesettings_sony_headphones_notifications_voice_guide, + R.xml.devicesettings_sony_headphones_device_info }; } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWH1000XM4Coordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWH1000XM4Coordinator.java index 713f4ebfb..d6a6a613d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWH1000XM4Coordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/sony/headphones/coordinators/SonyWH1000XM4Coordinator.java @@ -54,7 +54,8 @@ public class SonyWH1000XM4Coordinator extends SonyHeadphonesCoordinator { R.xml.devicesettings_sony_headphones_touch_sensor_single, R.xml.devicesettings_sony_headphones_pause_when_taken_off, R.xml.devicesettings_automatic_power_off_when_taken_off, - R.xml.devicesettings_sony_headphones_notifications_voice_guide + R.xml.devicesettings_sony_headphones_notifications_voice_guide, + R.xml.devicesettings_sony_headphones_device_info }; } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d45916241..8511f6fac 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1383,7 +1383,9 @@ Battery case Left earbud Right earbud + Audio Codec Ambient Sound Control + Device Information Mode Off Noise Cancelling 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 new file mode 100644 index 000000000..0997efcfb --- /dev/null +++ b/app/src/main/res/xml/devicesettings_sony_headphones_device_info.xml @@ -0,0 +1,18 @@ + + + + + + + + diff --git a/app/src/main/res/xml/devicesettings_sony_headphones_equalizer.xml b/app/src/main/res/xml/devicesettings_sony_headphones_equalizer.xml index abc973b2f..f223aeb96 100644 --- a/app/src/main/res/xml/devicesettings_sony_headphones_equalizer.xml +++ b/app/src/main/res/xml/devicesettings_sony_headphones_equalizer.xml @@ -3,7 +3,7 @@