summaryrefslogtreecommitdiff
path: root/core/src/main/java/de/danoeh
diff options
context:
space:
mode:
authorTom Hennen <TomHennen@users.noreply.github.com>2016-03-24 12:00:37 -0400
committerTom Hennen <TomHennen@users.noreply.github.com>2016-03-24 12:00:37 -0400
commit1833d7b319c8c25578685e4a0dca579252f6856f (patch)
tree38302e6d0a6a11f3ab0493e94fdfc5c61e2e6626 /core/src/main/java/de/danoeh
parent07c2818cc5d67f27623673b94a4759558d42d8ce (diff)
parent6de985c99397edd3007c8d81847f5443f4c39687 (diff)
downloadAntennaPod-1833d7b319c8c25578685e4a0dca579252f6856f.zip
Merge pull request #1814 from mfietz/issue/1812-bind-service
PlaybackController.bindService() with Observable
Diffstat (limited to 'core/src/main/java/de/danoeh')
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/playback/PlaybackController.java200
1 files changed, 98 insertions, 102 deletions
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..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
@@ -50,28 +53,30 @@ 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;
+ private Subscription serviceBinder;
+
/**
* 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 +91,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 +114,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();
}
@@ -135,6 +138,9 @@ public abstract class PlaybackController {
// ignore
}
+ if(serviceBinder != null) {
+ serviceBinder.unsubscribe();
+ }
try {
activity.unbindService(mConnection);
} catch (IllegalArgumentException e) {
@@ -167,34 +173,33 @@ public abstract class PlaybackController {
*/
private void bindToService() {
Log.d(TAG, "Trying to connect to service");
- AsyncTask<Void, Void, Intent> intentLoader = new AsyncTask<Void, Void, Intent>() {
- @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));
+ });
}
/**
@@ -203,27 +208,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 +256,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 +264,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 +276,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 +286,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 +495,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 +725,7 @@ public abstract class PlaybackController {
* Returns true if PlaybackController can communicate with the playback
* service.
*/
- public boolean isConnectedToPlaybackService() {
+ private boolean isConnectedToPlaybackService() {
return playbackService != null;
}