#2906: refactor: move auto feed update by interval helper,

checkShouldRefreshFeeds(), to proper level of abstraction.
This commit is contained in:
orionlee 2018-11-21 12:11:39 -08:00
parent 1dc366544e
commit feef92f802
4 changed files with 38 additions and 33 deletions

View File

@ -49,12 +49,12 @@ import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.FeedItemUtil;
import de.danoeh.antennapod.core.util.Flavors;
import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.StorageUtils;
import de.danoeh.antennapod.core.util.download.AutoUpdateManager;
import de.danoeh.antennapod.core.util.gui.NotificationUtils;
import de.danoeh.antennapod.dialog.RatingDialog;
import de.danoeh.antennapod.dialog.RenameFeedDialog;
@ -471,7 +471,7 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
protected void onResume() {
super.onResume();
StorageUtils.checkStorageAvailability(this);
DBTasks.checkShouldRefreshFeeds(getApplicationContext());
AutoUpdateManager.checkShouldRefreshFeeds(getApplicationContext());
Intent intent = getIntent();
if (intent.hasExtra(EXTRA_FEED_ID) ||

View File

@ -46,9 +46,9 @@ import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.download.AutoUpdateManager;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.danoeh.antennapod.dialog.RenameFeedDialog;
@ -187,7 +187,7 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
pagerAdapter.onMediaChanged(media);
pagerAdapter.setController(controller);
}
DBTasks.checkShouldRefreshFeeds(getApplicationContext());
AutoUpdateManager.checkShouldRefreshFeeds(getApplicationContext());
EventDistributor.getInstance().register(contentUpdate);
EventBus.getDefault().register(this);

View File

@ -17,7 +17,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import de.danoeh.antennapod.core.ClientConfig;
@ -28,13 +27,10 @@ import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.FeedPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.GpodnetSyncService;
import de.danoeh.antennapod.core.service.download.DownloadStatus;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.DownloadError;
import de.danoeh.antennapod.core.util.FeedUpdateUtils;
import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator;
@ -208,6 +204,11 @@ public final class DBTasks {
}).start();
}
public static long getLastRefreshAllFeedsTimeMillis(final Context context) {
SharedPreferences prefs = context.getSharedPreferences(DBTasks.PREF_NAME, MODE_PRIVATE);
return prefs.getLong(DBTasks.PREF_LAST_REFRESH, 0);
}
/**
* @param context
* @param feedList the list of feeds to refresh
@ -318,31 +319,6 @@ public final class DBTasks {
DownloadRequester.getInstance().downloadFeed(context, f, loadAllPages, force);
}
/*
* Checks if the app should refresh all feeds, i.e. if the last auto refresh failed.
*
* The feeds are only refreshed if an update interval or time of day is set and the last
* (successful) refresh was before the last interval or more than a day ago, respectively.
*/
public static void checkShouldRefreshFeeds(Context context) {
long interval = 0;
if(UserPreferences.getUpdateInterval() > 0) {
interval = UserPreferences.getUpdateInterval();
} else if(UserPreferences.getUpdateTimeOfDay().length > 0){
interval = TimeUnit.DAYS.toMillis(1);
}
if(interval == 0) { // auto refresh is disabled
return;
}
SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
long lastRefresh = prefs.getLong(PREF_LAST_REFRESH, 0);
Log.d(TAG, "last refresh: " + Converter.getDurationStringLocalized(context,
System.currentTimeMillis() - lastRefresh) + " ago");
if(lastRefresh <= System.currentTimeMillis() - interval) {
FeedUpdateUtils.startAutoUpdate(context, null);
}
}
/**
* Notifies the database about a missing FeedMedia file. This method will correct the FeedMedia object's values in the
* DB and send a FeedUpdateBroadcast.

View File

@ -15,8 +15,12 @@ import android.util.Log;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.receiver.FeedUpdateReceiver;
import de.danoeh.antennapod.core.service.FeedUpdateJobService;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.FeedUpdateUtils;
public class AutoUpdateManager {
private static final int JOB_ID_FEED_UPDATE = 42;
@ -153,4 +157,29 @@ public class AutoUpdateManager {
updateIntent);
Log.d(TAG, "Changed alarm to new time of day " + alarm.get(Calendar.HOUR_OF_DAY) + ":" + alarm.get(Calendar.MINUTE));
}
/*
* Checks if the app should refresh all feeds, i.e. if the last auto refresh failed.
*
* The feeds are only refreshed if an update interval or time of day is set and the last
* (successful) refresh was before the last interval or more than a day ago, respectively.
*
*/
public static void checkShouldRefreshFeeds(Context context) {
long interval = 0;
if(UserPreferences.getUpdateInterval() > 0) {
interval = UserPreferences.getUpdateInterval();
} else if(UserPreferences.getUpdateTimeOfDay().length > 0){
interval = TimeUnit.DAYS.toMillis(1);
}
if(interval == 0) { // auto refresh is disabled
return;
}
long lastRefresh = DBTasks.getLastRefreshAllFeedsTimeMillis(context);
Log.d(TAG, "last refresh: " + Converter.getDurationStringLocalized(context,
System.currentTimeMillis() - lastRefresh) + " ago");
if(lastRefresh <= System.currentTimeMillis() - interval) {
FeedUpdateUtils.startAutoUpdate(context, null);
}
}
}