|
|
|
@ -1,13 +1,18 @@
|
|
|
|
|
package org.tasks.jobs;
|
|
|
|
|
|
|
|
|
|
import static org.tasks.time.DateTimeUtils.currentTimeMillis;
|
|
|
|
|
import static com.todoroo.andlib.utility.DateUtilities.now;
|
|
|
|
|
import static org.tasks.date.DateTimeUtils.midnight;
|
|
|
|
|
import static org.tasks.date.DateTimeUtils.newDateTime;
|
|
|
|
|
import static org.tasks.time.DateTimeUtils.printTimestamp;
|
|
|
|
|
|
|
|
|
|
import androidx.work.BackoffPolicy;
|
|
|
|
|
import androidx.work.Constraints;
|
|
|
|
|
import androidx.work.Data;
|
|
|
|
|
import androidx.work.ExistingPeriodicWorkPolicy;
|
|
|
|
|
import androidx.work.ExistingWorkPolicy;
|
|
|
|
|
import androidx.work.NetworkType;
|
|
|
|
|
import androidx.work.OneTimeWorkRequest;
|
|
|
|
|
import androidx.work.OneTimeWorkRequest.Builder;
|
|
|
|
|
import androidx.work.PeriodicWorkRequest;
|
|
|
|
|
import androidx.work.Worker;
|
|
|
|
|
import com.google.common.primitives.Longs;
|
|
|
|
@ -20,7 +25,6 @@ import org.tasks.data.CaldavDao;
|
|
|
|
|
import org.tasks.data.GoogleTaskListDao;
|
|
|
|
|
import org.tasks.injection.ApplicationScope;
|
|
|
|
|
import org.tasks.preferences.Preferences;
|
|
|
|
|
import org.tasks.time.DateTime;
|
|
|
|
|
import timber.log.Timber;
|
|
|
|
|
|
|
|
|
|
@ApplicationScope
|
|
|
|
@ -61,11 +65,19 @@ public class WorkManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void syncNow() {
|
|
|
|
|
workManager.enqueue(
|
|
|
|
|
Constraints constraints =
|
|
|
|
|
new Constraints.Builder()
|
|
|
|
|
.setRequiredNetworkType(
|
|
|
|
|
preferences.getBoolean(R.string.p_background_sync_unmetered_only, false)
|
|
|
|
|
? NetworkType.UNMETERED
|
|
|
|
|
: NetworkType.CONNECTED)
|
|
|
|
|
.build();
|
|
|
|
|
OneTimeWorkRequest request =
|
|
|
|
|
new OneTimeWorkRequest.Builder(SyncWork.class)
|
|
|
|
|
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
|
|
|
|
|
.addTag(TAG_SYNC)
|
|
|
|
|
.build());
|
|
|
|
|
.setConstraints(constraints)
|
|
|
|
|
.build();
|
|
|
|
|
workManager.beginUniqueWork(TAG_SYNC, ExistingWorkPolicy.KEEP, request).enqueue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateBackgroundSync() {
|
|
|
|
@ -92,10 +104,11 @@ public class WorkManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void scheduleBackgroundSynchronization(boolean enabled, boolean onlyOnUnmetered) {
|
|
|
|
|
cancelAllForTag(TAG_BACKGROUND_SYNC);
|
|
|
|
|
Timber.d("background sync enabled: %s, onlyOnUnmetered: %s", enabled, onlyOnUnmetered);
|
|
|
|
|
if (enabled) {
|
|
|
|
|
workManager.enqueue(
|
|
|
|
|
workManager.enqueueUniquePeriodicWork(
|
|
|
|
|
TAG_BACKGROUND_SYNC,
|
|
|
|
|
ExistingPeriodicWorkPolicy.KEEP,
|
|
|
|
|
new PeriodicWorkRequest.Builder(SyncWork.class, 1, TimeUnit.HOURS)
|
|
|
|
|
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
|
|
|
|
|
.setConstraints(
|
|
|
|
@ -104,66 +117,49 @@ public class WorkManager {
|
|
|
|
|
onlyOnUnmetered ? NetworkType.UNMETERED : NetworkType.CONNECTED)
|
|
|
|
|
.build())
|
|
|
|
|
.build());
|
|
|
|
|
} else {
|
|
|
|
|
workManager.cancelUniqueWork(TAG_BACKGROUND_SYNC);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void scheduleRefresh(long time) {
|
|
|
|
|
enqueue(RefreshWork.class, time, TAG_REFRESH);
|
|
|
|
|
enqueueUnique(TAG_REFRESH, RefreshWork.class, time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void scheduleMidnightRefresh() {
|
|
|
|
|
enqueue(
|
|
|
|
|
MidnightRefreshWork.class,
|
|
|
|
|
new DateTime(currentTimeMillis()).plusDays(1).startOfDay().getMillis(),
|
|
|
|
|
TAG_MIDNIGHT_REFRESH);
|
|
|
|
|
enqueueUnique(TAG_MIDNIGHT_REFRESH, MidnightRefreshWork.class, midnight());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void scheduleNotification(long time, boolean cancelCurrent) {
|
|
|
|
|
if (cancelCurrent) {
|
|
|
|
|
cancelNotifications();
|
|
|
|
|
}
|
|
|
|
|
enqueue(NotificationWork.class, time, TAG_NOTIFICATION);
|
|
|
|
|
void scheduleNotification(long time) {
|
|
|
|
|
enqueueUnique(TAG_NOTIFICATION, NotificationWork.class, time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void scheduleBackup() {
|
|
|
|
|
enqueue(
|
|
|
|
|
long lastBackup = preferences.getLong(R.string.p_last_backup, 0L);
|
|
|
|
|
enqueueUnique(
|
|
|
|
|
TAG_BACKUP,
|
|
|
|
|
BackupWork.class,
|
|
|
|
|
new DateTime(currentTimeMillis()).plusDays(1).startOfDay().getMillis(),
|
|
|
|
|
TAG_BACKUP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void enqueue(Class<? extends Worker> c, long time, String tag) {
|
|
|
|
|
long delay = calculateDelay(time);
|
|
|
|
|
Timber.d("enqueue %s: %s (%sms)", tag, printTimestamp(time), delay);
|
|
|
|
|
workManager.enqueue(
|
|
|
|
|
new OneTimeWorkRequest.Builder(c)
|
|
|
|
|
.setInitialDelay(delay, TimeUnit.MILLISECONDS)
|
|
|
|
|
.addTag(tag)
|
|
|
|
|
.build());
|
|
|
|
|
Math.min(newDateTime(lastBackup).plusDays(1).getMillis(), midnight()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cancelRefresh() {
|
|
|
|
|
cancelAllForTag(TAG_REFRESH);
|
|
|
|
|
private void enqueueUnique(String key, Class<? extends Worker> c, long time) {
|
|
|
|
|
long delay = time - now();
|
|
|
|
|
OneTimeWorkRequest.Builder builder = new Builder(c);
|
|
|
|
|
if (delay > 0) {
|
|
|
|
|
builder.setInitialDelay(delay, TimeUnit.MILLISECONDS);
|
|
|
|
|
}
|
|
|
|
|
Timber.d("%s: %s (%sms)", key, printTimestamp(time), delay);
|
|
|
|
|
workManager.beginUniqueWork(key, ExistingWorkPolicy.REPLACE, builder.build()).enqueue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void cancelNotifications() {
|
|
|
|
|
cancelAllForTag(TAG_NOTIFICATION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cancelAllForTag(String tag) {
|
|
|
|
|
Timber.d("cancelAllWorkByTag(%s)", tag);
|
|
|
|
|
workManager.cancelAllWorkByTag(tag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long calculateDelay(long time) {
|
|
|
|
|
return Math.max(5000, time - currentTimeMillis());
|
|
|
|
|
Timber.d("cancelNotifications");
|
|
|
|
|
workManager.cancelAllWorkByTag(TAG_NOTIFICATION);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onStartup() {
|
|
|
|
|
updateBackgroundSync();
|
|
|
|
|
cancelAllForTag(TAG_MIDNIGHT_REFRESH);
|
|
|
|
|
scheduleMidnightRefresh();
|
|
|
|
|
cancelAllForTag(TAG_BACKUP);
|
|
|
|
|
scheduleBackup();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|