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 index 4e0b92480f0ba5c34fcf37853ab392e470b447e8..a95d15c63d37382700ab3b2b8e93bc25941550c1 100644 --- a/app/src/main/java/foundation/e/drive/account/receivers/AccountAddedReceiver.kt +++ b/app/src/main/java/foundation/e/drive/account/receivers/AccountAddedReceiver.kt @@ -12,15 +12,11 @@ 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.AccountUtils 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 foundation.e.drive.utils.WorkerUtils import timber.log.Timber /** @@ -48,11 +44,7 @@ class AccountAddedReceiver() : BroadcastReceiver() { .putString(AccountManager.KEY_ACCOUNT_NAME, accountName) .apply() - if (registerSetupWorkers(context)) { - DavClientProvider.getInstance().cleanUp() - - CommonUtils.registerPeriodicUserInfoChecking(WorkManager.getInstance(context)) - } + WorkerUtils.registerSetupWorkers(context) } /** @@ -68,7 +60,7 @@ class AccountAddedReceiver() : BroadcastReceiver() { prefs: SharedPreferences, context: Context ): Boolean { - if (isSetupAlreadyDone(prefs)) { + if (AccountUtils.isSetupAlreadyDone(prefs)) { return false } @@ -92,44 +84,9 @@ class AccountAddedReceiver() : BroadcastReceiver() { 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 finishSetupRequest = getOneTimeWorkRequest(WorkType.ONE_TIME_FINISH_SETUP, null) - val workManager = WorkManager.getInstance(context) - - workManager.beginWith(getUserInfoRequest) - .then(rootFolderSetupWorkers) - .then(finishSetupRequest) - .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 rootFolderSetupWorkRequest = getOneTimeWorkRequest( - WorkType.ONE_TIME_ROOT_FOLDER_SETUP, - folder - ) - workRequests.add(rootFolderSetupWorkRequest) - } - return workRequests - } } \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/account/setup/FinishSetupWorker.java b/app/src/main/java/foundation/e/drive/account/setup/FinishSetupWorker.java index 1563cf01cbf0b2af40f4bfb9af174522f328840d..316f58abe3f4a52776cc408951e3371afe9a75de 100644 --- a/app/src/main/java/foundation/e/drive/account/setup/FinishSetupWorker.java +++ b/app/src/main/java/foundation/e/drive/account/setup/FinishSetupWorker.java @@ -27,6 +27,7 @@ import androidx.work.WorkerParameters; import foundation.e.drive.periodicScan.FullScanWorker; import foundation.e.drive.periodicScan.PeriodicScanWorker; import foundation.e.drive.utils.AppConstants; +import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.work.WorkRequestFactory; import timber.log.Timber; @@ -71,6 +72,8 @@ public class FinishSetupWorker extends Worker { enqueueAppListGenerationWorker(workManager); enqueueFullScanWorker(workManager); enqueuePeriodicFileScanWorker(workManager); + + CommonUtils.registerPeriodicUserInfoChecking(workManager); } private void enqueueFullScanWorker(@NonNull final WorkManager workManager) { 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 1f67e5a741a0cd27d5c80d4dc578cef9b55d5569..31e339f57c5b9b03bfadab5f9e7ff666825bbbb3 100644 --- a/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java @@ -20,8 +20,10 @@ import androidx.annotation.NonNull; import foundation.e.drive.BuildConfig; import foundation.e.drive.database.DbHelper; import foundation.e.drive.synchronization.SyncProxy; +import foundation.e.drive.utils.AccountUtils; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; +import foundation.e.drive.utils.WorkerUtils; import timber.log.Timber; /** @@ -31,10 +33,11 @@ import timber.log.Timber; public class BootCompletedReceiver extends BroadcastReceiver { private static final String DATE_SYSTEM_PROPERTY = "ro.build.date"; private static final String PREF_VERSION_CODE = "VERSION_CODE"; + @Override public void onReceive(@NonNull Context context, @NonNull Intent intent) { final String action = intent.getAction(); - Timber.tag(BootCompletedReceiver.class.getSimpleName()).v("onReceive(...)"); + Timber.v("onReceive(...)"); final SharedPreferences pref = context.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { @@ -44,8 +47,10 @@ public class BootCompletedReceiver extends BroadcastReceiver { handleOsUpdate(context); } - if (pref.getBoolean(AppConstants.SETUP_COMPLETED, false) - && BuildConfig.VERSION_CODE > pref.getInt(PREF_VERSION_CODE, 1002000)) { + final boolean isSetupDone = AccountUtils.isSetupAlreadyDone(pref); + final boolean isAccountPresent = AccountUtils.isAccountPresent(context); + + if (isSetupDone && BuildConfig.VERSION_CODE > pref.getInt(PREF_VERSION_CODE, 1002000)) { pref.edit().putInt(PREF_VERSION_CODE, BuildConfig.VERSION_CODE).apply(); try { DbHelper.cleanSyncedFileStateTableAfterUpdate(context); @@ -54,6 +59,10 @@ public class BootCompletedReceiver extends BroadcastReceiver { } } + if (isAccountPresent) { + WorkerUtils.INSTANCE.registerSetupWorkers(context.getApplicationContext()); + } + SyncProxy.INSTANCE.startListeningFiles((Application) context.getApplicationContext()); } } @@ -66,6 +75,7 @@ public class BootCompletedReceiver extends BroadcastReceiver { /** * Force reinitialization, upgrade of DB in case of OS update * todo remove when setPersistentFlag=true will be removed + * * @param context Context used to start InitializationService */ private void handleOsUpdate(@NonNull Context context) { diff --git a/app/src/main/java/foundation/e/drive/utils/AccountUtils.java b/app/src/main/java/foundation/e/drive/utils/AccountUtils.java index d9b944d9b5dbfac22173abc143225701539df762..740b027592f49e3c185a2ef95689a5a50081b353 100644 --- a/app/src/main/java/foundation/e/drive/utils/AccountUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/AccountUtils.java @@ -20,6 +20,8 @@ import static foundation.e.drive.utils.AppConstants.ACCOUNT_DATA_GROUPS; import android.accounts.Account; import android.accounts.AccountManager; +import android.content.Context; +import android.content.SharedPreferences; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -27,6 +29,8 @@ import androidx.annotation.Nullable; import java.util.Arrays; import java.util.Optional; +import foundation.e.drive.R; + public class AccountUtils { @@ -48,4 +52,14 @@ public class AccountUtils { .filter(group -> group.contains("premium-")) .findFirst(); } + + public static boolean isSetupAlreadyDone(@NonNull SharedPreferences preferences) { + return preferences.getBoolean(AppConstants.SETUP_COMPLETED, false); + } + + public static boolean isAccountPresent(@NonNull Context context) { + AccountManager accountManager = AccountManager.get(context.getApplicationContext()); + Account[] accountList = accountManager.getAccountsByType(context.getString(R.string.eelo_account_type)); + return accountList != null && accountList.length > 0; + } } diff --git a/app/src/main/java/foundation/e/drive/utils/RootSyncedFolderProvider.kt b/app/src/main/java/foundation/e/drive/utils/RootSyncedFolderProvider.kt index 5f7295af7cf45f00c652365610cf1cb139135043..905d61909b0320468701fe0651881e43455a15d5 100644 --- a/app/src/main/java/foundation/e/drive/utils/RootSyncedFolderProvider.kt +++ b/app/src/main/java/foundation/e/drive/utils/RootSyncedFolderProvider.kt @@ -38,7 +38,7 @@ object RootSyncedFolderProvider { private val REMOTE_ROM_SETTINGS_PATH = PATH_SEPARATOR + "Devices" + PATH_SEPARATOR + BRAND + "_" + MODEL + "_" + Build.getSerial() + "/rom_settings/" private val REMOTE_APP_LIST_PATH = REMOTE_ROM_SETTINGS_PATH + "app_list/" - fun getSyncedFolderRoots(context : Context): ArrayList { + fun getSyncedFolderRoots(context : Context): List { val syncedFolders = ArrayList() val categories = getSyncableCategories() diff --git a/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt b/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt new file mode 100644 index 0000000000000000000000000000000000000000..84a48585339b68ebd8cb1f4507c8d3e00b3a9744 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt @@ -0,0 +1,53 @@ +/* + * Copyright © MURENA SAS 2024. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ + +package foundation.e.drive.utils + +import android.content.Context +import androidx.work.OneTimeWorkRequest +import androidx.work.WorkManager +import foundation.e.drive.models.SyncedFolder +import foundation.e.drive.work.WorkRequestFactory + +object WorkerUtils { + fun registerSetupWorkers(context: Context) { + DavClientProvider.getInstance().cleanUp() + + val rootFolderSetupWorkers = generateRootFolderSetupWorkers(context) + if (rootFolderSetupWorkers.isEmpty()) { + return + } + + val getUserInfoRequest = WorkRequestFactory.getOneTimeWorkRequest( + WorkRequestFactory.WorkType.ONE_TIME_USER_INFO, + null + ) + val finishSetupRequest = WorkRequestFactory.getOneTimeWorkRequest( + WorkRequestFactory.WorkType.ONE_TIME_FINISH_SETUP, + null + ) + + WorkManager.getInstance(context) + .beginWith(getUserInfoRequest) + .then(rootFolderSetupWorkers) + .then(finishSetupRequest) + .enqueue() + } + + private fun generateRootFolderSetupWorkers(context: Context): List { + val rootSyncedFolderList: List = + RootSyncedFolderProvider.getSyncedFolderRoots(context) + + return rootSyncedFolderList.map { + WorkRequestFactory.getOneTimeWorkRequest( + WorkRequestFactory.WorkType.ONE_TIME_ROOT_FOLDER_SETUP, + it + ) + } + } +}