Skip to content

Commit 3103382

Browse files
committed
Merge pull request #1344 from garvankeeley/1338-sensortests-followup
Follow up patch to #1338 PR
2 parents b9db362 + 01f6e05 commit 3103382

File tree

3 files changed

+44
-41
lines changed

3 files changed

+44
-41
lines changed

android/src/main/java/org/mozilla/mozstumbler/service/stumblerthread/motiondetection/LocationChangeSensor.java

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,23 @@ public class LocationChangeSensor extends BroadcastReceiver {
3333
private final Context mContext;
3434
private final Handler mHandler = new Handler();
3535
private ISystemClock sysClock;
36-
3736
private int mPrefMotionChangeDistanceMeters;
3837
private long mPrefMotionChangeTimeWindowMs;
3938
private long mStartTimeMs;
4039
private boolean mDoSingleLocationCheck;
4140
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;
4642

4743
private final Runnable mCheckTimeout = new Runnable() {
4844
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;
6050
}
51+
Log.d(LOG_TAG, "We're getting GPS readings in a timely manner. Nothing to see here.");
52+
scheduleTimeoutCheck(mPrefMotionChangeTimeWindowMs);
6153
}
6254
};
6355

@@ -111,11 +103,12 @@ public void start() {
111103
IntentFilter intentFilter = new IntentFilter();
112104
intentFilter.addAction(GPSScanner.ACTION_GPS_UPDATED);
113105
LocalBroadcastManager.getInstance(mContext).registerReceiver(this, intentFilter);
114-
mHandler.postDelayed(mCheckTimeout, mPrefMotionChangeTimeWindowMs);
106+
107+
scheduleTimeoutCheck(mPrefMotionChangeTimeWindowMs);
115108
}
116109

117110
public void stop() {
118-
mHandler.removeCallbacks(mCheckTimeout);
111+
removeTimeoutCheck();
119112
try {
120113
LocalBroadcastManager.getInstance(mContext).unregisterReceiver(this);
121114
} catch (Exception e) {}
@@ -162,20 +155,28 @@ public void onReceive(Context context, Intent intent) {
162155
}
163156

164157
mDoSingleLocationCheck = false;
165-
scheduleTimeoutCheck();
158+
scheduleTimeoutCheck(mPrefMotionChangeTimeWindowMs);
166159
}
167160

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;
169174
try {
170175
mHandler.removeCallbacks(mCheckTimeout);
176+
wasScheduled = true;
171177
} catch (Exception e) {}
172178

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;
179180
}
180181

181182
public void quickCheckForFalsePositiveAfterMotionSensorMovement() {
@@ -187,6 +188,11 @@ public void quickCheckForFalsePositiveAfterMotionSensorMovement() {
187188
mDoSingleLocationCheck = true;
188189
Log.d(LOG_TAG, "Scheduled timeout check for " + (kWaitTimeMs/1000) + " seconds");
189190

190-
mHandler.postDelayed(mCheckTimeout, kWaitTimeMs);
191+
scheduleTimeoutCheck(kWaitTimeMs);
191192
}
193+
194+
Location testing_getLastLocation() {
195+
return mLastLocation;
196+
}
197+
192198
}

android/src/main/java/org/mozilla/mozstumbler/service/stumblerthread/scanners/cellscanner/CellInfo.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ void setNetworkOperator(String mccMnc) {
269269
}
270270

