diff --git a/gradle.properties b/gradle.properties index 747520b82..7e741b7ff 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,5 +13,5 @@ POM_SETTING_DEVELOPER_ID=opensrp POM_SETTING_DEVELOPER_NAME=OpenSRP Onadev android.useAndroidX=true android.enableJetifier=true -org.gradle.jvmargs=-Xmx2048m +org.gradle.jvmargs=-Xmx2048m --add-opens java.base/java.io=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED android.jetifier.ignorelist=shadows,bcprov-jdk15on diff --git a/opensrp-core/src/main/java/org/smartregister/job/SyncSettingsServiceWorker.java b/opensrp-core/src/main/java/org/smartregister/job/SyncSettingsServiceWorker.java index c86e199f9..74baa6ffc 100644 --- a/opensrp-core/src/main/java/org/smartregister/job/SyncSettingsServiceWorker.java +++ b/opensrp-core/src/main/java/org/smartregister/job/SyncSettingsServiceWorker.java @@ -1,25 +1,88 @@ package org.smartregister.job; +import static org.smartregister.util.Log.logError; + import android.content.Context; import android.content.Intent; +import android.os.Build; import androidx.annotation.NonNull; +import androidx.work.ExistingPeriodicWorkPolicy; +import androidx.work.ExistingWorkPolicy; +import androidx.work.OneTimeWorkRequest; +import androidx.work.PeriodicWorkRequest; +import androidx.work.WorkManager; import androidx.work.Worker; import androidx.work.WorkerParameters; +import org.json.JSONException; +import org.smartregister.AllConstants; +import org.smartregister.CoreLibrary; +import org.smartregister.service.HTTPAgent; +import org.smartregister.sync.helper.SyncSettingsServiceHelper; import org.smartregister.sync.intent.SettingsSyncIntentService; +import java.util.concurrent.TimeUnit; + +import timber.log.Timber; + // replaces SyncSettingsServiceJob public class SyncSettingsServiceWorker extends Worker { + public static final String SETTINGS_URL = "/rest/settings/sync"; + + private static final String TAG = SettingsSyncIntentService.class.getCanonicalName(); + + protected SyncSettingsServiceHelper syncSettingsServiceHelper; + + public static final String EVENT_SYNC_COMPLETE = "event_sync_complete"; + public SyncSettingsServiceWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); + org.smartregister.Context opensrpContext = CoreLibrary.getInstance().context(); + CoreLibrary coreLibrary = CoreLibrary.getInstance(); + HTTPAgent httpAgent = coreLibrary.context().httpAgent(); + assert coreLibrary.getSyncConfiguration() != null; + httpAgent.setConnectTimeout(coreLibrary.getSyncConfiguration().getConnectTimeout()); + httpAgent.setReadTimeout(coreLibrary.getSyncConfiguration().getReadTimeout()); + syncSettingsServiceHelper = new SyncSettingsServiceHelper(opensrpContext.configuration().dristhiBaseURL(), opensrpContext.getHttpAgent()); } @NonNull @Override public Result doWork() { - Intent intent = new Intent(getApplicationContext(), SettingsSyncIntentService.class); - getApplicationContext().startService(intent); + Timber.d("doing work in SyncsettingsServiceWorker"); + boolean isSuccessfulSync = processSettings(); + if (isSuccessfulSync) { + Timber.d("processingSettings is a success enqueueing SyncSettingsServiceWorker..."); + // Schedule the sync job using WorkManager + } + + Timber.d("returning success on SyncSettings work"); return Result.success(); } + + protected boolean processSettings() { + Timber.d("In Settings SyncSettingService..."); + boolean isSuccessfulSync = true; + try { + syncSettingsServiceHelper.processIntent(); + + } catch (JSONException e) { + isSuccessfulSync = false; + logError(TAG + " Error fetching client settings"); + } + return isSuccessfulSync; + } + + public static void enqueueOnetimeSettingsSyncIntentService(android.content.Context context){ + OneTimeWorkRequest onetimeSettingSyncWorkRequest = new OneTimeWorkRequest.Builder(SyncSettingsServiceWorker.class).build(); + WorkManager.getInstance(context).beginUniqueWork("settingsSyncService", ExistingWorkPolicy.KEEP, onetimeSettingSyncWorkRequest).enqueue(); + } + + public static void enqueuePeriodicSettingsSyncIntentService(android.content.Context context){ + PeriodicWorkRequest periodicSettingsSyncWorkRequest = new PeriodicWorkRequest.Builder(SyncSettingsServiceWorker.class, 15, TimeUnit.MINUTES) + .build(); + WorkManager.getInstance(context).enqueueUniquePeriodicWork("periodicSettingsSyncService", ExistingPeriodicWorkPolicy.KEEP, periodicSettingsSyncWorkRequest); + + } } diff --git a/opensrp-core/src/main/java/org/smartregister/login/interactor/BaseLoginInteractor.java b/opensrp-core/src/main/java/org/smartregister/login/interactor/BaseLoginInteractor.java index 6db591dd5..d7acab87c 100644 --- a/opensrp-core/src/main/java/org/smartregister/login/interactor/BaseLoginInteractor.java +++ b/opensrp-core/src/main/java/org/smartregister/login/interactor/BaseLoginInteractor.java @@ -28,12 +28,14 @@ import org.smartregister.job.P2pServiceJob; import org.smartregister.job.PullUniqueIdsServiceJob; import org.smartregister.job.SyncSettingsServiceJob; +import org.smartregister.job.SyncSettingsServiceWorker; import org.smartregister.login.task.LocalLoginTask; import org.smartregister.login.task.RemoteLoginTask; import org.smartregister.multitenant.ResetAppHelper; import org.smartregister.repository.AllSharedPreferences; import org.smartregister.service.UserService; import org.smartregister.sync.helper.ServerSettingsHelper; +import org.smartregister.sync.intent.SettingsSyncIntentService; import org.smartregister.util.NetworkUtils; import org.smartregister.view.activity.ChangePasswordActivity; import org.smartregister.view.activity.DrishtiApplication; @@ -299,7 +301,7 @@ protected void scheduleJobsImmediately() { if (NetworkUtils.isNetworkAvailable()) { PullUniqueIdsServiceJob.scheduleJobImmediately(PullUniqueIdsServiceJob.TAG); - SyncSettingsServiceJob.scheduleJobImmediately(SyncSettingsServiceJob.TAG); + SyncSettingsServiceWorker.enqueueOnetimeSettingsSyncIntentService(getApplicationContext()); } } diff --git a/opensrp-core/src/main/java/org/smartregister/sync/intent/SettingsSyncIntentService.java b/opensrp-core/src/main/java/org/smartregister/sync/intent/SettingsSyncIntentService.java index b31f0d675..660470968 100644 --- a/opensrp-core/src/main/java/org/smartregister/sync/intent/SettingsSyncIntentService.java +++ b/opensrp-core/src/main/java/org/smartregister/sync/intent/SettingsSyncIntentService.java @@ -1,21 +1,19 @@ package org.smartregister.sync.intent; import android.content.Intent; +import android.util.Log; import org.json.JSONException; import org.smartregister.AllConstants; import org.smartregister.Context; import org.smartregister.CoreLibrary; -import org.smartregister.job.SyncSettingsServiceWorker; +import org.smartregister.job.SyncServiceJob; import org.smartregister.sync.helper.SyncSettingsServiceHelper; import timber.log.Timber; import static org.smartregister.util.Log.logError; -import androidx.work.OneTimeWorkRequest; -import androidx.work.WorkManager; - /** * Created by ndegwamartin on 14/09/2018. */ @@ -36,9 +34,7 @@ public SettingsSyncIntentService() { protected void onHandleIntent(Intent intent) { boolean isSuccessfulSync = processSettings(intent); if (isSuccessfulSync) { - // Schedule the sync job using WorkManager - OneTimeWorkRequest syncWorkRequest = new OneTimeWorkRequest.Builder(SyncSettingsServiceWorker.class).build(); - WorkManager.getInstance(this).enqueue(syncWorkRequest); + SyncServiceJob.scheduleJobImmediately(SyncServiceJob.TAG); } } diff --git a/sample/src/main/java/org/smartregister/sample/interactor/LoginInteractor.java b/sample/src/main/java/org/smartregister/sample/interactor/LoginInteractor.java index 9ddfd5d9f..b792fca81 100644 --- a/sample/src/main/java/org/smartregister/sample/interactor/LoginInteractor.java +++ b/sample/src/main/java/org/smartregister/sample/interactor/LoginInteractor.java @@ -1,11 +1,16 @@ package org.smartregister.sample.interactor; +import static org.smartregister.job.SyncSettingsServiceWorker.*; + import androidx.work.PeriodicWorkRequest; import androidx.work.WorkManager; import androidx.work.WorkRequest; +import org.smartregister.domain.Setting; import org.smartregister.job.DuplicateCleanerWorker; +import org.smartregister.job.SyncSettingsServiceWorker; import org.smartregister.login.interactor.BaseLoginInteractor; +import org.smartregister.sync.intent.SettingsSyncIntentService; import org.smartregister.view.contract.BaseLoginContract; import java.util.concurrent.TimeUnit; @@ -25,5 +30,7 @@ protected void scheduleJobsPeriodically() { WorkRequest cleanZeirIdsWorkRequest = new PeriodicWorkRequest.Builder(DuplicateCleanerWorker.class, 15, TimeUnit.MINUTES) .build(); WorkManager.getInstance(this.getApplicationContext()).enqueue(cleanZeirIdsWorkRequest); + + enqueuePeriodicSettingsSyncIntentService(getApplicationContext()); } }