Sleep timer adjustments

- Close button instead of OK
- Save settings on-the-fly
This commit is contained in:
ByteHamster 2020-05-20 10:38:51 +02:00
parent 62fc413f9c
commit 2273d60ac4
6 changed files with 49 additions and 77 deletions

View File

@ -5,6 +5,7 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.annotation.UiThreadTest; import androidx.test.annotation.UiThreadTest;
import androidx.test.filters.LargeTest; import androidx.test.filters.LargeTest;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
import org.awaitility.Awaitility; import org.awaitility.Awaitility;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.junit.After; import org.junit.After;
@ -53,6 +54,8 @@ public class PlaybackServiceTaskManagerTest {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
adapter.close(); adapter.close();
SleepTimerPreferences.setShakeToReset(false);
SleepTimerPreferences.setVibrate(false);
} }
@Test @Test
@ -304,7 +307,7 @@ public class PlaybackServiceTaskManagerTest {
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.startWidgetUpdater(); pstm.startWidgetUpdater();
pstm.startPositionSaver(); pstm.startPositionSaver();
pstm.setSleepTimer(100000, false, false); pstm.setSleepTimer(100000);
pstm.cancelAllTasks(); pstm.cancelAllTasks();
assertFalse(pstm.isPositionSaverActive()); assertFalse(pstm.isPositionSaverActive());
assertFalse(pstm.isWidgetUpdaterActive()); assertFalse(pstm.isWidgetUpdaterActive());
@ -353,7 +356,7 @@ public class PlaybackServiceTaskManagerTest {
} }
}); });
pstm.setSleepTimer(TIME, false, false); pstm.setSleepTimer(TIME);
countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS); countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
pstm.shutdown(); pstm.shutdown();
} }
@ -396,7 +399,7 @@ public class PlaybackServiceTaskManagerTest {
} }
}); });
pstm.setSleepTimer(TIME, false, false); pstm.setSleepTimer(TIME);
pstm.disableSleepTimer(); pstm.disableSleepTimer();
assertFalse(countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)); assertFalse(countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS));
pstm.shutdown(); pstm.shutdown();
@ -407,7 +410,7 @@ public class PlaybackServiceTaskManagerTest {
public void testIsSleepTimerActivePositive() { public void testIsSleepTimerActivePositive() {
final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.setSleepTimer(10000, false, false); pstm.setSleepTimer(1000);
assertTrue(pstm.isSleepTimerActive()); assertTrue(pstm.isSleepTimerActive());
pstm.shutdown(); pstm.shutdown();
} }
@ -417,7 +420,7 @@ public class PlaybackServiceTaskManagerTest {
public void testIsSleepTimerActiveNegative() { public void testIsSleepTimerActiveNegative() {
final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext();
PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
pstm.setSleepTimer(10000, false, false); pstm.setSleepTimer(10000);
pstm.disableSleepTimer(); pstm.disableSleepTimer();
assertFalse(pstm.isSleepTimerActive()); assertFalse(pstm.isSleepTimerActive());
pstm.shutdown(); pstm.shutdown();

View File

@ -409,13 +409,7 @@ public class MainActivity extends CastEnabledActivity {
public void onEventMainThread(MessageEvent event) { public void onEventMainThread(MessageEvent event) {
Log.d(TAG, "onEvent(" + event + ")"); Log.d(TAG, "onEvent(" + event + ")");
Snackbar snackbar; Snackbar snackbar = showSnackbarAbovePlayer(event.message, Snackbar.LENGTH_SHORT);
if (getBottomSheet().getState() == BottomSheetBehavior.STATE_COLLAPSED) {
snackbar = showSnackbarAbovePlayer(event.message, Snackbar.LENGTH_SHORT);
} else {
snackbar = Snackbar.make(findViewById(android.R.id.content), event.message, Snackbar.LENGTH_SHORT);
snackbar.show();
}
if (event.action != null) { if (event.action != null) {
snackbar.setAction(getString(R.string.undo), v -> event.action.run()); snackbar.setAction(getString(R.string.undo), v -> event.action.run());
} }
@ -453,21 +447,21 @@ public class MainActivity extends CastEnabledActivity {
setIntent(intent); setIntent(intent);
} }
public Snackbar showSnackbarAbovePlayer(int text, int duration) { public Snackbar showSnackbarAbovePlayer(CharSequence text, int duration) {
Snackbar s = Snackbar.make(findViewById(R.id.main_view), text, duration); Snackbar s;
if (findViewById(R.id.audioplayerFragment).getVisibility() == View.VISIBLE) { if (getBottomSheet().getState() == BottomSheetBehavior.STATE_COLLAPSED) {
s.setAnchorView(findViewById(R.id.audioplayerFragment)); s = Snackbar.make(findViewById(R.id.main_view), text, duration);
if (findViewById(R.id.audioplayerFragment).getVisibility() == View.VISIBLE) {
s.setAnchorView(findViewById(R.id.audioplayerFragment));
}
} else {
s = Snackbar.make(findViewById(android.R.id.content), text, duration);
} }
s.show(); s.show();
return s; return s;
} }
public Snackbar showSnackbarAbovePlayer(String text, int duration) { public Snackbar showSnackbarAbovePlayer(int text, int duration) {
Snackbar s = Snackbar.make(findViewById(R.id.main_view), text, duration); return showSnackbarAbovePlayer(getResources().getText(text), duration);
if (findViewById(R.id.audioplayerFragment).getVisibility() == View.VISIBLE) {
s.setAnchorView(findViewById(R.id.audioplayerFragment));
}
s.show();
return s;
} }
} }

View File

@ -2,9 +2,8 @@ package de.danoeh.antennapod.dialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
@ -14,7 +13,6 @@ import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
@ -36,9 +34,6 @@ public class SleepTimerDialog extends DialogFragment {
private EditText etxtTime; private EditText etxtTime;
private Spinner spTimeUnit; private Spinner spTimeUnit;
private CheckBox cbShakeToReset;
private CheckBox cbVibrate;
private CheckBox chAutoEnable;
private LinearLayout timeSetup; private LinearLayout timeSetup;
private LinearLayout timeDisplay; private LinearLayout timeDisplay;
private TextView time; private TextView time;
@ -85,33 +80,15 @@ public class SleepTimerDialog extends DialogFragment {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(R.string.sleep_timer_label); builder.setTitle(R.string.sleep_timer_label);
builder.setView(content); builder.setView(content);
builder.setPositiveButton(android.R.string.ok, null); builder.setPositiveButton(R.string.close_label, null);
etxtTime = content.findViewById(R.id.etxtTime); etxtTime = content.findViewById(R.id.etxtTime);
spTimeUnit = content.findViewById(R.id.spTimeUnit); spTimeUnit = content.findViewById(R.id.spTimeUnit);
cbShakeToReset = content.findViewById(R.id.cbShakeToReset);
cbVibrate = content.findViewById(R.id.cbVibrate);
chAutoEnable = content.findViewById(R.id.chAutoEnable);
timeSetup = content.findViewById(R.id.timeSetup); timeSetup = content.findViewById(R.id.timeSetup);
timeDisplay = content.findViewById(R.id.timeDisplay); timeDisplay = content.findViewById(R.id.timeDisplay);
time = content.findViewById(R.id.time); time = content.findViewById(R.id.time);
AlertDialog dialog = builder.create();
etxtTime.setText(SleepTimerPreferences.lastTimerValue()); etxtTime.setText(SleepTimerPreferences.lastTimerValue());
etxtTime.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(s.length() > 0);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
etxtTime.postDelayed(() -> { etxtTime.postDelayed(() -> {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etxtTime, InputMethodManager.SHOW_IMPLICIT); imm.showSoftInput(etxtTime, InputMethodManager.SHOW_IMPLICIT);
@ -127,12 +104,21 @@ public class SleepTimerDialog extends DialogFragment {
spTimeUnit.setAdapter(spinnerAdapter); spTimeUnit.setAdapter(spinnerAdapter);
spTimeUnit.setSelection(SleepTimerPreferences.lastTimerTimeUnit()); spTimeUnit.setSelection(SleepTimerPreferences.lastTimerTimeUnit());
CheckBox cbShakeToReset = content.findViewById(R.id.cbShakeToReset);
CheckBox cbVibrate = content.findViewById(R.id.cbVibrate);
CheckBox chAutoEnable = content.findViewById(R.id.chAutoEnable);
cbShakeToReset.setChecked(SleepTimerPreferences.shakeToReset()); cbShakeToReset.setChecked(SleepTimerPreferences.shakeToReset());
cbVibrate.setChecked(SleepTimerPreferences.vibrate()); cbVibrate.setChecked(SleepTimerPreferences.vibrate());
chAutoEnable.setChecked(SleepTimerPreferences.autoEnable()); chAutoEnable.setChecked(SleepTimerPreferences.autoEnable());
cbShakeToReset.setOnCheckedChangeListener((buttonView, isChecked)
-> SleepTimerPreferences.setShakeToReset(isChecked));
cbVibrate.setOnCheckedChangeListener((buttonView, isChecked)
-> SleepTimerPreferences.setVibrate(isChecked));
chAutoEnable.setOnCheckedChangeListener((compoundButton, isChecked) chAutoEnable.setOnCheckedChangeListener((compoundButton, isChecked)
-> SleepTimerPreferences.setAutoEnable(isChecked)); -> SleepTimerPreferences.setAutoEnable(isChecked));
Button disableButton = content.findViewById(R.id.disableSleeptimerButton); Button disableButton = content.findViewById(R.id.disableSleeptimerButton);
disableButton.setOnClickListener(v -> { disableButton.setOnClickListener(v -> {
if (controller != null) { if (controller != null) {
@ -143,27 +129,20 @@ public class SleepTimerDialog extends DialogFragment {
setButton.setOnClickListener(v -> { setButton.setOnClickListener(v -> {
if (!PlaybackService.isRunning) { if (!PlaybackService.isRunning) {
Snackbar.make(content, R.string.no_media_playing_label, Snackbar.LENGTH_LONG).show(); Snackbar.make(content, R.string.no_media_playing_label, Snackbar.LENGTH_LONG).show();
return;
} }
try { try {
savePreferences(); SleepTimerPreferences.setLastTimer(etxtTime.getText().toString(), spTimeUnit.getSelectedItemPosition());
long time = SleepTimerPreferences.timerMillis(); long time = SleepTimerPreferences.timerMillis();
if (controller != null) { if (controller != null) {
controller.setSleepTimer(time, cbShakeToReset.isChecked(), cbVibrate.isChecked()); controller.setSleepTimer(time);
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
e.printStackTrace(); e.printStackTrace();
Toast.makeText(getContext(), R.string.time_dialog_invalid_input, Toast.LENGTH_LONG).show(); Snackbar.make(content, R.string.time_dialog_invalid_input, Snackbar.LENGTH_LONG).show();
} }
}); });
return dialog; return builder.create();
}
private void savePreferences() {
SleepTimerPreferences.setLastTimer(etxtTime.getText().toString(),
spTimeUnit.getSelectedItemPosition());
SleepTimerPreferences.setShakeToReset(cbShakeToReset.isChecked());
SleepTimerPreferences.setVibrate(cbVibrate.isChecked());
SleepTimerPreferences.setAutoEnable(chAutoEnable.isChecked());
} }
private void updateTime() { private void updateTime() {

View File

@ -791,8 +791,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
// set sleep timer if auto-enabled // set sleep timer if auto-enabled
if (newInfo.oldPlayerStatus != null && newInfo.oldPlayerStatus != PlayerStatus.SEEKING if (newInfo.oldPlayerStatus != null && newInfo.oldPlayerStatus != PlayerStatus.SEEKING
&& SleepTimerPreferences.autoEnable() && !sleepTimerActive()) { && SleepTimerPreferences.autoEnable() && !sleepTimerActive()) {
setSleepTimer(SleepTimerPreferences.timerMillis(), SleepTimerPreferences.shakeToReset(), setSleepTimer(SleepTimerPreferences.timerMillis());
SleepTimerPreferences.vibrate());
EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_enabled_label), EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_enabled_label),
PlaybackService.this::disableSleepTimer)); PlaybackService.this::disableSleepTimer));
} }
@ -1055,9 +1054,9 @@ public class PlaybackService extends MediaBrowserServiceCompat {
} }
} }
public void setSleepTimer(long waitingTime, boolean shakeToReset, boolean vibrate) { public void setSleepTimer(long waitingTime) {
Log.d(TAG, "Setting sleep timer to " + waitingTime + " milliseconds"); Log.d(TAG, "Setting sleep timer to " + waitingTime + " milliseconds");
taskManager.setSleepTimer(waitingTime, shakeToReset, vibrate); taskManager.setSleepTimer(waitingTime);
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0); sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
} }

View File

@ -7,6 +7,7 @@ import android.os.Vibrator;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import android.util.Log; import android.util.Log;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
@ -215,7 +216,7 @@ public class PlaybackServiceTaskManager {
* *
* @throws java.lang.IllegalArgumentException if waitingTime <= 0 * @throws java.lang.IllegalArgumentException if waitingTime <= 0
*/ */
public synchronized void setSleepTimer(long waitingTime, boolean shakeToReset, boolean vibrate) { public synchronized void setSleepTimer(long waitingTime) {
if (waitingTime <= 0) { if (waitingTime <= 0) {
throw new IllegalArgumentException("Waiting time <= 0"); throw new IllegalArgumentException("Waiting time <= 0");
} }
@ -224,7 +225,7 @@ public class PlaybackServiceTaskManager {
if (isSleepTimerActive()) { if (isSleepTimerActive()) {
sleepTimerFuture.cancel(true); sleepTimerFuture.cancel(true);
} }
sleepTimer = new SleepTimer(waitingTime, shakeToReset, vibrate); sleepTimer = new SleepTimer(waitingTime);
sleepTimerFuture = schedExecutor.schedule(sleepTimer, 0, TimeUnit.MILLISECONDS); sleepTimerFuture = schedExecutor.schedule(sleepTimer, 0, TimeUnit.MILLISECONDS);
} }
@ -361,17 +362,13 @@ public class PlaybackServiceTaskManager {
private static final long NOTIFICATION_THRESHOLD = 10000; private static final long NOTIFICATION_THRESHOLD = 10000;
private final long waitingTime; private final long waitingTime;
private long timeLeft; private long timeLeft;
private final boolean shakeToReset;
private final boolean vibrate;
private ShakeListener shakeListener; private ShakeListener shakeListener;
private final Handler handler; private final Handler handler;
public SleepTimer(long waitingTime, boolean shakeToReset, boolean vibrate) { public SleepTimer(long waitingTime) {
super(); super();
this.waitingTime = waitingTime; this.waitingTime = waitingTime;
this.timeLeft = waitingTime; this.timeLeft = waitingTime;
this.shakeToReset = shakeToReset;
this.vibrate = vibrate;
if (UserPreferences.useExoplayer() && Looper.myLooper() == Looper.getMainLooper()) { if (UserPreferences.useExoplayer() && Looper.myLooper() == Looper.getMainLooper()) {
// Run callbacks in main thread so they can call ExoPlayer methods themselves // Run callbacks in main thread so they can call ExoPlayer methods themselves
@ -409,13 +406,13 @@ public class PlaybackServiceTaskManager {
if (timeLeft < 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 (SleepTimerPreferences.vibrate()) {
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);
} }
} }
if (shakeListener == null && shakeToReset) { if (shakeListener == null && SleepTimerPreferences.shakeToReset()) {
shakeListener = new ShakeListener(context, this); shakeListener = new ShakeListener(context, this);
} }
postCallback(callback::onSleepTimerAlmostExpired); postCallback(callback::onSleepTimerAlmostExpired);
@ -442,7 +439,7 @@ public class PlaybackServiceTaskManager {
public void restart() { public void restart() {
postCallback(() -> { postCallback(() -> {
setSleepTimer(waitingTime, shakeToReset, vibrate); setSleepTimer(waitingTime);
callback.onSleepTimerReset(); callback.onSleepTimerReset();
}); });
if (shakeListener != null) { if (shakeListener != null) {

View File

@ -556,9 +556,9 @@ public class PlaybackController {
} }
} }
public void setSleepTimer(long time, boolean shakeToReset, boolean vibrate) { public void setSleepTimer(long time) {
if (playbackService != null) { if (playbackService != null) {
playbackService.setSleepTimer(time, shakeToReset, vibrate); playbackService.setSleepTimer(time);
} }
} }