diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-03-20 19:16:58 +0100 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-03-20 19:16:58 +0100 |
commit | 76d4e58b61614e744d1491a107c0b894d81878f3 (patch) | |
tree | 8ad35572597ddcf9b84645eb681b0b997c438334 /src | |
parent | 96b70ce9ecc5f32c819f0c262011e97912f308a2 (diff) | |
download | AntennaPod-76d4e58b61614e744d1491a107c0b894d81878f3.zip |
PlaybackController didn't always connect with PlaybackService
Diffstat (limited to 'src')
-rw-r--r-- | src/de/danoeh/antennapod/service/PlaybackService.java | 2 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/util/playback/PlaybackController.java | 127 |
2 files changed, 74 insertions, 55 deletions
diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java index 8ff115077..e169eaa0f 100644 --- a/src/de/danoeh/antennapod/service/PlaybackService.java +++ b/src/de/danoeh/antennapod/service/PlaybackService.java @@ -336,6 +336,8 @@ public class PlaybackService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { + super.onStartCommand(intent, flags, startId); + if (AppConfig.DEBUG) Log.d(TAG, "OnStartCommand called"); int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1); diff --git a/src/de/danoeh/antennapod/util/playback/PlaybackController.java b/src/de/danoeh/antennapod/util/playback/PlaybackController.java index 331e2bf0c..cebb11cf0 100644 --- a/src/de/danoeh/antennapod/util/playback/PlaybackController.java +++ b/src/de/danoeh/antennapod/util/playback/PlaybackController.java @@ -95,6 +95,15 @@ public abstract class PlaybackController { * activity's onResume() method. */ public void init() { + activity.registerReceiver(statusUpdate, new IntentFilter( + PlaybackService.ACTION_PLAYER_STATUS_CHANGED)); + + activity.registerReceiver(notificationReceiver, new IntentFilter( + PlaybackService.ACTION_PLAYER_NOTIFICATION)); + + activity.registerReceiver(shutdownReceiver, new IntentFilter( + PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)); + if (!released) { bindToService(); } else { @@ -188,9 +197,11 @@ public abstract class PlaybackController { Log.d(TAG, "Trying to restore last played media"); SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(activity.getApplicationContext()); - long currentlyPlayingMedia = PlaybackPreferences.getCurrentlyPlayingMedia(); + long currentlyPlayingMedia = PlaybackPreferences + .getCurrentlyPlayingMedia(); if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) { - Playable media = PlayableUtils.createInstanceFromPreferences((int) currentlyPlayingMedia, prefs); + Playable media = PlayableUtils.createInstanceFromPreferences( + (int) currentlyPlayingMedia, prefs); if (media != null) { Intent serviceIntent = new Intent(activity, PlaybackService.class); @@ -200,7 +211,8 @@ public abstract class PlaybackController { serviceIntent.putExtra( PlaybackService.EXTRA_PREPARE_IMMEDIATELY, false); boolean fileExists = media.localFileAvailable(); - boolean lastIsStream = PlaybackPreferences.getCurrentEpisodeIsStream(); + boolean lastIsStream = PlaybackPreferences + .getCurrentEpisodeIsStream(); if (!fileExists && !lastIsStream && media instanceof FeedMedia) { FeedManager.getInstance().notifyMissingFeedMediaFile( activity, (FeedMedia) media); @@ -248,15 +260,6 @@ public abstract class PlaybackController { playbackService = ((PlaybackService.LocalBinder) service) .getService(); - activity.registerReceiver(statusUpdate, new IntentFilter( - PlaybackService.ACTION_PLAYER_STATUS_CHANGED)); - - activity.registerReceiver(notificationReceiver, new IntentFilter( - PlaybackService.ACTION_PLAYER_NOTIFICATION)); - - activity.registerReceiver(shutdownReceiver, new IntentFilter( - PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)); - queryService(); if (AppConfig.DEBUG) Log.d(TAG, "Connection to Service established"); @@ -276,12 +279,13 @@ public abstract class PlaybackController { public void onReceive(Context context, Intent intent) { if (AppConfig.DEBUG) Log.d(TAG, "Received statusUpdate Intent."); - if (playbackService != null) { + if (isConnectedToPlaybackService()) { status = playbackService.getStatus(); handleStatus(); } else { Log.w(TAG, "Couldn't receive status update: playbackService was null"); + bindToService(); } } }; @@ -290,46 +294,49 @@ public abstract class PlaybackController { @Override public void onReceive(Context context, Intent intent) { - int type = intent.getIntExtra( - PlaybackService.EXTRA_NOTIFICATION_TYPE, -1); - int code = intent.getIntExtra( - PlaybackService.EXTRA_NOTIFICATION_CODE, -1); - if (code != -1 && type != -1) { - switch (type) { - case PlaybackService.NOTIFICATION_TYPE_ERROR: - handleError(code); - break; - case PlaybackService.NOTIFICATION_TYPE_BUFFER_UPDATE: - float progress = ((float) code) / 100; - onBufferUpdate(progress); - break; - case PlaybackService.NOTIFICATION_TYPE_RELOAD: - cancelPositionObserver(); - mediaInfoLoaded = false; - onReloadNotification(intent.getIntExtra( - PlaybackService.EXTRA_NOTIFICATION_CODE, -1)); - queryService(); - - break; - case PlaybackService.NOTIFICATION_TYPE_SLEEPTIMER_UPDATE: - onSleepTimerUpdate(); - break; - case PlaybackService.NOTIFICATION_TYPE_BUFFER_START: - onBufferStart(); - break; - case PlaybackService.NOTIFICATION_TYPE_BUFFER_END: - onBufferEnd(); - break; - case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_END: - onPlaybackEnd(); - break; - } + if (isConnectedToPlaybackService()) { + int type = intent.getIntExtra( + PlaybackService.EXTRA_NOTIFICATION_TYPE, -1); + int code = intent.getIntExtra( + PlaybackService.EXTRA_NOTIFICATION_CODE, -1); + if (code != -1 && type != -1) { + switch (type) { + case PlaybackService.NOTIFICATION_TYPE_ERROR: + handleError(code); + break; + case PlaybackService.NOTIFICATION_TYPE_BUFFER_UPDATE: + float progress = ((float) code) / 100; + onBufferUpdate(progress); + break; + case PlaybackService.NOTIFICATION_TYPE_RELOAD: + cancelPositionObserver(); + mediaInfoLoaded = false; + onReloadNotification(intent.getIntExtra( + PlaybackService.EXTRA_NOTIFICATION_CODE, -1)); + queryService(); + break; + case PlaybackService.NOTIFICATION_TYPE_SLEEPTIMER_UPDATE: + onSleepTimerUpdate(); + break; + case PlaybackService.NOTIFICATION_TYPE_BUFFER_START: + onBufferStart(); + break; + case PlaybackService.NOTIFICATION_TYPE_BUFFER_END: + onBufferEnd(); + break; + case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_END: + onPlaybackEnd(); + break; + } + + } else { + if (AppConfig.DEBUG) + Log.d(TAG, "Bad arguments. Won't handle intent"); + } } else { - if (AppConfig.DEBUG) - Log.d(TAG, "Bad arguments. Won't handle intent"); + bindToService(); } - } }; @@ -338,10 +345,12 @@ public abstract class PlaybackController { @Override public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals( - PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)) { - release(); - onShutdownNotification(); + if (isConnectedToPlaybackService()) { + if (intent.getAction().equals( + PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)) { + release(); + onShutdownNotification(); + } } } }; @@ -360,7 +369,7 @@ public abstract class PlaybackController { public abstract void onSleepTimerUpdate(); public abstract void handleError(int code); - + public abstract void onPlaybackEnd(); /** @@ -645,6 +654,14 @@ public abstract class PlaybackController { return false; } + /** + * Returns true if PlaybackController can communicate with the playback + * service. + */ + public boolean isConnectedToPlaybackService() { + return playbackService != null; + } + public void notifyVideoSurfaceAbandoned() { if (playbackService != null) { playbackService.notifyVideoSurfaceAbandoned(); |