Skip to content

Commit

Permalink
cleanup workmanager code
Browse files Browse the repository at this point in the history
  • Loading branch information
hilpitome committed Aug 8, 2024
1 parent dae4bbe commit 2dda8a3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -299,7 +301,7 @@ protected void scheduleJobsImmediately() {

if (NetworkUtils.isNetworkAvailable()) {
PullUniqueIdsServiceJob.scheduleJobImmediately(PullUniqueIdsServiceJob.TAG);
SyncSettingsServiceJob.scheduleJobImmediately(SyncSettingsServiceJob.TAG);
SyncSettingsServiceWorker.enqueueOnetimeSettingsSyncIntentService(getApplicationContext());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
}
}

0 comments on commit 2dda8a3

Please sign in to comment.