From 042512c0032f3d1a8a298bdb0577dd16779934d8 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 2 Mar 2022 09:43:04 +0100 Subject: [PATCH 01/10] clean dependencies in app/build.gradle - replace all androidTestImplementation and testImplementation library into androidx - reorganize dependencies to make them cleaner --- app/build.gradle | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 44a0e1de..e2bfaed3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -58,27 +58,19 @@ android { dependencies { + api project(':NextcloudLib') implementation fileTree(include: ['*.jar'], dir: 'libs') - //implementation 'com.android.support:appcompat-v7:26.1.0' - - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test:rules:1.0.2' - androidTestImplementation 'androidx.annotation:annotation:1.3.0' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' api 'androidx.annotation:annotation:1.3.0' - api project(':NextcloudLib') - - //start to add lib for test - 1/4/21 - //@TODO: add junit runner as lib for testImplementation + + androidTestImplementation 'androidx.test:runner:1.4.0' + androidTestImplementation 'androidx.test:rules:1.4.0 ' + androidTestImplementation 'androidx.annotation:annotation:1.3.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + androidTestImplementation 'junit:junit:4.12' - testImplementation 'com.android.support.test:runner:1.0.2' - testImplementation 'com.android.support.test:rules:1.0.2' + testImplementation 'androidx.test:runner:1.4.0' + testImplementation 'androidx.test:rules:1.4.0 ' testImplementation 'junit:junit:4.12' - //testImplementation 'org.robolectric:robolectric:4.4' //need AndroidX - testImplementation "org.robolectric:robolectric:3.8" + testImplementation 'org.robolectric:robolectric:4.4' testImplementation('org.mockito:mockito-inline:3.4.0') - - //testImplementation Libs.AndroidX.Test.archCoreTesting //TODO: replace by not android X version - //implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' - androidTestImplementation 'junit:junit:4.12' } -- GitLab From 2af8a5bc19e24b133060ef95ff68666630f08e6e Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 2 Mar 2022 09:44:47 +0100 Subject: [PATCH 02/10] add jetpack's work dependency --- app/build.gradle | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index e2bfaed3..510784e9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -61,6 +61,10 @@ dependencies { api project(':NextcloudLib') implementation fileTree(include: ['*.jar'], dir: 'libs') api 'androidx.annotation:annotation:1.3.0' + + def work_version = "2.7.1" + // (Java only) + implementation "androidx.work:work-runtime:$work_version" androidTestImplementation 'androidx.test:runner:1.4.0' androidTestImplementation 'androidx.test:rules:1.4.0 ' -- GitLab From 8cf7652674a9170f4f53457dd91d4a0ea0319f1b Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 2 Mar 2022 09:50:16 +0100 Subject: [PATCH 03/10] fix typo for 'androidx.test:rules' dependencies --- app/build.gradle | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 510784e9..018d91fd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -53,7 +53,6 @@ android { //includeAndroidResources = true } } - } @@ -65,15 +64,15 @@ dependencies { def work_version = "2.7.1" // (Java only) implementation "androidx.work:work-runtime:$work_version" - + androidTestImplementation 'androidx.test:runner:1.4.0' - androidTestImplementation 'androidx.test:rules:1.4.0 ' + androidTestImplementation 'androidx.test:rules:1.4.0' androidTestImplementation 'androidx.annotation:annotation:1.3.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation 'junit:junit:4.12' testImplementation 'androidx.test:runner:1.4.0' - testImplementation 'androidx.test:rules:1.4.0 ' + testImplementation 'androidx.test:rules:1.4.0' testImplementation 'junit:junit:4.12' testImplementation 'org.robolectric:robolectric:4.4' testImplementation('org.mockito:mockito-inline:3.4.0') -- GitLab From 5aa1a30996708a7562d833c4f34c1810fef36535 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 3 Mar 2022 10:13:03 +0100 Subject: [PATCH 04/10] Add new foundation.e.drive.work.FullScanWorker.java - Add new class FullScanWorker.java to be used as replacement of jobs.ScannerJob --- .../e/drive/work/FullScanWorker.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 app/src/main/java/foundation/e/drive/work/FullScanWorker.java diff --git a/app/src/main/java/foundation/e/drive/work/FullScanWorker.java b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java new file mode 100644 index 00000000..dd538e1e --- /dev/null +++ b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java @@ -0,0 +1,45 @@ +/* + * 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.content.Context; +import android.content.Intent; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.work.Worker; +import androidx.work.WorkerParameters; + +import foundation.e.drive.services.ObserverService; + +/** + * As a first step, this class must replace foundation.e.drive.jobs.ScannerJob + * in order to allow to use Jetpack Work API + * + * In further development it will be a part of Workers that will replace ObserverService + * I will update this header accordingly + * + * @author Vincent Bourgmayer + */ +public class FullScanWorker extends Worker { + final private static String TAG = FullScanWorker.class.getSimpleName(); + + public FullScanWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { + super(context, workerParams); + } + + @NonNull + @Override + public Result doWork() { + Log.d(TAG, "doWork(): going to send intent to ObserverService"); + Intent observerServiceIntent = new Intent(this.getApplicationContext(), ObserverService.class); + this.getApplicationContext().startService(observerServiceIntent); + return Result.success(); + } +} -- GitLab From 18d4cd547caad20c7cfdaeeae254e9f98c090c30 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 3 Mar 2022 10:20:53 +0100 Subject: [PATCH 05/10] add generic tag for worker in utils.AppConstants.java --- app/src/main/java/foundation/e/drive/utils/AppConstants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1e180424..7506a525 100644 --- a/app/src/main/java/foundation/e/drive/utils/AppConstants.java +++ b/app/src/main/java/foundation/e/drive/utils/AppConstants.java @@ -40,7 +40,7 @@ public abstract class AppConstants { 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 +")"; -- GitLab From 4ee407908cddfb09133922798552057c015a5920 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 3 Mar 2022 10:26:03 +0100 Subject: [PATCH 06/10] clean InitializerService.java - remove useless comment - remove useless import --- .../e/drive/services/InitializerService.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 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 289229b2..89ae39f4 100644 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java @@ -36,7 +36,6 @@ import foundation.e.drive.receivers.ScreenOffReceiver; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.JobUtils; -import foundation.e.drive.utils.ServiceExceptionHandler; import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR; import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; @@ -50,9 +49,9 @@ import androidx.annotation.Nullable; */ public class InitializerService extends Service implements OnRemoteOperationListener { final private String TAG = InitializerService.class.getSimpleName(); - //Complex properties - private int existingRemoteFolderCounter; //@dev-only; Temporarily used to know if all remotePath exist - private List mSyncedFolders; //syncedFolders + + private int existingRemoteFolderCounter; //Temporarily used to know if all remotePath exist + private List mSyncedFolders; private OwnCloudClient mCloudClient; private Handler mHandler; private Account mAccount; @@ -63,7 +62,6 @@ public class InitializerService extends Service implements OnRemoteOperationList Log.i(TAG, "onCreate()"); super.onCreate(); this.existingRemoteFolderCounter = 0; - //JobUtils.scheduleInitializerJob(getApplicationContext()); } @Override @@ -86,7 +84,6 @@ public class InitializerService extends Service implements OnRemoteOperationList accountName = intent.getExtras().getString( AccountManager.KEY_ACCOUNT_NAME, "" ); accountType = intent.getExtras().getString( AccountManager.KEY_ACCOUNT_TYPE, "" ); - //If data come from intent, store them into pref because there aren't stored prefs.edit().putString( AccountManager.KEY_ACCOUNT_NAME, accountName ) .putString( AccountManager.KEY_ACCOUNT_TYPE, accountType ) .apply(); @@ -94,7 +91,6 @@ public class InitializerService extends Service implements OnRemoteOperationList if(accountName.isEmpty() ) { Log.w(TAG, "Account's name not found. Neither in shared prefs nor in intent's extras"); - //JobUtils.stopScheduledJob(getApplicationContext(), JobUtils.InitializerJobId); stopSelf(); }else{ this.mAccount = CommonUtils.getAccount( accountName, accountType, AccountManager.get(this) ); @@ -123,7 +119,6 @@ public class InitializerService extends Service implements OnRemoteOperationList return; } - //Get categories of element to sync List syncCategories = new ArrayList<>(); if (CommonUtils.isMediaSyncEnabled(mAccount)) { @@ -134,7 +129,6 @@ public class InitializerService extends Service implements OnRemoteOperationList syncCategories.addAll(Arrays.asList(SETTINGS_SYNCABLE_CATEGORIES)); } - //Get SyncedFolders getInitialSyncedFolders(syncCategories); this.existingRemoteFolderCounter = 0; @@ -213,7 +207,6 @@ public class InitializerService extends Service implements OnRemoteOperationList this.restartFolderCreationCounter = 0; if( this.mSyncedFolders == null || this.mSyncedFolders.isEmpty() ){ - //JobUtils.stopScheduledJob(getApplicationContext(), JobUtils.InitializerJobId); this.stopSelf(); } -- GitLab From fabf2ff8aa753e162d83dfb58ae49384f784e131 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 3 Mar 2022 10:27:22 +0100 Subject: [PATCH 07/10] Add usage of WorkManager to enqueue FullScanWorker in InitializerService - Instanciate Constraint for the workRequest (Battery not low, and network available) - Instanciate PeriodicWorkRequest to start FullScanWorker job every 31 minutes with 5 minutes as flex time - Enqueue the workRequest into WorkManager - Add the generic Generic work Tag into the work request - Comment "JobUtils.scheduleScannerJob()" to prevent JobScheduler to run ScannerJob periodically --- .../e/drive/services/InitializerService.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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 89ae39f4..e3cd4390 100644 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java @@ -29,6 +29,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.operations.CreateInitialFolderRemoteOperation; @@ -36,6 +37,7 @@ import foundation.e.drive.receivers.ScreenOffReceiver; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.JobUtils; +import foundation.e.drive.work.FullScanWorker; import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR; import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; @@ -43,6 +45,11 @@ import static foundation.e.drive.utils.AppConstants.MEDIA_SYNCABLE_CATEGORIES; import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES; import androidx.annotation.Nullable; +import androidx.work.Constraints; +import androidx.work.NetworkType; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; +import androidx.work.WorkRequest; /** * @author Vincent Bourgmayer @@ -277,9 +284,24 @@ public class InitializerService extends Service implements OnRemoteOperationList .putInt( INITIALFOLDERS_NUMBER, mSyncedFolders.size() ) .apply(); + Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .setRequiresBatteryNotLow(true) + .build(); + + WorkRequest periodicFullScanRequest = + new PeriodicWorkRequest.Builder(FullScanWorker.class, + 31, TimeUnit.MINUTES, + 5, TimeUnit.MINUTES) + .setConstraints(constraints) + .addTag(AppConstants.WORK_GENERIC_TAG) + .build(); + + WorkManager.getInstance(this).enqueue(periodicFullScanRequest); + //all folder have been created //JobUtils.stopScheduledJob(appContext, JobUtils.InitializerJobId); - JobUtils.scheduleScannerJob(appContext); + //JobUtils.scheduleScannerJob(appContext); Log.d(TAG, "RegisterReceiver: screenOffReceiver"); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); -- GitLab From b10255e0ef757b04fb7f59322231bf4d57ba4a7e Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 3 Mar 2022 10:43:34 +0100 Subject: [PATCH 08/10] Cancel all Work Enqueued when user account is removed and optimize WorkRequest registration - Cancel all work enqueued using the work generic tag in ResetService.java - Add method "RegisterPeriodicFullScanWorker() in CommonUtils - Replace code in InitializerService by CommonUtils.RegisterPeriodicFullScanWorker - Remove Remove unused import --- .../e/drive/services/InitializerService.java | 28 ++++-------------- .../e/drive/services/ResetService.java | 4 +++ .../foundation/e/drive/utils/CommonUtils.java | 29 ++++++++++++++++++- .../e/drive/work/FullScanWorker.java | 1 + 4 files changed, 38 insertions(+), 24 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 e3cd4390..11c48174 100644 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java @@ -11,10 +11,12 @@ package foundation.e.drive.services; import android.accounts.Account; import android.accounts.AccountManager; import android.app.Service; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Build; import android.os.Environment; import android.os.Handler; @@ -26,10 +28,10 @@ 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 java.util.concurrent.TimeUnit; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.operations.CreateInitialFolderRemoteOperation; @@ -37,7 +39,6 @@ import foundation.e.drive.receivers.ScreenOffReceiver; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.JobUtils; -import foundation.e.drive.work.FullScanWorker; import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR; import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; @@ -45,11 +46,7 @@ import static foundation.e.drive.utils.AppConstants.MEDIA_SYNCABLE_CATEGORIES; import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES; import androidx.annotation.Nullable; -import androidx.work.Constraints; -import androidx.work.NetworkType; -import androidx.work.PeriodicWorkRequest; import androidx.work.WorkManager; -import androidx.work.WorkRequest; /** * @author Vincent Bourgmayer @@ -275,8 +272,7 @@ public class InitializerService extends Service implements OnRemoteOperationList **/ private void doLastStep(){ Log.i(TAG, "doLastStep()"); - - Context appContext = getApplicationContext(); + final Context appContext = getApplicationContext(); appContext.getSharedPreferences( AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE ) .edit() @@ -284,20 +280,7 @@ public class InitializerService extends Service implements OnRemoteOperationList .putInt( INITIALFOLDERS_NUMBER, mSyncedFolders.size() ) .apply(); - Constraints constraints = new Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .setRequiresBatteryNotLow(true) - .build(); - - WorkRequest periodicFullScanRequest = - new PeriodicWorkRequest.Builder(FullScanWorker.class, - 31, TimeUnit.MINUTES, - 5, TimeUnit.MINUTES) - .setConstraints(constraints) - .addTag(AppConstants.WORK_GENERIC_TAG) - .build(); - - WorkManager.getInstance(this).enqueue(periodicFullScanRequest); + CommonUtils.registerPeriodicFullScanWorker(WorkManager.getInstance(appContext)); //all folder have been created //JobUtils.stopScheduledJob(appContext, JobUtils.InitializerJobId); @@ -314,7 +297,6 @@ public class InitializerService extends Service implements OnRemoteOperationList stopSelf(); - } @Override diff --git a/app/src/main/java/foundation/e/drive/services/ResetService.java b/app/src/main/java/foundation/e/drive/services/ResetService.java index dad3cb20..e0e1bb53 100644 --- a/app/src/main/java/foundation/e/drive/services/ResetService.java +++ b/app/src/main/java/foundation/e/drive/services/ResetService.java @@ -27,6 +27,7 @@ import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; import static foundation.e.drive.utils.AppConstants.INITIALIZATION_HAS_BEEN_DONE; import androidx.annotation.Nullable; +import androidx.work.WorkManager; /** * @author Vincent Bourgmayer @@ -67,6 +68,9 @@ public class ResetService extends Service { //2. Stop the scheduledJob JobUtils.stopScheduledJob(this, JobUtils.ScannerJobId); //JobUtils.stopScheduledJob(this, JobUtils.InitializerJobId); + + WorkManager.getInstance(this).cancelAllWorkByTag(AppConstants.WORK_GENERIC_TAG); + //3. delete DB result = this.deleteDatabase(DbHelper.DATABASE_NAME); Log.d(TAG, "Remove Database: "+result); 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 e0f762fc..59406ac8 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -17,7 +17,6 @@ import android.content.ContentResolver; import android.content.Context; import android.media.MediaScannerConnection; import android.net.ConnectivityManager; -import android.net.Network; import android.net.NetworkInfo; import android.net.Uri; import android.util.Log; @@ -31,13 +30,20 @@ import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.resources.files.FileUtils; import java.io.File; +import java.util.concurrent.TimeUnit; import foundation.e.drive.receivers.ScreenOffReceiver; +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 androidx.annotation.NonNull; +import androidx.work.Constraints; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.NetworkType; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; /** @@ -293,4 +299,25 @@ public abstract class CommonUtils { + "\n File can be read?: " + f.canRead() + "\n File can be written?: " + f.canWrite(); } + + /** + * Enqueue a unique periodic worker to look for file to be synchronized (remote files + local files + * @param workManager the instance of workManager + */ + public static void registerPeriodicFullScanWorker(WorkManager workManager){ + final Constraints constraints = new Constraints.Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .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); + } } \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/work/FullScanWorker.java b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java index dd538e1e..d05b7b6c 100644 --- a/app/src/main/java/foundation/e/drive/work/FullScanWorker.java +++ b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java @@ -29,6 +29,7 @@ import foundation.e.drive.services.ObserverService; */ public class FullScanWorker extends Worker { final private static String TAG = FullScanWorker.class.getSimpleName(); + final public static String UNIQUE_WORK_NAME="FullScan"; public FullScanWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); -- GitLab From b270cfa146bc3e07e836a39e4187cf21e25a6cdc Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 3 Mar 2022 17:57:29 +0100 Subject: [PATCH 09/10] FullScanWorker doesn't send intent to ObserverService when synchronization is disabled --- .../e/drive/work/FullScanWorker.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/work/FullScanWorker.java b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java index d05b7b6c..b67151c8 100644 --- a/app/src/main/java/foundation/e/drive/work/FullScanWorker.java +++ b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java @@ -8,8 +8,11 @@ package foundation.e.drive.work; +import android.accounts.Account; +import android.accounts.AccountManager; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.util.Log; import androidx.annotation.NonNull; @@ -17,6 +20,8 @@ import androidx.work.Worker; import androidx.work.WorkerParameters; import foundation.e.drive.services.ObserverService; +import foundation.e.drive.utils.AppConstants; +import foundation.e.drive.utils.CommonUtils; /** * As a first step, this class must replace foundation.e.drive.jobs.ScannerJob @@ -39,8 +44,20 @@ public class FullScanWorker extends Worker { @Override public Result doWork() { Log.d(TAG, "doWork(): going to send intent to ObserverService"); - Intent observerServiceIntent = new Intent(this.getApplicationContext(), ObserverService.class); - this.getApplicationContext().startService(observerServiceIntent); + final 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, "" ); + + final Account mAccount = CommonUtils.getAccount( accountName, accountType, AccountManager.get(this.getApplicationContext()) ); + + if(mAccount != null && CommonUtils.isSettingsSyncEnabled(mAccount) && CommonUtils.isMediaSyncEnabled(mAccount)) { + final Intent observerServiceIntent = new Intent(this.getApplicationContext(), ObserverService.class); + this.getApplicationContext().startService(observerServiceIntent); + }else{ + Log.w(TAG, "Intent for ObserverService not send : account is null or \"settings sync\" & \"media sync\" settings are disabled"); + } + return Result.success(); } } -- GitLab From 840bf519279aa532c8e8405ef6af89cf0d8d7902 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 4 Mar 2022 10:07:26 +0100 Subject: [PATCH 10/10] fix spacing in if/else statement and fix 'final' word placement --- .../e/drive/services/InitializerService.java | 38 +++++++++---------- .../foundation/e/drive/utils/CommonUtils.java | 4 +- .../e/drive/work/FullScanWorker.java | 10 ++--- 3 files changed, 25 insertions(+), 27 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 11c48174..8939223c 100644 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java @@ -11,12 +11,10 @@ package foundation.e.drive.services; import android.accounts.Account; import android.accounts.AccountManager; import android.app.Service; -import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; -import android.net.Uri; import android.os.Build; import android.os.Environment; import android.os.Handler; @@ -52,7 +50,7 @@ import androidx.work.WorkManager; * @author Vincent Bourgmayer */ public class InitializerService extends Service implements OnRemoteOperationListener { - final private String TAG = InitializerService.class.getSimpleName(); + private final String TAG = InitializerService.class.getSimpleName(); private int existingRemoteFolderCounter; //Temporarily used to know if all remotePath exist private List mSyncedFolders; @@ -76,10 +74,10 @@ public class InitializerService extends Service implements OnRemoteOperationList //Get account SharedPreferences prefs = this.getSharedPreferences( AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE ); - if( prefs.getBoolean( AppConstants.INITIALIZATION_HAS_BEEN_DONE, false ) ) { + if (prefs.getBoolean( AppConstants.INITIALIZATION_HAS_BEEN_DONE, false ) ) { JobUtils.scheduleScannerJob(this); Log.w(TAG, "Initializer has already been run"); - }else{ + } else { String accountName = prefs.getString( AccountManager.KEY_ACCOUNT_NAME, "" ); String accountType = prefs.getString( AccountManager.KEY_ACCOUNT_TYPE, "" ); @@ -93,16 +91,16 @@ public class InitializerService extends Service implements OnRemoteOperationList .apply(); } - if(accountName.isEmpty() ) { + if (accountName.isEmpty() ) { Log.w(TAG, "Account's name not found. Neither in shared prefs nor in intent's extras"); stopSelf(); - }else{ + } else { this.mAccount = CommonUtils.getAccount( accountName, accountType, AccountManager.get(this) ); //Get OwnCloudlient if (this.mAccount != null) { this.mCloudClient = CommonUtils.getOwnCloudClient( this.mAccount, getApplicationContext()); start(); - }else { + } else { Log.w(TAG, "Got account is invalid."); stopSelf(); } @@ -193,7 +191,7 @@ public class InitializerService extends Service implements OnRemoteOperationList false, true, false) ); - }catch (Exception e){ Log.e(TAG, e.toString()); } + } catch (Exception e){ Log.e(TAG, e.toString()); } break; } } @@ -210,14 +208,14 @@ public class InitializerService extends Service implements OnRemoteOperationList Log.i(TAG, "createNextRemoteFolder()"); this.restartFolderCreationCounter = 0; - if( this.mSyncedFolders == null || this.mSyncedFolders.isEmpty() ){ + if (this.mSyncedFolders == null || this.mSyncedFolders.isEmpty() ){ this.stopSelf(); } //It means that there are still folders to create - if( this.existingRemoteFolderCounter < this.mSyncedFolders.size() ){ + if (this.existingRemoteFolderCounter < this.mSyncedFolders.size() ){ - if( this.mHandler == null ) this.mHandler = new Handler(); + if (this.mHandler == null ) this.mHandler = new Handler(); CreateInitialFolderRemoteOperation createFolderOperation = new CreateInitialFolderRemoteOperation( @@ -227,11 +225,11 @@ public class InitializerService extends Service implements OnRemoteOperationList createFolderOperation.execute(this.mCloudClient, this, this.mHandler); - }else if(this.existingRemoteFolderCounter == this.mSyncedFolders.size() ){ + } else if (this.existingRemoteFolderCounter == this.mSyncedFolders.size() ){ doLastStep(); - }else{ + } else { Log.e(TAG, "this.existingRemoteFolderCounter : "+this.existingRemoteFolderCounter+" > this.mSyncedFolders.size() : "+this.mSyncedFolders.size() ); this.stopSelf(); } @@ -240,26 +238,26 @@ public class InitializerService extends Service implements OnRemoteOperationList @Override public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { Log.i(TAG, "onRemoteOperationFinish()"); - if(operation instanceof CreateInitialFolderRemoteOperation){ + if (operation instanceof CreateInitialFolderRemoteOperation){ - if(result.isSuccess() || result.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS){ + 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 + } else if (result.getHttpCode() == 423 || result.getHttpCode() == 409){//file locked or conflict in result Log.e( TAG, result.getLogMessage() ); - if( this.restartFolderCreationCounter < 3) { + if (this.restartFolderCreationCounter < 3) { Log.w( TAG, " restart operation" ); operation.execute( this.mCloudClient, this, this.mHandler ); this.restartFolderCreationCounter+=1; - }else{ + } else { Log.e(TAG, "Remote folder's creation failed due to conflict with server"); stopSelf(); } - }else{ + } else { Log.e(TAG, result.getLogMessage()+" "+result.getHttpCode() ); stopSelf(); } 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 59406ac8..5ba7a072 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -51,7 +51,7 @@ import androidx.work.WorkManager; */ public abstract class CommonUtils { - final private static String TAG = CommonUtils.class.getSimpleName(); + private final static String TAG = CommonUtils.class.getSimpleName(); /** * Set ServiceUncaughtExceptionHandler to be the MainThread Exception Handler @@ -169,7 +169,7 @@ public abstract class CommonUtils { oc.setCredentials(new OwnCloudBasicCredentials(account.name, AccountManager.get(context).getPassword(account))); Log.d(TAG, "user agent: "+AppConstants.USER_AGENT); - if(!AppConstants.USER_AGENT.equals(OwnCloudClientManagerFactory.getUserAgent())) { + if (!AppConstants.USER_AGENT.equals(OwnCloudClientManagerFactory.getUserAgent()) ){ OwnCloudClientManagerFactory.setUserAgent(AppConstants.USER_AGENT); } diff --git a/app/src/main/java/foundation/e/drive/work/FullScanWorker.java b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java index b67151c8..08d5b875 100644 --- a/app/src/main/java/foundation/e/drive/work/FullScanWorker.java +++ b/app/src/main/java/foundation/e/drive/work/FullScanWorker.java @@ -33,8 +33,8 @@ import foundation.e.drive.utils.CommonUtils; * @author Vincent Bourgmayer */ public class FullScanWorker extends Worker { - final private static String TAG = FullScanWorker.class.getSimpleName(); - final public static String UNIQUE_WORK_NAME="FullScan"; + private final static String TAG = FullScanWorker.class.getSimpleName(); + public final static String UNIQUE_WORK_NAME="FullScan"; public FullScanWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); @@ -51,13 +51,13 @@ public class FullScanWorker extends Worker { final Account mAccount = CommonUtils.getAccount( accountName, accountType, AccountManager.get(this.getApplicationContext()) ); - if(mAccount != null && CommonUtils.isSettingsSyncEnabled(mAccount) && CommonUtils.isMediaSyncEnabled(mAccount)) { + if (mAccount != null && CommonUtils.isSettingsSyncEnabled(mAccount) && CommonUtils.isMediaSyncEnabled(mAccount)) { final Intent observerServiceIntent = new Intent(this.getApplicationContext(), ObserverService.class); this.getApplicationContext().startService(observerServiceIntent); - }else{ + } else { Log.w(TAG, "Intent for ObserverService not send : account is null or \"settings sync\" & \"media sync\" settings are disabled"); } return Result.success(); } -} +} \ No newline at end of file -- GitLab