Bug that caused PlaybackService to load without a media object should

now be fixed
This commit is contained in:
daniel oeh 2012-09-10 12:05:16 +02:00
parent c6af7b542a
commit ce7653fb8d
2 changed files with 78 additions and 40 deletions

View File

@ -92,7 +92,7 @@ public class PlaybackService extends Service {
public static final int NOTIFICATION_TYPE_BUFFER_END = 6; public static final int NOTIFICATION_TYPE_BUFFER_END = 6;
/** Is true if service is running. */ /** Is true if service is running. */
public static boolean isRunning = false; public static boolean isRunning;
private static final int NOTIFICATION_ID = 1; private static final int NOTIFICATION_ID = 1;
private NotificationCompat.Builder notificationBuilder; private NotificationCompat.Builder notificationBuilder;
@ -138,6 +138,13 @@ public class PlaybackService extends Service {
} }
} }
@Override
public boolean onUnbind(Intent intent) {
if (AppConfig.DEBUG)
Log.d(TAG, "Received onUnbind event");
return super.onUnbind(intent);
}
/** /**
* Returns an intent which starts an audio- or videoplayer, depending on the * Returns an intent which starts an audio- or videoplayer, depending on the
* type of media that is being played. If the playbackservice is not * type of media that is being played. If the playbackservice is not
@ -179,11 +186,11 @@ public class PlaybackService extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
if (AppConfig.DEBUG)
Log.d(TAG, "Service created.");
isRunning = true; isRunning = true;
pausedBecauseOfTransientAudiofocusLoss = false; pausedBecauseOfTransientAudiofocusLoss = false;
status = PlayerStatus.STOPPED; status = PlayerStatus.STOPPED;
if (AppConfig.DEBUG)
Log.d(TAG, "Service created.");
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
manager = FeedManager.getInstance(); manager = FeedManager.getInstance();
schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOL_SIZE, schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOL_SIZE,
@ -226,11 +233,11 @@ public class PlaybackService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (AppConfig.DEBUG)
Log.d(TAG, "Service is about to be destroyed");
isRunning = false; isRunning = false;
disableSleepTimer(); disableSleepTimer();
unregisterReceiver(headsetDisconnected); unregisterReceiver(headsetDisconnected);
if (AppConfig.DEBUG)
Log.d(TAG, "Service is about to be destroyed");
if (android.os.Build.VERSION.SDK_INT >= 14) { if (android.os.Build.VERSION.SDK_INT >= 14) {
audioManager.unregisterRemoteControlClient(remoteControlClient); audioManager.unregisterRemoteControlClient(remoteControlClient);
} }
@ -243,6 +250,8 @@ public class PlaybackService extends Service {
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
if (AppConfig.DEBUG)
Log.d(TAG, "Received onBind event");
return mBinder; return mBinder;
} }
@ -284,6 +293,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) {
if (AppConfig.DEBUG)
Log.d(TAG, "OnStartCommand called");
int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1); int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1);
if (keycode != -1) { if (keycode != -1) {
if (AppConfig.DEBUG) if (AppConfig.DEBUG)

View File

@ -62,7 +62,8 @@ public abstract class PlaybackController {
public PlaybackController(Activity activity) { public PlaybackController(Activity activity) {
this.activity = activity; this.activity = activity;
schedExecutor = new ScheduledThreadPoolExecutor(2, new ThreadFactory() { schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOLSIZE,
new ThreadFactory() {
@Override @Override
public Thread newThread(Runnable r) { public Thread newThread(Runnable r) {
@ -75,7 +76,8 @@ public abstract class PlaybackController {
@Override @Override
public void rejectedExecution(Runnable r, public void rejectedExecution(Runnable r,
ThreadPoolExecutor executor) { ThreadPoolExecutor executor) {
Log.w(TAG, "Rejected execution of runnable in schedExecutor"); Log.w(TAG,
"Rejected execution of runnable in schedExecutor");
} }
}); });
} }
@ -130,42 +132,59 @@ public abstract class PlaybackController {
* as the arguments of the launch intent. * as the arguments of the launch intent.
*/ */
private void bindToService() { private void bindToService() {
Intent serviceIntent = new Intent(activity, PlaybackService.class); if (AppConfig.DEBUG)
Log.d(TAG, "Trying to connect to service");
Intent serviceIntent = getPlayLastPlayedMediaIntent();
boolean bound = false; boolean bound = false;
if (!PlaybackService.isRunning) { if (!PlaybackService.isRunning) {
if (AppConfig.DEBUG) if (serviceIntent != null) {
Log.d(TAG, "Trying to restore last played media");
SharedPreferences prefs = activity.getApplicationContext()
.getSharedPreferences(PodcastApp.PREF_NAME, 0);
long mediaId = prefs.getLong(PlaybackService.PREF_LAST_PLAYED_ID,
-1);
long feedId = prefs.getLong(
PlaybackService.PREF_LAST_PLAYED_FEED_ID, -1);
if (mediaId != -1 && feedId != -1) {
serviceIntent.putExtra(PlaybackService.EXTRA_FEED_ID, feedId);
serviceIntent.putExtra(PlaybackService.EXTRA_MEDIA_ID, mediaId);
serviceIntent.putExtra(
PlaybackService.EXTRA_START_WHEN_PREPARED, false);
serviceIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM,
prefs.getBoolean(PlaybackService.PREF_LAST_IS_STREAM,
true));
activity.startService(serviceIntent); activity.startService(serviceIntent);
bound = activity.bindService(serviceIntent, mConnection, bound = activity.bindService(serviceIntent, mConnection, 0);
Context.BIND_AUTO_CREATE);
} else { } else {
if (AppConfig.DEBUG)
Log.d(TAG, "No last played media found");
status = PlayerStatus.STOPPED; status = PlayerStatus.STOPPED;
setupGUI(); setupGUI();
handleStatus(); handleStatus();
} }
} else { } else {
if (AppConfig.DEBUG)
Log.d(TAG,
"PlaybackService is running, trying to connect without start command.");
bound = activity.bindService(serviceIntent, mConnection, 0); bound = activity.bindService(serviceIntent, mConnection, 0);
} }
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Result for service binding: " + bound); Log.d(TAG, "Result for service binding: " + bound);
} }
/**
* Returns an intent that starts the PlaybackService and plays the last
* played media or null if no last played media could be found.
*/
private Intent getPlayLastPlayedMediaIntent() {
if (AppConfig.DEBUG)
Log.d(TAG, "Trying to restore last played media");
SharedPreferences prefs = activity.getApplicationContext()
.getSharedPreferences(PodcastApp.PREF_NAME, 0);
long mediaId = prefs.getLong(PlaybackService.PREF_LAST_PLAYED_ID, -1);
long feedId = prefs.getLong(PlaybackService.PREF_LAST_PLAYED_FEED_ID,
-1);
if (mediaId != -1 && feedId != -1) {
Intent serviceIntent = new Intent(activity, PlaybackService.class);
serviceIntent.putExtra(PlaybackService.EXTRA_FEED_ID, feedId);
serviceIntent.putExtra(PlaybackService.EXTRA_MEDIA_ID, mediaId);
serviceIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED,
false);
serviceIntent
.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, prefs
.getBoolean(PlaybackService.PREF_LAST_IS_STREAM,
true));
return serviceIntent;
} else {
if (AppConfig.DEBUG)
Log.d(TAG, "No last played media found");
return null;
}
}
public abstract void setupGUI(); public abstract void setupGUI();
private void setupPositionObserver() { private void setupPositionObserver() {
@ -374,6 +393,14 @@ public abstract class PlaybackController {
if (playbackService != null) { if (playbackService != null) {
status = playbackService.getStatus(); status = playbackService.getStatus();
media = playbackService.getMedia(); media = playbackService.getMedia();
if (media == null) {
Log.w(TAG,
"PlaybackService has no media object. Trying to restore last played media.");
Intent serviceIntent = getPlayLastPlayedMediaIntent();
if (serviceIntent != null) {
activity.startService(serviceIntent);
}
}
onServiceQueried(); onServiceQueried();
setupGUI(); setupGUI();