diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-07-07 16:37:21 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-07-07 16:37:21 +0200 |
commit | 54741495dfa0fa5681c73c6b61a74dc58f0ab724 (patch) | |
tree | d55478f060d446cbb458f44f97d994fbc20e2687 /src/de/podfetcher | |
parent | 8ae87e2a567031f913687794a95882c668659942 (diff) | |
download | AntennaPod-54741495dfa0fa5681c73c6b61a74dc58f0ab724.zip |
Implemented Widget update calls in PlaybackService
Diffstat (limited to 'src/de/podfetcher')
-rw-r--r-- | src/de/podfetcher/service/PlaybackService.java | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/de/podfetcher/service/PlaybackService.java b/src/de/podfetcher/service/PlaybackService.java index de8527fd0..81fd1501e 100644 --- a/src/de/podfetcher/service/PlaybackService.java +++ b/src/de/podfetcher/service/PlaybackService.java @@ -85,6 +85,7 @@ public class PlaybackService extends Service { private FeedManager manager; private PlayerStatus status; private PositionSaver positionSaver; + private WidgetUpdateWorker widgetUpdater; private PlayerStatus statusBeforeSeek; @@ -128,6 +129,8 @@ public class PlaybackService extends Service { audioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiver); audioManager.abandonAudioFocus(audioFocusChangeListener); player.release(); + stopWidgetUpdater(); + updateWidget(); } @Override @@ -394,6 +397,7 @@ public class PlaybackService extends Service { FeedItem nextItem = manager.getFirstQueueItem(); if (nextItem == null) { Log.d(TAG, "No more items in queue"); + stopWidgetUpdater(); setStatus(PlayerStatus.STOPPED); stopForeground(true); } else { @@ -428,6 +432,7 @@ public class PlaybackService extends Service { positionSaver.cancel(true); } saveCurrentPosition(); + stopWidgetUpdater(); setStatus(PlayerStatus.PAUSED); stopForeground(true); } @@ -460,6 +465,7 @@ public class PlaybackService extends Service { player.seekTo((int) media.getPosition()); setStatus(PlayerStatus.PLAYING); setupPositionSaver(); + setupWidgetUpdater(); setupNotification(); pausedBecauseOfTransientAudiofocusLoss = false; } else { @@ -472,7 +478,7 @@ public class PlaybackService extends Service { Log.d(TAG, "Setting status to " + newStatus); status = newStatus; sendBroadcast(new Intent(ACTION_PLAYER_STATUS_CHANGED)); - sendBroadcast(new Intent(PlayerWidget.FORCE_WIDGET_UPDATE)); + updateWidget(); } private void sendNotificationBroadcast(int type, int code) { @@ -526,6 +532,24 @@ public class PlaybackService extends Service { media.setPosition(player.getCurrentPosition()); manager.setFeedMedia(this, media); } + + private void stopWidgetUpdater() { + if (widgetUpdater != null) { + widgetUpdater.cancel(true); + } + } + + private void setupWidgetUpdater() { + if (widgetUpdater == null || widgetUpdater.isCancelled()) { + widgetUpdater = new WidgetUpdateWorker(); + widgetUpdater.execute(); + } + } + + private void updateWidget() { + Log.d(TAG, "Sending widget update request"); + PlaybackService.this.sendBroadcast(new Intent(PlayerWidget.FORCE_WIDGET_UPDATE)); + } public PlayerStatus getStatus() { return status; @@ -564,6 +588,31 @@ public class PlaybackService extends Service { } + /** Notifies the player widget in the specified intervall */ + class WidgetUpdateWorker extends AsyncTask<Void, Void, Void> { + private static final String TAG = "WidgetUpdateWorker"; + private static final int NOTIFICATION_INTERVALL = 2000; + + @Override + protected void onProgressUpdate(Void... values) { + updateWidget(); + } + + @Override + protected Void doInBackground(Void... params) { + while (PlaybackService.isRunning && !isCancelled()) { + publishProgress(); + try { + Thread.sleep(NOTIFICATION_INTERVALL); + } catch (InterruptedException e) { + return null; + } + } + return null; + } + + } + public boolean isPlayingVideo() { return playingVideo; } |