Commit 3f0594c0 authored by Chris Scott's avatar Chris Scott

Implement stationaryAlarm to detect user stopped rather than passively...

Implement stationaryAlarm to detect user stopped rather than passively calculating in onLocationChanged, since onLocationChanged won't fire until distanceFilter is surpassed
parent 27b8132e
...@@ -12,6 +12,7 @@ import com.tenforwardconsulting.cordova.bgloc.data.DAOFactory; ...@@ -12,6 +12,7 @@ import com.tenforwardconsulting.cordova.bgloc.data.DAOFactory;
import com.tenforwardconsulting.cordova.bgloc.data.LocationDAO; import com.tenforwardconsulting.cordova.bgloc.data.LocationDAO;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.ToneGenerator; import android.media.ToneGenerator;
import android.telephony.PhoneStateListener; import android.telephony.PhoneStateListener;
...@@ -48,8 +49,9 @@ import static java.lang.Math.*; ...@@ -48,8 +49,9 @@ import static java.lang.Math.*;
public class LocationUpdateService extends Service implements LocationListener { public class LocationUpdateService extends Service implements LocationListener {
private static final String TAG = "LocationUpdateService"; private static final String TAG = "LocationUpdateService";
private static final String STATIONARY_REGION_ACTION = "com.tenforwardconsulting.cordova.bgloc.STATIONARY_REGION_ACTION"; private static final String STATIONARY_REGION_ACTION = "com.tenforwardconsulting.cordova.bgloc.STATIONARY_REGION_ACTION";
private static final String STATIONARY_ALARM_ACTION = "com.tenforwardconsulting.cordova.bgloc.STATIONARY_ALARM_ACTION";
private static final String SINGLE_LOCATION_UPDATE_ACTION = "com.tenforwardconsulting.cordova.bgloc.SINGLE_LOCATION_UPDATE_ACTION"; private static final String SINGLE_LOCATION_UPDATE_ACTION = "com.tenforwardconsulting.cordova.bgloc.SINGLE_LOCATION_UPDATE_ACTION";
private static long STATIONARY_TIMEOUT = 60 * 1000 * 10; private static long STATIONARY_TIMEOUT = 60 * 1000 * 1;
private PowerManager.WakeLock wakeLock; private PowerManager.WakeLock wakeLock;
private Location lastLocation; private Location lastLocation;
...@@ -60,6 +62,8 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -60,6 +62,8 @@ public class LocationUpdateService extends Service implements LocationListener {
private float stationaryRadius; private float stationaryRadius;
private Location stationaryLocation; private Location stationaryLocation;
private PendingIntent stationaryAlarmPI;
private Integer desiredAccuracy; private Integer desiredAccuracy;
private Integer distanceFilter; private Integer distanceFilter;
private Integer scaledDistanceFilter; private Integer scaledDistanceFilter;
...@@ -71,6 +75,7 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -71,6 +75,7 @@ public class LocationUpdateService extends Service implements LocationListener {
private PendingIntent proximityPI; private PendingIntent proximityPI;
private LocationManager locationManager; private LocationManager locationManager;
private AlarmManager alarmManager;
private ConnectivityManager connectivityManager; private ConnectivityManager connectivityManager;
private Criteria criteria; private Criteria criteria;
...@@ -94,6 +99,9 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -94,6 +99,9 @@ public class LocationUpdateService extends Service implements LocationListener {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
stationaryAlarmPI = PendingIntent.getBroadcast(this, 0, new Intent(STATIONARY_ALARM_ACTION), 0);
registerReceiver(stationaryAlarmReceiver, new IntentFilter(STATIONARY_ALARM_ACTION));
connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
...@@ -132,15 +140,15 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -132,15 +140,15 @@ public class LocationUpdateService extends Service implements LocationListener {
} }
Toast.makeText(this, "Background location tracking started", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Background location tracking started", Toast.LENGTH_SHORT).show();
this.setPace(false); this.setPace(true);
/** /**
* Experimental cell-location-change handler * Experimental cell-location-change handler
* *
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, LISTEN_CELL_LOCATION); telephonyManager.listen(phoneStateListener, LISTEN_CELL_LOCATION);
* *
*/ */
//We want this service to continue running until it is explicitly //We want this service to continue running until it is explicitly
// stopped, so return sticky. // stopped, so return sticky.
...@@ -195,6 +203,15 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -195,6 +203,15 @@ public class LocationUpdateService extends Service implements LocationListener {
criteria.setHorizontalAccuracy(translateDesiredAccuracy(desiredAccuracy)); criteria.setHorizontalAccuracy(translateDesiredAccuracy(desiredAccuracy));
criteria.setPowerRequirement(Criteria.POWER_HIGH); criteria.setPowerRequirement(Criteria.POWER_HIGH);
locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), locationTimeout*1000, scaledDistanceFilter, this); locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), locationTimeout*1000, scaledDistanceFilter, this);
// Configure stationary alarm timout. If no location update within configured timeout, tone-down tracking.
/*
if (stationaryAlarmPI != null) {
alarmManager.cancel(stationaryAlarmPI);
stationaryAlarmPI = null;
}
*/
resetStationaryAlarm();
} else { } else {
stationaryLocation = null; stationaryLocation = null;
criteria.setAccuracy(Criteria.ACCURACY_COARSE); criteria.setAccuracy(Criteria.ACCURACY_COARSE);
...@@ -208,6 +225,10 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -208,6 +225,10 @@ public class LocationUpdateService extends Service implements LocationListener {
} }
} }
public void resetStationaryAlarm() {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + STATIONARY_TIMEOUT, stationaryAlarmPI); // Millisec * Second * Minute
}
/** /**
* Returns the most accurate and timely previously detected location. * Returns the most accurate and timely previously detected location.
* Where the last result is beyond the specified maximum distance or * Where the last result is beyond the specified maximum distance or
...@@ -257,20 +278,8 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -257,20 +278,8 @@ public class LocationUpdateService extends Service implements LocationListener {
// If user hasn't moved beyond the stationaryRadius within time of STATIONARY_TIMEOUT // If user hasn't moved beyond the stationaryRadius within time of STATIONARY_TIMEOUT
// assume they've stopped. // assume they've stopped.
if (lastLocation != null) { if (lastLocation != null) {
Float stationaryDistance = (location.getAccuracy() < stationaryRadius) ? stationaryRadius : location.getAccuracy(); if (lastLocation.distanceTo(location) > stationaryRadius) {
Log.i(TAG, "- has lastLocation " + lastLocation.distanceTo(location) + "<" + stationaryDistance); resetStationaryAlarm();
if (lastLocation.distanceTo(location) < stationaryDistance) {
Log.i(TAG, "- lastLocation is within stationaryRadius");
if (stationaryLocation == null) {
stationaryLocation = lastLocation;
}
long timeDelta = location.getTime() - stationaryLocation.getTime();
Log.i(TAG, "- timeDelta: " + timeDelta + ">" + STATIONARY_TIMEOUT);
if (timeDelta > STATIONARY_TIMEOUT) {
setPace(false);
}
} else {
stationaryLocation = null;
Integer newDistanceFilter = calculateDistanceFilter(location.getSpeed()); Integer newDistanceFilter = calculateDistanceFilter(location.getSpeed());
if (newDistanceFilter != scaledDistanceFilter) { if (newDistanceFilter != scaledDistanceFilter) {
Log.i(TAG, "- updated distanceFilter, new: " + newDistanceFilter + ", old: " + scaledDistanceFilter); Log.i(TAG, "- updated distanceFilter, new: " + newDistanceFilter + ", old: " + scaledDistanceFilter);
...@@ -324,17 +333,27 @@ public class LocationUpdateService extends Service implements LocationListener { ...@@ -324,17 +333,27 @@ public class LocationUpdateService extends Service implements LocationListener {
proximityPI = PendingIntent.getBroadcast(this, 0, intent, 0); proximityPI = PendingIntent.getBroadcast(this, 0, intent, 0);
locationManager.addProximityAlert( locationManager.addProximityAlert(
location.getLatitude(), location.getLatitude(),
location.getLongitude(), location.getLongitude(),
stationaryRadius, stationaryRadius,
-1, -1,
proximityPI proximityPI
); );
IntentFilter filter = new IntentFilter(STATIONARY_REGION_ACTION); IntentFilter filter = new IntentFilter(STATIONARY_REGION_ACTION);
registerReceiver(stationaryRegionReceiver, filter); registerReceiver(stationaryRegionReceiver, filter);
} }
private BroadcastReceiver stationaryAlarmReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
// Put here YOUR code.
Log.i(TAG, "- stationaryAlarm fired");
setPace(false);
}
};
private BroadcastReceiver stationaryRegionReceiver = new BroadcastReceiver() { private BroadcastReceiver stationaryRegionReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
......
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