-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Referral Notifications - Background Sync #2680
Changes from 1 commit
f865524
8fc36d8
2500bb5
13c1ca4
491e08d
b363687
8652d7b
076adaa
1080733
e5317c7
7f137fa
13cf532
d72ed7c
1b529f3
106b465
11516b7
1194304
e6a6ea6
c61d023
722b24f
78439ad
d2e4af1
d683391
2f0f48b
0bc0b04
a13afe8
0c2f463
308a828
7ca96a8
51510e4
6e0d9d7
7ed33cf
71a7306
dfbe5ec
1c84b3a
c0412b5
e699380
cfcd5dc
6d42c01
414cccb
c7a70c1
cd27032
b9525ed
68e05d0
415bc8b
f6416f3
bb99c16
30e2de6
e1daf72
ff10165
be12087
edfce31
65cc28e
f1fbb2a
d96060d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
|
||
import static org.commcare.activities.DispatchActivity.EXIT_AFTER_FORM_SUBMISSION; | ||
import static org.commcare.activities.DispatchActivity.EXIT_AFTER_FORM_SUBMISSION_DEFAULT; | ||
import static org.commcare.activities.DispatchActivity.REBUILD_SESSION; | ||
import static org.commcare.activities.DispatchActivity.SESSION_ENDPOINT_ARGUMENTS_BUNDLE; | ||
import static org.commcare.activities.DispatchActivity.SESSION_ENDPOINT_ARGUMENTS_LIST; | ||
import static org.commcare.activities.DispatchActivity.SESSION_ENDPOINT_ID; | ||
|
@@ -172,7 +171,8 @@ public abstract class HomeScreenBaseActivity<T> extends SyncCapableCommCareActiv | |
protected boolean showCommCareUpdateMenu = false; | ||
private static final int MAX_CC_UPDATE_CANCELLATION = 3; | ||
|
||
public static boolean safeToTriggerBackgroundSyncOnResume = true; | ||
// This is to trigger a background sync after a form submission, | ||
public static boolean shouldTriggerBackgroundSync = true; | ||
|
||
// This is to restore the selected entity when restarting EntityDetailActivity after a | ||
// background sync | ||
|
@@ -353,10 +353,9 @@ private boolean doLoginLaunchChecksInOrder() { | |
|
||
// In case a sync request from FCM was made while the user was logged out, this will | ||
// trigger a blocking sync | ||
if (HiddenPreferences.isPendingSyncRequestFromServer() && | ||
HiddenPreferences.getPendingSyncRequestFromServerTime()>SyncDetailCalculations.getLastSyncTime()) { | ||
HiddenPreferences.setPendingSyncRequestFromServer(false); | ||
if (HiddenPreferences.isPendingSyncRequestFromServerForUser()) { | ||
sendFormsOrSync(false); | ||
|
||
return true; | ||
} | ||
|
||
|
@@ -405,7 +404,7 @@ private boolean showUpdateInfoForm() { | |
private void clearOneTimeLoginActionFlags() { | ||
HiddenPreferences.setPostUpdateSyncNeeded(false); | ||
HiddenPreferences.clearInterruptedSSD(); | ||
HiddenPreferences.setPendingSyncRequestFromServer(false); | ||
HiddenPreferences.clearPendingSyncRequestFromServerForUser(); | ||
} | ||
|
||
private boolean tryRestoringFormFromSessionExpiration() { | ||
|
@@ -1232,7 +1231,7 @@ private void formEntry(int formDefId, FormRecord r, String headerTitle, | |
boolean isRestartAfterSessionExpiration) { | ||
|
||
// Block any background syncs during a form entry | ||
safeToTriggerBackgroundSyncOnResume = false; | ||
shouldTriggerBackgroundSync = false; | ||
|
||
Logger.log(LogTypes.TYPE_FORM_ENTRY, "Form Entry Starting|" + | ||
(r.getInstanceID() == null ? "" : r.getInstanceID() + "|") + | ||
|
@@ -1301,14 +1300,14 @@ public void onResumeSessionSafe() { | |
} | ||
|
||
// In case a Sync was blocked because of a form entry, trigger now if it's safe | ||
if (HiddenPreferences.isPostFormSubmissionSyncNeeded() && safeToTriggerBackgroundSyncOnResume) { | ||
if (HiddenPreferences.isPostFormSubmissionSyncNeeded() && shouldTriggerBackgroundSync) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shubham1g5 Not necessarily, when we open a form There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But that means that sync won't get triggered immediately after user completes the form but only when user comes back to home screen one more time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shubham1g5 I think we are supposed to go back to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am considering the scenario where a user go through this navigation -
Is my interpretaion of sync behaviour in step 4 and 5 right based on the current code ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why wouldn't there be a sync in number 4? After a form submission, we would navigate through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, My understanding is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think so too, but why does it need to be reinitialized for the sync to occur? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
dataSyncer.syncData(HiddenPreferences.getPostFormSubmissionSyncNeededFCMMessageData()); | ||
} | ||
|
||
// reset these | ||
redirectedInOnCreate = false; | ||
sessionNavigationProceedingAfterOnResume = false; | ||
safeToTriggerBackgroundSyncOnResume = true; | ||
shouldTriggerBackgroundSync = true; | ||
} | ||
|
||
private void attemptDispatchHomeScreen() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,7 @@ public class HiddenPreferences { | |
private static final long NO_OF_HOURS_TO_WAIT_TO_RESUME_BACKGROUND_WORK = 36; | ||
|
||
// This is to be used by CommCareFirebaseMessagingService to schedule a sync after the next Login | ||
public final static String PENDING_SYNC_REQUEST_FROM_SERVER = "pending-sync-request-from-server"; | ||
public final static String PENDING_SYNC_REQUEST_FROM_SERVER = "pending-sync-request-from-server-"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can just call it this |
||
|
||
private static final String ENABLE_ANDROID_WINDOW_SECURE_FLAG = "cc-enable-android-window-secure-flag"; | ||
|
||
|
@@ -110,6 +110,12 @@ public class HiddenPreferences { | |
private static final String POST_FOR_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"; | ||
|
||
/** | ||
* The domain name in the application profile file comes in the <domain>.commcarehq.org form, | ||
* this is standard across the different HQ servers. This constant is to store that suffix and | ||
* be used to remove it form the user domain name to match how the domain represented in the backend | ||
*/ | ||
public static final String USER_DOMAIN_SERVER_URL_SUFFIX = ".commcarehq.org"; | ||
|
||
/** | ||
* @return How many seconds should a user session remain open before expiring? | ||
|
@@ -272,6 +278,18 @@ public static String getUserDomain() { | |
return prefs.getString(USER_DOMAIN_SUFFIX, null); | ||
} | ||
|
||
public static String getUserDomainWithoutServerUrl() { | ||
String userDomain = getUserDomain(); | ||
if (userDomain == null){ | ||
return null; | ||
} | ||
|
||
if (userDomain.contains(USER_DOMAIN_SERVER_URL_SUFFIX)){ | ||
return userDomain.replace(USER_DOMAIN_SERVER_URL_SUFFIX, ""); | ||
} | ||
return userDomain; | ||
} | ||
|
||
public static void setPostUpdateSyncNeeded(boolean b) { | ||
CommCareApplication.instance().getCurrentApp().getAppPreferences().edit() | ||
.putBoolean(POST_UPDATE_SYNC_NEEDED, b).apply(); | ||
|
@@ -565,30 +583,28 @@ public static void markRawMediaCleanUpComplete() { | |
.edit().putBoolean(RAW_MEDIA_CLEANUP_COMPLETE, true).apply(); | ||
} | ||
|
||
public static boolean isPendingSyncRequestFromServer() { | ||
public static boolean isPendingSyncRequestFromServerForUser() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand, although my suggestion is to avoid hidden session exception if in future we call this method from other places. Supplying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point |
||
String loggedUsername = CommCareApplication.instance().getSession().getLoggedInUser().getUsername(); | ||
return PreferenceManager.getDefaultSharedPreferences(CommCareApplication.instance()) | ||
.getBoolean(PENDING_SYNC_REQUEST_FROM_SERVER, false); | ||
.contains(PENDING_SYNC_REQUEST_FROM_SERVER + loggedUsername + "@"+ getUserDomainWithoutServerUrl()); | ||
} | ||
public static void setPendingSyncRequestFromServer(boolean syncNeeded) { | ||
public static void setPendingSyncRequestFromServerForUser(FCMMessageData fcmMessageData) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can rename all methods for |
||
PreferenceManager.getDefaultSharedPreferences(CommCareApplication.instance()).edit() | ||
.putBoolean(PENDING_SYNC_REQUEST_FROM_SERVER, syncNeeded) | ||
.putBoolean(PENDING_SYNC_REQUEST_FROM_SERVER + fcmMessageData.getUsername() + "@"+ fcmMessageData.getDomain(), true) | ||
.apply(); | ||
} | ||
|
||
public static boolean isFlagSecureEnabled() { | ||
return DeveloperPreferences.doesPropertyMatch(ENABLE_ANDROID_WINDOW_SECURE_FLAG, PrefValues.NO, PrefValues.YES); | ||
} | ||
public static long getPendingSyncRequestFromServerTime() { | ||
return PreferenceManager.getDefaultSharedPreferences(CommCareApplication.instance()) | ||
.getLong(PENDING_SYNC_REQUEST_FROM_SERVER_TIME, 0); | ||
} | ||
|
||
public static void setPendingSyncRequestFromServerTime(long requestTime) { | ||
public static void clearPendingSyncRequestFromServerForUser() { | ||
String loggedUsername = CommCareApplication.instance().getSession().getLoggedInUser().getUsername(); | ||
PreferenceManager.getDefaultSharedPreferences(CommCareApplication.instance()).edit() | ||
.putLong(PENDING_SYNC_REQUEST_FROM_SERVER_TIME, requestTime) | ||
.remove(PENDING_SYNC_REQUEST_FROM_SERVER + loggedUsername + "@"+ getUserDomainWithoutServerUrl()) | ||
.apply(); | ||
} | ||
|
||
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_FOR_SUBMISSION_SYNC_NEEDED, false); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
package org.commcare.sync; | ||
|
||
|
||
import static org.commcare.utils.FirebaseMessagingUtil.removeServerUrlFromUserDomain; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
|
@@ -53,7 +51,7 @@ public FirebaseMessagingDataSyncer(Context context) { | |
this.context = context; | ||
} | ||
|
||
private List<String> syncSafeActivities = Arrays.asList(new String[]{"FormEntryActivity"}); | ||
private List<String> syncUnsafeActivities = Arrays.asList(new String[]{"FormEntryActivity"}); | ||
private CommCareTask currentTask = null; | ||
private PinnedNotificationWithProgress<DataPullTask.PullTaskResult> mPinnedNotificationProgress = null; | ||
|
||
|
@@ -65,21 +63,9 @@ public FirebaseMessagingDataSyncer(Context context) { | |
* 2) Ensure that the sync is only triggered for the intended 'recipient' of the message | ||
*/ | ||
public void syncData(FCMMessageData fcmMessageData) { | ||
// Abort if FCM Message data is null | ||
if (fcmMessageData == null){ | ||
disablePendingSyncs(); | ||
return; | ||
} | ||
|
||
if (!CommCareApplication.isSessionActive()) { | ||
// There is no active session at the moment, proceed accordingly | ||
// TODO: Decide whether to only trigger the Sync when the 'recipient' of the message logs in | ||
// or anyone, in case multiple users are sharing the same device | ||
// TODO: Decide whether to check if when there is no active session, the recipient has ever | ||
// logged in the device, before scheduling a sync post login | ||
HiddenPreferences.setPendingSyncRequestFromServer(true); | ||
HiddenPreferences.setPendingSyncRequestFromServerTime(fcmMessageData.getCreationTime().getMillis()); | ||
|
||
HiddenPreferences.setPendingSyncRequestFromServerForUser(fcmMessageData); | ||
return; | ||
} | ||
// Retrieve the current User | ||
|
@@ -199,14 +185,14 @@ protected void onPostExecute(ResultAndError<PullTaskResult> resultAndErrorMessag | |
private boolean checkUserAndDomain(User user, String payloadUsername, String payloadDomain) { | ||
if(payloadUsername != null && payloadDomain != null){ | ||
String loggedInUsername = user.getUsername(); | ||
String userDomain = removeServerUrlFromUserDomain(HiddenPreferences.getUserDomain()); | ||
String userDomain = HiddenPreferences.getUserDomainWithoutServerUrl(); | ||
return payloadUsername.equalsIgnoreCase(loggedInUsername) && payloadDomain.equalsIgnoreCase(userDomain); | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isCurrentActivitySyncSafe() { | ||
return !syncSafeActivities.contains(CommCareApplication.currentActivityName); | ||
return !syncUnsafeActivities.contains(CommCareApplication.currentActivityName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of setting the |
||
} | ||
|
||
// This method is responsible for informing the User about a pending sync and scheduling | ||
|
@@ -233,14 +219,6 @@ public void startBlockingForTask(int id) { | |
} | ||
mPinnedNotificationProgress = new PinnedNotificationWithProgress(context, | ||
"sync.communicating.title","sync.progress.starting", -1); | ||
|
||
// Disable any pending sync | ||
disablePendingSyncs(); | ||
} | ||
|
||
private void disablePendingSyncs() { | ||
HiddenPreferences.setPendingSyncRequestFromServer(false); | ||
HiddenPreferences.setPostFormSubmissionSyncNeeded(false); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
private boolean shouldTriggerBackgroundSync