Skip to content

Commit

Permalink
Test run workmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Oct 8, 2022
1 parent 2c94dec commit 5830f89
Show file tree
Hide file tree
Showing 32 changed files with 808 additions and 86 deletions.
8 changes: 8 additions & 0 deletions opensrp-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ dependencies {
compileOnly 'com.google.firebase:firebase-perf'
// Add the dependency for the Performance Monitoring library

// WorkManager
def work_version = "2.7.1"
implementation "androidx.work:work-runtime:$work_version"
implementation "androidx.work:work-gcm:$work_version"
implementation "androidx.work:work-multiprocess:$work_version"
implementation "com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava"
testImplementation "androidx.work:work-testing:$work_version"

//Mockito
def mockitoVersion = '4.6.1'
testImplementation("org.mockito:mockito-core:$mockitoVersion")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.smartregister.sync;

import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.AllConstants;
import org.smartregister.CoreLibrary;
import org.smartregister.sync.intent.BaseSyncIntentService;

import java.util.LinkedHashMap;
import java.util.Map;

import timber.log.Timber;

/**
* A helper class for building the url request for intent services
*/
public class RequestParamsBuilder {

private final Map<String, Object> paramMap;
private final StringBuilder getSyncParamsBuilder;
private final JSONObject postSyncParamsBuilder;

public RequestParamsBuilder() {
this.paramMap = new LinkedHashMap<>();
this.getSyncParamsBuilder = new StringBuilder();
this.postSyncParamsBuilder = new JSONObject();
}

public RequestParamsBuilder addServerVersion(long value) {
paramMap.put(AllConstants.SERVER_VERSION, value);
return this;
}

public RequestParamsBuilder addEventPullLimit(int value) {
paramMap.put(AllConstants.LIMIT, value);
return this;
}

public RequestParamsBuilder configureSyncFilter(String syncFilterParam, String syncFilterValue) {
paramMap.put(syncFilterParam, syncFilterValue);
return this;
}

public RequestParamsBuilder returnCount(boolean value) {
paramMap.put(AllConstants.RETURN_COUNT, value);
return this;
}

public RequestParamsBuilder addParam(String key, Object value) {
paramMap.put(key, value);
return this;
}

public RequestParamsBuilder removeParam(String key) {
paramMap.remove(key);
return this;
}

public String build() {

for (Map.Entry<String, Object> entry : paramMap.entrySet()) {

if (CoreLibrary.getInstance().getSyncConfiguration().isSyncUsingPost()) {

try {
postSyncParamsBuilder.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
Timber.e(e);
}

} else {

if (0 != getSyncParamsBuilder.length()) {
getSyncParamsBuilder.append('&');
}

getSyncParamsBuilder.append(entry.getKey()).append('=').append(entry.getValue());
}

}

return CoreLibrary.getInstance().getSyncConfiguration().isSyncUsingPost() ? postSyncParamsBuilder.toString() : getSyncParamsBuilder.toString();
}

@Override
public String toString() {
return build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.smartregister.domain.SyncStatus;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.service.HTTPAgent;
import org.smartregister.sync.intent.BaseSyncIntentService;
import org.smartregister.sync.RequestParamsBuilder;
import org.smartregister.sync.intent.SettingsSyncIntentService;
import org.smartregister.util.JsonFormUtils;
import org.smartregister.util.Utils;
Expand Down Expand Up @@ -95,7 +95,7 @@ private void getExtraSettings(JSONArray settings, String accessToken) throws JSO
JSONArray completeExtraSettings = new JSONArray();
if (getInstance().getSyncConfiguration() != null && getInstance().getSyncConfiguration().hasExtraSettingsSync()) {
String syncParams = getInstance().getSyncConfiguration().getExtraStringSettingsParameters();
BaseSyncIntentService.RequestParamsBuilder builder = new BaseSyncIntentService.RequestParamsBuilder().addParam(AllConstants.SERVER_VERSION, "0").addParam(AllConstants.RESOLVE, getInstance().getSyncConfiguration().resolveSettings());
RequestParamsBuilder builder = new RequestParamsBuilder().addParam(AllConstants.SERVER_VERSION, "0").addParam(AllConstants.RESOLVE, getInstance().getSyncConfiguration().resolveSettings());
String url = SettingsSyncIntentService.SETTINGS_URL + "?" + syncParams + "&" + builder.toString();
JSONArray extraSettings = pullSettings(url, accessToken);
if (extraSettings != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/**
* Created by Vincent Karuri on 26/08/2019
*/

@Deprecated
public class BaseSyncIntentService extends IntentService {

public BaseSyncIntentService(String name) {
Expand All @@ -31,81 +33,4 @@ protected void onHandleIntent(Intent intent) {
httpAgent.setReadTimeout(coreLibrary.getSyncConfiguration().getReadTimeout());
}

/**
* A helper class for building the url request for intent services
*/
public static class RequestParamsBuilder {

private final Map<String, Object> paramMap;
private final StringBuilder getSyncParamsBuilder;
private final JSONObject postSyncParamsBuilder;

public RequestParamsBuilder() {
this.paramMap = new LinkedHashMap<>();
this.getSyncParamsBuilder = new StringBuilder();
this.postSyncParamsBuilder = new JSONObject();
}

public RequestParamsBuilder addServerVersion(long value) {
paramMap.put(AllConstants.SERVER_VERSION, value);
return this;
}

public RequestParamsBuilder addEventPullLimit(int value) {
paramMap.put(AllConstants.LIMIT, value);
return this;
}

public RequestParamsBuilder configureSyncFilter(String syncFilterParam, String syncFilterValue) {
paramMap.put(syncFilterParam, syncFilterValue);
return this;
}

public RequestParamsBuilder returnCount(boolean value) {
paramMap.put(AllConstants.RETURN_COUNT, value);
return this;
}

public RequestParamsBuilder addParam(String key, Object value) {
paramMap.put(key, value);
return this;
}

public RequestParamsBuilder removeParam(String key) {
paramMap.remove(key);
return this;
}

public String build() {

for (Map.Entry<String, Object> entry : paramMap.entrySet()) {

if (CoreLibrary.getInstance().getSyncConfiguration().isSyncUsingPost()) {

try {
postSyncParamsBuilder.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
Timber.e(e);
}

} else {

if (0 != getSyncParamsBuilder.length()) {
getSyncParamsBuilder.append('&');
}

getSyncParamsBuilder.append(entry.getKey()).append('=').append(entry.getValue());
}

}

return CoreLibrary.getInstance().getSyncConfiguration().isSyncUsingPost() ? postSyncParamsBuilder.toString() : getSyncParamsBuilder.toString();
}

@Override
public String toString() {
return build();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static org.smartregister.AllConstants.CAMPAIGNS;

@Deprecated
public class CampaignIntentService extends BaseSyncIntentService {
public static final String CAMPAIGN_URL = "/rest/campaign/";
private static final String TAG = "CampaignIntentService";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*
* @author cozej4 https://github.com/cozej4
*/
@Deprecated
public class DocumentConfigurationIntentService extends BaseSyncIntentService {
private HTTPAgent httpAgent;
private ManifestRepository manifestRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import timber.log.Timber;


@Deprecated
public class ExtendedSyncIntentService extends BaseSyncIntentService {

private ActionService actionService = CoreLibrary.getInstance().context().actionService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.smartregister.util.DateTimeTypeConverter;
import org.smartregister.util.PropertiesConverter;

@Deprecated
public class LocationIntentService extends BaseSyncIntentService {
private static final String TAG = "LocationIntentService";
public static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/**
* Created by Vincent Karuri on 08/05/2019
*/
@Deprecated
public class PlanIntentService extends BaseSyncIntentService {

private static final String TAG = "PlanIntentService";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.smartregister.util.PropertiesConverter;

import timber.log.Timber;

@Deprecated
public class SyncAllLocationsIntentService extends BaseSyncIntentService {
private static final String TAG = "SyncAllLocationsIntentService";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.repository.EventClientRepository;
import org.smartregister.service.HTTPAgent;
import org.smartregister.sync.RequestParamsBuilder;
import org.smartregister.sync.helper.ECSyncHelper;
import org.smartregister.sync.helper.ValidateAssignmentHelper;
import org.smartregister.util.NetworkUtils;
Expand Down Expand Up @@ -170,7 +171,7 @@ private synchronized void fetchRetry(final int count, boolean returnCount) {

startEventTrace(FETCH, 0);

BaseSyncIntentService.RequestParamsBuilder syncParamBuilder = new BaseSyncIntentService.RequestParamsBuilder().
RequestParamsBuilder syncParamBuilder = new RequestParamsBuilder().
configureSyncFilter(configs.getSyncFilterParam().value(), configs.getSyncFilterValue()).addServerVersion(lastSyncDatetime).addEventPullLimit(getEventPullLimit());

Response resp = getUrlResponse(baseUrl + SYNC_URL, syncParamBuilder, configs, returnCount);
Expand Down Expand Up @@ -216,7 +217,7 @@ private synchronized void fetchRetry(final int count, boolean returnCount) {
* @param configs the Sync Configuration object with various configurations
* @param returnCount a boolean flag, whether to return the total count of records as part of the response (field - total_records)
*/
protected Response getUrlResponse(@NonNull String baseURL, @NonNull BaseSyncIntentService.RequestParamsBuilder requestParamsBuilder, @NonNull SyncConfiguration configs, boolean returnCount) {
protected Response getUrlResponse(@NonNull String baseURL, @NonNull RequestParamsBuilder requestParamsBuilder, @NonNull SyncConfiguration configs, boolean returnCount) {
Response response;
String requestUrl = baseURL;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import timber.log.Timber;

@Deprecated
public class SyncLocationsByLevelAndTagsIntentService extends BaseSyncIntentService {

private static final String TAG = "SyncLocationsByLevelAndTagsIntentService";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import timber.log.Timber;

@Deprecated
public class SyncLocationsByTeamIdsIntentService extends BaseSyncIntentService {

private static final String TAG = "SyncLocationsByTeamIdsIntentService";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.smartregister.sync.helper.TaskServiceHelper;

@Deprecated
public class SyncTaskIntentService extends BaseSyncIntentService {
private static final String TAG = "SyncTaskIntentService";
private TaskServiceHelper taskServiceHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**
* Created by keyman on 11/10/2017.
*/
@Deprecated
public class ValidateIntentService extends BaseSyncIntentService {

private Context context;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.smartregister.sync.wm.worker

import android.content.Context
import androidx.work.Worker
import androidx.work.WorkerParameters
import org.smartregister.CoreLibrary

abstract class BaseWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams){

fun beforeWork(){
val coreLibrary = CoreLibrary.getInstance()
val syncConfiguration = coreLibrary.syncConfiguration
if (syncConfiguration != null){
coreLibrary.context().httpAgent
.apply {
connectTimeout = syncConfiguration.connectTimeout
readTimeout = syncConfiguration.readTimeout
}
}
}

}
Loading

0 comments on commit 5830f89

Please sign in to comment.