Commit c6b59b5c authored by Chris Scott's avatar Chris Scott

Implement new config stopTimeout for delaying GPS OFF after ARS detects STILL....

Implement new config stopTimeout for delaying GPS OFF after ARS detects STILL.  Use this for when concerned about vehicles stopped at traffic lights
parent b587113d
...@@ -279,6 +279,10 @@ An interval of 0 is allowed, but not recommended, since location updates may be ...@@ -279,6 +279,10 @@ An interval of 0 is allowed, but not recommended, since location updates may be
the desired time between activity detections. Larger values will result in fewer activity detections while improving battery life. A value of 0 will result in activity detections at the fastest possible rate. the desired time between activity detections. Larger values will result in fewer activity detections while improving battery life. A value of 0 will result in activity detections at the fastest possible rate.
#####`@param {Integer minutes} stopTimeout`
The number of miutes to wait before turning off the GPS after the ActivityRecognition System (ARS) detects the device is ```STILL``` (defaults to 0, no timeout). If you don't set a value, the plugin is eager to turn off the GPS ASAP. An example use-case for this configuration is to delay GPS OFF while in a car waiting at a traffic light.
### iOS Config ### iOS Config
#####`@param {String} activityType [AutomotiveNavigation, OtherNavigation, Fitness, Other]` #####`@param {String} activityType [AutomotiveNavigation, OtherNavigation, Fitness, Other]`
......
...@@ -179,6 +179,7 @@ var app = { ...@@ -179,6 +179,7 @@ var app = {
distanceFilter: 30, distanceFilter: 30,
locationUpdateInterval: 30000, locationUpdateInterval: 30000,
activityRecognitionInterval: 10000, activityRecognitionInterval: 10000,
stopTimeout: 0, // <-- Minutes to wait before turning off GPS after stop-detection.
activityType: 'AutomotiveNavigation', activityType: 'AutomotiveNavigation',
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle. debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false // <-- enable this to clear background location settings when the app terminates stopOnTerminate: false // <-- enable this to clear background location settings when the app terminates
......
package com.transistorsoft.cordova.bggeo; package com.transistorsoft.cordova.bggeo;
import java.util.concurrent.TimeUnit;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaWebView;
...@@ -14,6 +15,7 @@ import com.google.android.gms.common.ConnectionResult; ...@@ -14,6 +15,7 @@ import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition; import com.google.android.gms.location.ActivityRecognition;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity; import com.google.android.gms.location.DetectedActivity;
import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.LocationServices;
...@@ -50,10 +52,17 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati ...@@ -50,10 +52,17 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
private Float distanceFilter = (float) 50; private Float distanceFilter = (float) 50;
private Boolean isDebugging = false; private Boolean isDebugging = false;
private Boolean stopOnTerminate = false; private Boolean stopOnTerminate = false;
// Android-only config // Android-only config
private Integer locationUpdateInterval = 60000; private Integer locationUpdateInterval = 60000;
private Integer activityRecognitionInterval = 60000; private Integer activityRecognitionInterval = 60000;
/**
* @config {Integer} stopTimeout The time to wait after ARS STILL to turn of GPS
*/
private long stopTimeout = 0;
// The elapsed millis when the ARS detected STILL
private long stoppedAt = 0;
// Geolocation callback // Geolocation callback
private CallbackContext locationCallback; private CallbackContext locationCallback;
...@@ -61,7 +70,7 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati ...@@ -61,7 +70,7 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
// Called when DetectedActivity is STILL // Called when DetectedActivity is STILL
private CallbackContext stationaryCallback; private CallbackContext stationaryCallback;
private Location stationaryLocation; private Location stationaryLocation;
private GoogleApiClient googleApiClient; private GoogleApiClient googleApiClient;
private DetectedActivity currentActivity; private DetectedActivity currentActivity;
...@@ -168,6 +177,9 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati ...@@ -168,6 +177,9 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
if (config.has("activityRecognitionInterval")) { if (config.has("activityRecognitionInterval")) {
activityRecognitionInterval = config.getInt("activityRecognitionInterval"); activityRecognitionInterval = config.getInt("activityRecognitionInterval");
} }
if (config.has("stopTimeout")) {
stopTimeout = config.getLong("stopTimeout");
}
if (config.has("debug")) { if (config.has("debug")) {
isDebugging = config.getBoolean("debug"); isDebugging = config.getBoolean("debug");
} }
...@@ -253,16 +265,15 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati ...@@ -253,16 +265,15 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
} }
} }
public void onEventMainThread(DetectedActivity probableActivity) { public void onEventMainThread(ActivityRecognitionResult result) {
String probableActivityName = getActivityName(probableActivity.getType()); currentActivity = result.getMostProbableActivity();
Log.w(TAG, "- DetectedActivity: " + probableActivityName + ", confidence: " + probableActivity.getConfidence()); String probableActivityName = getActivityName(currentActivity.getType());
Log.w(TAG, "- DetectedActivity: " + probableActivityName + ", confidence: " + currentActivity.getConfidence());
currentActivity = probableActivity;
boolean wasMoving = isMoving; boolean wasMoving = isMoving;
boolean nowMoving = false; boolean nowMoving = false;
switch (probableActivity.getType()) { switch (currentActivity.getType()) {
case DetectedActivity.IN_VEHICLE: case DetectedActivity.IN_VEHICLE:
case DetectedActivity.ON_BICYCLE: case DetectedActivity.ON_BICYCLE:
case DetectedActivity.ON_FOOT: case DetectedActivity.ON_FOOT:
...@@ -283,6 +294,24 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati ...@@ -283,6 +294,24 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
boolean justStopped = wasMoving && !nowMoving; boolean justStopped = wasMoving && !nowMoving;
boolean initialState = !nowMoving && (stationaryLocation == null); boolean initialState = !nowMoving && (stationaryLocation == null);
// If we're using a stopTimeout, record the current activity's timestamp.
if (justStopped && stopTimeout > 0 && stoppedAt == 0) {
stoppedAt = result.getElapsedRealtimeMillis();
return;
}
// If we're using a stopTimeout, compare the current activity's timestamp with the 1st recorded STILL event.
if (!nowMoving && stoppedAt > 0) {
long elapsedMillis = result.getElapsedRealtimeMillis() - stoppedAt;
long elapsedMinutes = TimeUnit.MILLISECONDS.toMinutes(elapsedMillis);
Log.i(TAG, "- Waiting for stopTimeout (" + stopTimeout + " min): elapsed min: " + elapsedMinutes);
if (elapsedMinutes == stopTimeout) {
justStopped = true;
} else {
return;
}
}
stoppedAt = 0;
if ( startedMoving || justStopped || initialState ) { if ( startedMoving || justStopped || initialState ) {
setPace(nowMoving); setPace(nowMoving);
} }
......
...@@ -56,7 +56,7 @@ public class BackgroundGeolocationService extends IntentService { ...@@ -56,7 +56,7 @@ public class BackgroundGeolocationService extends IntentService {
} }
// Post activity to the bus. // Post activity to the bus.
EventBus.getDefault().post(probableActivity); EventBus.getDefault().post(result);
} else { } else {
final Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED); final Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
if (location != null) { if (location != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment