Merge pull request #2913 from orionlee/auto_feed_update_fix_in_airplane_2906

Auto feed update fix in airplane mode
This commit is contained in:
H. Lehmann 2019-01-03 19:25:51 +01:00 committed by GitHub
commit 8252f6a41d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 32 deletions

View File

@ -50,12 +50,12 @@ import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService; import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.storage.DBReader; 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.storage.DBWriter;
import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.FeedItemUtil;
import de.danoeh.antennapod.core.util.Flavors; import de.danoeh.antennapod.core.util.Flavors;
import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.StorageUtils; 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.core.util.gui.NotificationUtils;
import de.danoeh.antennapod.dialog.RatingDialog; import de.danoeh.antennapod.dialog.RatingDialog;
import de.danoeh.antennapod.dialog.RenameFeedDialog; import de.danoeh.antennapod.dialog.RenameFeedDialog;
@ -474,7 +474,7 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
StorageUtils.checkStorageAvailability(this); StorageUtils.checkStorageAvailability(this);
DBTasks.checkShouldRefreshFeeds(getApplicationContext()); AutoUpdateManager.checkShouldRefreshFeeds(getApplicationContext());
Intent intent = getIntent(); Intent intent = getIntent();
if (intent.hasExtra(EXTRA_FEED_ID) || 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.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlayerStatus; import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBReader; 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.storage.DBWriter;
import de.danoeh.antennapod.core.util.IntentUtils; 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.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController; import de.danoeh.antennapod.core.util.playback.PlaybackController;
import de.danoeh.antennapod.dialog.RenameFeedDialog; import de.danoeh.antennapod.dialog.RenameFeedDialog;
@ -187,7 +187,7 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
pagerAdapter.onMediaChanged(media); pagerAdapter.onMediaChanged(media);
pagerAdapter.setController(controller); pagerAdapter.setController(controller);
} }
DBTasks.checkShouldRefreshFeeds(getApplicationContext()); AutoUpdateManager.checkShouldRefreshFeeds(getApplicationContext());
EventDistributor.getInstance().register(contentUpdate); EventDistributor.getInstance().register(contentUpdate);
EventBus.getDefault().register(this); EventBus.getDefault().register(this);

View File

@ -17,7 +17,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import de.danoeh.antennapod.core.ClientConfig; import de.danoeh.antennapod.core.ClientConfig;
@ -28,11 +27,9 @@ import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.FeedPreferences; 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.GpodnetSyncService;
import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.service.download.DownloadStatus;
import de.danoeh.antennapod.core.service.playback.PlaybackService; 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.DownloadError;
import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.LongList; import de.danoeh.antennapod.core.util.LongList;
@ -207,6 +204,11 @@ public final class DBTasks {
}).start(); }).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 context
* @param feedList the list of feeds to refresh * @param feedList the list of feeds to refresh
@ -317,31 +319,6 @@ public final class DBTasks {
DownloadRequester.getInstance().downloadFeed(context, f, loadAllPages, force); 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) {
DBTasks.refreshAllFeeds(context, null);
}
}
/** /**
* Notifies the database about a missing FeedMedia file. This method will correct the FeedMedia object's values in the * Notifies the database about a missing FeedMedia file. This method will correct the FeedMedia object's values in the
* DB and send a FeedUpdateBroadcast. * DB and send a FeedUpdateBroadcast.

View File

@ -15,8 +15,12 @@ import android.util.Log;
import java.util.Calendar; import java.util.Calendar;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.receiver.FeedUpdateReceiver; import de.danoeh.antennapod.core.receiver.FeedUpdateReceiver;
import de.danoeh.antennapod.core.service.FeedUpdateJobService; 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 { public class AutoUpdateManager {
private static final int JOB_ID_FEED_UPDATE = 42; private static final int JOB_ID_FEED_UPDATE = 42;
@ -153,4 +157,29 @@ public class AutoUpdateManager {
updateIntent); updateIntent);
Log.d(TAG, "Changed alarm to new time of day " + alarm.get(Calendar.HOUR_OF_DAY) + ":" + alarm.get(Calendar.MINUTE)); 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);
}
}
} }