271271
static String getCellRadioTypeName(int networkType) {
272-
Log.d(LOG_TAG, "getCellRadioTypeName("+networkType+")");
273272
switch (networkType) {
274273
// If the network is either GSM or any high-data-rate variant of it, the radio
275274
// field should be specified as `gsm`. This includes `GSM`, `EDGE` and `GPRS`.

android/src/test/java/org/mozilla/mozstumbler/service/stumblerthread/motiondetection/LocationChangeSensorTest.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class LocationChangeSensorTest {
3737

3838
private static final String LOG_TAG = AppGlobals.makeLogTag(LocationChangeSensorTest.class);
3939
private LocationChangeSensor locationChangeSensor;
40-
4140
private LinkedList<Intent> receivedIntent = new LinkedList<Intent>();
4241

4342
// After DetectUnchangingLocation reports the user is not moving, and the scanning pauses,
@@ -92,11 +91,11 @@ public void testStartMotionDetectionAfterFirstGPSLock() {
9291
// we are waiting for mPrefMotionChangeTimeWindowMs for the next GPS signal or else
9392
// we go to sleep.
9493

95-
assertFalse(locationChangeSensor.checkTimeScheduled);
94+
locationChangeSensor.removeTimeoutCheck();
9695

9796
Intent intent = getLocationIntent(0, 0);
9897
locationChangeSensor.onReceive(ctx, intent);
99-
assertTrue(locationChangeSensor.checkTimeScheduled);
98+
assertTrue(locationChangeSensor.removeTimeoutCheck());
10099
}
101100

102101
@Test
@@ -110,19 +109,21 @@ public void testNotFirstGPSFix_MovedDistance() {
110109
// but the person hasn't actually moved geographically. Keep the scanners on
111110
// and schedule the next timeout check to see if the user just stops moving around.
112111

113-
assertFalse(locationChangeSensor.checkTimeScheduled);
114-
115112
intent = getLocationIntent(20, 30);
116113
expectedPosition = intent.getParcelableExtra(GPSScanner.NEW_LOCATION_ARG_LOCATION);
117114
locationChangeSensor.onReceive(ctx, intent);
118-
assertEquals(expectedPosition, locationChangeSensor.mLastLocation);
115+
assertEquals(expectedPosition, locationChangeSensor.testing_getLastLocation());
119116

120117
intent = getLocationIntent(21, 30);
121118
locationChangeSensor.onReceive(ctx, intent);
122-
123119
// The new recorded position should be 21, 30
124120
expectedPosition = intent.getParcelableExtra(GPSScanner.NEW_LOCATION_ARG_LOCATION);
125-
assertEquals(expectedPosition, locationChangeSensor.mLastLocation);
121+
assertEquals(expectedPosition, locationChangeSensor.testing_getLastLocation());
122+
123+
intent = getLocationIntent(21.000001, 30);
124+
locationChangeSensor.onReceive(ctx, intent);
125+
// The new recorded position should be unchanged, movement too small
126+
assertEquals(expectedPosition, locationChangeSensor.testing_getLastLocation());
126127
}
127128

128129
@Test
@@ -136,17 +137,14 @@ public void testNotFirstGPSFix_LongWaitForSecondFix() {
136137
Intent intent;
137138
Location expectedPosition;
138139

139-
140140
Robolectric.runUiThreadTasksIncludingDelayedTasks();
141141

142-
assertFalse(locationChangeSensor.checkTimeScheduled);
143-
144142
intent = getLocationIntent(20, 30);
145143
expectedPosition = intent.getParcelableExtra(GPSScanner.NEW_LOCATION_ARG_LOCATION);
146144
locationChangeSensor.onReceive(ctx, intent);
147145
Robolectric.runUiThreadTasksIncludingDelayedTasks();
148146

149-
assertEquals(expectedPosition, locationChangeSensor.mLastLocation);
147+
assertEquals(expectedPosition, locationChangeSensor.testing_getLastLocation());
150148

151149
intent = getLocationIntent(20.01, 30.01);
152150

@@ -156,7 +154,7 @@ public void testNotFirstGPSFix_LongWaitForSecondFix() {
156154
Robolectric.runUiThreadTasksIncludingDelayedTasks();
157155

158156
// The new recorded position should not be changed!
159-
assertEquals(expectedPosition, locationChangeSensor.mLastLocation);
157+
assertEquals(expectedPosition, locationChangeSensor.testing_getLastLocation());
160158

161159
boolean foundIntent = false;
162160

0 commit comments

Comments
 (0)