1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-29 11:15:51 +00:00

Set initial delay for polling

This commit is contained in:
M66B 2020-01-10 12:26:31 +01:00
parent 9d3eb2d270
commit 24529d8f77

View file

@ -31,6 +31,7 @@ import androidx.work.Worker;
import androidx.work.WorkerParameters;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -55,10 +56,13 @@ public class WorkerPoll extends Worker {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int pollInterval = prefs.getInt("poll_interval", 0);
if (enabled && pollInterval > 0) {
Log.i("Queuing " + getName() + " every " + pollInterval + " minutes");
int min = (int) (new Date().getTime() / (60 * 1000L));
int delay = pollInterval - min % pollInterval;
Log.i("Queuing " + getName() + " every " + pollInterval + " minutes delay=" + delay);
PeriodicWorkRequest workRequest =
new PeriodicWorkRequest.Builder(WorkerPoll.class, pollInterval, TimeUnit.MINUTES)
.setInitialDelay(delay, TimeUnit.MINUTES)
.build();
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.REPLACE, workRequest);