Review changes

This commit is contained in:
ByteHamster 2018-05-04 14:36:32 +02:00
parent ffdfefc35d
commit f66e19845c
7 changed files with 35 additions and 30 deletions

View File

@ -613,11 +613,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
Log.d(TAG, "onResume()"); Log.d(TAG, "onResume()");
StorageUtils.checkStorageAvailability(this); StorageUtils.checkStorageAvailability(this);
if (controller != null) { if (controller != null) {
if (PlaybackService.isRunning) { controller.init();
controller.init();
} else {
controller.resumeServiceNotRunning();
}
} }
} }

View File

@ -81,15 +81,11 @@ public class DefaultActionButtonCallback implements ActionButtonCallback {
} }
} else { // media is downloaded } else { // media is downloaded
if (item.hasMedia() && item.getMedia().isCurrentlyPlaying()) { if (item.hasMedia() && item.getMedia().isCurrentlyPlaying()) {
if (!PlaybackService.isRunning) { PlaybackService.startIfNotRunning(context, media, true, false);
PlaybackService.startService(context, media, true, false);
}
context.sendBroadcast(new Intent(PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE)); context.sendBroadcast(new Intent(PlaybackService.ACTION_PAUSE_PLAY_CURRENT_EPISODE));
} }
else if (item.hasMedia() && item.getMedia().isCurrentlyPaused()) { else if (item.hasMedia() && item.getMedia().isCurrentlyPaused()) {
if (!PlaybackService.isRunning) { PlaybackService.startIfNotRunning(context, media, true, false);
PlaybackService.startService(context, media, true, false);
}
context.sendBroadcast(new Intent(PlaybackService.ACTION_RESUME_PLAY_CURRENT_EPISODE)); context.sendBroadcast(new Intent(PlaybackService.ACTION_RESUME_PLAY_CURRENT_EPISODE));
} }
else { else {

View File

@ -127,11 +127,7 @@ public class ExternalPlayerFragment extends Fragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
if (PlaybackService.isRunning) { controller.init();
controller.init();
} else {
controller.resumeServiceNotRunning();
}
onPositionObserverUpdate(); onPositionObserverUpdate();
} }

View File

@ -244,9 +244,7 @@ public class ItemFragment extends Fragment implements OnSwipeGesture {
if (item.hasMedia()) { if (item.hasMedia()) {
FeedMedia media = item.getMedia(); FeedMedia media = item.getMedia();
if (!media.isDownloaded()) { if (!media.isDownloaded()) {
if (!PlaybackService.isRunning) { PlaybackService.startIfNotRunning(getActivity(), media, true, false);
PlaybackService.startService(getActivity(), media, true, false);
}
DBTasks.playMedia(getActivity(), media, true, true, true); DBTasks.playMedia(getActivity(), media, true, true, true);
((MainActivity) getActivity()).dismissChildFragment(); ((MainActivity) getActivity()).dismissChildFragment();
} else { } else {

View File

@ -111,7 +111,6 @@ public class UserPreferences {
// JobScheduler // JobScheduler
private static final int JOB_ID_FEED_UPDATE = 42; private static final int JOB_ID_FEED_UPDATE = 42;
private static final float JOB_SCHEDULER_TIME_VARIATION = 1.5f;
// Mediaplayer // Mediaplayer
private static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed"; private static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed";
@ -813,13 +812,19 @@ public class UserPreferences {
return; return;
} }
if (Build.VERSION.SDK_INT >= 23) { if (Build.VERSION.SDK_INT >= 24) {
JobInfo.Builder builder = getFeedUpdateJobBuilder();
builder.setOverrideDeadline((long) (triggerAtMillis * JOB_SCHEDULER_TIME_VARIATION));
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
if (jobScheduler != null) { if (jobScheduler != null) {
jobScheduler.schedule(builder.build()); JobInfo oldJob = jobScheduler.getPendingJob(JOB_ID_FEED_UPDATE);
Log.d(TAG, "JobScheduler was set for " + triggerAtMillis); 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; return;
} }
@ -848,12 +853,13 @@ public class UserPreferences {
alarm.add(Calendar.DATE, 1); alarm.add(Calendar.DATE, 1);
} }
if (Build.VERSION.SDK_INT >= 23) { if (Build.VERSION.SDK_INT >= 24) {
JobInfo.Builder builder = getFeedUpdateJobBuilder(); JobInfo.Builder builder = getFeedUpdateJobBuilder();
long triggerAtMillis = alarm.getTimeInMillis() - now.getTimeInMillis(); long triggerAtMillis = alarm.getTimeInMillis() - now.getTimeInMillis();
builder.setOverrideDeadline((long) (triggerAtMillis * JOB_SCHEDULER_TIME_VARIATION)); builder.setMinimumLatency(triggerAtMillis);
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
if (jobScheduler != null) { if (jobScheduler != null) {
jobScheduler.cancel(JOB_ID_FEED_UPDATE);
jobScheduler.schedule(builder.build()); jobScheduler.schedule(builder.build());
Log.d(TAG, "JobScheduler was set for " + triggerAtMillis); Log.d(TAG, "JobScheduler was set for " + triggerAtMillis);
} }
@ -876,7 +882,6 @@ public class UserPreferences {
private static JobInfo.Builder getFeedUpdateJobBuilder() { private static JobInfo.Builder getFeedUpdateJobBuilder() {
ComponentName serviceComponent = new ComponentName(context, FeedUpdateJobService.class); ComponentName serviceComponent = new ComponentName(context, FeedUpdateJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID_FEED_UPDATE, serviceComponent); JobInfo.Builder builder = new JobInfo.Builder(JOB_ID_FEED_UPDATE, serviceComponent);
builder.setMinimumLatency(15 * 60 * 1000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
return builder; return builder;
} }

View File

@ -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) { public static void startService(final Context context, final Playable media, boolean startWhenPrepared, boolean shouldStream) {
Intent launchIntent = new Intent(context, PlaybackService.class); Intent launchIntent = new Intent(context, PlaybackService.class);
launchIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media); launchIntent.putExtra(PlaybackService.EXTRA_PLAYABLE, media);

View File

@ -96,7 +96,15 @@ public abstract class PlaybackController {
/** /**
* Creates a new connection to the playbackService. * 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) { if (initialized) {
return; return;
} }
@ -779,7 +787,7 @@ public abstract class PlaybackController {
} }
} }
public void resumeServiceNotRunning() { private void initServiceNotRunning() {
if (getMedia() == null) { if (getMedia() == null) {
return; return;
} }