Merge changes Iab3f9914,I5d5da5e7

* changes:
  Average scan results for AccessPoints.
  Fix broken AccessPointTests.
This commit is contained in:
TreeHugger Robot
2017-03-17 23:57:06 +00:00
committed by Android (Google) Code Review
3 changed files with 46 additions and 17 deletions

View File

@@ -394,8 +394,9 @@ public class AccessPoint implements Comparable<AccessPoint> {
*
* <p>If the given connection is active, the existing value of {@link #mRssi} will be returned.
* If the given AccessPoint is not active, a value will be calculated from previous scan
* results, returning the best RSSI for all matching AccessPoints. If the access point is not
* connected and there are no scan results, the rssi will be set to {@link #UNREACHABLE_RSSI}.
* results, returning the best RSSI for all matching AccessPoints averaged with the previous
* value. If the access point is not connected and there are no scan results, the rssi will be
* set to {@link #UNREACHABLE_RSSI}.
*
* <p>Old scan results will be evicted from the cache when this method is invoked.
*/
@@ -413,7 +414,11 @@ public class AccessPoint implements Comparable<AccessPoint> {
}
}
mRssi = rssi;
if (rssi != UNREACHABLE_RSSI && mRssi != UNREACHABLE_RSSI) {
mRssi = (mRssi + rssi) / 2; // half-life previous value
} else {
mRssi = rssi;
}
}
/**

View File

@@ -195,6 +195,26 @@ public class AccessPointTest {
assertThat(ap.getRssi()).isEqualTo(newRssi);
}
@Test
public void testUpdateWithScanResultShouldAverageRssi() {
String ssid = "ssid";
int originalRssi = -65;
int newRssi = -80;
int expectedRssi = (originalRssi + newRssi) / 2;
AccessPoint ap =
new TestAccessPointBuilder(mContext).setSsid(ssid).setRssi(originalRssi).build();
ScanResult scanResult = new ScanResult();
scanResult.SSID = ssid;
scanResult.level = newRssi;
scanResult.BSSID = "bssid";
scanResult.timestamp = SystemClock.elapsedRealtime() * 1000;
scanResult.capabilities = "";
assertThat(ap.update(scanResult)).isTrue();
assertThat(ap.getRssi()).isEqualTo(expectedRssi);
}
private AccessPoint createAccessPointWithScanResultCache() {
Bundle bundle = new Bundle();
ArrayList<ScanResult> scanResults = new ArrayList<>();
@@ -203,6 +223,7 @@ public class AccessPointTest {
scanResult.level = i;
scanResult.BSSID = "bssid-" + i;
scanResult.timestamp = SystemClock.elapsedRealtime() * 1000;
scanResult.capabilities = "";
scanResults.add(scanResult);
}

View File

@@ -73,25 +73,28 @@ public class TestAccessPointBuilder {
return this;
}
public TestAccessPointBuilder setRssi(int rssi) {
mRssi = rssi;
return this;
}
/**
* Set the signal level.
* Side effect: if this AccessPoint was previously unreachable,
* Set the rssi based upon the desired signal level.
*
* <p>Side effect: if this AccessPoint was previously unreachable,
* setting the level will also make it reachable.
*/
public TestAccessPointBuilder setLevel(int level) {
int outputRange = AccessPoint.SIGNAL_LEVELS - 1;
if (level > outputRange) {
level = outputRange;
} else if (level < 0) {
level = 0;
// Reversal of WifiManager.calculateSignalLevels
if (level == 0) {
mRssi = MIN_RSSI;
} else if (level >= AccessPoint.SIGNAL_LEVELS) {
mRssi = MAX_RSSI;
} else {
float inputRange = MAX_RSSI - MIN_RSSI;
float outputRange = AccessPoint.SIGNAL_LEVELS - 1;
mRssi = (int) (level * inputRange / outputRange + MIN_RSSI);
}
int inputRange = MAX_RSSI - MIN_RSSI;
// calculate the rssi required to get the level we want.
// this is a rearrangement of the formula from WifiManager.calculateSignalLevel()
mRssi = (int)((float)(level * inputRange) / (float)outputRange) + MIN_RSSI;
return this;
}