Skip to content

Commit

Permalink
fix AntennaPod#4229 - configurable skip silence
Browse files Browse the repository at this point in the history
- followup feed-specific skip silence config (AntennaPod#6910)
- toggle buttons to configure
- medium 300ms gap
- aggressive 50ms gap
  • Loading branch information
MartinNowak committed May 17, 2024
1 parent 59c5042 commit 34670f6
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,25 @@ private void setupPlaybackSpeedPreference() {
viewBinding.seekBar.setAlpha(isChecked ? 0.4f : 1f);
viewBinding.currentSpeedLabel.setAlpha(isChecked ? 0.4f : 1f);

viewBinding.skipSilenceFeed.setEnabled(!isChecked);
viewBinding.skipSilenceFeed.setAlpha(isChecked ? 0.4f : 1f);
viewBinding.skipSilence.setEnabled(!isChecked);
viewBinding.skipSilence.setAlpha(isChecked ? 0.4f : 1f);
});
float speed = feedPreferences.getFeedPlaybackSpeed();
FeedPreferences.SkipSilence skipSilence = feedPreferences.getFeedSkipSilence();
boolean isGlobal = speed == FeedPreferences.SPEED_USE_GLOBAL;
viewBinding.useGlobalCheckbox.setChecked(isGlobal);
viewBinding.seekBar.updateSpeed(isGlobal ? 1 : speed);
viewBinding.skipSilenceFeed.setChecked(!isGlobal
&& skipSilence == FeedPreferences.SkipSilence.AGGRESSIVE);
if (isGlobal) {
viewBinding.skipSilence.clearChecked();
} else {
int id = View.NO_ID;
switch (skipSilence) {
case OFF: id = R.id.skipSilenceOff; break;
case MEDIUM: id = R.id.skipSilenceMedium; break;
case AGGRESSIVE: id = R.id.skipSilenceAggressive; break;
}
viewBinding.skipSilence.check(id);
}
new MaterialAlertDialogBuilder(getContext())
.setTitle(R.string.playback_speed)
.setView(viewBinding.getRoot())
Expand All @@ -263,10 +272,12 @@ private void setupPlaybackSpeedPreference() {
FeedPreferences.SkipSilence newSkipSilence;
if (viewBinding.useGlobalCheckbox.isChecked()) {
newSkipSilence = FeedPreferences.SkipSilence.GLOBAL;
} else if (viewBinding.skipSilenceFeed.isChecked()) {
newSkipSilence = FeedPreferences.SkipSilence.AGGRESSIVE;
} else {
newSkipSilence = FeedPreferences.SkipSilence.OFF;
final int id = viewBinding.skipSilence.getCheckedButtonId();
if (id == R.id.skipSilenceOff) newSkipSilence = FeedPreferences.SkipSilence.OFF;
else if (id == R.id.skipSilenceMedium) newSkipSilence = FeedPreferences.SkipSilence.MEDIUM;
else if (id == R.id.skipSilenceAggressive) newSkipSilence = FeedPreferences.SkipSilence.AGGRESSIVE;
else newSkipSilence = FeedPreferences.SkipSilence.GLOBAL;
}
feedPreferences.setFeedSkipSilence(newSkipSilence);
DBWriter.setFeedPreferences(feedPreferences);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.button.MaterialButtonToggleGroup;
import com.google.android.material.chip.Chip;
import com.google.android.material.snackbar.Snackbar;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.event.playback.SpeedChangedEvent;
import de.danoeh.antennapod.model.feed.FeedPreferences;
import de.danoeh.antennapod.playback.service.PlaybackController;
import de.danoeh.antennapod.storage.preferences.UserPreferences;
import de.danoeh.antennapod.ui.view.ItemOffsetDecoration;
Expand All @@ -36,7 +39,7 @@ public class VariableSpeedDialog extends BottomSheetDialogFragment {
private final List<Float> selectedSpeeds;
private PlaybackSpeedSeekBar speedSeekBar;
private Chip addCurrentSpeedChip;
private CheckBox skipSilenceCheckbox;
private MaterialButtonToggleGroup skipSilenceToggleGroup;

public VariableSpeedDialog() {
DecimalFormatSymbols format = new DecimalFormatSymbols(Locale.US);
Expand Down Expand Up @@ -74,8 +77,28 @@ public void updateSpeed(SpeedChangedEvent event) {
addCurrentSpeedChip.setText(String.format(Locale.getDefault(), "%1$.2f", event.getNewSpeed()));
}

public void updateSkipSilence(boolean skipSilence) {
skipSilenceCheckbox.setChecked(skipSilence);
public void updateSkipSilence(FeedPreferences.SkipSilence skipSilence) {
int id = View.NO_ID;
switch (skipSilence) {
case OFF: id = R.id.skipSilenceOff; break;
case MEDIUM: id = R.id.skipSilenceMedium; break;
case AGGRESSIVE: id = R.id.skipSilenceAggressive; break;
}
skipSilenceToggleGroup.check(id);
}

private void onSkipSilenceChanged(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
FeedPreferences.SkipSilence opt;
final int id = group.getCheckedButtonId();
if (id == R.id.skipSilenceOff) opt = FeedPreferences.SkipSilence.OFF;
else if (id == R.id.skipSilenceMedium) opt = FeedPreferences.SkipSilence.MEDIUM;
else if (id == R.id.skipSilenceAggressive) opt = FeedPreferences.SkipSilence.AGGRESSIVE;
else opt = FeedPreferences.SkipSilence.GLOBAL;
if (UserPreferences.getSkipSilence() != opt)
UserPreferences.setSkipSilence(opt);
if (controller != null) {
controller.setSkipSilence(opt);
}
}

@Nullable
Expand Down Expand Up @@ -104,12 +127,10 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
addCurrentSpeedChip.setCloseIconContentDescription(getString(R.string.add_preset));
addCurrentSpeedChip.setOnClickListener(v -> addCurrentSpeed());

skipSilenceCheckbox = root.findViewById(R.id.skipSilence);
skipSilenceCheckbox.setChecked(UserPreferences.isSkipSilence());
skipSilenceCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
UserPreferences.setSkipSilence(isChecked);
controller.setSkipSilence(isChecked);
});
skipSilenceToggleGroup = root.findViewById(R.id.skipSilence);
skipSilenceToggleGroup.addOnButtonCheckedListener(
(group, checkedId, isChecked) -> onSkipSilenceChanged(group, checkedId, isChecked));
updateSkipSilence(UserPreferences.getSkipSilence());
return root;
}

Expand Down
34 changes: 31 additions & 3 deletions app/src/main/res/layout/playback_speed_feed_setting_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,38 @@

</LinearLayout>

<CheckBox
android:id="@+id/skipSilenceFeed"
<com.google.android.material.button.MaterialButtonToggleGroup
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/skipSilence"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pref_skip_silence_title" />
android:weightSum="3"
app:singleSelection="true">

<Button
android:id="@+id/skipSilenceOff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Off"
style="@style/OutlinedButtonBetterContrast" />

<Button
android:id="@+id/skipSilenceMedium"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Medium"
style="@style/OutlinedButtonBetterContrast" />

<Button
android:id="@+id/skipSilenceAggressive"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Aggressive"
style="@style/OutlinedButtonBetterContrast" />

</com.google.android.material.button.MaterialButtonToggleGroup>

</LinearLayout>
39 changes: 37 additions & 2 deletions app/src/main/res/layout/speed_select_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,45 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<CheckBox
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="@string/pref_skip_silence_title"
style="@style/AntennaPod.TextView.ListItemPrimaryTitle" />

<com.google.android.material.button.MaterialButtonToggleGroup
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/skipSilence"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pref_skip_silence_title" />
android:weightSum="3"
app:singleSelection="true">

<Button
android:id="@+id/skipSilenceOff"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Off"
style="@style/OutlinedButtonBetterContrast" />

<Button
android:id="@+id/skipSilenceMedium"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Medium"
style="@style/OutlinedButtonBetterContrast" />

<Button
android:id="@+id/skipSilenceAggressive"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Aggressive"
style="@style/OutlinedButtonBetterContrast" />

</com.google.android.material.button.MaterialButtonToggleGroup>

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static NewEpisodesAction fromCode(int code) {
}

public enum SkipSilence {
OFF(0), GLOBAL(1), AGGRESSIVE(2);
GLOBAL(0), OFF(1), MEDIUM(2), AGGRESSIVE(3);

public final int code;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import androidx.annotation.Nullable;
import de.danoeh.antennapod.model.playback.MediaType;
import de.danoeh.antennapod.model.playback.Playable;
import de.danoeh.antennapod.model.feed.FeedPreferences;


/*
Expand Down Expand Up @@ -144,14 +145,14 @@ protected PlaybackServiceMediaPlayer(@NonNull Context context,
* - SkipSilence (ExoPlayer only)
* This method is executed on an internal executor service.
*/
public abstract void setPlaybackParams(final float speed, final boolean skipSilence);
public abstract void setPlaybackParams(final float speed, final FeedPreferences.SkipSilence skipSilence);

/**
* Returns the current playback speed. If the playback speed could not be retrieved, 1 is returned.
*/
public abstract float getPlaybackSpeed();

public abstract boolean getSkipSilence();
public abstract FeedPreferences.SkipSilence getSkipSilence();

/**
* Sets the playback volume.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import de.danoeh.antennapod.event.PlayerErrorEvent;
import de.danoeh.antennapod.event.playback.BufferUpdateEvent;
import de.danoeh.antennapod.model.feed.FeedMedia;
import de.danoeh.antennapod.model.feed.FeedPreferences;
import de.danoeh.antennapod.model.playback.MediaType;
import de.danoeh.antennapod.model.playback.Playable;
import de.danoeh.antennapod.model.playback.RemoteMedia;
Expand Down Expand Up @@ -415,7 +416,7 @@ public void setStartWhenPrepared(boolean startWhenPrepared) {
}

@Override
public void setPlaybackParams(float speed, boolean skipSilence) {
public void setPlaybackParams(float speed, FeedPreferences.SkipSilence skipSilenceDurationUs) {
double playbackRate = (float) Math.max(MediaLoadOptions.PLAYBACK_RATE_MIN,
Math.min(MediaLoadOptions.PLAYBACK_RATE_MAX, speed));
remoteMediaClient.setPlaybackRate(playbackRate);
Expand All @@ -428,9 +429,9 @@ public float getPlaybackSpeed() {
}

@Override
public boolean getSkipSilence() {
public FeedPreferences.SkipSilence getSkipSilence() {
// Don't think this is supported
return false;
return FeedPreferences.SkipSilence.OFF;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public void setPlaybackSpeed(float speed) {
}
}

public void setSkipSilence(boolean skipSilence) {
public void setSkipSilence(FeedPreferences.SkipSilence skipSilence) {
if (playbackService != null) {
playbackService.setSkipSilence(skipSilence);
}
Expand All @@ -421,12 +421,11 @@ public float getCurrentPlaybackSpeedMultiplier() {
}
}

public boolean getCurrentPlaybackSkipSilence() {
public FeedPreferences.SkipSilence getCurrentPlaybackSkipSilence() {
if (playbackService != null) {
return playbackService.getCurrentSkipSilence();
} else {
return PlaybackSpeedUtils.getCurrentSkipSilencePreference(getMedia())
== FeedPreferences.SkipSilence.AGGRESSIVE;
return PlaybackSpeedUtils.getCurrentSkipSilencePreference(getMedia());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1641,9 +1641,9 @@ public void speedPresetChanged(SpeedPresetChangedEvent event) {
setSpeed(event.getSpeed());
}
if (event.getSkipSilence() == FeedPreferences.SkipSilence.GLOBAL) {
setSkipSilence(UserPreferences.isSkipSilence());
setSkipSilence(UserPreferences.getSkipSilence());
} else {
setSkipSilence(event.getSkipSilence() == FeedPreferences.SkipSilence.AGGRESSIVE);
setSkipSilence(event.getSkipSilence());
}
}
}
Expand Down Expand Up @@ -1703,7 +1703,7 @@ public void setSpeed(float speed) {
mediaPlayer.setPlaybackParams(speed, getCurrentSkipSilence());
}

public void setSkipSilence(boolean skipSilence) {
public void setSkipSilence(FeedPreferences.SkipSilence skipSilence) {
PlaybackPreferences.setCurrentlyPlayingTemporarySkipSilence(skipSilence);
mediaPlayer.setPlaybackParams(getCurrentPlaybackSpeed(), skipSilence);
}
Expand All @@ -1715,9 +1715,9 @@ public float getCurrentPlaybackSpeed() {
return mediaPlayer.getPlaybackSpeed();
}

public boolean getCurrentSkipSilence() {
public FeedPreferences.SkipSilence getCurrentSkipSilence() {
if (mediaPlayer == null) {
return false;
return FeedPreferences.SkipSilence.OFF;
}
return mediaPlayer.getSkipSilence();
}
Expand Down
Loading

0 comments on commit 34670f6

Please sign in to comment.