Refactor automatic feed update - remove extra threads no longer necessary.
This commit is contained in:
parent
f24c8a3237
commit
907867652f
|
@ -6,8 +6,7 @@ import android.content.Intent;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import de.danoeh.antennapod.core.ClientConfig;
|
import de.danoeh.antennapod.core.ClientConfig;
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
import de.danoeh.antennapod.core.util.download.AutoUpdateManager;
|
||||||
import de.danoeh.antennapod.core.util.FeedUpdateUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refreshes all feeds when it receives an intent
|
* Refreshes all feeds when it receives an intent
|
||||||
|
@ -20,7 +19,8 @@ public class FeedUpdateReceiver extends BroadcastReceiver {
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
Log.d(TAG, "Received intent");
|
Log.d(TAG, "Received intent");
|
||||||
ClientConfig.initialize(context);
|
ClientConfig.initialize(context);
|
||||||
FeedUpdateUtils.startAutoUpdate(context, null);
|
|
||||||
|
AutoUpdateManager.runImmediate();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,26 @@ package de.danoeh.antennapod.core.service;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.work.Worker;
|
import androidx.work.Worker;
|
||||||
import androidx.work.WorkerParameters;
|
import androidx.work.WorkerParameters;
|
||||||
import de.danoeh.antennapod.core.ClientConfig;
|
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
|
||||||
import de.danoeh.antennapod.core.util.FeedUpdateUtils;
|
|
||||||
import org.awaitility.Awaitility;
|
import org.awaitility.Awaitility;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
public class FeedUpdateWorker extends Worker {
|
public class FeedUpdateWorker extends Worker {
|
||||||
|
|
||||||
|
private static final String TAG = "FeedUpdateWorker";
|
||||||
|
|
||||||
|
public static final String PARAM_RUN_IMMEDIATE = "runImmediate";
|
||||||
|
|
||||||
public FeedUpdateWorker(@NonNull Context context, @NonNull WorkerParameters params) {
|
public FeedUpdateWorker(@NonNull Context context, @NonNull WorkerParameters params) {
|
||||||
super(context, params);
|
super(context, params);
|
||||||
}
|
}
|
||||||
|
@ -20,13 +29,20 @@ public class FeedUpdateWorker extends Worker {
|
||||||
@Override
|
@Override
|
||||||
@NonNull
|
@NonNull
|
||||||
public Result doWork() {
|
public Result doWork() {
|
||||||
|
final boolean isImmediate = getInputData() != null ?
|
||||||
|
getInputData().getBoolean(PARAM_RUN_IMMEDIATE, false) : false;
|
||||||
|
Log.d(TAG, "doWork() : isImmediate = " + isImmediate);
|
||||||
ClientConfig.initialize(getApplicationContext());
|
ClientConfig.initialize(getApplicationContext());
|
||||||
|
|
||||||
AtomicBoolean finished = new AtomicBoolean(false);
|
AtomicBoolean finished = new AtomicBoolean(false);
|
||||||
FeedUpdateUtils.startAutoUpdate(getApplicationContext(), () -> finished.set(true));
|
if (NetworkUtils.networkAvailable() && NetworkUtils.isFeedRefreshAllowed()) {
|
||||||
|
DBTasks.refreshAllFeeds(getApplicationContext(), null, () -> finished.set(true));
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Blocking automatic update: no wifi available / no mobile updates allowed");
|
||||||
|
}
|
||||||
Awaitility.await().until(finished::get);
|
Awaitility.await().until(finished::get);
|
||||||
|
|
||||||
if (UserPreferences.isAutoUpdateTimeOfDay()) {
|
if (!isImmediate && UserPreferences.isAutoUpdateTimeOfDay()) {
|
||||||
// WorkManager does not allow to set specific time for repeated tasks.
|
// WorkManager does not allow to set specific time for repeated tasks.
|
||||||
// We repeatedly schedule a OneTimeWorkRequest instead.
|
// We repeatedly schedule a OneTimeWorkRequest instead.
|
||||||
UserPreferences.restartUpdateAlarm();
|
UserPreferences.restartUpdateAlarm();
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
package de.danoeh.antennapod.core.util;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import org.awaitility.core.ConditionTimeoutException;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import de.danoeh.antennapod.core.storage.DBTasks;
|
|
||||||
|
|
||||||
import static org.awaitility.Awaitility.with;
|
|
||||||
|
|
||||||
public class FeedUpdateUtils {
|
|
||||||
private static final String TAG = "FeedUpdateUtils";
|
|
||||||
|
|
||||||
private FeedUpdateUtils() {}
|
|
||||||
|
|
||||||
public static void startAutoUpdate(Context context, Runnable callback) {
|
|
||||||
// the network check is blocking for possibly a long time: so run the logic
|
|
||||||
// in a separate thread to prevent the code blocking the callers
|
|
||||||
final Runnable runnable = () -> {
|
|
||||||
try {
|
|
||||||
with().pollInterval(1, TimeUnit.SECONDS)
|
|
||||||
.await()
|
|
||||||
.atMost(10, TimeUnit.SECONDS)
|
|
||||||
.until(() -> NetworkUtils.networkAvailable() && NetworkUtils.isFeedRefreshAllowed());
|
|
||||||
DBTasks.refreshAllFeeds(context, null, callback);
|
|
||||||
} catch (ConditionTimeoutException ignore) {
|
|
||||||
Log.d(TAG, "Blocking automatic update: no wifi available / no mobile updates allowed");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
new Thread(runnable).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,21 +1,25 @@
|
||||||
package de.danoeh.antennapod.core.util.download;
|
package de.danoeh.antennapod.core.util.download;
|
||||||
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.work.Constraints;
|
import androidx.work.Constraints;
|
||||||
|
import androidx.work.Data;
|
||||||
import androidx.work.ExistingPeriodicWorkPolicy;
|
import androidx.work.ExistingPeriodicWorkPolicy;
|
||||||
import androidx.work.ExistingWorkPolicy;
|
import androidx.work.ExistingWorkPolicy;
|
||||||
import androidx.work.NetworkType;
|
import androidx.work.NetworkType;
|
||||||
import androidx.work.OneTimeWorkRequest;
|
import androidx.work.OneTimeWorkRequest;
|
||||||
import androidx.work.PeriodicWorkRequest;
|
import androidx.work.PeriodicWorkRequest;
|
||||||
import androidx.work.WorkManager;
|
import androidx.work.WorkManager;
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
|
||||||
import de.danoeh.antennapod.core.service.FeedUpdateWorker;
|
|
||||||
|
|
||||||
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.service.FeedUpdateWorker;
|
||||||
|
|
||||||
public class AutoUpdateManager {
|
public class AutoUpdateManager {
|
||||||
private static final String WORK_ID_FEED_UPDATE = FeedUpdateWorker.class.getName();
|
private static final String WORK_ID_FEED_UPDATE = FeedUpdateWorker.class.getName();
|
||||||
|
private static final String WORK_ID_FEED_UPDATE_IMMEDIATE = FeedUpdateWorker.class.getName() +"Immediate";
|
||||||
private static final String TAG = "AutoUpdateManager";
|
private static final String TAG = "AutoUpdateManager";
|
||||||
|
|
||||||
private AutoUpdateManager() {
|
private AutoUpdateManager() {
|
||||||
|
@ -60,6 +64,22 @@ public class AutoUpdateManager {
|
||||||
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_FEED_UPDATE, ExistingWorkPolicy.REPLACE, workRequest);
|
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_FEED_UPDATE, ExistingWorkPolicy.REPLACE, workRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void runImmediate() {
|
||||||
|
Log.d(TAG, "Run auto update immediately.");
|
||||||
|
|
||||||
|
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(FeedUpdateWorker.class)
|
||||||
|
.setConstraints(getConstraints())
|
||||||
|
.setInitialDelay(0L, TimeUnit.MILLISECONDS)
|
||||||
|
.setInputData(new Data.Builder()
|
||||||
|
.putBoolean(FeedUpdateWorker.PARAM_RUN_IMMEDIATE, true)
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_FEED_UPDATE_IMMEDIATE, ExistingWorkPolicy.REPLACE, workRequest);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static void disableAutoUpdate() {
|
public static void disableAutoUpdate() {
|
||||||
WorkManager.getInstance().cancelUniqueWork(WORK_ID_FEED_UPDATE);
|
WorkManager.getInstance().cancelUniqueWork(WORK_ID_FEED_UPDATE);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue