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
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);

View File

@ -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,6 +294,7 @@ public abstract class PlaybackController {
@Override
public void onReceive(Context context, Intent intent) {
if (isConnectedToPlaybackService()) {
int type = intent.getIntExtra(
PlaybackService.EXTRA_NOTIFICATION_TYPE, -1);
int code = intent.getIntExtra(
@ -329,7 +334,9 @@ public abstract class PlaybackController {
if (AppConfig.DEBUG)
Log.d(TAG, "Bad arguments. Won't handle intent");
}
} else {
bindToService();
}
}
};
@ -338,12 +345,14 @@ public abstract class PlaybackController {
@Override
public void onReceive(Context context, Intent intent) {
if (isConnectedToPlaybackService()) {
if (intent.getAction().equals(
PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE)) {
release();
onShutdownNotification();
}
}
}
};
public abstract void onShutdownNotification();
@ -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();