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

View File

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

View File

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

View File

@ -791,8 +791,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
// set sleep timer if auto-enabled
if (newInfo.oldPlayerStatus != null && newInfo.oldPlayerStatus != PlayerStatus.SEEKING
&& SleepTimerPreferences.autoEnable() && !sleepTimerActive()) {
setSleepTimer(SleepTimerPreferences.timerMillis(), SleepTimerPreferences.shakeToReset(),
SleepTimerPreferences.vibrate());
setSleepTimer(SleepTimerPreferences.timerMillis());
EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_enabled_label),
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");
taskManager.setSleepTimer(waitingTime, shakeToReset, vibrate);
taskManager.setSleepTimer(waitingTime);
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
}

View File

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