Moved methods into AutoUpdateManager

This commit is contained in:
ByteHamster 2018-05-26 23:30:06 +02:00
parent b3e02e215c
commit 443f6315fa
2 changed files with 41 additions and 43 deletions

View File

@ -2,7 +2,6 @@ package de.danoeh.antennapod.core.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
@ -25,7 +24,6 @@ import java.io.IOException;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -776,49 +774,14 @@ public class UserPreferences {
int[] timeOfDay = getUpdateTimeOfDay();
Log.d(TAG, "timeOfDay: " + Arrays.toString(timeOfDay));
if (timeOfDay.length == 2) {
restartUpdateTimeOfDayAlarm(timeOfDay[0], timeOfDay[1]);
AutoUpdateManager.restartUpdateTimeOfDayAlarm(context, timeOfDay[0], timeOfDay[1]);
} else {
long milliseconds = getUpdateInterval();
long startTrigger = milliseconds;
if (now) {
startTrigger = TimeUnit.SECONDS.toMillis(10);
}
restartUpdateIntervalAlarm(startTrigger, milliseconds);
}
}
/**
* Sets the interval in which the feeds are refreshed automatically
*/
private static void restartUpdateIntervalAlarm(long triggerAtMillis, long intervalMillis) {
Log.d(TAG, "Restarting update alarm.");
if (Build.VERSION.SDK_INT >= 24) {
AutoUpdateManager.restartJobServiceInterval(context, intervalMillis);
} else {
AutoUpdateManager.restartAlarmManagerInterval(context, triggerAtMillis, intervalMillis);
}
}
/**
* Sets time of day the feeds are refreshed automatically
*/
private static void restartUpdateTimeOfDayAlarm(int hoursOfDay, int minute) {
Log.d(TAG, "Restarting update alarm.");
Calendar now = Calendar.getInstance();
Calendar alarm = (Calendar)now.clone();
alarm.set(Calendar.HOUR_OF_DAY, hoursOfDay);
alarm.set(Calendar.MINUTE, minute);
if (alarm.before(now) || alarm.equals(now)) {
alarm.add(Calendar.DATE, 1);
}
if (Build.VERSION.SDK_INT >= 24) {
long triggerAtMillis = alarm.getTimeInMillis() - now.getTimeInMillis();
AutoUpdateManager.restartJobServiceTriggerAt(context, triggerAtMillis);
} else {
AutoUpdateManager.restartAlarmManagerTimeOfDay(context, alarm);
AutoUpdateManager.restartUpdateIntervalAlarm(context, startTrigger, milliseconds);
}
}

View File

@ -25,6 +25,41 @@ public class AutoUpdateManager {
}
/**
* Sets the interval in which the feeds are refreshed automatically
*/
public static void restartUpdateIntervalAlarm(Context context, long triggerAtMillis, long intervalMillis) {
Log.d(TAG, "Restarting update alarm.");
if (Build.VERSION.SDK_INT >= 24) {
restartJobServiceInterval(context, intervalMillis);
} else {
restartAlarmManagerInterval(context, triggerAtMillis, intervalMillis);
}
}
/**
* Sets time of day the feeds are refreshed automatically
*/
public static void restartUpdateTimeOfDayAlarm(Context context, int hoursOfDay, int minute) {
Log.d(TAG, "Restarting update alarm.");
Calendar now = Calendar.getInstance();
Calendar alarm = (Calendar)now.clone();
alarm.set(Calendar.HOUR_OF_DAY, hoursOfDay);
alarm.set(Calendar.MINUTE, minute);
if (alarm.before(now) || alarm.equals(now)) {
alarm.add(Calendar.DATE, 1);
}
if (Build.VERSION.SDK_INT >= 24) {
long triggerAtMillis = alarm.getTimeInMillis() - now.getTimeInMillis();
restartJobServiceTriggerAt(context, triggerAtMillis);
} else {
restartAlarmManagerTimeOfDay(context, alarm);
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static JobInfo.Builder getFeedUpdateJobBuilder(Context context) {
ComponentName serviceComponent = new ComponentName(context, FeedUpdateJobService.class);
@ -35,7 +70,7 @@ public class AutoUpdateManager {
}
@RequiresApi(api = Build.VERSION_CODES.N)
public static void restartJobServiceInterval(Context context, long intervalMillis) {
private static void restartJobServiceInterval(Context context, long intervalMillis) {
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
if (jobScheduler == null) {
Log.d(TAG, "JobScheduler was null.");
@ -61,7 +96,7 @@ public class AutoUpdateManager {
Log.d(TAG, "JobScheduler was set at interval " + intervalMillis);
}
public static void restartAlarmManagerInterval(Context context, long triggerAtMillis, long intervalMillis) {
private static void restartAlarmManagerInterval(Context context, long triggerAtMillis, long intervalMillis) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager == null) {
@ -85,7 +120,7 @@ public class AutoUpdateManager {
}
@RequiresApi(api = Build.VERSION_CODES.N)
public static void restartJobServiceTriggerAt(Context context, long triggerAtMillis) {
private static void restartJobServiceTriggerAt(Context context, long triggerAtMillis) {
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
if (jobScheduler == null) {
Log.d(TAG, "JobScheduler was null.");
@ -99,7 +134,7 @@ public class AutoUpdateManager {
Log.d(TAG, "JobScheduler was set for " + triggerAtMillis);
}
public static void restartAlarmManagerTimeOfDay(Context context, Calendar alarm) {
private static void restartAlarmManagerTimeOfDay(Context context, Calendar alarm) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager == null) {