From 459d8687c3c553c49f75ae93c7d0d4afa67c1e61 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 2 Feb 2024 19:50:09 +0600 Subject: [PATCH 1/4] fix: workers not initialize on boot complete On os update, workers are not initialize by itself. We have to init the workers on successful boot. issue: https://gitlab.e.foundation/e/backlog/-/issues/7578 --- .../account/receivers/AccountAddedReceiver.kt | 51 ++--------------- .../account/setup/FinishSetupWorker.java | 3 + .../receivers/BootCompletedReceiver.java | 16 +++++- .../e/drive/utils/AccountUtils.java | 14 +++++ .../foundation/e/drive/utils/WorkerUtils.kt | 57 +++++++++++++++++++ 5 files changed, 91 insertions(+), 50 deletions(-) create mode 100644 app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt 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 4e0b9248..a95d15c6 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 1563cf01..316f58ab 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 1f67e5a7..31e339f5 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 d9b944d9..740b0275 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/WorkerUtils.kt b/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt new file mode 100644 index 00000000..f506228f --- /dev/null +++ b/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt @@ -0,0 +1,57 @@ +/* + * 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) ?: return + + val getUserInfoRequest = WorkRequestFactory.getOneTimeWorkRequest( + WorkRequestFactory.WorkType.ONE_TIME_USER_INFO, + null + ) + val finishSetupRequest = WorkRequestFactory.getOneTimeWorkRequest( + WorkRequestFactory.WorkType.ONE_TIME_FINISH_SETUP, + null + ) + val workManager = WorkManager.getInstance(context) + + workManager.beginWith(getUserInfoRequest) + .then(rootFolderSetupWorkers) + .then(finishSetupRequest) + .enqueue() + } + + 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 = WorkRequestFactory.getOneTimeWorkRequest( + WorkRequestFactory.WorkType.ONE_TIME_ROOT_FOLDER_SETUP, + folder + ) + workRequests.add(rootFolderSetupWorkRequest) + } + return workRequests + } +} \ No newline at end of file -- GitLab From 1074005056f104b4161ad754d96e66699d751fa1 Mon Sep 17 00:00:00 2001 From: Vincent Bourgmayer Date: Tue, 6 Feb 2024 14:35:21 +0000 Subject: [PATCH 2/4] fix: linter issue --- app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt b/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt index f506228f..ddf75387 100644 --- a/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt +++ b/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt @@ -54,4 +54,5 @@ object WorkerUtils { } return workRequests } -} \ No newline at end of file +} + -- GitLab From a4663d9e759a4a2f7b872de33fa207bb6dbfe331 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 7 Feb 2024 10:42:29 +0600 Subject: [PATCH 3/4] chore: refactor according to review --- .../e/drive/utils/RootSyncedFolderProvider.kt | 2 +- .../foundation/e/drive/utils/WorkerUtils.kt | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) 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 5f7295af..905d6190 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 index ddf75387..0e07e291 100644 --- a/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt +++ b/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt @@ -28,31 +28,27 @@ object WorkerUtils { WorkRequestFactory.WorkType.ONE_TIME_FINISH_SETUP, null ) - val workManager = WorkManager.getInstance(context) - workManager.beginWith(getUserInfoRequest) + WorkManager.getInstance(context) + .beginWith(getUserInfoRequest) .then(rootFolderSetupWorkers) .then(finishSetupRequest) .enqueue() } - private fun generateRootFolderSetupWorkers(context: Context): MutableList? { + private fun generateRootFolderSetupWorkers(context: Context): List? { val rootSyncedFolderList: List = RootSyncedFolderProvider.getSyncedFolderRoots(context) - if (rootSyncedFolderList.isNullOrEmpty()) { + if (rootSyncedFolderList.isEmpty()) { return null } - val workRequests: MutableList = ArrayList() - for (folder in rootSyncedFolderList) { - val rootFolderSetupWorkRequest = WorkRequestFactory.getOneTimeWorkRequest( + return rootSyncedFolderList.map { + WorkRequestFactory.getOneTimeWorkRequest( WorkRequestFactory.WorkType.ONE_TIME_ROOT_FOLDER_SETUP, - folder + it ) - workRequests.add(rootFolderSetupWorkRequest) } - return workRequests } } - -- GitLab From da82b36d86943879ac678144a6b5b01af729f70f Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 7 Feb 2024 14:44:35 +0600 Subject: [PATCH 4/4] chore: refactor according to review --- .../main/java/foundation/e/drive/utils/WorkerUtils.kt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt b/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt index 0e07e291..84a48585 100644 --- a/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt +++ b/app/src/main/java/foundation/e/drive/utils/WorkerUtils.kt @@ -18,7 +18,10 @@ object WorkerUtils { fun registerSetupWorkers(context: Context) { DavClientProvider.getInstance().cleanUp() - val rootFolderSetupWorkers = generateRootFolderSetupWorkers(context) ?: return + val rootFolderSetupWorkers = generateRootFolderSetupWorkers(context) + if (rootFolderSetupWorkers.isEmpty()) { + return + } val getUserInfoRequest = WorkRequestFactory.getOneTimeWorkRequest( WorkRequestFactory.WorkType.ONE_TIME_USER_INFO, @@ -36,14 +39,10 @@ object WorkerUtils { .enqueue() } - private fun generateRootFolderSetupWorkers(context: Context): List? { + private fun generateRootFolderSetupWorkers(context: Context): List { val rootSyncedFolderList: List = RootSyncedFolderProvider.getSyncedFolderRoots(context) - if (rootSyncedFolderList.isEmpty()) { - return null - } - return rootSyncedFolderList.map { WorkRequestFactory.getOneTimeWorkRequest( WorkRequestFactory.WorkType.ONE_TIME_ROOT_FOLDER_SETUP, -- GitLab