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 @Override
public void onSleepTimerAlmostExpired() { public void onSleepTimerAlmostExpired(long timeLeft) {
} }
@ -233,7 +233,7 @@ public class PlaybackServiceTaskManagerTest {
} }
@Override @Override
public void onSleepTimerAlmostExpired() { public void onSleepTimerAlmostExpired(long timeLeft) {
} }
@ -330,7 +330,7 @@ public class PlaybackServiceTaskManagerTest {
} }
@Override @Override
public void onSleepTimerAlmostExpired() { public void onSleepTimerAlmostExpired(long timeLeft) {
} }
@ -376,7 +376,7 @@ public class PlaybackServiceTaskManagerTest {
} }
@Override @Override
public void onSleepTimerAlmostExpired() { public void onSleepTimerAlmostExpired(long timeLeft) {
} }
@ -434,7 +434,7 @@ public class PlaybackServiceTaskManagerTest {
} }
@Override @Override
public void onSleepTimerAlmostExpired() { public void onSleepTimerAlmostExpired(long timeLeft) {
} }

View File

@ -711,9 +711,12 @@ public class PlaybackService extends MediaBrowserServiceCompat {
} }
@Override @Override
public void onSleepTimerAlmostExpired() { public void onSleepTimerAlmostExpired(long timeLeft) {
float leftVolume = 0.1f * UserPreferences.getLeftVolume(); final float[] multiplicators = {0.1f, 0.2f, 0.3f, 0.3f, 0.3f, 0.4f, 0.4f, 0.4f, 0.6f, 0.8f};
float rightVolume = 0.1f * UserPreferences.getRightVolume(); 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); mediaPlayer.setVolume(leftVolume, rightVolume);
} }

View File

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