Use work manager for after save work

pull/757/head
Alex Baker 6 years ago
parent 01ef2476ad
commit 0e32ddaf54

@ -355,10 +355,6 @@
</intent-filter>
</receiver>
<service
android:exported="false"
android:name=".jobs.AfterSaveIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<service
android:exported="false"
android:name=".scheduling.GeofenceSchedulingIntentService"

@ -7,11 +7,10 @@ package com.todoroo.astrid.dao;
import static com.todoroo.andlib.utility.DateUtilities.now;
import android.database.Cursor;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Update;
import android.content.Context;
import android.database.Cursor;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Functions;
@ -23,7 +22,7 @@ import com.todoroo.astrid.helper.UUIDHelper;
import java.util.ArrayList;
import java.util.List;
import org.tasks.BuildConfig;
import org.tasks.jobs.AfterSaveIntentService;
import org.tasks.jobs.WorkManager;
import timber.log.Timber;
@Dao
@ -33,14 +32,14 @@ public abstract class TaskDao {
private final Database database;
private Context context;
private WorkManager workManager;
public TaskDao(Database database) {
this.database = database;
}
public void initialize(Context context) {
this.context = context;
public void initialize(WorkManager workManager) {
this.workManager = workManager;
}
public List<Task> needsRefresh() {
@ -144,7 +143,7 @@ public abstract class TaskDao {
// TODO: get rid of this super-hack
public void save(Task task, Task original) {
if (saveExisting(task, original)) {
AfterSaveIntentService.enqueue(context, task, original);
workManager.afterSave(task, original);
}
}

@ -23,6 +23,7 @@ import org.tasks.data.TaskAttachmentDao;
import org.tasks.data.TaskListMetadataDao;
import org.tasks.data.UserActivityDao;
import org.tasks.db.Migrations;
import org.tasks.jobs.WorkManager;
import org.tasks.locale.Locale;
import org.tasks.notifications.NotificationDao;
import org.tasks.security.Encryption;
@ -133,9 +134,9 @@ public class ApplicationModule {
@Provides
@ApplicationScope
public TaskDao getTaskDao(Database database) {
public TaskDao getTaskDao(Database database, WorkManager workManager) {
TaskDao taskDao = database.getTaskDao();
taskDao.initialize(context);
taskDao.initialize(workManager);
return taskDao;
}

@ -11,7 +11,6 @@ public abstract class InjectingJobIntentService extends JobIntentService {
public static final int JOB_ID_BACKGROUND_SCHEDULER = 1080;
public static final int JOB_ID_GEOFENCE_TRANSITION = 1081;
public static final int JOB_ID_GEOFENCE_SCHEDULING = 1082;
public static final int JOB_ID_TASK_STATUS_CHANGE = 1083;
public static final int JOB_ID_NOTIFICATION_SCHEDULER = 1084;
public static final int JOB_ID_CALENDAR_NOTIFICATION = 1085;
public static final int JOB_ID_TASKER = 1086;

@ -1,7 +1,6 @@
package org.tasks.injection;
import dagger.Subcomponent;
import org.tasks.jobs.AfterSaveIntentService;
import org.tasks.locale.receiver.TaskerIntentService;
import org.tasks.location.GeofenceTransitionsIntentService;
import org.tasks.scheduling.BackgroundScheduler;
@ -22,7 +21,5 @@ public interface IntentServiceComponent {
void inject(BackgroundScheduler backgroundScheduler);
void inject(AfterSaveIntentService afterSaveIntentService);
void inject(TaskerIntentService taskerIntentService);
}

@ -1,6 +1,7 @@
package org.tasks.injection;
import dagger.Subcomponent;
import org.tasks.jobs.AfterSaveWork;
import org.tasks.jobs.BackupWork;
import org.tasks.jobs.CleanupWork;
import org.tasks.jobs.MidnightRefreshWork;
@ -22,4 +23,6 @@ public interface JobComponent {
void inject(CleanupWork cleanupWork);
void inject(MidnightRefreshWork midnightRefreshWork);
void inject(AfterSaveWork afterSaveWork);
}

@ -5,11 +5,15 @@ import static com.todoroo.astrid.dao.TaskDao.TRANS_SUPPRESS_REFRESH;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.CalendarContract;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Data.Builder;
import androidx.work.WorkerParameters;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.SyncFlags;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.reminders.ReminderService;
import com.todoroo.astrid.repeats.RepeatTaskHelper;
@ -18,18 +22,22 @@ import javax.inject.Inject;
import org.tasks.LocalBroadcastManager;
import org.tasks.R;
import org.tasks.injection.ForApplication;
import org.tasks.injection.InjectingJobIntentService;
import org.tasks.injection.IntentServiceComponent;
import org.tasks.injection.InjectingWorker;
import org.tasks.injection.JobComponent;
import org.tasks.location.GeofenceService;
import org.tasks.notifications.NotificationManager;
import org.tasks.receivers.PushReceiver;
import org.tasks.scheduling.RefreshScheduler;
import org.tasks.sync.SyncAdapters;
import timber.log.Timber;
public class AfterSaveIntentService extends InjectingJobIntentService {
public class AfterSaveWork extends InjectingWorker {
private static final String EXTRA_ID = "extra_id";
private static final String EXTRA_ORIG_COMPLETED = "extra_was_completed";
private static final String EXTRA_ORIG_DELETED = "extra_was_deleted";
private static final String EXTRA_PUSH_GTASKS = "extra_push_gtasks";
private static final String EXTRA_PUSH_CALDAV = "extra_push_caldav";
private static final String EXTRA_CURRENT = "extra_current";
private static final String EXTRA_ORIGINAL = "extra_original";
@Inject RepeatTaskHelper repeatTaskHelper;
@Inject @ForApplication Context context;
@Inject NotificationManager notificationManager;
@ -38,42 +46,48 @@ public class AfterSaveIntentService extends InjectingJobIntentService {
@Inject ReminderService reminderService;
@Inject RefreshScheduler refreshScheduler;
@Inject LocalBroadcastManager localBroadcastManager;
@Inject PushReceiver pushReceiver;
public static void enqueue(Context context, Task current, Task original) {
Intent intent = new Intent(context, AfterSaveIntentService.class);
intent.putExtra(EXTRA_CURRENT, current);
intent.putExtra(EXTRA_ORIGINAL, original);
AfterSaveIntentService.enqueueWork(
context,
AfterSaveIntentService.class,
InjectingJobIntentService.JOB_ID_TASK_STATUS_CHANGE,
intent);
@Inject TaskDao taskDao;
@Inject SyncAdapters syncAdapters;
@Inject WorkManager workManager;
static Data getInputData(Task current, Task original) {
boolean suppress = current.checkTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC);
boolean force = current.checkTransitory(SyncFlags.FORCE_SYNC);
Builder builder =
new Builder()
.putLong(EXTRA_ID, current.getId())
.putBoolean(
EXTRA_PUSH_GTASKS, !suppress && (force || !current.googleTaskUpToDate(original)))
.putBoolean(
EXTRA_PUSH_CALDAV, !suppress && (force || !current.caldavUpToDate(original)));
if (original != null) {
builder
.putLong(EXTRA_ORIG_DELETED, original.getCompletionDate())
.putLong(EXTRA_ORIG_DELETED, original.getDeletionDate());
}
return builder.build();
}
public AfterSaveWork(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@Override
protected void doWork(@NonNull Intent intent) {
Task task = intent.getParcelableExtra(EXTRA_CURRENT);
protected Result run() {
Data data = getInputData();
long taskId = data.getLong(EXTRA_ID, -1);
Task task = taskDao.fetch(taskId);
if (task == null) {
Timber.e("Missing saved task");
return;
}
long taskId = task.getId();
Task original = intent.getParcelableExtra(EXTRA_ORIGINAL);
if (original == null
|| !task.getDueDate().equals(original.getDueDate())
|| !task.getReminderFlags().equals(original.getReminderFlags())
|| !task.getReminderPeriod().equals(original.getReminderPeriod())
|| !task.getReminderLast().equals(original.getReminderLast())
|| !task.getReminderSnooze().equals(original.getReminderSnooze())) {
reminderService.scheduleAlarm(task);
return Result.FAILURE;
}
reminderService.scheduleAlarm(task);
boolean completionDateModified =
original == null || !task.getCompletionDate().equals(original.getCompletionDate());
!task.getCompletionDate().equals(data.getLong(EXTRA_ORIG_COMPLETED, 0));
boolean deletionDateModified =
original != null && !task.getDeletionDate().equals(original.getDeletionDate());
!task.getDeletionDate().equals(data.getLong(EXTRA_ORIG_DELETED, 0));
boolean justCompleted = completionDateModified && task.isCompleted();
boolean justDeleted = deletionDateModified && task.isDeleted();
@ -93,11 +107,17 @@ public class AfterSaveIntentService extends InjectingJobIntentService {
}
}
pushReceiver.push(task, original);
if ((data.getBoolean(EXTRA_PUSH_GTASKS, false) && syncAdapters.isGoogleTaskSyncEnabled())
|| (data.getBoolean(EXTRA_PUSH_CALDAV, false) && syncAdapters.isCaldavSyncEnabled())) {
workManager.syncNow();
}
refreshScheduler.scheduleRefresh(task);
if (!task.checkAndClearTransitory(TRANS_SUPPRESS_REFRESH)) {
localBroadcastManager.broadcastRefresh();
}
return Result.SUCCESS;
}
private void updateCalendarTitle(Task task) {
@ -118,7 +138,7 @@ public class AfterSaveIntentService extends InjectingJobIntentService {
}
@Override
protected void inject(IntentServiceComponent component) {
protected void inject(JobComponent component) {
component.inject(this);
}
}

@ -16,6 +16,7 @@ import androidx.work.OneTimeWorkRequest.Builder;
import androidx.work.PeriodicWorkRequest;
import androidx.work.Worker;
import com.google.common.primitives.Longs;
import com.todoroo.astrid.data.Task;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
@ -54,6 +55,13 @@ public class WorkManager {
workManager = androidx.work.WorkManager.getInstance();
}
public void afterSave(Task current, Task original) {
workManager.enqueue(
new OneTimeWorkRequest.Builder(AfterSaveWork.class)
.setInputData(AfterSaveWork.getInputData(current, original))
.build());
}
public void cleanup(List<Long> ids) {
workManager.enqueue(
new OneTimeWorkRequest.Builder(CleanupWork.class)

@ -1,37 +0,0 @@
package org.tasks.receivers;
import com.todoroo.astrid.data.SyncFlags;
import com.todoroo.astrid.data.Task;
import javax.inject.Inject;
import org.tasks.jobs.WorkManager;
import org.tasks.sync.SyncAdapters;
import timber.log.Timber;
public class PushReceiver {
private final SyncAdapters syncAdapters;
private final WorkManager workManager;
@Inject
public PushReceiver(SyncAdapters syncAdapters, WorkManager workManager) {
this.syncAdapters = syncAdapters;
this.workManager = workManager;
}
public void push(Task task, Task original) {
boolean googleTaskSyncEnabled = syncAdapters.isGoogleTaskSyncEnabled();
boolean caldavSyncEnabled = syncAdapters.isCaldavSyncEnabled();
if (!(googleTaskSyncEnabled || caldavSyncEnabled)) {
return;
}
if (task.checkTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC)) {
Timber.d("Suppressed sync: %s", task);
return;
}
if (task.checkAndClearTransitory(SyncFlags.FORCE_SYNC)
|| (googleTaskSyncEnabled && !task.googleTaskUpToDate(original))
|| (caldavSyncEnabled && !task.caldavUpToDate(original))) {
workManager.syncNow();
}
}
}
Loading…
Cancel
Save