diff --git a/app/src/main/java/foundation/e/drive/account/receivers/AccountAddedReceiver.kt b/app/src/main/java/foundation/e/drive/account/receivers/AccountAddedReceiver.kt index 2d0eb8d950f09f780d6945a3ed6ac62965e0b0b5..62190ca9910ec546a62f0e1955f518efaa0e5793 100644 --- a/app/src/main/java/foundation/e/drive/account/receivers/AccountAddedReceiver.kt +++ b/app/src/main/java/foundation/e/drive/account/receivers/AccountAddedReceiver.kt @@ -21,7 +21,7 @@ import foundation.e.drive.utils.AppConstants import foundation.e.drive.utils.DavClientProvider import foundation.e.drive.utils.RootSyncedFolderProvider import foundation.e.drive.work.WorkRequestFactory.* -import foundation.e.drive.work.WorkerUtils +import foundation.e.drive.work.WorkLauncher import timber.log.Timber /** @@ -52,7 +52,7 @@ class AccountAddedReceiver() : BroadcastReceiver() { if (registerSetupWorkers(context)) { DavClientProvider.getInstance().cleanUp() - WorkerUtils.enqueuePeriodicUserInfoFetching(WorkManager.getInstance(context)) + WorkLauncher.getInstance(context).enqueuePeriodicUserInfoFetching() } } diff --git a/app/src/main/java/foundation/e/drive/account/setup/FinishSetupWorker.java b/app/src/main/java/foundation/e/drive/account/setup/FinishSetupWorker.java index 3a8479739d8155e47e2bc4b09cf1a854547a6606..f1ed29853751feb672a22c6f1abde74ac3799fe0 100644 --- a/app/src/main/java/foundation/e/drive/account/setup/FinishSetupWorker.java +++ b/app/src/main/java/foundation/e/drive/account/setup/FinishSetupWorker.java @@ -15,12 +15,11 @@ import static foundation.e.drive.utils.AppConstants.SHARED_PREFERENCE_NAME; import android.content.Context; import androidx.annotation.NonNull; -import androidx.work.WorkManager; import androidx.work.Worker; import androidx.work.WorkerParameters; import foundation.e.drive.utils.AppConstants; -import foundation.e.drive.work.WorkerUtils; +import foundation.e.drive.work.WorkLauncher; import timber.log.Timber; /** @@ -60,9 +59,9 @@ public class FinishSetupWorker extends Worker { } private void enqueueWorkers(@NonNull final Context appContext) { - final WorkManager workManager = WorkManager.getInstance(appContext); - WorkerUtils.enqueueOneTimeAppListGenerator(workManager); - WorkerUtils.enqueueOneTimeFullScan(workManager, false); - WorkerUtils.enqueuePeriodicFullScan(workManager); + final WorkLauncher launcher = WorkLauncher.getInstance(appContext); + launcher.enqueueOneTimeAppListGenerator(); + launcher.enqueueOneTimeFullScan(false); + launcher.enqueuePeriodicFullScan(); } } \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java b/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java index ae1c037bccc532a7994f2ac58f4ddef7284f0ba7..ab0a9c4a6951ac5abc1feede8c042b96c0205c53 100644 --- a/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java @@ -23,7 +23,7 @@ import foundation.e.drive.database.DbHelper; import foundation.e.drive.synchronization.SyncProxy; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; -import foundation.e.drive.work.WorkerUtils; +import foundation.e.drive.work.WorkLauncher; import timber.log.Timber; /** @@ -113,8 +113,8 @@ public class BootCompletedReceiver extends BroadcastReceiver { final WorkManager workManager= WorkManager.getInstance(context); workManager.cancelAllWork(); - WorkerUtils.enqueuePeriodicFullScan(workManager); - WorkerUtils.enqueuePeriodicUserInfoFetching(workManager); + WorkLauncher.getInstance(context).enqueuePeriodicFullScan(); + WorkLauncher.getInstance(context).enqueuePeriodicUserInfoFetching(); } } diff --git a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java index 2235d838b4c7b836afdacea9e75e729e30111f59..412f0339e57e0481dbddd2fbcae06301f15aa137 100644 --- a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java @@ -30,7 +30,7 @@ import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.synchronization.SyncProxy; import foundation.e.drive.synchronization.SyncRequestCollector; import foundation.e.drive.utils.ReleaseTree; -import foundation.e.drive.work.WorkerUtils; +import foundation.e.drive.work.WorkLauncher; import timber.log.Timber; /** @@ -56,7 +56,7 @@ public class DebugCmdReceiver extends BroadcastReceiver { switch (intent.getAction()) { case ACTION_FORCE_SCAN: Timber.d("Force Sync intent received"); - WorkerUtils.enqueueOneTimeFullScan(workManager, true); + WorkLauncher.getInstance(context).enqueueOneTimeFullScan(true); break; case ACTION_DUMP_DATABASE: Timber.d("Dump database intent received"); diff --git a/app/src/main/java/foundation/e/drive/synchronization/SyncProxy.kt b/app/src/main/java/foundation/e/drive/synchronization/SyncProxy.kt index dbd8f5f69365c9770121f2f5139a147a85e48e89..2c676d087f7f96ae9394ddf8a288724ecc4fe720 100644 --- a/app/src/main/java/foundation/e/drive/synchronization/SyncProxy.kt +++ b/app/src/main/java/foundation/e/drive/synchronization/SyncProxy.kt @@ -9,12 +9,11 @@ package foundation.e.drive.synchronization import android.app.Application import android.content.Context -import androidx.work.WorkManager import foundation.e.drive.EdriveApplication import foundation.e.drive.database.FailedSyncPrefsManager import foundation.e.drive.models.SyncRequest import foundation.e.drive.models.SyncWrapper -import foundation.e.drive.work.WorkerUtils +import foundation.e.drive.work.WorkLauncher import timber.log.Timber import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentLinkedQueue @@ -131,7 +130,7 @@ object SyncProxy: SyncRequestCollector, SyncManager { context.startRecursiveFileObserver() if (previousSyncState != SyncState.SYNCHRONIZING) { - WorkerUtils.enqueueOneTimeSync(WorkManager.getInstance(context)) + WorkLauncher.getInstance(context).enqueueOneTimeSync(); } } diff --git a/app/src/main/java/foundation/e/drive/work/WorkerUtils.kt b/app/src/main/java/foundation/e/drive/work/WorkLauncher.kt similarity index 72% rename from app/src/main/java/foundation/e/drive/work/WorkerUtils.kt rename to app/src/main/java/foundation/e/drive/work/WorkLauncher.kt index 0575de175ce69ad8d05f17753c81ded1cb3731f7..77e4133a9f62ea97918ee97a233adbd6d05338a5 100644 --- a/app/src/main/java/foundation/e/drive/work/WorkerUtils.kt +++ b/app/src/main/java/foundation/e/drive/work/WorkLauncher.kt @@ -7,6 +7,7 @@ */ package foundation.e.drive.work +import android.content.Context import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingWorkPolicy import androidx.work.WorkManager @@ -19,26 +20,39 @@ import foundation.e.drive.work.WorkRequestFactory.WorkType /** * @author Vincent Bourgmayer */ -object WorkerUtils { +class WorkLauncher private constructor(context: Context) { + private val workManager: WorkManager - @JvmStatic - fun enqueuePeriodicUserInfoFetching(workManager: WorkManager) { + init { + workManager = WorkManager.getInstance(context) + } + + companion object { + private var instance: WorkLauncher? = null + + @JvmStatic + fun getInstance(context: Context): WorkLauncher { + return instance ?: WorkLauncher(context.applicationContext) + } + } + + fun enqueuePeriodicUserInfoFetching() { val request = WorkRequestFactory.getPeriodicWorkRequest(WorkType.PERIODIC_USER_INFO) - workManager.enqueueUniquePeriodicWork(AccountUserInfoWorker.UNIQUE_WORK_NAME, + workManager.enqueueUniquePeriodicWork( + AccountUserInfoWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.UPDATE, - request) + request + ) } - @JvmStatic - fun enqueuePeriodicFullScan(workManager: WorkManager) { + fun enqueuePeriodicFullScan() { val request = WorkRequestFactory.getPeriodicWorkRequest(WorkType.PERIODIC_SCAN) workManager.enqueueUniquePeriodicWork(PeriodicScanWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, request) } - @JvmStatic - fun enqueueOneTimeFullScan(workManager: WorkManager, isForced: Boolean) { + fun enqueueOneTimeFullScan(isForced: Boolean) { if (isForced) { val request = WorkRequestFactory.getOneTimeWorkRequest(WorkType.ONE_TIME_FORCED_FULL_SCAN, null) @@ -55,15 +69,13 @@ object WorkerUtils { request) } - @JvmStatic - fun enqueueOneTimeAppListGenerator(workManager: WorkManager) { + fun enqueueOneTimeAppListGenerator() { val request = WorkRequestFactory.getOneTimeWorkRequest(WorkType.ONE_TIME_APP_LIST, null) workManager.enqueue(request) } - @JvmStatic - fun enqueueOneTimeSync(workManager: WorkManager) { + fun enqueueOneTimeSync() { val request = WorkRequestFactory.getOneTimeWorkRequest(WorkType.ONE_TIME_SYNC, null) workManager.enqueueUniqueWork(SyncWorker.UNIQUE_WORK_NAME, ExistingWorkPolicy.KEEP, request) }