From 7f11cd351a2cfe776bad71ae29070a26af1e827f Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Wed, 23 Mar 2016 20:28:22 +0100 Subject: Refactor --- .../core/util/playback/PlaybackController.java | 139 ++++++++++----------- 1 file changed, 64 insertions(+), 75 deletions(-) (limited to 'core/src/main/java/de/danoeh') diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java index 016ff9a85..f05b1f73b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java @@ -50,19 +50,19 @@ public abstract class PlaybackController { private static final String TAG = "PlaybackController"; - public static final int INVALID_TIME = -1; + private static final int INVALID_TIME = -1; private final Activity activity; private PlaybackService playbackService; - protected Playable media; + private Playable media; private PlayerStatus status; - private ScheduledThreadPoolExecutor schedExecutor; + private final ScheduledThreadPoolExecutor schedExecutor; private static final int SCHED_EX_POOLSIZE = 1; - protected MediaPositionObserver positionObserver; - protected ScheduledFuture positionObserverFuture; + private MediaPositionObserver positionObserver; + private ScheduledFuture positionObserverFuture; private boolean mediaInfoLoaded = false; private boolean released = false; @@ -71,7 +71,7 @@ public abstract class PlaybackController { * True if controller should reinit playback service if 'pause' button is * pressed. */ - private boolean reinitOnPause; + private final boolean reinitOnPause; public PlaybackController(@NonNull Activity activity, boolean reinitOnPause) { @@ -86,8 +86,7 @@ public abstract class PlaybackController { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { - Log.w(TAG, - "Rejected execution of runnable in schedExecutor"); + Log.w(TAG, "Rejected execution of runnable in schedExecutor"); } } ); @@ -110,8 +109,7 @@ public abstract class PlaybackController { if (!released) { bindToService(); } else { - throw new IllegalStateException( - "Can't call init() after release() has been called"); + throw new IllegalStateException("Can't call init() after release() has been called"); } checkMediaInfoLoaded(); } @@ -203,27 +201,21 @@ public abstract class PlaybackController { */ private Intent getPlayLastPlayedMediaIntent() { Log.d(TAG, "Trying to restore last played media"); - SharedPreferences prefs = PreferenceManager - .getDefaultSharedPreferences(activity.getApplicationContext()); - long currentlyPlayingMedia = PlaybackPreferences - .getCurrentlyPlayingMedia(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( + activity.getApplicationContext()); + long currentlyPlayingMedia = PlaybackPreferences.getCurrentlyPlayingMedia(); if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) { Playable media = PlayableUtils.createInstanceFromPreferences(activity, (int) currentlyPlayingMedia, prefs); if (media != null) { - Intent serviceIntent = new Intent(activity, - PlaybackService.class); + Intent serviceIntent = new Intent(activity, PlaybackService.class); serviceIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media); - serviceIntent.putExtra( - PlaybackService.EXTRA_START_WHEN_PREPARED, false); - serviceIntent.putExtra( - PlaybackService.EXTRA_PREPARE_IMMEDIATELY, false); + serviceIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED, false); + 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) { - DBTasks.notifyMissingFeedMediaFile( - activity, (FeedMedia) media); + DBTasks.notifyMissingFeedMediaFile(activity, (FeedMedia) media); } serviceIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, lastIsStream || !fileExists); @@ -257,7 +249,7 @@ public abstract class PlaybackController { } } - private ServiceConnection mConnection = new ServiceConnection() { + private final ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { playbackService = ((PlaybackService.LocalBinder) service) .getService(); @@ -265,7 +257,8 @@ public abstract class PlaybackController { queryService(); Log.d(TAG, "Connection to Service established"); } else { - Log.i(TAG, "Connection to playback service has been established, but controller has already been released"); + Log.i(TAG, "Connection to playback service has been established, " + + "but controller has already been released"); } } @@ -276,7 +269,7 @@ public abstract class PlaybackController { } }; - protected BroadcastReceiver statusUpdate = new BroadcastReceiver() { + private final BroadcastReceiver statusUpdate = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "Received statusUpdate Intent."); @@ -286,66 +279,62 @@ public abstract class PlaybackController { media = info.playable; handleStatus(); } else { - Log.w(TAG, - "Couldn't receive status update: playbackService was null"); + Log.w(TAG, "Couldn't receive status update: playbackService was null"); bindToService(); } } }; - protected BroadcastReceiver notificationReceiver = new BroadcastReceiver() { + private final BroadcastReceiver notificationReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - 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; - queryService(); - onReloadNotification(intent.getIntExtra( - PlaybackService.EXTRA_NOTIFICATION_CODE, -1)); - 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; - case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE: - onPlaybackSpeedChange(); - break; - } - - } else { - Log.d(TAG, "Bad arguments. Won't handle intent"); - } - } else { + if (!isConnectedToPlaybackService()) { bindToService(); + return; + } + int type = intent.getIntExtra(PlaybackService.EXTRA_NOTIFICATION_TYPE, -1); + int code = intent.getIntExtra(PlaybackService.EXTRA_NOTIFICATION_CODE, -1); + if(code == -1 || type == -1) { + Log.d(TAG, "Bad arguments. Won't handle intent"); + return; + } + 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; + queryService(); + onReloadNotification(intent.getIntExtra( + PlaybackService.EXTRA_NOTIFICATION_CODE, -1)); + 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; + case PlaybackService.NOTIFICATION_TYPE_PLAYBACK_SPEED_CHANGE: + onPlaybackSpeedChange(); + break; } } }; - private BroadcastReceiver shutdownReceiver = new BroadcastReceiver() { + private final BroadcastReceiver shutdownReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -499,7 +488,7 @@ public abstract class PlaybackController { * Called when connection to playback service has been established or * information has to be refreshed */ - void queryService() { + private void queryService() { Log.d(TAG, "Querying service info"); if (playbackService != null) { status = playbackService.getStatus(); @@ -729,7 +718,7 @@ public abstract class PlaybackController { * Returns true if PlaybackController can communicate with the playback * service. */ - public boolean isConnectedToPlaybackService() { + private boolean isConnectedToPlaybackService() { return playbackService != null; } -- cgit v1.2.3 From 6de985c99397edd3007c8d81847f5443f4c39687 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Wed, 23 Mar 2016 20:39:28 +0100 Subject: Replace AsyncTask with Observable --- .../core/util/playback/PlaybackController.java | 61 ++++++++++++---------- 1 file changed, 34 insertions(+), 27 deletions(-) (limited to 'core/src/main/java/de/danoeh') diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java index f05b1f73b..0ad286093 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java @@ -10,7 +10,6 @@ import android.content.ServiceConnection; import android.content.SharedPreferences; import android.content.res.TypedArray; import android.media.MediaPlayer; -import android.os.AsyncTask; import android.os.Build; import android.os.IBinder; import android.preference.PreferenceManager; @@ -41,6 +40,10 @@ import de.danoeh.antennapod.core.service.playback.PlayerStatus; import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.playback.Playable.PlayableUtils; +import rx.Observable; +import rx.Subscription; +import rx.android.schedulers.AndroidSchedulers; +import rx.schedulers.Schedulers; /** * Communicates with the playback service. GUI classes should use this class to @@ -67,6 +70,8 @@ public abstract class PlaybackController { private boolean mediaInfoLoaded = false; private boolean released = false; + private Subscription serviceBinder; + /** * True if controller should reinit playback service if 'pause' button is * pressed. @@ -133,6 +138,9 @@ public abstract class PlaybackController { // ignore } + if(serviceBinder != null) { + serviceBinder.unsubscribe(); + } try { activity.unbindService(mConnection); } catch (IllegalArgumentException e) { @@ -165,34 +173,33 @@ public abstract class PlaybackController { */ private void bindToService() { Log.d(TAG, "Trying to connect to service"); - AsyncTask intentLoader = new AsyncTask() { - @Override - protected Intent doInBackground(Void... voids) { - return getPlayLastPlayedMediaIntent(); - } - - @Override - protected void onPostExecute(Intent serviceIntent) { - boolean bound = false; - if (!PlaybackService.started) { - if (serviceIntent != null) { - Log.d(TAG, "Calling start service"); - activity.startService(serviceIntent); - bound = activity.bindService(serviceIntent, mConnection, 0); + if(serviceBinder != null) { + serviceBinder.unsubscribe(); + } + serviceBinder = Observable.fromCallable(() -> getPlayLastPlayedMediaIntent()) + .subscribeOn(Schedulers.newThread()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(intent -> { + boolean bound = false; + if (!PlaybackService.started) { + if (intent != null) { + Log.d(TAG, "Calling start service"); + activity.startService(intent); + bound = activity.bindService(intent, mConnection, 0); + } else { + status = PlayerStatus.STOPPED; + setupGUI(); + handleStatus(); + } } else { - status = PlayerStatus.STOPPED; - setupGUI(); - handleStatus(); + Log.d(TAG, "PlaybackService is running, trying to connect without start command."); + bound = activity.bindService(new Intent(activity, PlaybackService.class), + mConnection, 0); } - } else { - Log.d(TAG, "PlaybackService is running, trying to connect without start command."); - bound = activity.bindService(new Intent(activity, - PlaybackService.class), mConnection, 0); - } - Log.d(TAG, "Result for service binding: " + bound); - } - }; - intentLoader.execute(); + Log.d(TAG, "Result for service binding: " + bound); + }, error -> { + Log.e(TAG, Log.getStackTraceString(error)); + }); } /** -- cgit v1.2.3