@@ -138,12 +138,31 @@ static class NetworkLocationChangeDetector {
138
138
Context mContext ;
139
139
private static final float DIST_THRESHOLD_M = 30.0f ;
140
140
private static final long TIME_INTERVAL_MS = 30000 ;
141
+ private Location mLastLocation ;
141
142
142
143
private LocationListener mNetworkLocationListener = new LocationListener () {
143
144
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)" );
145
160
Intent sendIntent = new Intent (ACTION_USER_MOTION_DETECTED );
146
161
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 ;
147
166
}
148
167
149
168
public void onStatusChanged (String provider , int status , Bundle extras ) {}
@@ -153,12 +172,13 @@ public void onProviderDisabled(String provider) {}
153
172
154
173
public void start (Context c ) {
155
174
mContext = c ;
175
+ mLastLocation = null ;
156
176
LocationManager lm = (LocationManager ) mContext .getSystemService (Context .LOCATION_SERVICE );
157
177
if (!lm .getAllProviders ().contains (LocationManager .NETWORK_PROVIDER )) {
158
178
return ;
159
179
}
160
180
161
- lm .requestLocationUpdates (LocationManager .NETWORK_PROVIDER , TIME_INTERVAL_MS , DIST_THRESHOLD_M , mNetworkLocationListener );
181
+ lm .requestLocationUpdates (LocationManager .NETWORK_PROVIDER , TIME_INTERVAL_MS , 0 , mNetworkLocationListener );
162
182
}
163
183
164
184
public void stop () {
0 commit comments