From ce7653fb8de775654863991e07b114b50527642f Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Mon, 10 Sep 2012 12:05:16 +0200 Subject: Bug that caused PlaybackService to load without a media object should now be fixed --- .../danoeh/antennapod/service/PlaybackService.java | 21 +++-- .../danoeh/antennapod/util/PlaybackController.java | 97 ++++++++++++++-------- 2 files changed, 78 insertions(+), 40 deletions(-) (limited to 'src/de/danoeh') diff --git a/src/de/danoeh/antennapod/service/PlaybackService.java b/src/de/danoeh/antennapod/service/PlaybackService.java index cefeb51b5..889a31465 100644 --- a/src/de/danoeh/antennapod/service/PlaybackService.java +++ b/src/de/danoeh/antennapod/service/PlaybackService.java @@ -92,7 +92,7 @@ public class PlaybackService extends Service { public static final int NOTIFICATION_TYPE_BUFFER_END = 6; /** Is true if service is running. */ - public static boolean isRunning = false; + public static boolean isRunning; private static final int NOTIFICATION_ID = 1; 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 * type of media that is being played. If the playbackservice is not @@ -179,11 +186,11 @@ public class PlaybackService extends Service { @Override public void onCreate() { super.onCreate(); + if (AppConfig.DEBUG) + Log.d(TAG, "Service created."); isRunning = true; pausedBecauseOfTransientAudiofocusLoss = false; status = PlayerStatus.STOPPED; - if (AppConfig.DEBUG) - Log.d(TAG, "Service created."); audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); manager = FeedManager.getInstance(); schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOL_SIZE, @@ -226,11 +233,11 @@ public class PlaybackService extends Service { @Override public void onDestroy() { super.onDestroy(); + if (AppConfig.DEBUG) + Log.d(TAG, "Service is about to be destroyed"); isRunning = false; disableSleepTimer(); unregisterReceiver(headsetDisconnected); - if (AppConfig.DEBUG) - Log.d(TAG, "Service is about to be destroyed"); if (android.os.Build.VERSION.SDK_INT >= 14) { audioManager.unregisterRemoteControlClient(remoteControlClient); } @@ -243,6 +250,8 @@ public class PlaybackService extends Service { @Override public IBinder onBind(Intent intent) { + if (AppConfig.DEBUG) + Log.d(TAG, "Received onBind event"); return mBinder; } @@ -284,6 +293,8 @@ public class PlaybackService extends Service { @Override 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); if (keycode != -1) { if (AppConfig.DEBUG) diff --git a/src/de/danoeh/antennapod/util/PlaybackController.java b/src/de/danoeh/antennapod/util/PlaybackController.java index 3095c7710..34ebb0eec 100644 --- a/src/de/danoeh/antennapod/util/PlaybackController.java +++ b/src/de/danoeh/antennapod/util/PlaybackController.java @@ -62,22 +62,24 @@ public abstract class PlaybackController { public PlaybackController(Activity activity) { this.activity = activity; - schedExecutor = new ScheduledThreadPoolExecutor(2, new ThreadFactory() { + schedExecutor = new ScheduledThreadPoolExecutor(SCHED_EX_POOLSIZE, + new ThreadFactory() { - @Override - public Thread newThread(Runnable r) { - Thread t = new Thread(r); - t.setPriority(Thread.MIN_PRIORITY); - return t; - } - }, new RejectedExecutionHandler() { + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r); + t.setPriority(Thread.MIN_PRIORITY); + return t; + } + }, new RejectedExecutionHandler() { - @Override - public void rejectedExecution(Runnable r, - ThreadPoolExecutor executor) { - Log.w(TAG, "Rejected execution of runnable in schedExecutor"); - } - }); + @Override + public void rejectedExecution(Runnable r, + ThreadPoolExecutor executor) { + Log.w(TAG, + "Rejected execution of runnable in schedExecutor"); + } + }); } /** @@ -130,42 +132,59 @@ public abstract class PlaybackController { * as the arguments of the launch intent. */ 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; if (!PlaybackService.isRunning) { - 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) { - 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)); + if (serviceIntent != null) { activity.startService(serviceIntent); - bound = activity.bindService(serviceIntent, mConnection, - Context.BIND_AUTO_CREATE); + bound = activity.bindService(serviceIntent, mConnection, 0); } else { - if (AppConfig.DEBUG) - Log.d(TAG, "No last played media found"); status = PlayerStatus.STOPPED; setupGUI(); handleStatus(); } } else { + if (AppConfig.DEBUG) + Log.d(TAG, + "PlaybackService is running, trying to connect without start command."); bound = activity.bindService(serviceIntent, mConnection, 0); } if (AppConfig.DEBUG) 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(); private void setupPositionObserver() { @@ -374,6 +393,14 @@ public abstract class PlaybackController { if (playbackService != null) { status = playbackService.getStatus(); 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(); setupGUI(); -- cgit v1.2.3