This commit is contained in:
Martin Fietz 2016-03-23 20:28:22 +01:00
parent 8a0f6cb529
commit 7f11cd351a
1 changed files with 64 additions and 75 deletions

View File

@ -50,19 +50,19 @@ public abstract class PlaybackController {
private static final String TAG = "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 final Activity activity;
private PlaybackService playbackService; private PlaybackService playbackService;
protected Playable media; private Playable media;
private PlayerStatus status; private PlayerStatus status;
private ScheduledThreadPoolExecutor schedExecutor; private final ScheduledThreadPoolExecutor schedExecutor;
private static final int SCHED_EX_POOLSIZE = 1; private static final int SCHED_EX_POOLSIZE = 1;
protected MediaPositionObserver positionObserver; private MediaPositionObserver positionObserver;
protected ScheduledFuture positionObserverFuture; private ScheduledFuture positionObserverFuture;
private boolean mediaInfoLoaded = false; private boolean mediaInfoLoaded = false;
private boolean released = false; private boolean released = false;
@ -71,7 +71,7 @@ public abstract class PlaybackController {
* True if controller should reinit playback service if 'pause' button is * True if controller should reinit playback service if 'pause' button is
* pressed. * pressed.
*/ */
private boolean reinitOnPause; private final boolean reinitOnPause;
public PlaybackController(@NonNull Activity activity, boolean reinitOnPause) { public PlaybackController(@NonNull Activity activity, boolean reinitOnPause) {
@ -86,8 +86,7 @@ public abstract class PlaybackController {
@Override @Override
public void rejectedExecution(Runnable r, public void rejectedExecution(Runnable r,
ThreadPoolExecutor executor) { ThreadPoolExecutor executor) {
Log.w(TAG, Log.w(TAG, "Rejected execution of runnable in schedExecutor");
"Rejected execution of runnable in schedExecutor");
} }
} }
); );
@ -110,8 +109,7 @@ public abstract class PlaybackController {
if (!released) { if (!released) {
bindToService(); bindToService();
} else { } else {
throw new IllegalStateException( throw new IllegalStateException("Can't call init() after release() has been called");
"Can't call init() after release() has been called");
} }
checkMediaInfoLoaded(); checkMediaInfoLoaded();
} }
@ -203,27 +201,21 @@ public abstract class PlaybackController {
*/ */
private Intent getPlayLastPlayedMediaIntent() { private Intent getPlayLastPlayedMediaIntent() {
Log.d(TAG, "Trying to restore last played media"); Log.d(TAG, "Trying to restore last played media");
SharedPreferences prefs = PreferenceManager SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
.getDefaultSharedPreferences(activity.getApplicationContext()); activity.getApplicationContext());
long currentlyPlayingMedia = PlaybackPreferences long currentlyPlayingMedia = PlaybackPreferences.getCurrentlyPlayingMedia();
.getCurrentlyPlayingMedia();
if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) { if (currentlyPlayingMedia != PlaybackPreferences.NO_MEDIA_PLAYING) {
Playable media = PlayableUtils.createInstanceFromPreferences(activity, Playable media = PlayableUtils.createInstanceFromPreferences(activity,
(int) currentlyPlayingMedia, prefs); (int) currentlyPlayingMedia, prefs);
if (media != null) { if (media != null) {
Intent serviceIntent = new Intent(activity, Intent serviceIntent = new Intent(activity, PlaybackService.class);
PlaybackService.class);
serviceIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media); serviceIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);
serviceIntent.putExtra( serviceIntent.putExtra(PlaybackService.EXTRA_START_WHEN_PREPARED, false);
PlaybackService.EXTRA_START_WHEN_PREPARED, false); serviceIntent.putExtra(PlaybackService.EXTRA_PREPARE_IMMEDIATELY, false);
serviceIntent.putExtra(
PlaybackService.EXTRA_PREPARE_IMMEDIATELY, false);
boolean fileExists = media.localFileAvailable(); boolean fileExists = media.localFileAvailable();
boolean lastIsStream = PlaybackPreferences boolean lastIsStream = PlaybackPreferences.getCurrentEpisodeIsStream();
.getCurrentEpisodeIsStream();
if (!fileExists && !lastIsStream && media instanceof FeedMedia) { if (!fileExists && !lastIsStream && media instanceof FeedMedia) {
DBTasks.notifyMissingFeedMediaFile( DBTasks.notifyMissingFeedMediaFile(activity, (FeedMedia) media);
activity, (FeedMedia) media);
} }
serviceIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM, serviceIntent.putExtra(PlaybackService.EXTRA_SHOULD_STREAM,
lastIsStream || !fileExists); 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) { public void onServiceConnected(ComponentName className, IBinder service) {
playbackService = ((PlaybackService.LocalBinder) service) playbackService = ((PlaybackService.LocalBinder) service)
.getService(); .getService();
@ -265,7 +257,8 @@ public abstract class PlaybackController {
queryService(); queryService();
Log.d(TAG, "Connection to Service established"); Log.d(TAG, "Connection to Service established");
} else { } 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 @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received statusUpdate Intent."); Log.d(TAG, "Received statusUpdate Intent.");
@ -286,23 +279,26 @@ public abstract class PlaybackController {
media = info.playable; media = info.playable;
handleStatus(); handleStatus();
} else { } else {
Log.w(TAG, Log.w(TAG, "Couldn't receive status update: playbackService was null");
"Couldn't receive status update: playbackService was null");
bindToService(); bindToService();
} }
} }
}; };
protected BroadcastReceiver notificationReceiver = new BroadcastReceiver() { private final BroadcastReceiver notificationReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (isConnectedToPlaybackService()) { if (!isConnectedToPlaybackService()) {
int type = intent.getIntExtra( bindToService();
PlaybackService.EXTRA_NOTIFICATION_TYPE, -1); return;
int code = intent.getIntExtra( }
PlaybackService.EXTRA_NOTIFICATION_CODE, -1); int type = intent.getIntExtra(PlaybackService.EXTRA_NOTIFICATION_TYPE, -1);
if (code != -1 && 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) { switch (type) {
case PlaybackService.NOTIFICATION_TYPE_ERROR: case PlaybackService.NOTIFICATION_TYPE_ERROR:
handleError(code); handleError(code);
@ -334,18 +330,11 @@ public abstract class PlaybackController {
onPlaybackSpeedChange(); onPlaybackSpeedChange();
break; break;
} }
} else {
Log.d(TAG, "Bad arguments. Won't handle intent");
}
} else {
bindToService();
}
} }
}; };
private BroadcastReceiver shutdownReceiver = new BroadcastReceiver() { private final BroadcastReceiver shutdownReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { 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 * Called when connection to playback service has been established or
* information has to be refreshed * information has to be refreshed
*/ */
void queryService() { private void queryService() {
Log.d(TAG, "Querying service info"); Log.d(TAG, "Querying service info");
if (playbackService != null) { if (playbackService != null) {
status = playbackService.getStatus(); status = playbackService.getStatus();
@ -729,7 +718,7 @@ public abstract class PlaybackController {
* Returns true if PlaybackController can communicate with the playback * Returns true if PlaybackController can communicate with the playback
* service. * service.
*/ */
public boolean isConnectedToPlaybackService() { private boolean isConnectedToPlaybackService() {
return playbackService != null; return playbackService != null;
} }