diff --git a/app/src/org/commcare/CommCareApplication.java b/app/src/org/commcare/CommCareApplication.java index c1beb1844..7ef93bb27 100644 --- a/app/src/org/commcare/CommCareApplication.java +++ b/app/src/org/commcare/CommCareApplication.java @@ -98,6 +98,7 @@ import org.commcare.tasks.DeleteLogs; import org.commcare.tasks.LogSubmissionTask; import org.commcare.tasks.PrimeEntityCache; +import org.commcare.tasks.PrimeEntityCacheHelper; import org.commcare.tasks.PurgeStaleArchivedFormsTask; import org.commcare.tasks.templates.ManagedAsyncTask; import org.commcare.update.UpdateHelper; @@ -160,7 +161,6 @@ public class CommCareApplication extends Application implements LifecycleEventOb public static final int STATE_MIGRATION_QUESTIONABLE = 32; private static final String DELETE_LOGS_REQUEST = "delete-logs-request"; private static final String CLEAN_RAW_MEDIA_REQUEST = "clean-raw-media-request"; - private static final String PRIME_ENTITY_CACHE_REQUEST = "prime-entity-cache-request"; private static final long BACKOFF_DELAY_FOR_UPDATE_RETRY = 5 * 60 * 1000L; // 5 mins private static final long BACKOFF_DELAY_FOR_FORM_SUBMISSION_RETRY = 5 * 60 * 1000L; // 5 mins private static final long PERIODICITY_FOR_FORM_SUBMISSION_IN_HOURS = 1; @@ -389,6 +389,7 @@ protected void cancelWorkManagerTasks() { if (currentApp != null) { WorkManager.getInstance(this).cancelUniqueWork( FormSubmissionHelper.getFormSubmissionRequestName(currentApp.getUniqueId())); + PrimeEntityCacheHelper.cancelWork(); } } @@ -798,7 +799,7 @@ public void onServiceConnected(ComponentName className, IBinder service) { purgeLogs(); cleanRawMedia(); - primeEntityCache(); + PrimeEntityCacheHelper.schedulePrimeEntityCacheWorker(); } TimedStatsTracker.registerStartSession(); @@ -829,12 +830,6 @@ public void onServiceDisconnected(ComponentName className) { sessionServiceIsBinding = true; } - public void primeEntityCache() { - OneTimeWorkRequest primeEntityCacheRequest = new OneTimeWorkRequest.Builder(PrimeEntityCache.class).build(); - WorkManager.getInstance(CommCareApplication.instance()) - .enqueueUniqueWork(PRIME_ENTITY_CACHE_REQUEST, ExistingWorkPolicy.KEEP, primeEntityCacheRequest); - } - private void cleanRawMedia() { OneTimeWorkRequest cleanRawMediaRequest = new OneTimeWorkRequest.Builder(CleanRawMedia.class).build(); WorkManager.getInstance(CommCareApplication.instance()) diff --git a/app/src/org/commcare/tasks/DataPullTask.java b/app/src/org/commcare/tasks/DataPullTask.java index 3eec80862..761ca366e 100644 --- a/app/src/org/commcare/tasks/DataPullTask.java +++ b/app/src/org/commcare/tasks/DataPullTask.java @@ -437,7 +437,7 @@ private ResultAndError handleBadLocalState(AndroidTransactionPar if (returnCode == PROGRESS_DONE) { // Recovery was successful onSuccessfulSync(); - CommCareApplication.instance().primeEntityCache(); + PrimeEntityCacheHelper.schedulePrimeEntityCacheWorker(); return new ResultAndError<>(PullTaskResult.DOWNLOAD_SUCCESS); } else if (returnCode == PROGRESS_RECOVERY_FAIL_SAFE || returnCode == PROGRESS_RECOVERY_FAIL_BAD) { wipeLoginIfItOccurred(); diff --git a/app/src/org/commcare/tasks/PrimeEntityCacheHelper.kt b/app/src/org/commcare/tasks/PrimeEntityCacheHelper.kt index bb7008a3a..a344fa3c8 100644 --- a/app/src/org/commcare/tasks/PrimeEntityCacheHelper.kt +++ b/app/src/org/commcare/tasks/PrimeEntityCacheHelper.kt @@ -1,15 +1,20 @@ package org.commcare.tasks import android.util.Pair +import androidx.work.ExistingWorkPolicy +import androidx.work.OneTimeWorkRequest +import androidx.work.WorkManager import io.reactivex.functions.Cancellable import org.commcare.CommCareApplication import org.commcare.cases.entity.Entity import org.commcare.entity.PrimeEntityCacheListener import org.commcare.suite.model.Detail import org.commcare.suite.model.EntityDatum +import org.commcare.sync.FormSubmissionHelper import org.commcare.utils.AndroidCommCarePlatform import org.javarosa.core.model.condition.EvaluationContext import org.javarosa.core.model.instance.TreeReference +import org.javarosa.core.services.Logger /** * Helper to prime cache for all entity screens in the app @@ -28,10 +33,33 @@ class PrimeEntityCacheHelper private constructor() : Cancellable { @Volatile private var instance: PrimeEntityCacheHelper? = null + const val PRIME_ENTITY_CACHE_REQUEST = "prime-entity-cache-request" + + @JvmStatic fun getInstance() = instance ?: synchronized(this) { instance ?: PrimeEntityCacheHelper().also { instance = it } } + + /** + * Schedules a background worker request to prime cache for all + * cache backed entity list screens in the current seated app + */ + @JvmStatic + fun schedulePrimeEntityCacheWorker() { + val primeEntityCacheRequest = OneTimeWorkRequest.Builder(PrimeEntityCache::class.java).build() + WorkManager.getInstance(CommCareApplication.instance()) + .enqueueUniqueWork( + PRIME_ENTITY_CACHE_REQUEST, + ExistingWorkPolicy.KEEP, + primeEntityCacheRequest + ) + } + + @JvmStatic + fun cancelWork() { + WorkManager.getInstance(CommCareApplication.instance()).cancelUniqueWork(PRIME_ENTITY_CACHE_REQUEST) + } } /** @@ -59,6 +87,7 @@ class PrimeEntityCacheHelper private constructor() : Cancellable { /** * Cancel any current cache prime process to expedite cache calculations for given [detail] + * Reschedules the work again in background afterwards */ fun expediteDetailWithId(detail: Detail, entities: MutableList>) { cancel() @@ -89,7 +118,6 @@ class PrimeEntityCacheHelper private constructor() : Cancellable { private fun primeCacheForDetail(detail: Detail, sessionDatum: EntityDatum? = null, entities: MutableList>? = null) { if (!detail.shouldCache()) return - currentDetailInProgress = detail.id entityLoaderHelper = EntityLoaderHelper(detail, evalCtx())