Skip to content

Commit f86ad6d

Browse files
committed
Merge pull request #1595 from garvankeeley/1579-net-geo-motion-sensor
Use the 2nd network location changed as a trigger for movement
2 parents 5f8aae2 + 9f8ab42 commit f86ad6d

File tree

1 file changed

+22
-2
lines changed
  • libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/stumblerthread/motiondetection

1 file changed

+22
-2
lines changed

libraries/stumbler/src/main/java/org/mozilla/mozstumbler/service/stumblerthread/motiondetection/MotionSensor.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,31 @@ static class NetworkLocationChangeDetector {
138138
Context mContext;
139139
private static final float DIST_THRESHOLD_M = 30.0f;
140140
private static final long TIME_INTERVAL_MS = 30000;
141+
private Location mLastLocation;
141142

142143
private LocationListener mNetworkLocationListener = new LocationListener() {
143144
public void onLocationChanged(Location location) {
144-
AppGlobals.guiLogInfo("MotionSensor.NetworkLocationChangeDetector triggered.");
145+
if (mLastLocation == null) {
146+
// Use the first location change to init the location. This means an initial movement
147+
// can be missed, and a subsequent movement is required. However this is the most reliable
148+
// means of initializing the location.
149+
mLastLocation = location;
150+
return;
151+
}
152+
153+
final int distanceMeters = Math.round(location.distanceTo(mLastLocation));
154+
if (distanceMeters < DIST_THRESHOLD_M) {
155+
// The threshold set in requestLocationUpdates is unreliable, I have seen false triggers, check here instead
156+
return;
157+
}
158+
159+
AppGlobals.guiLogInfo("MotionSensor.NetworkLocationChangeDetector triggered. (" + distanceMeters + "m)");
145160
Intent sendIntent = new Intent(ACTION_USER_MOTION_DETECTED);
146161
LocalBroadcastManager.getInstance(mContext).sendBroadcastSync(sendIntent);
162+
163+
// Under expected usage, after the motion detection broadcast, this class goes through a stop()/start() cycle,
164+
// which sets mLastLocation to null. In case someone wants to use this class differently, we update mLastLocation here.
165+
mLastLocation = location;
147166
}
148167

149168
public void onStatusChanged(String provider, int status, Bundle extras) {}
@@ -153,12 +172,13 @@ public void onProviderDisabled(String provider) {}
153172

154173
public void start(Context c) {
155174
mContext = c;
175+
mLastLocation = null;
156176
LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
157177
if (!lm.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) {
158178
return;
159179
}
160180

161-
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_INTERVAL_MS, DIST_THRESHOLD_M, mNetworkLocationListener);
181+
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_INTERVAL_MS, 0, mNetworkLocationListener);
162182
}
163183

164184
public void stop() {

0 commit comments

Comments
 (0)