diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ec966a21a2736438a0b7faa9d948c36443b78489..177f5488b06b08e947ab429251ff9d7f6bdd5ff8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,6 +28,9 @@ android:protectionLevel="signature" tools:ignore="ReservedSystemPermission" /> + + - - - - - - - @@ -115,12 +107,21 @@ - + + + + + + \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/account/receivers/AccountAddedReceiver.kt b/app/src/main/java/foundation/e/drive/account/receivers/AccountAddedReceiver.kt new file mode 100644 index 0000000000000000000000000000000000000000..f14a3e0647317de56285487ae63fcf681f8373ef --- /dev/null +++ b/app/src/main/java/foundation/e/drive/account/receivers/AccountAddedReceiver.kt @@ -0,0 +1,136 @@ +/* + * Copyright © MURENA SAS 2023. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ +package foundation.e.drive.account.receivers + +import android.accounts.AccountManager +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import androidx.work.OneTimeWorkRequest +import androidx.work.WorkManager +import foundation.e.drive.R +import foundation.e.drive.models.SyncedFolder +import foundation.e.drive.utils.AppConstants +import foundation.e.drive.utils.CommonUtils +import foundation.e.drive.utils.DavClientProvider +import foundation.e.drive.utils.RootSyncedFolderProvider +import foundation.e.drive.work.WorkRequestFactory.* +import timber.log.Timber + +/** + * Entry point for eDrive + * Triggered by AccountManager + * @author Vincent Bourgmayer + */ +class AccountAddedReceiver() : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + Timber.d("\"Account added\" intent received") + + if (context == null || intent == null || intent.extras == null) return + + val extras = intent.extras!! + val accountName = extras.getString(AccountManager.KEY_ACCOUNT_NAME, "") + val accountType = extras.getString(AccountManager.KEY_ACCOUNT_TYPE, "") + val prefs = context.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, + Context.MODE_PRIVATE) + + Timber.d("AccountAddedReceiver.onReceive with name: %s and type: %s", accountName, accountType) + + if (!canStart(accountName, accountType, prefs, context)) return + + prefs.edit() + .putString(AccountManager.KEY_ACCOUNT_NAME, accountName) + .apply() + + if (registerSetupWorkers(context)) { + DavClientProvider.getInstance().cleanUp() + + CommonUtils.registerPeriodicUserInfoChecking(WorkManager.getInstance(context)) + } +} + + /** + * Check that conditions to start are met: + * - Setup has not already been done + * - AccountName is not empty + * - AccountType is /e/ account + * - the account is effectively available through accountManager + */ + private fun canStart( + accountName: String, + accountType: String, + prefs: SharedPreferences, + context: Context + ): Boolean { + if (isSetupAlreadyDone(prefs)) { + return false + } + + if (accountName.isEmpty()) { + return false + } + + if (isInvalidAccountType(accountType, context)) { + return false + } + + if (!isExistingAccount(accountName, accountType, context)) { + Timber.w("No account exist for username: %s ", accountType, accountName) + return false + } + return true + } + + private fun isInvalidAccountType(accountType: String, context: Context): Boolean { + val validAccountType = context.getString(R.string.eelo_account_type) + return accountType != validAccountType + } + + private fun isSetupAlreadyDone(prefs: SharedPreferences): Boolean { + return prefs.getBoolean(AppConstants.SETUP_COMPLETED, false) + } + + private fun isExistingAccount(accountName: String, accountType: String, context: Context): Boolean { + val account = CommonUtils.getAccount(accountName, accountType, AccountManager.get(context)) + return account != null + } + + private fun registerSetupWorkers(context: Context): Boolean { + val rootFolderSetupWorkers = generateRootFolderSetupWorkers(context) ?: return false + val getUserInfoRequest = getOneTimeWorkRequest(WorkType.ONE_TIME_USER_INFO, null) + val firstStartRequest = getOneTimeWorkRequest(WorkType.FIRST_START, null) + + val workManager = WorkManager.getInstance(context) + + workManager.beginWith(getUserInfoRequest) + .then(rootFolderSetupWorkers) + .then(firstStartRequest) + .enqueue() + return true + } + + private fun generateRootFolderSetupWorkers(context: Context): MutableList? { + val rootSyncedFolderList: List = + RootSyncedFolderProvider.getSyncedFolderRoots(context) + + if (rootSyncedFolderList.isNullOrEmpty()) { + return null + } + + val workRequests: MutableList = ArrayList() + for (folder in rootSyncedFolderList) { + val createRemoteFolderWorkRequest = getOneTimeWorkRequest( + WorkType.CREATE_REMOTE_DIR, + folder + ) + workRequests.add(createRemoteFolderWorkRequest) + } + return workRequests + } +} \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/receivers/AccountRemoveCallbackReceiver.java b/app/src/main/java/foundation/e/drive/account/receivers/AccountRemoveCallbackReceiver.java similarity index 92% rename from app/src/main/java/foundation/e/drive/receivers/AccountRemoveCallbackReceiver.java rename to app/src/main/java/foundation/e/drive/account/receivers/AccountRemoveCallbackReceiver.java index fc7293f74ab57c99652d53801fd0c6cb5a68a999..3363425bf2e82d044f602bf7b5cc1c3ecc6bb955 100644 --- a/app/src/main/java/foundation/e/drive/receivers/AccountRemoveCallbackReceiver.java +++ b/app/src/main/java/foundation/e/drive/account/receivers/AccountRemoveCallbackReceiver.java @@ -6,10 +6,10 @@ * http://www.gnu.org/licenses/gpl.html */ -package foundation.e.drive.receivers; +package foundation.e.drive.account.receivers; import static foundation.e.drive.utils.AppConstants.INITIAL_FOLDER_NUMBER; -import static foundation.e.drive.utils.AppConstants.INITIALIZATION_HAS_BEEN_DONE; +import static foundation.e.drive.utils.AppConstants.SETUP_COMPLETED; import android.accounts.AccountManager; import android.annotation.SuppressLint; @@ -26,7 +26,6 @@ import java.io.File; import foundation.e.drive.EdriveApplication; import foundation.e.drive.database.DbHelper; import foundation.e.drive.database.FailedSyncPrefsManager; -import foundation.e.drive.services.InitializerService; import foundation.e.drive.services.ObserverService; import foundation.e.drive.services.SynchronizationService; import foundation.e.drive.utils.AppConstants; @@ -100,10 +99,6 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver { boolean observerServiceStopResult = applicationContext.stopService(observerServiceIntent); Timber.d("stop ObserverService: %s", observerServiceStopResult); - Intent initializerServiceIntent = new Intent(applicationContext, InitializerService.class); - boolean initServiceStopResult = applicationContext.stopService(initializerServiceIntent); - Timber.d("stop InitializerService: %s", initServiceStopResult); - Intent synchronizationServiceIntent = new Intent(applicationContext, SynchronizationService.class); boolean syncServiceStopResult = applicationContext.stopService(synchronizationServiceIntent); Timber.d("stop SynchronizationService: %s", syncServiceStopResult); @@ -114,7 +109,7 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver { //If removal failed, clear all data inside prefs.edit().remove(AccountManager.KEY_ACCOUNT_NAME) .remove(AccountManager.KEY_ACCOUNT_TYPE) - .remove(INITIALIZATION_HAS_BEEN_DONE) + .remove(SETUP_COMPLETED) .remove(INITIAL_FOLDER_NUMBER) .remove(AppConstants.KEY_LAST_SYNC_TIME) .apply(); diff --git a/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java b/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java index 12593beed998e4aeb44a896bae643a08d63572b0..ce806ce1d6f92f8981b022ce16525592c8ec092e 100644 --- a/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java @@ -18,7 +18,6 @@ import androidx.annotation.NonNull; import foundation.e.drive.BuildConfig; import foundation.e.drive.database.DbHelper; -import foundation.e.drive.services.InitializerService; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import timber.log.Timber; @@ -39,11 +38,11 @@ public class BootCompletedReceiver extends BroadcastReceiver { if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { final String currentDateProp = CommonUtils.getProp(DATE_SYSTEM_PROPERTY); - if (isOsUpdated(pref, currentDateProp)) { // App is persistent and system so do not have classical update - handleOsUpdate(pref, context, currentDateProp); + if (isOsUpdated(pref, currentDateProp)) { // App is persistent so can only be updated (by replacement) on OS update + handleOsUpdate(context); } - if (pref.getBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, false) + if (pref.getBoolean(AppConstants.SETUP_COMPLETED, false) && BuildConfig.VERSION_CODE > pref.getInt(PREF_VERSION_CODE, 1002000)) { pref.edit().putInt(PREF_VERSION_CODE, BuildConfig.VERSION_CODE).apply(); try { @@ -62,17 +61,10 @@ public class BootCompletedReceiver extends BroadcastReceiver { /** * Force reinitialization, upgrade of DB in case of OS update - * - * @param prefs SharedPreferences to extract last known build date + * todo remove when setPersistentFlag=true will be removed * @param context Context used to start InitializationService - * @param currentDateProp new build date */ - private void handleOsUpdate(@NonNull SharedPreferences prefs, @NonNull Context context, @NonNull String currentDateProp) { - prefs.edit().putString(DATE_SYSTEM_PROPERTY, currentDateProp) - .putBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, false) - .apply(); - - context.startService(new Intent(context, InitializerService.class)); + private void handleOsUpdate(@NonNull Context context) { forceDBUpdate(context); } diff --git a/app/src/main/java/foundation/e/drive/services/InitializerService.java b/app/src/main/java/foundation/e/drive/services/InitializerService.java deleted file mode 100644 index cafe34ffb39fabb601a3ae17a1999c0a2bfb7fc9..0000000000000000000000000000000000000000 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright © CLEUS SAS 2018-2019. - * Copyright © ECORP SAS 2022-2023. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Public License v3.0 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/gpl.html - */ - -package foundation.e.drive.services; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.app.Service; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.IBinder; - -import java.io.File; -import java.util.List; - -import foundation.e.drive.models.SyncedFolder; -import foundation.e.drive.utils.AppConstants; -import foundation.e.drive.utils.CommonUtils; -import foundation.e.drive.utils.DavClientProvider; -import foundation.e.drive.utils.RootSyncedFolderProvider; -import timber.log.Timber; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.VisibleForTesting; -import androidx.work.WorkManager; - -/** - * @author Vincent Bourgmayer - * @author Jonathan Klee - * @author Abhishek Aggarwal - */ -public class InitializerService extends Service { - private Account account; - - @Override - public void onCreate() { - Timber.tag(InitializerService.class.getSimpleName()); - super.onCreate(); - } - - @Override - public int onStartCommand(@Nullable Intent intent, int flags, int startId) { - Timber.i("onStartCommand()"); - DavClientProvider.getInstance().cleanUp(); - CommonUtils.setServiceUnCaughtExceptionHandler(this); - - //Get account - final SharedPreferences prefs = this.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); - - String accountName = prefs.getString(AccountManager.KEY_ACCOUNT_NAME, ""); - String accountType = prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, ""); - - if (accountName.isEmpty() && accountType.isEmpty() && intent != null && intent.getExtras() != null) { - - accountName = intent.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME, ""); - accountType = intent.getExtras().getString(AccountManager.KEY_ACCOUNT_TYPE, ""); - - prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, accountName) - .putString(AccountManager.KEY_ACCOUNT_TYPE, accountType) - .apply(); - } - - if (checkStartConditions(prefs, accountName, accountType)) { - start(); - } - return super.onStartCommand(intent, flags, startId); - } - - /** - * Check if condition are present to start - * - Initialization not already done - * - AccountName is not empty - * - Account available - * @return true if condition are met - */ - @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) - public boolean checkStartConditions(@NonNull final SharedPreferences prefs, - @NonNull final String accountName, @NonNull final String accountType) { - if (prefs.getBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, false)) { - Timber.w("Initialization has already been done"); - return false; - } - - if (accountName.isEmpty()) { - Timber.w("No account Name available"); - return false; - } - - account = CommonUtils.getAccount(accountName, accountType, AccountManager.get(this)); - if (account == null) { - Timber.w("got Invalid %s account for username: %s ", accountType, accountName); - return false; - } - return true; - } - - /** - * Set up base component for eDrive: - * - Register basic worker - * - build root folders to sync - */ - private void start() { - Timber.d("start()"); - CommonUtils.registerPeriodicUserInfoChecking(WorkManager.getInstance(this)); - - final List syncedFolders = RootSyncedFolderProvider.INSTANCE.getSyncedFolderRoots(getApplicationContext()); - CommonUtils.registerInitializationWorkers(syncedFolders, WorkManager.getInstance(getApplicationContext()) ); - } - - @Nullable - @Override - public IBinder onBind(@Nullable Intent intent) { - return null; - } -} \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java index 8b1d6747c01413d0dd5d87d276e4be22f200eb89..813b02b954fabea1519dcd631149e109057dab1e 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -9,7 +9,7 @@ package foundation.e.drive.services; -import static foundation.e.drive.utils.AppConstants.INITIALIZATION_HAS_BEEN_DONE; +import static foundation.e.drive.utils.AppConstants.SETUP_COMPLETED; import android.accounts.Account; import android.accounts.AccountManager; @@ -36,6 +36,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import foundation.e.drive.R; import foundation.e.drive.contentScanner.LocalContentScanner; import foundation.e.drive.contentScanner.LocalFileLister; import foundation.e.drive.contentScanner.RemoteContentScanner; @@ -112,7 +113,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene final SharedPreferences prefs = this.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 String accountType = getApplicationContext().getString(R.string.eelo_account_type); this.mAccount = CommonUtils.getAccount(accountName, accountType, AccountManager.get(this)); forcedSync = intent != null && DebugCmdReceiver.ACTION_FORCE_SYNC.equals(intent.getAction()); @@ -150,10 +151,8 @@ public class ObserverService extends Service implements OnRemoteOperationListene return false; } - if (!prefs.getBoolean(INITIALIZATION_HAS_BEEN_DONE, false)) { - Timber.d("Initialization hasn't been done"); - Intent initializerIntent = new Intent(this, InitializerService.class); - startService(initializerIntent); + if (!prefs.getBoolean(SETUP_COMPLETED, false)) { + Timber.d("setup hasn't been done"); return false; } diff --git a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java index 11f6c7f0083f80b049ae7b5165c1987e89b0437b..08b7f76c44c8d2ed7ff878cddb12be19fd7da4ec 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -33,6 +33,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedDeque; +import foundation.e.drive.R; import foundation.e.drive.database.DbHelper; import foundation.e.drive.database.FailedSyncPrefsManager; import foundation.e.drive.models.SyncRequest; @@ -78,7 +79,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation final SharedPreferences prefs = this.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 String accountType = getApplicationContext().getString(R.string.eelo_account_type); account = CommonUtils.getAccount(accountName, accountType, AccountManager.get(this)); if (account == null) { diff --git a/app/src/main/java/foundation/e/drive/utils/AppConstants.kt b/app/src/main/java/foundation/e/drive/utils/AppConstants.kt index fbf2dc62be05f4b615aeb72e19d59a7017970890..49d2b5d636b20375ac27eb63fd22fd116083d35c 100644 --- a/app/src/main/java/foundation/e/drive/utils/AppConstants.kt +++ b/app/src/main/java/foundation/e/drive/utils/AppConstants.kt @@ -22,7 +22,7 @@ object AppConstants { const val SETTINGS_SYNC_PROVIDER_AUTHORITY = "foundation.e.drive.providers.SettingsSyncProvider" const val METERED_NETWORK_ALLOWED_AUTHORITY = "foundation.e.drive.providers.MeteredConnectionAllowedProvider" - const val INITIALIZATION_HAS_BEEN_DONE = "initService_has_run" + const val SETUP_COMPLETED = "initService_has_run" const val INITIAL_FOLDER_NUMBER = "initial_folder_number" const val APPLICATIONS_LIST_FILE_NAME = "packages_list.csv" const val SHARED_PREFERENCE_NAME = "preferences" 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 b5eb5fe5524f1c641f7e308bdb33a41150a73788..1d6ec907f01c140beead7dce0c18e6a17865cbf0 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -200,44 +200,6 @@ public abstract class CommonUtils { return String.format(Locale.ENGLISH, "%.1f %cB", value / 1024.0, ci.current()); } - /** - * 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(@Nullable List syncedFolders, @NonNull WorkManager workManager) { - if (syncedFolders == null || syncedFolders.isEmpty()) { - Timber.d("registerInitializationWorkers: Can't create remote folders. List is empty"); - return; - } - - final OneTimeWorkRequest getUserInfoRequest = WorkRequestFactory.getOneTimeWorkRequest(WorkRequestFactory.WorkType.ONE_TIME_USER_INFO, null); - - final List workRequests = new ArrayList<>(); - for (SyncedFolder folder : syncedFolders) { - final OneTimeWorkRequest createRemoteFolderWorkRequest = WorkRequestFactory.getOneTimeWorkRequest(WorkRequestFactory.WorkType.CREATE_REMOTE_DIR, folder); - workRequests.add(createRemoteFolderWorkRequest); - } - - final OneTimeWorkRequest firstStartRequest = WorkRequestFactory.getOneTimeWorkRequest(WorkRequestFactory.WorkType.FIRST_START, null); - - workManager.beginWith(getUserInfoRequest) - .then(workRequests) - .then(firstStartRequest) - .enqueue(); - } - - public static void createNotificationChannel(@NonNull Context context) { final CharSequence name = context.getString(R.string.notif_channel_name); final String description = context.getString(R.string.notif_channel_description); 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 58e3669cf1468cd05a8159cca637076ae48471e2..aaf7f8e5ca345856b8fda4cb1c326c02a4d1afab 100644 --- a/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java +++ b/app/src/main/java/foundation/e/drive/work/FirstStartWorker.java @@ -49,7 +49,7 @@ public class FirstStartWorker extends Worker { appContext.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE) .edit() - .putBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, true) + .putBoolean(AppConstants.SETUP_COMPLETED, true) .putInt(INITIAL_FOLDER_NUMBER, 9) .apply(); diff --git a/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java b/app/src/main/java/foundation/e/drive/work/RootFolderSetupWorker.java similarity index 91% rename from app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java rename to app/src/main/java/foundation/e/drive/work/RootFolderSetupWorker.java index d24339508794fdac4d5bf47fe3a672d49cbd2db4..8ff54f22354017e337a2c49519737a3b8c39f5a9 100644 --- a/app/src/main/java/foundation/e/drive/work/CreateRemoteFolderWorker.java +++ b/app/src/main/java/foundation/e/drive/work/RootFolderSetupWorker.java @@ -25,6 +25,7 @@ import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation; import java.io.File; +import foundation.e.drive.R; import foundation.e.drive.database.DbHelper; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.utils.AppConstants; @@ -33,11 +34,13 @@ import foundation.e.drive.utils.DavClientProvider; import timber.log.Timber; /** - * /!\ Doesn't require NextcloudClient yet * Create folder on ecloud for a given local folder + * Then register it in the database + * + * /i\ Doesn't require NextcloudClient yet * @author Vincent Bourgmayer */ -public class CreateRemoteFolderWorker extends Worker { +public class RootFolderSetupWorker extends Worker { 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"; @@ -49,9 +52,9 @@ public class CreateRemoteFolderWorker extends Worker { public static final String DATA_KEY_ENABLE="enable"; public static final String DATA_KEY_MEDIATYPE="mediaType"; - public CreateRemoteFolderWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { + public RootFolderSetupWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); - Timber.tag(CreateRemoteFolderWorker.class.getSimpleName()); + Timber.tag(RootFolderSetupWorker.class.getSimpleName()); } @NonNull @@ -131,9 +134,9 @@ public class CreateRemoteFolderWorker extends Worker { private Account getAccount() { 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 String accountType = getApplicationContext().getString(R.string.eelo_account_type); - if (accountName.isEmpty() && accountType.isEmpty()) return null; + if (accountName.isEmpty()) return null; return CommonUtils.getAccount(accountName, accountType, AccountManager.get(getApplicationContext())); } } 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 58c87d75572934077c630a43fdcab32ba3681f54..0bef2fd5cd524958f5861cea2d7f6e8c5a4fed01 100644 --- a/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java +++ b/app/src/main/java/foundation/e/drive/work/WorkRequestFactory.java @@ -8,16 +8,16 @@ 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 static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_ENABLE; +import static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_ID; +import static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_LAST_ETAG; +import static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_LAST_MODIFIED; +import static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_LIBELLE; +import static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_LOCAL_PATH; +import static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_MEDIATYPE; +import static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_REMOTE_PATH; +import static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_SCAN_LOCAL; +import static foundation.e.drive.work.RootFolderSetupWorker.DATA_KEY_SCAN_REMOTE; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -183,7 +183,7 @@ public class WorkRequestFactory { final Constraints constraints = createUnmeteredNetworkAndHighBatteryConstraints(); return new OneTimeWorkRequest.Builder( - CreateRemoteFolderWorker.class) + RootFolderSetupWorker.class) .setBackoffCriteria(BackoffPolicy.LINEAR, 2, TimeUnit.MINUTES) .setInputData(createDataFromSyncedFolder(syncedFolder)) .addTag(AppConstants.WORK_GENERIC_TAG) diff --git a/app/src/test/java/foundation/e/drive/services/AbstractServiceIT.java b/app/src/test/java/foundation/e/drive/services/AbstractServiceIT.java index c7b695cbaff9dd52c57e1de54b542e46877bb731..998157602efeb7a6b112107d884f02aedf119506 100644 --- a/app/src/test/java/foundation/e/drive/services/AbstractServiceIT.java +++ b/app/src/test/java/foundation/e/drive/services/AbstractServiceIT.java @@ -111,7 +111,7 @@ public abstract class AbstractServiceIT { * So update the field before to call this to store specific value */ protected void registerSharedPref(){ - sharedPreferences.edit().putBoolean( AppConstants.INITIALIZATION_HAS_BEEN_DONE, init_done) + sharedPreferences.edit().putBoolean( AppConstants.SETUP_COMPLETED, init_done) .putString(AccountManager.KEY_ACCOUNT_NAME, TEST_ACCOUNT_NAME) .putString(AccountManager.KEY_ACCOUNT_TYPE, TEST_ACCOUNT_TYPE) .putInt(AppConstants.INITIAL_FOLDER_NUMBER, initial_folder_number) diff --git a/app/src/test/java/foundation/e/drive/services/InitializerServiceTest.java b/app/src/test/java/foundation/e/drive/services/InitializerServiceTest.java deleted file mode 100644 index ca5febde32ca4d45688701e970af2035ffb5371b..0000000000000000000000000000000000000000 --- a/app/src/test/java/foundation/e/drive/services/InitializerServiceTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package foundation.e.drive.services; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import android.accounts.AccountManager; -import android.app.job.JobScheduler; -import android.content.Context; -import android.net.ConnectivityManager; - -import androidx.test.core.app.ApplicationProvider; -import org.junit.Test; -import org.robolectric.Robolectric; - -import foundation.e.drive.TestUtils; -import foundation.e.drive.database.DbHelper; -import foundation.e.drive.utils.AppConstants; - -public class InitializerServiceTest extends AbstractServiceIT{ - - public InitializerServiceTest(){ - mServiceController = Robolectric.buildService(InitializerService.class); - mService = mServiceController.get(); - context = ApplicationProvider.getApplicationContext(); - accountManager = AccountManager.get(context); - jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); - contentResolver = context.getContentResolver(); - sharedPreferences = context.getSharedPreferences( AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); - connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - dbHelper = new DbHelper(context); - TestUtils.initializeWorkmanager(context); - init_done = false; - TestUtils.loadServerCredentials(); - TestUtils.prepareValidAccount(accountManager); - } - - @Test - public void checkStartConditions_noAccountInIntent_NorInPrefs_falseExpected() { - registerSharedPref(); - final boolean canStart = mService.checkStartConditions(sharedPreferences, "", ""); - assertFalse("InitializerService.checkStartConditions without any available account info should return false but returned true", canStart); - } - - @Test - public void checkStartConditions_trueExpected() { - registerSharedPref(); - final boolean canStart = mService.checkStartConditions(sharedPreferences, TestUtils.TEST_ACCOUNT_NAME, TestUtils.TEST_ACCOUNT_TYPE); - assertTrue("InitializerService.checkStartConditions with invalid account should return false but returned true", canStart); - } - - @Test - public void checkStartConditions_withFakeAccountNameOrType_falseExpected() { - registerSharedPref(); - final boolean canStart = mService.checkStartConditions(sharedPreferences, "any", "any"); - assertFalse("InitializerService.checkStartConditions with invalid account should return false but returned true", canStart); - } - - @Test - public void checkStartConditions_withInitDone_falseExpected() { - registerSharedPref(); - sharedPreferences.edit().putBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, true).apply(); - final boolean canStart = mService.checkStartConditions(sharedPreferences, TestUtils.TEST_ACCOUNT_NAME, TestUtils.TEST_ACCOUNT_TYPE); - assertFalse("InitializerService.checkStartConditions with valid account but init already done should return false but returned true", canStart); - } -} diff --git a/app/src/test/java/foundation/e/drive/services/ObserverServiceTest.java b/app/src/test/java/foundation/e/drive/services/ObserverServiceTest.java index bb9ff1ad2c6988ca46ed7dd90a86752dc151e2b1..e9a9b7076c0f23c54de138250b055d9f61ca5144 100644 --- a/app/src/test/java/foundation/e/drive/services/ObserverServiceTest.java +++ b/app/src/test/java/foundation/e/drive/services/ObserverServiceTest.java @@ -3,7 +3,6 @@ package foundation.e.drive.services; import android.accounts.AccountManager; import android.app.job.JobScheduler; import android.content.Context; -import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; @@ -19,7 +18,6 @@ import org.robolectric.shadows.ShadowLog; import org.robolectric.shadows.ShadowNetworkInfo; import java.io.File; -import android.net.Uri; import java.util.List; import foundation.e.drive.TestUtils; @@ -32,7 +30,7 @@ import foundation.e.drive.utils.CommonUtils; import static foundation.e.drive.TestUtils.TEST_LOCAL_ROOT_FOLDER_PATH; import static foundation.e.drive.TestUtils.TEST_REMOTE_ROOT_FOLDER_PATH; import static foundation.e.drive.TestUtils.getValidAccount; -import static foundation.e.drive.utils.AppConstants.INITIALIZATION_HAS_BEEN_DONE; +import static foundation.e.drive.utils.AppConstants.SETUP_COMPLETED; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -187,8 +185,6 @@ public class ObserverServiceTest extends AbstractServiceIT { //Start the service mServiceController.create().startCommand(0, 0); - //How to assert this... ? - List logs = ShadowLog.getLogs(); ShadowLog.LogItem lastLog = logs.get(logs.size()-1); @@ -253,25 +249,18 @@ public class ObserverServiceTest extends AbstractServiceIT { */ @Ignore("Binding to synchronizationService make test fails") @Test - public void InitializationNotDone_shouldStop() { + public void setupNotDone_shouldStop() { init_done = false; //This is the key settings for this test - setWifiNetworkStatus(); prepareValidAccount(); enableMediaAndSettingsSync(getValidAccount()); registerSharedPref(); - - assertFalse("SharedPref doesn't contains the expected value for Initialization_has_been_done key", sharedPreferences.getBoolean(INITIALIZATION_HAS_BEEN_DONE, true)); + assertFalse("SharedPref doesn't contains the expected value for Initialization_has_been_done key", sharedPreferences.getBoolean(SETUP_COMPLETED, true)); mServiceController.create().startCommand(0, 0); //How to assert this... ? - Intent expectedIntent = new Intent(mService, InitializerService.class); - Intent actualIntent = shadowOf(RuntimeEnvironment.application).getNextStartedService(); - - assertEquals("Checked intent not the expected one", expectedIntent.getComponent(), actualIntent.getComponent()); - List logs = ShadowLog.getLogs(); ShadowLog.LogItem lastLog = logs.get(logs.size()-1);