Skip to content

Commit

Permalink
Generate a local broadcast notification when the sync is complete and…
Browse files Browse the repository at this point in the history
… there is new data to display

This can be used by any cached views in order to refresh themselves so that the
user is not presented with stale data

+ on ios, convert a bunch of NSLogs dealing with the sync to DB logs, including
some that go to the device screen, so that we can ensure that the sync works
(or doesn't work) properly.
  • Loading branch information
shankari committed Mar 4, 2016
1 parent 7d145db commit 99da79b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
27 changes: 10 additions & 17 deletions src/android/ServerSyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.content.SyncResult;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -21,7 +22,7 @@
import java.io.IOException;
import java.util.Properties;

import edu.berkeley.eecs.emission.cordova.tracker.location.TripDiaryStateMachineService;
import edu.berkeley.eecs.emission.cordova.tracker.location.TripDiaryStateMachineReceiver;
import edu.berkeley.eecs.emission.cordova.tracker.sensors.BatteryUtils;
import edu.berkeley.eecs.emission.cordova.clientstats.ClientStatsHelper;
import edu.berkeley.eecs.emission.R;
Expand Down Expand Up @@ -161,22 +162,14 @@ public void onPerformSync(Account account, Bundle extras, String authority,
* help with issues we have seen in the field where location updates pause mysteriously, or
* geofences are never exited.
*/
validateAndCleanupState();
}

/*
* TODO: This should probably be moved into the state machine code somehow
*/
public void validateAndCleanupState() {
/*
* Check for being in geofence if in waiting_for_trip_state.
*/
if (TripDiaryStateMachineService.getState(cachedContext).equals(cachedContext.getString(R.string.state_start))) {
cachedContext.sendBroadcast(new Intent(cachedContext.getString(R.string.transition_initialize)));
} else if (TripDiaryStateMachineService.getState(cachedContext).equals(
cachedContext.getString(R.string.state_waiting_for_trip_start))) {
// check in geofence
}
TripDiaryStateMachineReceiver.validateAndCleanupState(cachedContext);
// We are sending this only locally, so we don't care about the URI and so on.
Intent localIntent = new Intent("edu.berkeley.eecs.emission.sync.NEW_DATA");
Bundle b = new Bundle();
b.putString( "userdata", "{}" );
localIntent.putExtras(b);
Log.i(cachedContext, TAG, "Finished sync, sending local broadcast");
LocalBroadcastManager.getInstance(cachedContext).sendBroadcastSync(localIntent);
}

/*
Expand Down
26 changes: 20 additions & 6 deletions src/ios/BEMServerSyncCommunicationHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,40 +116,54 @@ +(BFTask*) pullIntoUserCache {
* to contribute to the battery drain ourselves. Instead, we are going to check the battery level when the app is launched anyway for other reasons,
* by the user, or as part of background sync.
*/
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"pullIntoUserCache called"] showUI:TRUE];
BFTaskCompletionSource *task = [BFTaskCompletionSource taskCompletionSource];
ClientStatsDatabase* statsDb = [ClientStatsDatabase database];
NSString* currTS = [ClientStatsDatabase getCurrentTimeMillisString];
NSString* batteryLevel = [@([UIDevice currentDevice].batteryLevel) stringValue];
[statsDb storeMeasurement:@"battery_level" value:batteryLevel ts:currTS];

[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"about to launch remote call for server_to_phone"] showUI:TRUE];
long msTimeStart = [ClientStatsDatabase getCurrentTimeMillis];

// Called in order to download data in the background
[self server_to_phone:^(NSData *data, NSURLResponse *response, NSError *error) {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"received response for server_to_phone"] showUI:TRUE];

if (error != NULL) {
NSLog(@"Got error %@ while retrieving data", error);
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Got error %@ while retrieving data", error] showUI:TRUE];

if ([error.domain isEqualToString:errorDomain] && (error.code == authFailedNeedUserInput)) {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Please sign in"] showUI:TRUE];
}
[task setResult:@(FALSE)];
} else {
if (data == NULL) {
NSLog(@"Got data == NULL while retrieving data");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Got data == NULL while retrieving data"] showUI:TRUE];

[statsDb storeMeasurement:@"sync_pull_list_size" value:CLIENT_STATS_DB_NIL_VALUE ts:currTS];
[task setResult:@(TRUE)];
} else {
NSLog(@"Got non NULL data while retrieving data");
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Got non NULL data while retrieving data"] showUI:TRUE];
NSInteger newSectionCount = [self fetchedData:data];
NSLog(@"Section count = %ld", (long)newSectionCount);
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Retrieved %@ documents", @(newSectionCount)] showUI:TRUE];
[statsDb storeMeasurement:@"sync_pull_list_size" value:[@(newSectionCount) stringValue] ts:currTS];
if (newSectionCount > 0) {
[LocalNotificationManager addNotification:[NSString stringWithFormat:
@"Retrieved %ld documents", newSectionCount] showUI:TRUE];
// Note that we need to update the UI before calling the completion handler, otherwise
// when the view appears, users won't see the newly fetched data!
[[NSNotificationCenter defaultCenter] postNotificationName:BackgroundRefreshNewData
object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:@"edu.berkeley.eecs.emission.sync.NEW_DATA"
object:nil
userInfo:nil];
[task setResult:@(TRUE)];
} else {
[statsDb storeMeasurement:@"sync_pull_list_size" value:@"0" ts:currTS];
Expand Down

0 comments on commit 99da79b

Please sign in to comment.