diff --git a/app/src/main/java/foundation/e/drive/periodicScan/PeriodicScanWorker.java b/app/src/main/java/foundation/e/drive/periodicScan/PeriodicScanWorker.java index ffaf89c664679dfe4d7dfe296b43381fbba9a216..bcfcfbe9fcc37e808cfe15b0e430570027a884f1 100644 --- a/app/src/main/java/foundation/e/drive/periodicScan/PeriodicScanWorker.java +++ b/app/src/main/java/foundation/e/drive/periodicScan/PeriodicScanWorker.java @@ -8,9 +8,6 @@ package foundation.e.drive.periodicScan; -import static foundation.e.drive.work.WorkRequestFactory.WorkType.ONE_TIME_APP_LIST; -import static foundation.e.drive.work.WorkRequestFactory.WorkType.ONE_TIME_FULL_SCAN; - import android.content.Context; import androidx.annotation.NonNull; @@ -23,6 +20,7 @@ import androidx.work.WorkerParameters; import java.util.ArrayList; import java.util.List; +import foundation.e.drive.work.OneTimeWorkType; import foundation.e.drive.work.WorkRequestFactory; import timber.log.Timber; @@ -43,10 +41,11 @@ public class PeriodicScanWorker extends Worker { public Result doWork() { try { final WorkManager workManager = WorkManager.getInstance(getApplicationContext()); + final WorkRequestFactory workRequestFactory = WorkRequestFactory.INSTANCE; final List workRequestsLists = new ArrayList<>(); - workRequestsLists.add(WorkRequestFactory.getOneTimeWorkRequest(ONE_TIME_APP_LIST, null)); - workRequestsLists.add(WorkRequestFactory.getOneTimeWorkRequest(ONE_TIME_FULL_SCAN, null)); + workRequestsLists.add(workRequestFactory.createOneTimeWorkRequest(OneTimeWorkType.LIST_APPS, null)); + workRequestsLists.add(workRequestFactory.createOneTimeWorkRequest(OneTimeWorkType.FULL_SCAN, null)); workManager.beginUniqueWork(FullScanWorker.UNIQUE_WORK_NAME, ExistingWorkPolicy.KEEP, workRequestsLists) .enqueue(); diff --git a/app/src/main/java/foundation/e/drive/work/WorkLauncher.kt b/app/src/main/java/foundation/e/drive/work/WorkLauncher.kt index ab0324f849e7b39fbdb0123c69f25918b174c258..21027cb9c999928fc4ccbb789967592c82158924 100644 --- a/app/src/main/java/foundation/e/drive/work/WorkLauncher.kt +++ b/app/src/main/java/foundation/e/drive/work/WorkLauncher.kt @@ -18,7 +18,6 @@ import foundation.e.drive.periodicScan.FullScanWorker import foundation.e.drive.periodicScan.PeriodicScanWorker import foundation.e.drive.synchronization.SyncWorker import foundation.e.drive.utils.RootSyncedFolderProvider -import foundation.e.drive.work.WorkRequestFactory.WorkType /** * @author Vincent Bourgmayer @@ -40,7 +39,7 @@ class WorkLauncher private constructor(context: Context) { } fun enqueuePeriodicUserInfoFetching() { - val request = WorkRequestFactory.getPeriodicWorkRequest(WorkType.PERIODIC_USER_INFO) + val request = WorkRequestFactory.createPeriodicWorkRequest(PeriodicWorkType.FETCH_USER_INFO) workManager.enqueueUniquePeriodicWork( AccountUserInfoWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.UPDATE, @@ -49,7 +48,7 @@ class WorkLauncher private constructor(context: Context) { } fun enqueuePeriodicFullScan() { - val request = WorkRequestFactory.getPeriodicWorkRequest(WorkType.PERIODIC_SCAN) + val request = WorkRequestFactory.createPeriodicWorkRequest(PeriodicWorkType.PERIODIC_SCAN) workManager.enqueueUniquePeriodicWork(PeriodicScanWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, request) @@ -57,7 +56,7 @@ class WorkLauncher private constructor(context: Context) { fun enqueueOneTimeFullScan(isForced: Boolean) { if (isForced) { - val request = WorkRequestFactory.getOneTimeWorkRequest(WorkType.ONE_TIME_FORCED_FULL_SCAN, + val request = WorkRequestFactory.createOneTimeWorkRequest(OneTimeWorkType.USER_TRIGGERED_FULL_SCAN, null) workManager.enqueueUniqueWork(FullScanWorker.UNIQUE_WORK_NAME, ExistingWorkPolicy.REPLACE, @@ -65,7 +64,7 @@ class WorkLauncher private constructor(context: Context) { return } - val request = WorkRequestFactory.getOneTimeWorkRequest(WorkType.ONE_TIME_FULL_SCAN, + val request = WorkRequestFactory.createOneTimeWorkRequest(OneTimeWorkType.FULL_SCAN, null) workManager.enqueueUniqueWork(FullScanWorker.UNIQUE_WORK_NAME, ExistingWorkPolicy.KEEP, @@ -73,13 +72,13 @@ class WorkLauncher private constructor(context: Context) { } fun enqueueOneTimeAppListGenerator() { - val request = WorkRequestFactory.getOneTimeWorkRequest(WorkType.ONE_TIME_APP_LIST, + val request = WorkRequestFactory.createOneTimeWorkRequest(OneTimeWorkType.LIST_APPS, null) workManager.enqueue(request) } fun enqueueOneTimeSync() { - val request = WorkRequestFactory.getOneTimeWorkRequest(WorkType.ONE_TIME_SYNC, null) + val request = WorkRequestFactory.createOneTimeWorkRequest(OneTimeWorkType.ONE_TIME_SYNC, null) workManager.enqueueUniqueWork(SyncWorker.UNIQUE_WORK_NAME, ExistingWorkPolicy.KEEP, request) } @@ -90,13 +89,13 @@ class WorkLauncher private constructor(context: Context) { return false } - val getUserInfoRequest = WorkRequestFactory.getOneTimeWorkRequest( - WorkType.ONE_TIME_USER_INFO, + val getUserInfoRequest = WorkRequestFactory.createOneTimeWorkRequest( + OneTimeWorkType.FETCH_USER_INFO, null ) - val finishSetupRequest = WorkRequestFactory.getOneTimeWorkRequest( - WorkType.ONE_TIME_FINISH_SETUP, + val finishSetupRequest = WorkRequestFactory.createOneTimeWorkRequest( + OneTimeWorkType.FINISH_SETUP, null ) @@ -119,8 +118,8 @@ class WorkLauncher private constructor(context: Context) { RootSyncedFolderProvider.getSyncedFolderRoots(context) return rootSyncedFolderList.map { - WorkRequestFactory.getOneTimeWorkRequest( - WorkType.ONE_TIME_ROOT_FOLDER_SETUP, + WorkRequestFactory.createOneTimeWorkRequest( + OneTimeWorkType.ROOT_FOLDER_SETUP, it ) } diff --git a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java deleted file mode 100644 index 460ed5761fb8714b00e86b32080691a92bba748b..0000000000000000000000000000000000000000 --- a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java +++ /dev/null @@ -1,280 +0,0 @@ -/* - * Copyright © MURENA SAS 2022-2023. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Public License v3.0 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/gpl.html - */ - -package foundation.e.drive.work; - -import static androidx.work.BackoffPolicy.LINEAR; -import static java.util.concurrent.TimeUnit.MINUTES; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_ENABLE; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_ID; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_LAST_ETAG; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_LAST_MODIFIED; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_LIBELLE; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_LOCAL_PATH; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_MEDIATYPE; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_REMOTE_PATH; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_SCAN_LOCAL; -import static foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_SCAN_REMOTE; -import static foundation.e.drive.utils.AppConstants.WORK_GENERIC_TAG; -import static foundation.e.drive.utils.AppConstants.WORK_SETUP_TAG; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.work.Constraints; -import androidx.work.Data; -import androidx.work.NetworkType; -import androidx.work.OneTimeWorkRequest; -import androidx.work.PeriodicWorkRequest; - -import java.security.InvalidParameterException; - -import foundation.e.drive.account.AccountUserInfoWorker; -import foundation.e.drive.account.setup.FinishSetupWorker; -import foundation.e.drive.account.setup.RootFolderSetupWorker; -import foundation.e.drive.models.SyncedFolder; -import foundation.e.drive.periodicScan.FullScanWorker; -import foundation.e.drive.periodicScan.ListAppsWorker; -import foundation.e.drive.periodicScan.PeriodicScanWorker; -import foundation.e.drive.synchronization.SyncWorker; - -public class WorkRequestFactory { - public enum WorkType { - PERIODIC_USER_INFO, - PERIODIC_SCAN, - ONE_TIME_FULL_SCAN, - ONE_TIME_FORCED_FULL_SCAN, - ONE_TIME_APP_LIST, - ONE_TIME_USER_INFO, - ONE_TIME_ROOT_FOLDER_SETUP, - ONE_TIME_FINISH_SETUP, - ONE_TIME_SYNC - } - - private final static int PERIODIC_WORK_REPEAT_INTERVAL = 30; - private final static int PERIODIC_SCAN_FLEX_TIME = 5; - - /** - * Build an instance of PeriodicWorkRequest depending of the work type specified - * @param type WorkType. Should be FULL_SCAN or PERIODIC_USER_INFO or PERIODIC_APP_LIST - * If not, it will throw an InvalidParameterException - * @return Periodic WorkRequest - */ - @NonNull - public static PeriodicWorkRequest getPeriodicWorkRequest(@NonNull WorkType type) { - switch (type) { - case PERIODIC_SCAN: - return createPeriodicScanWorkRequest(); - case PERIODIC_USER_INFO: - return createPeriodicGetUserInfoWorkRequest(); - default: - throw new InvalidParameterException("Unsupported Work Type: " + type); - } - } - - /** - * Create a PeriodicWorkRequest instance for - * a Full scan with constraints on network (should - * be unmetered) and battery (shouldn't be low) - * @return instance of PeriodicWorkRequest - */ - @NonNull - private static PeriodicWorkRequest createPeriodicScanWorkRequest() { - final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); - - final PeriodicWorkRequest.Builder workRequestBuilder = new PeriodicWorkRequest.Builder( - PeriodicScanWorker.class, - PERIODIC_WORK_REPEAT_INTERVAL, MINUTES, - PERIODIC_SCAN_FLEX_TIME, MINUTES); - - return workRequestBuilder.setConstraints(constraints) - .addTag(WORK_GENERIC_TAG) - .build(); - } - - /** - * Create a periodic work request to get userInfo - * @return instance of PeriodicWorkRequest - */ - @NonNull - private static PeriodicWorkRequest createPeriodicGetUserInfoWorkRequest() { - final Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build(); - - final PeriodicWorkRequest.Builder workRequestBuilder = new PeriodicWorkRequest.Builder(AccountUserInfoWorker.class, - PERIODIC_WORK_REPEAT_INTERVAL, MINUTES); - - return workRequestBuilder.addTag(WORK_GENERIC_TAG) - .setConstraints(constraints) - .build(); - } - - /** - * Build an instance of OneTimeWorkRequest depending of the work type specified. - * @param type Should be ONE_TIME_USER_INFO, or FIRST_START, or CREATE_REMOTE_DIR - * or it will throw InvalidParameterException - * @param syncedFolder this parameter is required for CREATE_REMOTE_DIR work type. If null it will throw an NPE. - * @return OneTimeWorkRequest's instance. - */ - @NonNull - public static OneTimeWorkRequest getOneTimeWorkRequest(@NonNull WorkType type, @Nullable SyncedFolder syncedFolder) { - switch (type) { - case ONE_TIME_APP_LIST: - return createAppListGenerationWorkRequest(); - case ONE_TIME_FULL_SCAN: - return createFullScanWorkRequest(false); - case ONE_TIME_FORCED_FULL_SCAN: - return createFullScanWorkRequest(true); - case ONE_TIME_USER_INFO: - return createGetUserInfoWorkRequest(); - case ONE_TIME_FINISH_SETUP: - return createFinishSetupWorkRequest(); - case ONE_TIME_ROOT_FOLDER_SETUP: - if (syncedFolder == null) throw new NullPointerException("Synced folder is null"); - return createRootFolderSetupWorkRequest(syncedFolder); - case ONE_TIME_SYNC: - return createSyncWorkRequest(); - default: - throw new InvalidParameterException("Unsupported Work Type: " + type); - } - } - - private static OneTimeWorkRequest createSyncWorkRequest() { - final Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .setRequiresBatteryNotLow(true) - .build(); - - final OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(SyncWorker.class); - - return builder.setBackoffCriteria(LINEAR, 2, MINUTES).addTag(WORK_GENERIC_TAG) - .setConstraints(constraints) - .build(); - } - - /** - * Create a workRequest to generate file which contains list of installed apps - * @return the workRequest - */ - @NonNull - private static OneTimeWorkRequest createAppListGenerationWorkRequest() { - final OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(ListAppsWorker.class); - - return builder.setBackoffCriteria(LINEAR, 2, MINUTES) - .addTag(WORK_GENERIC_TAG) - .build(); - } - - /** - * Create a OneTimeWorkRequest instance for - * a Full scan with constraints on network (should - * be unmetered) and battery (shouldn't be low) - * @return instance of OneTimeWorkRequest - */ - @NonNull - private static OneTimeWorkRequest createFullScanWorkRequest(boolean forced) { - final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); - - final OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(FullScanWorker.class); - - final Data data = new Data.Builder() - .putBoolean(FullScanWorker.ACTION_FORCED_SYNC_KEY, forced) - .build(); - - return builder.setBackoffCriteria(LINEAR, 2, MINUTES) - .setConstraints(constraints) - .setInputData(data) - .addTag(WORK_GENERIC_TAG) - .build(); - } - - - /** - * Instanciate a OneTimeWorkRequest to retrieve user info - * @return instance of OneTimeWorkRequest - */ - @NonNull - private static OneTimeWorkRequest createGetUserInfoWorkRequest() { - final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); - - final OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(AccountUserInfoWorker.class); - - return builder.setBackoffCriteria(LINEAR, 2, MINUTES) - .addTag(WORK_GENERIC_TAG) - .addTag(WORK_SETUP_TAG) - .setConstraints(constraints) - .build(); - } - - /** - * Create a OneTime workRequest to create a remote folder - * With constraints on network (unmetered) and battery (not low) - * @param syncedFolder SyncedFolder instance with data about folder to create - * @return Instance OneTimeWorkRequest - */ - @NonNull - private static OneTimeWorkRequest createRootFolderSetupWorkRequest(@NonNull SyncedFolder syncedFolder) { - final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); - - return new OneTimeWorkRequest.Builder( - RootFolderSetupWorker.class) - .setBackoffCriteria(LINEAR, 2, MINUTES) - .setInputData(createDataFromSyncedFolder(syncedFolder)) - .addTag(WORK_GENERIC_TAG) - .addTag(WORK_SETUP_TAG) - .setConstraints(constraints) - .build(); - } - - /** - * Create a OneTime WorkRequest which finish setup process - * @return Instance of OneTimeWorkRequest - */ - @NonNull - private static OneTimeWorkRequest createFinishSetupWorkRequest() { - return new OneTimeWorkRequest.Builder(FinishSetupWorker.class) - .setBackoffCriteria(LINEAR, 2, MINUTES) - .addTag(WORK_GENERIC_TAG) - .addTag(WORK_SETUP_TAG) - .build(); - } - - /** - * Create Constraints for unmetered network - * and battery not low - * @return instance of Constraints - */ - @NonNull - private static Constraints createUnmeteredNetworkAndHighBatteryConstraints() { - return new Constraints.Builder() - .setRequiredNetworkType(NetworkType.UNMETERED) - .setRequiresBatteryNotLow(true) - .build(); - } - - /** - * Parse SyncedFolder instance in Data, used as data for WorkRequest - * @param folder SyncedFolder instance - * @return Data instance - */ - @NonNull - private static Data createDataFromSyncedFolder(@NonNull SyncedFolder folder) { - return new Data.Builder() - .putInt(DATA_KEY_ID, folder.getId()) - .putString(DATA_KEY_LIBELLE, folder.getLibelle()) - .putString(DATA_KEY_LOCAL_PATH, folder.getLocalFolder()) - .putString(DATA_KEY_REMOTE_PATH, folder.getRemoteFolder()) - .putString(DATA_KEY_LAST_ETAG, folder.getLastEtag()) - .putLong(DATA_KEY_LAST_MODIFIED, folder.getLastModified()) - .putBoolean(DATA_KEY_SCAN_LOCAL, folder.isScanLocal()) - .putBoolean(DATA_KEY_SCAN_REMOTE, folder.isScanRemote()) - .putBoolean(DATA_KEY_ENABLE, folder.isEnabled()) - .putBoolean(DATA_KEY_MEDIATYPE, folder.isMediaType()) - .build(); - } -} \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.kt b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.kt new file mode 100644 index 0000000000000000000000000000000000000000..0c3b245b806627a42bfbce23fb1c4a1949c6e064 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.kt @@ -0,0 +1,247 @@ +/* + * Copyright © MURENA SAS 2022-2024. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ +package foundation.e.drive.work + +import androidx.work.BackoffPolicy.LINEAR +import androidx.work.Constraints +import androidx.work.Data +import androidx.work.NetworkType +import androidx.work.OneTimeWorkRequest +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.PeriodicWorkRequest +import androidx.work.PeriodicWorkRequestBuilder +import foundation.e.drive.account.AccountUserInfoWorker +import foundation.e.drive.account.setup.FinishSetupWorker +import foundation.e.drive.account.setup.RootFolderSetupWorker +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_ENABLE +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_ID +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_LAST_ETAG +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_LAST_MODIFIED +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_LIBELLE +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_LOCAL_PATH +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_MEDIATYPE +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_REMOTE_PATH +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_SCAN_LOCAL +import foundation.e.drive.account.setup.RootFolderSetupWorker.DATA_KEY_SCAN_REMOTE +import foundation.e.drive.models.SyncedFolder +import foundation.e.drive.periodicScan.FullScanWorker +import foundation.e.drive.periodicScan.ListAppsWorker +import foundation.e.drive.periodicScan.PeriodicScanWorker +import foundation.e.drive.synchronization.SyncWorker +import foundation.e.drive.utils.AppConstants.WORK_GENERIC_TAG +import foundation.e.drive.utils.AppConstants.WORK_SETUP_TAG +import java.util.concurrent.TimeUnit.MINUTES + + +enum class PeriodicWorkType { + FETCH_USER_INFO, + PERIODIC_SCAN} + +enum class OneTimeWorkType { + FULL_SCAN, + USER_TRIGGERED_FULL_SCAN, + LIST_APPS, + FETCH_USER_INFO, + ROOT_FOLDER_SETUP, + FINISH_SETUP, + ONE_TIME_SYNC +} + +object WorkRequestFactory { + + private const val PERIODIC_WORK_REPEAT_INTERVAL_IN_MIN = 30L + private const val PERIODIC_SCAN_FLEX_TIME_IN_MIN = 5L + + /** + * Builds an instance of PeriodicWorkRequest depending of the work type specified + * @param type WorkType. Should be PERIODIC_SCAN or PERIODIC_USER_INFO + * @return Periodic WorkRequest + */ + fun createPeriodicWorkRequest(type: PeriodicWorkType): PeriodicWorkRequest { + return when (type) { + PeriodicWorkType.FETCH_USER_INFO -> createPeriodicGetUserInfoWorkRequest() + PeriodicWorkType.PERIODIC_SCAN -> createPeriodicScanWorkRequest() + } + } + + /** + * Builds an instance of OneTimeWorkRequest depending of the work type specified. + * @param type Should be ONE_TIME_USER_INFO, or FIRST_START, or CREATE_REMOTE_DIR + * or it will throw InvalidParameterException + * @param syncedFolder @Nullable this parameter is required for CREATE_REMOTE_DIR work type. Throw an NPE if null. + * @return OneTimeWorkRequest's instance. + */ + fun createOneTimeWorkRequest(type: OneTimeWorkType, syncedFolder: SyncedFolder? = null): OneTimeWorkRequest { + return when (type) { + OneTimeWorkType.FULL_SCAN -> createFullScanWorkRequest(false) + OneTimeWorkType.USER_TRIGGERED_FULL_SCAN -> createFullScanWorkRequest(true) + OneTimeWorkType.LIST_APPS -> createAppListGenerationWorkRequest() + OneTimeWorkType.FETCH_USER_INFO -> createGetUserInfoWorkRequest() + OneTimeWorkType.ROOT_FOLDER_SETUP -> { + if (syncedFolder == null) { + throw NullPointerException("Cannot create RootFolderSetupWorker without syncFolder") + } + createRootFolderSetupWorkRequest(syncedFolder) + } + OneTimeWorkType.FINISH_SETUP -> createFinishSetupWorkRequest() + OneTimeWorkType.ONE_TIME_SYNC -> createSyncWorkRequest() + } + } + + /** + * Creates a OneTimeWorkRequest instance for + * a Full scan with constraints on network (CONNECTED) and battery (shouldn't be low) + * @return instance of OneTimeWorkRequest + */ + private fun createFullScanWorkRequest(forced: Boolean): OneTimeWorkRequest { + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .setRequiresBatteryNotLow(true) + .build() + + val data = Data.Builder() + .putBoolean(FullScanWorker.ACTION_FORCED_SYNC_KEY, forced) + .build() + + return OneTimeWorkRequestBuilder() + .setBackoffCriteria(LINEAR, 2, MINUTES) + .setConstraints(constraints) + .setInputData(data) + .addTag(WORK_GENERIC_TAG) + .build() + } + + /** + * Creates a workRequest to generate file which contains list of installed apps + * @return the workRequest + */ + private fun createAppListGenerationWorkRequest(): OneTimeWorkRequest { + return OneTimeWorkRequestBuilder() + .setBackoffCriteria(LINEAR, 2, MINUTES) + .addTag(WORK_GENERIC_TAG) + .build() + + } + + /** + * Instantiates a OneTimeWorkRequest to retrieve user info + * @return instance of OneTimeWorkRequest + */ + private fun createGetUserInfoWorkRequest(): OneTimeWorkRequest { + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .setRequiresBatteryNotLow(true) + .build() + + return OneTimeWorkRequestBuilder() + .setConstraints(constraints) + .setBackoffCriteria(LINEAR, 2, MINUTES) + .addTag(WORK_GENERIC_TAG) + .addTag(WORK_SETUP_TAG) + .build() + } + + /** + * Creates a OneTime workRequest to create a remote folder + * With constraints on network (unmetered) and battery (not low) + * @param syncedFolder SyncedFolder instance with data about folder to create + * @return Instance OneTimeWorkRequest + */ + private fun createRootFolderSetupWorkRequest(syncedFolder: SyncedFolder): OneTimeWorkRequest { + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.UNMETERED) + .setRequiresBatteryNotLow(true) + .build() + + return OneTimeWorkRequestBuilder() + .setInputData(createDataFromSyncedFolder(syncedFolder)) + .setConstraints(constraints) + .setBackoffCriteria(LINEAR, 2, MINUTES) + .addTag(WORK_GENERIC_TAG) + .addTag(WORK_SETUP_TAG) + .build() + } + + /** + * Parses SyncedFolder instance in Data, used as data for WorkRequest + * @param folder SyncedFolder instance + * @return Data instance + */ + private fun createDataFromSyncedFolder(folder: SyncedFolder): Data { + return Data.Builder() + .putInt(DATA_KEY_ID, folder.getId()) + .putString(DATA_KEY_LIBELLE, folder.getLibelle()) + .putString(DATA_KEY_LOCAL_PATH, folder.getLocalFolder()) + .putString(DATA_KEY_REMOTE_PATH, folder.getRemoteFolder()) + .putString(DATA_KEY_LAST_ETAG, folder.getLastEtag()) + .putLong(DATA_KEY_LAST_MODIFIED, folder.getLastModified()) + .putBoolean(DATA_KEY_SCAN_LOCAL, folder.isScanLocal()) + .putBoolean(DATA_KEY_SCAN_REMOTE, folder.isScanRemote()) + .putBoolean(DATA_KEY_ENABLE, folder.isEnabled()) + .putBoolean(DATA_KEY_MEDIATYPE, folder.isMediaType()) + .build() + + } + + /** + * Creates a OneTime WorkRequest which finish setup process + * @return Instance of OneTimeWorkRequest + */ + private fun createFinishSetupWorkRequest(): OneTimeWorkRequest { + return OneTimeWorkRequestBuilder() + .setBackoffCriteria(LINEAR, 2, MINUTES) + .addTag(WORK_GENERIC_TAG) + .addTag(WORK_SETUP_TAG) + .build() + + } + + private fun createSyncWorkRequest(): OneTimeWorkRequest { + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .setRequiresBatteryNotLow(true) + .build() + + return OneTimeWorkRequestBuilder() + .setBackoffCriteria(LINEAR, 2, MINUTES) + .setConstraints(constraints) + .addTag(WORK_GENERIC_TAG) + .build() + } + + /** + * Creates a PeriodicWorkRequest instance for + * a Full scan with constraints on network (CONNECTED) and battery (not low) + * @return instance of PeriodicWorkRequest + */ + private fun createPeriodicScanWorkRequest(): PeriodicWorkRequest { + val constraints = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .setRequiresBatteryNotLow(true) + .build() + + return PeriodicWorkRequestBuilder( + PERIODIC_WORK_REPEAT_INTERVAL_IN_MIN, MINUTES, + PERIODIC_SCAN_FLEX_TIME_IN_MIN,MINUTES) + .setConstraints(constraints) + .addTag(WORK_GENERIC_TAG) + .build() + } + + private fun createPeriodicGetUserInfoWorkRequest(): PeriodicWorkRequest { + val constraint = Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build() + + return PeriodicWorkRequestBuilder( + PERIODIC_WORK_REPEAT_INTERVAL_IN_MIN, MINUTES) + .setConstraints(constraint) + .addTag(WORK_GENERIC_TAG) + .build() + } +}