summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod
diff options
context:
space:
mode:
authordaniel oeh <daniel.oeh@gmail.com>2012-07-27 13:52:54 +0200
committerdaniel oeh <daniel.oeh@gmail.com>2012-07-27 13:52:54 +0200
commit791acc935c483fe096c69a01fd6c9abcf1b3e916 (patch)
tree2eedc1faf3fb1ae91b41551679087a7467c788ff /src/de/danoeh/antennapod
parentb93c5c9b6342468d98932e7ff62378159cb0d44c (diff)
downloadAntennaPod-791acc935c483fe096c69a01fd6c9abcf1b3e916.zip
Implemented sleep timer in playaback service and created time dialog
Diffstat (limited to 'src/de/danoeh/antennapod')
-rw-r--r--src/de/danoeh/antennapod/activity/MediaplayerActivity.java5
-rw-r--r--src/de/danoeh/antennapod/dialog/TimeDialog.java73
-rw-r--r--src/de/danoeh/antennapod/service/PlaybackService.java68
3 files changed, 145 insertions, 1 deletions
diff --git a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java
index bb8d50f61..81ebceb62 100644
--- a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java
+++ b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java
@@ -142,6 +142,11 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements
media != null && media.getItem().getLink() != null);
menu.findItem(R.id.visit_website_item).setVisible(
media != null && media.getItem().getLink() != null);
+
+ boolean sleepTimerSet = playbackService != null && playbackService.sleepTimerActive();
+ boolean sleepTimerNotSet = playbackService != null && !playbackService.sleepTimerActive();
+ menu.findItem(R.id.set_sleeptimer_item).setVisible(sleepTimerNotSet);
+ menu.findItem(R.id.disable_sleeptimer_item).setVisible(sleepTimerSet);
return true;
}
diff --git a/src/de/danoeh/antennapod/dialog/TimeDialog.java b/src/de/danoeh/antennapod/dialog/TimeDialog.java
new file mode 100644
index 000000000..4f820eafb
--- /dev/null
+++ b/src/de/danoeh/antennapod/dialog/TimeDialog.java
@@ -0,0 +1,73 @@
+package de.danoeh.antennapod.dialog;
+
+import java.util.concurrent.TimeUnit;
+
+import de.danoeh.antennapod.R;
+import android.app.Dialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.Spinner;
+
+public abstract class TimeDialog extends Dialog {
+ private int leftButtonTextId;
+ private int titleTextId;
+
+ private EditText etxtTime;
+ private Spinner spTimeUnit;
+ private Button butConfirm;
+ private Button butCancel;
+
+ private String[] spinnerContent = { "min", "h" };
+ private TimeUnit[] units = { TimeUnit.MINUTES, TimeUnit.HOURS };
+
+ public TimeDialog(Context context, int TitleTextId, int leftButtonTextId) {
+ super(context);
+ this.leftButtonTextId = leftButtonTextId;
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.time_dialog);
+ etxtTime = (EditText) findViewById(R.id.etxtTime);
+ spTimeUnit = (Spinner) findViewById(R.id.spTimeUnit);
+ butConfirm = (Button) findViewById(R.id.butConfirm);
+ butCancel = (Button) findViewById(R.id.butCancel);
+ butConfirm.setText(leftButtonTextId);
+ butCancel.setText(R.string.cancel_label);
+ setTitle(titleTextId);
+ spTimeUnit.setAdapter(new ArrayAdapter<String>(this.getContext(), 0,
+ spinnerContent));
+ butCancel.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ dismiss();
+ }
+ });
+ butConfirm.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ dismiss();
+ onTimeEntered(readTimeMillis());
+ }
+ });
+
+ }
+
+ public abstract void onTimeEntered(long millis);
+
+ private long readTimeMillis() {
+ TimeUnit selectedUnit = units[spTimeUnit.getSelectedItemPosition()];
+ long value = Long.valueOf(etxtTime.getText().toString());
+ return selectedUnit.toMillis(value);
+ }
+
+}
diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java
index f991ffbe1..f1328f23e 100644
--- a/src/de/danoeh/antennapod/service/PlaybackService.java
+++ b/src/de/danoeh/antennapod/service/PlaybackService.java
@@ -70,10 +70,13 @@ public class PlaybackService extends Service {
public static final int NOTIFICATION_TYPE_INFO = 1;
public static final int NOTIFICATION_TYPE_BUFFER_UPDATE = 2;
public static final int NOTIFICATION_TYPE_RELOAD = 3;
+ public static final int NOTIFICATION_TYPE_SLEEPTIMER_UPDATE = 4;
/** Is true if service is running. */
public static boolean isRunning = false;
+ private SleepTimer sleepTimer;
+
private static final int NOTIFICATION_ID = 1;
private NotificationCompat.Builder notificationBuilder;
@@ -134,6 +137,7 @@ public class PlaybackService extends Service {
public void onDestroy() {
super.onDestroy();
isRunning = false;
+ disableSleepTimer();
unregisterReceiver(headsetDisconnected);
if (AppConfig.DEBUG)
Log.d(TAG, "Service is about to be destroyed");
@@ -454,6 +458,25 @@ public class PlaybackService extends Service {
}
};
+ public void setSleepTimer(long waitingTime) {
+ if (sleepTimer != null) {
+ sleepTimer.interrupt();
+ }
+ sleepTimer = new SleepTimer(waitingTime);
+ sleepTimer.start();
+ sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
+ }
+
+ public void disableSleepTimer() {
+ if (sleepTimer != null) {
+ if (AppConfig.DEBUG)
+ Log.d(TAG, "Disabling sleep timer");
+ sleepTimer.interrupt();
+ sleepTimer = null;
+ sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
+ }
+ }
+
/**
* Saves the current position and pauses playback
*
@@ -591,6 +614,7 @@ public class PlaybackService extends Service {
}
}
+ @SuppressLint("NewApi")
private void setupWidgetUpdater() {
if (widgetUpdater == null || widgetUpdater.isCancelled()) {
widgetUpdater = new WidgetUpdateWorker();
@@ -608,6 +632,10 @@ public class PlaybackService extends Service {
PlaybackService.this.sendBroadcast(new Intent(
PlayerWidget.FORCE_WIDGET_UPDATE));
}
+
+ public boolean sleepTimerActive() {
+ return sleepTimer == null || sleepTimer.isWaiting();
+ }
/**
* Pauses playback when the headset is disconnected and the preference is
@@ -660,7 +688,7 @@ public class PlaybackService extends Service {
} catch (InterruptedException e) {
if (AppConfig.DEBUG)
Log.d(TAG,
- "Thread was interrupted while waiting. Finishing now...");
+ "Threżad was interrupted while waiting. Finishing now...");
return null;
} catch (IllegalStateException e) {
if (AppConfig.DEBUG)
@@ -699,6 +727,44 @@ public class PlaybackService extends Service {
}
+ /** Sleeps for a given time and then pauses playback. */
+ class SleepTimer extends Thread {
+ private static final String TAG = "SleepTimer";
+ private long waitingTime;
+ private boolean isWaiting;
+
+ public SleepTimer(long waitingTime) {
+ super();
+ this.waitingTime = waitingTime;
+ }
+
+ @Override
+ public void run() {
+ isWaiting = true;
+ if (AppConfig.DEBUG)
+ Log.d(TAG, "Starting");
+ try {
+ Thread.sleep(waitingTime);
+ if (status == PlayerStatus.PLAYING) {
+ Log.d(TAG, "Pausing playback");
+ pause(true);
+ }
+ } catch (InterruptedException e) {
+ Log.d(TAG, "Thread was interrupted while waiting");
+ }
+ isWaiting = false;
+ }
+
+ public long getWaitingTime() {
+ return waitingTime;
+ }
+
+ public boolean isWaiting() {
+ return isWaiting;
+ }
+
+ }
+
public boolean isPlayingVideo() {
return playingVideo;
}