Skip to content

Commit

Permalink
Merge shared preferences post-form-submission-sync and background-syn…
Browse files Browse the repository at this point in the history
…c-pending
  • Loading branch information
avazirna committed Sep 4, 2023
1 parent ff10165 commit be12087
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 40 deletions.
5 changes: 2 additions & 3 deletions app/src/org/commcare/activities/FormEntryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1516,9 +1516,8 @@ public void onRequestPermissionsResult(int requestCode,

@Override
public void alertPendingSync(FCMMessageData fcmMessageData) {
if (!HiddenPreferences.isPostFormSubmissionSyncNeeded()) {
HiddenPreferences.setPostFormSubmissionSyncNeeded(true);
HiddenPreferences.setPostFormSubmissionSyncNeededFCMMessageData(fcmMessageData);
if (!HiddenPreferences.isPendingSyncRequest(fcmMessageData.getUsername())) {
HiddenPreferences.setPendingSyncRequest(fcmMessageData);

if (!HiddenPreferences.isPendingSyncDialogDisabled()) {
StandardAlertDialog dialog = StandardAlertDialog.getBasicAlertDialogWithDisablingCheckbox(this,
Expand Down
9 changes: 5 additions & 4 deletions app/src/org/commcare/activities/HomeScreenBaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private boolean doLoginLaunchChecksInOrder() {
// In case a sync request from FCM was made while the user was logged out, this will
// trigger a blocking sync
String username = CommCareApplication.instance().getSession().getLoggedInUser().getUsername();
if (HiddenPreferences.isPendingSyncRequestFromServerForUser(username)) {
if (HiddenPreferences.isPendingSyncRequest(username)) {
sendFormsOrSync(false);

return true;
Expand Down Expand Up @@ -406,7 +406,7 @@ private void clearOneTimeLoginActionFlags() {
HiddenPreferences.setPostUpdateSyncNeeded(false);
HiddenPreferences.clearInterruptedSSD();
String username = CommCareApplication.instance().getSession().getLoggedInUser().getUsername();
HiddenPreferences.clearPendingSyncRequestFromServerForUser(username);
HiddenPreferences.clearPendingSyncRequest(username);
}

private boolean tryRestoringFormFromSessionExpiration() {
Expand Down Expand Up @@ -1305,8 +1305,9 @@ public void onResumeSessionSafe() {
}

// In case a Sync was blocked because of a form entry, trigger now if it's safe
if (HiddenPreferences.isPostFormSubmissionSyncNeeded() && shouldTriggerBackgroundSync) {
dataSyncer.syncData(HiddenPreferences.getPostFormSubmissionSyncNeededFCMMessageData());
String username = CommCareApplication.instance().getSession().getLoggedInUser().getUsername();
if (HiddenPreferences.isPendingSyncRequest(username) && shouldTriggerBackgroundSync) {
dataSyncer.syncData(HiddenPreferences.getPendingSyncRequest(username));
}

// reset these
Expand Down
42 changes: 12 additions & 30 deletions app/src/org/commcare/preferences/HiddenPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public class HiddenPreferences {
public final static String BACKGROUND_SYNC_PENDING = "background-sync-pending-";

private static final String ENABLE_ANDROID_WINDOW_SECURE_FLAG = "cc-enable-android-window-secure-flag";
public final static String POST_FORM_SUBMISSION_SYNC_NEEDED = "post-form-submission-sync-needed";
public final static String DONT_SHOW_PENDING_SYNC_DIALOG = "dont-show-pending-sync-dialog";
private static final String POST_FORM_SUBMISSION_SYNC_NEEDED_FCM_MESSAGE_DATA = "post-form-submission-sync-needed-fcm-message-data";
private static final String ENABLE_BACKGROUND_SYNC = "cc-enable-background-sync";

/**
Expand Down Expand Up @@ -579,17 +577,25 @@ public static void markRawMediaCleanUpComplete() {
.edit().putBoolean(RAW_MEDIA_CLEANUP_COMPLETE, true).apply();
}

public static boolean isPendingSyncRequestFromServerForUser(String loggedInUser) {
public static boolean isPendingSyncRequest(String loggedInUser) {
return PreferenceManager.getDefaultSharedPreferences(CommCareApplication.instance())
.contains(BACKGROUND_SYNC_PENDING + loggedInUser + "@"+ getUserDomainWithoutServerUrl());
}
public static void setPendingSyncRequestFromServerForUser(FCMMessageData fcmMessageData) {

public static void setPendingSyncRequest(FCMMessageData fcmMessageData) {
PreferenceManager.getDefaultSharedPreferences(CommCareApplication.instance()).edit()
.putBoolean(BACKGROUND_SYNC_PENDING + fcmMessageData.getUsername() + "@"+ fcmMessageData.getDomain(), true)
.putString(BACKGROUND_SYNC_PENDING + fcmMessageData.getUsername() + "@"+ fcmMessageData.getDomain(),
FirebaseMessagingUtil.serializeFCMMessageData(fcmMessageData))
.apply();
}

public static void clearPendingSyncRequestFromServerForUser(String loggedInUser) {
public static FCMMessageData getPendingSyncRequest(String username) {
String serializedMessageData = PreferenceManager.getDefaultSharedPreferences(CommCareApplication.instance())
.getString(BACKGROUND_SYNC_PENDING + username + "@"+ getUserDomainWithoutServerUrl(), null);
return FirebaseMessagingUtil.deserializeFCMMessageData(serializedMessageData);
}

public static void clearPendingSyncRequest(String loggedInUser) {
PreferenceManager.getDefaultSharedPreferences(CommCareApplication.instance()).edit()
.remove(BACKGROUND_SYNC_PENDING + loggedInUser + "@"+ getUserDomainWithoutServerUrl())
.apply();
Expand All @@ -599,30 +605,6 @@ public static boolean isFlagSecureEnabled() {
return DeveloperPreferences.doesPropertyMatch(ENABLE_ANDROID_WINDOW_SECURE_FLAG, PrefValues.NO, PrefValues.YES);
}

public static boolean isPostFormSubmissionSyncNeeded() {
return CommCareApplication.instance().getCurrentApp().getAppPreferences()
.getBoolean(POST_FORM_SUBMISSION_SYNC_NEEDED, false);
}

public static void setPostFormSubmissionSyncNeeded(boolean syncNeeded) {
CommCareApplication.instance().getCurrentApp().getAppPreferences().edit()
.putBoolean(POST_FORM_SUBMISSION_SYNC_NEEDED, syncNeeded)
.apply();
}

public static void setPostFormSubmissionSyncNeededFCMMessageData(FCMMessageData fcmMessageData) {
CommCareApplication.instance().getCurrentApp().getAppPreferences().edit()
.putString(POST_FORM_SUBMISSION_SYNC_NEEDED_FCM_MESSAGE_DATA,
FirebaseMessagingUtil.serializeFCMMessageData(fcmMessageData))
.apply();
}

public static FCMMessageData getPostFormSubmissionSyncNeededFCMMessageData() {
String serializedMessageData = CommCareApplication.instance().getCurrentApp().getAppPreferences()
.getString(POST_FORM_SUBMISSION_SYNC_NEEDED_FCM_MESSAGE_DATA, null);
return FirebaseMessagingUtil.deserializeFCMMessageData(serializedMessageData);
}

public static boolean isPendingSyncDialogDisabled() {
return CommCareApplication.instance().getCurrentApp().getAppPreferences()
.getBoolean(DONT_SHOW_PENDING_SYNC_DIALOG, false);
Expand Down
2 changes: 1 addition & 1 deletion app/src/org/commcare/sync/FirebaseMessagingDataSyncer.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public FirebaseMessagingDataSyncer(Context context) {
public void syncData(FCMMessageData fcmMessageData) {
if (!CommCareApplication.isSessionActive()) {
// There is no active session at the moment, proceed accordingly
HiddenPreferences.setPendingSyncRequestFromServerForUser(fcmMessageData);
HiddenPreferences.setPendingSyncRequest(fcmMessageData);
return;
}
// Retrieve the current User
Expand Down
3 changes: 1 addition & 2 deletions app/src/org/commcare/tasks/DataPullTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,7 @@ private void onSuccessfulSync() {
updateCurrentUser(password);

// Disable pending background syncs
HiddenPreferences.clearPendingSyncRequestFromServerForUser(username);
HiddenPreferences.setPostFormSubmissionSyncNeeded(false);
HiddenPreferences.clearPendingSyncRequest(username);

this.publishProgress(PROGRESS_DONE);
}
Expand Down

0 comments on commit be12087

Please sign in to comment.