Smoothier sleep timer (#4314)

This commit is contained in:
Lucas Olivoto 2020-07-31 18:42:17 -03:00 committed by GitHub
parent 16e8c353dc
commit 9a178726a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 15 deletions

View File

@ -172,7 +172,7 @@ public class PlaybackServiceTaskManagerTest {
}
@Override
public void onSleepTimerAlmostExpired() {
public void onSleepTimerAlmostExpired(long timeLeft) {
}
@ -233,7 +233,7 @@ public class PlaybackServiceTaskManagerTest {
}
@Override
public void onSleepTimerAlmostExpired() {
public void onSleepTimerAlmostExpired(long timeLeft) {
}
@ -330,7 +330,7 @@ public class PlaybackServiceTaskManagerTest {
}
@Override
public void onSleepTimerAlmostExpired() {
public void onSleepTimerAlmostExpired(long timeLeft) {
}
@ -376,7 +376,7 @@ public class PlaybackServiceTaskManagerTest {
}
@Override
public void onSleepTimerAlmostExpired() {
public void onSleepTimerAlmostExpired(long timeLeft) {
}
@ -434,7 +434,7 @@ public class PlaybackServiceTaskManagerTest {
}
@Override
public void onSleepTimerAlmostExpired() {
public void onSleepTimerAlmostExpired(long timeLeft) {
}

View File

@ -711,9 +711,12 @@ public class PlaybackService extends MediaBrowserServiceCompat {
}
@Override
public void onSleepTimerAlmostExpired() {
float leftVolume = 0.1f * UserPreferences.getLeftVolume();
float rightVolume = 0.1f * UserPreferences.getRightVolume();
public void onSleepTimerAlmostExpired(long timeLeft) {
final float[] multiplicators = {0.1f, 0.2f, 0.3f, 0.3f, 0.3f, 0.4f, 0.4f, 0.4f, 0.6f, 0.8f};
float multiplicator = multiplicators[Math.max(0, (int) timeLeft / 1000)];
Log.d(TAG, "onSleepTimerAlmostExpired: " + multiplicator);
float leftVolume = multiplicator * UserPreferences.getLeftVolume();
float rightVolume = multiplicator * UserPreferences.getRightVolume();
mediaPlayer.setVolume(leftVolume, rightVolume);
}

View File

@ -360,7 +360,8 @@ public class PlaybackServiceTaskManager {
class SleepTimer implements Runnable {
private static final String TAG = "SleepTimer";
private static final long UPDATE_INTERVAL = 1000L;
private static final long NOTIFICATION_THRESHOLD = 10000;
public static final long NOTIFICATION_THRESHOLD = 10000;
private boolean hasVibrated = false;
private final long waitingTime;
private long timeLeft;
private ShakeListener shakeListener;
@ -390,7 +391,6 @@ public class PlaybackServiceTaskManager {
@Override
public void run() {
Log.d(TAG, "Starting");
boolean notifiedAlmostExpired = false;
long lastTick = System.currentTimeMillis();
while (timeLeft > 0) {
try {
@ -405,19 +405,19 @@ public class PlaybackServiceTaskManager {
timeLeft -= now - lastTick;
lastTick = now;
if (timeLeft < NOTIFICATION_THRESHOLD && !notifiedAlmostExpired) {
if (timeLeft < NOTIFICATION_THRESHOLD) {
Log.d(TAG, "Sleep timer is about to expire");
if (SleepTimerPreferences.vibrate()) {
if (SleepTimerPreferences.vibrate() && !hasVibrated) {
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
v.vibrate(500);
hasVibrated = true;
}
}
if (shakeListener == null && SleepTimerPreferences.shakeToReset()) {
shakeListener = new ShakeListener(context, this);
}
postCallback(callback::onSleepTimerAlmostExpired);
notifiedAlmostExpired = true;
postCallback(() -> callback.onSleepTimerAlmostExpired(timeLeft));
}
if (timeLeft <= 0) {
Log.d(TAG, "Sleep timer expired");
@ -425,6 +425,7 @@ public class PlaybackServiceTaskManager {
shakeListener.pause();
shakeListener = null;
}
hasVibrated = false;
if (!Thread.currentThread().isInterrupted()) {
postCallback(callback::onSleepTimerExpired);
} else {
@ -461,7 +462,7 @@ public class PlaybackServiceTaskManager {
public interface PSTMCallback {
void positionSaverTick();
void onSleepTimerAlmostExpired();
void onSleepTimerAlmostExpired(long timeLeft);
void onSleepTimerExpired();