From b155a4cdf94eb74423cedd54cfd7d0ed376ac4e0 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Fri, 27 Jul 2012 15:46:56 +0200 Subject: [PATCH] Completed sleep timer implementation --- res/menu/mediaplayer.xml | 2 +- res/values/strings.xml | 2 ++ .../activity/MediaplayerActivity.java | 30 ++++++++++++++++++- .../danoeh/antennapod/dialog/TimeDialog.java | 8 +++-- .../antennapod/service/PlaybackService.java | 26 ++++++++++++---- 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/res/menu/mediaplayer.xml b/res/menu/mediaplayer.xml index 13f6b4d0d..4378d0479 100644 --- a/res/menu/mediaplayer.xml +++ b/res/menu/mediaplayer.xml @@ -1,7 +1,7 @@ - diff --git a/res/values/strings.xml b/res/values/strings.xml index cd8d8bf35..f704b0f21 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -156,6 +156,8 @@ Set sleep timer Disable sleep timer Enter time + Sleep timer + Time left:\u0020 \ No newline at end of file diff --git a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java index 39ec5c990..1023fd8f7 100644 --- a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -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: diff --git a/src/de/danoeh/antennapod/dialog/TimeDialog.java b/src/de/danoeh/antennapod/dialog/TimeDialog.java index 9634a19a5..de4ccabd1 100644 --- a/src/de/danoeh/antennapod/dialog/TimeDialog.java +++ b/src/de/danoeh/antennapod/dialog/TimeDialog.java @@ -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 diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java index df519984e..af8b50060 100644 --- a/src/de/danoeh/antennapod/service/PlaybackService.java +++ b/src/de/danoeh/antennapod/service/PlaybackService.java @@ -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 { 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;