diff --git a/plugin.xml b/plugin.xml index 01c5efa..5904d24 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + version="1.9.1"> DataCollection Background data collection FTW! This is the part that I really diff --git a/src/android/location/TripDiaryStateMachineService.java b/src/android/location/TripDiaryStateMachineService.java index cfde9d8..8565287 100644 --- a/src/android/location/TripDiaryStateMachineService.java +++ b/src/android/location/TripDiaryStateMachineService.java @@ -281,7 +281,8 @@ public void handleTripStart(Context ctxt, final String actionString) { if (isFleet) { Log.d(this, TAG, "Found beacon in fleet mode, starting location tracking"); } else { - Log.e(this, TAG, "Found beacon in non-fleet mode, not sure why this happened, starting location tracking anyway"); + Log.e(this, TAG, "ERROR: Found beacon in non-fleet mode, not starting location tracking"); + return; } } diff --git a/src/android/location/TripDiaryStateMachineServiceOngoing.java b/src/android/location/TripDiaryStateMachineServiceOngoing.java index 8457a7d..2c4ece8 100644 --- a/src/android/location/TripDiaryStateMachineServiceOngoing.java +++ b/src/android/location/TripDiaryStateMachineServiceOngoing.java @@ -13,6 +13,9 @@ import java.util.LinkedList; import java.util.List; +import org.json.JSONException; +import org.json.JSONObject; + import edu.berkeley.eecs.emission.cordova.tracker.ConfigManager; import edu.berkeley.eecs.emission.cordova.unifiedlogger.NotificationHelper; import edu.berkeley.eecs.emission.R; @@ -48,6 +51,8 @@ public class TripDiaryStateMachineServiceOngoing extends Service { private String mTransition = null; private SharedPreferences mPrefs = null; private ForegroundServiceComm mComm = null; + private JSONObject config; + private boolean isFleet = false; public TripDiaryStateMachineServiceOngoing() { super(); @@ -57,6 +62,16 @@ public TripDiaryStateMachineServiceOngoing() { public void onCreate() { Log.i(this, TAG, "Service created. Initializing one-time variables!"); mComm = new ForegroundServiceComm(this); + + try { + JSONObject c = (JSONObject) UserCacheFactory.getUserCache(this).getDocument("config/app_ui_config", false); + config = c; + isFleet = (config != null && config.has("tracking") && config.getJSONObject("tracking").getBoolean("bluetooth_only")); + } catch (JSONException e) { + Log.d(this, TAG, "Error reading config! " + e); + // TODO: Need to figure out what to do about the fleet flag when the config is invalid + // Original implementation by @louisg1337 had isFleet = true in that case (location tracking would not stop) + } } @Override @@ -158,9 +173,17 @@ private void handleStart(Context ctxt, String actionString) { Log.d(this, TAG, "TripDiaryStateMachineReceiver handleStarted(" + actionString + ") called"); // Get current location if (actionString.equals(ctxt.getString(R.string.transition_initialize)) && - !mCurrState.equals(ctxt.getString(R.string.state_tracking_stopped))) { - startEverything(ctxt, actionString); - } + !mCurrState.equals(ctxt.getString(R.string.state_tracking_stopped))) { + if (isFleet) { + // Start up the bluetooth service to check for beacons + Intent foregroundStartBluetooth = new Intent(ctxt, TripDiaryStateMachineForegroundService.class); + foregroundStartBluetooth.setAction("foreground_start_bluetooth"); + ctxt.startService(foregroundStartBluetooth); + setNewState(ctxt.getString(R.string.state_waiting_for_trip_start)); + } else { + startEverything(ctxt, actionString); + } + } if (actionString.equals(ctxt.getString(R.string.transition_stop_tracking))) { // Haven't started anything yet (that's why we are in the start state). // just move to the stop tracking state @@ -185,9 +208,11 @@ private void handleStart(Context ctxt, String actionString) { // If we stop tracking, we stop everything // For everything else, go to the ongoing state :) private void handleWaitingForTripStart(final Context ctxt, final String actionString) { - if (actionString.equals(getString(R.string.transition_exited_geofence))) { + if (actionString.equals(getString(R.string.transition_exited_geofence)) && !isFleet) { startEverything(ctxt, actionString); - } else if (actionString.equals(getString(R.string.transition_start_tracking))) { + } else if (actionString.equals(ctxt.getString(R.string.transition_ble_beacon_found)) && isFleet) { + startEverything(ctxt, actionString); + } else if (actionString.equals(getString(R.string.transition_start_tracking)) && !isFleet) { startEverything(ctxt, actionString); } else if (actionString.equals(ctxt.getString(R.string.transition_stop_tracking))) { // Haven't started anything yet (that's why we are in the start state). @@ -220,7 +245,11 @@ private void handleOngoing(Context ctxt, String actionString) { private void handleTrackingStopped(final Context ctxt, String actionString) { Log.d(this, TAG, "TripDiaryStateMachineReceiver handleTrackingStopped(" + actionString + ") called"); if (actionString.equals(ctxt.getString(R.string.transition_start_tracking))) { - startEverything(ctxt, actionString); + if (isFleet) { + setNewState(ctxt.getString(R.string.state_waiting_for_trip_start)); + } else { + startEverything(ctxt, actionString); + } } if (actionString.equals(ctxt.getString(R.string.transition_tracking_error))) { Log.i(this, TAG, "Tracking manually turned off, no need to prompt for location"); diff --git a/src/ios/Location/TripDiaryStateMachine.m b/src/ios/Location/TripDiaryStateMachine.m index 787f5aa..c04f739 100644 --- a/src/ios/Location/TripDiaryStateMachine.m +++ b/src/ios/Location/TripDiaryStateMachine.m @@ -129,7 +129,7 @@ - (id) init { // currently only in `BEMDataCollection initWithConsent` // https://github.com/e-mission/e-mission-docs/issues/735#issuecomment-1179774103 - if (![ConfigManager instance].is_duty_cycling && self.currState != kTrackingStoppedState) { + if (![ConfigManager instance].is_duty_cycling && self.currState != kTrackingStoppedState && !self.isFleet) { /* If we are not using geofencing, and the tracking is not manually turned off, then we don't need to listen to any transitions. We just turn on the tracking here and never stop. Turning off all transitions makes it easier for us to ignore silent push as well as the transitions generated from here. @@ -240,7 +240,7 @@ -(void) handleStart:(NSString*) transition withUserInfo:(NSDictionary*) userInfo // Start location services so that we can get the current location // We will receive the first location asynchronously [TripDiaryActions createGeofenceHere:self.locMgr withGeofenceLocator:_geofenceLocator inState:self.currState]; - } else { + } else if (!self.isFleet) { // Technically, we don't need this since we move to the ongoing state in the init code. // but if tracking is stopped, we can skip that, and then if we turn it on again, we // need to turn everything on here as well @@ -261,11 +261,9 @@ -(void) handleStart:(NSString*) transition withUserInfo:(NSDictionary*) userInfo [TripDiaryActions deleteGeofence:self.locMgr]; } else { [LocalNotificationManager addNotification:[NSString stringWithFormat: - @"Got transition %@ in state %@ with fleet mode, not sure why this happened, starting location tracking anyway", + @"ERROR: Got transition %@ in state %@ without fleet mode", transition, [TripDiaryStateMachine getStateName:self.currState]]]; - [TripDiaryActions startTracking:transition withLocationMgr:self.locMgr]; - [TripDiaryActions deleteGeofence:self.locMgr]; } [[NSNotificationCenter defaultCenter] postNotificationName:CFCTransitionNotificationName @@ -361,11 +359,9 @@ - (void) handleWaitingForTripStart:(NSString*) transition withUserInfo:(NSDicti [TripDiaryActions deleteGeofence:self.locMgr]; } else { [LocalNotificationManager addNotification:[NSString stringWithFormat: - @"Got transition %@ in state %@ with fleet mode, not sure why this happened, starting location tracking anyway", + @"ERROR: Got transition %@ in state %@ without fleet mode", transition, [TripDiaryStateMachine getStateName:self.currState]]]; - [TripDiaryActions startTracking:transition withLocationMgr:self.locMgr]; - [TripDiaryActions deleteGeofence:self.locMgr]; } [[NSNotificationCenter defaultCenter] postNotificationName:CFCTransitionNotificationName