auto feed update mini-refactor: move periodic update entry point method from UserPreference to AutoUpdateManager

This commit is contained in:
orionlee 2019-09-24 11:14:35 -07:00
parent 33eddaa256
commit 26b7f6c300
4 changed files with 27 additions and 19 deletions

View File

@ -3,9 +3,11 @@ package de.danoeh.antennapod.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.download.AutoUpdateManager;
import de.danoeh.antennapod.core.util.gui.NotificationUtils;
public class PreferenceUpgrader {
@ -22,7 +24,7 @@ public class PreferenceUpgrader {
if (oldVersion != newVersion) {
NotificationUtils.createChannels(context);
UserPreferences.restartUpdateAlarm();
AutoUpdateManager.restartUpdateAlarm();
upgrade(oldVersion);
upgraderPrefs.edit().putInt(PREF_CONFIGURED_VERSION, newVersion).apply();

View File

@ -622,7 +622,7 @@ public class UserPreferences {
.apply();
// when updating with an interval, we assume the user wants
// to update *now* and then every 'hours' interval thereafter.
restartUpdateAlarm();
AutoUpdateManager.restartUpdateAlarm();
}
/**
@ -632,7 +632,7 @@ public class UserPreferences {
prefs.edit()
.putString(PREF_UPDATE_INTERVAL, hourOfDay + ":" + minute)
.apply();
restartUpdateAlarm();
AutoUpdateManager.restartUpdateAlarm();
}
public static void disableAutoUpdate() {
@ -882,19 +882,6 @@ public class UserPreferences {
return getUpdateTimeOfDay().length == 2;
}
public static void restartUpdateAlarm() {
if (isAutoUpdateDisabled()) {
AutoUpdateManager.disableAutoUpdate();
} else if (isAutoUpdateTimeOfDay()) {
int[] timeOfDay = getUpdateTimeOfDay();
Log.d(TAG, "timeOfDay: " + Arrays.toString(timeOfDay));
AutoUpdateManager.restartUpdateTimeOfDayAlarm(timeOfDay[0], timeOfDay[1]);
} else {
long milliseconds = getUpdateInterval();
AutoUpdateManager.restartUpdateIntervalAlarm(milliseconds);
}
}
/**
* Evaluates whether Cast support (Chromecast, Audio Cast, etc) is enabled on the preferences.
*/

View File

@ -11,6 +11,7 @@ import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.core.util.download.AutoUpdateManager;
public class FeedUpdateWorker extends Worker {
@ -38,7 +39,7 @@ public class FeedUpdateWorker extends Worker {
if (!isRunOnce && UserPreferences.isAutoUpdateTimeOfDay()) {
// WorkManager does not allow to set specific time for repeated tasks.
// We repeatedly schedule a OneTimeWorkRequest instead.
UserPreferences.restartUpdateAlarm();
AutoUpdateManager.restartUpdateAlarm();
}
return Result.success();

View File

@ -13,6 +13,7 @@ import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import java.util.Arrays;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
@ -29,10 +30,26 @@ public class AutoUpdateManager {
}
/**
* Start / restart periodic auto feed refresh
*/
public static void restartUpdateAlarm() {
if (UserPreferences.isAutoUpdateDisabled()) {
disableAutoUpdate();
} else if (UserPreferences.isAutoUpdateTimeOfDay()) {
int[] timeOfDay = UserPreferences.getUpdateTimeOfDay();
Log.d(TAG, "timeOfDay: " + Arrays.toString(timeOfDay));
restartUpdateTimeOfDayAlarm(timeOfDay[0], timeOfDay[1]);
} else {
long milliseconds = UserPreferences.getUpdateInterval();
restartUpdateIntervalAlarm(milliseconds);
}
}
/**
* Sets the interval in which the feeds are refreshed automatically
*/
public static void restartUpdateIntervalAlarm(long intervalMillis) {
private static void restartUpdateIntervalAlarm(long intervalMillis) {
Log.d(TAG, "Restarting update alarm.");
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(FeedUpdateWorker.class,
@ -47,7 +64,7 @@ public class AutoUpdateManager {
/**
* Sets time of day the feeds are refreshed automatically
*/
public static void restartUpdateTimeOfDayAlarm(int hoursOfDay, int minute) {
private static void restartUpdateTimeOfDayAlarm(int hoursOfDay, int minute) {
Log.d(TAG, "Restarting update alarm.");
Calendar now = Calendar.getInstance();
@ -116,4 +133,5 @@ public class AutoUpdateManager {
}
return constraints.build();
}
}