Commit e2dfd2d9 authored by Chris Scott's avatar Chris Scott

Merge pull request #5 from christocracy/stop_timeout

Implement new config stopTimeout for delaying GPS OFF after ARS detects ...
parents b587113d c6b59b5c
......@@ -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.
#####`@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
#####`@param {String} activityType [AutomotiveNavigation, OtherNavigation, Fitness, Other]`
......
......@@ -179,6 +179,7 @@ var app = {
distanceFilter: 30,
locationUpdateInterval: 30000,
activityRecognitionInterval: 10000,
stopTimeout: 0, // <-- Minutes to wait before turning off GPS after stop-detection.
activityType: 'AutomotiveNavigation',
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false // <-- enable this to clear background location settings when the app terminates
......
package com.transistorsoft.cordova.bggeo;
import java.util.concurrent.TimeUnit;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
......@@ -14,6 +15,7 @@ import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
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.LocationRequest;
import com.google.android.gms.location.LocationServices;
......@@ -50,10 +52,17 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
private Float distanceFilter = (float) 50;
private Boolean isDebugging = false;
private Boolean stopOnTerminate = false;
// Android-only config
private Integer locationUpdateInterval = 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
private CallbackContext locationCallback;
......@@ -61,7 +70,7 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
// Called when DetectedActivity is STILL
private CallbackContext stationaryCallback;
private Location stationaryLocation;
private GoogleApiClient googleApiClient;
private DetectedActivity currentActivity;
......@@ -168,6 +177,9 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
if (config.has("activityRecognitionInterval")) {
activityRecognitionInterval = config.getInt("activityRecognitionInterval");
}
if (config.has("stopTimeout")) {
stopTimeout = config.getLong("stopTimeout");
}
if (config.has("debug")) {
isDebugging = config.getBoolean("debug");
}
......@@ -253,16 +265,15 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
}
}
public void onEventMainThread(DetectedActivity probableActivity) {
String probableActivityName = getActivityName(probableActivity.getType());
Log.w(TAG, "- DetectedActivity: " + probableActivityName + ", confidence: " + probableActivity.getConfidence());
public void onEventMainThread(ActivityRecognitionResult result) {
currentActivity = result.getMostProbableActivity();
String probableActivityName = getActivityName(currentActivity.getType());
Log.w(TAG, "- DetectedActivity: " + probableActivityName + ", confidence: " + currentActivity.getConfidence());
currentActivity = probableActivity;
boolean wasMoving = isMoving;
boolean nowMoving = false;
switch (probableActivity.getType()) {
switch (currentActivity.getType()) {
case DetectedActivity.IN_VEHICLE:
case DetectedActivity.ON_BICYCLE:
case DetectedActivity.ON_FOOT:
......@@ -283,6 +294,24 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Locati
boolean justStopped = wasMoving && !nowMoving;
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 ) {
setPace(nowMoving);
}
......
......@@ -56,7 +56,7 @@ public class BackgroundGeolocationService extends IntentService {
}
// Post activity to the bus.
EventBus.getDefault().post(probableActivity);
EventBus.getDefault().post(result);
} else {
final Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
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