Merge pull request #2095 from mfietz/issue/2070-sleep-time-shake

On shake, reset timer to the current waiting time
This commit is contained in:
Martin Fietz 2016-08-13 09:56:35 +02:00 committed by GitHub
commit da063d5e81
1 changed files with 9 additions and 7 deletions

View File

@ -299,7 +299,8 @@ public class PlaybackServiceTaskManager {
private static final String TAG = "SleepTimer"; private static final String TAG = "SleepTimer";
private static final long UPDATE_INTERVAL = 1000L; private static final long UPDATE_INTERVAL = 1000L;
private static final long NOTIFICATION_THRESHOLD = 10000; private static final long NOTIFICATION_THRESHOLD = 10000;
private long waitingTime; private final long waitingTime;
private long timeLeft;
private final boolean shakeToReset; private final boolean shakeToReset;
private final boolean vibrate; private final boolean vibrate;
private ShakeListener shakeListener; private ShakeListener shakeListener;
@ -307,6 +308,7 @@ public class PlaybackServiceTaskManager {
public SleepTimer(long waitingTime, boolean shakeToReset, boolean vibrate) { public SleepTimer(long waitingTime, boolean shakeToReset, boolean vibrate) {
super(); super();
this.waitingTime = waitingTime; this.waitingTime = waitingTime;
this.timeLeft = waitingTime;
this.shakeToReset = shakeToReset; this.shakeToReset = shakeToReset;
this.vibrate = vibrate; this.vibrate = vibrate;
} }
@ -316,14 +318,14 @@ public class PlaybackServiceTaskManager {
Log.d(TAG, "Starting"); Log.d(TAG, "Starting");
boolean notifiedAlmostExpired = false; boolean notifiedAlmostExpired = false;
long lastTick = System.currentTimeMillis(); long lastTick = System.currentTimeMillis();
while (waitingTime > 0) { while (timeLeft > 0) {
try { try {
Thread.sleep(UPDATE_INTERVAL); Thread.sleep(UPDATE_INTERVAL);
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
waitingTime -= now - lastTick; timeLeft -= now - lastTick;
lastTick = now; lastTick = now;
if(waitingTime < NOTIFICATION_THRESHOLD && !notifiedAlmostExpired) { if(timeLeft < NOTIFICATION_THRESHOLD && !notifiedAlmostExpired) {
Log.d(TAG, "Sleep timer is about to expire"); Log.d(TAG, "Sleep timer is about to expire");
if(vibrate) { if(vibrate) {
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
@ -337,7 +339,7 @@ public class PlaybackServiceTaskManager {
callback.onSleepTimerAlmostExpired(); callback.onSleepTimerAlmostExpired();
notifiedAlmostExpired = true; notifiedAlmostExpired = true;
} }
if (waitingTime <= 0) { if (timeLeft <= 0) {
Log.d(TAG, "Sleep timer expired"); Log.d(TAG, "Sleep timer expired");
if(shakeListener != null) { if(shakeListener != null) {
shakeListener.pause(); shakeListener.pause();
@ -358,11 +360,11 @@ public class PlaybackServiceTaskManager {
} }
public long getWaitingTime() { public long getWaitingTime() {
return waitingTime; return timeLeft;
} }
public void onShake() { public void onShake() {
setSleepTimer(15 * 60 * 1000, shakeToReset, vibrate); setSleepTimer(waitingTime, shakeToReset, vibrate);
callback.onSleepTimerReset(); callback.onSleepTimerReset();
shakeListener.pause(); shakeListener.pause();
shakeListener = null; shakeListener = null;