Refresh local feeds even if there is no internet (#6633)

This commit is contained in:
ByteHamster 2023-09-12 19:53:42 +02:00 committed by GitHub
parent 1e7c347cd2
commit b933c0eb71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 28 deletions

View File

@ -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<Feed> 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<Feed> 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();

View File

@ -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();
}
}