From 7f11cd351a2cfe776bad71ae29070a26af1e827f Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Wed, 23 Mar 2016 20:28:22 +0100 Subject: [PATCH] Refactor --- .../util/playback/PlaybackController.java | 139 ++++++++---------- 1 file changed, 64 insertions(+), 75 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..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; }