summaryrefslogtreecommitdiff
path: root/src/de/danoeh
diff options
context:
space:
mode:
authordaniel oeh <daniel.oeh@gmail.com>2012-09-26 12:33:54 +0200
committerdaniel oeh <daniel.oeh@gmail.com>2012-09-26 12:33:54 +0200
commitff364f13f99c021657cdaceaf25728d8a33afa2d (patch)
treefcfc813e2924c41792ddc907161430318b248e50 /src/de/danoeh
parent5bd7ee129e4e312dabc519478b6a5e1af0527966 (diff)
downloadAntennaPod-ff364f13f99c021657cdaceaf25728d8a33afa2d.zip
Made sure getDuration() and getCurrentPosition() are not called in an
invalid state
Diffstat (limited to 'src/de/danoeh')
-rw-r--r--src/de/danoeh/antennapod/activity/MediaplayerActivity.java5
-rw-r--r--src/de/danoeh/antennapod/service/PlaybackService.java70
-rw-r--r--src/de/danoeh/antennapod/service/PlayerWidgetService.java27
-rw-r--r--src/de/danoeh/antennapod/util/PlaybackController.java16
4 files changed, 88 insertions, 30 deletions
diff --git a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java
index 10107d440..91f2f99a2 100644
--- a/src/de/danoeh/antennapod/activity/MediaplayerActivity.java
+++ b/src/de/danoeh/antennapod/activity/MediaplayerActivity.java
@@ -23,6 +23,7 @@ import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.dialog.TimeDialog;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.feed.FeedMedia;
+import de.danoeh.antennapod.service.PlaybackService;
import de.danoeh.antennapod.storage.DownloadRequestException;
import de.danoeh.antennapod.util.Converter;
import de.danoeh.antennapod.util.MediaPlayerError;
@@ -309,8 +310,8 @@ public abstract class MediaplayerActivity extends SherlockFragmentActivity
protected void onPositionObserverUpdate() {
int currentPosition = controller.getPosition();
int duration = controller.getDuration();
- if (currentPosition != PlaybackController.INVALID_TIME
- && duration != PlaybackController.INVALID_TIME) {
+ if (currentPosition != PlaybackService.INVALID_TIME
+ && duration != PlaybackService.INVALID_TIME) {
controller.getMedia().setPosition(currentPosition);
txtvPosition.setText(Converter
.getDurationStringLong(currentPosition));
diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java
index 38ee2cf83..315cf2509 100644
--- a/src/de/danoeh/antennapod/service/PlaybackService.java
+++ b/src/de/danoeh/antennapod/service/PlaybackService.java
@@ -106,6 +106,12 @@ public class PlaybackService extends Service {
public static final int NOTIFICATION_TYPE_BUFFER_START = 5;
public static final int NOTIFICATION_TYPE_BUFFER_END = 6;
+ /**
+ * Returned by getPositionSafe() or getDurationSafe() if the playbackService
+ * is in an invalid state.
+ */
+ public static final int INVALID_TIME = -1;
+
/** Is true if service is running. */
public static boolean isRunning = false;
@@ -134,7 +140,7 @@ public class PlaybackService extends Service {
private SleepTimer sleepTimer;
private Future sleepTimerFuture;
-
+
private Thread chapterLoader;
private static final int SCHED_EX_POOL_SIZE = 3;
@@ -748,7 +754,8 @@ public class PlaybackService extends Service {
/** Pauses playback and destroys service. Recommended for video playback. */
public void stop() {
- if (AppConfig.DEBUG) Log.d(TAG, "Stopping playback");
+ if (AppConfig.DEBUG)
+ Log.d(TAG, "Stopping playback");
player.stop();
stopSelf();
}
@@ -838,7 +845,10 @@ public class PlaybackService extends Service {
* offset from current position (positive or negative)
* */
public void seekDelta(int delta) {
- seek(player.getCurrentPosition() + delta);
+ int position = getCurrentPositionSafe();
+ if (position != INVALID_TIME) {
+ seek(player.getCurrentPosition() + delta);
+ }
}
public void seek(int i) {
@@ -860,11 +870,13 @@ public class PlaybackService extends Service {
/** Saves the current position of the media file to the DB */
private synchronized void saveCurrentPosition() {
- if (AppConfig.DEBUG)
- Log.d(TAG,
- "Saving current position to " + player.getCurrentPosition());
- media.setPosition(player.getCurrentPosition());
- manager.setFeedMedia(this, media);
+ int position = getCurrentPositionSafe();
+ if (position != INVALID_TIME) {
+ if (AppConfig.DEBUG)
+ Log.d(TAG, "Saving current position to " + position);
+ media.setPosition(position);
+ manager.setFeedMedia(this, media);
+ }
}
private void stopWidgetUpdater() {
@@ -1133,4 +1145,46 @@ public class PlaybackService extends Service {
postStatusUpdateIntent();
}
+ /**
+ * call getDuration() on mediaplayer or return INVALID_TIME if player is in
+ * an invalid state. This method should be used instead of calling
+ * getDuration() directly to avoid an error.
+ */
+ public int getDurationSafe() {
+ if (status != null && player != null) {
+ switch (status) {
+ case PREPARED:
+ case PLAYING:
+ case PAUSED:
+ case SEEKING:
+ return player.getDuration();
+ default:
+ return INVALID_TIME;
+ }
+ } else {
+ return INVALID_TIME;
+ }
+ }
+
+ /**
+ * call getCurrentPosition() on mediaplayer or return INVALID_TIME if player
+ * is in an invalid state. This method should be used instead of calling
+ * getCurrentPosition() directly to avoid an error.
+ */
+ public int getCurrentPositionSafe() {
+ if (status != null && player != null) {
+ switch (status) {
+ case PREPARED:
+ case PLAYING:
+ case PAUSED:
+ case SEEKING:
+ return player.getCurrentPosition();
+ default:
+ return INVALID_TIME;
+ }
+ } else {
+ return INVALID_TIME;
+ }
+ }
+
}
diff --git a/src/de/danoeh/antennapod/service/PlayerWidgetService.java b/src/de/danoeh/antennapod/service/PlayerWidgetService.java
index 74fda6de9..08a2680e4 100644
--- a/src/de/danoeh/antennapod/service/PlayerWidgetService.java
+++ b/src/de/danoeh/antennapod/service/PlayerWidgetService.java
@@ -86,14 +86,15 @@ public class PlayerWidgetService extends Service {
views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer);
if (playbackService != null) {
FeedMedia media = playbackService.getMedia();
- MediaPlayer player = playbackService.getPlayer();
PlayerStatus status = playbackService.getStatus();
views.setTextViewText(R.id.txtvTitle, media.getItem().getTitle());
if (status == PlayerStatus.PLAYING) {
- views.setTextViewText(R.id.txtvProgress,
- getProgressString(player));
+ String progressString = getProgressString(playbackService);
+ if (progressString != null) {
+ views.setTextViewText(R.id.txtvProgress, progressString);
+ }
views.setImageViewResource(R.id.butPlay, R.drawable.av_pause);
} else {
views.setImageViewResource(R.id.butPlay, R.drawable.av_play);
@@ -125,10 +126,16 @@ public class PlayerWidgetService extends Service {
return PendingIntent.getBroadcast(this, 0, startingIntent, 0);
}
- private String getProgressString(MediaPlayer player) {
-
- return Converter.getDurationStringLong(player.getCurrentPosition())
- + " / " + Converter.getDurationStringLong(player.getDuration());
+ private String getProgressString(PlaybackService ps) {
+ int position = ps.getCurrentPositionSafe();
+ int duration = ps.getDurationSafe();
+ if (position != PlaybackService.INVALID_TIME
+ && duration != PlaybackService.INVALID_TIME) {
+ return Converter.getDurationStringLong(position) + " / "
+ + Converter.getDurationStringLong(duration);
+ } else {
+ return null;
+ }
}
private ServiceConnection mConnection = new ServiceConnection() {
@@ -148,7 +155,7 @@ public class PlayerWidgetService extends Service {
}
};
-
+
private void startViewUpdaterIfNotRunning() {
if (!isUpdating) {
ViewUpdater updateThread = new ViewUpdater(this);
@@ -159,12 +166,12 @@ public class PlayerWidgetService extends Service {
static class ViewUpdater extends Thread {
private static final String THREAD_NAME = "ViewUpdater";
private PlayerWidgetService service;
-
+
public ViewUpdater(PlayerWidgetService service) {
super();
setName(THREAD_NAME);
this.service = service;
-
+
}
@Override
diff --git a/src/de/danoeh/antennapod/util/PlaybackController.java b/src/de/danoeh/antennapod/util/PlaybackController.java
index cb7b0c1bd..bb9c5dd5a 100644
--- a/src/de/danoeh/antennapod/util/PlaybackController.java
+++ b/src/de/danoeh/antennapod/util/PlaybackController.java
@@ -39,13 +39,8 @@ import de.danoeh.antennapod.service.PlayerStatus;
public abstract class PlaybackController {
private static final String TAG = "PlaybackController";
- /**
- * Returned by getPosition() or getDuration() if the playbackService is in
- * an invalid state.
- */
- public static final int INVALID_TIME = -1;
-
static final int DEFAULT_SEEK_DELTA = 30000;
+ public static final int INVALID_TIME = -1;
private Activity activity;
@@ -533,17 +528,18 @@ public abstract class PlaybackController {
public int getPosition() {
if (playbackService != null) {
- return playbackService.getPlayer().getCurrentPosition();
+ return playbackService.getCurrentPositionSafe();
} else {
- return INVALID_TIME;
+ return PlaybackService.INVALID_TIME;
}
}
+
public int getDuration() {
if (playbackService != null) {
- return playbackService.getPlayer().getDuration();
+ return playbackService.getDurationSafe();
} else {
- return INVALID_TIME;
+ return PlaybackService.INVALID_TIME;
}
}