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