From 45430d75323531ddc1d835ea5a9796ea68a34159 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 19 Oct 2018 12:54:59 -0500 Subject: [PATCH] Update to work manager alpha 10 --- app/build.gradle | 12 +++- .../org/tasks/jobs/BackupServiceTests.java | 9 +-- .../org/tasks/backup/TasksJsonExporter.java | 55 ++++++++++--------- .../org/tasks/injection/InjectingWorker.java | 6 ++ .../main/java/org/tasks/jobs/BackupWork.java | 11 ++-- .../main/java/org/tasks/jobs/CleanupWork.java | 6 ++ .../org/tasks/jobs/MidnightRefreshWork.java | 7 +++ .../java/org/tasks/jobs/NotificationWork.java | 7 ++- .../main/java/org/tasks/jobs/RefreshWork.java | 12 +++- .../java/org/tasks/jobs/RepeatingWorker.java | 6 ++ .../main/java/org/tasks/jobs/SyncWork.java | 6 ++ 11 files changed, 90 insertions(+), 47 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0b6dd1f2e..99a9136bf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -112,7 +112,7 @@ final SUPPORT_VERSION = '28.0.0' final ROOM_VERSION = '1.1.1' final STETHO_VERSION = '1.5.0' final TESTING_SUPPORT_VERSION = '1.0.0' -final WORK_VERSION = '1.0.0-alpha06' +final WORK_VERSION = '1.0.0-alpha10' final LEAKCANARY_VERSION = '1.6.1' dependencies { @@ -169,9 +169,15 @@ dependencies { implementation 'com.google.apis:google-api-services-tasks:v1-rev52-1.23.0' implementation 'com.google.api-client:google-api-client-android:1.23.0' implementation 'com.android.billingclient:billing:1.1' - implementation "android.arch.work:work-runtime:${WORK_VERSION}" + implementation("android.arch.work:work-runtime:${WORK_VERSION}") { + // https://groups.google.com/forum/#!topic/guava-announce/Km82fZG68Sw + exclude group: 'com.google.guava', module: 'listenablefuture' + } - googleplayImplementation "android.arch.work:work-firebase:${WORK_VERSION}" + googleplayImplementation("android.arch.work:work-firebase:${WORK_VERSION}") { + // https://groups.google.com/forum/#!topic/guava-announce/Km82fZG68Sw + exclude group: 'com.google.guava', module: 'listenablefuture' + } googleplayImplementation 'com.crashlytics.sdk.android:crashlytics:2.9.5' googleplayImplementation "com.google.firebase:firebase-core:16.0.4" googleplayImplementation "com.google.android.gms:play-services-location:16.0.0" diff --git a/app/src/androidTest/java/org/tasks/jobs/BackupServiceTests.java b/app/src/androidTest/java/org/tasks/jobs/BackupServiceTests.java index 94bd0e2ae..8c52c527a 100644 --- a/app/src/androidTest/java/org/tasks/jobs/BackupServiceTests.java +++ b/app/src/androidTest/java/org/tasks/jobs/BackupServiceTests.java @@ -10,7 +10,6 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; import android.support.test.runner.AndroidJUnit4; -import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.data.Task; import java.io.File; @@ -21,6 +20,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.tasks.R; import org.tasks.backup.TasksJsonExporter; +import org.tasks.backup.TasksJsonExporter.ExportType; import org.tasks.injection.InjectingTestCase; import org.tasks.injection.TestComponent; import org.tasks.preferences.Preferences; @@ -28,7 +28,6 @@ import org.tasks.preferences.Preferences; @RunWith(AndroidJUnit4.class) public class BackupServiceTests extends InjectingTestCase { - private static final long BACKUP_WAIT_TIME = 500L; @Inject TasksJsonExporter jsonExporter; @Inject TaskDao taskDao; @Inject Preferences preferences; @@ -80,11 +79,7 @@ public class BackupServiceTests extends InjectingTestCase { public void testBackup() { assertEquals(0, temporaryDirectory.list().length); - // create a backup - BackupWork service = new BackupWork(getTargetContext(), jsonExporter, preferences); - service.startBackup(getTargetContext()); - - AndroidUtilities.sleepDeep(BACKUP_WAIT_TIME); + jsonExporter.exportTasks(getTargetContext(), ExportType.EXPORT_TYPE_SERVICE, null); // assert file created File[] files = temporaryDirectory.listFiles(); diff --git a/app/src/main/java/org/tasks/backup/TasksJsonExporter.java b/app/src/main/java/org/tasks/backup/TasksJsonExporter.java index 60435ee9a..26fb59bd3 100755 --- a/app/src/main/java/org/tasks/backup/TasksJsonExporter.java +++ b/app/src/main/java/org/tasks/backup/TasksJsonExporter.java @@ -118,36 +118,39 @@ public class TasksJsonExporter { this.latestSetVersionName = null; this.progressDialog = progressDialog; - handler = exportType == ExportType.EXPORT_TYPE_MANUAL ? new Handler() : null; + if (exportType == ExportType.EXPORT_TYPE_MANUAL) { + handler = new Handler(); + new Thread(() -> runBackup(exportType)).start(); + } else { + runBackup(exportType); + } + } - new Thread( - () -> { - try { - String output = setupFile(backupDirectory, exportType); + private void runBackup(ExportType exportType) { + try { + String output = setupFile(backupDirectory, exportType); - List tasks = taskDao.getAll(); + List tasks = taskDao.getAll(); - if (tasks.size() > 0) { - doTasksExport(output, tasks); - } + if (tasks.size() > 0) { + doTasksExport(output, tasks); + } - if (exportType == ExportType.EXPORT_TYPE_MANUAL) { - onFinishExport(output); - } - } catch (IOException e) { - Timber.e(e); - } finally { - post( - () -> { - if (progressDialog != null - && progressDialog.isShowing() - && context instanceof Activity) { - DialogUtilities.dismissDialog((Activity) context, progressDialog); - } - }); - } - }) - .start(); + if (exportType == ExportType.EXPORT_TYPE_MANUAL) { + onFinishExport(output); + } + } catch (IOException e) { + Timber.e(e); + } finally { + post( + () -> { + if (progressDialog != null + && progressDialog.isShowing() + && context instanceof Activity) { + DialogUtilities.dismissDialog((Activity) context, progressDialog); + } + }); + } } private void doTasksExport(String output, List tasks) throws IOException { diff --git a/app/src/main/java/org/tasks/injection/InjectingWorker.java b/app/src/main/java/org/tasks/injection/InjectingWorker.java index ca5bc1061..b2e37ba40 100644 --- a/app/src/main/java/org/tasks/injection/InjectingWorker.java +++ b/app/src/main/java/org/tasks/injection/InjectingWorker.java @@ -1,7 +1,9 @@ package org.tasks.injection; +import android.content.Context; import android.support.annotation.NonNull; import androidx.work.Worker; +import androidx.work.WorkerParameters; import javax.inject.Inject; import org.tasks.analytics.Tracker; import timber.log.Timber; @@ -10,6 +12,10 @@ public abstract class InjectingWorker extends Worker { @Inject Tracker tracker; + public InjectingWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + @NonNull @Override public Result doWork() { diff --git a/app/src/main/java/org/tasks/jobs/BackupWork.java b/app/src/main/java/org/tasks/jobs/BackupWork.java index a1343a6b5..a075acc61 100644 --- a/app/src/main/java/org/tasks/jobs/BackupWork.java +++ b/app/src/main/java/org/tasks/jobs/BackupWork.java @@ -6,6 +6,8 @@ import static com.todoroo.andlib.utility.DateUtilities.now; import static java.util.Collections.emptyList; import android.content.Context; +import android.support.annotation.NonNull; +import androidx.work.WorkerParameters; import java.io.File; import java.io.FileFilter; import java.util.Arrays; @@ -33,13 +35,8 @@ public class BackupWork extends RepeatingWorker { @Inject Preferences preferences; @Inject WorkManager workManager; - @SuppressWarnings("unused") - public BackupWork() {} - - BackupWork(Context context, TasksJsonExporter tasksJsonExporter, Preferences preferences) { - this.context = context; - this.tasksJsonExporter = tasksJsonExporter; - this.preferences = preferences; + public BackupWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); } @Override diff --git a/app/src/main/java/org/tasks/jobs/CleanupWork.java b/app/src/main/java/org/tasks/jobs/CleanupWork.java index db21eb7ce..6c85a529a 100644 --- a/app/src/main/java/org/tasks/jobs/CleanupWork.java +++ b/app/src/main/java/org/tasks/jobs/CleanupWork.java @@ -1,6 +1,8 @@ package org.tasks.jobs; +import android.content.Context; import android.support.annotation.NonNull; +import androidx.work.WorkerParameters; import com.todoroo.astrid.alarms.AlarmService; import com.todoroo.astrid.reminders.ReminderService; import com.todoroo.astrid.timers.TimerPlugin; @@ -21,6 +23,10 @@ public class CleanupWork extends InjectingWorker { @Inject ReminderService reminderService; @Inject AlarmService alarmService; + public CleanupWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + @NonNull @Override public Result run() { diff --git a/app/src/main/java/org/tasks/jobs/MidnightRefreshWork.java b/app/src/main/java/org/tasks/jobs/MidnightRefreshWork.java index 88ce94b03..0311e3766 100644 --- a/app/src/main/java/org/tasks/jobs/MidnightRefreshWork.java +++ b/app/src/main/java/org/tasks/jobs/MidnightRefreshWork.java @@ -1,5 +1,8 @@ package org.tasks.jobs; +import android.content.Context; +import android.support.annotation.NonNull; +import androidx.work.WorkerParameters; import javax.inject.Inject; import org.tasks.LocalBroadcastManager; import org.tasks.injection.JobComponent; @@ -9,6 +12,10 @@ public class MidnightRefreshWork extends RepeatingWorker { @Inject WorkManager workManager; @Inject LocalBroadcastManager localBroadcastManager; + public MidnightRefreshWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + @Override protected Result run() { localBroadcastManager.broadcastRefresh(); diff --git a/app/src/main/java/org/tasks/jobs/NotificationWork.java b/app/src/main/java/org/tasks/jobs/NotificationWork.java index 10b1c7778..9683a1da5 100644 --- a/app/src/main/java/org/tasks/jobs/NotificationWork.java +++ b/app/src/main/java/org/tasks/jobs/NotificationWork.java @@ -1,11 +1,12 @@ package org.tasks.jobs; +import android.content.Context; import android.support.annotation.NonNull; +import androidx.work.WorkerParameters; import java.util.List; import javax.inject.Inject; import org.tasks.BuildConfig; import org.tasks.Notifier; -import org.tasks.injection.InjectingWorker; import org.tasks.injection.JobComponent; import org.tasks.preferences.Preferences; @@ -15,6 +16,10 @@ public class NotificationWork extends RepeatingWorker { @Inject Notifier notifier; @Inject NotificationQueue notificationQueue; + public NotificationWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + @NonNull @Override public Result run() { diff --git a/app/src/main/java/org/tasks/jobs/RefreshWork.java b/app/src/main/java/org/tasks/jobs/RefreshWork.java index 30dc51669..879439b7f 100644 --- a/app/src/main/java/org/tasks/jobs/RefreshWork.java +++ b/app/src/main/java/org/tasks/jobs/RefreshWork.java @@ -1,9 +1,10 @@ package org.tasks.jobs; +import android.content.Context; import android.support.annotation.NonNull; +import androidx.work.WorkerParameters; import javax.inject.Inject; import org.tasks.LocalBroadcastManager; -import org.tasks.injection.InjectingWorker; import org.tasks.injection.JobComponent; import org.tasks.scheduling.RefreshScheduler; @@ -12,11 +13,16 @@ public class RefreshWork extends RepeatingWorker { @Inject RefreshScheduler refreshScheduler; @Inject LocalBroadcastManager localBroadcastManager; + public RefreshWork(@NonNull Context context, + @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + @NonNull @Override public Result run() { - localBroadcastManager.broadcastRefresh(); - return Result.SUCCESS; + localBroadcastManager.broadcastRefresh(); + return Result.SUCCESS; } @Override diff --git a/app/src/main/java/org/tasks/jobs/RepeatingWorker.java b/app/src/main/java/org/tasks/jobs/RepeatingWorker.java index af08a0bcf..47a4b0267 100644 --- a/app/src/main/java/org/tasks/jobs/RepeatingWorker.java +++ b/app/src/main/java/org/tasks/jobs/RepeatingWorker.java @@ -1,10 +1,16 @@ package org.tasks.jobs; +import android.content.Context; import android.support.annotation.NonNull; +import androidx.work.WorkerParameters; import org.tasks.injection.InjectingWorker; public abstract class RepeatingWorker extends InjectingWorker { + public RepeatingWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + @NonNull @Override public final Result doWork() { diff --git a/app/src/main/java/org/tasks/jobs/SyncWork.java b/app/src/main/java/org/tasks/jobs/SyncWork.java index 050623b89..5b95d3cf8 100644 --- a/app/src/main/java/org/tasks/jobs/SyncWork.java +++ b/app/src/main/java/org/tasks/jobs/SyncWork.java @@ -1,6 +1,8 @@ package org.tasks.jobs; +import android.content.Context; import android.support.annotation.NonNull; +import androidx.work.WorkerParameters; import javax.inject.Inject; import org.tasks.LocalBroadcastManager; import org.tasks.caldav.CaldavSynchronizer; @@ -19,6 +21,10 @@ public class SyncWork extends InjectingWorker { @Inject LocalBroadcastManager localBroadcastManager; @Inject Preferences preferences; + public SyncWork(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + @NonNull @Override public Result run() {