From b933c0eb71fb59b1acb044763f05413b4da5106e Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Tue, 12 Sep 2023 19:53:42 +0200 Subject: [PATCH] Refresh local feeds even if there is no internet (#6633) --- .../core/service/FeedUpdateWorker.java | 31 ++++++++++++------- .../core/util/download/FeedUpdateManager.java | 26 ++++++---------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateWorker.java b/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateWorker.java index b207db83b..d71922e7b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateWorker.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/FeedUpdateWorker.java @@ -34,7 +34,6 @@ import de.danoeh.antennapod.model.download.DownloadResult; import de.danoeh.antennapod.model.feed.Feed; import de.danoeh.antennapod.net.download.serviceinterface.DownloadRequest; -import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -57,15 +56,10 @@ public class FeedUpdateWorker extends Worker { ClientConfigurator.initialize(getApplicationContext()); newEpisodesNotification.loadCountersBeforeRefresh(); - if (!getInputData().getBoolean(FeedUpdateManager.EXTRA_EVEN_ON_MOBILE, false)) { - if (!NetworkUtils.networkAvailable() || !NetworkUtils.isFeedRefreshAllowed()) { - Log.d(TAG, "Blocking automatic update: no wifi available / no mobile updates allowed"); - return Result.retry(); - } - } - List toUpdate; long feedId = getInputData().getLong(FeedUpdateManager.EXTRA_FEED_ID, -1); + boolean allAreLocal = true; + boolean force = false; if (feedId == -1) { // Update all toUpdate = DBReader.getFeedList(); Iterator itr = toUpdate.iterator(); @@ -74,18 +68,31 @@ public class FeedUpdateWorker extends Worker { if (!feed.getPreferences().getKeepUpdated()) { itr.remove(); } + if (!feed.isLocalFeed()) { + allAreLocal = false; + } } Collections.shuffle(toUpdate); // If the worker gets cancelled early, every feed has a chance to be updated - refreshFeeds(toUpdate, false); } else { - toUpdate = new ArrayList<>(); Feed feed = DBReader.getFeed(feedId); if (feed == null) { return Result.success(); } - toUpdate.add(feed); - refreshFeeds(toUpdate, true); + if (!feed.isLocalFeed()) { + allAreLocal = false; + } + toUpdate = Collections.singletonList(feed); + force = true; } + + if (!getInputData().getBoolean(FeedUpdateManager.EXTRA_EVEN_ON_MOBILE, false) && !allAreLocal) { + if (!NetworkUtils.networkAvailable() || !NetworkUtils.isFeedRefreshAllowed()) { + Log.d(TAG, "Blocking automatic update"); + return Result.retry(); + } + } + refreshFeeds(toUpdate, force); + notificationManager.cancel(R.id.notification_updating_feeds); DBTasks.autodownloadUndownloadedItems(getApplicationContext()); return Result.success(); diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/download/FeedUpdateManager.java b/core/src/main/java/de/danoeh/antennapod/core/util/download/FeedUpdateManager.java index 4fd75a615..2f2726bdb 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/download/FeedUpdateManager.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/download/FeedUpdateManager.java @@ -47,7 +47,9 @@ public class FeedUpdateManager { } else { PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder( FeedUpdateWorker.class, UserPreferences.getUpdateInterval(), TimeUnit.HOURS) - .setConstraints(getConstraints()) + .setConstraints(new Constraints.Builder() + .setRequiredNetworkType(UserPreferences.isAllowMobileFeedRefresh() + ? NetworkType.CONNECTED : NetworkType.UNMETERED).build()) .build(); WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORK_ID_FEED_UPDATE, replace ? ExistingPeriodicWorkPolicy.REPLACE : ExistingPeriodicWorkPolicy.KEEP, workRequest); @@ -66,9 +68,11 @@ public class FeedUpdateManager { OneTimeWorkRequest.Builder workRequest = new OneTimeWorkRequest.Builder(FeedUpdateWorker.class) .setInitialDelay(0L, TimeUnit.MILLISECONDS) .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) - .addTag(WORK_TAG_FEED_UPDATE) - .setConstraints(new Constraints.Builder() + .addTag(WORK_TAG_FEED_UPDATE); + if (feed == null || !feed.isLocalFeed()) { + workRequest.setConstraints(new Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED).build()); + } Data.Builder builder = new Data.Builder(); builder.putBoolean(EXTRA_EVEN_ON_MOBILE, true); if (feed != null) { @@ -86,7 +90,9 @@ public class FeedUpdateManager { public static void runOnceOrAsk(@NonNull Context context, @Nullable Feed feed) { Log.d(TAG, "Run auto update immediately in background."); - if (!NetworkUtils.networkAvailable()) { + if (feed != null && feed.isLocalFeed()) { + runOnce(context, feed); + } else if (!NetworkUtils.networkAvailable()) { EventBus.getDefault().post(new MessageEvent(context.getString(R.string.download_error_no_connection))); } else if (NetworkUtils.isFeedRefreshAllowed()) { runOnce(context, feed); @@ -112,16 +118,4 @@ public class FeedUpdateManager { } builder.show(); } - - private static Constraints getConstraints() { - Constraints.Builder constraints = new Constraints.Builder(); - - if (UserPreferences.isAllowMobileFeedRefresh()) { - constraints.setRequiredNetworkType(NetworkType.CONNECTED); - } else { - constraints.setRequiredNetworkType(NetworkType.UNMETERED); - } - return constraints.build(); - } - }