From e072b42465a12d73d4a7c62212d8879d2cb89cb4 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 25 Aug 2022 10:32:47 +0200 Subject: [PATCH 1/3] Implement WorkRequestFactory to clean code from CommonUtils --- .../foundation/e/drive/utils/CommonUtils.java | 98 ++---------- .../e/drive/work/WorkRequestFactory.java | 148 ++++++++++++++++++ 2 files changed, 161 insertions(+), 85 deletions(-) create mode 100644 app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java diff --git a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java index c909dc6d..5a24faf4 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -39,35 +39,19 @@ import java.text.StringCharacterIterator; import java.util.Locale; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; import foundation.e.drive.R; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.work.AccountUserInfoWorker; -import foundation.e.drive.work.CreateRemoteFolderWorker; -import foundation.e.drive.work.FirstStartWorker; import foundation.e.drive.work.FullScanWorker; +import foundation.e.drive.work.WorkRequestFactory; import static foundation.e.drive.utils.AppConstants.MEDIASYNC_PROVIDER_AUTHORITY; import static foundation.e.drive.utils.AppConstants.METERED_NETWORK_ALLOWED_AUTHORITY; import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORITY; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_ENABLE; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_ID; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_LAST_ETAG; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_LAST_MODIFIED; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_LIBELLE; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_LOCAL_PATH; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_MEDIATYPE; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_REMOTE_PATH; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_SCAN_LOCAL; -import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_SCAN_REMOTE; import androidx.annotation.NonNull; -import androidx.work.BackoffPolicy; -import androidx.work.Constraints; -import androidx.work.Data; import androidx.work.ExistingPeriodicWorkPolicy; -import androidx.work.NetworkType; import androidx.work.OneTimeWorkRequest; import androidx.work.PeriodicWorkRequest; import androidx.work.WorkManager; @@ -323,20 +307,9 @@ public abstract class CommonUtils { * @param workManager the instance of workManager */ public static void registerPeriodicFullScanWorker(WorkManager workManager) { - final Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.UNMETERED) - .setRequiresBatteryNotLow(true) - .build(); - - final PeriodicWorkRequest periodicFullScanRequest = - new PeriodicWorkRequest.Builder(FullScanWorker.class, - 31, TimeUnit.MINUTES, - 5, TimeUnit.MINUTES) - .setConstraints(constraints) - .addTag(AppConstants.WORK_GENERIC_TAG) - .build(); - - workManager.enqueueUniquePeriodicWork(FullScanWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, periodicFullScanRequest); + workManager.enqueueUniquePeriodicWork(FullScanWorker.UNIQUE_WORK_NAME, + ExistingPeriodicWorkPolicy.KEEP, + new WorkRequestFactory().createPeriodicFullScanWorkRequest()); } @@ -360,39 +333,18 @@ public abstract class CommonUtils { Log.e(TAG, "Can't create remote folders. List is empty"); return; } + + final WorkRequestFactory factory = new WorkRequestFactory(); - final Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.UNMETERED) - .setRequiresBatteryNotLow(true) - .build(); - - final OneTimeWorkRequest getUserInfoRequest = new OneTimeWorkRequest.Builder(AccountUserInfoWorker.class) - .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) - .addTag(AppConstants.WORK_GENERIC_TAG) - .addTag(AppConstants.WORK_INITIALIZATION_TAG) - .setConstraints(constraints) - .build(); + final OneTimeWorkRequest getUserInfoRequest = factory.createOneTimeGetUserInfoWorkRequest(); final List workRequests = new ArrayList<>(); - for (SyncedFolder folder : syncedFolders) { - final OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder( - CreateRemoteFolderWorker.class) - .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) - .setInputData(createDataFromSyncedFolder(folder)) - .addTag(AppConstants.WORK_GENERIC_TAG) - .addTag(AppConstants.WORK_INITIALIZATION_TAG) - .setConstraints(constraints) - .build(); - - workRequests.add(oneTimeWorkRequest); + final OneTimeWorkRequest createRemoteFolderWorkRequest = factory.createOneTimeCreateRemoteFolderWorkRequest(folder); + workRequests.add(createRemoteFolderWorkRequest); } - final OneTimeWorkRequest firstStartRequest = new OneTimeWorkRequest.Builder(FirstStartWorker.class) - .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) - .addTag(AppConstants.WORK_GENERIC_TAG) - .addTag(AppConstants.WORK_INITIALIZATION_TAG) - .build(); + final OneTimeWorkRequest firstStartRequest = factory.createOneTimeFirstStartWorkRequest(); workManager.beginWith(getUserInfoRequest) .then(workRequests) @@ -401,21 +353,6 @@ public abstract class CommonUtils { } - private static Data createDataFromSyncedFolder(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(); - } - public static void createNotificationChannel(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { final CharSequence name = context.getString(R.string.notif_channel_name); @@ -446,18 +383,9 @@ public abstract class CommonUtils { * @param workManager */ public static void registerPeriodicUserInfoChecking(WorkManager workManager) { - final Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build(); - - final PeriodicWorkRequest periodicUserInfoScanRequest = - new PeriodicWorkRequest.Builder(AccountUserInfoWorker.class, - 30, TimeUnit.MINUTES) - .addTag(AppConstants.WORK_GENERIC_TAG) - .setConstraints(constraints) - .build(); - - workManager.enqueueUniquePeriodicWork(AccountUserInfoWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, periodicUserInfoScanRequest); + final WorkRequestFactory factory = new WorkRequestFactory(); + PeriodicWorkRequest workRequest = factory.createPeriodicGetUserInfoWorkRequest(); + workManager.enqueueUniquePeriodicWork(AccountUserInfoWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, workRequest); } /** diff --git a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java new file mode 100644 index 00000000..069a22f9 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java @@ -0,0 +1,148 @@ +package foundation.e.drive.work; + +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_ENABLE; +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_ID; +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_LAST_ETAG; +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_LAST_MODIFIED; +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_LIBELLE; +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_LOCAL_PATH; +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_MEDIATYPE; +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_REMOTE_PATH; +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_SCAN_LOCAL; +import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_SCAN_REMOTE; + +import androidx.work.BackoffPolicy; +import androidx.work.Constraints; +import androidx.work.Data; +import androidx.work.NetworkType; +import androidx.work.OneTimeWorkRequest; +import androidx.work.PeriodicWorkRequest; + +import java.util.concurrent.TimeUnit; + +import foundation.e.drive.models.SyncedFolder; +import foundation.e.drive.utils.AppConstants; + +public class WorkRequestFactory { + + /** + * Create a PeridocWorkRequest instance for + * a Full scan with constraints on network (should + * be unmetered) and battery (shouldn't be low) + * @return instance of PeriodicWorkRequest + */ + public PeriodicWorkRequest createPeriodicFullScanWorkRequest() { + final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); + + final PeriodicWorkRequest workRequest = + new PeriodicWorkRequest.Builder(FullScanWorker.class, + 31, TimeUnit.MINUTES, + 5, TimeUnit.MINUTES) + .setConstraints(constraints) + .addTag(AppConstants.WORK_GENERIC_TAG) + .build(); + return workRequest; + } + + /** + * Create a periodic work request to get userInfo + * @return instance of PeriodicWorkRequest + */ + public PeriodicWorkRequest createPeriodicGetUserInfoWorkRequest() { + final Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(); + + final PeriodicWorkRequest workRequest = + new PeriodicWorkRequest.Builder(AccountUserInfoWorker.class, + 30, TimeUnit.MINUTES) + .addTag(AppConstants.WORK_GENERIC_TAG) + .setConstraints(constraints) + .build(); + return workRequest; + } + + /** + * Instanciate a OneTimeWorkRequest to retrieve user info + * @return instance of OneTimeWorkRequest + */ + public OneTimeWorkRequest createOneTimeGetUserInfoWorkRequest() { + final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); + + final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(AccountUserInfoWorker.class) + .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) + .addTag(AppConstants.WORK_GENERIC_TAG) + .addTag(AppConstants.WORK_INITIALIZATION_TAG) + .setConstraints(constraints) + .build(); + return workRequest; + } + + /** + * 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 + */ + public OneTimeWorkRequest createOneTimeCreateRemoteFolderWorkRequest(SyncedFolder syncedFolder) { + final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); + + final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder( + CreateRemoteFolderWorker.class) + .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) + .setInputData(createDataFromSyncedFolder(syncedFolder)) + .addTag(AppConstants.WORK_GENERIC_TAG) + .addTag(AppConstants.WORK_INITIALIZATION_TAG) + .setConstraints(constraints) + .build(); + return workRequest; + } + + /** + * Create a OneTime WorkRequest which start eDrive + * after initialization + * @return Instance of OneTimeWorkRequest + */ + public OneTimeWorkRequest createOneTimeFirstStartWorkRequest() { + final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(FirstStartWorker.class) + .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) + .addTag(AppConstants.WORK_GENERIC_TAG) + .addTag(AppConstants.WORK_INITIALIZATION_TAG) + .build(); + return workRequest; + } + + /** + * Create Constraints for unmetered network + * and battery not low + * @return instance of Constraints + */ + private Constraints createUnmeteredNetworkAndHighBatteryConstraints() { + final Constraints constraint = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.UNMETERED) + .setRequiresBatteryNotLow(true) + .build(); + return constraint; + } + + + /** + * Parse SyncedFolder instance in Data, used as data for WorkRequest + * @param folder SyncedFolder instance + * @return Data instance + */ + private Data createDataFromSyncedFolder(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(); + } +} -- GitLab From 6fb91ab3710f18593e48cb168296191cd0e0c117 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Mon, 29 Aug 2022 18:02:20 +0200 Subject: [PATCH 2/3] refactor WorkRequestFactory --- .../foundation/e/drive/utils/CommonUtils.java | 13 ++--- .../e/drive/work/WorkRequestFactory.java | 50 ++++++++++++++++--- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java index 5a24faf4..946e5c46 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -309,7 +309,7 @@ public abstract class CommonUtils { public static void registerPeriodicFullScanWorker(WorkManager workManager) { workManager.enqueueUniquePeriodicWork(FullScanWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, - new WorkRequestFactory().createPeriodicFullScanWorkRequest()); + WorkRequestFactory.getPeriodicWorkRequest(WorkRequestFactory.WorkType.FULL_SCAN)); } @@ -334,17 +334,15 @@ public abstract class CommonUtils { return; } - final WorkRequestFactory factory = new WorkRequestFactory(); - - final OneTimeWorkRequest getUserInfoRequest = factory.createOneTimeGetUserInfoWorkRequest(); + final OneTimeWorkRequest getUserInfoRequest = WorkRequestFactory.getOneTimeWorkRequest(WorkRequestFactory.WorkType.ONE_TIME_USER_INFO, null); final List workRequests = new ArrayList<>(); for (SyncedFolder folder : syncedFolders) { - final OneTimeWorkRequest createRemoteFolderWorkRequest = factory.createOneTimeCreateRemoteFolderWorkRequest(folder); + final OneTimeWorkRequest createRemoteFolderWorkRequest = WorkRequestFactory.getOneTimeWorkRequest(WorkRequestFactory.WorkType.CREATE_REMOTE_DIR, folder); workRequests.add(createRemoteFolderWorkRequest); } - final OneTimeWorkRequest firstStartRequest = factory.createOneTimeFirstStartWorkRequest(); + final OneTimeWorkRequest firstStartRequest = WorkRequestFactory.getOneTimeWorkRequest(WorkRequestFactory.WorkType.FIRST_START, null); workManager.beginWith(getUserInfoRequest) .then(workRequests) @@ -383,8 +381,7 @@ public abstract class CommonUtils { * @param workManager */ public static void registerPeriodicUserInfoChecking(WorkManager workManager) { - final WorkRequestFactory factory = new WorkRequestFactory(); - PeriodicWorkRequest workRequest = factory.createPeriodicGetUserInfoWorkRequest(); + final PeriodicWorkRequest workRequest = WorkRequestFactory.getPeriodicWorkRequest(WorkRequestFactory.WorkType.PERIODIC_USER_INFO); workManager.enqueueUniquePeriodicWork(AccountUserInfoWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.REPLACE, workRequest); } diff --git a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java index 069a22f9..49d4bcfc 100644 --- a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java +++ b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java @@ -11,19 +11,55 @@ import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_REMOTE_P import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_SCAN_LOCAL; import static foundation.e.drive.work.CreateRemoteFolderWorker.DATA_KEY_SCAN_REMOTE; +import androidx.annotation.Nullable; import androidx.work.BackoffPolicy; import androidx.work.Constraints; import androidx.work.Data; import androidx.work.NetworkType; import androidx.work.OneTimeWorkRequest; import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkRequest; +import java.security.InvalidParameterException; import java.util.concurrent.TimeUnit; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.utils.AppConstants; public class WorkRequestFactory { + public enum WorkType { + FULL_SCAN, + PERIODIC_USER_INFO, + ONE_TIME_USER_INFO, + CREATE_REMOTE_DIR, + FIRST_START + } + + public static PeriodicWorkRequest getPeriodicWorkRequest(WorkType type) { + switch (type) { + case FULL_SCAN: + return createPeriodicFullScanWorkRequest(); + case PERIODIC_USER_INFO: + return createPeriodicGetUserInfoWorkRequest(); + default: + throw new InvalidParameterException("Unsupported Work Type: " + type); + } + } + + public static OneTimeWorkRequest getOneTimeWorkRequest(WorkType type, @Nullable SyncedFolder syncedFolder) throws InvalidParameterException { + switch (type) { + case ONE_TIME_USER_INFO: + return createOneTimeGetUserInfoWorkRequest(); + case FIRST_START: + return createOneTimeFirstStartWorkRequest(); + case CREATE_REMOTE_DIR: + if (syncedFolder == null) throw new InvalidParameterException("Invalid synced folder"); + return createOneTimeCreateRemoteFolderWorkRequest(syncedFolder); + default: + throw new InvalidParameterException("Unsupported Work Type: " + type); + } + } + /** * Create a PeridocWorkRequest instance for @@ -31,7 +67,7 @@ public class WorkRequestFactory { * be unmetered) and battery (shouldn't be low) * @return instance of PeriodicWorkRequest */ - public PeriodicWorkRequest createPeriodicFullScanWorkRequest() { + private static PeriodicWorkRequest createPeriodicFullScanWorkRequest() { final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); final PeriodicWorkRequest workRequest = @@ -48,7 +84,7 @@ public class WorkRequestFactory { * Create a periodic work request to get userInfo * @return instance of PeriodicWorkRequest */ - public PeriodicWorkRequest createPeriodicGetUserInfoWorkRequest() { + private static PeriodicWorkRequest createPeriodicGetUserInfoWorkRequest() { final Constraints constraints = new Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) .build(); @@ -66,7 +102,7 @@ public class WorkRequestFactory { * Instanciate a OneTimeWorkRequest to retrieve user info * @return instance of OneTimeWorkRequest */ - public OneTimeWorkRequest createOneTimeGetUserInfoWorkRequest() { + private static OneTimeWorkRequest createOneTimeGetUserInfoWorkRequest() { final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(AccountUserInfoWorker.class) @@ -84,7 +120,7 @@ public class WorkRequestFactory { * @param syncedFolder SyncedFolder instance with data about folder to create * @return Instance OneTimeWorkRequest */ - public OneTimeWorkRequest createOneTimeCreateRemoteFolderWorkRequest(SyncedFolder syncedFolder) { + private static OneTimeWorkRequest createOneTimeCreateRemoteFolderWorkRequest(SyncedFolder syncedFolder) { final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder( @@ -103,7 +139,7 @@ public class WorkRequestFactory { * after initialization * @return Instance of OneTimeWorkRequest */ - public OneTimeWorkRequest createOneTimeFirstStartWorkRequest() { + private static OneTimeWorkRequest createOneTimeFirstStartWorkRequest() { final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(FirstStartWorker.class) .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) .addTag(AppConstants.WORK_GENERIC_TAG) @@ -117,7 +153,7 @@ public class WorkRequestFactory { * and battery not low * @return instance of Constraints */ - private Constraints createUnmeteredNetworkAndHighBatteryConstraints() { + private static Constraints createUnmeteredNetworkAndHighBatteryConstraints() { final Constraints constraint = new Constraints.Builder() .setRequiredNetworkType(NetworkType.UNMETERED) .setRequiresBatteryNotLow(true) @@ -131,7 +167,7 @@ public class WorkRequestFactory { * @param folder SyncedFolder instance * @return Data instance */ - private Data createDataFromSyncedFolder(SyncedFolder folder) { + private static Data createDataFromSyncedFolder(SyncedFolder folder) { return new Data.Builder() .putInt(DATA_KEY_ID, folder.getId()) .putString(DATA_KEY_LIBELLE, folder.getLibelle()) -- GitLab From 9f2c40b070e22f9a961cb397bdbf6b73c742a257 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 31 Aug 2022 11:46:31 +0200 Subject: [PATCH 3/3] refactor WorkRequestFactory --- .../e/drive/work/WorkRequestFactory.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java index 49d4bcfc..e3daf516 100644 --- a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java +++ b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java @@ -18,7 +18,6 @@ import androidx.work.Data; import androidx.work.NetworkType; import androidx.work.OneTimeWorkRequest; import androidx.work.PeriodicWorkRequest; -import androidx.work.WorkRequest; import java.security.InvalidParameterException; import java.util.concurrent.TimeUnit; @@ -35,6 +34,12 @@ public class WorkRequestFactory { FIRST_START } + /** + * Build an instance of PeriodicWorkRequest depending of the work type specified + * @param type WorkType. Should be FULL_SCAN or PERIODIC_USER_INFO. + * If not, it will throw an InvalidParameterException + * @return Periodic WorkRequest + */ public static PeriodicWorkRequest getPeriodicWorkRequest(WorkType type) { switch (type) { case FULL_SCAN: @@ -46,14 +51,21 @@ public class WorkRequestFactory { } } - public static OneTimeWorkRequest getOneTimeWorkRequest(WorkType type, @Nullable SyncedFolder syncedFolder) throws InvalidParameterException { + /** + * 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. + */ + public static OneTimeWorkRequest getOneTimeWorkRequest(WorkType type, @Nullable SyncedFolder syncedFolder) { switch (type) { case ONE_TIME_USER_INFO: return createOneTimeGetUserInfoWorkRequest(); case FIRST_START: return createOneTimeFirstStartWorkRequest(); case CREATE_REMOTE_DIR: - if (syncedFolder == null) throw new InvalidParameterException("Invalid synced folder"); + if (syncedFolder == null) throw new NullPointerException("Synced folder is null"); return createOneTimeCreateRemoteFolderWorkRequest(syncedFolder); default: throw new InvalidParameterException("Unsupported Work Type: " + type); -- GitLab