PlaybackController didn't always connect with PlaybackService

This commit is contained in:
daniel oeh 2013-03-20 19:16:58 +01:00
parent 96b70ce9ec
commit 76d4e58b61
2 changed files with 73 additions and 54 deletions

View File

@ -336,6 +336,8 @@ public class PlaybackService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "OnStartCommand called"); Log.d(TAG, "OnStartCommand called");
int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1); int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1);

View File

@ -95,6 +95,15 @@ public abstract class PlaybackController {
* activity's onResume() method. * activity's onResume() method.
*/ */
public void init() { 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) { if (!released) {
bindToService(); bindToService();
} else { } else {
@ -188,9 +197,11 @@ public abstract class PlaybackController {
Log.d(TAG, "Trying to restore last played media"); Log.d(TAG, "Trying to restore last played media");
SharedPreferences prefs = PreferenceManager SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(activity.getApplicationContext()); .getDefaultSharedPreferences(activity.getApplicationContext());
long currentlyPlayingMedia = PlaybackPreferences.getCurrentlyPlayingMedia(); long currentlyPlayingMedia = PlaybackPreferences
.getCurrentlyPlayingMedia();
if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) { if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) {
Playable media = PlayableUtils.createInstanceFromPreferences((int) currentlyPlayingMedia, prefs); Playable media = PlayableUtils.createInstanceFromPreferences(
(int) currentlyPlayingMedia, prefs);
if (media != null) { if (media != null) {
Intent serviceIntent = new Intent(activity, Intent serviceIntent = new Intent(activity,
PlaybackService.class); PlaybackService.class);
@ -200,7 +211,8 @@ public abstract class PlaybackController {
serviceIntent.putExtra( serviceIntent.putExtra(
PlaybackService.EXTRA_PREPARE_IMMEDIATELY, false); PlaybackService.EXTRA_PREPARE_IMMEDIATELY, false);
boolean fileExists = media.localFileAvailable(); boolean fileExists = media.localFileAvailable();
boolean lastIsStream = PlaybackPreferences.getCurrentEpisodeIsStream(); boolean lastIsStream = PlaybackPreferences
.getCurrentEpisodeIsStream();
if (!fileExists && !lastIsStream && media instanceof FeedMedia) { if (!fileExists && !lastIsStream && media instanceof FeedMedia) {
FeedManager.getInstance().notifyMissingFeedMediaFile( FeedManager.getInstance().notifyMissingFeedMediaFile(
activity, (FeedMedia) media); activity, (FeedMedia) media);
@ -248,15 +260,6 @@ public abstract class PlaybackController {
playbackService = ((PlaybackService.LocalBinder) service) playbackService = ((PlaybackService.LocalBinder) service)
.getService(); .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(); queryService();
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Connection to Service established"); Log.d(TAG, "Connection to Service established");
@ -276,12 +279,13 @@ public abstract class PlaybackController {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Received statusUpdate Intent."); Log.d(TAG, "Received statusUpdate Intent.");
if (playbackService != null) { if (isConnectedToPlaybackService()) {
status = playbackService.getStatus(); status = playbackService.getStatus();
handleStatus(); handleStatus();
} else { } else {
Log.w(TAG, Log.w(TAG,
"Couldn't receive status update: playbackService was null"); "Couldn't receive status update: playbackService was null");
bindToService();
} }
} }
}; };
@ -290,46 +294,49 @@ public abstract class PlaybackController {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
int type = intent.getIntExtra( if (isConnectedToPlaybackService()) {
PlaybackService.EXTRA_NOTIFICATION_TYPE, -1); int type = intent.getIntExtra(
int code = intent.getIntExtra( PlaybackService.EXTRA_NOTIFICATION_TYPE, -1);
PlaybackService.EXTRA_NOTIFICATION_CODE, -1); int code = intent.getIntExtra(
if (code != -1 && type != -1) { PlaybackService.EXTRA_NOTIFICATION_CODE, -1);
switch (type) { if (code != -1 && type != -1) {
case PlaybackService.NOTIFICATION_TYPE_ERROR: switch (type) {
handleError(code); case PlaybackService.NOTIFICATION_TYPE_ERROR:
break; handleError(code);
case PlaybackService.NOTIFICATION_TYPE_BUFFER_UPDATE: break;
float progress = ((float) code) / 100; case PlaybackService.NOTIFICATION_TYPE_BUFFER_UPDATE:
onBufferUpdate(progress); float progress = ((float) code) / 100;
break; onBufferUpdate(progress);
case PlaybackService.NOTIFICATION_TYPE_RELOAD: break;
cancelPositionObserver(); case PlaybackService.NOTIFICATION_TYPE_RELOAD:
mediaInfoLoaded = false; cancelPositionObserver();
onReloadNotification(intent.getIntExtra( mediaInfoLoaded = false;
PlaybackService.EXTRA_NOTIFICATION_CODE, -1)); onReloadNotification(intent.getIntExtra(
queryService(); PlaybackService.EXTRA_NOTIFICATION_CODE, -1));
queryService();
break; break;
case PlaybackService.NOTIFICATION_TYPE_SLEEPTIMER_UPDATE: case PlaybackService.NOTIFICATION_TYPE_SLEEPTIMER_UPDATE:
onSleepTimerUpdate(); onSleepTimerUpdate();
break; break;
case PlaybackService.NOTIFICATION_TYPE_BUFFER_START: case PlaybackService.NOTIFICATION_TYPE_BUFFER_START:
onBufferStart(); onBufferStart();
break; break;
case PlaybackService.NOTIFICATION_TYPE_BUFFER_END: case PlaybackService.NOTIFICATION_TYPE_BUFFER_END:
onBufferEnd(); onBufferEnd();
break; break;
case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_END: case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_END:
onPlaybackEnd(); onPlaybackEnd();
break; break;
}
} else {
if (AppConfig.DEBUG)
Log.d(TAG, "Bad arguments. Won't handle intent");
} }
} else { } else {
if (AppConfig.DEBUG) bindToService();
Log.d(TAG, "Bad arguments. Won't handle intent");
} }
} }
}; };
@ -338,10 +345,12 @@ public abstract class PlaybackController {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals( if (isConnectedToPlaybackService()) {
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)) { if (intent.getAction().equals(
release(); PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)) {
onShutdownNotification(); release();
onShutdownNotification();
}
} }
} }
}; };
@ -360,7 +369,7 @@ public abstract class PlaybackController {
public abstract void onSleepTimerUpdate(); public abstract void onSleepTimerUpdate();
public abstract void handleError(int code); public abstract void handleError(int code);
public abstract void onPlaybackEnd(); public abstract void onPlaybackEnd();
/** /**
@ -645,6 +654,14 @@ public abstract class PlaybackController {
return false; return false;
} }
/**
* Returns true if PlaybackController can communicate with the playback
* service.
*/
public boolean isConnectedToPlaybackService() {
return playbackService != null;
}
public void notifyVideoSurfaceAbandoned() { public void notifyVideoSurfaceAbandoned() {
if (playbackService != null) { if (playbackService != null) {
playbackService.notifyVideoSurfaceAbandoned(); playbackService.notifyVideoSurfaceAbandoned();