Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sleep Timer: Stop at end of track #4535

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
Expand All @@ -21,6 +20,7 @@
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import io.reactivex.Observable;
Expand Down Expand Up @@ -89,16 +89,17 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
timeDisplay = content.findViewById(R.id.timeDisplay);
time = content.findViewById(R.id.time);

etxtTime.setText(SleepTimerPreferences.lastTimerValue());
etxtTime.setText("" + SleepTimerPreferences.lastTimerValue());
etxtTime.postDelayed(() -> {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etxtTime, InputMethodManager.SHOW_IMPLICIT);
}, 100);

String[] spinnerContent = new String[] {
String[] spinnerContent = new String[]{
getString(R.string.time_seconds),
getString(R.string.time_minutes),
getString(R.string.time_hours) };
getString(R.string.time_hours),
getString(R.string.time_episodes)};
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_spinner_item, spinnerContent);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Expand Down Expand Up @@ -132,17 +133,34 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
Snackbar.make(content, R.string.no_media_playing_label, Snackbar.LENGTH_LONG).show();
return;
}

int timeUnit = spTimeUnit.getSelectedItemPosition();
int timeValue;
try {
SleepTimerPreferences.setLastTimer(etxtTime.getText().toString(), spTimeUnit.getSelectedItemPosition());
timeValue = Integer.parseInt(etxtTime.getText().toString());
} catch (Exception e) {
e.printStackTrace();
Snackbar.make(content, R.string.time_dialog_invalid_input, Snackbar.LENGTH_LONG).show();
return;
}

if (timeUnit < 3) {
//Seconds, minutes, hours
SleepTimerPreferences.setLastTimer(timeValue, timeUnit);
long time = SleepTimerPreferences.timerMillis();
if (controller != null) {
controller.setSleepTimer(time);
}
closeKeyboard(content);
} catch (NumberFormatException e) {
e.printStackTrace();
Snackbar.make(content, R.string.time_dialog_invalid_input, Snackbar.LENGTH_LONG).show();
} else {
//Episodes
if(timeValue >= DBReader.getQueueIDList().size()){
Snackbar.make(content, "Invalid input, there are not so many episodes in the queue", Snackbar.LENGTH_LONG).show();
return;
}

SleepTimerPreferences.setStopAfterEpisodes(timeValue);
}
closeKeyboard(content);
});
return builder.create();
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/time_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/setSleeptimerButton"/>

</LinearLayout>

<LinearLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ public class SleepTimerPreferences {
private static final String TAG = "SleepTimerPreferences";

private static final String PREF_NAME = "SleepTimerDialog";
private static final String PREF_VALUE = "LastValue";
private static final String PREF_VALUE = "LastValueInt";
private static final String PREF_TIME_UNIT = "LastTimeUnit";
private static final String PREF_VIBRATE = "Vibrate";
private static final String PREF_SHAKE_TO_RESET = "ShakeToReset";
private static final String PREF_AUTO_ENABLE = "AutoEnable";
private static final String PREF_STOP_AFTER_EPISODE = "StopAfterEpisode";

private static final TimeUnit[] UNITS = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS };

private static final String DEFAULT_VALUE = "15";
private static final int DEFAULT_VALUE = 15;
private static final int DEFAULT_TIME_UNIT = 1;

private static SharedPreferences prefs;
Expand All @@ -35,21 +36,21 @@ public static void init(@NonNull Context context) {
SleepTimerPreferences.prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}

public static void setLastTimer(String value, int timeUnit) {
prefs.edit().putString(PREF_VALUE, value).putInt(PREF_TIME_UNIT, timeUnit).apply();
public static void setLastTimer(int value, int timeUnit) {
prefs.edit().putInt(PREF_VALUE, value).putInt(PREF_TIME_UNIT, timeUnit).apply();
resetStopAfterEpisodes();
}

public static String lastTimerValue() {
return prefs.getString(PREF_VALUE, DEFAULT_VALUE);
public static int lastTimerValue() {
return prefs.getInt(PREF_VALUE, DEFAULT_VALUE);
}

public static int lastTimerTimeUnit() {
return prefs.getInt(PREF_TIME_UNIT, DEFAULT_TIME_UNIT);
}

public static long timerMillis() {
long value = Long.parseLong(lastTimerValue());
return UNITS[lastTimerTimeUnit()].toMillis(value);
return UNITS[lastTimerTimeUnit()].toMillis(lastTimerValue());
}

public static void setVibrate(boolean vibrate) {
Expand All @@ -76,4 +77,39 @@ public static boolean autoEnable() {
return prefs.getBoolean(PREF_AUTO_ENABLE, false);
}

public static void setStopAfterEpisodes(int value) {
if (value <= 0) {
value = 1;
}
prefs.edit().putInt(PREF_VALUE, value).putInt(PREF_STOP_AFTER_EPISODE, value).apply();
}

private static int getStopAfterEpisodes() {
return prefs.getInt(PREF_STOP_AFTER_EPISODE, -1);
}

private static void resetStopAfterEpisodes() {
prefs.edit().putInt(PREF_STOP_AFTER_EPISODE, -1).apply();
}

public static void restartStopAfterEpisode() {
if (isStopAfterEpisodeEnabled())
setStopAfterEpisodes(lastTimerValue());
}

public static boolean isStopAfterEpisodeEnabled() {
return getStopAfterEpisodes() > 0;
}

public static boolean stopAfterEpisode() {
int stopAfterEpisodes = getStopAfterEpisodes();
boolean stopAfter = (stopAfterEpisodes == 1);
if (stopAfter) {
resetStopAfterEpisodes();
} else {
setStopAfterEpisodes(stopAfterEpisodes - 1);
}
return stopAfter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@

import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.event.FeedItemEvent;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.event.PlaybackPositionEvent;
import de.danoeh.antennapod.core.event.ServiceEvent;
Expand Down Expand Up @@ -927,6 +926,9 @@ public Playable getNextInQueue(Playable currentMedia) {

@Override
public void onPlaybackEnded(MediaType mediaType, boolean stopPlaying) {
if (SleepTimerPreferences.stopAfterEpisode()) {
stopPlaying = true;
}
PlaybackService.this.onPlaybackEnded(mediaType, stopPlaying);
}
};
Expand Down
1 change: 1 addition & 0 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@
<string name="time_seconds">seconds</string>
<string name="time_minutes">minutes</string>
<string name="time_hours">hours</string>
<string name="time_episodes">episodes</string>
<plurals name="time_seconds_quantified">
<item quantity="one">1 second</item>
<item quantity="other">%d seconds</item>
Expand Down