From 17141db8a6ce65b19386b7bfd92d0a371b51bd6d Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Sun, 4 Dec 2016 21:40:34 +0100 Subject: [PATCH 1/2] Turn auto enable sleep timer on/off immediately --- .../de/danoeh/antennapod/dialog/SleepTimerDialog.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java index 0ddee9f61..7d6a66a54 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/SleepTimerDialog.java @@ -1,6 +1,7 @@ package de.danoeh.antennapod.dialog; import android.content.Context; +import android.support.design.widget.Snackbar; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; @@ -8,6 +9,7 @@ import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.CheckBox; +import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; @@ -16,7 +18,9 @@ import com.afollestad.materialdialogs.DialogAction; import com.afollestad.materialdialogs.MaterialDialog; import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.event.MessageEvent; import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; +import de.greenrobot.event.EventBus; public abstract class SleepTimerDialog { @@ -32,7 +36,7 @@ public abstract class SleepTimerDialog { private CheckBox chAutoEnable; - public SleepTimerDialog(Context context) { + protected SleepTimerDialog(Context context) { this.context = context; } @@ -99,6 +103,11 @@ public abstract class SleepTimerDialog { cbVibrate.setChecked(SleepTimerPreferences.vibrate()); chAutoEnable.setChecked(SleepTimerPreferences.autoEnable()); + chAutoEnable.setOnCheckedChangeListener((compoundButton, isChecked) -> { + SleepTimerPreferences.setAutoEnable(isChecked); + int messageString = isChecked ? R.string.sleep_timer_enabled_label : R.string.sleep_timer_disabled_label; + EventBus.getDefault().post(new MessageEvent(context.getString(messageString))); + }); return dialog; } From dc65f13f442ae7feddd7b8c12c2e61aada29983c Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Sun, 4 Dec 2016 21:42:03 +0100 Subject: [PATCH 2/2] Do not auto-enable sleep timer after skipping --- .../service/playback/PlaybackService.java | 3 +- .../playback/PlaybackServiceMediaPlayer.java | 32 ++++++++++--------- .../playback/PlaybackServiceFlavorHelper.java | 5 +-- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index aca1d34e1..39d79e521 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -612,7 +612,8 @@ public class PlaybackService extends MediaBrowserServiceCompat { setupNotification(newInfo); started = true; // set sleep timer if auto-enabled - if(SleepTimerPreferences.autoEnable() && !sleepTimerActive()) { + if(newInfo.oldPlayerStatus != null && newInfo.oldPlayerStatus != PlayerStatus.SEEKING && + SleepTimerPreferences.autoEnable() && !sleepTimerActive()) { setSleepTimer(SleepTimerPreferences.timerMillis(), SleepTimerPreferences.shakeToReset(), SleepTimerPreferences.vibrate()); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java index 3b9a11610..393019fd1 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java @@ -29,9 +29,10 @@ public abstract class PlaybackServiceMediaPlayer { /** * Return value of some PSMP methods if the method call failed. */ - public static final int INVALID_TIME = -1; + static final int INVALID_TIME = -1; - protected volatile PlayerStatus playerStatus; + volatile PlayerStatus oldPlayerStatus; + volatile PlayerStatus playerStatus; /** * A wifi-lock that is acquired if the media file is being streamed. @@ -41,8 +42,8 @@ public abstract class PlaybackServiceMediaPlayer { protected final PSMPCallback callback; protected final Context context; - public PlaybackServiceMediaPlayer(@NonNull Context context, - @NonNull PSMPCallback callback){ + PlaybackServiceMediaPlayer(@NonNull Context context, + @NonNull PSMPCallback callback){ this.context = context; this.callback = callback; @@ -204,7 +205,7 @@ public abstract class PlaybackServiceMediaPlayer { * @return The PSMPInfo object. */ public final synchronized PSMPInfo getPSMPInfo() { - return new PSMPInfo(playerStatus, getPlayable()); + return new PSMPInfo(oldPlayerStatus, playerStatus, getPlayable()); } /** @@ -275,7 +276,7 @@ public abstract class PlaybackServiceMediaPlayer { */ protected abstract boolean shouldLockWifi(); - protected final synchronized void acquireWifiLockIfNecessary() { + final synchronized void acquireWifiLockIfNecessary() { if (shouldLockWifi()) { if (wifiLock == null) { wifiLock = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)) @@ -286,7 +287,7 @@ public abstract class PlaybackServiceMediaPlayer { } } - protected final synchronized void releaseWifiLockIfNecessary() { + final synchronized void releaseWifiLockIfNecessary() { if (wifiLock != null && wifiLock.isHeld()) { wifiLock.release(); } @@ -307,29 +308,28 @@ public abstract class PlaybackServiceMediaPlayer { * @param position The position to be set to the current Playable object in case playback started or paused. * Will be ignored if given the value of {@link #INVALID_TIME}. */ - protected final synchronized void setPlayerStatus(@NonNull PlayerStatus newStatus, Playable newMedia, int position) { + final synchronized void setPlayerStatus(@NonNull PlayerStatus newStatus, Playable newMedia, int position) { Log.d(TAG, this.getClass().getSimpleName() + ": Setting player status to " + newStatus); - PlayerStatus oldStatus = playerStatus; - + this.oldPlayerStatus = playerStatus; this.playerStatus = newStatus; setPlayable(newMedia); if (newMedia != null && newStatus != PlayerStatus.INDETERMINATE) { - if (oldStatus == PlayerStatus.PLAYING && newStatus != PlayerStatus.PLAYING) { + if (oldPlayerStatus == PlayerStatus.PLAYING && newStatus != PlayerStatus.PLAYING) { callback.onPlaybackPause(newMedia, position); - } else if (oldStatus != PlayerStatus.PLAYING && newStatus == PlayerStatus.PLAYING) { + } else if (oldPlayerStatus != PlayerStatus.PLAYING && newStatus == PlayerStatus.PLAYING) { callback.onPlaybackStart(newMedia, position); } } - callback.statusChanged(new PSMPInfo(playerStatus, getPlayable())); + callback.statusChanged(new PSMPInfo(oldPlayerStatus, playerStatus, getPlayable())); } /** * @see #setPlayerStatus(PlayerStatus, Playable, int) */ - protected final void setPlayerStatus(@NonNull PlayerStatus newStatus, Playable newMedia) { + final void setPlayerStatus(@NonNull PlayerStatus newStatus, Playable newMedia) { setPlayerStatus(newStatus, newMedia, INVALID_TIME); } @@ -365,10 +365,12 @@ public abstract class PlaybackServiceMediaPlayer { * Holds information about a PSMP object. */ public static class PSMPInfo { + public PlayerStatus oldPlayerStatus; public PlayerStatus playerStatus; public Playable playable; - public PSMPInfo(PlayerStatus playerStatus, Playable playable) { + PSMPInfo(PlayerStatus oldPlayerStatus, PlayerStatus playerStatus, Playable playable) { + this.oldPlayerStatus = oldPlayerStatus; this.playerStatus = playerStatus; this.playable = playable; } diff --git a/core/src/play/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceFlavorHelper.java b/core/src/play/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceFlavorHelper.java index c7428947b..0f493e63e 100644 --- a/core/src/play/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceFlavorHelper.java +++ b/core/src/play/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceFlavorHelper.java @@ -131,7 +131,8 @@ public class PlaybackServiceFlavorHelper { info = mediaPlayer.getPSMPInfo(); } if (info == null) { - info = new PlaybackServiceMediaPlayer.PSMPInfo(PlayerStatus.STOPPED, null); + info = new PlaybackServiceMediaPlayer.PSMPInfo(PlayerStatus.INDETERMINATE, + PlayerStatus.STOPPED, null); } switchMediaPlayer(new LocalPSMP(context, callback.getMediaPlayerCallback()), info, true); @@ -166,7 +167,7 @@ public class PlaybackServiceFlavorHelper { } } if (info == null) { - info = new PlaybackServiceMediaPlayer.PSMPInfo(PlayerStatus.STOPPED, null); + info = new PlaybackServiceMediaPlayer.PSMPInfo(PlayerStatus.INDETERMINATE, PlayerStatus.STOPPED, null); } callback.sendNotificationBroadcast(PlaybackService.NOTIFICATION_TYPE_RELOAD, PlaybackService.EXTRA_CODE_CAST);