diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java index 116709587..de65ca406 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -613,11 +613,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements Log.d(TAG, "onResume()"); StorageUtils.checkStorageAvailability(this); if (controller != null) { - if (PlaybackService.isRunning) { - controller.init(); - } else { - controller.resumeServiceNotRunning(); - } + controller.init(); } } diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/DefaultActionButtonCallback.java b/app/src/main/java/de/danoeh/antennapod/adapter/DefaultActionButtonCallback.java index 31b0ce333..3f8f871c2 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/DefaultActionButtonCallback.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/DefaultActionButtonCallback.java @@ -81,15 +81,11 @@ public class DefaultActionButtonCallback implements ActionButtonCallback { } } else { // media is downloaded if (item.hasMedia() && item.getMedia().isCurrentlyPlaying()) { - if (!PlaybackService.isRunning) { - PlaybackService.startService(context, media, true, false); - } + PlaybackService.startIfNotRunning(context, media, true, false); context.sendBroadcast(new Intent(PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE)); } else if (item.hasMedia() && item.getMedia().isCurrentlyPaused()) { - if (!PlaybackService.isRunning) { - PlaybackService.startService(context, media, true, false); - } + PlaybackService.startIfNotRunning(context, media, true, false); context.sendBroadcast(new Intent(PlaybackService.ACTION_RESUME_PLAY_CURRENT_EPISODE)); } else { diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java index e75f312d3..27df29f9f 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ExternalPlayerFragment.java @@ -127,11 +127,7 @@ public class ExternalPlayerFragment extends Fragment { @Override public void onResume() { super.onResume(); - if (PlaybackService.isRunning) { - controller.init(); - } else { - controller.resumeServiceNotRunning(); - } + controller.init(); onPositionObserverUpdate(); } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java index 7078eeb63..5088d71a6 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/ItemFragment.java @@ -244,9 +244,7 @@ public class ItemFragment extends Fragment implements OnSwipeGesture { if (item.hasMedia()) { FeedMedia media = item.getMedia(); if (!media.isDownloaded()) { - if (!PlaybackService.isRunning) { - PlaybackService.startService(getActivity(), media, true, false); - } + PlaybackService.startIfNotRunning(getActivity(), media, true, false); DBTasks.playMedia(getActivity(), media, true, true, true); ((MainActivity) getActivity()).dismissChildFragment(); } else { diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java index 17362c721..d2ef5d2a6 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java @@ -111,7 +111,6 @@ public class UserPreferences { // JobScheduler private static final int JOB_ID_FEED_UPDATE = 42; - private static final float JOB_SCHEDULER_TIME_VARIATION = 1.5f; // Mediaplayer private static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed"; @@ -813,13 +812,19 @@ public class UserPreferences { return; } - if (Build.VERSION.SDK_INT >= 23) { - JobInfo.Builder builder = getFeedUpdateJobBuilder(); - builder.setOverrideDeadline((long) (triggerAtMillis * JOB_SCHEDULER_TIME_VARIATION)); + if (Build.VERSION.SDK_INT >= 24) { JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); if (jobScheduler != null) { - jobScheduler.schedule(builder.build()); - Log.d(TAG, "JobScheduler was set for " + triggerAtMillis); + JobInfo oldJob = jobScheduler.getPendingJob(JOB_ID_FEED_UPDATE); + if (oldJob == null || oldJob.getIntervalMillis() != intervalMillis) { + JobInfo.Builder builder = getFeedUpdateJobBuilder(); + builder.setPeriodic(intervalMillis); + jobScheduler.cancel(JOB_ID_FEED_UPDATE); + jobScheduler.schedule(builder.build()); + Log.d(TAG, "JobScheduler was set at interval " + intervalMillis); + } else { + Log.d(TAG, "JobScheduler was already set at interval " + intervalMillis + ", ignoring."); + } } return; } @@ -848,12 +853,13 @@ public class UserPreferences { alarm.add(Calendar.DATE, 1); } - if (Build.VERSION.SDK_INT >= 23) { + if (Build.VERSION.SDK_INT >= 24) { JobInfo.Builder builder = getFeedUpdateJobBuilder(); long triggerAtMillis = alarm.getTimeInMillis() - now.getTimeInMillis(); - builder.setOverrideDeadline((long) (triggerAtMillis * JOB_SCHEDULER_TIME_VARIATION)); + builder.setMinimumLatency(triggerAtMillis); JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); if (jobScheduler != null) { + jobScheduler.cancel(JOB_ID_FEED_UPDATE); jobScheduler.schedule(builder.build()); Log.d(TAG, "JobScheduler was set for " + triggerAtMillis); } @@ -876,7 +882,6 @@ public class UserPreferences { private static JobInfo.Builder getFeedUpdateJobBuilder() { ComponentName serviceComponent = new ComponentName(context, FeedUpdateJobService.class); JobInfo.Builder builder = new JobInfo.Builder(JOB_ID_FEED_UPDATE, serviceComponent); - builder.setMinimumLatency(15 * 60 * 1000); builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); return builder; } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 0ec135923..5801ee3d5 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -795,6 +795,12 @@ public class PlaybackService extends MediaBrowserServiceCompat { } }; + public static void startIfNotRunning(final Context context, final Playable media, boolean startWhenPrepared, boolean shouldStream) { + if (!isRunning) { + startService(context, media, startWhenPrepared, shouldStream); + } + } + public static void startService(final Context context, final Playable media, boolean startWhenPrepared, boolean shouldStream) { Intent launchIntent = new Intent(context, PlaybackService.class); launchIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media); 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 36253d075..0b874be1f 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 @@ -96,7 +96,15 @@ public abstract class PlaybackController { /** * Creates a new connection to the playbackService. */ - public synchronized void init() { + public void init() { + if (PlaybackService.isRunning) { + initServiceRunning(); + } else { + initServiceNotRunning(); + } + } + + private synchronized void initServiceRunning() { if (initialized) { return; } @@ -779,7 +787,7 @@ public abstract class PlaybackController { } } - public void resumeServiceNotRunning() { + private void initServiceNotRunning() { if (getMedia() == null) { return; }