@@ -33,31 +33,23 @@ public class LocationChangeSensor extends BroadcastReceiver {
33
33
private final Context mContext ;
34
34
private final Handler mHandler = new Handler ();
35
35
private ISystemClock sysClock ;
36
-
37
36
private int mPrefMotionChangeDistanceMeters ;
38
37
private long mPrefMotionChangeTimeWindowMs ;
39
38
private long mStartTimeMs ;
40
39
private boolean mDoSingleLocationCheck ;
41
40
public static String ACTION_LOCATION_NOT_CHANGING = AppGlobals .ACTION_NAMESPACE + ".LOCATION_UNCHANGING" ;
42
-
43
- // attributes used to track the state of the LocationChangeSensor
44
- boolean checkTimeScheduled = false ;
45
- Location mLastLocation ;
41
+ private Location mLastLocation ;
46
42
47
43
private final Runnable mCheckTimeout = new Runnable () {
48
44
public void run () {
49
- try {
50
- if (isTimeWindowForMovementExceeded ()) {
51
- AppGlobals .guiLogInfo ("No GPS in time window." );
52
- Log .d (LOG_TAG , "No GPS in time window." );
53
- LocalBroadcastManager .getInstance (mContext ).sendBroadcastSync (new Intent (ACTION_LOCATION_NOT_CHANGING ));
54
- return ;
55
- }
56
- Log .d (LOG_TAG , "We're getting GPS readings in a timely manner. Nothing to see here." );
57
- scheduleTimeoutCheck ();
58
- } finally {
59
- checkTimeScheduled = false ;
45
+ if (isTimeWindowForMovementExceeded ()) {
46
+ AppGlobals .guiLogInfo ("No GPS in time window." );
47
+ Log .d (LOG_TAG , "No GPS in time window." );
48
+ LocalBroadcastManager .getInstance (mContext ).sendBroadcastSync (new Intent (ACTION_LOCATION_NOT_CHANGING ));
49
+ return ;
60
50
}
51
+ Log .d (LOG_TAG , "We're getting GPS readings in a timely manner. Nothing to see here." );
52
+ scheduleTimeoutCheck (mPrefMotionChangeTimeWindowMs );
61
53
}
62
54
};
63
55
@@ -111,11 +103,12 @@ public void start() {
111
103
IntentFilter intentFilter = new IntentFilter ();
112
104
intentFilter .addAction (GPSScanner .ACTION_GPS_UPDATED );
113
105
LocalBroadcastManager .getInstance (mContext ).registerReceiver (this , intentFilter );
114
- mHandler .postDelayed (mCheckTimeout , mPrefMotionChangeTimeWindowMs );
106
+
107
+ scheduleTimeoutCheck (mPrefMotionChangeTimeWindowMs );
115
108
}
116
109
117
110
public void stop () {
118
- mHandler . removeCallbacks ( mCheckTimeout );
111
+ removeTimeoutCheck ( );
119
112
try {
120
113
LocalBroadcastManager .getInstance (mContext ).unregisterReceiver (this );
121
114
} catch (Exception e ) {}
@@ -162,20 +155,28 @@ public void onReceive(Context context, Intent intent) {
162
155
}
163
156
164
157
mDoSingleLocationCheck = false ;
165
- scheduleTimeoutCheck ();
158
+ scheduleTimeoutCheck (mPrefMotionChangeTimeWindowMs );
166
159
}
167
160
168
- private void scheduleTimeoutCheck () {
161
+ private void scheduleTimeoutCheck (long delay ) {
162
+ removeTimeoutCheck ();
163
+
164
+ // Don't schedule it for an exact delay, we want it slightly after this timeout, as the OS can
165
+ // trigger this earlier than requested (by a fraction of a second).
166
+ final long addedDelay = 2 * 1000 ;
167
+ mHandler .postDelayed (mCheckTimeout , delay + addedDelay );
168
+
169
+ Log .d (LOG_TAG , "Scheduled timeout check for " + (delay / 1000 ) + " seconds" );
170
+ }
171
+
172
+ boolean removeTimeoutCheck () {
173
+ boolean wasScheduled = false ;
169
174
try {
170
175
mHandler .removeCallbacks (mCheckTimeout );
176
+ wasScheduled = true ;
171
177
} catch (Exception e ) {}
172
178
173
- // Don't schedule it for exactly mPrefMotionChangeTimeWindowMs, we want it slightly after this timeout
174
- final long addedDelay = 2 * 1000 ;
175
- Log .d (LOG_TAG , "Scheduled timeout check for " + (addedDelay /1000 ) + " seconds" );
176
-
177
- checkTimeScheduled = true ;
178
- mHandler .postDelayed (mCheckTimeout , mPrefMotionChangeTimeWindowMs + addedDelay );
179
+ return wasScheduled ;
179
180
}
180
181
181
182
public void quickCheckForFalsePositiveAfterMotionSensorMovement () {
@@ -187,6 +188,11 @@ public void quickCheckForFalsePositiveAfterMotionSensorMovement() {
187
188
mDoSingleLocationCheck = true ;
188
189
Log .d (LOG_TAG , "Scheduled timeout check for " + (kWaitTimeMs /1000 ) + " seconds" );
189
190
190
- mHandler . postDelayed ( mCheckTimeout , kWaitTimeMs );
191
+ scheduleTimeoutCheck ( kWaitTimeMs );
191
192
}
193
+
194
+ Location testing_getLastLocation () {
195
+ return mLastLocation ;
196
+ }
197
+
192
198
}
0 commit comments