auto feed update - make the calls from UI use background thread rather than WorkManager to ensure the updates are immediate.

This commit is contained in:
orionlee 2019-09-24 11:02:49 -07:00
parent 8dc9939736
commit 33eddaa256
5 changed files with 34 additions and 12 deletions

View File

@ -196,7 +196,7 @@ public abstract class EpisodesListFragment extends Fragment {
if (!super.onOptionsItemSelected(item)) {
switch (item.getItemId()) {
case R.id.refresh_item:
AutoUpdateManager.runImmediate();
AutoUpdateManager.runImmediate(requireContext());
return true;
case R.id.mark_all_read_item:
ConfirmationDialog markAllReadConfirmationDialog = new ConfirmationDialog(getActivity(),

View File

@ -304,7 +304,7 @@ public class QueueFragment extends Fragment {
toggleQueueLock();
return true;
case R.id.refresh_item:
AutoUpdateManager.runImmediate();
AutoUpdateManager.runImmediate(requireContext());
return true;
case R.id.clear_queue:
// make sure the user really wants to clear the queue

View File

@ -20,7 +20,7 @@ public class FeedUpdateReceiver extends BroadcastReceiver {
Log.d(TAG, "Received intent");
ClientConfig.initialize(context);
AutoUpdateManager.runImmediate();
AutoUpdateManager.runOnce();
}
}

View File

@ -16,7 +16,7 @@ public class FeedUpdateWorker extends Worker {
private static final String TAG = "FeedUpdateWorker";
public static final String PARAM_RUN_IMMEDIATE = "runImmediate";
public static final String PARAM_RUN_ONCE = "runOnce";
public FeedUpdateWorker(@NonNull Context context, @NonNull WorkerParameters params) {
super(context, params);
@ -25,8 +25,8 @@ public class FeedUpdateWorker extends Worker {
@Override
@NonNull
public Result doWork() {
final boolean isImmediate = getInputData().getBoolean(PARAM_RUN_IMMEDIATE, false);
Log.d(TAG, "doWork() : isImmediate = " + isImmediate);
final boolean isRunOnce = getInputData().getBoolean(PARAM_RUN_ONCE, false);
Log.d(TAG, "doWork() : isRunOnce = " + isRunOnce);
ClientConfig.initialize(getApplicationContext());
if (NetworkUtils.networkAvailable() && NetworkUtils.isFeedRefreshAllowed()) {
@ -35,7 +35,7 @@ public class FeedUpdateWorker extends Worker {
Log.d(TAG, "Blocking automatic update: no wifi available / no mobile updates allowed");
}
if (!isImmediate && UserPreferences.isAutoUpdateTimeOfDay()) {
if (!isRunOnce && UserPreferences.isAutoUpdateTimeOfDay()) {
// WorkManager does not allow to set specific time for repeated tasks.
// We repeatedly schedule a OneTimeWorkRequest instead.
UserPreferences.restartUpdateAlarm();

View File

@ -1,5 +1,7 @@
package de.danoeh.antennapod.core.util.download;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import androidx.work.Constraints;
@ -16,10 +18,11 @@ import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.FeedUpdateWorker;
import de.danoeh.antennapod.core.storage.DBTasks;
public class AutoUpdateManager {
private static final String WORK_ID_FEED_UPDATE = "de.danoeh.antennapod.core.service.FeedUpdateWorker";
private static final String WORK_ID_FEED_UPDATE_IMMEDIATE = WORK_ID_FEED_UPDATE + "Immediate";
private static final String WORK_ID_FEED_UPDATE_ONCE = WORK_ID_FEED_UPDATE + "Once";
private static final String TAG = "AutoUpdateManager";
private AutoUpdateManager() {
@ -64,22 +67,41 @@ public class AutoUpdateManager {
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_FEED_UPDATE, ExistingWorkPolicy.REPLACE, workRequest);
}
public static void runImmediate() {
Log.d(TAG, "Run auto update immediately.");
/**
* Run auto feed refresh once in background, as soon as what OS scheduling allows.
*
* Callers from UI should use {@link #runImmediate(Context)}, as it will guarantee
* the refresh be run immediately.
*/
public static void runOnce() {
Log.d(TAG, "Run auto update once, as soon as OS allows.");
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(FeedUpdateWorker.class)
.setConstraints(getConstraints())
.setInitialDelay(0L, TimeUnit.MILLISECONDS)
.setInputData(new Data.Builder()
.putBoolean(FeedUpdateWorker.PARAM_RUN_IMMEDIATE, true)
.putBoolean(FeedUpdateWorker.PARAM_RUN_ONCE, true)
.build()
)
.build();
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_FEED_UPDATE_IMMEDIATE, ExistingWorkPolicy.REPLACE, workRequest);
WorkManager.getInstance().enqueueUniqueWork(WORK_ID_FEED_UPDATE_ONCE, ExistingWorkPolicy.REPLACE, workRequest);
}
/**
/**
* Run auto feed refresh once in background immediately, using its own thread.
*
* Callers where the additional threads is not suitable should use {@link #runOnce()}
*/
public static void runImmediate(@NonNull Context context) {
Log.d(TAG, "Run auto update immediately in background.");
new Thread(() -> {
DBTasks.refreshAllFeeds(context.getApplicationContext());
}, "ManualRefreshAllFeeds").start();
}
public static void disableAutoUpdate() {
WorkManager.getInstance().cancelUniqueWork(WORK_ID_FEED_UPDATE);
}