From 4ec1246ca265ce307fd4492773bada7b8186a388 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Mon, 28 Mar 2022 16:31:53 +0200 Subject: [PATCH 01/14] Add worker class to extract part of InitializerService into WorkRequest - CreateRemoteRootFolderWorker.java to create Root folder on ecloud - FirstStartWorker.java which start all service, FileObserver, etc... once remote folder are ready --- .../work/CreateRemoteRootFolderWorker.java | 118 ++++++++++++++++++ .../e/drive/work/FirstStartWorker.java | 58 +++++++++ 2 files changed, 176 insertions(+) create mode 100644 app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java create mode 100644 app/src/main/java/foundation/e/drive/work/FirstStartWorker.java diff --git a/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java b/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java new file mode 100644 index 00000000..1409dcb5 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java @@ -0,0 +1,118 @@ +/* + * Copyright © Vincent Bourgmayer (/e/ foundation). + * 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 android.accounts.Account; +import android.accounts.AccountManager; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Handler; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.work.Data; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; + +import foundation.e.drive.models.SyncedFolder; +import foundation.e.drive.operations.CreateInitialFolderRemoteOperation; +import foundation.e.drive.utils.AppConstants; +import foundation.e.drive.utils.CommonUtils; + +/** + * Create folder on ecloud for a given local folder + * @author Vincent Bourgmayer + */ +public class CreateRemoteRootFolderWorker extends Worker { + private static final String TAG = CreateRemoteRootFolderWorker.class.getSimpleName(); + public static final String DATA_KEY_ID="id"; + public static final String DATA_KEY_LIBELLE="libelle"; + public static final String DATA_KEY_LOCAL_PATH="localPath"; + public static final String DATA_KEY_REMOTE_PATH="remotePath"; + public static final String DATA_KEY_LAST_ETAG="etag"; + public static final String DATA_KEY_LAST_MODIFIED="lModified"; + public static final String DATA_KEY_SCAN_LOCAL="scanLocal"; + public static final String DATA_KEY_SCAN_REMOTE="scanRemote"; + public static final String DATA_KEY_ENABLE="enable"; + public static final String DATA_KEY_MEDIATYPE="mediaType"; + + final Handler handler; + + public CreateRemoteRootFolderWorker( @NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + handler = new Handler(); + } + + @NonNull + @Override + public Result doWork() { + if (!CommonUtils.haveNetworkConnection(getApplicationContext())) { + Log.e(TAG, "Can't create remote folder because there is no unmetered connexion"); + return Result.retry(); + } + + final Account account = getAccount(); + if (account == null) { + Log.e(TAG, "Can't get valid account: stop everything"); + return Result.failure(); + } + + final SyncedFolder syncedFolder = getSyncedFolderFromData(); + final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, getApplicationContext()); + if (client == null) { + Log.e(TAG, "Can't get OwnCloudClient."); + return Result.retry(); + } + + final CreateInitialFolderRemoteOperation createFolderOperation = + new CreateInitialFolderRemoteOperation( + syncedFolder, + true, + getApplicationContext()); + + final RemoteOperationResult result = createFolderOperation.execute(client); + if (result.isSuccess() || result.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS) { + return Result.success(); + } else { + Log.e(TAG, "Remote folder's creation failed: "+result.getHttpCode()); + return Result.retry(); + } + } + + private SyncedFolder getSyncedFolderFromData() { + Data data = getInputData(); + SyncedFolder result = new SyncedFolder( + data.getString(DATA_KEY_LIBELLE), + data.getString(DATA_KEY_LOCAL_PATH), + data.getString(DATA_KEY_REMOTE_PATH), + data.getBoolean(DATA_KEY_SCAN_LOCAL, true), + data.getBoolean(DATA_KEY_SCAN_REMOTE, true), + data.getBoolean(DATA_KEY_ENABLE, true), + data.getBoolean(DATA_KEY_MEDIATYPE, true)); + + result.setLastModified(data.getLong(DATA_KEY_LAST_MODIFIED, 0L)); + result.setLastEtag(data.getString(DATA_KEY_LAST_ETAG)); + result.setId(data.getInt(DATA_KEY_ID, -1)); + + return result; + } + + + private Account getAccount() { + SharedPreferences prefs = getApplicationContext().getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); + final String accountName = prefs.getString(AccountManager.KEY_ACCOUNT_NAME, ""); + final String accountType = prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, ""); + + if (accountName.isEmpty() && accountType.isEmpty()) return null; + return CommonUtils.getAccount(accountName, accountType, AccountManager.get(getApplicationContext())); + } +} diff --git a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java new file mode 100644 index 00000000..f83e2728 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java @@ -0,0 +1,58 @@ +/* + * Copyright © Vincent Bourgmayer (/e/ foundation). + * 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 foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; + +import android.content.Context; +import android.content.Intent; + +import androidx.annotation.NonNull; +import androidx.work.WorkManager; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +import foundation.e.drive.EdriveApplication; +import foundation.e.drive.utils.AppConstants; +import foundation.e.drive.utils.CommonUtils; + +/** + * This class start eDrive work after initialization. + * It contains job to start FileObserver, periodic fullScan and Synchronization service + * for the first time + * @author Vincent Bourgmayer + */ +public class FirstStartWorker extends Worker { + + public FirstStartWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + + @NonNull + @Override + public Result doWork() { + final Context appContext = getApplicationContext(); + appContext.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, + Context.MODE_PRIVATE) + .edit() + .putBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, true) + .putInt(INITIALFOLDERS_NUMBER, 9) + .apply(); + + CommonUtils.registerPeriodicFullScanWorker(WorkManager.getInstance(appContext)); + + getApplicationContext().startService(new Intent(getApplicationContext(), foundation.e.drive.services.SynchronizationService.class)); + getApplicationContext().startService(new Intent(getApplicationContext(), foundation.e.drive.services.ObserverService.class)); + + //all folder have been created + ((EdriveApplication) getApplicationContext()).startRecursiveFileObserver(); + + return null; + } +} -- GitLab From 2117452ac98930c7c71196a69095f1f611d208e0 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Mon, 28 Mar 2022 18:07:21 +0200 Subject: [PATCH 02/14] - Add work request tag for initialization worker - Fix coding style: switch 'final' and 'static' where it weren't good - Add method in CommonUtils to trigger/register new WorkRequest for Initialization - Add method in CommonUtils to create Data instance from SyncedFolder instance. It is required to pass SyncedFolder to CreateRemoteRootFolderWorker.java --- .../e/drive/utils/AppConstants.java | 10 +-- .../foundation/e/drive/utils/CommonUtils.java | 79 +++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/utils/AppConstants.java b/app/src/main/java/foundation/e/drive/utils/AppConstants.java index 4c3be31d..fec0762d 100644 --- a/app/src/main/java/foundation/e/drive/utils/AppConstants.java +++ b/app/src/main/java/foundation/e/drive/utils/AppConstants.java @@ -34,11 +34,11 @@ public abstract class AppConstants { public static final String[] MEDIA_SYNCABLE_CATEGORIES = new String[]{"Images", "Movies", "Music", "Ringtones", "Documents", "Podcasts"}; public static final String[] SETTINGS_SYNCABLE_CATEGORIES = new String[] {"Rom settings"}; - public final static String notificationChannelID ="3310"; - public final static String notificationChannelName="eDrive channel"; - public final static String WORK_GENERIC_TAG="eDrive"; - - public final static String USER_AGENT = "eos("+getBuildTime()+")-eDrive("+ BuildConfig.VERSION_NAME +")"; + public static final String notificationChannelID ="3310"; + public static final String notificationChannelName="eDrive channel"; + public static final String WORK_GENERIC_TAG="eDrive"; + public static final String WORK_INITIALIZATION_TAG="eDrive-init"; + public static final String USER_AGENT = "eos("+getBuildTime()+")-eDrive("+ BuildConfig.VERSION_NAME +")"; /** * Get a readable OS's build date String 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 a7fcdf0c..1de9a4e3 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -32,20 +32,40 @@ import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.resources.files.FileUtils; import java.io.File; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.TimeUnit; +import foundation.e.drive.models.SyncedFolder; +import foundation.e.drive.work.CreateRemoteRootFolderWorker; +import foundation.e.drive.work.FirstStartWorker; import foundation.e.drive.work.FullScanWorker; import static foundation.e.drive.utils.AppConstants.MEDIASYNC_PROVIDER_AUTHORITY; import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORITY; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_ENABLE; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_ID; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_LAST_ETAG; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_LAST_MODIFIED; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_LIBELLE; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_LOCAL_PATH; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_MEDIATYPE; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_REMOTE_PATH; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_SCAN_LOCAL; +import static foundation.e.drive.work.CreateRemoteRootFolderWorker.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.WorkContinuation; import androidx.work.WorkManager; +import androidx.work.WorkerParameters; /** @@ -304,4 +324,63 @@ public abstract class CommonUtils { workManager.enqueueUniquePeriodicWork(FullScanWorker.UNIQUE_WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, periodicFullScanRequest); } + + + public static void registerInitializationWorkers(List syncedFolders, WorkManager workManager){ + if (syncedFolders == null || syncedFolders.isEmpty()) { + Log.e(TAG, "Can't create remote folders. List is empty"); + return; + } + + final Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.UNMETERED) + .setRequiresBatteryNotLow(true) + .build(); + + final List workRequests = new ArrayList<>(); + + for (SyncedFolder folder : syncedFolders) { + final OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder( + CreateRemoteRootFolderWorker.class) + .setBackoffCriteria(BackoffPolicy.LINEAR, + OneTimeWorkRequest.MIN_BACKOFF_MILLIS, + TimeUnit.MINUTES) + .setInputData(createDataFromSyncedFolder(folder)) + .addTag(AppConstants.WORK_GENERIC_TAG) + .addTag(AppConstants.WORK_INITIALIZATION_TAG) + .setConstraints(constraints) + .build(); + + workRequests.add(oneTimeWorkRequest); + } + + final OneTimeWorkRequest firstStartRequest = new OneTimeWorkRequest.Builder(FirstStartWorker.class) + .setBackoffCriteria(BackoffPolicy.LINEAR, + OneTimeWorkRequest.MIN_BACKOFF_MILLIS, + TimeUnit.MINUTES) + .addTag(AppConstants.WORK_GENERIC_TAG) + .addTag(AppConstants.WORK_INITIALIZATION_TAG) + .build(); + + + workManager.beginWith(workRequests) + .then(firstStartRequest) + .enqueue(); + } + + + 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(); + } } \ No newline at end of file -- GitLab From 2a1617bfbc4a833976e9065de2393e5bca650af3 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Mon, 28 Mar 2022 18:25:08 +0200 Subject: [PATCH 03/14] Refactor initializerService to use Work request --- .../e/drive/services/InitializerService.java | 120 +----------------- 1 file changed, 7 insertions(+), 113 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/services/InitializerService.java b/app/src/main/java/foundation/e/drive/services/InitializerService.java index ef08ccb2..d493028d 100644 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java @@ -16,27 +16,20 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Environment; -import android.os.Handler; import android.os.IBinder; import android.util.Log; import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import foundation.e.drive.EdriveApplication; import foundation.e.drive.models.SyncedFolder; -import foundation.e.drive.operations.CreateInitialFolderRemoteOperation; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR; -import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; import static foundation.e.drive.utils.AppConstants.MEDIA_SYNCABLE_CATEGORIES; import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES; @@ -46,21 +39,16 @@ import androidx.work.WorkManager; /** * @author Vincent Bourgmayer */ -public class InitializerService extends Service implements OnRemoteOperationListener { +public class InitializerService extends Service { private final String TAG = InitializerService.class.getSimpleName(); - - private int existingRemoteFolderCounter; //Temporarily used to know if all remotePath exist private List syncedFolders; private OwnCloudClient cloudClient; - private Handler handler; private Account account; - private int restartFolderCreationCounter =0; @Override public void onCreate() { Log.i(TAG, "onCreate()"); super.onCreate(); - this.existingRemoteFolderCounter = 0; } @Override @@ -112,21 +100,13 @@ public class InitializerService extends Service implements OnRemoteOperationList return; } - List syncCategories = new ArrayList<>(); - - if (CommonUtils.isMediaSyncEnabled(account)) { - syncCategories.addAll(Arrays.asList(MEDIA_SYNCABLE_CATEGORIES)); - } + final List syncCategories = new ArrayList<>(); - if (CommonUtils.isSettingsSyncEnabled(account)) { - syncCategories.addAll(Arrays.asList(SETTINGS_SYNCABLE_CATEGORIES)); - } + syncCategories.addAll(Arrays.asList(MEDIA_SYNCABLE_CATEGORIES)); + syncCategories.addAll(Arrays.asList(SETTINGS_SYNCABLE_CATEGORIES)); getInitialSyncedFolders(syncCategories); - - this.existingRemoteFolderCounter = 0; - - CreateNextRemoteFolder(); + CommonUtils.registerInitializationWorkers(syncedFolders, WorkManager.getInstance(getApplicationContext()) ); } /** @@ -142,8 +122,6 @@ public class InitializerService extends Service implements OnRemoteOperationList final String DEVICE_SPECIFIC_PATH = PATH_SEPARATOR+"Devices"+PATH_SEPARATOR+ Build.BRAND+"_"+ Build.MODEL+"_" + Build.SERIAL; switch (categories.get(i)) { - case "Medias": - break; case "Images": syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_DCIM), "/Photos/", true)); @@ -171,6 +149,7 @@ public class InitializerService extends Service implements OnRemoteOperationList "/Podcasts/", true)); break; case "Rom settings": + String remoteFolderPath = DEVICE_SPECIFIC_PATH+"/rom_settings/"; syncedFolders.add(new SyncedFolder(categories.get(i), "/data/system/users/0/", remoteFolderPath, true, false, true, false)); try{ @@ -180,7 +159,7 @@ public class InitializerService extends Service implements OnRemoteOperationList remoteFolderPath+"app_list/", true, false, - true, + CommonUtils.isSettingsSyncEnabled(account), false)); } catch (Exception e) { Log.e(TAG, e.toString()); } break; @@ -192,94 +171,9 @@ public class InitializerService extends Service implements OnRemoteOperationList return CommonUtils.getLocalPath(Environment.getExternalStoragePublicDirectory(directory))+ PATH_SEPARATOR; } - private void CreateNextRemoteFolder() { - Log.i(TAG, "createNextRemoteFolder()"); - this.restartFolderCreationCounter = 0; - - if (this.syncedFolders == null || this.syncedFolders.isEmpty()) { - this.stopSelf(); - } - - //It means that there are still folders to create - if (this.existingRemoteFolderCounter < this.syncedFolders.size()) { - if (this.handler == null) this.handler = new Handler(); - - CreateInitialFolderRemoteOperation createFolderOperation = - new CreateInitialFolderRemoteOperation( - this.syncedFolders.get(this.existingRemoteFolderCounter), - true, - this); - - createFolderOperation.execute(this.cloudClient, this, this.handler); - - } else if (this.existingRemoteFolderCounter == this.syncedFolders.size()) { - doLastStep(); - } else { - Log.e(TAG, "this.existingRemoteFolderCounter : "+this.existingRemoteFolderCounter+" > this.mSyncedFolders.size() : "+this.syncedFolders.size()); - this.stopSelf(); - } - } - - @Override - public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { - Log.i(TAG, "onRemoteOperationFinish()"); - if (operation instanceof CreateInitialFolderRemoteOperation) { - - if (result.isSuccess() || result.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS) { - this.existingRemoteFolderCounter+=1; - CreateNextRemoteFolder(); - - } else if (result.getHttpCode() == 423 || result.getHttpCode() == 409) {//file locked or conflict in result - Log.e(TAG, result.getLogMessage()); - - if (this.restartFolderCreationCounter < 3) { - Log.w(TAG, " restart operation"); - operation.execute(this.cloudClient, this, this.handler); - this.restartFolderCreationCounter+=1; - } else { - Log.e(TAG, "Remote folder's creation failed due to conflict with server"); - stopSelf(); - } - } else { - Log.e(TAG, result.getLogMessage()+" "+result.getHttpCode()); - stopSelf(); - } - } - } - - - /** - * Function to check if all remote folder have been created - **/ - private void doLastStep() { - Log.i(TAG, "doLastStep()"); - final Context appContext = getApplicationContext(); - appContext.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, - Context.MODE_PRIVATE) - .edit() - .putBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, true) - .putInt(INITIALFOLDERS_NUMBER, syncedFolders.size()) - .apply(); - - CommonUtils.registerPeriodicFullScanWorker(WorkManager.getInstance(appContext)); - - //all folder have been created - ((EdriveApplication) this.getApplication()).startRecursiveFileObserver(); - - Intent SynchronizationServiceIntent = new Intent(getApplicationContext(), foundation.e.drive.services.SynchronizationService.class); - startService(SynchronizationServiceIntent); - - //Immediatly start ObserverService to not have to wait 30 minutes. - Intent observersServiceIntent = new Intent(getApplicationContext(), foundation.e.drive.services.ObserverService.class); - startService(observersServiceIntent); - - stopSelf(); - } - @Override public void onDestroy() { super.onDestroy(); - this.handler = null; this.account = null; this.cloudClient = null; if (this.syncedFolders != null) this.syncedFolders.clear(); -- GitLab From 1185b135e1f971db69dbf001dd2bb90987cb4857 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 29 Mar 2022 09:00:48 +0200 Subject: [PATCH 04/14] set Initialization work request to be expedited --- app/src/main/java/foundation/e/drive/utils/CommonUtils.java | 3 +++ 1 file changed, 3 insertions(+) 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 1de9a4e3..3d7b8d5f 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -62,6 +62,7 @@ import androidx.work.Data; import androidx.work.ExistingPeriodicWorkPolicy; import androidx.work.NetworkType; import androidx.work.OneTimeWorkRequest; +import androidx.work.OutOfQuotaPolicy; import androidx.work.PeriodicWorkRequest; import androidx.work.WorkContinuation; import androidx.work.WorkManager; @@ -346,6 +347,7 @@ public abstract class CommonUtils { OneTimeWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MINUTES) .setInputData(createDataFromSyncedFolder(folder)) + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) .addTag(AppConstants.WORK_GENERIC_TAG) .addTag(AppConstants.WORK_INITIALIZATION_TAG) .setConstraints(constraints) @@ -358,6 +360,7 @@ public abstract class CommonUtils { .setBackoffCriteria(BackoffPolicy.LINEAR, OneTimeWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MINUTES) + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) .addTag(AppConstants.WORK_GENERIC_TAG) .addTag(AppConstants.WORK_INITIALIZATION_TAG) .build(); -- GitLab From 3cd9e27451491c88264ae309ea368c7b2ac74d8f Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 29 Mar 2022 11:17:46 +0200 Subject: [PATCH 05/14] remove BatteryConstraint because incompatble with Expedited work --- app/src/main/java/foundation/e/drive/utils/CommonUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3d7b8d5f..022ce886 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -335,7 +335,7 @@ public abstract class CommonUtils { final Constraints constraints = new Constraints.Builder() .setRequiredNetworkType(NetworkType.UNMETERED) - .setRequiresBatteryNotLow(true) + //.setRequiresBatteryNotLow(true)//Expedited job doesn't support battery constraint .build(); final List workRequests = new ArrayList<>(); -- GitLab From ebcac1d295973908cc348e7a87885715a0ecfb8d Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 29 Mar 2022 11:18:21 +0200 Subject: [PATCH 06/14] Fix CreateRemoteRootFolderWorker.java by removing useless handler Also add logcat debuging info --- .../foundation/e/drive/work/CreateRemoteRootFolderWorker.java | 4 +--- .../main/java/foundation/e/drive/work/FirstStartWorker.java | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java b/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java index 1409dcb5..181364c9 100644 --- a/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java +++ b/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java @@ -45,11 +45,8 @@ public class CreateRemoteRootFolderWorker extends Worker { public static final String DATA_KEY_ENABLE="enable"; public static final String DATA_KEY_MEDIATYPE="mediaType"; - final Handler handler; - public CreateRemoteRootFolderWorker( @NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); - handler = new Handler(); } @NonNull @@ -67,6 +64,7 @@ public class CreateRemoteRootFolderWorker extends Worker { } final SyncedFolder syncedFolder = getSyncedFolderFromData(); + Log.d(TAG, "doWork() for :"+syncedFolder.getLocalFolder()); final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, getApplicationContext()); if (client == null) { Log.e(TAG, "Can't get OwnCloudClient."); diff --git a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java index f83e2728..b39b2003 100644 --- a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java +++ b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java @@ -12,6 +12,7 @@ import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; import android.content.Context; import android.content.Intent; +import android.util.Log; import androidx.annotation.NonNull; import androidx.work.WorkManager; @@ -29,7 +30,7 @@ import foundation.e.drive.utils.CommonUtils; * @author Vincent Bourgmayer */ public class FirstStartWorker extends Worker { - + private static final String TAG = FirstStartWorker.class.getSimpleName(); public FirstStartWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); } @@ -37,6 +38,7 @@ public class FirstStartWorker extends Worker { @NonNull @Override public Result doWork() { + Log.d(TAG, "doWork()"); final Context appContext = getApplicationContext(); appContext.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE) -- GitLab From 4f882debdd7887d1065e2f8d8999affc7afaf107 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Tue, 29 Mar 2022 14:30:19 +0200 Subject: [PATCH 07/14] disable expedited work --- app/src/main/java/foundation/e/drive/utils/CommonUtils.java | 4 +--- 1 file changed, 1 insertion(+), 3 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 022ce886..a1617dff 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -335,7 +335,7 @@ public abstract class CommonUtils { final Constraints constraints = new Constraints.Builder() .setRequiredNetworkType(NetworkType.UNMETERED) - //.setRequiresBatteryNotLow(true)//Expedited job doesn't support battery constraint + .setRequiresBatteryNotLow(true) .build(); final List workRequests = new ArrayList<>(); @@ -347,7 +347,6 @@ public abstract class CommonUtils { OneTimeWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MINUTES) .setInputData(createDataFromSyncedFolder(folder)) - .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) .addTag(AppConstants.WORK_GENERIC_TAG) .addTag(AppConstants.WORK_INITIALIZATION_TAG) .setConstraints(constraints) @@ -360,7 +359,6 @@ public abstract class CommonUtils { .setBackoffCriteria(BackoffPolicy.LINEAR, OneTimeWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MINUTES) - .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) .addTag(AppConstants.WORK_GENERIC_TAG) .addTag(AppConstants.WORK_INITIALIZATION_TAG) .build(); -- GitLab From acacde12466a95bcd9dda1a366d2cf23f9aaea12 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 30 Mar 2022 16:10:03 +0200 Subject: [PATCH 08/14] Handle Exception when executing MKCol requet --- .../drive/work/CreateRemoteRootFolderWorker.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java b/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java index 181364c9..dc382cd8 100644 --- a/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java +++ b/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java @@ -77,13 +77,16 @@ public class CreateRemoteRootFolderWorker extends Worker { true, getApplicationContext()); - final RemoteOperationResult result = createFolderOperation.execute(client); - if (result.isSuccess() || result.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS) { - return Result.success(); - } else { - Log.e(TAG, "Remote folder's creation failed: "+result.getHttpCode()); - return Result.retry(); + try { + final RemoteOperationResult result = createFolderOperation.execute(client); + if (result.isSuccess() || result.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS) { + return Result.success(); + } + } catch (Exception e) { + Log.e(TAG, "Exception: "+e.getClass().getSimpleName()); + //do nothing cause it will be reach Result.retry() after } + return Result.retry(); } private SyncedFolder getSyncedFolderFromData() { -- GitLab From 58d0fe2d2bf1913b4d7c28801003538b28cb8e18 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 30 Mar 2022 16:11:04 +0200 Subject: [PATCH 09/14] Fix return value of FirstStartWorker --- app/src/main/java/foundation/e/drive/work/FirstStartWorker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java index b39b2003..98ded192 100644 --- a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java +++ b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java @@ -55,6 +55,6 @@ public class FirstStartWorker extends Worker { //all folder have been created ((EdriveApplication) getApplicationContext()).startRecursiveFileObserver(); - return null; + return Result.success(); } } -- GitLab From 5f33ef937315edb7250cc906b0b833e1319a098c Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 30 Mar 2022 16:11:48 +0200 Subject: [PATCH 10/14] Fix wrong backoff criteria --- .../main/java/foundation/e/drive/utils/CommonUtils.java | 8 ++------ 1 file changed, 2 insertions(+), 6 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 a1617dff..2d86e7d7 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -343,9 +343,7 @@ public abstract class CommonUtils { for (SyncedFolder folder : syncedFolders) { final OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder( CreateRemoteRootFolderWorker.class) - .setBackoffCriteria(BackoffPolicy.LINEAR, - OneTimeWorkRequest.MIN_BACKOFF_MILLIS, - TimeUnit.MINUTES) + .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) .setInputData(createDataFromSyncedFolder(folder)) .addTag(AppConstants.WORK_GENERIC_TAG) .addTag(AppConstants.WORK_INITIALIZATION_TAG) @@ -356,9 +354,7 @@ public abstract class CommonUtils { } final OneTimeWorkRequest firstStartRequest = new OneTimeWorkRequest.Builder(FirstStartWorker.class) - .setBackoffCriteria(BackoffPolicy.LINEAR, - OneTimeWorkRequest.MIN_BACKOFF_MILLIS, - TimeUnit.MINUTES) + .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) .addTag(AppConstants.WORK_GENERIC_TAG) .addTag(AppConstants.WORK_INITIALIZATION_TAG) .build(); -- GitLab From 7c2c5b2d7262f53840de10ec630d140df188f47b Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 31 Mar 2022 09:51:57 +0200 Subject: [PATCH 11/14] improve use of context in CreateRemoteRootFolderWorker --- .../e/drive/work/CreateRemoteRootFolderWorker.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java b/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java index dc382cd8..b818f8a7 100644 --- a/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java +++ b/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java @@ -12,7 +12,6 @@ import android.accounts.Account; import android.accounts.AccountManager; import android.content.Context; import android.content.SharedPreferences; -import android.os.Handler; import android.util.Log; import androidx.annotation.NonNull; @@ -52,7 +51,8 @@ public class CreateRemoteRootFolderWorker extends Worker { @NonNull @Override public Result doWork() { - if (!CommonUtils.haveNetworkConnection(getApplicationContext())) { + final Context context = getApplicationContext(); + if (!CommonUtils.haveNetworkConnection(context)) { Log.e(TAG, "Can't create remote folder because there is no unmetered connexion"); return Result.retry(); } @@ -65,17 +65,15 @@ public class CreateRemoteRootFolderWorker extends Worker { final SyncedFolder syncedFolder = getSyncedFolderFromData(); Log.d(TAG, "doWork() for :"+syncedFolder.getLocalFolder()); - final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, getApplicationContext()); + + final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, context); if (client == null) { Log.e(TAG, "Can't get OwnCloudClient."); return Result.retry(); } final CreateInitialFolderRemoteOperation createFolderOperation = - new CreateInitialFolderRemoteOperation( - syncedFolder, - true, - getApplicationContext()); + new CreateInitialFolderRemoteOperation( syncedFolder, true, context); try { final RemoteOperationResult result = createFolderOperation.execute(client); -- GitLab From 8986ab09b4f21b9da22189fff31a0ac0778d04f6 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 31 Mar 2022 09:53:13 +0200 Subject: [PATCH 12/14] rename CreateRemoteRootFolderWorker to remove 'Root' --- .../foundation/e/drive/utils/CommonUtils.java | 29 ++++++++----------- ...ker.java => CreateRemoteFolderWorker.java} | 6 ++-- 2 files changed, 15 insertions(+), 20 deletions(-) rename app/src/main/java/foundation/e/drive/work/{CreateRemoteRootFolderWorker.java => CreateRemoteFolderWorker.java} (94%) 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 2d86e7d7..1a116e2d 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -17,9 +17,7 @@ import android.content.ContentResolver; import android.content.Context; import android.media.MediaScannerConnection; import android.net.ConnectivityManager; -import android.net.Network; import android.net.NetworkCapabilities; -import android.net.NetworkInfo; import android.net.Uri; import android.util.Log; import android.webkit.MimeTypeMap; @@ -38,22 +36,22 @@ import java.util.concurrent.TimeUnit; import foundation.e.drive.models.SyncedFolder; -import foundation.e.drive.work.CreateRemoteRootFolderWorker; +import foundation.e.drive.work.CreateRemoteFolderWorker; import foundation.e.drive.work.FirstStartWorker; import foundation.e.drive.work.FullScanWorker; import static foundation.e.drive.utils.AppConstants.MEDIASYNC_PROVIDER_AUTHORITY; import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORITY; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_ENABLE; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_ID; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_LAST_ETAG; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_LAST_MODIFIED; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_LIBELLE; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_LOCAL_PATH; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_MEDIATYPE; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_REMOTE_PATH; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_SCAN_LOCAL; -import static foundation.e.drive.work.CreateRemoteRootFolderWorker.DATA_KEY_SCAN_REMOTE; +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; @@ -62,11 +60,8 @@ import androidx.work.Data; import androidx.work.ExistingPeriodicWorkPolicy; import androidx.work.NetworkType; import androidx.work.OneTimeWorkRequest; -import androidx.work.OutOfQuotaPolicy; import androidx.work.PeriodicWorkRequest; -import androidx.work.WorkContinuation; import androidx.work.WorkManager; -import androidx.work.WorkerParameters; /** @@ -342,7 +337,7 @@ public abstract class CommonUtils { for (SyncedFolder folder : syncedFolders) { final OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder( - CreateRemoteRootFolderWorker.class) + CreateRemoteFolderWorker.class) .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) .setInputData(createDataFromSyncedFolder(folder)) .addTag(AppConstants.WORK_GENERIC_TAG) diff --git a/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java b/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java similarity index 94% rename from app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java rename to app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java index b818f8a7..9dc10efe 100644 --- a/app/src/main/java/foundation/e/drive/work/CreateRemoteRootFolderWorker.java +++ b/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java @@ -31,8 +31,8 @@ import foundation.e.drive.utils.CommonUtils; * Create folder on ecloud for a given local folder * @author Vincent Bourgmayer */ -public class CreateRemoteRootFolderWorker extends Worker { - private static final String TAG = CreateRemoteRootFolderWorker.class.getSimpleName(); +public class CreateRemoteFolderWorker extends Worker { + private static final String TAG = CreateRemoteFolderWorker.class.getSimpleName(); public static final String DATA_KEY_ID="id"; public static final String DATA_KEY_LIBELLE="libelle"; public static final String DATA_KEY_LOCAL_PATH="localPath"; @@ -44,7 +44,7 @@ public class CreateRemoteRootFolderWorker extends Worker { public static final String DATA_KEY_ENABLE="enable"; public static final String DATA_KEY_MEDIATYPE="mediaType"; - public CreateRemoteRootFolderWorker( @NonNull Context context, @NonNull WorkerParameters workerParams) { + public CreateRemoteFolderWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); } -- GitLab From 6d07d9155f55e251e266d7f70bad0bf3ac4622b8 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 31 Mar 2022 10:27:17 +0200 Subject: [PATCH 13/14] fusion operation.CreateInitialFolderRemoteOperation into work.CreateRemoteFolderWorker --- .../CreateInitialFolderRemoteOperation.java | 64 ------------------- .../drive/work/CreateRemoteFolderWorker.java | 20 ++++-- 2 files changed, 15 insertions(+), 69 deletions(-) delete mode 100644 app/src/main/java/foundation/e/drive/operations/CreateInitialFolderRemoteOperation.java diff --git a/app/src/main/java/foundation/e/drive/operations/CreateInitialFolderRemoteOperation.java b/app/src/main/java/foundation/e/drive/operations/CreateInitialFolderRemoteOperation.java deleted file mode 100644 index 72b837a3..00000000 --- a/app/src/main/java/foundation/e/drive/operations/CreateInitialFolderRemoteOperation.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright © Vincent Bourgmayer (/e/ foundation). - * 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.operations; - -import android.content.Context; -import android.util.Log; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation; -import java.io.File; -import foundation.e.drive.database.DbHelper; -import foundation.e.drive.models.SyncedFolder; - -/** - * @author Vincent Bourgmayer - * Perform initial folder creation - */ -public class CreateInitialFolderRemoteOperation extends RemoteOperation { - private static final String TAG = CreateInitialFolderRemoteOperation.class.getSimpleName(); - - private SyncedFolder mSyncedFolder; - private String mRemotePath; - private boolean mCreateFullPath;//should recreate parent path if not exist or not - private final Context mContext; - - public CreateInitialFolderRemoteOperation(SyncedFolder root, boolean createFullPath, Context context) { - super(); - this.mSyncedFolder = root; - this.mRemotePath = root.getRemoteFolder(); - this.mCreateFullPath = createFullPath; - this.mContext = context; - } - - @Override - protected RemoteOperationResult run(OwnCloudClient client) { - File folder = new File(mSyncedFolder.getLocalFolder() ); - if( !folder.exists() ){ - Log.e(TAG, "Local folder doesn't exist, so create it"); - folder.mkdirs(); - } - - //Perfom request - CreateFolderRemoteOperation createFolderOperation = new CreateFolderRemoteOperation(mRemotePath, mCreateFullPath); - RemoteOperationResult createOperationResult = createFolderOperation.execute(client, true); - - if(createOperationResult.isSuccess() || createOperationResult.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS ){ - if(DbHelper.insertSyncedFolder(mSyncedFolder, mContext) >= 0 ) { - Log.d(TAG, "Insertion in DB succeed"); - return new RemoteOperationResult(RemoteOperationResult.ResultCode.OK); - }else { - Log.d(TAG, "insertion of folder in DB failed"); - return new RemoteOperationResult(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS); - } - } - return createOperationResult; - } -} diff --git a/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java b/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java index 9dc10efe..8c1443e2 100644 --- a/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java +++ b/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java @@ -21,9 +21,12 @@ import androidx.work.WorkerParameters; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation; +import java.io.File; + +import foundation.e.drive.database.DbHelper; import foundation.e.drive.models.SyncedFolder; -import foundation.e.drive.operations.CreateInitialFolderRemoteOperation; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; @@ -65,6 +68,11 @@ public class CreateRemoteFolderWorker extends Worker { final SyncedFolder syncedFolder = getSyncedFolderFromData(); Log.d(TAG, "doWork() for :"+syncedFolder.getLocalFolder()); + final File folder = new File(syncedFolder.getLocalFolder() ); + if (!folder.exists()) { + folder.mkdirs(); + syncedFolder.setLastModified(folder.lastModified()); + } final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, context); if (client == null) { @@ -72,17 +80,19 @@ public class CreateRemoteFolderWorker extends Worker { return Result.retry(); } - final CreateInitialFolderRemoteOperation createFolderOperation = - new CreateInitialFolderRemoteOperation( syncedFolder, true, context); + final CreateFolderRemoteOperation mkcolRequest = + new CreateFolderRemoteOperation(syncedFolder.getRemoteFolder(), true); try { - final RemoteOperationResult result = createFolderOperation.execute(client); + final RemoteOperationResult result = mkcolRequest.execute(client, true); if (result.isSuccess() || result.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS) { + if(DbHelper.insertSyncedFolder(syncedFolder, context) >= 0 ) { + Log.d(TAG, "Insertion in DB succeed"); + } return Result.success(); } } catch (Exception e) { Log.e(TAG, "Exception: "+e.getClass().getSimpleName()); - //do nothing cause it will be reach Result.retry() after } return Result.retry(); } -- GitLab From 34ff9a84416eb7c1b27187728562dd11f0efed7f Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 31 Mar 2022 17:05:49 +0200 Subject: [PATCH 14/14] add missing javadoc for CommonUtils.registerInitializationWorkers(...) --- .../foundation/e/drive/utils/CommonUtils.java | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 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 1a116e2d..2cab9888 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -322,6 +322,21 @@ public abstract class CommonUtils { } + /** + * This method create a chain of WorkRequests to perform Initialization tasks: + * Firstly, it creates WorkRequest to create remote root folders on ecloud + * Then, once all folders are present on cloud, run the FirstStartWorker. + * + * in details: + * - Create 9 remote folders on ecloud + * - Run a first fullscan (ObserverService) + * - start SynchronizationService + * - Active FileObserver + * - Schedule periodic fullscan. + * + * @param syncedFolders List of SyncedFolder for which we want to create a remote folder on ecloud + * @param workManager WorkManager instance to register WorkRequest + */ public static void registerInitializationWorkers(List syncedFolders, WorkManager workManager){ if (syncedFolders == null || syncedFolders.isEmpty()) { Log.e(TAG, "Can't create remote folders. List is empty"); @@ -337,27 +352,26 @@ public abstract class CommonUtils { 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 firstStartRequest = new OneTimeWorkRequest.Builder(FirstStartWorker.class) + 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 firstStartRequest = new OneTimeWorkRequest.Builder(FirstStartWorker.class) + .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) + .addTag(AppConstants.WORK_GENERIC_TAG) + .addTag(AppConstants.WORK_INITIALIZATION_TAG) + .build(); workManager.beginWith(workRequests) - .then(firstStartRequest) - .enqueue(); + .then(firstStartRequest) + .enqueue(); } -- GitLab