From f476086114a56d214558f37b066849150a141390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Drobni=C4=8D?= Date: Fri, 29 Dec 2023 17:15:21 +0100 Subject: [PATCH] Check if volume boost effect is supported on the device (#6808) --- .../VolumeAdaptationPreference.java | 27 +++++++++++++++++ app/src/main/res/xml/feed_settings.xml | 2 +- .../service/playback/ExoPlayerWrapper.java | 19 +++++++++--- .../core/feed/VolumeAdaptionSettingTest.java | 13 ++++++++ .../model/feed/VolumeAdaptionSetting.java | 30 +++++++++++++++++++ 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/preferences/VolumeAdaptationPreference.java diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/VolumeAdaptationPreference.java b/app/src/main/java/de/danoeh/antennapod/preferences/VolumeAdaptationPreference.java new file mode 100644 index 000000000..584b7d90e --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/preferences/VolumeAdaptationPreference.java @@ -0,0 +1,27 @@ +package de.danoeh.antennapod.preferences; + +import android.content.Context; +import android.util.AttributeSet; + +import java.util.Arrays; + +import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting; + +public class VolumeAdaptationPreference extends MaterialListPreference { + public VolumeAdaptationPreference(Context context) { + super(context); + } + + public VolumeAdaptationPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + public CharSequence[] getEntries() { + if (VolumeAdaptionSetting.isBoostSupported()) { + return super.getEntries(); + } else { + return Arrays.copyOfRange(super.getEntries(), 0, 3); + } + } +} diff --git a/app/src/main/res/xml/feed_settings.xml b/app/src/main/res/xml/feed_settings.xml index fb9e2e425..b322217ad 100644 --- a/app/src/main/res/xml/feed_settings.xml +++ b/app/src/main/res/xml/feed_settings.xml @@ -48,7 +48,7 @@ android:summary="@string/global_default" android:title="@string/auto_delete_label" /> - 1) { exoPlayer.setVolume(1f); - loudnessEnhancer.setEnabled(true); - loudnessEnhancer.setTargetGain((int) (1000 * (v - 1))); + if (loudnessEnhancer != null) { + loudnessEnhancer.setEnabled(true); + loudnessEnhancer.setTargetGain((int) (1000 * (v - 1))); + } } else { exoPlayer.setVolume(v); - loudnessEnhancer.setEnabled(false); + if (loudnessEnhancer != null) { + loudnessEnhancer.setEnabled(false); + } } } @@ -354,6 +361,10 @@ public class ExoPlayerWrapper { } private void initLoudnessEnhancer(int audioStreamId) { + if (!VolumeAdaptionSetting.isBoostSupported()) { + return; + } + LoudnessEnhancer newEnhancer = new LoudnessEnhancer(audioStreamId); LoudnessEnhancer oldEnhancer = this.loudnessEnhancer; if (oldEnhancer != null) { diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java index 30767bdc8..966351a5e 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java @@ -1,6 +1,9 @@ package de.danoeh.antennapod.core.feed; import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting; + +import org.junit.After; +import org.junit.Before; import org.junit.Test; import static org.hamcrest.Matchers.equalTo; @@ -11,6 +14,16 @@ import static org.junit.Assert.assertTrue; public class VolumeAdaptionSettingTest { + @Before + public void setUp() throws Exception { + VolumeAdaptionSetting.setBoostSupported(false); + } + + @After + public void tearDown() throws Exception { + VolumeAdaptionSetting.setBoostSupported(null); + } + @Test public void mapOffToInteger() { VolumeAdaptionSetting setting = VolumeAdaptionSetting.OFF; diff --git a/model/src/main/java/de/danoeh/antennapod/model/feed/VolumeAdaptionSetting.java b/model/src/main/java/de/danoeh/antennapod/model/feed/VolumeAdaptionSetting.java index e71c5ad36..a9b1089f2 100644 --- a/model/src/main/java/de/danoeh/antennapod/model/feed/VolumeAdaptionSetting.java +++ b/model/src/main/java/de/danoeh/antennapod/model/feed/VolumeAdaptionSetting.java @@ -1,5 +1,10 @@ package de.danoeh.antennapod.model.feed; +import android.media.audiofx.AudioEffect; + +import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; + public enum VolumeAdaptionSetting { OFF(0, 1.0f), LIGHT_REDUCTION(1, 0.5f), @@ -32,4 +37,29 @@ public enum VolumeAdaptionSetting { public float getAdaptionFactor() { return adaptionFactor; } + + @Nullable + private static Boolean boostSupported = null; + + public static boolean isBoostSupported() { + if (boostSupported != null) { + return boostSupported; + } + final AudioEffect.Descriptor[] audioEffects = AudioEffect.queryEffects(); + if (audioEffects != null) { + for (AudioEffect.Descriptor effect : audioEffects) { + if (effect.type.equals(AudioEffect.EFFECT_TYPE_LOUDNESS_ENHANCER)) { + boostSupported = true; + return boostSupported; + } + } + } + boostSupported = false; + return boostSupported; + } + + @VisibleForTesting + public static void setBoostSupported(@Nullable Boolean boostSupported) { + VolumeAdaptionSetting.boostSupported = boostSupported; + } }