Completed sleep timer implementation

This commit is contained in:
daniel oeh 2012-07-27 15:46:56 +02:00
parent e23c1a0143
commit b155a4cdf9
5 changed files with 57 additions and 11 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/disable_sleeptimer_item" android:icon="@drawable/device_access_time" android:title="@string/disable_sleeptimer_label" android:showAsAction="always"></item><item android:id="@+id/set_sleeptimer_item" android:showAsAction="collapseActionView" android:title="@string/set_sleeptimer_label"></item><item <item android:id="@+id/disable_sleeptimer_item" android:icon="@drawable/device_access_time" android:title="@string/sleep_timer_label" android:showAsAction="always"></item><item android:id="@+id/set_sleeptimer_item" android:showAsAction="collapseActionView" android:title="@string/set_sleeptimer_label"></item><item
android:id="@+id/share_link_item" android:id="@+id/share_link_item"
android:showAsAction="collapseActionView" android:showAsAction="collapseActionView"
android:title="@string/share_link_label"> android:title="@string/share_link_label">

View File

@ -156,6 +156,8 @@
<string name="set_sleeptimer_label">Set sleep timer</string> <string name="set_sleeptimer_label">Set sleep timer</string>
<string name="disable_sleeptimer_label">Disable sleep timer</string> <string name="disable_sleeptimer_label">Disable sleep timer</string>
<string name="enter_time_here_label">Enter time</string> <string name="enter_time_here_label">Enter time</string>
<string name="sleep_timer_label">Sleep timer</string>
<string name="time_left_label">Time left:\u0020</string>
</resources> </resources>

View File

@ -2,6 +2,7 @@ package de.danoeh.antennapod.activity;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
@ -162,7 +163,34 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
break; break;
case R.id.disable_sleeptimer_item: case R.id.disable_sleeptimer_item:
if (playbackService != null) { if (playbackService != null) {
playbackService.disableSleepTimer(); AlertDialog.Builder stDialog = new AlertDialog.Builder(this);
stDialog.setTitle(R.string.sleep_timer_label);
stDialog.setMessage(getString(R.string.time_left_label)
+ Converter
.getDurationStringLong((int) playbackService
.getSleepTimerTimeLeft()));
stDialog.setPositiveButton(R.string.disable_sleeptimer_label,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
if (playbackService != null) {
playbackService.disableSleepTimer();
}
}
});
stDialog.setNegativeButton(R.string.cancel_label,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
stDialog.create().show();
} }
break; break;
case R.id.set_sleeptimer_item: case R.id.set_sleeptimer_item:

View File

@ -16,13 +16,15 @@ import android.widget.Spinner;
public abstract class TimeDialog extends Dialog { public abstract class TimeDialog extends Dialog {
private static final int DEFAULT_SPINNER_POSITION = 1;
private EditText etxtTime; private EditText etxtTime;
private Spinner spTimeUnit; private Spinner spTimeUnit;
private Button butConfirm; private Button butConfirm;
private Button butCancel; private Button butCancel;
private String[] spinnerContent = { "min", "h" }; private String[] spinnerContent = { "s", "min", "h" };
private TimeUnit[] units = { TimeUnit.MINUTES, TimeUnit.HOURS }; private TimeUnit[] units = { TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS };
public TimeDialog(Context context, int titleTextId, int leftButtonTextId) { public TimeDialog(Context context, int titleTextId, int leftButtonTextId) {
super(context); super(context);
@ -45,7 +47,7 @@ public abstract class TimeDialog extends Dialog {
spinnerContent); spinnerContent);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTimeUnit.setAdapter(spinnerAdapter); spTimeUnit.setAdapter(spinnerAdapter);
spTimeUnit.setSelection(0); spTimeUnit.setSelection(DEFAULT_SPINNER_POSITION);
butCancel.setOnClickListener(new View.OnClickListener() { butCancel.setOnClickListener(new View.OnClickListener() {
@Override @Override

View File

@ -494,6 +494,7 @@ public class PlaybackService extends Service {
player.pause(); player.pause();
if (abandonFocus) { if (abandonFocus) {
audioManager.abandonAudioFocus(audioFocusChangeListener); audioManager.abandonAudioFocus(audioFocusChangeListener);
disableSleepTimer();
} }
if (positionSaver != null) { if (positionSaver != null) {
positionSaver.cancel(true); positionSaver.cancel(true);
@ -641,6 +642,14 @@ public class PlaybackService extends Service {
return sleepTimer != null && sleepTimer.isWaiting(); return sleepTimer != null && sleepTimer.isWaiting();
} }
public long getSleepTimerTimeLeft() {
if (sleepTimerActive()) {
return sleepTimer.getWaitingTime();
} else {
return 0;
}
}
/** /**
* Pauses playback when the headset is disconnected and the preference is * Pauses playback when the headset is disconnected and the preference is
* set * set
@ -735,7 +744,7 @@ public class PlaybackService extends Service {
class SleepTimer extends AsyncTask<Void, Void, Void> { class SleepTimer extends AsyncTask<Void, Void, Void> {
private static final String TAG = "SleepTimer"; private static final String TAG = "SleepTimer";
private static final long UPDATE_INTERVALL = 1000L; private static final long UPDATE_INTERVALL = 1000L;
private long waitingTime; private volatile long waitingTime;
private boolean isWaiting; private boolean isWaiting;
public SleepTimer(long waitingTime) { public SleepTimer(long waitingTime) {
@ -753,9 +762,14 @@ public class PlaybackService extends Service {
Thread.sleep(UPDATE_INTERVALL); Thread.sleep(UPDATE_INTERVALL);
waitingTime -= UPDATE_INTERVALL; waitingTime -= UPDATE_INTERVALL;
if (waitingTime <= 0 && status == PlayerStatus.PLAYING) { if (waitingTime <= 0) {
Log.d(TAG, "Pausing playback"); if (AppConfig.DEBUG)
pause(true); Log.d(TAG, "Waiting completed");
if (status == PlayerStatus.PLAYING) {
if (AppConfig.DEBUG)
Log.d(TAG, "Pausing playback");
pause(true);
}
return null; return null;
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {