Merge pull request #2206 from mfietz/issue/2193-sleep-timer-skipping

Do not auto-enable sleep timer after skipping
This commit is contained in:
Martin Fietz 2016-12-04 21:51:29 +01:00 committed by GitHub
commit 58c3e8514d
4 changed files with 32 additions and 19 deletions

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.dialog; package de.danoeh.antennapod.dialog;
import android.content.Context; import android.content.Context;
import android.support.design.widget.Snackbar;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
@ -8,6 +9,7 @@ import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.Toast; import android.widget.Toast;
@ -16,7 +18,9 @@ import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog; import com.afollestad.materialdialogs.MaterialDialog;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
import de.greenrobot.event.EventBus;
public abstract class SleepTimerDialog { public abstract class SleepTimerDialog {
@ -32,7 +36,7 @@ public abstract class SleepTimerDialog {
private CheckBox chAutoEnable; private CheckBox chAutoEnable;
public SleepTimerDialog(Context context) { protected SleepTimerDialog(Context context) {
this.context = context; this.context = context;
} }
@ -99,6 +103,11 @@ public abstract class SleepTimerDialog {
cbVibrate.setChecked(SleepTimerPreferences.vibrate()); cbVibrate.setChecked(SleepTimerPreferences.vibrate());
chAutoEnable.setChecked(SleepTimerPreferences.autoEnable()); 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; return dialog;
} }

View File

@ -612,7 +612,8 @@ public class PlaybackService extends MediaBrowserServiceCompat {
setupNotification(newInfo); setupNotification(newInfo);
started = true; started = true;
// set sleep timer if auto-enabled // 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(), setSleepTimer(SleepTimerPreferences.timerMillis(), SleepTimerPreferences.shakeToReset(),
SleepTimerPreferences.vibrate()); SleepTimerPreferences.vibrate());
} }

View File

@ -29,9 +29,10 @@ public abstract class PlaybackServiceMediaPlayer {
/** /**
* Return value of some PSMP methods if the method call failed. * 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. * 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 PSMPCallback callback;
protected final Context context; protected final Context context;
public PlaybackServiceMediaPlayer(@NonNull Context context, PlaybackServiceMediaPlayer(@NonNull Context context,
@NonNull PSMPCallback callback){ @NonNull PSMPCallback callback){
this.context = context; this.context = context;
this.callback = callback; this.callback = callback;
@ -204,7 +205,7 @@ public abstract class PlaybackServiceMediaPlayer {
* @return The PSMPInfo object. * @return The PSMPInfo object.
*/ */
public final synchronized PSMPInfo getPSMPInfo() { 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 abstract boolean shouldLockWifi();
protected final synchronized void acquireWifiLockIfNecessary() { final synchronized void acquireWifiLockIfNecessary() {
if (shouldLockWifi()) { if (shouldLockWifi()) {
if (wifiLock == null) { if (wifiLock == null) {
wifiLock = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)) 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()) { if (wifiLock != null && wifiLock.isHeld()) {
wifiLock.release(); 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. * @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}. * 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); Log.d(TAG, this.getClass().getSimpleName() + ": Setting player status to " + newStatus);
PlayerStatus oldStatus = playerStatus; this.oldPlayerStatus = playerStatus;
this.playerStatus = newStatus; this.playerStatus = newStatus;
setPlayable(newMedia); setPlayable(newMedia);
if (newMedia != null && newStatus != PlayerStatus.INDETERMINATE) { if (newMedia != null && newStatus != PlayerStatus.INDETERMINATE) {
if (oldStatus == PlayerStatus.PLAYING && newStatus != PlayerStatus.PLAYING) { if (oldPlayerStatus == PlayerStatus.PLAYING && newStatus != PlayerStatus.PLAYING) {
callback.onPlaybackPause(newMedia, position); 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.onPlaybackStart(newMedia, position);
} }
} }
callback.statusChanged(new PSMPInfo(playerStatus, getPlayable())); callback.statusChanged(new PSMPInfo(oldPlayerStatus, playerStatus, getPlayable()));
} }
/** /**
* @see #setPlayerStatus(PlayerStatus, Playable, int) * @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); setPlayerStatus(newStatus, newMedia, INVALID_TIME);
} }
@ -365,10 +365,12 @@ public abstract class PlaybackServiceMediaPlayer {
* Holds information about a PSMP object. * Holds information about a PSMP object.
*/ */
public static class PSMPInfo { public static class PSMPInfo {
public PlayerStatus oldPlayerStatus;
public PlayerStatus playerStatus; public PlayerStatus playerStatus;
public Playable playable; public Playable playable;
public PSMPInfo(PlayerStatus playerStatus, Playable playable) { PSMPInfo(PlayerStatus oldPlayerStatus, PlayerStatus playerStatus, Playable playable) {
this.oldPlayerStatus = oldPlayerStatus;
this.playerStatus = playerStatus; this.playerStatus = playerStatus;
this.playable = playable; this.playable = playable;
} }

View File

@ -131,7 +131,8 @@ public class PlaybackServiceFlavorHelper {
info = mediaPlayer.getPSMPInfo(); info = mediaPlayer.getPSMPInfo();
} }
if (info == null) { 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()), switchMediaPlayer(new LocalPSMP(context, callback.getMediaPlayerCallback()),
info, true); info, true);
@ -166,7 +167,7 @@ public class PlaybackServiceFlavorHelper {
} }
} }
if (info == null) { 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, callback.sendNotificationBroadcast(PlaybackService.NOTIFICATION_TYPE_RELOAD,
PlaybackService.EXTRA_CODE_CAST); PlaybackService.EXTRA_CODE_CAST);