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"?>
<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:showAsAction="collapseActionView"
android:title="@string/share_link_label">

View File

@ -156,6 +156,8 @@
<string name="set_sleeptimer_label">Set sleep timer</string>
<string name="disable_sleeptimer_label">Disable sleep timer</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>

View File

@ -2,6 +2,7 @@ package de.danoeh.antennapod.activity;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
@ -162,7 +163,34 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
break;
case R.id.disable_sleeptimer_item:
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;
case R.id.set_sleeptimer_item:

View File

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

View File

@ -494,6 +494,7 @@ public class PlaybackService extends Service {
player.pause();
if (abandonFocus) {
audioManager.abandonAudioFocus(audioFocusChangeListener);
disableSleepTimer();
}
if (positionSaver != null) {
positionSaver.cancel(true);
@ -641,6 +642,14 @@ public class PlaybackService extends Service {
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
* set
@ -735,7 +744,7 @@ public class PlaybackService extends Service {
class SleepTimer extends AsyncTask<Void, Void, Void> {
private static final String TAG = "SleepTimer";
private static final long UPDATE_INTERVALL = 1000L;
private long waitingTime;
private volatile long waitingTime;
private boolean isWaiting;
public SleepTimer(long waitingTime) {
@ -753,9 +762,14 @@ public class PlaybackService extends Service {
Thread.sleep(UPDATE_INTERVALL);
waitingTime -= UPDATE_INTERVALL;
if (waitingTime <= 0 && status == PlayerStatus.PLAYING) {
Log.d(TAG, "Pausing playback");
pause(true);
if (waitingTime <= 0) {
if (AppConfig.DEBUG)
Log.d(TAG, "Waiting completed");
if (status == PlayerStatus.PLAYING) {
if (AppConfig.DEBUG)
Log.d(TAG, "Pausing playback");
pause(true);
}
return null;
}
} catch (InterruptedException e) {
@ -764,7 +778,7 @@ public class PlaybackService extends Service {
}
return null;
}
@SuppressLint("NewApi")
public void executeAsync() {
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
@ -773,7 +787,7 @@ public class PlaybackService extends Service {
execute();
}
}
@Override
protected void onCancelled() {
isWaiting = false;